pc.cpp 400 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903
  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/ers.hpp" // ers_destroy
  11. #include "../common/malloc.hpp"
  12. #include "../common/mmo.hpp" //NAME_LENGTH
  13. #include "../common/nullpo.hpp"
  14. #include "../common/random.hpp"
  15. #include "../common/showmsg.hpp"
  16. #include "../common/socket.hpp" // session[]
  17. #include "../common/strlib.hpp" // safestrncpy()
  18. #include "../common/timer.hpp"
  19. #include "../common/utilities.hpp"
  20. #include "../common/utils.hpp"
  21. #include "achievement.hpp"
  22. #include "atcommand.hpp" // get_atcommand_level()
  23. #include "battle.hpp" // battle_config
  24. #include "battleground.hpp"
  25. #include "buyingstore.hpp" // struct s_buyingstore
  26. #include "channel.hpp"
  27. #include "chat.hpp"
  28. #include "chrif.hpp"
  29. #include "clan.hpp"
  30. #include "clif.hpp"
  31. #include "date.hpp" // is_day_of_*()
  32. #include "duel.hpp"
  33. #include "elemental.hpp"
  34. #include "guild.hpp"
  35. #include "homunculus.hpp"
  36. #include "instance.hpp"
  37. #include "intif.hpp"
  38. #include "itemdb.hpp" // MAX_ITEMGROUP
  39. #include "log.hpp"
  40. #include "map.hpp"
  41. #include "mercenary.hpp"
  42. #include "mob.hpp"
  43. #include "npc.hpp"
  44. #include "party.hpp" // party_search()
  45. #include "pc_groups.hpp"
  46. #include "pet.hpp" // pet_unlocktarget()
  47. #include "quest.hpp"
  48. #include "script.hpp" // struct script_reg, struct script_regstr
  49. #include "searchstore.hpp" // struct s_search_store_info
  50. #include "status.hpp" // OPTION_*, struct weapon_atk
  51. #include "storage.hpp"
  52. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  53. #include "vending.hpp" // struct s_vending
  54. using namespace rathena;
  55. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  56. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  57. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  58. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  59. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  60. static unsigned int statp[MAX_LEVEL+1];
  61. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  62. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  63. #endif
  64. // h-files are for declarations, not for implementations... [Shinomori]
  65. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  66. // timer for night.day implementation
  67. int day_timer_tid = INVALID_TIMER;
  68. int night_timer_tid = INVALID_TIMER;
  69. struct eri *pc_sc_display_ers = NULL;
  70. struct eri *pc_itemgrouphealrate_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. struct s_attendance_reward{
  79. uint16 item_id;
  80. uint16 amount;
  81. };
  82. struct s_attendance_period{
  83. uint32 start;
  84. uint32 end;
  85. std::map<int,struct s_attendance_reward> rewards;
  86. };
  87. std::vector<struct s_attendance_period> attendance_periods;
  88. #define MOTD_LINE_SIZE 128
  89. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  90. bool reg_load;
  91. /**
  92. * Translation table from athena equip index to aegis bitmask
  93. */
  94. unsigned int equip_bitmask[EQI_MAX] = {
  95. EQP_ACC_L, // EQI_ACC_L
  96. EQP_ACC_R, // EQI_ACC_R
  97. EQP_SHOES, // EQI_SHOES
  98. EQP_GARMENT, // EQI_GARMENT
  99. EQP_HEAD_LOW, // EQI_HEAD_LOW
  100. EQP_HEAD_MID, // EQI_HEAD_MID
  101. EQP_HEAD_TOP, // EQI_HEAD_TOP
  102. EQP_ARMOR, // EQI_ARMOR
  103. EQP_HAND_L, // EQI_HAND_L
  104. EQP_HAND_R, // EQI_HAND_R
  105. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  106. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  107. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  108. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  109. EQP_AMMO, // EQI_AMMO
  110. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  111. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  112. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  113. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  114. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  115. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  116. };
  117. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  118. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  119. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  120. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  121. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  122. };
  123. void pc_set_reg_load( bool val ){
  124. reg_load = val;
  125. }
  126. /**
  127. * Item Cool Down Delay Saving
  128. * Struct item_cd is not a member of struct map_session_data
  129. * to keep cooldowns in memory between player log-ins.
  130. * All cooldowns are reset when server is restarted.
  131. **/
  132. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  133. struct item_cd {
  134. unsigned int tick[MAX_ITEMDELAYS]; //tick
  135. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  136. };
  137. /**
  138. * Converts a class to its array index for CLASS_COUNT defined arrays.
  139. * Note that it does not do a validity check for speed purposes, where parsing
  140. * player input make sure to use a pcdb_checkid first!
  141. * @param class_ Job ID see enum e_job
  142. * @return Class Index
  143. */
  144. int pc_class2idx(int class_) {
  145. if (class_ >= JOB_NOVICE_HIGH)
  146. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  147. return class_;
  148. }
  149. /**
  150. * Get player's group ID
  151. * @param sd
  152. * @return Group ID
  153. */
  154. int pc_get_group_id(struct map_session_data *sd) {
  155. return sd->group_id;
  156. }
  157. /** Get player's group Level
  158. * @param sd
  159. * @return Group Level
  160. */
  161. int pc_get_group_level(struct map_session_data *sd) {
  162. return sd->group_level;
  163. }
  164. static TIMER_FUNC(pc_invincible_timer){
  165. struct map_session_data *sd;
  166. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  167. return 1;
  168. if(sd->invincible_timer != tid){
  169. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  170. return 0;
  171. }
  172. sd->invincible_timer = INVALID_TIMER;
  173. skill_unit_move(&sd->bl,tick,1);
  174. return 0;
  175. }
  176. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  177. nullpo_retv(sd);
  178. if( sd->invincible_timer != INVALID_TIMER )
  179. delete_timer(sd->invincible_timer,pc_invincible_timer);
  180. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  181. }
  182. void pc_delinvincibletimer(struct map_session_data* sd)
  183. {
  184. nullpo_retv(sd);
  185. if( sd->invincible_timer != INVALID_TIMER )
  186. {
  187. delete_timer(sd->invincible_timer,pc_invincible_timer);
  188. sd->invincible_timer = INVALID_TIMER;
  189. skill_unit_move(&sd->bl,gettick(),1);
  190. }
  191. }
  192. static TIMER_FUNC(pc_spiritball_timer){
  193. struct map_session_data *sd;
  194. int i;
  195. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  196. return 1;
  197. if( sd->spiritball <= 0 )
  198. {
  199. 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);
  200. sd->spiritball = 0;
  201. return 0;
  202. }
  203. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  204. if( i == sd->spiritball )
  205. {
  206. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  207. return 0;
  208. }
  209. sd->spiritball--;
  210. if( i != sd->spiritball )
  211. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  212. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  213. clif_spiritball(&sd->bl);
  214. return 0;
  215. }
  216. /**
  217. * Adds a spiritball to player for 'interval' ms
  218. * @param sd
  219. * @param interval
  220. * @param max
  221. */
  222. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  223. {
  224. int tid;
  225. uint8 i;
  226. nullpo_retv(sd);
  227. if(max > MAX_SPIRITBALL)
  228. max = MAX_SPIRITBALL;
  229. if(sd->spiritball < 0)
  230. sd->spiritball = 0;
  231. if( sd->spiritball && sd->spiritball >= max )
  232. {
  233. if(sd->spirit_timer[0] != INVALID_TIMER)
  234. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  235. sd->spiritball--;
  236. if( sd->spiritball != 0 )
  237. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  238. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  239. }
  240. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  241. 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);
  242. if( i != sd->spiritball )
  243. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  244. sd->spirit_timer[i] = tid;
  245. sd->spiritball++;
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. /**
  252. * Removes number of spiritball from player
  253. * @param sd
  254. * @param count
  255. * @param type 1 = doesn't give client effect
  256. */
  257. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  258. {
  259. uint8 i;
  260. nullpo_retv(sd);
  261. if(sd->spiritball <= 0) {
  262. sd->spiritball = 0;
  263. return;
  264. }
  265. if(count == 0)
  266. return;
  267. if(count > sd->spiritball)
  268. count = sd->spiritball;
  269. sd->spiritball -= count;
  270. if(count > MAX_SPIRITBALL)
  271. count = MAX_SPIRITBALL;
  272. for(i=0;i<count;i++) {
  273. if(sd->spirit_timer[i] != INVALID_TIMER) {
  274. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  275. sd->spirit_timer[i] = INVALID_TIMER;
  276. }
  277. }
  278. for(i=count;i<MAX_SPIRITBALL;i++) {
  279. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  280. sd->spirit_timer[i] = INVALID_TIMER;
  281. }
  282. if(!type) {
  283. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  284. clif_millenniumshield(&sd->bl,sd->spiritball);
  285. else
  286. clif_spiritball(&sd->bl);
  287. }
  288. }
  289. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  290. int *c, *b_sd;
  291. struct block_list *src;
  292. struct map_session_data *tsd;
  293. struct status_change *sc;
  294. nullpo_ret(bl);
  295. nullpo_ret(tsd = (struct map_session_data*)bl);
  296. nullpo_ret(src = va_arg(ap,struct block_list *));
  297. c = va_arg(ap,int *);
  298. b_sd = va_arg(ap, int *);
  299. if(pc_isdead(tsd))
  300. return 0;
  301. sc = status_get_sc(bl);
  302. if( sc && sc->data[SC_BANDING] )
  303. {
  304. b_sd[(*c)++] = tsd->bl.id;
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  310. int c;
  311. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  312. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  313. int range = skill_get_splash(LG_BANDING,skill_lv);
  314. nullpo_ret(sd);
  315. c = 0;
  316. memset(b_sd, 0, sizeof(b_sd));
  317. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  318. if( c < 1 ) //just recalc status no need to recalc hp
  319. { // No more Royal Guards in Banding found.
  320. struct status_change *sc;
  321. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  322. {
  323. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  324. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  325. }
  326. return 0;
  327. }
  328. //Add yourself
  329. hp = status_get_hp(&sd->bl);
  330. i++;
  331. // Get total HP of all Royal Guards in party.
  332. for( j = 0; j < i; j++ ){
  333. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  334. if( bsd != NULL )
  335. hp += status_get_hp(&bsd->bl);
  336. }
  337. // Set average HP.
  338. hp = hp / i;
  339. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  340. for( j = 0; j < i; j++ )
  341. {
  342. int tmp_hp=0;
  343. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  344. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  345. extra_hp += tmp_hp;
  346. tmp_qty++;
  347. }
  348. }
  349. if( extra_hp > 0 && tmp_qty > 0 )
  350. hp += extra_hp / tmp_qty;
  351. for( j = 0; j < i; j++ ){
  352. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  353. if( bsd != NULL )
  354. {
  355. struct status_change *sc;
  356. status_set_hp(&bsd->bl,hp,0); // Set hp
  357. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  358. {
  359. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  360. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  361. }
  362. }
  363. }
  364. return c;
  365. }
  366. /**
  367. * Increases a player's fame points and displays a notice to him
  368. * @param sd Player
  369. * @param count Fame point
  370. */
  371. void pc_addfame(struct map_session_data *sd,int count)
  372. {
  373. enum e_rank ranktype;
  374. nullpo_retv(sd);
  375. sd->status.fame += count;
  376. if(sd->status.fame > MAX_FAME)
  377. sd->status.fame = MAX_FAME;
  378. switch(sd->class_&MAPID_UPPERMASK){
  379. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  380. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  381. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  382. default:
  383. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  384. return;
  385. }
  386. clif_update_rankingpoint(sd,ranktype,count);
  387. chrif_updatefamelist(sd);
  388. }
  389. /**
  390. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  391. * @param sd
  392. * @param job Job use enum e_mapid
  393. * @return Rank
  394. */
  395. unsigned char pc_famerank(uint32 char_id, int job)
  396. {
  397. uint8 i;
  398. switch(job){
  399. case MAPID_BLACKSMITH: // Blacksmith
  400. for(i = 0; i < MAX_FAME_LIST; i++){
  401. if(smith_fame_list[i].id == char_id)
  402. return i + 1;
  403. }
  404. break;
  405. case MAPID_ALCHEMIST: // Alchemist
  406. for(i = 0; i < MAX_FAME_LIST; i++){
  407. if(chemist_fame_list[i].id == char_id)
  408. return i + 1;
  409. }
  410. break;
  411. case MAPID_TAEKWON: // Taekwon
  412. for(i = 0; i < MAX_FAME_LIST; i++){
  413. if(taekwon_fame_list[i].id == char_id)
  414. return i + 1;
  415. }
  416. break;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * Restart player's HP & SP value
  422. * @param sd
  423. * @param type Restart type: 1 - Normal Resurection
  424. */
  425. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  426. struct status_data *status, *b_status;
  427. nullpo_retv(sd);
  428. b_status = &sd->base_status;
  429. status = &sd->battle_status;
  430. if (type&1) { //Normal resurrection
  431. status->hp = 1; //Otherwise status_heal may fail if dead.
  432. status_heal(&sd->bl, b_status->hp, 0, 1);
  433. if( status->sp < b_status->sp )
  434. status_set_sp(&sd->bl, b_status->sp, 1);
  435. } else { //Just for saving on the char-server (with values as if respawned)
  436. sd->status.hp = b_status->hp;
  437. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  438. }
  439. }
  440. /*==========================================
  441. Rental System
  442. *------------------------------------------*/
  443. /**
  444. * Ends a rental and removes the item/effect
  445. * @param tid: Tick ID
  446. * @param tick: Timer
  447. * @param id: Timer ID
  448. * @param data: Data
  449. * @return false - failure, true - success
  450. */
  451. TIMER_FUNC(pc_inventory_rental_end){
  452. struct map_session_data *sd = map_id2sd(id);
  453. if( sd == NULL )
  454. return 0;
  455. if( tid != sd->rental_timer ) {
  456. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  457. return 0;
  458. }
  459. pc_inventory_rentals(sd);
  460. return 1;
  461. }
  462. /**
  463. * Removes the rental timer from the player
  464. * @param sd: Player data
  465. */
  466. void pc_inventory_rental_clear(struct map_session_data *sd)
  467. {
  468. if( sd->rental_timer != INVALID_TIMER ) {
  469. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  470. sd->rental_timer = INVALID_TIMER;
  471. }
  472. }
  473. /**
  474. * Check for items in the player's inventory that are rental type
  475. * @param sd: Player data
  476. */
  477. void pc_inventory_rentals(struct map_session_data *sd)
  478. {
  479. int i, c = 0;
  480. unsigned int next_tick = UINT_MAX;
  481. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  482. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  483. continue; // Nothing here
  484. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  485. continue;
  486. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  487. if (sd->inventory_data[i]->unequip_script)
  488. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  489. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  490. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  491. } else {
  492. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  493. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  494. next_tick = umin(expire_tick * 1000U, next_tick);
  495. c++;
  496. }
  497. }
  498. 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
  499. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  500. else
  501. sd->rental_timer = INVALID_TIMER;
  502. }
  503. /**
  504. * Add a rental item to the player and adjusts the rental timer appropriately
  505. * @param sd: Player data
  506. * @param seconds: Rental time
  507. */
  508. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  509. {
  510. unsigned int tick = seconds * 1000;
  511. if( sd == NULL )
  512. return;
  513. if( sd->rental_timer != INVALID_TIMER ) {
  514. const struct TimerData * td;
  515. td = get_timer(sd->rental_timer);
  516. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  517. pc_inventory_rental_clear(sd);
  518. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  519. }
  520. } else
  521. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  522. }
  523. /**
  524. * Check if the player can sell the current item
  525. * @param sd: map_session_data of the player
  526. * @param item: struct of the checking item
  527. * @return bool 'true' is sellable, 'false' otherwise
  528. */
  529. bool pc_can_sell_item(struct map_session_data *sd, struct item *item) {
  530. struct npc_data *nd;
  531. if (sd == NULL || item == NULL)
  532. return false;
  533. nd = map_id2nd(sd->npc_shopid);
  534. if (battle_config.hide_fav_sell && item->favorite)
  535. return false; //Cannot sell favs (optional config)
  536. if (item->expire_time)
  537. return false; // Cannot Sell Rental Items
  538. if (nd && nd->subtype == NPCTYPE_ITEMSHOP) {
  539. struct item_data *itd;
  540. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND)
  541. return true; // NPCTYPE_ITEMSHOP and bound item config is sellable
  542. if ((itd = itemdb_search(item->nameid)) && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  543. return true; // NPCTYPE_ITEMSHOP and sell restricted item config is sellable
  544. }
  545. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  546. return false;
  547. if (item->bound && !pc_can_give_bounded_items(sd))
  548. return false; // Don't allow sale of bound items
  549. return true;
  550. }
  551. /**
  552. * Determines if player can give / drop / trade / vend items
  553. */
  554. bool pc_can_give_items(struct map_session_data *sd)
  555. {
  556. return pc_has_permission(sd, PC_PERM_TRADE);
  557. }
  558. /**
  559. * Determines if player can give / drop / trade / vend bounded items
  560. */
  561. bool pc_can_give_bounded_items(struct map_session_data *sd)
  562. {
  563. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  564. }
  565. /*==========================================
  566. * Prepares character for saving.
  567. * @param sd
  568. *------------------------------------------*/
  569. void pc_makesavestatus(struct map_session_data *sd) {
  570. nullpo_retv(sd);
  571. if(!battle_config.save_clothcolor)
  572. sd->status.clothes_color = 0;
  573. // Since this is currently not officially released,
  574. // its best to have a forced option to not save body styles.
  575. if(!battle_config.save_body_style)
  576. sd->status.body = 0;
  577. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  578. //status change load/saving. [Skotlex]
  579. #ifdef NEW_CARTS
  580. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  581. #else
  582. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  583. #endif
  584. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  585. if(pc_isdead(sd)){
  586. pc_setrestartvalue(sd, 0);
  587. } else {
  588. sd->status.hp = sd->battle_status.hp;
  589. sd->status.sp = sd->battle_status.sp;
  590. }
  591. sd->status.last_point.map = sd->mapindex;
  592. sd->status.last_point.x = sd->bl.x;
  593. sd->status.last_point.y = sd->bl.y;
  594. return;
  595. }
  596. if(pc_isdead(sd)) {
  597. pc_setrestartvalue(sd, 0);
  598. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  599. } else {
  600. sd->status.hp = sd->battle_status.hp;
  601. sd->status.sp = sd->battle_status.sp;
  602. sd->status.last_point.map = sd->mapindex;
  603. sd->status.last_point.x = sd->bl.x;
  604. sd->status.last_point.y = sd->bl.y;
  605. }
  606. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  607. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  608. if(mapdata->save.map)
  609. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  610. else
  611. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  612. }
  613. }
  614. /*==========================================
  615. * Off init ? Connection?
  616. *------------------------------------------*/
  617. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
  618. nullpo_retv(sd);
  619. sd->bl.id = account_id;
  620. sd->status.account_id = account_id;
  621. sd->status.char_id = char_id;
  622. sd->status.sex = sex;
  623. sd->login_id1 = login_id1;
  624. sd->login_id2 = 0; // at this point, we can not know the value :(
  625. sd->client_tick = client_tick;
  626. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  627. sd->bl.type = BL_PC;
  628. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  629. sd->canlog_tick = gettick();
  630. //Required to prevent homunculus copuing a base speed of 0.
  631. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  632. }
  633. /**
  634. * Get equip point for an equip
  635. * @param sd
  636. * @param id
  637. */
  638. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  639. int ep = 0;
  640. nullpo_ret(sd);
  641. nullpo_ret(id);
  642. if (!itemdb_isequip2(id))
  643. return 0; //Not equippable by players.
  644. ep = id->equip;
  645. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  646. 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]
  647. if (ep == EQP_WEAPON)
  648. return EQP_ARMS;
  649. if (ep == EQP_SHADOW_WEAPON)
  650. return EQP_SHADOW_ARMS;
  651. }
  652. }
  653. return ep;
  654. }
  655. /**
  656. * Get equip point for an equip
  657. * @param sd
  658. * @param n Equip index in inventory
  659. */
  660. int pc_equippoint(struct map_session_data *sd,int n){
  661. nullpo_ret(sd);
  662. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  663. }
  664. /**
  665. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  666. * @param sd : player session
  667. * @return 0 sucess, 1:invalid sd
  668. */
  669. void pc_setinventorydata(struct map_session_data *sd)
  670. {
  671. uint8 i;
  672. nullpo_retv(sd);
  673. for(i = 0; i < MAX_INVENTORY; i++) {
  674. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  675. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  676. }
  677. }
  678. /**
  679. * 'Calculates' weapon type
  680. * @param sd : Player
  681. */
  682. void pc_calcweapontype(struct map_session_data *sd)
  683. {
  684. nullpo_retv(sd);
  685. // single-hand
  686. if(sd->weapontype2 == W_FIST) {
  687. sd->status.weapon = sd->weapontype1;
  688. return;
  689. }
  690. if(sd->weapontype1 == W_FIST) {
  691. sd->status.weapon = sd->weapontype2;
  692. return;
  693. }
  694. // dual-wield
  695. sd->status.weapon = 0;
  696. switch (sd->weapontype1){
  697. case W_DAGGER:
  698. switch (sd->weapontype2) {
  699. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  700. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  701. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  702. }
  703. break;
  704. case W_1HSWORD:
  705. switch (sd->weapontype2) {
  706. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  707. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  708. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  709. }
  710. break;
  711. case W_1HAXE:
  712. switch (sd->weapontype2) {
  713. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  714. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  715. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  716. }
  717. }
  718. // unknown, default to right hand type
  719. if (!sd->status.weapon)
  720. sd->status.weapon = sd->weapontype1;
  721. }
  722. /**
  723. * Set equip index
  724. * @param sd : Player
  725. */
  726. void pc_setequipindex(struct map_session_data *sd)
  727. {
  728. uint16 i;
  729. nullpo_retv(sd);
  730. for (i = 0; i < EQI_MAX; i++)
  731. sd->equip_index[i] = -1;
  732. for (i = 0; i < MAX_INVENTORY; i++) {
  733. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  734. continue;
  735. if (sd->inventory.u.items_inventory[i].equip) {
  736. uint8 j;
  737. for (j = 0; j < EQI_MAX; j++)
  738. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  739. sd->equip_index[j] = i;
  740. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  741. if (sd->inventory_data[i])
  742. sd->weapontype1 = sd->inventory_data[i]->look;
  743. else
  744. sd->weapontype1 = 0;
  745. }
  746. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  747. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  748. sd->weapontype2 = sd->inventory_data[i]->look;
  749. else
  750. sd->weapontype2 = 0;
  751. }
  752. }
  753. }
  754. pc_calcweapontype(sd);
  755. }
  756. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  757. //{
  758. // int i;
  759. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  760. // struct item_data *data;
  761. //
  762. // //Crafted/made/hatched items.
  763. // if (itemdb_isspecial(item->card[0]))
  764. // return 1;
  765. //
  766. // /* scan for enchant armor gems */
  767. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  768. // s = MAX_SLOTS - 1;
  769. //
  770. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  771. // return( i < s ) ? 0 : 1;
  772. //}
  773. /**
  774. * Check if an item is equiped by player
  775. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  776. * @param sd : player session
  777. * @param nameid : itemid
  778. * @return 1:yes, 0:no
  779. */
  780. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  781. {
  782. uint8 i;
  783. for( i = 0; i < EQI_MAX; i++ )
  784. {
  785. short index = sd->equip_index[i], j;
  786. if( index < 0 )
  787. continue;
  788. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  789. continue;
  790. if( !sd->inventory_data[index] )
  791. continue;
  792. if( sd->inventory_data[index]->nameid == nameid )
  793. return true;
  794. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  795. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  796. return true;
  797. }
  798. }
  799. return false;
  800. }
  801. /**
  802. * Check adoption rules
  803. * @param p1_sd: Player 1
  804. * @param p2_sd: Player 2
  805. * @param b_sd: Player that will be adopted
  806. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  807. * ADOPT_ALREADY_ADOPTED - Already adopted
  808. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  809. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  810. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  811. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  812. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  813. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  814. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  815. */
  816. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  817. {
  818. if( !p1_sd || !p2_sd || !b_sd )
  819. return ADOPT_CHARACTER_NOT_FOUND;
  820. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  821. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  822. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  823. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  824. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  825. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  826. if( p2_sd->status.party_id != p1_sd->status.party_id )
  827. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  828. // Parents need to have their ring equipped
  829. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  830. return ADOPT_EQUIP_RINGS;
  831. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  832. return ADOPT_EQUIP_RINGS;
  833. // Already adopted a baby
  834. if( p1_sd->status.child || p2_sd->status.child ) {
  835. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  836. return ADOPT_MORE_CHILDREN;
  837. }
  838. // Parents need at least lvl 70 to adopt
  839. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  840. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  841. return ADOPT_LEVEL_70;
  842. }
  843. if( b_sd->status.partner_id ) {
  844. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  845. return ADOPT_MARRIED;
  846. }
  847. 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 ) )
  848. return ADOPT_NOT_NOVICE;
  849. return ADOPT_ALLOWED;
  850. }
  851. /*==========================================
  852. * Adoption Process
  853. *------------------------------------------*/
  854. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  855. {
  856. int job, joblevel;
  857. unsigned int jobexp;
  858. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  859. return false;
  860. // Preserve current job levels and progress
  861. joblevel = b_sd->status.job_level;
  862. jobexp = b_sd->status.job_exp;
  863. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  864. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  865. { // Success, proceed to configure parents and baby skills
  866. p1_sd->status.child = b_sd->status.char_id;
  867. p2_sd->status.child = b_sd->status.char_id;
  868. b_sd->status.father = p1_sd->status.char_id;
  869. b_sd->status.mother = p2_sd->status.char_id;
  870. // Restore progress
  871. b_sd->status.job_level = joblevel;
  872. clif_updatestatus(b_sd, SP_JOBLEVEL);
  873. b_sd->status.job_exp = jobexp;
  874. clif_updatestatus(b_sd, SP_JOBEXP);
  875. // Baby Skills
  876. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  877. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  878. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  879. // Parents Skills
  880. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  881. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  882. chrif_save(p1_sd, CSAVE_NORMAL);
  883. chrif_save(p2_sd, CSAVE_NORMAL);
  884. chrif_save(b_sd, CSAVE_NORMAL);
  885. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  886. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  887. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  888. return true;
  889. }
  890. return false; // Job Change Fail
  891. }
  892. /*==========================================
  893. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  894. Returns:
  895. false : Cannot use/equip
  896. true : Can use/equip
  897. * Credits:
  898. [Inkfish] for first idea
  899. [Haru] for third-classes extension
  900. [Cydh] finishing :D
  901. *------------------------------------------*/
  902. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  903. while (1) {
  904. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  905. break;
  906. #ifndef RENEWAL
  907. //allow third classes to use trans. class items
  908. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  909. break;
  910. //third-baby classes can use same item too
  911. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  912. break;
  913. //don't need to decide specific rules for third-classes?
  914. //items for third classes can be used for all third classes
  915. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  916. break;
  917. #else
  918. //trans. classes (exl. third-trans.)
  919. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  920. break;
  921. //baby classes (exl. third-baby)
  922. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  923. break;
  924. //third classes (exl. third-trans. and baby-third)
  925. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  926. break;
  927. //trans-third classes
  928. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  929. break;
  930. //third-baby classes
  931. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  932. break;
  933. #endif
  934. return false;
  935. }
  936. return true;
  937. }
  938. /*=================================================
  939. * Checks if the player can equip the item at index n in inventory.
  940. * @param sd
  941. * @param n Item index in inventory
  942. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  943. *------------------------------------------------*/
  944. uint8 pc_isequip(struct map_session_data *sd,int n)
  945. {
  946. struct item_data *item;
  947. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  948. item = sd->inventory_data[n];
  949. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  950. return ITEM_EQUIP_ACK_OK;
  951. if(item == NULL)
  952. return ITEM_EQUIP_ACK_FAIL;
  953. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  954. return ITEM_EQUIP_ACK_FAILLEVEL;
  955. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  956. return ITEM_EQUIP_ACK_FAILLEVEL;
  957. if(item->sex != 2 && sd->status.sex != item->sex)
  958. return ITEM_EQUIP_ACK_FAIL;
  959. //fail to equip if item is restricted
  960. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  961. return ITEM_EQUIP_ACK_FAIL;
  962. if (sd->sc.count) {
  963. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  964. return ITEM_EQUIP_ACK_FAIL;
  965. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  966. return ITEM_EQUIP_ACK_FAIL;
  967. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  968. return ITEM_EQUIP_ACK_FAIL;
  969. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  970. return ITEM_EQUIP_ACK_FAIL;
  971. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  972. return ITEM_EQUIP_ACK_FAIL;
  973. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  974. return ITEM_EQUIP_ACK_FAIL;
  975. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  976. return ITEM_EQUIP_ACK_FAIL;
  977. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  978. //Spirit of Super Novice equip bonuses. [Skotlex]
  979. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  980. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  981. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  982. switch(item->look) { //In weapons, the look determines type of weapon.
  983. case W_DAGGER: //All level 4 - Daggers
  984. case W_1HSWORD: //All level 4 - 1H Swords
  985. case W_1HAXE: //All level 4 - 1H Axes
  986. case W_MACE: //All level 4 - 1H Maces
  987. case W_STAFF: //All level 4 - 1H Staves
  988. case W_2HSTAFF: //All level 4 - 2H Staves
  989. return ITEM_EQUIP_ACK_OK;
  990. }
  991. }
  992. }
  993. //Not equipable by class. [Skotlex]
  994. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  995. return ITEM_EQUIP_ACK_FAIL;
  996. if (!pc_isItemClass(sd, item))
  997. return ITEM_EQUIP_ACK_FAIL;
  998. return ITEM_EQUIP_ACK_OK;
  999. }
  1000. /*==========================================
  1001. * No problem with the session id
  1002. * set the status that has been sent from char server
  1003. *------------------------------------------*/
  1004. 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)
  1005. {
  1006. int i;
  1007. unsigned long tick = gettick();
  1008. uint32 ip = session[sd->fd]->client_addr;
  1009. sd->login_id2 = login_id2;
  1010. sd->group_id = group_id;
  1011. /* load user permissions */
  1012. pc_group_pc_load(sd);
  1013. memcpy(&sd->status, st, sizeof(*st));
  1014. if (st->sex != sd->status.sex) {
  1015. clif_authfail_fd(sd->fd, 0);
  1016. return false;
  1017. }
  1018. //Set the map-server used job id. [Skotlex]
  1019. i = pc_jobid2mapid(sd->status.class_);
  1020. if (i == -1) { //Invalid class?
  1021. 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);
  1022. sd->status.class_ = JOB_NOVICE;
  1023. sd->class_ = MAPID_NOVICE;
  1024. } else
  1025. sd->class_ = i;
  1026. // Checks and fixes to character status data, that are required
  1027. // in case of configuration change or stuff, which cannot be
  1028. // checked on char-server.
  1029. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1030. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1031. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1032. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1033. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1034. sd->state.connect_new = 1;
  1035. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1036. sd->invincible_timer = INVALID_TIMER;
  1037. sd->npc_timer_id = INVALID_TIMER;
  1038. sd->pvp_timer = INVALID_TIMER;
  1039. sd->expiration_tid = INVALID_TIMER;
  1040. sd->autotrade_tid = INVALID_TIMER;
  1041. #ifdef SECURE_NPCTIMEOUT
  1042. // Initialize to defaults/expected
  1043. sd->npc_idle_timer = INVALID_TIMER;
  1044. sd->npc_idle_tick = tick;
  1045. sd->npc_idle_type = NPCT_INPUT;
  1046. sd->state.ignoretimeout = false;
  1047. #endif
  1048. sd->canuseitem_tick = tick;
  1049. sd->canusecashfood_tick = tick;
  1050. sd->canequip_tick = tick;
  1051. sd->cantalk_tick = tick;
  1052. sd->canskill_tick = tick;
  1053. sd->cansendmail_tick = tick;
  1054. sd->idletime = last_tick;
  1055. for(i = 0; i < MAX_SPIRITBALL; i++)
  1056. sd->spirit_timer[i] = INVALID_TIMER;
  1057. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  1058. sd->autobonus[i].active = INVALID_TIMER;
  1059. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  1060. sd->autobonus2[i].active = INVALID_TIMER;
  1061. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  1062. sd->autobonus3[i].active = INVALID_TIMER;
  1063. if (battle_config.item_auto_get)
  1064. sd->state.autoloot = 10000;
  1065. if (battle_config.disp_experience)
  1066. sd->state.showexp = 1;
  1067. if (battle_config.disp_zeny)
  1068. sd->state.showzeny = 1;
  1069. #ifdef VIP_ENABLE
  1070. if (!battle_config.vip_disp_rate)
  1071. sd->vip.disableshowrate = 1;
  1072. #endif
  1073. if (!(battle_config.display_skill_fail&2))
  1074. sd->state.showdelay = 1;
  1075. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1076. memset(&sd->cart, 0, sizeof(struct s_storage));
  1077. memset(&sd->storage, 0, sizeof(struct s_storage));
  1078. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1079. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1080. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1081. sd->status.option &= ~OPTION_INVISIBLE;
  1082. }
  1083. status_change_init(&sd->bl);
  1084. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1085. unit_dataset(&sd->bl);
  1086. sd->guild_x = -1;
  1087. sd->guild_y = -1;
  1088. sd->delayed_damage = 0;
  1089. // Event Timers
  1090. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1091. sd->eventtimer[i] = INVALID_TIMER;
  1092. // Rental Timer
  1093. sd->rental_timer = INVALID_TIMER;
  1094. for( i = 0; i < 3; i++ )
  1095. sd->hate_mob[i] = -1;
  1096. sd->quest_log = NULL;
  1097. sd->num_quests = 0;
  1098. sd->avail_quests = 0;
  1099. sd->save_quest = false;
  1100. sd->count_rewarp = 0;
  1101. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1102. sd->regs.arrays = NULL;
  1103. sd->vars_dirty = false;
  1104. sd->vars_ok = false;
  1105. sd->vars_received = 0x0;
  1106. sd->qi_display = NULL;
  1107. sd->qi_count = 0;
  1108. //warp player
  1109. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1110. 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);
  1111. // try warping to a default map instead (church graveyard)
  1112. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1113. // if we fail again
  1114. clif_authfail_fd(sd->fd, 0);
  1115. return false;
  1116. }
  1117. }
  1118. clif_authok(sd);
  1119. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1120. sd->die_counter=-1;
  1121. //display login notice
  1122. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1123. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1124. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1125. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1126. sd->status.name, sd->status.account_id, sd->status.char_id,
  1127. CONVIP(ip), sd->group_id);
  1128. // Send friends list
  1129. clif_friendslist_send(sd);
  1130. if( !changing_mapservers ) {
  1131. if (battle_config.display_version == 1)
  1132. pc_show_version(sd);
  1133. // Message of the Day [Valaris]
  1134. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1135. if (battle_config.motd_type)
  1136. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1137. else
  1138. clif_displaymessage(sd->fd, motd_text[i]);
  1139. }
  1140. if (expiration_time != 0)
  1141. sd->expiration_time = expiration_time;
  1142. /**
  1143. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1144. **/
  1145. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1146. }
  1147. pc_validate_skill(sd);
  1148. /* [Ind] */
  1149. sd->sc_display = NULL;
  1150. sd->sc_display_count = 0;
  1151. // Player has not yet received the CashShop list
  1152. sd->status.cashshop_sent = false;
  1153. sd->last_addeditem_index = -1;
  1154. sd->bonus_script.head = NULL;
  1155. sd->bonus_script.count = 0;
  1156. #if PACKETVER >= 20150513
  1157. sd->hatEffectIDs = NULL;
  1158. sd->hatEffectCount = 0;
  1159. #endif
  1160. sd->catch_target_class = PET_CATCH_FAIL;
  1161. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1162. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1163. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1164. clif_updatestatus(sd, SP_BASEEXP);
  1165. }
  1166. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1167. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1168. clif_updatestatus(sd, SP_JOBEXP);
  1169. }
  1170. // Request all registries (auth is considered completed whence they arrive)
  1171. intif_request_registry(sd,7);
  1172. return true;
  1173. }
  1174. /*==========================================
  1175. * Closes a connection because it failed to be authenticated from the char server.
  1176. *------------------------------------------*/
  1177. void pc_authfail(struct map_session_data *sd)
  1178. {
  1179. clif_authfail_fd(sd->fd, 0);
  1180. return;
  1181. }
  1182. /**
  1183. * Player register a bl as hatred
  1184. * @param sd : player session
  1185. * @param pos : hate position [0;2]
  1186. * @param bl : target bl
  1187. * @return false:failed, true:success
  1188. */
  1189. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1190. {
  1191. int class_;
  1192. if (!sd || !bl || pos < 0 || pos > 2)
  1193. return false;
  1194. if (sd->hate_mob[pos] != -1)
  1195. { //Can't change hate targets.
  1196. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1197. return false;
  1198. }
  1199. class_ = status_get_class(bl);
  1200. if (!pcdb_checkid(class_)) {
  1201. unsigned int max_hp = status_get_max_hp(bl);
  1202. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1203. return false;
  1204. if (pos != status_get_size(bl))
  1205. return false; //Wrong size
  1206. }
  1207. sd->hate_mob[pos] = class_;
  1208. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1209. clif_hate_info(sd, pos, class_, 1);
  1210. return true;
  1211. }
  1212. /*==========================================
  1213. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1214. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1215. * See intif_parse_StorageReceived() for item operations [lighta]
  1216. *------------------------------------------*/
  1217. void pc_reg_received(struct map_session_data *sd)
  1218. {
  1219. uint8 i;
  1220. sd->vars_ok = true;
  1221. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1222. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1223. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1224. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1225. if (msg_checklangtype(sd->langtype,true) < 0)
  1226. sd->langtype = 0; //invalid langtype reset to default
  1227. // Cash shop
  1228. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1229. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1230. // Cooking Exp
  1231. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1232. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1233. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1234. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1235. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1236. }
  1237. if (battle_config.feature_banking)
  1238. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1239. if (battle_config.feature_roulette) {
  1240. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1241. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1242. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1243. }
  1244. sd->roulette.prizeIdx = -1;
  1245. //SG map and mob read [Komurka]
  1246. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1247. uint16 j;
  1248. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1249. sd->feel_map[i].index = j;
  1250. sd->feel_map[i].m = map_mapindex2mapid(j);
  1251. } else {
  1252. sd->feel_map[i].index = 0;
  1253. sd->feel_map[i].m = -1;
  1254. }
  1255. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1256. }
  1257. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1258. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1259. sd->cloneskill_idx = skill_get_index(skid);
  1260. if (sd->cloneskill_idx > 0) {
  1261. sd->status.skill[sd->cloneskill_idx].id = skid;
  1262. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1263. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1264. sd->status.skill[sd->cloneskill_idx].lv = i;
  1265. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1266. }
  1267. }
  1268. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1269. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1270. sd->reproduceskill_idx = skill_get_index(skid);
  1271. if (sd->reproduceskill_idx > 0) {
  1272. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1273. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1274. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1275. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1276. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1277. }
  1278. }
  1279. //Weird... maybe registries were reloaded?
  1280. if (sd->state.active)
  1281. return;
  1282. sd->state.active = 1;
  1283. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1284. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1285. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1286. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1287. if (sd->status.party_id)
  1288. party_member_joined(sd);
  1289. if (sd->status.guild_id)
  1290. guild_member_joined(sd);
  1291. if( sd->status.clan_id )
  1292. clan_member_joined(sd);
  1293. // pet
  1294. if (sd->status.pet_id > 0)
  1295. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1296. // Homunculus [albator]
  1297. if( sd->status.hom_id > 0 )
  1298. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1299. if( sd->status.mer_id > 0 )
  1300. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1301. if( sd->status.ele_id > 0 )
  1302. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1303. map_addiddb(&sd->bl);
  1304. map_delnickdb(sd->status.char_id, sd->status.name);
  1305. if (!chrif_auth_finished(sd))
  1306. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1307. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1308. chrif_bsdata_request(sd->status.char_id);
  1309. #ifdef VIP_ENABLE
  1310. sd->vip.time = 0;
  1311. sd->vip.enabled = 0;
  1312. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1313. #endif
  1314. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1315. intif_request_questlog(sd);
  1316. if (battle_config.feature_achievement) {
  1317. sd->achievement_data.total_score = 0;
  1318. sd->achievement_data.level = 0;
  1319. sd->achievement_data.save = false;
  1320. sd->achievement_data.count = 0;
  1321. sd->achievement_data.incompleteCount = 0;
  1322. sd->achievement_data.achievements = NULL;
  1323. intif_request_achievements(sd->status.char_id);
  1324. }
  1325. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1326. sd->state.connect_new = 1;
  1327. clif_parse_LoadEndAck(sd->fd, sd);
  1328. }
  1329. if( pc_isinvisible(sd) ) {
  1330. sd->vd.class_ = JT_INVISIBLE;
  1331. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1332. // decrement the number of pvp players on the map
  1333. map_getmapdata(sd->bl.m)->users_pvp--;
  1334. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1335. // unregister the player for ranking
  1336. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1337. sd->pvp_timer = INVALID_TIMER;
  1338. }
  1339. clif_changeoption( &sd->bl );
  1340. }
  1341. channel_autojoin(sd);
  1342. }
  1343. static int pc_calc_skillpoint(struct map_session_data* sd)
  1344. {
  1345. uint16 i, skill_point = 0;
  1346. nullpo_ret(sd);
  1347. for(i = 1; i < MAX_SKILL; i++) {
  1348. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1349. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1350. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1351. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1352. )
  1353. {
  1354. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1355. skill_point += sd->status.skill[i].lv;
  1356. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1357. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1358. }
  1359. }
  1360. }
  1361. return skill_point;
  1362. }
  1363. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1364. uint16 i = 0;
  1365. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1366. return false;
  1367. /**
  1368. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1369. * skill tree and since they have no icons they'll give resource errors
  1370. * Get ALL skills except npc/guild ones. [Skotlex]
  1371. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1372. **/
  1373. for( i = 0; i < MAX_SKILL; i++ ) {
  1374. uint16 skill_id = skill_idx2id(i);
  1375. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1376. continue;
  1377. switch (skill_id) {
  1378. case SM_SELFPROVOKE:
  1379. case AB_DUPLELIGHT_MELEE:
  1380. case AB_DUPLELIGHT_MAGIC:
  1381. case WL_CHAINLIGHTNING_ATK:
  1382. case WL_TETRAVORTEX_FIRE:
  1383. case WL_TETRAVORTEX_WATER:
  1384. case WL_TETRAVORTEX_WIND:
  1385. case WL_TETRAVORTEX_GROUND:
  1386. case WL_SUMMON_ATK_FIRE:
  1387. case WL_SUMMON_ATK_WIND:
  1388. case WL_SUMMON_ATK_WATER:
  1389. case WL_SUMMON_ATK_GROUND:
  1390. case LG_OVERBRAND_BRANDISH:
  1391. case LG_OVERBRAND_PLUSATK:
  1392. case WM_SEVERE_RAINSTORM_MELEE:
  1393. case RL_R_TRIP_PLUSATK:
  1394. case SG_DEVIL:
  1395. case MO_TRIPLEATTACK:
  1396. case RG_SNATCHER:
  1397. continue;
  1398. default:
  1399. {
  1400. uint8 lv = (uint8)skill_get_max(skill_id);
  1401. if (lv > 0) {
  1402. sd->status.skill[i].id = skill_id;
  1403. if (addlv)
  1404. sd->status.skill[i].lv = lv;
  1405. }
  1406. }
  1407. break;
  1408. }
  1409. }
  1410. return true;
  1411. }
  1412. /*==========================================
  1413. * Calculation of skill level.
  1414. * @param sd
  1415. *------------------------------------------*/
  1416. void pc_calc_skilltree(struct map_session_data *sd)
  1417. {
  1418. int i, flag;
  1419. int c = 0;
  1420. nullpo_retv(sd);
  1421. i = pc_calc_skilltree_normalize_job(sd);
  1422. c = pc_mapid2jobid(i, sd->status.sex);
  1423. if( c == -1 )
  1424. { //Unable to normalize job??
  1425. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1426. return;
  1427. }
  1428. c = pc_class2idx(c);
  1429. for( i = 0; i < MAX_SKILL; i++ ) {
  1430. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1431. sd->status.skill[i].id = 0; //First clear skills.
  1432. /* permanent skills that must be re-checked */
  1433. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1434. uint16 sk_id = skill_idx2id(i);
  1435. if (!sk_id) {
  1436. sd->status.skill[i].id = 0;
  1437. sd->status.skill[i].lv = 0;
  1438. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1439. continue;
  1440. }
  1441. switch (sk_id) {
  1442. case NV_TRICKDEAD:
  1443. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1444. sd->status.skill[i].id = 0;
  1445. sd->status.skill[i].lv = 0;
  1446. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1447. }
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. for( i = 0; i < MAX_SKILL; i++ ) {
  1453. uint16 skill_id = 0;
  1454. // Restore original level of skills after deleting earned skills.
  1455. 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 ) {
  1456. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1457. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1458. }
  1459. //Enable Bard/Dancer spirit linked skills.
  1460. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1461. continue;
  1462. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1463. //Link Dancer skills to bard.
  1464. if( sd->status.sex ) {
  1465. if( sd->status.skill[i-8].lv < 10 )
  1466. continue;
  1467. sd->status.skill[i].id = skill_id;
  1468. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1469. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1470. }
  1471. //Link Bard skills to dancer.
  1472. else {
  1473. if( sd->status.skill[i].lv < 10 )
  1474. continue;
  1475. sd->status.skill[i-8].id = skill_id - 8;
  1476. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1477. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1478. }
  1479. }
  1480. }
  1481. // Removes Taekwon Ranker skill bonus
  1482. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1483. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1484. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1485. uint16 sk_id = skill_tree[c_][i].skill_id;
  1486. uint16 sk_idx = 0;
  1487. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1488. continue;
  1489. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1490. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1491. continue;
  1492. sd->status.skill[sk_idx].id = 0;
  1493. }
  1494. }
  1495. }
  1496. // Grant all skills
  1497. pc_grant_allskills(sd, false);
  1498. do {
  1499. uint16 skid = 0;
  1500. flag = 0;
  1501. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1502. bool fail = false;
  1503. uint16 sk_idx = skill_get_index(skid);
  1504. if (sd->status.skill[sk_idx].id)
  1505. continue; //Skill already known.
  1506. if (!battle_config.skillfree) {
  1507. uint8 j;
  1508. // Checking required skills
  1509. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1510. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1511. uint16 sk_need_idx = 0;
  1512. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1513. short sk_need = sk_need_id;
  1514. 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)
  1515. sk_need = 0; //Not learned.
  1516. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1517. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1518. else
  1519. sk_need = pc_checkskill(sd,sk_need_id);
  1520. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1521. fail = true;
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1527. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1528. class_ = pc_class2idx(class_);
  1529. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1530. fail = true; // base level requirement wasn't satisfied
  1531. }
  1532. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1533. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1534. class_ = pc_class2idx(class_);
  1535. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1536. fail = true; // job level requirement wasn't satisfied
  1537. }
  1538. }
  1539. if (!fail) {
  1540. int inf2 = skill_get_inf2(skid);
  1541. if (!sd->status.skill[sk_idx].lv && (
  1542. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1543. inf2&INF2_WEDDING_SKILL ||
  1544. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1545. ))
  1546. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1547. sd->status.skill[sk_idx].id = skid;
  1548. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1549. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1550. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1551. }
  1552. flag = 1; // skill list has changed, perform another pass
  1553. }
  1554. }
  1555. } while(flag);
  1556. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1557. unsigned short skid = 0;
  1558. /* Taekwon Ranker Bonus Skill Tree
  1559. ============================================
  1560. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1561. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1562. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1563. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1564. uint16 sk_idx = 0;
  1565. if (!(sk_idx = skill_get_index(skid)))
  1566. continue;
  1567. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1568. continue; //Do not include Quest/Wedding skills.
  1569. if( sd->status.skill[sk_idx].id == 0 ) {
  1570. sd->status.skill[sk_idx].id = skid;
  1571. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1572. } else if( skid != NV_BASIC )
  1573. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1574. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1575. }
  1576. }
  1577. }
  1578. //Checks if you can learn a new skill after having leveled up a skill.
  1579. static void pc_check_skilltree(struct map_session_data *sd)
  1580. {
  1581. int i, flag = 0;
  1582. int c = 0;
  1583. if (battle_config.skillfree)
  1584. return; //Function serves no purpose if this is set
  1585. i = pc_calc_skilltree_normalize_job(sd);
  1586. c = pc_mapid2jobid(i, sd->status.sex);
  1587. if (c == -1) { //Unable to normalize job??
  1588. 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);
  1589. return;
  1590. }
  1591. c = pc_class2idx(c);
  1592. do {
  1593. uint16 skid = 0;
  1594. flag = 0;
  1595. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1596. uint16 sk_idx = skill_get_index(skid);
  1597. bool fail = false;
  1598. uint8 j = 0;
  1599. if (sd->status.skill[sk_idx].id) //Already learned
  1600. continue;
  1601. // Checking required skills
  1602. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1603. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1604. uint16 sk_need_idx = 0;
  1605. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1606. short sk_need = sk_need_id;
  1607. 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)
  1608. sk_need = 0; //Not learned.
  1609. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1610. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1611. else
  1612. sk_need = pc_checkskill(sd,sk_need_id);
  1613. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1614. fail = true;
  1615. break;
  1616. }
  1617. }
  1618. }
  1619. if( fail )
  1620. continue;
  1621. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1622. continue;
  1623. j = skill_get_inf2(skid);
  1624. if( !sd->status.skill[sk_idx].lv && (
  1625. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1626. j&INF2_WEDDING_SKILL ||
  1627. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1628. ) )
  1629. continue; //Cannot be learned via normal means.
  1630. sd->status.skill[sk_idx].id = skid;
  1631. flag = 1;
  1632. }
  1633. } while(flag);
  1634. }
  1635. // Make sure all the skills are in the correct condition
  1636. // before persisting to the backend.. [MouseJstr]
  1637. void pc_clean_skilltree(struct map_session_data *sd)
  1638. {
  1639. uint16 i;
  1640. for (i = 0; i < MAX_SKILL; i++){
  1641. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1642. sd->status.skill[i].id = 0;
  1643. sd->status.skill[i].lv = 0;
  1644. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1645. }
  1646. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1647. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1648. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1649. }
  1650. }
  1651. }
  1652. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1653. {
  1654. int skill_point, novice_skills;
  1655. int c = sd->class_;
  1656. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1657. return c;
  1658. skill_point = pc_calc_skillpoint(sd);
  1659. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1660. // limit 1st class and above to novice job levels
  1661. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1662. {
  1663. c = MAPID_NOVICE;
  1664. }
  1665. // limit 2nd class and above to first class job levels (super novices are exempt)
  1666. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1667. {
  1668. // regenerate change_level_2nd
  1669. if (!sd->change_level_2nd)
  1670. {
  1671. if (sd->class_&JOBL_THIRD)
  1672. {
  1673. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1674. if (!sd->change_level_3rd)
  1675. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1676. else
  1677. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1678. - (sd->status.job_level - 1)
  1679. - (sd->change_level_3rd - 1)
  1680. - novice_skills;
  1681. }
  1682. else
  1683. {
  1684. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1685. - (sd->status.job_level - 1)
  1686. - novice_skills;
  1687. }
  1688. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1689. }
  1690. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1691. {
  1692. c &= MAPID_BASEMASK;
  1693. }
  1694. // limit 3rd class to 2nd class/trans job levels
  1695. else if(sd->class_&JOBL_THIRD)
  1696. {
  1697. // regenerate change_level_3rd
  1698. if (!sd->change_level_3rd)
  1699. {
  1700. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1701. - (sd->status.job_level - 1)
  1702. - (sd->change_level_2nd - 1)
  1703. - novice_skills;
  1704. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1705. }
  1706. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1707. c &= MAPID_UPPERMASK;
  1708. }
  1709. }
  1710. // restore non-limiting flags
  1711. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1712. return c;
  1713. }
  1714. /*==========================================
  1715. * Updates the weight status
  1716. *------------------------------------------
  1717. * 1: overweight 50% for pre-renewal and 70% for renewal
  1718. * 2: overweight 90%
  1719. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1720. */
  1721. void pc_updateweightstatus(struct map_session_data *sd)
  1722. {
  1723. int old_overweight;
  1724. int new_overweight;
  1725. nullpo_retv(sd);
  1726. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1727. #ifdef RENEWAL
  1728. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1729. #else
  1730. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1731. #endif
  1732. if( old_overweight == new_overweight )
  1733. return; // no change
  1734. // stop old status change
  1735. if( old_overweight == 1 )
  1736. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1737. else if( old_overweight == 2 )
  1738. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1739. // start new status change
  1740. if( new_overweight == 1 )
  1741. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1742. else if( new_overweight == 2 )
  1743. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1744. // update overweight status
  1745. sd->regen.state.overweight = new_overweight;
  1746. }
  1747. int pc_disguise(struct map_session_data *sd, int class_)
  1748. {
  1749. if (!class_ && !sd->disguise)
  1750. return 0;
  1751. if (class_ && sd->disguise == class_)
  1752. return 0;
  1753. if(pc_isinvisible(sd))
  1754. { //Character is invisible. Stealth class-change. [Skotlex]
  1755. sd->disguise = class_; //viewdata is set on uncloaking.
  1756. return 2;
  1757. }
  1758. if (sd->bl.prev != NULL) {
  1759. pc_stop_walking(sd, 0);
  1760. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1761. }
  1762. if (!class_) {
  1763. sd->disguise = 0;
  1764. class_ = sd->status.class_;
  1765. } else
  1766. sd->disguise=class_;
  1767. status_set_viewdata(&sd->bl, class_);
  1768. clif_changeoption(&sd->bl);
  1769. if (sd->bl.prev != NULL) {
  1770. clif_spawn(&sd->bl);
  1771. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1772. { //It seems the cart info is lost on undisguise.
  1773. clif_cartlist(sd);
  1774. clif_updatestatus(sd,SP_CARTINFO);
  1775. }
  1776. if (sd->chatID) {
  1777. struct chat_data* cd;
  1778. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1779. clif_dispchat(cd,0);
  1780. }
  1781. }
  1782. return 1;
  1783. }
  1784. /// Show error message
  1785. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1786. /// Check for valid Element, break & show error message if invalid Element
  1787. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1788. /// Check for valid Race, break & show error message if invalid Race
  1789. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1790. /// Check for valid Race2, break & show error message if invalid Race2
  1791. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1792. /// Check for valid Class, break & show error message if invalid Class
  1793. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1794. /// Check for valid Size, break & show error message if invalid Size
  1795. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1796. /// Check for valid SC, break & show error message if invalid SC
  1797. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1798. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1799. {
  1800. uint8 i;
  1801. if( !rate )
  1802. return;
  1803. for( i = 0; i < max && spell[i].id; i++ )
  1804. {
  1805. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1806. {
  1807. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1808. return;
  1809. rate += spell[i].rate;
  1810. break;
  1811. }
  1812. }
  1813. if (i == max) {
  1814. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1815. return;
  1816. }
  1817. spell[i].id = id;
  1818. spell[i].lv = lv;
  1819. spell[i].rate = rate;
  1820. //Auto-update flag value.
  1821. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1822. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1823. if (!(flag&BF_SKILLMASK)) {
  1824. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1825. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1826. }
  1827. spell[i].flag|= flag;
  1828. spell[i].card_id = card_id;
  1829. }
  1830. static void pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1831. {
  1832. uint8 i;
  1833. if( !rate )
  1834. return;
  1835. for( i = 0; i < max && spell[i].id; i++ )
  1836. {
  1837. ; // each autospell works independently
  1838. }
  1839. if( i == max )
  1840. {
  1841. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1842. return;
  1843. }
  1844. spell[i].flag = src_skill;
  1845. spell[i].id = id;
  1846. spell[i].lv = lv;
  1847. spell[i].rate = rate;
  1848. spell[i].card_id = card_id;
  1849. return;
  1850. }
  1851. /**
  1852. * Add inflict effect bonus for player while attacking/atatcked
  1853. * @param effect Effect array
  1854. * @param pmax Max array
  1855. * @param sc SC/Effect type
  1856. * @param rate Success chance
  1857. * @param arrow_rate success chance if bonus comes from arrow-type item
  1858. * @param flag Target flag
  1859. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1860. **/
  1861. static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  1862. {
  1863. uint16 i;
  1864. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1865. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1866. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1867. flag |= ATF_TARGET; //Default target: enemy.
  1868. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1869. flag |= ATF_WEAPON; //Default type: weapon.
  1870. if (!duration)
  1871. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1872. for (i = 0; i < pmax && effect[i].flag; i++) {
  1873. if (effect[i].sc == sc && effect[i].flag == flag) {
  1874. effect[i].rate += rate;
  1875. effect[i].arrow_rate += arrow_rate;
  1876. effect[i].duration = umax(effect[i].duration, duration);
  1877. return;
  1878. }
  1879. }
  1880. if (i == pmax) {
  1881. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1882. return;
  1883. }
  1884. effect[i].sc = sc;
  1885. effect[i].rate = rate;
  1886. effect[i].arrow_rate = arrow_rate;
  1887. effect[i].flag = flag;
  1888. effect[i].duration = duration;
  1889. }
  1890. /**
  1891. * Add inflict effect bonus for player while attacking using skill
  1892. * @param effect Effect array
  1893. * @param pmax Max array
  1894. * @param sc SC/Effect type
  1895. * @param rate Success chance
  1896. * @param flag Target flag
  1897. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1898. **/
  1899. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  1900. {
  1901. uint8 i;
  1902. if (!duration)
  1903. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1904. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1905. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1906. effect[i].rate += rate;
  1907. effect[i].duration = umax(effect[i].duration, duration);
  1908. return;
  1909. }
  1910. }
  1911. if( i == pmax ) {
  1912. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1913. return;
  1914. }
  1915. effect[i].sc = sc;
  1916. effect[i].rate = rate;
  1917. effect[i].skill_id = skill_id;
  1918. effect[i].target = target;
  1919. effect[i].duration = duration;
  1920. }
  1921. /** Adjust/add drop rate modifier for player
  1922. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1923. * @param max: Max bonus can be received
  1924. * @param nameid: item id that will be dropped
  1925. * @param group: group id
  1926. * @param class_: target class
  1927. * @param race: target race. if < 0, means monster_id
  1928. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1929. */
  1930. static void pc_bonus_item_drop(struct s_add_drop *drop, const short max, unsigned short nameid, uint16 group, int class_, short race, int rate)
  1931. {
  1932. uint8 i;
  1933. if (!nameid && !group) {
  1934. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1935. return;
  1936. }
  1937. if (nameid && !itemdb_exists(nameid)) {
  1938. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1939. return;
  1940. }
  1941. if (group && !itemdb_group_exists(group)) {
  1942. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1943. return;
  1944. }
  1945. //Apply config rate adjustment settings.
  1946. if (rate >= 0) { //Absolute drop.
  1947. if (battle_config.item_rate_adddrop != 100)
  1948. rate = rate*battle_config.item_rate_adddrop/100;
  1949. if (rate < battle_config.item_drop_adddrop_min)
  1950. rate = battle_config.item_drop_adddrop_min;
  1951. else if (rate > battle_config.item_drop_adddrop_max)
  1952. rate = battle_config.item_drop_adddrop_max;
  1953. } else { //Relative drop, max/min limits are applied at drop time.
  1954. if (battle_config.item_rate_adddrop != 100)
  1955. rate = rate*battle_config.item_rate_adddrop/100;
  1956. if (rate > -1)
  1957. rate = -1;
  1958. }
  1959. //Find match entry, and adjust the rate only
  1960. for (i = 0; i < max; i++) {
  1961. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1962. continue;
  1963. if (drop[i].nameid == nameid &&
  1964. drop[i].group == group &&
  1965. drop[i].race == race &&
  1966. drop[i].class_ == class_
  1967. )
  1968. {
  1969. //Adjust the rate if it has same classification
  1970. if ((rate < 0 && drop[i].rate < 0) ||
  1971. (rate > 0 && drop[i].rate > 0))
  1972. {
  1973. drop[i].rate += rate;
  1974. return;
  1975. }
  1976. }
  1977. }
  1978. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1979. if (i >= max) {
  1980. 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,nameid,group,class_,race,rate);
  1981. return;
  1982. }
  1983. drop[i].nameid = nameid;
  1984. drop[i].group = group;
  1985. drop[i].race = race;
  1986. drop[i].class_ = class_;
  1987. drop[i].rate = rate;
  1988. }
  1989. bool pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned int pos,bool onskill)
  1990. {
  1991. int i;
  1992. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1993. if( i == max )
  1994. {
  1995. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1996. return false;
  1997. }
  1998. if( !onskill )
  1999. {
  2000. if( !(flag&BF_RANGEMASK) )
  2001. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  2002. if( !(flag&BF_WEAPONMASK) )
  2003. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  2004. if( !(flag&BF_SKILLMASK) )
  2005. {
  2006. if( flag&(BF_MAGIC|BF_MISC) )
  2007. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  2008. if( flag&BF_WEAPON )
  2009. flag|=BF_NORMAL|BF_SKILL;
  2010. }
  2011. }
  2012. bonus[i].rate = rate;
  2013. bonus[i].duration = dur;
  2014. bonus[i].active = INVALID_TIMER;
  2015. bonus[i].atk_type = flag;
  2016. bonus[i].pos = pos;
  2017. bonus[i].bonus_script = aStrdup(script);
  2018. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  2019. return true;
  2020. }
  2021. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  2022. {
  2023. int i;
  2024. if (!sd)
  2025. return;
  2026. for( i = 0; i < max; i++ )
  2027. {
  2028. if( autobonus[i].active != INVALID_TIMER )
  2029. {
  2030. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  2031. {
  2032. if( autobonus[i].bonus_script )
  2033. {
  2034. int j;
  2035. unsigned int equip_pos_idx = 0;
  2036. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2037. for(j = 0; j < EQI_MAX; j++) {
  2038. if(sd->equip_index[j] >= 0)
  2039. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2040. }
  2041. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  2042. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  2043. }
  2044. continue;
  2045. }
  2046. else
  2047. { // Logout / Unequipped an item with an activated bonus
  2048. delete_timer(autobonus[i].active,pc_endautobonus);
  2049. autobonus[i].active = INVALID_TIMER;
  2050. }
  2051. }
  2052. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  2053. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  2054. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  2055. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  2056. autobonus[i].active = INVALID_TIMER;
  2057. }
  2058. }
  2059. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  2060. {
  2061. if (!sd || !autobonus)
  2062. return;
  2063. if (autobonus->active != INVALID_TIMER)
  2064. delete_timer(autobonus->active, pc_endautobonus);
  2065. if( autobonus->other_script )
  2066. {
  2067. int j;
  2068. unsigned int equip_pos_idx = 0;
  2069. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2070. for(j = 0; j < EQI_MAX; j++) {
  2071. if(sd->equip_index[j] >= 0)
  2072. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2073. }
  2074. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2075. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2076. }
  2077. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  2078. sd->state.autobonus |= autobonus->pos;
  2079. status_calc_pc(sd,SCO_FORCE);
  2080. }
  2081. TIMER_FUNC(pc_endautobonus){
  2082. struct map_session_data *sd = map_id2sd(id);
  2083. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2084. nullpo_ret(sd);
  2085. nullpo_ret(autobonus);
  2086. autobonus->active = INVALID_TIMER;
  2087. sd->state.autobonus &= ~autobonus->pos;
  2088. status_calc_pc(sd,SCO_FORCE);
  2089. return 0;
  2090. }
  2091. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2092. {
  2093. uint8 i;
  2094. struct weapon_data* wd;
  2095. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2096. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2097. if (i == MAX_PC_BONUS)
  2098. {
  2099. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2100. return;
  2101. }
  2102. if (!(flag&BF_RANGEMASK))
  2103. flag |= BF_SHORT|BF_LONG;
  2104. if (!(flag&BF_WEAPONMASK))
  2105. flag |= BF_WEAPON;
  2106. if (!(flag&BF_SKILLMASK))
  2107. {
  2108. if (flag&(BF_MAGIC|BF_MISC))
  2109. flag |= BF_SKILL;
  2110. if (flag&BF_WEAPON)
  2111. flag |= BF_NORMAL|BF_SKILL;
  2112. }
  2113. wd->addele2[i].ele = ele;
  2114. wd->addele2[i].rate = rate;
  2115. wd->addele2[i].flag = flag;
  2116. }
  2117. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2118. {
  2119. uint8 i;
  2120. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2121. if (i == MAX_PC_BONUS)
  2122. {
  2123. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2124. return;
  2125. }
  2126. if (!(flag&BF_RANGEMASK))
  2127. flag |= BF_SHORT|BF_LONG;
  2128. if (!(flag&BF_WEAPONMASK))
  2129. flag |= BF_WEAPON;
  2130. if (!(flag&BF_SKILLMASK))
  2131. {
  2132. if (flag&(BF_MAGIC|BF_MISC))
  2133. flag |= BF_SKILL;
  2134. if (flag&BF_WEAPON)
  2135. flag |= BF_NORMAL|BF_SKILL;
  2136. }
  2137. sd->subele2[i].ele = ele;
  2138. sd->subele2[i].rate = rate;
  2139. sd->subele2[i].flag = flag;
  2140. }
  2141. /** Add item group heal rate bonus to player
  2142. * @param sd Player
  2143. * @param group_id Item Group ID
  2144. * @param rate
  2145. * @author Cydh
  2146. */
  2147. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2148. struct s_pc_itemgrouphealrate *entry;
  2149. uint8 i;
  2150. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2151. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2152. break;
  2153. }
  2154. if (i != sd->itemgrouphealrate_count) {
  2155. sd->itemgrouphealrate[i]->rate += rate;
  2156. return;
  2157. }
  2158. if (i >= UINT8_MAX) {
  2159. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2160. return;
  2161. }
  2162. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2163. entry->group_id = group_id;
  2164. entry->rate = rate;
  2165. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2166. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2167. }
  2168. /** Clear item group heal rate from player
  2169. * @param sd Player
  2170. * @author Cydh
  2171. */
  2172. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2173. if (!sd || !sd->itemgrouphealrate_count)
  2174. return;
  2175. else {
  2176. uint8 i;
  2177. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2178. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2179. sd->itemgrouphealrate_count = 0;
  2180. aFree(sd->itemgrouphealrate);
  2181. sd->itemgrouphealrate = NULL;
  2182. }
  2183. }
  2184. /*==========================================
  2185. * Add a bonus(type) to player sd
  2186. * format: bonus bBonusName,val;
  2187. * @param sd
  2188. * @param type Bonus type used by bBonusName
  2189. * @param val Value that usually for rate or fixed value
  2190. *------------------------------------------*/
  2191. void pc_bonus(struct map_session_data *sd,int type,int val)
  2192. {
  2193. struct status_data *status;
  2194. int bonus;
  2195. nullpo_retv(sd);
  2196. status = &sd->base_status;
  2197. switch(type){
  2198. case SP_STR:
  2199. case SP_AGI:
  2200. case SP_VIT:
  2201. case SP_INT:
  2202. case SP_DEX:
  2203. case SP_LUK:
  2204. if(sd->state.lr_flag != 2)
  2205. sd->param_bonus[type-SP_STR]+=val;
  2206. break;
  2207. case SP_ATK1:
  2208. if(!sd->state.lr_flag) {
  2209. bonus = status->rhw.atk + val;
  2210. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2211. }
  2212. else if(sd->state.lr_flag == 1) {
  2213. bonus = status->lhw.atk + val;
  2214. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2215. }
  2216. break;
  2217. case SP_ATK2:
  2218. if(!sd->state.lr_flag) {
  2219. bonus = status->rhw.atk2 + val;
  2220. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2221. }
  2222. else if(sd->state.lr_flag == 1) {
  2223. bonus = status->lhw.atk2 + val;
  2224. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2225. }
  2226. break;
  2227. case SP_BASE_ATK:
  2228. if(sd->state.lr_flag != 2) {
  2229. #ifdef RENEWAL
  2230. bonus = sd->bonus.eatk + val;
  2231. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2232. #else
  2233. bonus = status->batk + val;
  2234. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2235. #endif
  2236. }
  2237. break;
  2238. case SP_DEF1:
  2239. if(sd->state.lr_flag != 2) {
  2240. bonus = status->def + val;
  2241. #ifdef RENEWAL
  2242. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2243. #else
  2244. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2245. #endif
  2246. }
  2247. break;
  2248. case SP_DEF2:
  2249. if(sd->state.lr_flag != 2) {
  2250. bonus = status->def2 + val;
  2251. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2252. }
  2253. break;
  2254. case SP_MDEF1:
  2255. if(sd->state.lr_flag != 2) {
  2256. bonus = status->mdef + val;
  2257. #ifdef RENEWAL
  2258. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2259. #else
  2260. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2261. #endif
  2262. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2263. sd->bonus.shieldmdef += bonus;
  2264. }
  2265. }
  2266. break;
  2267. case SP_MDEF2:
  2268. if(sd->state.lr_flag != 2) {
  2269. bonus = status->mdef2 + val;
  2270. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2271. }
  2272. break;
  2273. case SP_HIT:
  2274. if(sd->state.lr_flag != 2) {
  2275. bonus = status->hit + val;
  2276. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2277. } else
  2278. sd->bonus.arrow_hit+=val;
  2279. break;
  2280. case SP_FLEE1:
  2281. if(sd->state.lr_flag != 2) {
  2282. bonus = status->flee + val;
  2283. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2284. }
  2285. break;
  2286. case SP_FLEE2:
  2287. if(sd->state.lr_flag != 2) {
  2288. bonus = status->flee2 + val*10;
  2289. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2290. }
  2291. break;
  2292. case SP_CRITICAL:
  2293. if(sd->state.lr_flag != 2) {
  2294. bonus = status->cri + val*10;
  2295. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2296. } else
  2297. sd->bonus.arrow_cri += val*10;
  2298. break;
  2299. case SP_ATKELE:
  2300. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2301. switch (sd->state.lr_flag)
  2302. {
  2303. case 2:
  2304. switch (sd->status.weapon) {
  2305. case W_BOW:
  2306. case W_REVOLVER:
  2307. case W_RIFLE:
  2308. case W_GATLING:
  2309. case W_SHOTGUN:
  2310. case W_GRENADE:
  2311. //Become weapon element.
  2312. status->rhw.ele=val;
  2313. break;
  2314. default: //Become arrow element.
  2315. sd->bonus.arrow_ele=val;
  2316. break;
  2317. }
  2318. break;
  2319. case 1:
  2320. status->lhw.ele=val;
  2321. break;
  2322. default:
  2323. status->rhw.ele=val;
  2324. break;
  2325. }
  2326. break;
  2327. case SP_DEFELE:
  2328. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2329. if(sd->state.lr_flag != 2)
  2330. status->def_ele=val;
  2331. break;
  2332. case SP_MAXHP:
  2333. if(sd->state.lr_flag == 2)
  2334. break;
  2335. sd->bonus.hp += val;
  2336. break;
  2337. case SP_MAXSP:
  2338. if(sd->state.lr_flag == 2)
  2339. break;
  2340. sd->bonus.sp += val;
  2341. break;
  2342. case SP_MAXHPRATE:
  2343. if(sd->state.lr_flag != 2)
  2344. sd->hprate+=val;
  2345. break;
  2346. case SP_MAXSPRATE:
  2347. if(sd->state.lr_flag != 2)
  2348. sd->sprate+=val;
  2349. break;
  2350. case SP_SPRATE:
  2351. if(sd->state.lr_flag != 2)
  2352. sd->dsprate+=val;
  2353. break;
  2354. case SP_ATTACKRANGE:
  2355. switch (sd->state.lr_flag) {
  2356. case 2:
  2357. switch (sd->status.weapon) {
  2358. case W_BOW:
  2359. case W_REVOLVER:
  2360. case W_RIFLE:
  2361. case W_GATLING:
  2362. case W_SHOTGUN:
  2363. case W_GRENADE:
  2364. status->rhw.range += val;
  2365. }
  2366. break;
  2367. case 1:
  2368. status->lhw.range += val;
  2369. break;
  2370. default:
  2371. status->rhw.range += val;
  2372. break;
  2373. }
  2374. break;
  2375. case SP_SPEED_RATE: //Non stackable increase
  2376. if(sd->state.lr_flag != 2)
  2377. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2378. break;
  2379. case SP_SPEED_ADDRATE: //Stackable increase
  2380. if(sd->state.lr_flag != 2)
  2381. sd->bonus.speed_add_rate -= val;
  2382. break;
  2383. case SP_ASPD: //Raw increase
  2384. if(sd->state.lr_flag != 2)
  2385. sd->bonus.aspd_add -= 10*val;
  2386. break;
  2387. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2388. if(sd->state.lr_flag != 2)
  2389. #ifndef RENEWAL_ASPD
  2390. status->aspd_rate -= 10*val;
  2391. #else
  2392. status->aspd_rate2 += val;
  2393. #endif
  2394. break;
  2395. case SP_HP_RECOV_RATE:
  2396. if(sd->state.lr_flag != 2)
  2397. sd->hprecov_rate += val;
  2398. break;
  2399. case SP_SP_RECOV_RATE:
  2400. if(sd->state.lr_flag != 2)
  2401. sd->sprecov_rate += val;
  2402. break;
  2403. case SP_CRITICAL_DEF:
  2404. if(sd->state.lr_flag != 2)
  2405. sd->bonus.critical_def += val;
  2406. break;
  2407. case SP_NEAR_ATK_DEF:
  2408. if(sd->state.lr_flag != 2)
  2409. sd->bonus.near_attack_def_rate += val;
  2410. break;
  2411. case SP_LONG_ATK_DEF:
  2412. if(sd->state.lr_flag != 2)
  2413. sd->bonus.long_attack_def_rate += val;
  2414. break;
  2415. case SP_DOUBLE_RATE:
  2416. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2417. sd->bonus.double_rate = val;
  2418. break;
  2419. case SP_DOUBLE_ADD_RATE:
  2420. if(sd->state.lr_flag == 0)
  2421. sd->bonus.double_add_rate += val;
  2422. break;
  2423. case SP_MATK_RATE:
  2424. if(sd->state.lr_flag != 2)
  2425. sd->matk_rate += val;
  2426. break;
  2427. case SP_IGNORE_DEF_ELE:
  2428. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2429. if(!sd->state.lr_flag)
  2430. sd->right_weapon.ignore_def_ele |= 1<<val;
  2431. else if(sd->state.lr_flag == 1)
  2432. sd->left_weapon.ignore_def_ele |= 1<<val;
  2433. break;
  2434. case SP_IGNORE_DEF_RACE:
  2435. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2436. if(!sd->state.lr_flag)
  2437. sd->right_weapon.ignore_def_race |= 1<<val;
  2438. else if(sd->state.lr_flag == 1)
  2439. sd->left_weapon.ignore_def_race |= 1<<val;
  2440. break;
  2441. case SP_IGNORE_DEF_CLASS:
  2442. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2443. if(!sd->state.lr_flag)
  2444. sd->right_weapon.ignore_def_class |= 1<<val;
  2445. else if(sd->state.lr_flag == 1)
  2446. sd->left_weapon.ignore_def_class |= 1<<val;
  2447. break;
  2448. case SP_ATK_RATE:
  2449. if(sd->state.lr_flag != 2)
  2450. sd->bonus.atk_rate += val;
  2451. break;
  2452. case SP_MAGIC_ATK_DEF:
  2453. if(sd->state.lr_flag != 2)
  2454. sd->bonus.magic_def_rate += val;
  2455. break;
  2456. case SP_MISC_ATK_DEF:
  2457. if(sd->state.lr_flag != 2)
  2458. sd->bonus.misc_def_rate += val;
  2459. break;
  2460. case SP_IGNORE_MDEF_ELE:
  2461. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2462. if(sd->state.lr_flag != 2)
  2463. sd->bonus.ignore_mdef_ele |= 1<<val;
  2464. break;
  2465. case SP_IGNORE_MDEF_RACE:
  2466. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2467. if(sd->state.lr_flag != 2)
  2468. sd->bonus.ignore_mdef_race |= 1<<val;
  2469. break;
  2470. case SP_PERFECT_HIT_RATE:
  2471. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2472. sd->bonus.perfect_hit = val;
  2473. break;
  2474. case SP_PERFECT_HIT_ADD_RATE:
  2475. if(sd->state.lr_flag != 2)
  2476. sd->bonus.perfect_hit_add += val;
  2477. break;
  2478. case SP_CRITICAL_RATE:
  2479. if(sd->state.lr_flag != 2)
  2480. sd->critical_rate+=val;
  2481. break;
  2482. case SP_DEF_RATIO_ATK_ELE:
  2483. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2484. if(!sd->state.lr_flag)
  2485. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2486. else if(sd->state.lr_flag == 1)
  2487. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2488. break;
  2489. case SP_DEF_RATIO_ATK_RACE:
  2490. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2491. if(!sd->state.lr_flag)
  2492. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2493. else if(sd->state.lr_flag == 1)
  2494. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2495. break;
  2496. case SP_DEF_RATIO_ATK_CLASS:
  2497. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2498. if(!sd->state.lr_flag)
  2499. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2500. else if(sd->state.lr_flag == 1)
  2501. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2502. break;
  2503. case SP_HIT_RATE:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->hit_rate += val;
  2506. break;
  2507. case SP_FLEE_RATE:
  2508. if(sd->state.lr_flag != 2)
  2509. sd->flee_rate += val;
  2510. break;
  2511. case SP_FLEE2_RATE:
  2512. if(sd->state.lr_flag != 2)
  2513. sd->flee2_rate += val;
  2514. break;
  2515. case SP_DEF_RATE:
  2516. if(sd->state.lr_flag != 2)
  2517. sd->def_rate += val;
  2518. break;
  2519. case SP_DEF2_RATE:
  2520. if(sd->state.lr_flag != 2)
  2521. sd->def2_rate += val;
  2522. break;
  2523. case SP_MDEF_RATE:
  2524. if(sd->state.lr_flag != 2)
  2525. sd->mdef_rate += val;
  2526. break;
  2527. case SP_MDEF2_RATE:
  2528. if(sd->state.lr_flag != 2)
  2529. sd->mdef2_rate += val;
  2530. break;
  2531. case SP_RESTART_FULL_RECOVER:
  2532. if(sd->state.lr_flag != 2)
  2533. sd->special_state.restart_full_recover = 1;
  2534. break;
  2535. case SP_NO_CASTCANCEL:
  2536. if(sd->state.lr_flag != 2)
  2537. sd->special_state.no_castcancel = 1;
  2538. break;
  2539. case SP_NO_CASTCANCEL2:
  2540. if(sd->state.lr_flag != 2)
  2541. sd->special_state.no_castcancel2 = 1;
  2542. break;
  2543. case SP_NO_SIZEFIX:
  2544. if(sd->state.lr_flag != 2)
  2545. sd->special_state.no_sizefix = 1;
  2546. break;
  2547. case SP_NO_MAGIC_DAMAGE:
  2548. if(sd->state.lr_flag == 2)
  2549. break;
  2550. val+= sd->special_state.no_magic_damage;
  2551. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2552. break;
  2553. case SP_NO_WEAPON_DAMAGE:
  2554. if(sd->state.lr_flag == 2)
  2555. break;
  2556. val+= sd->special_state.no_weapon_damage;
  2557. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2558. break;
  2559. case SP_NO_MISC_DAMAGE:
  2560. if(sd->state.lr_flag == 2)
  2561. break;
  2562. val+= sd->special_state.no_misc_damage;
  2563. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2564. break;
  2565. case SP_NO_GEMSTONE:
  2566. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2567. sd->special_state.no_gemstone = 1;
  2568. break;
  2569. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2570. if(sd->state.lr_flag != 2) {
  2571. sd->special_state.intravision = 1;
  2572. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2573. }
  2574. break;
  2575. case SP_NO_KNOCKBACK:
  2576. if(sd->state.lr_flag != 2)
  2577. sd->special_state.no_knockback = 1;
  2578. break;
  2579. case SP_SPLASH_RANGE:
  2580. if(sd->bonus.splash_range < val)
  2581. sd->bonus.splash_range = val;
  2582. break;
  2583. case SP_SPLASH_ADD_RANGE:
  2584. sd->bonus.splash_add_range += val;
  2585. break;
  2586. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2587. if(sd->state.lr_flag != 2)
  2588. sd->bonus.short_weapon_damage_return += val;
  2589. break;
  2590. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2591. if(sd->state.lr_flag != 2)
  2592. sd->bonus.long_weapon_damage_return += val;
  2593. break;
  2594. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2595. if(sd->state.lr_flag != 2)
  2596. sd->bonus.magic_damage_return += val;
  2597. break;
  2598. case SP_ALL_STATS: // [Valaris]
  2599. if(sd->state.lr_flag!=2) {
  2600. sd->param_bonus[SP_STR-SP_STR]+=val;
  2601. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2602. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2603. sd->param_bonus[SP_INT-SP_STR]+=val;
  2604. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2605. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2606. }
  2607. break;
  2608. case SP_AGI_VIT: // [Valaris]
  2609. if(sd->state.lr_flag!=2) {
  2610. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2611. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2612. }
  2613. break;
  2614. case SP_AGI_DEX_STR: // [Valaris]
  2615. if(sd->state.lr_flag!=2) {
  2616. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2617. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2618. sd->param_bonus[SP_STR-SP_STR]+=val;
  2619. }
  2620. break;
  2621. case SP_PERFECT_HIDE: // [Valaris]
  2622. if(sd->state.lr_flag!=2)
  2623. sd->special_state.perfect_hiding=1;
  2624. break;
  2625. case SP_UNBREAKABLE:
  2626. if(sd->state.lr_flag!=2)
  2627. sd->bonus.unbreakable += val;
  2628. break;
  2629. case SP_UNBREAKABLE_WEAPON:
  2630. if(sd->state.lr_flag != 2)
  2631. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2632. break;
  2633. case SP_UNBREAKABLE_ARMOR:
  2634. if(sd->state.lr_flag != 2)
  2635. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2636. break;
  2637. case SP_UNBREAKABLE_HELM:
  2638. if(sd->state.lr_flag != 2)
  2639. sd->bonus.unbreakable_equip |= EQP_HELM;
  2640. break;
  2641. case SP_UNBREAKABLE_SHIELD:
  2642. if(sd->state.lr_flag != 2)
  2643. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2644. break;
  2645. case SP_UNBREAKABLE_GARMENT:
  2646. if(sd->state.lr_flag != 2)
  2647. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2648. break;
  2649. case SP_UNBREAKABLE_SHOES:
  2650. if(sd->state.lr_flag != 2)
  2651. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2652. break;
  2653. case SP_CLASSCHANGE: // [Valaris]
  2654. if(sd->state.lr_flag !=2)
  2655. sd->bonus.classchange=val;
  2656. break;
  2657. case SP_LONG_ATK_RATE:
  2658. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2659. sd->bonus.long_attack_atk_rate+=val;
  2660. break;
  2661. case SP_BREAK_WEAPON_RATE:
  2662. if(sd->state.lr_flag != 2)
  2663. sd->bonus.break_weapon_rate+=val;
  2664. break;
  2665. case SP_BREAK_ARMOR_RATE:
  2666. if(sd->state.lr_flag != 2)
  2667. sd->bonus.break_armor_rate+=val;
  2668. break;
  2669. case SP_ADD_STEAL_RATE:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.add_steal_rate+=val;
  2672. break;
  2673. case SP_DELAYRATE:
  2674. if(sd->state.lr_flag != 2)
  2675. sd->delayrate+=val;
  2676. break;
  2677. case SP_CRIT_ATK_RATE:
  2678. if(sd->state.lr_flag != 2)
  2679. sd->bonus.crit_atk_rate += val;
  2680. break;
  2681. case SP_NO_REGEN:
  2682. if(sd->state.lr_flag != 2)
  2683. sd->regen.state.block|=val;
  2684. break;
  2685. case SP_UNSTRIPABLE_WEAPON:
  2686. if(sd->state.lr_flag != 2)
  2687. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2688. break;
  2689. case SP_UNSTRIPABLE:
  2690. case SP_UNSTRIPABLE_ARMOR:
  2691. if(sd->state.lr_flag != 2)
  2692. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2693. break;
  2694. case SP_UNSTRIPABLE_HELM:
  2695. if(sd->state.lr_flag != 2)
  2696. sd->bonus.unstripable_equip |= EQP_HELM;
  2697. break;
  2698. case SP_UNSTRIPABLE_SHIELD:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2701. break;
  2702. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2703. if(!sd->state.lr_flag) {
  2704. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2705. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2706. } else if(sd->state.lr_flag == 1) {
  2707. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2708. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2709. }
  2710. break;
  2711. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2712. if(!sd->state.lr_flag) {
  2713. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2714. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2715. } else if(sd->state.lr_flag == 1) {
  2716. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2717. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2718. }
  2719. break;
  2720. case SP_SP_GAIN_VALUE:
  2721. if(!sd->state.lr_flag)
  2722. sd->bonus.sp_gain_value += val;
  2723. break;
  2724. case SP_HP_GAIN_VALUE:
  2725. if(!sd->state.lr_flag)
  2726. sd->bonus.hp_gain_value += val;
  2727. break;
  2728. case SP_MAGIC_SP_GAIN_VALUE:
  2729. if(!sd->state.lr_flag)
  2730. sd->bonus.magic_sp_gain_value += val;
  2731. break;
  2732. case SP_MAGIC_HP_GAIN_VALUE:
  2733. if(!sd->state.lr_flag)
  2734. sd->bonus.magic_hp_gain_value += val;
  2735. break;
  2736. case SP_ADD_HEAL_RATE:
  2737. if(sd->state.lr_flag != 2)
  2738. sd->bonus.add_heal_rate += val;
  2739. break;
  2740. case SP_ADD_HEAL2_RATE:
  2741. if(sd->state.lr_flag != 2)
  2742. sd->bonus.add_heal2_rate += val;
  2743. break;
  2744. case SP_ADD_ITEM_HEAL_RATE:
  2745. if(sd->state.lr_flag != 2)
  2746. sd->bonus.itemhealrate2 += val;
  2747. break;
  2748. case SP_EMATK:
  2749. if(sd->state.lr_flag != 2)
  2750. sd->bonus.ematk += val;
  2751. break;
  2752. #ifdef RENEWAL_CAST
  2753. case SP_FIXCASTRATE:
  2754. if(sd->state.lr_flag != 2)
  2755. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2756. break;
  2757. case SP_ADD_FIXEDCAST:
  2758. if(sd->state.lr_flag != 2)
  2759. sd->bonus.add_fixcast += val;
  2760. break;
  2761. case SP_CASTRATE:
  2762. case SP_VARCASTRATE:
  2763. if(sd->state.lr_flag != 2)
  2764. sd->bonus.varcastrate -= val;
  2765. break;
  2766. case SP_ADD_VARIABLECAST:
  2767. if(sd->state.lr_flag != 2)
  2768. sd->bonus.add_varcast += val;
  2769. break;
  2770. #else
  2771. case SP_ADD_FIXEDCAST:
  2772. case SP_FIXCASTRATE:
  2773. case SP_ADD_VARIABLECAST:
  2774. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2775. break;
  2776. case SP_VARCASTRATE:
  2777. case SP_CASTRATE:
  2778. if(sd->state.lr_flag != 2)
  2779. sd->castrate += val;
  2780. break;
  2781. #endif
  2782. case SP_ADDMAXWEIGHT:
  2783. if (sd->state.lr_flag != 2)
  2784. sd->add_max_weight += val;
  2785. break;
  2786. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2787. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2788. break;
  2789. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2790. if (sd->state.lr_flag != 2)
  2791. sd->bonus.critical_rangeatk += val*10;
  2792. else
  2793. sd->bonus.arrow_cri += val*10;
  2794. break;
  2795. case SP_WEAPON_ATK_RATE:
  2796. if (sd->state.lr_flag != 2)
  2797. sd->bonus.weapon_atk_rate += val;
  2798. break;
  2799. case SP_WEAPON_MATK_RATE:
  2800. if (sd->state.lr_flag != 2)
  2801. sd->bonus.weapon_matk_rate += val;
  2802. break;
  2803. case SP_NO_MADO_FUEL:
  2804. if (sd->state.lr_flag != 2)
  2805. sd->special_state.no_mado_fuel = 1;
  2806. break;
  2807. default:
  2808. if (running_npc_stat_calc_event) {
  2809. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2810. }
  2811. else if (current_equip_combo_pos > 0) {
  2812. 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);
  2813. }
  2814. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2815. 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);
  2816. }
  2817. else {
  2818. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2819. }
  2820. break;
  2821. }
  2822. }
  2823. /*==========================================
  2824. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2825. * format: bonus2 bBonusName,type2,val;
  2826. * @param sd
  2827. * @param type Bonus type used by bBonusName
  2828. * @param type2
  2829. * @param val Value that usually for rate or fixed value
  2830. *------------------------------------------*/
  2831. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2832. {
  2833. int i;
  2834. nullpo_retv(sd);
  2835. switch(type){
  2836. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2837. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2838. if(!sd->state.lr_flag)
  2839. sd->right_weapon.addele[type2]+=val;
  2840. else if(sd->state.lr_flag == 1)
  2841. sd->left_weapon.addele[type2]+=val;
  2842. else if(sd->state.lr_flag == 2)
  2843. sd->arrow_addele[type2]+=val;
  2844. break;
  2845. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2846. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2847. if(!sd->state.lr_flag)
  2848. sd->right_weapon.addrace[type2]+=val;
  2849. else if(sd->state.lr_flag == 1)
  2850. sd->left_weapon.addrace[type2]+=val;
  2851. else if(sd->state.lr_flag == 2)
  2852. sd->arrow_addrace[type2]+=val;
  2853. break;
  2854. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2855. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2856. if(!sd->state.lr_flag)
  2857. sd->right_weapon.addclass[type2]+=val;
  2858. else if(sd->state.lr_flag == 1)
  2859. sd->left_weapon.addclass[type2]+=val;
  2860. else if(sd->state.lr_flag == 2)
  2861. sd->arrow_addclass[type2]+=val;
  2862. break;
  2863. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2864. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2865. if(!sd->state.lr_flag)
  2866. sd->right_weapon.addsize[type2]+=val;
  2867. else if(sd->state.lr_flag == 1)
  2868. sd->left_weapon.addsize[type2]+=val;
  2869. else if(sd->state.lr_flag == 2)
  2870. sd->arrow_addsize[type2]+=val;
  2871. break;
  2872. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2873. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2874. if(sd->state.lr_flag != 2)
  2875. sd->subele_script[type2] += val;
  2876. break;
  2877. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2878. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2879. if(sd->state.lr_flag != 2)
  2880. sd->subrace[type2]+=val;
  2881. break;
  2882. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2883. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2884. if(sd->state.lr_flag != 2)
  2885. sd->subclass[type2]+=val;
  2886. break;
  2887. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2888. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2889. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2890. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2891. break;
  2892. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2893. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2894. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2895. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2896. break;
  2897. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2898. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2899. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2900. break;
  2901. }
  2902. if(sd->state.lr_flag == 2)
  2903. break;
  2904. i = sd->reseff[type2]+val;
  2905. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2906. break;
  2907. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2908. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2909. if(sd->state.lr_flag != 2)
  2910. sd->magic_addele_script[type2] += val;
  2911. break;
  2912. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2913. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2914. if(sd->state.lr_flag != 2)
  2915. sd->magic_addrace[type2]+=val;
  2916. break;
  2917. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2918. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2919. if(sd->state.lr_flag != 2)
  2920. sd->magic_addclass[type2]+=val;
  2921. break;
  2922. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2923. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2924. if(sd->state.lr_flag != 2)
  2925. sd->magic_addsize[type2]+=val;
  2926. break;
  2927. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2928. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2929. if(sd->state.lr_flag != 2)
  2930. sd->magic_atk_ele[type2]+=val;
  2931. break;
  2932. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2933. switch (sd->state.lr_flag) {
  2934. case 0: //Right hand
  2935. ARR_FIND(0, ARRAYLENGTH(sd->right_weapon.add_dmg), i, sd->right_weapon.add_dmg[i].rate == 0 || sd->right_weapon.add_dmg[i].class_ == type2);
  2936. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2937. {
  2938. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2939. break;
  2940. }
  2941. sd->right_weapon.add_dmg[i].class_ = type2;
  2942. sd->right_weapon.add_dmg[i].rate += val;
  2943. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2944. memmove(&sd->right_weapon.add_dmg[i], &sd->right_weapon.add_dmg[i+1], sizeof(sd->right_weapon.add_dmg) - (i+1)*sizeof(sd->right_weapon.add_dmg[0]));
  2945. break;
  2946. case 1: //Left hand
  2947. ARR_FIND(0, ARRAYLENGTH(sd->left_weapon.add_dmg), i, sd->left_weapon.add_dmg[i].rate == 0 || sd->left_weapon.add_dmg[i].class_ == type2);
  2948. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2949. {
  2950. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2951. break;
  2952. }
  2953. sd->left_weapon.add_dmg[i].class_ = type2;
  2954. sd->left_weapon.add_dmg[i].rate += val;
  2955. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2956. memmove(&sd->left_weapon.add_dmg[i], &sd->left_weapon.add_dmg[i+1], sizeof(sd->left_weapon.add_dmg) - (i+1)*sizeof(sd->left_weapon.add_dmg[0]));
  2957. break;
  2958. }
  2959. break;
  2960. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2961. if(sd->state.lr_flag == 2)
  2962. break;
  2963. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2964. if (i == ARRAYLENGTH(sd->add_mdmg))
  2965. {
  2966. ShowError("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2967. break;
  2968. }
  2969. sd->add_mdmg[i].class_ = type2;
  2970. sd->add_mdmg[i].rate += val;
  2971. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2972. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2973. break;
  2974. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2975. if(sd->state.lr_flag == 2)
  2976. break;
  2977. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2978. if (i == ARRAYLENGTH(sd->add_def))
  2979. {
  2980. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2981. break;
  2982. }
  2983. sd->add_def[i].class_ = type2;
  2984. sd->add_def[i].rate += val;
  2985. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2986. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2987. break;
  2988. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2989. if(sd->state.lr_flag == 2)
  2990. break;
  2991. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2992. if (i == ARRAYLENGTH(sd->add_mdef))
  2993. {
  2994. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2995. break;
  2996. }
  2997. sd->add_mdef[i].class_ = type2;
  2998. sd->add_mdef[i].rate += val;
  2999. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  3000. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  3001. break;
  3002. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3003. if(!sd->state.lr_flag) {
  3004. sd->right_weapon.hp_drain_rate.rate += type2;
  3005. sd->right_weapon.hp_drain_rate.per += val;
  3006. }
  3007. else if(sd->state.lr_flag == 1) {
  3008. sd->left_weapon.hp_drain_rate.rate += type2;
  3009. sd->left_weapon.hp_drain_rate.per += val;
  3010. }
  3011. break;
  3012. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3013. if(!sd->state.lr_flag) {
  3014. sd->right_weapon.sp_drain_rate.rate += type2;
  3015. sd->right_weapon.sp_drain_rate.per += val;
  3016. }
  3017. else if(sd->state.lr_flag == 1) {
  3018. sd->left_weapon.sp_drain_rate.rate += type2;
  3019. sd->left_weapon.sp_drain_rate.per += val;
  3020. }
  3021. break;
  3022. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3023. if(sd->state.lr_flag != 2) {
  3024. sd->bonus.sp_vanish_rate += type2;
  3025. sd->bonus.sp_vanish_per += val;
  3026. }
  3027. break;
  3028. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3029. if(sd->state.lr_flag != 2) {
  3030. sd->bonus.hp_vanish_rate += type2;
  3031. sd->bonus.hp_vanish_per += val;
  3032. }
  3033. break;
  3034. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3035. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3036. sd->bonus.get_zeny_rate = val;
  3037. sd->bonus.get_zeny_num = type2;
  3038. }
  3039. break;
  3040. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3041. if(sd->state.lr_flag != 2) {
  3042. sd->bonus.get_zeny_rate += val;
  3043. sd->bonus.get_zeny_num += type2;
  3044. }
  3045. break;
  3046. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3047. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3048. if(sd->state.lr_flag == 2)
  3049. break;
  3050. sd->weapon_coma_ele[type2] += val;
  3051. sd->special_state.bonus_coma = 1;
  3052. break;
  3053. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3054. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3055. if(sd->state.lr_flag == 2)
  3056. break;
  3057. sd->weapon_coma_race[type2] += val;
  3058. sd->special_state.bonus_coma = 1;
  3059. break;
  3060. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3061. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3062. if(sd->state.lr_flag == 2)
  3063. break;
  3064. sd->weapon_coma_class[type2] += val;
  3065. sd->special_state.bonus_coma = 1;
  3066. break;
  3067. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3068. if(sd->state.lr_flag != 2)
  3069. sd->weapon_atk[type2]+=val;
  3070. break;
  3071. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3072. if(sd->state.lr_flag != 2)
  3073. sd->weapon_damage_rate[type2]+=val;
  3074. break;
  3075. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3076. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3077. if(sd->state.lr_flag != 2)
  3078. sd->critaddrace[type2] += val*10;
  3079. break;
  3080. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3081. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3082. if(sd->state.lr_flag != 2)
  3083. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  3084. break;
  3085. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3086. if(sd->state.lr_flag == 2)
  3087. break;
  3088. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3089. if (i == ARRAYLENGTH(sd->skillatk))
  3090. { //Better mention this so the array length can be updated. [Skotlex]
  3091. ShowError("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  3092. break;
  3093. }
  3094. if (sd->skillatk[i].id == type2)
  3095. sd->skillatk[i].val += val;
  3096. else {
  3097. sd->skillatk[i].id = type2;
  3098. sd->skillatk[i].val = val;
  3099. }
  3100. break;
  3101. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3102. if(sd->state.lr_flag == 2)
  3103. break;
  3104. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3105. if (i == ARRAYLENGTH(sd->skillheal))
  3106. { // Better mention this so the array length can be updated. [Skotlex]
  3107. ShowError("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  3108. break;
  3109. }
  3110. if (sd->skillheal[i].id == type2)
  3111. sd->skillheal[i].val += val;
  3112. else {
  3113. sd->skillheal[i].id = type2;
  3114. sd->skillheal[i].val = val;
  3115. }
  3116. break;
  3117. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3118. if(sd->state.lr_flag == 2)
  3119. break;
  3120. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3121. if (i == ARRAYLENGTH(sd->skillheal2))
  3122. { // Better mention this so the array length can be updated. [Skotlex]
  3123. ShowError("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  3124. break;
  3125. }
  3126. if (sd->skillheal2[i].id == type2)
  3127. sd->skillheal2[i].val += val;
  3128. else {
  3129. sd->skillheal2[i].id = type2;
  3130. sd->skillheal2[i].val = val;
  3131. }
  3132. break;
  3133. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3134. if(sd->state.lr_flag == 2)
  3135. break;
  3136. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3137. if (i == ARRAYLENGTH(sd->skillblown))
  3138. { //Better mention this so the array length can be updated. [Skotlex]
  3139. ShowError("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  3140. break;
  3141. }
  3142. if(sd->skillblown[i].id == type2)
  3143. sd->skillblown[i].val += val;
  3144. else {
  3145. sd->skillblown[i].id = type2;
  3146. sd->skillblown[i].val = val;
  3147. }
  3148. break;
  3149. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3150. if(sd->state.lr_flag != 2) {
  3151. sd->hp_loss.value = type2;
  3152. sd->hp_loss.rate = val;
  3153. }
  3154. break;
  3155. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3156. if(sd->state.lr_flag != 2) {
  3157. sd->hp_regen.value = type2;
  3158. sd->hp_regen.rate = val;
  3159. }
  3160. break;
  3161. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3162. if (sd->state.lr_flag != 2) {
  3163. sd->percent_hp_regen.value = type2;
  3164. sd->percent_hp_regen.rate = val;
  3165. }
  3166. break;
  3167. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3168. if (sd->state.lr_flag != 2) {
  3169. sd->percent_sp_regen.value = type2;
  3170. sd->percent_sp_regen.rate = val;
  3171. }
  3172. break;
  3173. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3174. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3175. if(sd->state.lr_flag != 2)
  3176. sd->right_weapon.addrace2[type2] += val;
  3177. else
  3178. sd->left_weapon.addrace2[type2] += val;
  3179. break;
  3180. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3181. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3182. if(sd->state.lr_flag != 2)
  3183. sd->subsize[type2]+=val;
  3184. break;
  3185. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3186. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3187. if(sd->state.lr_flag != 2)
  3188. sd->subrace2[type2]+=val;
  3189. break;
  3190. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3191. if(sd->state.lr_flag == 2)
  3192. break;
  3193. if (!itemdb_exists(type2)) {
  3194. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3195. break;
  3196. }
  3197. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3198. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3199. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3200. break;
  3201. }
  3202. sd->itemhealrate[i].nameid = type2;
  3203. sd->itemhealrate[i].rate += val;
  3204. break;
  3205. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3206. if (sd->state.lr_flag == 2)
  3207. break;
  3208. if (!type2 || !itemdb_group_exists(type2)) {
  3209. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3210. break;
  3211. }
  3212. pc_itemgrouphealrate(sd, type2, val);
  3213. break;
  3214. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3215. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3216. if(sd->state.lr_flag != 2)
  3217. sd->expaddrace[type2]+=val;
  3218. break;
  3219. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3220. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3221. if(sd->state.lr_flag != 2)
  3222. sd->expaddclass[type2]+=val;
  3223. break;
  3224. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3225. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3226. if(sd->state.lr_flag != 2)
  3227. sd->sp_gain_race[type2]+=val;
  3228. break;
  3229. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3230. if (sd->state.lr_flag != 2)
  3231. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3232. break;
  3233. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3234. if (sd->state.lr_flag != 2)
  3235. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3236. break;
  3237. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3238. if(sd->state.lr_flag != 2) {
  3239. sd->sp_loss.value = type2;
  3240. sd->sp_loss.rate = val;
  3241. }
  3242. break;
  3243. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3244. if(sd->state.lr_flag != 2) {
  3245. sd->sp_regen.value = type2;
  3246. sd->sp_regen.rate = val;
  3247. }
  3248. break;
  3249. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3250. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3251. if(!sd->state.lr_flag) {
  3252. sd->right_weapon.hp_drain_race[type2] += val;
  3253. }
  3254. else if(sd->state.lr_flag == 1) {
  3255. sd->left_weapon.hp_drain_race[type2] += val;
  3256. }
  3257. break;
  3258. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3259. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3260. if(!sd->state.lr_flag) {
  3261. sd->right_weapon.sp_drain_race[type2] += val;
  3262. }
  3263. else if(sd->state.lr_flag == 1) {
  3264. sd->left_weapon.sp_drain_race[type2] += val;
  3265. }
  3266. break;
  3267. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3268. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3269. if(!sd->state.lr_flag) {
  3270. sd->right_weapon.hp_drain_class[type2] += val;
  3271. }
  3272. else if(sd->state.lr_flag == 1) {
  3273. sd->left_weapon.hp_drain_class[type2] += val;
  3274. }
  3275. break;
  3276. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3277. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3278. if(!sd->state.lr_flag) {
  3279. sd->right_weapon.sp_drain_class[type2] += val;
  3280. }
  3281. else if(sd->state.lr_flag == 1) {
  3282. sd->left_weapon.sp_drain_class[type2] += val;
  3283. }
  3284. break;
  3285. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3286. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3287. if(sd->state.lr_flag != 2)
  3288. sd->ignore_mdef_by_race[type2] += val;
  3289. break;
  3290. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3291. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3292. if(sd->state.lr_flag != 2)
  3293. sd->ignore_mdef_by_class[type2] += val;
  3294. break;
  3295. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3296. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3297. if(sd->state.lr_flag != 2)
  3298. sd->ignore_def_by_race[type2] += val;
  3299. break;
  3300. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3301. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3302. if (sd->state.lr_flag != 2)
  3303. sd->ignore_def_by_class[type2] += val;
  3304. break;
  3305. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3306. if(sd->state.lr_flag == 2)
  3307. break;
  3308. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3309. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3310. ShowError("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  3311. break;
  3312. }
  3313. if (sd->skillusesprate[i].id == type2)
  3314. sd->skillusesprate[i].val += val;
  3315. else {
  3316. sd->skillusesprate[i].id = type2;
  3317. sd->skillusesprate[i].val = val;
  3318. }
  3319. break;
  3320. case SP_SKILL_DELAY:
  3321. if(sd->state.lr_flag == 2)
  3322. break;
  3323. ARR_FIND(0, ARRAYLENGTH(sd->skilldelay), i, sd->skilldelay[i].id == 0 || sd->skilldelay[i].id == type2);
  3324. if (i == ARRAYLENGTH(sd->skilldelay)) {
  3325. ShowError("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skilldelay), type2, val);
  3326. break;
  3327. }
  3328. if (sd->skilldelay[i].id == type2)
  3329. sd->skilldelay[i].val += val;
  3330. else {
  3331. sd->skilldelay[i].id = type2;
  3332. sd->skilldelay[i].val = val;
  3333. }
  3334. break;
  3335. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3336. if(sd->state.lr_flag == 2)
  3337. break;
  3338. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3339. if (i == ARRAYLENGTH(sd->skillcooldown))
  3340. {
  3341. ShowError("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  3342. break;
  3343. }
  3344. if (sd->skillcooldown[i].id == type2)
  3345. sd->skillcooldown[i].val += val;
  3346. else {
  3347. sd->skillcooldown[i].id = type2;
  3348. sd->skillcooldown[i].val = val;
  3349. }
  3350. break;
  3351. #ifdef RENEWAL_CAST
  3352. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3353. if(sd->state.lr_flag == 2)
  3354. break;
  3355. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3356. if (i == ARRAYLENGTH(sd->skillfixcast))
  3357. {
  3358. ShowError("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  3359. break;
  3360. }
  3361. if (sd->skillfixcast[i].id == type2)
  3362. sd->skillfixcast[i].val += val;
  3363. else {
  3364. sd->skillfixcast[i].id = type2;
  3365. sd->skillfixcast[i].val = val;
  3366. }
  3367. break;
  3368. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3369. if(sd->state.lr_flag == 2)
  3370. break;
  3371. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3372. if (i == ARRAYLENGTH(sd->skillvarcast))
  3373. {
  3374. ShowError("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  3375. break;
  3376. }
  3377. if (sd->skillvarcast[i].id == type2)
  3378. sd->skillvarcast[i].val += val;
  3379. else {
  3380. sd->skillvarcast[i].id = type2;
  3381. sd->skillvarcast[i].val = val;
  3382. }
  3383. break;
  3384. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3385. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3386. if(sd->state.lr_flag == 2)
  3387. break;
  3388. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3389. if (i == ARRAYLENGTH(sd->skillcastrate))
  3390. {
  3391. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3392. break;
  3393. }
  3394. if(sd->skillcastrate[i].id == type2)
  3395. sd->skillcastrate[i].val -= val;
  3396. else {
  3397. sd->skillcastrate[i].id = type2;
  3398. sd->skillcastrate[i].val -= val;
  3399. }
  3400. break;
  3401. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3402. if(sd->state.lr_flag == 2)
  3403. break;
  3404. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3405. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3406. {
  3407. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3408. break;
  3409. }
  3410. if(sd->skillfixcastrate[i].id == type2)
  3411. sd->skillfixcastrate[i].val -= val;
  3412. else {
  3413. sd->skillfixcastrate[i].id = type2;
  3414. sd->skillfixcastrate[i].val -= val;
  3415. }
  3416. break;
  3417. #else
  3418. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3419. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3420. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3421. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3422. break;
  3423. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3424. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3425. if(sd->state.lr_flag == 2)
  3426. break;
  3427. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3428. if (i == ARRAYLENGTH(sd->skillcastrate))
  3429. { //Better mention this so the array length can be updated. [Skotlex]
  3430. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3431. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3432. break;
  3433. }
  3434. if(sd->skillcastrate[i].id == type2)
  3435. sd->skillcastrate[i].val += val;
  3436. else {
  3437. sd->skillcastrate[i].id = type2;
  3438. sd->skillcastrate[i].val = val;
  3439. }
  3440. break;
  3441. #endif
  3442. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3443. if(sd->state.lr_flag == 2)
  3444. break;
  3445. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3446. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3447. ShowError("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  3448. break;
  3449. }
  3450. if (sd->skillusesp[i].id == type2)
  3451. sd->skillusesp[i].val += val;
  3452. else {
  3453. sd->skillusesp[i].id = type2;
  3454. sd->skillusesp[i].val = val;
  3455. }
  3456. break;
  3457. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3458. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3459. if (i == ARRAYLENGTH(sd->subskill)) {
  3460. ShowError("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->subskill), type2, val);
  3461. break;
  3462. }
  3463. if (sd->subskill[i].id == type2)
  3464. sd->subskill[i].val += val;
  3465. else {
  3466. sd->subskill[i].id = type2;
  3467. sd->subskill[i].val = val;
  3468. }
  3469. break;
  3470. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3471. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3472. sd->subdefele[type2] += val;
  3473. break;
  3474. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3475. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3476. sd->coma_class[type2] += val;
  3477. sd->special_state.bonus_coma = 1;
  3478. break;
  3479. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3480. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3481. sd->coma_race[type2] += val;
  3482. sd->special_state.bonus_coma = 1;
  3483. break;
  3484. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3485. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3486. if(sd->state.lr_flag != 2)
  3487. sd->magic_addrace2[type2] += val;
  3488. break;
  3489. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3490. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3491. if (sd->state.lr_flag != 2)
  3492. sd->ignore_mdef_by_race2[type2] += val;
  3493. break;
  3494. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3495. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3496. if (sd->state.lr_flag != 2)
  3497. sd->dropaddrace[type2] += val;
  3498. break;
  3499. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3500. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3501. if (sd->state.lr_flag != 2)
  3502. sd->dropaddclass[type2] += val;
  3503. break;
  3504. default:
  3505. if (running_npc_stat_calc_event) {
  3506. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3507. }
  3508. else if (current_equip_combo_pos > 0) {
  3509. 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);
  3510. }
  3511. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3512. 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);
  3513. }
  3514. else {
  3515. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3516. }
  3517. break;
  3518. }
  3519. }
  3520. /**
  3521. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3522. * @param sd
  3523. * @param type Bonus type used by bBonusName
  3524. * @param type2
  3525. * @param val Value that usually for rate or fixed value
  3526. */
  3527. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3528. {
  3529. nullpo_retv(sd);
  3530. switch(type){
  3531. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3532. if(sd->state.lr_flag != 2)
  3533. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3534. break;
  3535. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3536. if(sd->state.lr_flag != 2)
  3537. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3538. break;
  3539. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3540. if(sd->state.lr_flag != 2)
  3541. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3542. break;
  3543. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3544. if(sd->state.lr_flag != 2)
  3545. {
  3546. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3547. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3548. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3549. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3550. }
  3551. break;
  3552. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3553. if(sd->state.lr_flag != 2)
  3554. {
  3555. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3556. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3557. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3558. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3559. }
  3560. break;
  3561. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3562. if (sd->state.lr_flag != 2)
  3563. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3564. break;
  3565. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3566. if (sd->state.lr_flag != 2)
  3567. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3568. break;
  3569. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3570. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3571. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3572. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3573. break;
  3574. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3575. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3576. if(sd->state.lr_flag != 2)
  3577. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3578. break;
  3579. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3580. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3581. if( sd->state.lr_flag != 2 )
  3582. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3583. break;
  3584. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3585. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3586. if (sd->state.lr_flag != 2)
  3587. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3588. break;
  3589. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3590. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3591. if (sd->state.lr_flag != 2)
  3592. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3593. break;
  3594. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3595. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3596. if(sd->state.lr_flag != 2) {
  3597. sd->sp_vanish_race[type2].rate += type3;
  3598. sd->sp_vanish_race[type2].per += val;
  3599. }
  3600. break;
  3601. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3602. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3603. if(sd->state.lr_flag != 2) {
  3604. sd->hp_vanish_race[type2].rate += type3;
  3605. sd->hp_vanish_race[type2].per += val;
  3606. }
  3607. break;
  3608. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3609. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3610. if (sd->state.lr_flag == 2)
  3611. break;
  3612. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3613. //if (type3 > sd->norecover_state_race[type2].rate) {
  3614. // sd->norecover_state_race[type2].rate = type3;
  3615. // sd->norecover_state_race[type2].tick = val;
  3616. // break;
  3617. //}
  3618. sd->norecover_state_race[type2].rate = type3;
  3619. sd->norecover_state_race[type2].tick = val;
  3620. break;
  3621. default:
  3622. if (running_npc_stat_calc_event) {
  3623. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3624. }
  3625. else if (current_equip_combo_pos > 0) {
  3626. 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);
  3627. }
  3628. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3629. 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);
  3630. }
  3631. else {
  3632. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3633. }
  3634. break;
  3635. }
  3636. }
  3637. /**
  3638. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3639. * @param sd
  3640. * @param type Bonus type used by bBonusName
  3641. * @param type2
  3642. * @param type3
  3643. * @param val Value that usually for rate or fixed value
  3644. */
  3645. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3646. {
  3647. nullpo_retv(sd);
  3648. switch(type){
  3649. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3650. if(sd->state.lr_flag != 2)
  3651. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3652. break;
  3653. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3654. if(sd->state.lr_flag != 2)
  3655. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3656. break;
  3657. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3658. if(sd->state.lr_flag != 2)
  3659. {
  3660. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3661. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3662. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3663. }
  3664. break;
  3665. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3666. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3667. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3668. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3669. break;
  3670. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3671. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3672. if (sd->state.lr_flag != 2)
  3673. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3674. break;
  3675. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3676. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3677. if( sd->state.lr_flag != 2 )
  3678. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3679. break;
  3680. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3681. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3682. if(sd->state.lr_flag == 2)
  3683. break;
  3684. sd->def_set_race[type2].rate = type3;
  3685. sd->def_set_race[type2].tick = type4;
  3686. sd->def_set_race[type2].value = val;
  3687. break;
  3688. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3689. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3690. if(sd->state.lr_flag == 2)
  3691. break;
  3692. sd->mdef_set_race[type2].rate = type3;
  3693. sd->mdef_set_race[type2].tick = type4;
  3694. sd->mdef_set_race[type2].value = val;
  3695. break;
  3696. default:
  3697. if (running_npc_stat_calc_event) {
  3698. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3699. }
  3700. else if (current_equip_combo_pos > 0) {
  3701. 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);
  3702. }
  3703. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3704. 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);
  3705. }
  3706. else {
  3707. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3708. }
  3709. break;
  3710. }
  3711. }
  3712. /**
  3713. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3714. * @param sd
  3715. * @param type Bonus type used by bBonusName
  3716. * @param type2
  3717. * @param type3
  3718. * @param type4
  3719. * @param val Value that usually for rate or fixed value
  3720. */
  3721. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3722. {
  3723. nullpo_retv(sd);
  3724. switch(type){
  3725. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3726. if(sd->state.lr_flag != 2)
  3727. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3728. break;
  3729. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3730. if(sd->state.lr_flag != 2)
  3731. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3732. break;
  3733. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3734. if(sd->state.lr_flag != 2)
  3735. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3736. break;
  3737. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3738. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3739. if( sd->state.lr_flag != 2 )
  3740. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3741. break;
  3742. default:
  3743. if (running_npc_stat_calc_event) {
  3744. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3745. }
  3746. else if (current_equip_combo_pos > 0) {
  3747. 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);
  3748. }
  3749. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3750. 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);
  3751. }
  3752. else {
  3753. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3754. }
  3755. break;
  3756. }
  3757. }
  3758. /*==========================================
  3759. * Grants a player a given skill. Flag values are:
  3760. * 0 - Grant permanent skill to be bound to skill tree
  3761. * 1 - Grant an item skill (temporary)
  3762. * 2 - Like 1, except the level granted can stack with previously learned level.
  3763. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3764. *------------------------------------------*/
  3765. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3766. uint16 idx = 0;
  3767. nullpo_ret(sd);
  3768. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3769. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3770. return false;
  3771. }
  3772. if (level > MAX_SKILL_LEVEL) {
  3773. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3774. return false;
  3775. }
  3776. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3777. 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);
  3778. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3779. }
  3780. switch (type) {
  3781. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3782. sd->status.skill[idx].id = skill_id;
  3783. sd->status.skill[idx].lv = level;
  3784. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3785. if (level == 0) { //Remove skill.
  3786. sd->status.skill[idx].id = 0;
  3787. clif_deleteskill(sd,skill_id);
  3788. } else
  3789. clif_addskill(sd,skill_id);
  3790. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3791. status_calc_pc(sd, SCO_NONE);
  3792. break;
  3793. case ADDSKILL_TEMP: //Item bonus skill.
  3794. if (sd->status.skill[idx].id != 0) {
  3795. if (sd->status.skill[idx].lv >= level)
  3796. return true;
  3797. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3798. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3799. } else {
  3800. sd->status.skill[idx].id = skill_id;
  3801. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3802. }
  3803. sd->status.skill[idx].lv = level;
  3804. break;
  3805. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3806. if (sd->status.skill[idx].id != 0) {
  3807. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3808. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3809. } else {
  3810. sd->status.skill[idx].id = skill_id;
  3811. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3812. }
  3813. sd->status.skill[idx].lv += level;
  3814. break;
  3815. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3816. sd->status.skill[idx].id = skill_id;
  3817. sd->status.skill[idx].lv = level;
  3818. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3819. if (level == 0) { //Remove skill.
  3820. sd->status.skill[idx].id = 0;
  3821. clif_deleteskill(sd,skill_id);
  3822. } else
  3823. clif_addskill(sd,skill_id);
  3824. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3825. status_calc_pc(sd, SCO_NONE);
  3826. break;
  3827. default:
  3828. return false;
  3829. }
  3830. return true;
  3831. }
  3832. /*==========================================
  3833. * Append a card to an item ?
  3834. *------------------------------------------*/
  3835. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3836. {
  3837. int i;
  3838. unsigned short nameid;
  3839. nullpo_ret(sd);
  3840. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3841. return 0; //Invalid item index.
  3842. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3843. return 0; //Invalid card index.
  3844. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3845. return 0; // target item missing
  3846. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3847. return 0; // target card missing
  3848. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3849. return 0; // only weapons and armor are allowed
  3850. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3851. return 0; // must be a card
  3852. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3853. return 0; // target must be identified
  3854. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3855. return 0; // card slots reserved for other purposes
  3856. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3857. return 0; // card cannot be compounded on this item type
  3858. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3859. return 0; // attempted to place shield card on left-hand weapon.
  3860. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3861. return 0; // item must be unequipped
  3862. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3863. if( i == sd->inventory_data[idx_equip]->slot )
  3864. return 0; // no free slots
  3865. // remember the card id to insert
  3866. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3867. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3868. {// failed
  3869. clif_insert_card(sd,idx_equip,idx_card,1);
  3870. }
  3871. else
  3872. {// success
  3873. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3874. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3875. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3876. clif_insert_card(sd,idx_equip,idx_card,0);
  3877. }
  3878. return 0;
  3879. }
  3880. //
  3881. // Items
  3882. //
  3883. /*==========================================
  3884. * Update buying value by skills
  3885. *------------------------------------------*/
  3886. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3887. {
  3888. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3889. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3890. rate1 = 5+skill*2-((skill==10)? 1:0);
  3891. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3892. rate2 = 5+skill*4;
  3893. if(rate1 < rate2) rate1 = rate2;
  3894. if(rate1)
  3895. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3896. if(val < 0) val = 0;
  3897. if(orig_value > 0 && val < 1) val = 1;
  3898. return val;
  3899. }
  3900. /*==========================================
  3901. * Update selling value by skills
  3902. *------------------------------------------*/
  3903. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3904. {
  3905. int skill,val = orig_value,rate = 0;
  3906. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3907. rate = 5+skill*2-((skill==10)? 1:0);
  3908. if(rate)
  3909. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3910. if(val < 0) val = 0;
  3911. if(orig_value > 0 && val < 1) val = 1;
  3912. return val;
  3913. }
  3914. /*==========================================
  3915. * Checking if we have enough place on inventory for new item
  3916. * Make sure to take 30k as limit (for client I guess)
  3917. * @param sd
  3918. * @param nameid
  3919. * @param amount
  3920. * @return e_chkitem_result
  3921. *------------------------------------------*/
  3922. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3923. {
  3924. int i;
  3925. struct item_data* data;
  3926. nullpo_ret(sd);
  3927. if(amount > MAX_AMOUNT)
  3928. return CHKADDITEM_OVERAMOUNT;
  3929. data = itemdb_search(nameid);
  3930. if(!itemdb_isstackable2(data))
  3931. return CHKADDITEM_NEW;
  3932. if( data->stack.inventory && amount > data->stack.amount )
  3933. return CHKADDITEM_OVERAMOUNT;
  3934. for(i=0;i<MAX_INVENTORY;i++){
  3935. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3936. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3937. 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 ) )
  3938. return CHKADDITEM_OVERAMOUNT;
  3939. return CHKADDITEM_EXIST;
  3940. }
  3941. }
  3942. return CHKADDITEM_NEW;
  3943. }
  3944. /*==========================================
  3945. * Return number of available place in inventory
  3946. * Each non stackable item will reduce place by 1
  3947. * @param sd
  3948. * @return Number of empty slots
  3949. *------------------------------------------*/
  3950. uint8 pc_inventoryblank(struct map_session_data *sd)
  3951. {
  3952. uint8 i, b;
  3953. nullpo_ret(sd);
  3954. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3955. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3956. b++;
  3957. }
  3958. return b;
  3959. }
  3960. /**
  3961. * Attempts to remove zeny from player
  3962. * @param sd: Player
  3963. * @param zeny: Zeny removed
  3964. * @param type: Log type
  3965. * @param tsd: (optional) From who to log (if null take sd)
  3966. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3967. */
  3968. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3969. {
  3970. nullpo_retr(2,sd);
  3971. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3972. if( zeny < 0 )
  3973. {
  3974. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3975. return 1;
  3976. }
  3977. if( sd->status.zeny < zeny )
  3978. return 1; //Not enough.
  3979. sd->status.zeny -= zeny;
  3980. clif_updatestatus(sd,SP_ZENY);
  3981. if(!tsd) tsd = sd;
  3982. log_zeny(sd, type, tsd, -zeny);
  3983. if( zeny > 0 && sd->state.showzeny ) {
  3984. char output[255];
  3985. sprintf(output, "Removed %dz.", zeny);
  3986. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3987. }
  3988. return 0;
  3989. }
  3990. /**
  3991. * Attempts to give zeny to player
  3992. * @param sd: Player
  3993. * @param type: Log type
  3994. * @param tsd: (optional) From who to log (if null take sd)
  3995. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3996. */
  3997. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3998. {
  3999. nullpo_retr(-1,sd);
  4000. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4001. if( zeny < 0 )
  4002. {
  4003. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4004. return 1;
  4005. }
  4006. if( zeny > MAX_ZENY - sd->status.zeny )
  4007. zeny = MAX_ZENY - sd->status.zeny;
  4008. sd->status.zeny += zeny;
  4009. clif_updatestatus(sd,SP_ZENY);
  4010. if(!tsd) tsd = sd;
  4011. log_zeny(sd, type, tsd, zeny);
  4012. if( zeny > 0 && sd->state.showzeny ) {
  4013. char output[255];
  4014. sprintf(output, "Gained %dz.", zeny);
  4015. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4016. }
  4017. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4018. return 0;
  4019. }
  4020. /**
  4021. * Attempts to remove Cash Points from player
  4022. * @param sd: Player
  4023. * @param price: Points player has to pay
  4024. * @param points: Points player has
  4025. * @param type: Log type
  4026. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  4027. */
  4028. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4029. {
  4030. int cash;
  4031. nullpo_retr(-1,sd);
  4032. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4033. if( price < 0 || points < 0 )
  4034. {
  4035. ShowError("pc_paycash: Paying negative points (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  4036. return -2;
  4037. }
  4038. if( points > price )
  4039. {
  4040. ShowWarning("pc_paycash: More kafra points provided than needed (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  4041. points = price;
  4042. }
  4043. cash = price-points;
  4044. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4045. {
  4046. 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);
  4047. return -1;
  4048. }
  4049. if( cash ){
  4050. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4051. sd->cashPoints -= cash;
  4052. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4053. }
  4054. if( points ){
  4055. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4056. sd->kafraPoints -= points;
  4057. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4058. }
  4059. if( battle_config.cashshop_show_points )
  4060. {
  4061. char output[CHAT_SIZE_MAX];
  4062. 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.
  4063. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4064. }
  4065. return cash+points;
  4066. }
  4067. /**
  4068. * Attempts to give Cash Points to player
  4069. * @param sd: Player
  4070. * @param cash: Cash player gets
  4071. * @param points: Points player has
  4072. * @param type: Log type
  4073. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  4074. */
  4075. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4076. {
  4077. char output[CHAT_SIZE_MAX];
  4078. nullpo_retr(-1,sd);
  4079. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4080. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4081. if( cash > 0 )
  4082. {
  4083. if( cash > MAX_ZENY-sd->cashPoints )
  4084. {
  4085. 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);
  4086. cash = MAX_ZENY-sd->cashPoints;
  4087. }
  4088. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4089. sd->cashPoints += cash;
  4090. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4091. if( battle_config.cashshop_show_points )
  4092. {
  4093. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4094. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4095. }
  4096. return cash;
  4097. }
  4098. else if( cash < 0 )
  4099. {
  4100. ShowError("pc_getcash: Obtaining negative cash points (cash=%d, account_id=%d, char_id=%d).\n", cash, sd->status.account_id, sd->status.char_id);
  4101. return -1;
  4102. }
  4103. if( points > 0 )
  4104. {
  4105. if( points > MAX_ZENY-sd->kafraPoints )
  4106. {
  4107. 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);
  4108. points = MAX_ZENY-sd->kafraPoints;
  4109. }
  4110. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4111. sd->kafraPoints += points;
  4112. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4113. if( battle_config.cashshop_show_points )
  4114. {
  4115. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4116. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4117. }
  4118. return points;
  4119. }
  4120. else if( points < 0 )
  4121. {
  4122. ShowError("pc_getcash: Obtaining negative kafra points (points=%d, account_id=%d, char_id=%d).\n", points, sd->status.account_id, sd->status.char_id);
  4123. return -1;
  4124. }
  4125. return -2; //shouldn't happen but just in case
  4126. }
  4127. /**
  4128. * Searching a specified itemid in inventory and return his stored index
  4129. * @param sd Player
  4130. * @param nameid Find this Item!
  4131. * @return Stored index in inventory, or -1 if not found.
  4132. **/
  4133. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4134. short i;
  4135. nullpo_retr(-1, sd);
  4136. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4137. return ( i < MAX_INVENTORY ) ? i : -1;
  4138. }
  4139. /** Attempt to add a new item to player inventory
  4140. * @param sd
  4141. * @param item_data
  4142. * @param amount
  4143. * @param log_type
  4144. * @return
  4145. * 0 = success
  4146. * 1 = invalid itemid not found or negative amount
  4147. * 2 = overweight
  4148. * 3 = ?
  4149. * 4 = no free place found
  4150. * 5 = max amount reached
  4151. * 6 = ?
  4152. * 7 = stack limitation
  4153. */
  4154. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4155. struct item_data *id;
  4156. int16 i;
  4157. unsigned int w;
  4158. nullpo_retr(1, sd);
  4159. nullpo_retr(1, item);
  4160. if( item->nameid == 0 || amount <= 0 )
  4161. return ADDITEM_INVALID;
  4162. if( amount > MAX_AMOUNT )
  4163. return ADDITEM_OVERAMOUNT;
  4164. id = itemdb_search(item->nameid);
  4165. if( id->stack.inventory && amount > id->stack.amount )
  4166. {// item stack limitation
  4167. return ADDITEM_STACKLIMIT;
  4168. }
  4169. w = id->weight*amount;
  4170. if(sd->weight + w > sd->max_weight)
  4171. return ADDITEM_OVERWEIGHT;
  4172. i = MAX_INVENTORY;
  4173. if (id->flag.guid && !item->unique_id)
  4174. item->unique_id = pc_generate_unique_id(sd);
  4175. // Stackable | Non Rental
  4176. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4177. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4178. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4179. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4180. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4181. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4182. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4183. 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 ) )
  4184. return ADDITEM_OVERAMOUNT;
  4185. sd->inventory.u.items_inventory[i].amount += amount;
  4186. clif_additem(sd,i,amount,0);
  4187. break;
  4188. }
  4189. }
  4190. }
  4191. if (i >= MAX_INVENTORY) {
  4192. i = pc_search_inventory(sd,0);
  4193. if( i < 0 )
  4194. return ADDITEM_OVERITEM;
  4195. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4196. // clear equip and favorite fields first, just in case
  4197. if( item->equip )
  4198. sd->inventory.u.items_inventory[i].equip = 0;
  4199. if( item->favorite )
  4200. sd->inventory.u.items_inventory[i].favorite = 0;
  4201. sd->inventory.u.items_inventory[i].amount = amount;
  4202. sd->inventory_data[i] = id;
  4203. sd->last_addeditem_index = i;
  4204. if (!itemdb_isstackable2(id) || id->flag.guid)
  4205. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4206. clif_additem(sd,i,amount,0);
  4207. }
  4208. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4209. sd->weight += w;
  4210. clif_updatestatus(sd,SP_WEIGHT);
  4211. //Auto-equip
  4212. if(id->flag.autoequip)
  4213. pc_equipitem(sd, i, id->equip);
  4214. /* rental item check */
  4215. if( item->expire_time ) {
  4216. if( time(NULL) > item->expire_time ) {
  4217. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4218. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4219. } else {
  4220. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4221. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4222. pc_inventory_rental_add(sd, seconds);
  4223. }
  4224. }
  4225. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4226. return ADDITEM_SUCCESS;
  4227. }
  4228. /*==========================================
  4229. * Remove an item at index n from inventory by amount.
  4230. * @param sd
  4231. * @param n Item index in inventory
  4232. * @param amount
  4233. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4234. * @param reason Delete reason
  4235. * @param log_type e_log_pick_type
  4236. * @return 1 - invalid itemid or negative amount; 0 - Success
  4237. *------------------------------------------*/
  4238. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4239. {
  4240. nullpo_retr(1, sd);
  4241. 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)
  4242. return 1;
  4243. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4244. sd->inventory.u.items_inventory[n].amount -= amount;
  4245. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4246. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4247. if(sd->inventory.u.items_inventory[n].equip)
  4248. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4249. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4250. sd->inventory_data[n] = NULL;
  4251. }
  4252. if(!(type&1))
  4253. clif_delitem(sd,n,amount,reason);
  4254. if(!(type&2))
  4255. clif_updatestatus(sd,SP_WEIGHT);
  4256. return 0;
  4257. }
  4258. /*==========================================
  4259. * Attempt to drop an item.
  4260. * @param sd
  4261. * @param n Item index in inventory
  4262. * @param amount Amount of item
  4263. * @return False = fail; True = success
  4264. *------------------------------------------*/
  4265. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4266. {
  4267. nullpo_retr(1, sd);
  4268. if(n < 0 || n >= MAX_INVENTORY)
  4269. return false;
  4270. if(amount <= 0)
  4271. return false;
  4272. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4273. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4274. sd->inventory.u.items_inventory[n].amount < amount ||
  4275. sd->state.trading || sd->state.vending ||
  4276. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4277. )
  4278. return false;
  4279. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4280. {
  4281. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4282. return false; //Can't drop items in nodrop mapflag maps.
  4283. }
  4284. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4285. {
  4286. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4287. return false;
  4288. }
  4289. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4290. return false;
  4291. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4292. clif_dropitem(sd, n, amount);
  4293. return true;
  4294. }
  4295. /*==========================================
  4296. * Attempt to pick up an item.
  4297. * @param sd
  4298. * @param fitem Item that will be picked
  4299. * @return False = fail; True = success
  4300. *------------------------------------------*/
  4301. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4302. {
  4303. int flag = 0;
  4304. unsigned int tick = gettick();
  4305. struct party_data *p = NULL;
  4306. nullpo_ret(sd);
  4307. nullpo_ret(fitem);
  4308. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4309. return false; // Distance is too far
  4310. if (sd->sc.cant.pickup)
  4311. return false;
  4312. if (sd->status.party_id)
  4313. p = party_search(sd->status.party_id);
  4314. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4315. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4316. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4317. if (!(p && p->party.item&1 &&
  4318. first_sd && first_sd->status.party_id == sd->status.party_id
  4319. ))
  4320. return false;
  4321. }
  4322. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4323. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4324. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4325. if (!(p && p->party.item&1 &&
  4326. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4327. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4328. ))
  4329. return false;
  4330. }
  4331. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4332. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4333. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4334. if(!(p && p->party.item&1 &&
  4335. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4336. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4337. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4338. ))
  4339. return false;
  4340. }
  4341. }
  4342. }
  4343. }
  4344. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4345. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4346. clif_additem(sd,0,0,flag);
  4347. return true;
  4348. }
  4349. //Display pickup animation.
  4350. pc_stop_attack(sd);
  4351. clif_takeitem(&sd->bl,&fitem->bl);
  4352. if (fitem->mob_id &&
  4353. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4354. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4355. )
  4356. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4357. map_clearflooritem(&fitem->bl);
  4358. return true;
  4359. }
  4360. /*==========================================
  4361. * Check if item is usable.
  4362. * Return:
  4363. * false = no
  4364. * true = yes
  4365. *------------------------------------------*/
  4366. bool pc_isUseitem(struct map_session_data *sd,int n)
  4367. {
  4368. struct item_data *item;
  4369. unsigned short nameid;
  4370. nullpo_ret(sd);
  4371. item = sd->inventory_data[n];
  4372. nameid = sd->inventory.u.items_inventory[n].nameid;
  4373. if( item == NULL )
  4374. return false;
  4375. //Not consumable item
  4376. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4377. return false;
  4378. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4379. return true;
  4380. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4381. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4382. return false;
  4383. //Prevent mass item usage. [Skotlex]
  4384. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4385. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4386. )
  4387. return false;
  4388. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4389. clif_msg(sd,ITEM_NOUSE_SITTING);
  4390. return false; // You cannot use this item while sitting.
  4391. }
  4392. if (sd->state.storage_flag && item->type != IT_CASH) {
  4393. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4394. return false; // You cannot use this item while storage is open.
  4395. }
  4396. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4397. return false;
  4398. switch( nameid ) {
  4399. case ITEMID_WING_OF_FLY:
  4400. case ITEMID_GIANT_FLY_WING:
  4401. case ITEMID_N_FLY_WING:
  4402. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4403. clif_skill_teleportmessage(sd,0);
  4404. return false;
  4405. }
  4406. if (nameid == ITEMID_GIANT_FLY_WING) {
  4407. struct party_data *pd = party_search(sd->status.party_id);
  4408. if (pd) {
  4409. int i;
  4410. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4411. if (i == MAX_PARTY) { // User is not party leader
  4412. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4413. break;
  4414. }
  4415. 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));
  4416. if (i == MAX_PARTY) { // No party members found on same map
  4417. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4418. break;
  4419. }
  4420. } else {
  4421. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4422. break;
  4423. }
  4424. }
  4425. // Fall through
  4426. case ITEMID_WING_OF_BUTTERFLY:
  4427. case ITEMID_N_BUTTERFLY_WING:
  4428. case ITEMID_DUN_TELE_SCROLL1:
  4429. case ITEMID_DUN_TELE_SCROLL2:
  4430. case ITEMID_DUN_TELE_SCROLL3:
  4431. case ITEMID_WOB_RUNE:
  4432. case ITEMID_WOB_SCHWALTZ:
  4433. case ITEMID_WOB_RACHEL:
  4434. case ITEMID_WOB_LOCAL:
  4435. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4436. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4437. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4438. return false;
  4439. }
  4440. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4441. return false;
  4442. break;
  4443. case ITEMID_MERCENARY_RED_POTION:
  4444. case ITEMID_MERCENARY_BLUE_POTION:
  4445. case ITEMID_M_CENTER_POTION:
  4446. case ITEMID_M_AWAKENING_POTION:
  4447. case ITEMID_M_BERSERK_POTION:
  4448. if( sd->md == NULL || sd->md->db == NULL )
  4449. return false;
  4450. if( sd->md->sc.data[SC_BERSERK] )
  4451. return false;
  4452. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4453. return false;
  4454. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4455. return false;
  4456. break;
  4457. case ITEMID_NEURALIZER:
  4458. if( !mapdata->flag[MF_RESET] )
  4459. return false;
  4460. break;
  4461. }
  4462. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4463. return false; // Mercenary Scrolls
  4464. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4465. if( pc_is90overweight(sd) ) {
  4466. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4467. return false;
  4468. }
  4469. if( !pc_inventoryblank(sd) ) {
  4470. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4471. return false;
  4472. }
  4473. }
  4474. //Gender check
  4475. if(item->sex != 2 && sd->status.sex != item->sex)
  4476. return false;
  4477. //Required level check
  4478. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4479. return false;
  4480. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4481. return false;
  4482. //Not equipable by class. [Skotlex]
  4483. if (!(
  4484. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4485. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4486. ))
  4487. return false;
  4488. if (sd->sc.count && (
  4489. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4490. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4491. sd->sc.data[SC_TRICKDEAD] ||
  4492. sd->sc.data[SC_HIDING] ||
  4493. sd->sc.data[SC__SHADOWFORM] ||
  4494. sd->sc.data[SC__INVISIBILITY] ||
  4495. sd->sc.data[SC__MANHOLE] ||
  4496. sd->sc.data[SC_DEEPSLEEP] ||
  4497. sd->sc.data[SC_CRYSTALIZE] ||
  4498. sd->sc.data[SC_KAGEHUMI] ||
  4499. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4500. sd->sc.data[SC_KINGS_GRACE] ||
  4501. sd->sc.data[SC_SUHIDE]))
  4502. return false;
  4503. if (!pc_isItemClass(sd,item))
  4504. return false;
  4505. //Dead Branch items
  4506. if( item->flag.dead_branch )
  4507. log_branch(sd);
  4508. return true;
  4509. }
  4510. /*==========================================
  4511. * Last checks to use an item.
  4512. * Return:
  4513. * 0 = fail
  4514. * 1 = success
  4515. *------------------------------------------*/
  4516. int pc_useitem(struct map_session_data *sd,int n)
  4517. {
  4518. unsigned int tick = gettick();
  4519. int amount;
  4520. unsigned short nameid;
  4521. struct script_code *script;
  4522. struct item item;
  4523. struct item_data *id;
  4524. nullpo_ret(sd);
  4525. if (sd->npc_id) {
  4526. if (sd->progressbar.npc_id) {
  4527. clif_progressbar_abort(sd);
  4528. return 0; // First item use attempt cancels the progress bar
  4529. }
  4530. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4531. #ifdef RENEWAL
  4532. clif_msg( sd, WORK_IN_PROGRESS );
  4533. #endif
  4534. return 0;
  4535. }
  4536. }
  4537. item = sd->inventory.u.items_inventory[n];
  4538. id = sd->inventory_data[n];
  4539. if (item.nameid == 0 || item.amount <= 0)
  4540. return 0;
  4541. if( !pc_isUseitem(sd,n) )
  4542. return 0;
  4543. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4544. nameid = id->nameid;
  4545. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4546. return 0;
  4547. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4548. if( id->flag.delay_consume ) {
  4549. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4550. return 0;
  4551. else if( pc_issit(sd) )
  4552. return 0;
  4553. }
  4554. //Since most delay-consume items involve using a "skill-type" target cursor,
  4555. //perform a skill-use check before going through. [Skotlex]
  4556. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4557. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4558. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4559. return 0;
  4560. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4561. return 0;
  4562. /* on restricted maps the item is consumed but the effect is not used */
  4563. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4564. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4565. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4566. clif_useitemack(sd,n,item.amount-1,true);
  4567. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4568. }
  4569. return 0;/* regardless, effect is not run */
  4570. }
  4571. sd->itemid = item.nameid;
  4572. sd->itemindex = n;
  4573. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4574. sd->catch_target_class = PET_CATCH_FAIL;
  4575. amount = item.amount;
  4576. script = id->script;
  4577. //Check if the item is to be consumed immediately [Skotlex]
  4578. if (id->flag.delay_consume)
  4579. clif_useitemack(sd, n, amount, true);
  4580. else
  4581. {
  4582. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4583. {
  4584. clif_useitemack(sd, n, amount - 1, true);
  4585. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4586. }
  4587. else
  4588. clif_useitemack(sd, n, 0, false);
  4589. }
  4590. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4591. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4592. //Update item use time.
  4593. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4594. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4595. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4596. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4597. potion_flag = 0;
  4598. return 1;
  4599. }
  4600. /**
  4601. * Add item on cart for given index.
  4602. * @param sd
  4603. * @param item
  4604. * @param amount
  4605. * @param log_type
  4606. * @return 0 = success; 1 = fail; 2 = no slot
  4607. */
  4608. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4609. {
  4610. struct item_data *data;
  4611. int i,w;
  4612. nullpo_retr(1, sd);
  4613. nullpo_retr(1, item);
  4614. if(item->nameid == 0 || amount <= 0)
  4615. return 1;
  4616. data = itemdb_search(item->nameid);
  4617. if( data->stack.cart && amount > data->stack.amount )
  4618. {// item stack limitation
  4619. return 1;
  4620. }
  4621. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4622. { // Check item trade restrictions [Skotlex]
  4623. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4624. return 1;
  4625. }
  4626. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4627. return 1;
  4628. i = MAX_CART;
  4629. if( itemdb_isstackable2(data) && !item->expire_time )
  4630. {
  4631. for (i = 0; i < MAX_CART; i++) {
  4632. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4633. && sd->cart.u.items_cart[i].bound == item->bound
  4634. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4635. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4636. )
  4637. break;
  4638. }
  4639. }
  4640. if( i < MAX_CART )
  4641. {// item already in cart, stack it
  4642. 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 ) )
  4643. return 2; // no slot
  4644. sd->cart.u.items_cart[i].amount += amount;
  4645. clif_cart_additem(sd,i,amount,0);
  4646. }
  4647. else
  4648. {// item not stackable or not present, add it
  4649. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4650. if( i == MAX_CART )
  4651. return 2; // no slot
  4652. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4653. sd->cart.u.items_cart[i].id = 0;
  4654. sd->cart.u.items_cart[i].amount = amount;
  4655. sd->cart_num++;
  4656. clif_cart_additem(sd,i,amount,0);
  4657. }
  4658. sd->cart.u.items_cart[i].favorite = 0; // clear
  4659. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4660. sd->cart_weight += w;
  4661. clif_updatestatus(sd,SP_CARTINFO);
  4662. return 0;
  4663. }
  4664. /*==========================================
  4665. * Delete item on cart for given index.
  4666. *------------------------------------------*/
  4667. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4668. {
  4669. nullpo_retv(sd);
  4670. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4671. sd->cart.u.items_cart[n].amount < amount)
  4672. return;
  4673. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4674. sd->cart.u.items_cart[n].amount -= amount;
  4675. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4676. if(sd->cart.u.items_cart[n].amount <= 0) {
  4677. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4678. sd->cart_num--;
  4679. }
  4680. if(!type) {
  4681. clif_cart_delitem(sd,n,amount);
  4682. clif_updatestatus(sd,SP_CARTINFO);
  4683. }
  4684. }
  4685. /*==========================================
  4686. * Transfer item from inventory to cart.
  4687. *------------------------------------------*/
  4688. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4689. {
  4690. struct item *item_data;
  4691. char flag;
  4692. nullpo_retv(sd);
  4693. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4694. return;
  4695. item_data = &sd->inventory.u.items_inventory[idx];
  4696. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4697. return;
  4698. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4699. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4700. else {
  4701. clif_dropitem(sd,idx,0);
  4702. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4703. }
  4704. }
  4705. /*==========================================
  4706. * Get number of item in cart.
  4707. * Return:
  4708. -1 = itemid not found or no amount found
  4709. x = remaining itemid on cart after get
  4710. *------------------------------------------*/
  4711. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4712. {
  4713. struct item* item_data;
  4714. nullpo_retr(-1, sd);
  4715. item_data = &sd->cart.u.items_cart[idx];
  4716. if( item_data->nameid == 0 || item_data->amount == 0 )
  4717. return -1;
  4718. return item_data->amount - amount;
  4719. }
  4720. /*==========================================
  4721. * Retrieve an item at index idx from cart.
  4722. *------------------------------------------*/
  4723. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4724. {
  4725. struct item *item_data;
  4726. unsigned char flag = 0;
  4727. nullpo_retv(sd);
  4728. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4729. return;
  4730. item_data=&sd->cart.u.items_cart[idx];
  4731. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4732. return;
  4733. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4734. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4735. else {
  4736. clif_dropitem(sd,idx,0);
  4737. clif_additem(sd,0,0,flag);
  4738. }
  4739. }
  4740. /*==========================================
  4741. * Bound Item Check
  4742. * Type:
  4743. * 1 Account Bound
  4744. * 2 Guild Bound
  4745. * 3 Party Bound
  4746. * 4 Character Bound
  4747. *------------------------------------------*/
  4748. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4749. {
  4750. int i = 0, j = 0;
  4751. for(i = 0; i < MAX_INVENTORY; i++) {
  4752. 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) {
  4753. idxlist[j] = i;
  4754. j++;
  4755. }
  4756. }
  4757. return j;
  4758. }
  4759. /*==========================================
  4760. * Display item stolen msg to player sd
  4761. *------------------------------------------*/
  4762. int pc_show_steal(struct block_list *bl,va_list ap)
  4763. {
  4764. struct map_session_data *sd;
  4765. int itemid;
  4766. struct item_data *item=NULL;
  4767. char output[100];
  4768. sd=va_arg(ap,struct map_session_data *);
  4769. itemid=va_arg(ap,int);
  4770. if((item=itemdb_exists(itemid))==NULL)
  4771. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4772. else
  4773. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4774. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4775. return 0;
  4776. }
  4777. /*==========================================
  4778. * Steal an item from bl (mob).
  4779. * Return:
  4780. * 0 = fail
  4781. * 1 = succes
  4782. *------------------------------------------*/
  4783. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4784. {
  4785. int i,itemid;
  4786. double rate;
  4787. unsigned char flag = 0;
  4788. struct status_data *sd_status, *md_status;
  4789. struct mob_data *md;
  4790. struct item tmp_item;
  4791. if(!sd || !bl || bl->type!=BL_MOB)
  4792. return 0;
  4793. md = (TBL_MOB *)bl;
  4794. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4795. return 0;
  4796. sd_status= status_get_status_data(&sd->bl);
  4797. md_status= status_get_status_data(bl);
  4798. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4799. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4800. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4801. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4802. ) { //Can't steal from
  4803. md->state.steal_flag = UCHAR_MAX;
  4804. return 0;
  4805. }
  4806. // base skill success chance (percentual)
  4807. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4808. rate += sd->bonus.add_steal_rate;
  4809. if( rate < 1 )
  4810. return 0;
  4811. // Try dropping one item, in the order from first to last possible slot.
  4812. // Droprate is affected by the skill success rate.
  4813. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4814. 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 * rate/100. )
  4815. break;
  4816. if( i == MAX_STEAL_DROP )
  4817. return 0;
  4818. itemid = md->db->dropitem[i].nameid;
  4819. memset(&tmp_item,0,sizeof(tmp_item));
  4820. tmp_item.nameid = itemid;
  4821. tmp_item.amount = 1;
  4822. tmp_item.identify = itemdb_isidentified(itemid);
  4823. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4824. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4825. //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?
  4826. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4827. if(flag) { //Failed to steal due to overweight
  4828. clif_additem(sd,0,0,flag);
  4829. return 0;
  4830. }
  4831. if(battle_config.show_steal_in_same_party)
  4832. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4833. //Logs items, Stolen from mobs [Lupus]
  4834. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4835. //A Rare Steal Global Announce by Lupus
  4836. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4837. struct item_data *i_data;
  4838. char message[128];
  4839. i_data = itemdb_search(itemid);
  4840. 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);
  4841. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4842. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4843. }
  4844. return 1;
  4845. }
  4846. /*==========================================
  4847. * Stole zeny from bl (mob)
  4848. * return
  4849. * 0 = fail
  4850. * 1 = success
  4851. *------------------------------------------*/
  4852. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4853. {
  4854. int rate, target_lv;
  4855. struct mob_data *md;
  4856. if(!sd || !target || target->type != BL_MOB)
  4857. return 0;
  4858. md = (TBL_MOB*)target;
  4859. target_lv = status_get_lv(target);
  4860. 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)
  4861. return 0;
  4862. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4863. if(rnd()%1000 < rate)
  4864. {
  4865. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4866. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4867. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4868. md->state.steal_coin_flag = 1;
  4869. return 1;
  4870. }
  4871. return 0;
  4872. }
  4873. /*==========================================
  4874. * Set's a player position.
  4875. * @param sd
  4876. * @param mapindex
  4877. * @param x
  4878. * @param y
  4879. * @param clrtype
  4880. * @return SETPOS_OK Success
  4881. * SETPOS_MAPINDEX Invalid map index
  4882. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4883. * SETPOS_AUTOTRADE Player is in autotrade state
  4884. *------------------------------------------*/
  4885. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4886. {
  4887. nullpo_retr(SETPOS_OK,sd);
  4888. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4889. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4890. return SETPOS_MAPINDEX;
  4891. }
  4892. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4893. return SETPOS_AUTOTRADE;
  4894. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4895. pc_setstand(sd, true);
  4896. pc_setrestartvalue(sd,1);
  4897. }
  4898. int16 m = map_mapindex2mapid(mapindex);
  4899. struct map_data *mapdata = map_getmapdata(m);
  4900. sd->state.changemap = (sd->mapindex != mapindex);
  4901. sd->state.warping = 1;
  4902. sd->state.workinprogress = WIP_DISABLE_NONE;
  4903. if( sd->state.changemap ) { // Misc map-changing settings
  4904. int i;
  4905. if(map_getmapdata(sd->bl.m)->instance_id && !mapdata->instance_id) {
  4906. bool instance_found = false;
  4907. struct party_data *p = NULL;
  4908. struct guild *g = NULL;
  4909. if (sd->instance_id) {
  4910. instance_delusers(sd->instance_id);
  4911. instance_found = true;
  4912. }
  4913. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4914. instance_delusers(p->instance_id);
  4915. instance_found = true;
  4916. }
  4917. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4918. instance_delusers(g->instance_id);
  4919. }
  4920. sd->state.pmap = sd->bl.m;
  4921. if (sd->sc.count) { // Cancel some map related stuff.
  4922. if (sd->sc.data[SC_JAILED])
  4923. return SETPOS_MAPINDEX; //You may not get out!
  4924. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4925. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4926. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4927. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4928. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4929. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4930. if (sd->sc.data[SC_KNOWLEDGE]) {
  4931. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4932. if (sce->timer != INVALID_TIMER)
  4933. delete_timer(sce->timer, status_change_timer);
  4934. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4935. }
  4936. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4937. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4938. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4939. }
  4940. for( i = 0; i < EQI_MAX; i++ ) {
  4941. if( sd->equip_index[i] >= 0 )
  4942. if( pc_isequip(sd,sd->equip_index[i]) )
  4943. pc_unequipitem(sd,sd->equip_index[i],2);
  4944. }
  4945. if (battle_config.clear_unit_onwarp&BL_PC)
  4946. skill_clear_unitgroup(&sd->bl);
  4947. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4948. guild_send_dot_remove(sd);
  4949. bg_send_dot_remove(sd);
  4950. if (sd->regen.state.gc)
  4951. sd->regen.state.gc = 0;
  4952. // make sure vending is allowed here
  4953. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  4954. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4955. vending_closevending(sd);
  4956. }
  4957. channel_pcquit(sd,4); //quit map chan
  4958. }
  4959. if( m < 0 )
  4960. {
  4961. uint32 ip;
  4962. uint16 port;
  4963. struct script_state *st;
  4964. //if can't find any map-servers, just abort setting position.
  4965. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4966. return SETPOS_NO_MAPSERVER;
  4967. if (sd->npc_id){
  4968. npc_event_dequeue(sd,false);
  4969. st = sd->st;
  4970. }else{
  4971. st = nullptr;
  4972. }
  4973. npc_script_event(sd, NPCE_LOGOUT);
  4974. //remove from map, THEN change x/y coordinates
  4975. unit_remove_map_pc(sd,clrtype);
  4976. sd->mapindex = mapindex;
  4977. sd->bl.x=x;
  4978. sd->bl.y=y;
  4979. pc_clean_skilltree(sd);
  4980. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4981. chrif_changemapserver(sd, ip, (short)port);
  4982. //Free session data from this map server [Kevin]
  4983. unit_free_pc(sd);
  4984. if( st ){
  4985. // Has to be done here, because otherwise unit_free_pc will free the stack already
  4986. st->state = END;
  4987. }
  4988. return SETPOS_OK;
  4989. }
  4990. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  4991. {
  4992. 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);
  4993. x = y = 0; // make it random
  4994. }
  4995. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4996. int c=0;
  4997. do {
  4998. x = rnd()%(mapdata->xs-2)+1;
  4999. y = rnd()%(mapdata->ys-2)+1;
  5000. c++;
  5001. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5002. 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));
  5003. return SETPOS_OK; //preventing warp
  5004. //break; //allow warp anyway
  5005. }
  5006. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5007. }
  5008. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5009. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5010. vending_closevending(sd);
  5011. }
  5012. if(sd->bl.prev != NULL){
  5013. unit_remove_map_pc(sd,clrtype);
  5014. clif_changemap(sd,m,x,y); // [MouseJstr]
  5015. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5016. sd->state.rewarp = 1;
  5017. sd->mapindex = mapindex;
  5018. sd->bl.m = m;
  5019. sd->bl.x = sd->ud.to_x = x;
  5020. sd->bl.y = sd->ud.to_y = y;
  5021. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5022. { // Increased guild castle regen [Valaris]
  5023. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5024. if(gc && gc->guild_id == sd->status.guild_id)
  5025. sd->regen.state.gc = 1;
  5026. }
  5027. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5028. {
  5029. sd->pd->bl.m = m;
  5030. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5031. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5032. sd->pd->ud.dir = sd->ud.dir;
  5033. }
  5034. if( hom_is_active(sd->hd) )
  5035. {
  5036. sd->hd->bl.m = m;
  5037. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5038. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5039. sd->hd->ud.dir = sd->ud.dir;
  5040. }
  5041. if( sd->md )
  5042. {
  5043. sd->md->bl.m = m;
  5044. sd->md->bl.x = sd->md->ud.to_x = x;
  5045. sd->md->bl.y = sd->md->ud.to_y = y;
  5046. sd->md->ud.dir = sd->ud.dir;
  5047. }
  5048. if( sd->ed ) {
  5049. sd->ed->bl.m = m;
  5050. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5051. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5052. sd->ed->ud.dir = sd->ud.dir;
  5053. }
  5054. pc_cell_basilica(sd);
  5055. //check if we gonna be rewarped [lighta]
  5056. if(npc_check_areanpc(1,m,x,y,0)){
  5057. sd->count_rewarp++;
  5058. }
  5059. else
  5060. sd->count_rewarp = 0;
  5061. return SETPOS_OK;
  5062. }
  5063. /*==========================================
  5064. * Warp player sd to random location on current map.
  5065. * May fail if no walkable cell found (1000 attempts).
  5066. * Return:
  5067. * 0 = Success
  5068. * 1,2,3 = Fail
  5069. *------------------------------------------*/
  5070. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5071. {
  5072. int x,y,i=0;
  5073. nullpo_ret(sd);
  5074. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5075. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5076. return 3;
  5077. do {
  5078. x = rnd()%(mapdata->xs-2)+1;
  5079. y = rnd()%(mapdata->ys-2)+1;
  5080. } 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);
  5081. if (i < 1000)
  5082. return pc_setpos(sd,mapdata->index,x,y,type);
  5083. return 3;
  5084. }
  5085. /*==========================================
  5086. * Records a memo point at sd's current position
  5087. * pos - entry to replace, (-1: shift oldest entry out)
  5088. *------------------------------------------*/
  5089. bool pc_memo(struct map_session_data* sd, int pos)
  5090. {
  5091. int skill;
  5092. nullpo_ret(sd);
  5093. // check mapflags
  5094. 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) ) {
  5095. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5096. return false;
  5097. }
  5098. // check inputs
  5099. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5100. return false; // invalid input
  5101. // check required skill level
  5102. skill = pc_checkskill(sd, AL_WARP);
  5103. if( skill < 1 ) {
  5104. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5105. return false;
  5106. }
  5107. if( skill < 2 || skill - 2 < pos ) {
  5108. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5109. return false;
  5110. }
  5111. if( pos == -1 )
  5112. {
  5113. uint8 i;
  5114. // prevent memo-ing the same map multiple times
  5115. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5116. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5117. pos = 0;
  5118. }
  5119. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5120. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5121. return false;
  5122. }
  5123. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5124. sd->status.memo_point[pos].x = sd->bl.x;
  5125. sd->status.memo_point[pos].y = sd->bl.y;
  5126. clif_skill_memomessage(sd, 0);
  5127. return true;
  5128. }
  5129. //
  5130. // Skills
  5131. //
  5132. /**
  5133. * Get the skill current cooldown for player.
  5134. * (get the db base cooldown for skill + player specific cooldown)
  5135. * @param sd : player pointer
  5136. * @param id : skill id
  5137. * @param lv : skill lv
  5138. * @return player skill cooldown
  5139. */
  5140. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5141. uint8 i;
  5142. uint16 idx = skill_get_index(skill_id);
  5143. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5144. if (!idx) return 0;
  5145. if (skill_db[idx]->cooldown[skill_lv - 1])
  5146. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5147. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5148. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5149. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5150. if (i < cooldownlen) {
  5151. cooldown += sd->skillcooldown[i].val;
  5152. cooldown = max(0,cooldown);
  5153. }
  5154. return cooldown;
  5155. }
  5156. /*==========================================
  5157. * Return player sd skill_lv learned for given skill
  5158. *------------------------------------------*/
  5159. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5160. {
  5161. uint16 idx = 0;
  5162. if (sd == NULL)
  5163. return 0;
  5164. if ((idx = skill_get_index(skill_id)) == 0) {
  5165. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5166. return 0;
  5167. }
  5168. if (SKILL_CHK_GUILD(skill_id) ) {
  5169. struct guild *g;
  5170. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5171. return guild_checkskill(g,skill_id);
  5172. return 0;
  5173. }
  5174. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5175. }
  5176. /**
  5177. * Check if we still have the correct weapon to continue the skill (actually status)
  5178. * If not ending it
  5179. * @param sd
  5180. * @return 0:error, 1:check done
  5181. */
  5182. static void pc_checkallowskill(struct map_session_data *sd)
  5183. {
  5184. const enum sc_type scw_list[] = {
  5185. SC_TWOHANDQUICKEN,
  5186. SC_ONEHAND,
  5187. SC_AURABLADE,
  5188. SC_PARRYING,
  5189. SC_SPEARQUICKEN,
  5190. SC_ADRENALINE,
  5191. SC_ADRENALINE2,
  5192. SC_DANCING,
  5193. SC_GATLINGFEVER,
  5194. };
  5195. uint8 i;
  5196. nullpo_retv(sd);
  5197. if(!sd->sc.count)
  5198. return;
  5199. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5200. { // Skills requiring specific weapon types
  5201. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5202. continue;
  5203. if(sd->sc.data[scw_list[i]] &&
  5204. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5205. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5206. }
  5207. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5208. // Spurt requires bare hands (feet, in fact xD)
  5209. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5210. if(sd->status.shield <= 0) { // Skills requiring a shield
  5211. const enum sc_type scs_list[] = {
  5212. SC_AUTOGUARD,
  5213. SC_DEFENDER,
  5214. SC_REFLECTSHIELD,
  5215. SC_REFLECTDAMAGE
  5216. };
  5217. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5218. if(sd->sc.data[scs_list[i]])
  5219. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5220. }
  5221. }
  5222. /*==========================================
  5223. * Return equipped index of item on player sd at pos
  5224. * Return
  5225. * -1 : Nothing equipped
  5226. * idx : (this index could be used in inventory to found item_data)
  5227. *------------------------------------------*/
  5228. short pc_checkequip(struct map_session_data *sd,int pos)
  5229. {
  5230. uint8 i;
  5231. nullpo_retr(-1, sd);
  5232. for(i=0;i<EQI_MAX;i++){
  5233. if(pos & equip_bitmask[i])
  5234. return sd->equip_index[i];
  5235. }
  5236. return -1;
  5237. }
  5238. /*==========================================
  5239. * Check if sd has nameid equipped somewhere
  5240. * @sd : the player session
  5241. * @nameid : id of the item to check
  5242. * @min : : see pc.hpp enum equip_index from ? to @max
  5243. * @max : see pc.hpp enum equip_index for @min to ?
  5244. * -return true,false
  5245. *------------------------------------------*/
  5246. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5247. {
  5248. int i;
  5249. for(i = min; i < max; i++) {
  5250. if(equip_bitmask[i]) {
  5251. int idx = sd->equip_index[i];
  5252. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5253. return true;
  5254. }
  5255. }
  5256. return false;
  5257. }
  5258. /*==========================================
  5259. * Convert's from the client's lame Job ID system
  5260. * to the map server's 'makes sense' system. [Skotlex]
  5261. *------------------------------------------*/
  5262. int pc_jobid2mapid(unsigned short b_class)
  5263. {
  5264. switch(b_class)
  5265. {
  5266. //Novice And 1-1 Jobs
  5267. case JOB_NOVICE: return MAPID_NOVICE;
  5268. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5269. case JOB_MAGE: return MAPID_MAGE;
  5270. case JOB_ARCHER: return MAPID_ARCHER;
  5271. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5272. case JOB_MERCHANT: return MAPID_MERCHANT;
  5273. case JOB_THIEF: return MAPID_THIEF;
  5274. case JOB_TAEKWON: return MAPID_TAEKWON;
  5275. case JOB_WEDDING: return MAPID_WEDDING;
  5276. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5277. case JOB_NINJA: return MAPID_NINJA;
  5278. case JOB_XMAS: return MAPID_XMAS;
  5279. case JOB_SUMMER: return MAPID_SUMMER;
  5280. case JOB_HANBOK: return MAPID_HANBOK;
  5281. case JOB_GANGSI: return MAPID_GANGSI;
  5282. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5283. case JOB_SUMMER2: return MAPID_SUMMER2;
  5284. //2-1 Jobs
  5285. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5286. case JOB_KNIGHT: return MAPID_KNIGHT;
  5287. case JOB_WIZARD: return MAPID_WIZARD;
  5288. case JOB_HUNTER: return MAPID_HUNTER;
  5289. case JOB_PRIEST: return MAPID_PRIEST;
  5290. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5291. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5292. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5293. case JOB_KAGEROU:
  5294. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5295. case JOB_REBELLION: return MAPID_REBELLION;
  5296. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5297. //2-2 Jobs
  5298. case JOB_CRUSADER: return MAPID_CRUSADER;
  5299. case JOB_SAGE: return MAPID_SAGE;
  5300. case JOB_BARD:
  5301. case JOB_DANCER: return MAPID_BARDDANCER;
  5302. case JOB_MONK: return MAPID_MONK;
  5303. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5304. case JOB_ROGUE: return MAPID_ROGUE;
  5305. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5306. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5307. //Trans Novice And Trans 1-1 Jobs
  5308. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5309. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5310. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5311. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5312. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5313. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5314. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5315. //Trans 2-1 Jobs
  5316. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5317. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5318. case JOB_SNIPER: return MAPID_SNIPER;
  5319. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5320. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5321. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5322. //Trans 2-2 Jobs
  5323. case JOB_PALADIN: return MAPID_PALADIN;
  5324. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5325. case JOB_CLOWN:
  5326. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5327. case JOB_CHAMPION: return MAPID_CHAMPION;
  5328. case JOB_CREATOR: return MAPID_CREATOR;
  5329. case JOB_STALKER: return MAPID_STALKER;
  5330. //Baby Novice And Baby 1-1 Jobs
  5331. case JOB_BABY: return MAPID_BABY;
  5332. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5333. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5334. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5335. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5336. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5337. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5338. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5339. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5340. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5341. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5342. //Baby 2-1 Jobs
  5343. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5344. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5345. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5346. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5347. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5348. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5349. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5350. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5351. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5352. case JOB_BABY_KAGEROU:
  5353. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5354. //Baby 2-2 Jobs
  5355. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5356. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5357. case JOB_BABY_BARD:
  5358. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5359. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5360. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5361. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5362. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5363. //3-1 Jobs
  5364. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5365. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5366. case JOB_WARLOCK: return MAPID_WARLOCK;
  5367. case JOB_RANGER: return MAPID_RANGER;
  5368. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5369. case JOB_MECHANIC: return MAPID_MECHANIC;
  5370. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5371. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5372. //3-2 Jobs
  5373. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5374. case JOB_SORCERER: return MAPID_SORCERER;
  5375. case JOB_MINSTREL:
  5376. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5377. case JOB_SURA: return MAPID_SURA;
  5378. case JOB_GENETIC: return MAPID_GENETIC;
  5379. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5380. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5381. //Trans 3-1 Jobs
  5382. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5383. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5384. case JOB_RANGER_T: return MAPID_RANGER_T;
  5385. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5386. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5387. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5388. //Trans 3-2 Jobs
  5389. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5390. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5391. case JOB_MINSTREL_T:
  5392. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5393. case JOB_SURA_T: return MAPID_SURA_T;
  5394. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5395. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5396. //Baby 3-1 Jobs
  5397. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5398. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5399. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5400. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5401. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5402. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5403. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5404. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5405. //Baby 3-2 Jobs
  5406. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5407. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5408. case JOB_BABY_MINSTREL:
  5409. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5410. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5411. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5412. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5413. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5414. //Doram Jobs
  5415. case JOB_SUMMONER: return MAPID_SUMMONER;
  5416. default:
  5417. return -1;
  5418. }
  5419. }
  5420. //Reverts the map-style class id to the client-style one.
  5421. int pc_mapid2jobid(unsigned short class_, int sex)
  5422. {
  5423. switch(class_) {
  5424. //Novice And 1-1 Jobs
  5425. case MAPID_NOVICE: return JOB_NOVICE;
  5426. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5427. case MAPID_MAGE: return JOB_MAGE;
  5428. case MAPID_ARCHER: return JOB_ARCHER;
  5429. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5430. case MAPID_MERCHANT: return JOB_MERCHANT;
  5431. case MAPID_THIEF: return JOB_THIEF;
  5432. case MAPID_TAEKWON: return JOB_TAEKWON;
  5433. case MAPID_WEDDING: return JOB_WEDDING;
  5434. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5435. case MAPID_NINJA: return JOB_NINJA;
  5436. case MAPID_XMAS: return JOB_XMAS;
  5437. case MAPID_SUMMER: return JOB_SUMMER;
  5438. case MAPID_HANBOK: return JOB_HANBOK;
  5439. case MAPID_GANGSI: return JOB_GANGSI;
  5440. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5441. case MAPID_SUMMER2: return JOB_SUMMER2;
  5442. //2-1 Jobs
  5443. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5444. case MAPID_KNIGHT: return JOB_KNIGHT;
  5445. case MAPID_WIZARD: return JOB_WIZARD;
  5446. case MAPID_HUNTER: return JOB_HUNTER;
  5447. case MAPID_PRIEST: return JOB_PRIEST;
  5448. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5449. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5450. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5451. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5452. case MAPID_REBELLION: return JOB_REBELLION;
  5453. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5454. //2-2 Jobs
  5455. case MAPID_CRUSADER: return JOB_CRUSADER;
  5456. case MAPID_SAGE: return JOB_SAGE;
  5457. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5458. case MAPID_MONK: return JOB_MONK;
  5459. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5460. case MAPID_ROGUE: return JOB_ROGUE;
  5461. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5462. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5463. //Trans Novice And Trans 2-1 Jobs
  5464. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5465. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5466. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5467. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5468. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5469. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5470. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5471. //Trans 2-1 Jobs
  5472. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5473. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5474. case MAPID_SNIPER: return JOB_SNIPER;
  5475. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5476. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5477. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5478. //Trans 2-2 Jobs
  5479. case MAPID_PALADIN: return JOB_PALADIN;
  5480. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5481. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5482. case MAPID_CHAMPION: return JOB_CHAMPION;
  5483. case MAPID_CREATOR: return JOB_CREATOR;
  5484. case MAPID_STALKER: return JOB_STALKER;
  5485. //Baby Novice And Baby 1-1 Jobs
  5486. case MAPID_BABY: return JOB_BABY;
  5487. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5488. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5489. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5490. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5491. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5492. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5493. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5494. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5495. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5496. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5497. //Baby 2-1 Jobs
  5498. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5499. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5500. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5501. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5502. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5503. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5504. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5505. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5506. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5507. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5508. //Baby 2-2 Jobs
  5509. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5510. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5511. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5512. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5513. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5514. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5515. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5516. //3-1 Jobs
  5517. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5518. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5519. case MAPID_WARLOCK: return JOB_WARLOCK;
  5520. case MAPID_RANGER: return JOB_RANGER;
  5521. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5522. case MAPID_MECHANIC: return JOB_MECHANIC;
  5523. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5524. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5525. //3-2 Jobs
  5526. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5527. case MAPID_SORCERER: return JOB_SORCERER;
  5528. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5529. case MAPID_SURA: return JOB_SURA;
  5530. case MAPID_GENETIC: return JOB_GENETIC;
  5531. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5532. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5533. //Trans 3-1 Jobs
  5534. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5535. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5536. case MAPID_RANGER_T: return JOB_RANGER_T;
  5537. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5538. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5539. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5540. //Trans 3-2 Jobs
  5541. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5542. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5543. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5544. case MAPID_SURA_T: return JOB_SURA_T;
  5545. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5546. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5547. //Baby 3-1 Jobs
  5548. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5549. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5550. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5551. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5552. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5553. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5554. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5555. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5556. //Baby 3-2 Jobs
  5557. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5558. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5559. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5560. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5561. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5562. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5563. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5564. //Doram Jobs
  5565. case MAPID_SUMMONER: return JOB_SUMMONER;
  5566. default:
  5567. return -1;
  5568. }
  5569. }
  5570. /*====================================================
  5571. * This function return the name of the job (by [Yor])
  5572. *----------------------------------------------------*/
  5573. const char* job_name(int class_)
  5574. {
  5575. switch (class_) {
  5576. case JOB_NOVICE:
  5577. case JOB_SWORDMAN:
  5578. case JOB_MAGE:
  5579. case JOB_ARCHER:
  5580. case JOB_ACOLYTE:
  5581. case JOB_MERCHANT:
  5582. case JOB_THIEF:
  5583. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5584. case JOB_KNIGHT:
  5585. case JOB_PRIEST:
  5586. case JOB_WIZARD:
  5587. case JOB_BLACKSMITH:
  5588. case JOB_HUNTER:
  5589. case JOB_ASSASSIN:
  5590. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5591. case JOB_KNIGHT2:
  5592. return msg_txt(NULL,557);
  5593. case JOB_CRUSADER:
  5594. case JOB_MONK:
  5595. case JOB_SAGE:
  5596. case JOB_ROGUE:
  5597. case JOB_ALCHEMIST:
  5598. case JOB_BARD:
  5599. case JOB_DANCER:
  5600. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5601. case JOB_CRUSADER2:
  5602. return msg_txt(NULL,563);
  5603. case JOB_WEDDING:
  5604. case JOB_SUPER_NOVICE:
  5605. case JOB_GUNSLINGER:
  5606. case JOB_NINJA:
  5607. case JOB_XMAS:
  5608. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5609. case JOB_SUMMER:
  5610. case JOB_SUMMER2:
  5611. return msg_txt(NULL,621);
  5612. case JOB_HANBOK:
  5613. return msg_txt(NULL,694);
  5614. case JOB_OKTOBERFEST:
  5615. return msg_txt(NULL,696);
  5616. case JOB_NOVICE_HIGH:
  5617. case JOB_SWORDMAN_HIGH:
  5618. case JOB_MAGE_HIGH:
  5619. case JOB_ARCHER_HIGH:
  5620. case JOB_ACOLYTE_HIGH:
  5621. case JOB_MERCHANT_HIGH:
  5622. case JOB_THIEF_HIGH:
  5623. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5624. case JOB_LORD_KNIGHT:
  5625. case JOB_HIGH_PRIEST:
  5626. case JOB_HIGH_WIZARD:
  5627. case JOB_WHITESMITH:
  5628. case JOB_SNIPER:
  5629. case JOB_ASSASSIN_CROSS:
  5630. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5631. case JOB_LORD_KNIGHT2:
  5632. return msg_txt(NULL,582);
  5633. case JOB_PALADIN:
  5634. case JOB_CHAMPION:
  5635. case JOB_PROFESSOR:
  5636. case JOB_STALKER:
  5637. case JOB_CREATOR:
  5638. case JOB_CLOWN:
  5639. case JOB_GYPSY:
  5640. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5641. case JOB_PALADIN2:
  5642. return msg_txt(NULL,588);
  5643. case JOB_BABY:
  5644. case JOB_BABY_SWORDMAN:
  5645. case JOB_BABY_MAGE:
  5646. case JOB_BABY_ARCHER:
  5647. case JOB_BABY_ACOLYTE:
  5648. case JOB_BABY_MERCHANT:
  5649. case JOB_BABY_THIEF:
  5650. return msg_txt(NULL,595 - JOB_BABY + class_);
  5651. case JOB_BABY_KNIGHT:
  5652. case JOB_BABY_PRIEST:
  5653. case JOB_BABY_WIZARD:
  5654. case JOB_BABY_BLACKSMITH:
  5655. case JOB_BABY_HUNTER:
  5656. case JOB_BABY_ASSASSIN:
  5657. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5658. case JOB_BABY_KNIGHT2:
  5659. return msg_txt(NULL,602);
  5660. case JOB_BABY_CRUSADER:
  5661. case JOB_BABY_MONK:
  5662. case JOB_BABY_SAGE:
  5663. case JOB_BABY_ROGUE:
  5664. case JOB_BABY_ALCHEMIST:
  5665. case JOB_BABY_BARD:
  5666. case JOB_BABY_DANCER:
  5667. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5668. case JOB_BABY_CRUSADER2:
  5669. return msg_txt(NULL,608);
  5670. case JOB_SUPER_BABY:
  5671. return msg_txt(NULL,615);
  5672. case JOB_TAEKWON:
  5673. return msg_txt(NULL,616);
  5674. case JOB_STAR_GLADIATOR:
  5675. case JOB_STAR_GLADIATOR2:
  5676. return msg_txt(NULL,617);
  5677. case JOB_SOUL_LINKER:
  5678. return msg_txt(NULL,618);
  5679. case JOB_GANGSI:
  5680. case JOB_DEATH_KNIGHT:
  5681. case JOB_DARK_COLLECTOR:
  5682. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5683. case JOB_RUNE_KNIGHT:
  5684. case JOB_WARLOCK:
  5685. case JOB_RANGER:
  5686. case JOB_ARCH_BISHOP:
  5687. case JOB_MECHANIC:
  5688. case JOB_GUILLOTINE_CROSS:
  5689. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5690. case JOB_RUNE_KNIGHT_T:
  5691. case JOB_WARLOCK_T:
  5692. case JOB_RANGER_T:
  5693. case JOB_ARCH_BISHOP_T:
  5694. case JOB_MECHANIC_T:
  5695. case JOB_GUILLOTINE_CROSS_T:
  5696. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5697. case JOB_ROYAL_GUARD:
  5698. case JOB_SORCERER:
  5699. case JOB_MINSTREL:
  5700. case JOB_WANDERER:
  5701. case JOB_SURA:
  5702. case JOB_GENETIC:
  5703. case JOB_SHADOW_CHASER:
  5704. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5705. case JOB_ROYAL_GUARD_T:
  5706. case JOB_SORCERER_T:
  5707. case JOB_MINSTREL_T:
  5708. case JOB_WANDERER_T:
  5709. case JOB_SURA_T:
  5710. case JOB_GENETIC_T:
  5711. case JOB_SHADOW_CHASER_T:
  5712. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5713. case JOB_RUNE_KNIGHT2:
  5714. case JOB_RUNE_KNIGHT_T2:
  5715. return msg_txt(NULL,625);
  5716. case JOB_ROYAL_GUARD2:
  5717. case JOB_ROYAL_GUARD_T2:
  5718. return msg_txt(NULL,631);
  5719. case JOB_RANGER2:
  5720. case JOB_RANGER_T2:
  5721. return msg_txt(NULL,627);
  5722. case JOB_MECHANIC2:
  5723. case JOB_MECHANIC_T2:
  5724. return msg_txt(NULL,629);
  5725. case JOB_BABY_RUNE:
  5726. case JOB_BABY_WARLOCK:
  5727. case JOB_BABY_RANGER:
  5728. case JOB_BABY_BISHOP:
  5729. case JOB_BABY_MECHANIC:
  5730. case JOB_BABY_CROSS:
  5731. case JOB_BABY_GUARD:
  5732. case JOB_BABY_SORCERER:
  5733. case JOB_BABY_MINSTREL:
  5734. case JOB_BABY_WANDERER:
  5735. case JOB_BABY_SURA:
  5736. case JOB_BABY_GENETIC:
  5737. case JOB_BABY_CHASER:
  5738. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5739. case JOB_BABY_RUNE2:
  5740. return msg_txt(NULL,638);
  5741. case JOB_BABY_GUARD2:
  5742. return msg_txt(NULL,644);
  5743. case JOB_BABY_RANGER2:
  5744. return msg_txt(NULL,640);
  5745. case JOB_BABY_MECHANIC2:
  5746. return msg_txt(NULL,642);
  5747. case JOB_SUPER_NOVICE_E:
  5748. case JOB_SUPER_BABY_E:
  5749. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5750. case JOB_KAGEROU:
  5751. case JOB_OBORO:
  5752. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5753. case JOB_REBELLION:
  5754. return msg_txt(NULL,695);
  5755. case JOB_SUMMONER:
  5756. return msg_txt(NULL,697);
  5757. case JOB_BABY_SUMMONER:
  5758. return msg_txt(NULL,698);
  5759. case JOB_BABY_NINJA:
  5760. return msg_txt(NULL,699);
  5761. case JOB_BABY_KAGEROU:
  5762. case JOB_BABY_OBORO:
  5763. case JOB_BABY_TAEKWON:
  5764. case JOB_BABY_STAR_GLADIATOR:
  5765. case JOB_BABY_SOUL_LINKER:
  5766. case JOB_BABY_GUNSLINGER:
  5767. case JOB_BABY_REBELLION:
  5768. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5769. case JOB_BABY_STAR_GLADIATOR2:
  5770. return msg_txt(NULL,756);
  5771. case JOB_STAR_EMPEROR:
  5772. case JOB_SOUL_REAPER:
  5773. case JOB_BABY_STAR_EMPEROR:
  5774. case JOB_BABY_SOUL_REAPER:
  5775. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5776. case JOB_STAR_EMPEROR2:
  5777. return msg_txt(NULL,782);
  5778. case JOB_BABY_STAR_EMPEROR2:
  5779. return msg_txt(NULL,784);
  5780. default:
  5781. return msg_txt(NULL,655);
  5782. }
  5783. }
  5784. /*====================================================
  5785. * Timered function to make id follow a target.
  5786. * @id = bl.id (player only atm)
  5787. * target is define in sd->followtarget (bl.id)
  5788. * used by pc_follow
  5789. *----------------------------------------------------*/
  5790. TIMER_FUNC(pc_follow_timer){
  5791. struct map_session_data *sd;
  5792. struct block_list *tbl;
  5793. sd = map_id2sd(id);
  5794. nullpo_ret(sd);
  5795. if (sd->followtimer != tid){
  5796. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5797. sd->followtimer = INVALID_TIMER;
  5798. return 0;
  5799. }
  5800. sd->followtimer = INVALID_TIMER;
  5801. tbl = map_id2bl(sd->followtarget);
  5802. if (tbl == NULL || pc_isdead(sd))
  5803. {
  5804. pc_stop_following(sd);
  5805. return 0;
  5806. }
  5807. // either player or target is currently detached from map blocks (could be teleporting),
  5808. // but still connected to this map, so we'll just increment the timer and check back later
  5809. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5810. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5811. {
  5812. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5813. if (!check_distance_bl(&sd->bl, tbl, 5))
  5814. unit_walktobl(&sd->bl, tbl, 5, 0);
  5815. } else
  5816. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5817. }
  5818. sd->followtimer = add_timer(
  5819. tick + 1000, // increase time a bit to loosen up map's load
  5820. pc_follow_timer, sd->bl.id, 0);
  5821. return 0;
  5822. }
  5823. int pc_stop_following (struct map_session_data *sd)
  5824. {
  5825. nullpo_ret(sd);
  5826. if (sd->followtimer != INVALID_TIMER) {
  5827. delete_timer(sd->followtimer,pc_follow_timer);
  5828. sd->followtimer = INVALID_TIMER;
  5829. }
  5830. sd->followtarget = -1;
  5831. sd->ud.target_to = 0;
  5832. unit_stop_walking(&sd->bl, 1);
  5833. return 0;
  5834. }
  5835. int pc_follow(struct map_session_data *sd,int target_id)
  5836. {
  5837. struct block_list *bl = map_id2bl(target_id);
  5838. if (bl == NULL /*|| bl->type != BL_PC*/)
  5839. return 1;
  5840. if (sd->followtimer != INVALID_TIMER)
  5841. pc_stop_following(sd);
  5842. sd->followtarget = target_id;
  5843. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5844. return 0;
  5845. }
  5846. int pc_checkbaselevelup(struct map_session_data *sd) {
  5847. unsigned int next = pc_nextbaseexp(sd);
  5848. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5849. return 0;
  5850. do {
  5851. sd->status.base_exp -= next;
  5852. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5853. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5854. sd->status.base_exp = next-1;
  5855. next = pc_gets_status_point(sd->status.base_level);
  5856. sd->status.base_level++;
  5857. sd->status.status_point += next;
  5858. if( pc_is_maxbaselv(sd) ){
  5859. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5860. break;
  5861. }
  5862. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5863. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5864. status_calc_pet(sd->pd,SCO_NONE);
  5865. clif_updatestatus(sd,SP_STATUSPOINT);
  5866. clif_updatestatus(sd,SP_BASELEVEL);
  5867. clif_updatestatus(sd,SP_BASEEXP);
  5868. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5869. status_calc_pc(sd,SCO_FORCE);
  5870. status_percent_heal(&sd->bl,100,100);
  5871. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5872. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5873. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5874. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5875. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5876. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5877. if (sd->state.snovice_dead_flag)
  5878. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5879. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5880. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5881. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5882. }
  5883. clif_misceffect(&sd->bl,0);
  5884. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5885. if(sd->status.party_id)
  5886. party_send_levelup(sd);
  5887. pc_baselevelchanged(sd);
  5888. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5889. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5890. return 1;
  5891. }
  5892. void pc_baselevelchanged(struct map_session_data *sd) {
  5893. uint8 i;
  5894. for( i = 0; i < EQI_MAX; i++ ) {
  5895. if( sd->equip_index[i] >= 0 ) {
  5896. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5897. pc_unequipitem(sd, sd->equip_index[i], 3);
  5898. }
  5899. }
  5900. pc_show_questinfo(sd);
  5901. }
  5902. int pc_checkjoblevelup(struct map_session_data *sd)
  5903. {
  5904. unsigned int next = pc_nextjobexp(sd);
  5905. nullpo_ret(sd);
  5906. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5907. return 0;
  5908. do {
  5909. sd->status.job_exp -= next;
  5910. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5911. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5912. sd->status.job_exp = next-1;
  5913. sd->status.job_level ++;
  5914. sd->status.skill_point ++;
  5915. if( pc_is_maxjoblv(sd) ){
  5916. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5917. break;
  5918. }
  5919. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5920. clif_updatestatus(sd,SP_JOBLEVEL);
  5921. clif_updatestatus(sd,SP_JOBEXP);
  5922. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5923. clif_updatestatus(sd,SP_SKILLPOINT);
  5924. status_calc_pc(sd,SCO_FORCE);
  5925. clif_misceffect(&sd->bl,1);
  5926. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5927. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5928. npc_script_event(sd, NPCE_JOBLVUP);
  5929. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5930. return 1;
  5931. }
  5932. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5933. * @param sd Player
  5934. * @param base_exp Base EXP before peronal bonuses
  5935. * @param job_exp Job EXP before peronal bonuses
  5936. * @param src Block list that affecting the exp calculation
  5937. */
  5938. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5939. {
  5940. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5941. if (src) {
  5942. struct status_data *status = status_get_status_data(src);
  5943. if( sd->expaddrace[status->race] )
  5944. bonus += sd->expaddrace[status->race];
  5945. if( sd->expaddrace[RC_ALL] )
  5946. bonus += sd->expaddrace[RC_ALL];
  5947. if( sd->expaddclass[status->class_] )
  5948. bonus += sd->expaddclass[status->class_];
  5949. if( sd->expaddclass[CLASS_ALL] )
  5950. bonus += sd->expaddclass[CLASS_ALL];
  5951. if (battle_config.pk_mode &&
  5952. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5953. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5954. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5955. vip_bonus_base = battle_config.vip_base_exp_increase;
  5956. vip_bonus_job = battle_config.vip_job_exp_increase;
  5957. }
  5958. }
  5959. // Give EXPBOOST for quests even if src is NULL.
  5960. if (sd->sc.data[SC_EXPBOOST]) {
  5961. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5962. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5963. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5964. }
  5965. if (*base_exp) {
  5966. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5967. *base_exp = cap_value(exp, 1, UINT_MAX);
  5968. }
  5969. // Give JEXPBOOST for quests even if src is NULL.
  5970. if (sd->sc.data[SC_JEXPBOOST])
  5971. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5972. if (*job_exp) {
  5973. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5974. *job_exp = cap_value(exp, 1, UINT_MAX);
  5975. }
  5976. return;
  5977. }
  5978. /**
  5979. * Show EXP gained by player in percentage by @showexp
  5980. * @param sd Player
  5981. * @param base_exp Base EXP gained/loss
  5982. * @param next_base_exp Base EXP needed for next base level
  5983. * @param job_exp Job EXP gained/loss
  5984. * @param next_job_exp Job EXP needed for next job level
  5985. * @param lost True:EXP penalty, lose EXP
  5986. **/
  5987. 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) {
  5988. char output[CHAT_SIZE_MAX];
  5989. nullpo_retv(sd);
  5990. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5991. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5992. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5993. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5994. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5995. }
  5996. /**
  5997. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5998. * @param sd Player
  5999. * @param src EXP source
  6000. * @param base_exp Base EXP gained
  6001. * @param base_exp Job EXP gained
  6002. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6003. * @return
  6004. **/
  6005. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6006. {
  6007. unsigned int nextb = 0, nextj = 0;
  6008. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6009. nullpo_retv(sd);
  6010. if(sd->bl.prev == NULL || pc_isdead(sd))
  6011. return;
  6012. if (!(exp_flag&2)) {
  6013. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6014. return; // no exp on pvp maps
  6015. if (sd->status.guild_id>0)
  6016. base_exp -= guild_payexp(sd,base_exp);
  6017. }
  6018. flag = ((base_exp) ? 1 : 0) |
  6019. ((job_exp) ? 2 : 0) |
  6020. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6021. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6022. if (!(exp_flag&2))
  6023. pc_calcexp(sd, &base_exp, &job_exp, src);
  6024. nextb = pc_nextbaseexp(sd);
  6025. nextj = pc_nextjobexp(sd);
  6026. if (flag&4){
  6027. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6028. base_exp = 0;
  6029. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6030. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6031. }
  6032. if (flag&8){
  6033. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6034. job_exp = 0;
  6035. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6036. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6037. }
  6038. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6039. //Note that this value should never be greater than the original
  6040. //therefore no overflow checks are needed. [Skotlex]
  6041. if (nextb > 0) {
  6042. float nextbp = (float) base_exp / (float) nextb;
  6043. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6044. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6045. }
  6046. if (nextj > 0) {
  6047. float nextjp = (float) job_exp / (float) nextj;
  6048. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6049. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6050. }
  6051. }
  6052. // Give EXP for Base Level
  6053. if (base_exp) {
  6054. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6055. sd->status.base_exp = UINT32_MAX;
  6056. else
  6057. sd->status.base_exp += base_exp;
  6058. if (!pc_checkbaselevelup(sd))
  6059. clif_updatestatus(sd,SP_BASEEXP);
  6060. }
  6061. // Give EXP for Job Level
  6062. if (job_exp) {
  6063. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6064. sd->status.job_exp = UINT32_MAX;
  6065. else
  6066. sd->status.job_exp += job_exp;
  6067. if (!pc_checkjoblevelup(sd))
  6068. clif_updatestatus(sd,SP_JOBEXP);
  6069. }
  6070. if (flag&1)
  6071. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6072. if (flag&2)
  6073. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6074. if (sd->state.showexp && (base_exp || job_exp))
  6075. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6076. }
  6077. /**
  6078. * Lost Base/Job EXP from a player
  6079. * @param sd Player
  6080. * @param base_exp Base EXP lost
  6081. * @param job_exp Job EXP lost
  6082. **/
  6083. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6084. nullpo_retv(sd);
  6085. if (base_exp) {
  6086. base_exp = u32min(sd->status.base_exp, base_exp);
  6087. sd->status.base_exp -= base_exp;
  6088. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6089. clif_updatestatus(sd, SP_BASEEXP);
  6090. }
  6091. if (job_exp) {
  6092. job_exp = u32min(sd->status.job_exp, job_exp);
  6093. sd->status.job_exp -= job_exp;
  6094. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6095. clif_updatestatus(sd, SP_JOBEXP);
  6096. }
  6097. if (sd->state.showexp && (base_exp || job_exp))
  6098. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6099. }
  6100. /**
  6101. * Returns max base level for this character's class.
  6102. * @param class_: Player's class
  6103. * @return Max Base Level
  6104. */
  6105. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6106. return job_info[pc_class2idx(class_)].max_level[0];
  6107. }
  6108. /**
  6109. * Returns max base level for this character.
  6110. * @param sd Player
  6111. * @return Max Base Level
  6112. **/
  6113. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6114. return pc_class_maxbaselv(sd->status.class_);
  6115. }
  6116. /**
  6117. * Returns max job level for this character's class.
  6118. * @param class_: Player's class
  6119. * @return Max Job Level
  6120. */
  6121. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6122. return job_info[pc_class2idx(class_)].max_level[1];
  6123. }
  6124. /**
  6125. * Returns max job level for this character.
  6126. * @param sd Player
  6127. * @return Max Job Level
  6128. **/
  6129. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6130. return pc_class_maxjoblv(sd->status.class_);
  6131. }
  6132. /**
  6133. * Check if player is reached max base level
  6134. * @param sd
  6135. * @return True if reached max level
  6136. **/
  6137. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6138. nullpo_retr(false, sd);
  6139. return (sd->status.base_level >= pc_maxbaselv(sd));
  6140. }
  6141. /**
  6142. * Check if player is reached max base level
  6143. * @param sd
  6144. * @return True if reached max level
  6145. **/
  6146. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6147. nullpo_retr(false, sd);
  6148. return (sd->status.job_level >= pc_maxjoblv(sd));
  6149. }
  6150. /**
  6151. * Base exp needed for player to level up.
  6152. * @param sd
  6153. * @return Base EXP needed for next base level
  6154. **/
  6155. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6156. nullpo_ret(sd);
  6157. if (sd->status.base_level == 0) // Is this something that possible?
  6158. return 0;
  6159. if (pc_is_maxbaselv(sd))
  6160. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6161. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6162. }
  6163. /**
  6164. * Job exp needed for player to level up.
  6165. * @param sd
  6166. * @return Job EXP needed for next job level
  6167. **/
  6168. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6169. nullpo_ret(sd);
  6170. if (sd->status.job_level == 0) // Is this something that possible?
  6171. return 0;
  6172. if (pc_is_maxjoblv(sd))
  6173. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6174. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6175. }
  6176. /// Returns the value of the specified stat.
  6177. static int pc_getstat(struct map_session_data* sd, int type)
  6178. {
  6179. nullpo_retr(-1, sd);
  6180. switch( type ) {
  6181. case SP_STR: return sd->status.str;
  6182. case SP_AGI: return sd->status.agi;
  6183. case SP_VIT: return sd->status.vit;
  6184. case SP_INT: return sd->status.int_;
  6185. case SP_DEX: return sd->status.dex;
  6186. case SP_LUK: return sd->status.luk;
  6187. default:
  6188. return -1;
  6189. }
  6190. }
  6191. /// Sets the specified stat to the specified value.
  6192. /// Returns the new value.
  6193. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6194. {
  6195. nullpo_retr(-1, sd);
  6196. switch( type ) {
  6197. case SP_STR: sd->status.str = val; break;
  6198. case SP_AGI: sd->status.agi = val; break;
  6199. case SP_VIT: sd->status.vit = val; break;
  6200. case SP_INT: sd->status.int_ = val; break;
  6201. case SP_DEX: sd->status.dex = val; break;
  6202. case SP_LUK: sd->status.luk = val; break;
  6203. default:
  6204. return -1;
  6205. }
  6206. return val;
  6207. }
  6208. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6209. int pc_gets_status_point(int level)
  6210. {
  6211. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6212. return (statp[level+1] - statp[level]);
  6213. else //Default increase
  6214. return ((level+15) / 5);
  6215. }
  6216. #ifdef RENEWAL_STAT
  6217. /// Renewal status point cost formula
  6218. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6219. #else
  6220. /// Pre-Renewal status point cost formula
  6221. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6222. #endif
  6223. /// Returns the number of stat points needed to change the specified stat by val.
  6224. /// If val is negative, returns the number of stat points that would be needed to
  6225. /// raise the specified stat from (current value - val) to current value.
  6226. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6227. {
  6228. int low, high, sp = 0, max = 0;
  6229. if ( val == 0 )
  6230. return 0;
  6231. low = pc_getstat(sd,type);
  6232. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6233. if ( low >= max && val > 0 )
  6234. return 0; // Official servers show '0' when max is reached
  6235. high = low + val;
  6236. if ( val < 0 )
  6237. SWAP(low, high);
  6238. for ( ; low < high; low++ )
  6239. sp += PC_STATUS_POINT_COST(low);
  6240. return sp;
  6241. }
  6242. /**
  6243. * Returns the value the specified stat can be increased by with the current
  6244. * amount of available status points for the current character's class.
  6245. *
  6246. * @param sd The target character.
  6247. * @param type Stat to verify.
  6248. * @return Maximum value the stat could grow by.
  6249. */
  6250. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6251. {
  6252. int base, final_val, status_points, max_param;
  6253. nullpo_ret(sd);
  6254. base = final_val = pc_getstat(sd, type);
  6255. status_points = sd->status.status_point;
  6256. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6257. while (final_val <= max_param && status_points >= 0) {
  6258. status_points -= PC_STATUS_POINT_COST(final_val);
  6259. final_val++;
  6260. }
  6261. final_val--;
  6262. return (final_val > base ? final_val-base : 0);
  6263. }
  6264. /**
  6265. * Raises a stat by the specified amount.
  6266. *
  6267. * Obeys max_parameter limits.
  6268. * Subtracts status points according to the cost of the increased stat points.
  6269. *
  6270. * @param sd The target character.
  6271. * @param type The stat to change (see enum _sp)
  6272. * @param increase The stat increase (strictly positive) amount.
  6273. * @retval true if the stat was increased by any amount.
  6274. * @retval false if there were no changes.
  6275. */
  6276. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6277. {
  6278. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6279. nullpo_ret(sd);
  6280. // check conditions
  6281. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6282. clif_statusupack(sd, type, 0, 0);
  6283. return false;
  6284. }
  6285. // check limits
  6286. current = pc_getstat(sd, type);
  6287. max_increase = pc_maxparameterincrease(sd, type);
  6288. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6289. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6290. clif_statusupack(sd, type, 0, 0);
  6291. return false;
  6292. }
  6293. // check status points
  6294. needed_points = pc_need_status_point(sd, type, increase);
  6295. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6296. clif_statusupack(sd, type, 0, 0);
  6297. return false;
  6298. }
  6299. // set new values
  6300. final_value = pc_setstat(sd, type, current + increase);
  6301. sd->status.status_point -= needed_points;
  6302. status_calc_pc(sd,SCO_NONE);
  6303. // update increase cost indicator
  6304. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6305. // update statpoint count
  6306. clif_updatestatus(sd, SP_STATUSPOINT);
  6307. // update stat value
  6308. clif_statusupack(sd, type, 1, final_value); // required
  6309. if( final_value > 255 )
  6310. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6311. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6312. return true;
  6313. }
  6314. /**
  6315. * Raises a stat by the specified amount.
  6316. *
  6317. * Obeys max_parameter limits.
  6318. * Does not subtract status points for the cost of the modified stat points.
  6319. *
  6320. * @param sd The target character.
  6321. * @param type The stat to change (see enum _sp)
  6322. * @param val The stat increase (or decrease) amount.
  6323. * @return the stat increase amount.
  6324. * @retval 0 if no changes were made.
  6325. */
  6326. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6327. {
  6328. int max, need;
  6329. nullpo_ret(sd);
  6330. if( type < SP_STR || type > SP_LUK )
  6331. {
  6332. clif_statusupack(sd,type,0,0);
  6333. return 0;
  6334. }
  6335. need = pc_need_status_point(sd,type,1);
  6336. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6337. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6338. status_calc_pc(sd,SCO_NONE);
  6339. // update increase cost indicator
  6340. if( need != pc_need_status_point(sd,type,1) )
  6341. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6342. // update stat value
  6343. clif_statusupack(sd,type,1,val); // required
  6344. if( val > 255 )
  6345. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6346. return val;
  6347. }
  6348. /*==========================================
  6349. * Update skill_lv for player sd
  6350. * Skill point allocation
  6351. *------------------------------------------*/
  6352. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6353. {
  6354. uint16 idx = skill_get_index(skill_id);
  6355. nullpo_retv(sd);
  6356. if (!idx) {
  6357. if (skill_id)
  6358. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6359. return;
  6360. }
  6361. // Level up guild skill
  6362. if (SKILL_CHK_GUILD(skill_id)) {
  6363. guild_skillup(sd, skill_id);
  6364. return;
  6365. }
  6366. // Level up homunculus skill
  6367. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6368. hom_skillup(sd->hd, skill_id);
  6369. return;
  6370. }
  6371. else {
  6372. if( sd->status.skill_point > 0 &&
  6373. sd->status.skill[idx].id &&
  6374. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6375. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6376. {
  6377. int lv, range, upgradable;
  6378. sd->status.skill[idx].lv++;
  6379. sd->status.skill_point--;
  6380. if( !skill_get_inf(skill_id) )
  6381. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6382. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6383. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6384. else
  6385. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6386. lv = sd->status.skill[idx].lv;
  6387. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6388. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6389. clif_skillup(sd,skill_id,lv,range,upgradable);
  6390. clif_updatestatus(sd,SP_SKILLPOINT);
  6391. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6392. clif_updatestatus(sd,SP_CARTINFO);
  6393. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6394. clif_skillinfoblock(sd);
  6395. }
  6396. //else
  6397. // 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);
  6398. }
  6399. }
  6400. /*==========================================
  6401. * /allskill
  6402. *------------------------------------------*/
  6403. int pc_allskillup(struct map_session_data *sd)
  6404. {
  6405. int i;
  6406. nullpo_ret(sd);
  6407. for (i = 0; i < MAX_SKILL; i++) {
  6408. 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) {
  6409. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6410. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6411. if (sd->status.skill[i].lv == 0)
  6412. sd->status.skill[i].id = 0;
  6413. }
  6414. }
  6415. if (!pc_grant_allskills(sd, true)) {
  6416. uint16 sk_id;
  6417. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6418. int inf2 = 0;
  6419. uint16 sk_idx = 0;
  6420. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6421. continue;
  6422. inf2 = skill_get_inf2(sk_id);
  6423. if (
  6424. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6425. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6426. sk_id == SG_DEVIL
  6427. )
  6428. continue; //Cannot be learned normally.
  6429. sd->status.skill[sk_idx].id = sk_id;
  6430. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6431. }
  6432. }
  6433. status_calc_pc(sd,SCO_NONE);
  6434. //Required because if you could level up all skills previously,
  6435. //the update will not be sent as only the lv variable changes.
  6436. clif_skillinfoblock(sd);
  6437. return 0;
  6438. }
  6439. /*==========================================
  6440. * /resetlvl
  6441. *------------------------------------------*/
  6442. int pc_resetlvl(struct map_session_data* sd,int type)
  6443. {
  6444. int i;
  6445. nullpo_ret(sd);
  6446. if (type != 3) //Also reset skills
  6447. pc_resetskill(sd, 0);
  6448. if(type == 1){
  6449. sd->status.skill_point=0;
  6450. sd->status.base_level=1;
  6451. sd->status.job_level=1;
  6452. sd->status.base_exp=0;
  6453. sd->status.job_exp=0;
  6454. if(sd->sc.option !=0)
  6455. sd->sc.option = 0;
  6456. sd->status.str=1;
  6457. sd->status.agi=1;
  6458. sd->status.vit=1;
  6459. sd->status.int_=1;
  6460. sd->status.dex=1;
  6461. sd->status.luk=1;
  6462. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6463. sd->status.status_point=100; // not 88 [celest]
  6464. // give platinum skills upon changing
  6465. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6466. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6467. }
  6468. }
  6469. if(type == 2){
  6470. sd->status.skill_point=0;
  6471. sd->status.base_level=1;
  6472. sd->status.job_level=1;
  6473. sd->status.base_exp=0;
  6474. sd->status.job_exp=0;
  6475. }
  6476. if(type == 3){
  6477. sd->status.base_level=1;
  6478. sd->status.base_exp=0;
  6479. }
  6480. if(type == 4){
  6481. sd->status.job_level=1;
  6482. sd->status.job_exp=0;
  6483. }
  6484. clif_updatestatus(sd,SP_STATUSPOINT);
  6485. clif_updatestatus(sd,SP_STR);
  6486. clif_updatestatus(sd,SP_AGI);
  6487. clif_updatestatus(sd,SP_VIT);
  6488. clif_updatestatus(sd,SP_INT);
  6489. clif_updatestatus(sd,SP_DEX);
  6490. clif_updatestatus(sd,SP_LUK);
  6491. clif_updatestatus(sd,SP_BASELEVEL);
  6492. clif_updatestatus(sd,SP_JOBLEVEL);
  6493. clif_updatestatus(sd,SP_STATUSPOINT);
  6494. clif_updatestatus(sd,SP_BASEEXP);
  6495. clif_updatestatus(sd,SP_JOBEXP);
  6496. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6497. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6498. clif_updatestatus(sd,SP_SKILLPOINT);
  6499. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6500. clif_updatestatus(sd,SP_UAGI);
  6501. clif_updatestatus(sd,SP_UVIT);
  6502. clif_updatestatus(sd,SP_UINT);
  6503. clif_updatestatus(sd,SP_UDEX);
  6504. clif_updatestatus(sd,SP_ULUK); // End Addition
  6505. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6506. if(sd->equip_index[i] >= 0)
  6507. if(pc_isequip(sd,sd->equip_index[i]))
  6508. pc_unequipitem(sd,sd->equip_index[i],2);
  6509. }
  6510. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6511. party_send_levelup(sd);
  6512. status_calc_pc(sd, SCO_FORCE);
  6513. clif_skillinfoblock(sd);
  6514. return 0;
  6515. }
  6516. /*==========================================
  6517. * /resetstate
  6518. *------------------------------------------*/
  6519. int pc_resetstate(struct map_session_data* sd)
  6520. {
  6521. nullpo_ret(sd);
  6522. if (battle_config.use_statpoint_table)
  6523. { // New statpoint table used here - Dexity
  6524. if (sd->status.base_level > MAX_LEVEL)
  6525. { //statp[] goes out of bounds, can't reset!
  6526. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6527. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6528. return 0;
  6529. }
  6530. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6531. }
  6532. else
  6533. {
  6534. int add=0;
  6535. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6536. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6537. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6538. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6539. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6540. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6541. sd->status.status_point+=add;
  6542. }
  6543. pc_setstat(sd, SP_STR, 1);
  6544. pc_setstat(sd, SP_AGI, 1);
  6545. pc_setstat(sd, SP_VIT, 1);
  6546. pc_setstat(sd, SP_INT, 1);
  6547. pc_setstat(sd, SP_DEX, 1);
  6548. pc_setstat(sd, SP_LUK, 1);
  6549. clif_updatestatus(sd,SP_STR);
  6550. clif_updatestatus(sd,SP_AGI);
  6551. clif_updatestatus(sd,SP_VIT);
  6552. clif_updatestatus(sd,SP_INT);
  6553. clif_updatestatus(sd,SP_DEX);
  6554. clif_updatestatus(sd,SP_LUK);
  6555. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6556. clif_updatestatus(sd,SP_UAGI);
  6557. clif_updatestatus(sd,SP_UVIT);
  6558. clif_updatestatus(sd,SP_UINT);
  6559. clif_updatestatus(sd,SP_UDEX);
  6560. clif_updatestatus(sd,SP_ULUK); // End Addition
  6561. clif_updatestatus(sd,SP_STATUSPOINT);
  6562. if( sd->mission_mobid ) { //bugreport:2200
  6563. sd->mission_mobid = 0;
  6564. sd->mission_count = 0;
  6565. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6566. }
  6567. status_calc_pc(sd, SCO_NONE);
  6568. return 1;
  6569. }
  6570. /*==========================================
  6571. * /resetskill
  6572. * if flag&1, perform block resync and status_calc call.
  6573. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6574. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6575. *------------------------------------------*/
  6576. int pc_resetskill(struct map_session_data* sd, int flag)
  6577. {
  6578. int i, skill_point=0;
  6579. nullpo_ret(sd);
  6580. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6581. return 0;
  6582. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6583. /**
  6584. * 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)
  6585. **/
  6586. if( pc_is_taekwon_ranker(sd) )
  6587. return 0;
  6588. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6589. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6590. i = sd->sc.option;
  6591. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6592. i &= ~OPTION_RIDING;
  6593. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6594. i &= ~OPTION_FALCON;
  6595. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6596. i &= ~OPTION_DRAGON;
  6597. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6598. i &= ~OPTION_WUG;
  6599. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6600. i &= ~OPTION_WUGRIDER;
  6601. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6602. i &= ~OPTION_MADOGEAR;
  6603. #ifndef NEW_CARTS
  6604. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6605. i &= ~OPTION_CART;
  6606. #else
  6607. if( sd->sc.data[SC_PUSH_CART] )
  6608. pc_setcart(sd, 0);
  6609. #endif
  6610. if( i != sd->sc.option )
  6611. pc_setoption(sd, i);
  6612. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6613. hom_vaporize(sd, HOM_ST_ACTIVE);
  6614. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6615. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6616. }
  6617. for( i = 1; i < MAX_SKILL; i++ )
  6618. {
  6619. uint8 lv = sd->status.skill[i].lv;
  6620. int inf2;
  6621. uint16 skill_id = skill_idx2id(i);
  6622. if (lv == 0 || skill_id == 0)
  6623. continue;
  6624. inf2 = skill_get_inf2(skill_id);
  6625. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6626. continue;
  6627. // Don't reset trick dead if not a novice/baby
  6628. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6629. {
  6630. sd->status.skill[i].lv = 0;
  6631. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6632. continue;
  6633. }
  6634. // do not reset basic skill
  6635. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6636. continue;
  6637. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6638. continue;
  6639. if( flag&4 && !skill_ischangesex(skill_id) )
  6640. continue;
  6641. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6642. { //Only handle quest skills in a special way when you can't learn them manually
  6643. if( battle_config.quest_skill_reset && !(flag&2) )
  6644. { //Wipe them
  6645. sd->status.skill[i].lv = 0;
  6646. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6647. }
  6648. continue;
  6649. }
  6650. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6651. skill_point += lv;
  6652. else
  6653. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6654. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6655. if( !(flag&2) )
  6656. {// reset
  6657. sd->status.skill[i].lv = 0;
  6658. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6659. }
  6660. }
  6661. if( flag&2 || !skill_point ) return skill_point;
  6662. sd->status.skill_point += skill_point;
  6663. if (flag&1) {
  6664. clif_updatestatus(sd,SP_SKILLPOINT);
  6665. clif_skillinfoblock(sd);
  6666. status_calc_pc(sd, SCO_FORCE);
  6667. }
  6668. return skill_point;
  6669. }
  6670. /*==========================================
  6671. * /resetfeel [Komurka]
  6672. *------------------------------------------*/
  6673. int pc_resetfeel(struct map_session_data* sd)
  6674. {
  6675. int i;
  6676. nullpo_ret(sd);
  6677. for (i=0; i<MAX_PC_FEELHATE; i++)
  6678. {
  6679. sd->feel_map[i].m = -1;
  6680. sd->feel_map[i].index = 0;
  6681. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6682. }
  6683. return 0;
  6684. }
  6685. int pc_resethate(struct map_session_data* sd)
  6686. {
  6687. int i;
  6688. nullpo_ret(sd);
  6689. for (i=0; i<3; i++)
  6690. {
  6691. sd->hate_mob[i] = -1;
  6692. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6693. }
  6694. return 0;
  6695. }
  6696. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6697. {
  6698. int i, bonus = 0;
  6699. nullpo_ret(sd);
  6700. skill_id = skill_dummy2skill_id(skill_id);
  6701. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6702. if( i < ARRAYLENGTH(sd->skillatk) )
  6703. bonus = sd->skillatk[i].val;
  6704. return bonus;
  6705. }
  6706. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6707. {
  6708. int i, bonus = 0;
  6709. nullpo_ret(sd);
  6710. skill_id = skill_dummy2skill_id(skill_id);
  6711. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6712. if( i < ARRAYLENGTH(sd->subskill) )
  6713. bonus = sd->subskill[i].val;
  6714. return bonus;
  6715. }
  6716. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6717. int i, bonus = sd->bonus.add_heal_rate;
  6718. skill_id = skill_dummy2skill_id(skill_id);
  6719. if( bonus ) {
  6720. switch( skill_id ) {
  6721. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6722. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6723. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6724. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6725. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6726. }
  6727. }
  6728. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6729. if( i < ARRAYLENGTH(sd->skillheal) )
  6730. bonus += sd->skillheal[i].val;
  6731. return bonus;
  6732. }
  6733. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6734. int i, bonus = sd->bonus.add_heal2_rate;
  6735. skill_id = skill_dummy2skill_id(skill_id);
  6736. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6737. if( i < ARRAYLENGTH(sd->skillheal2) )
  6738. bonus += sd->skillheal2[i].val;
  6739. return bonus;
  6740. }
  6741. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6742. {
  6743. if( !pc_isdead(sd) )
  6744. return; // not applicable
  6745. if( sd->bg_id && bg_member_respawn(sd) )
  6746. return; // member revived by battleground
  6747. pc_setstand(sd, true);
  6748. pc_setrestartvalue(sd,3);
  6749. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6750. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6751. }
  6752. static TIMER_FUNC(pc_respawn_timer){
  6753. struct map_session_data *sd = map_id2sd(id);
  6754. if( sd != NULL )
  6755. {
  6756. sd->pvp_point=0;
  6757. pc_respawn(sd,CLR_OUTSIGHT);
  6758. }
  6759. return 0;
  6760. }
  6761. /*==========================================
  6762. * Invoked when a player has received damage
  6763. *------------------------------------------*/
  6764. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6765. {
  6766. if (sp) clif_updatestatus(sd,SP_SP);
  6767. if (hp) clif_updatestatus(sd,SP_HP);
  6768. else return;
  6769. if (!src)
  6770. return;
  6771. if( pc_issit(sd) ) {
  6772. pc_setstand(sd, true);
  6773. skill_sit(sd,0);
  6774. }
  6775. if (sd->progressbar.npc_id)
  6776. clif_progressbar_abort(sd);
  6777. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6778. pet_target_check(sd->pd,src,1);
  6779. if( sd->status.ele_id > 0 )
  6780. elemental_set_target(sd,src);
  6781. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6782. sd->canlog_tick = gettick();
  6783. }
  6784. TIMER_FUNC(pc_close_npc_timer){
  6785. TBL_PC *sd = map_id2sd(id);
  6786. if(sd) pc_close_npc(sd,data);
  6787. return 0;
  6788. }
  6789. /*
  6790. * Method to properly close npc for player and clear anything related
  6791. * @flag == 1 : produce close button
  6792. * @flag == 2 : directly close it
  6793. */
  6794. void pc_close_npc(struct map_session_data *sd,int flag)
  6795. {
  6796. nullpo_retv(sd);
  6797. if (sd->npc_id || sd->npc_shopid) {
  6798. if (sd->state.using_fake_npc) {
  6799. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6800. sd->state.using_fake_npc = 0;
  6801. }
  6802. if (sd->st) {
  6803. if(sd->st->state == RUN){ //wait ending code execution
  6804. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6805. return;
  6806. }
  6807. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6808. sd->st->mes_active = 0;
  6809. }
  6810. sd->state.menu_or_input = 0;
  6811. sd->npc_menu = 0;
  6812. sd->npc_shopid = 0;
  6813. #ifdef SECURE_NPCTIMEOUT
  6814. sd->npc_idle_timer = INVALID_TIMER;
  6815. #endif
  6816. if (sd->st && sd->st->state == CLOSE) {
  6817. clif_scriptclose(sd, sd->npc_id);
  6818. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6819. sd->st->state = END; // Force to end now
  6820. }
  6821. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6822. script_free_state(sd->st);
  6823. sd->st = NULL;
  6824. sd->npc_id = 0;
  6825. }
  6826. }
  6827. }
  6828. /*==========================================
  6829. * Invoked when a player has negative current hp
  6830. *------------------------------------------*/
  6831. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6832. {
  6833. int i=0,k=0;
  6834. unsigned int tick = gettick();
  6835. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6836. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6837. // Super Novices have no kill or die functions attached when saved by their angel
  6838. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6839. unsigned int exp = pc_nextbaseexp(sd);
  6840. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6841. sd->state.snovice_dead_flag = 1;
  6842. pc_setrestartvalue(sd,1);
  6843. status_percent_heal(&sd->bl, 100, 100);
  6844. clif_resurrection(&sd->bl, 1);
  6845. if(battle_config.pc_invincible_time)
  6846. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6847. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6848. if(mapdata_flag_gvg2(mapdata))
  6849. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6850. return 0;
  6851. }
  6852. }
  6853. for(k = 0; k < MAX_DEVOTION; k++) {
  6854. if (sd->devotion[k]){
  6855. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6856. if (devsd)
  6857. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6858. sd->devotion[k] = 0;
  6859. }
  6860. }
  6861. if(sd->shadowform_id) { //if we were target of shadowform
  6862. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6863. sd->shadowform_id = 0; //should be remove on status end anyway
  6864. }
  6865. if(sd->status.pet_id > 0 && sd->pd) {
  6866. struct pet_data *pd = sd->pd;
  6867. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  6868. pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
  6869. if( pd->pet.intimate < 0 )
  6870. pd->pet.intimate = 0;
  6871. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6872. }
  6873. if( sd->pd->target_id ) // Unlock all targets...
  6874. pet_unlocktarget(sd->pd);
  6875. }
  6876. 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)
  6877. hom_vaporize(sd, HOM_ST_ACTIVE);
  6878. if( sd->md )
  6879. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6880. if( sd->ed )
  6881. elemental_delete(sd->ed);
  6882. // Leave duel if you die [LuzZza]
  6883. if(battle_config.duel_autoleave_when_die) {
  6884. if(sd->duel_group > 0)
  6885. duel_leave(sd->duel_group, sd);
  6886. if(sd->duel_invite > 0)
  6887. duel_reject(sd->duel_invite, sd);
  6888. }
  6889. pc_close_npc(sd,2); //close npc if we were using one
  6890. /* e.g. not killed thru pc_damage */
  6891. if( pc_issit(sd) ) {
  6892. clif_status_load(&sd->bl,EFST_SIT,0);
  6893. }
  6894. pc_setdead(sd);
  6895. clif_party_dead( sd );
  6896. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  6897. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6898. //Reset menu skills/item skills
  6899. if ((sd->skillitem) != 0)
  6900. sd->skillitem = sd->skillitemlv = 0;
  6901. if ((sd->menuskill_id) != 0)
  6902. sd->menuskill_id = sd->menuskill_val = 0;
  6903. //Reset ticks.
  6904. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6905. if ( sd->spiritball !=0 )
  6906. pc_delspiritball(sd,sd->spiritball,0);
  6907. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6908. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6909. if (src)
  6910. switch (src->type) {
  6911. case BL_MOB:
  6912. {
  6913. struct mob_data *md=(struct mob_data *)src;
  6914. if(md->target_id==sd->bl.id)
  6915. mob_unlocktarget(md,tick);
  6916. if(battle_config.mobs_level_up && md->status.hp &&
  6917. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6918. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6919. ) { // monster level up [Valaris]
  6920. clif_misceffect(&md->bl,0);
  6921. md->level++;
  6922. status_calc_mob(md, SCO_NONE);
  6923. status_percent_heal(src,10,0);
  6924. if( battle_config.show_mob_info&4 )
  6925. {// update name with new level
  6926. clif_name_area(&md->bl);
  6927. }
  6928. }
  6929. src = battle_get_master(src); // Maybe Player Summon
  6930. }
  6931. break;
  6932. case BL_PET: //Pass on to master...
  6933. case BL_HOM:
  6934. case BL_MER:
  6935. src = battle_get_master(src);
  6936. break;
  6937. }
  6938. if (src && src->type == BL_PC) {
  6939. struct map_session_data *ssd = (struct map_session_data *)src;
  6940. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6941. npc_script_event(ssd, NPCE_KILLPC);
  6942. if (battle_config.pk_mode&2) {
  6943. ssd->status.manner -= 5;
  6944. if(ssd->status.manner < 0)
  6945. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6946. #if 0
  6947. // PK/Karma system code (not enabled yet) [celest]
  6948. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6949. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6950. // karma going down = more 'good' / more honourable.
  6951. // The Karma System way...
  6952. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6953. sd->status.karma--;
  6954. ssd->status.karma--;
  6955. }
  6956. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6957. ssd->status.karma++;
  6958. // or the PK System way...
  6959. if (sd->status.karma > 0) // player killed is dishonourable?
  6960. ssd->status.karma--; // honour points earned
  6961. sd->status.karma++; // honour points lost
  6962. // To-do: Receive exp on certain occasions
  6963. #endif
  6964. }
  6965. }
  6966. if(battle_config.bone_drop==2
  6967. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  6968. {
  6969. struct item item_tmp;
  6970. memset(&item_tmp,0,sizeof(item_tmp));
  6971. item_tmp.nameid=ITEMID_SKULL_;
  6972. item_tmp.identify=1;
  6973. item_tmp.card[0]=CARD0_CREATE;
  6974. item_tmp.card[1]=0;
  6975. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6976. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6977. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6978. }
  6979. //Remove bonus_script when dead
  6980. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6981. // changed penalty options, added death by player if pk_mode [Valaris]
  6982. if(battle_config.death_penalty_type
  6983. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6984. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  6985. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  6986. {
  6987. uint32 base_penalty = 0;
  6988. uint32 job_penalty = 0;
  6989. uint32 zeny_penalty = 0;
  6990. if (pc_isvip(sd)) { // EXP penalty for VIP
  6991. base_penalty = battle_config.vip_exp_penalty_base;
  6992. job_penalty = battle_config.vip_exp_penalty_job;
  6993. zeny_penalty = battle_config.vip_zeny_penalty;
  6994. } else {
  6995. base_penalty = battle_config.death_penalty_base;
  6996. job_penalty = battle_config.death_penalty_job;
  6997. zeny_penalty = battle_config.zeny_penalty;
  6998. }
  6999. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7000. switch (battle_config.death_penalty_type) {
  7001. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7002. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7003. }
  7004. if (base_penalty){ //recheck after altering to speedup
  7005. if (battle_config.pk_mode && src && src->type==BL_PC)
  7006. base_penalty *= 2;
  7007. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7008. }
  7009. }
  7010. else
  7011. base_penalty = 0;
  7012. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7013. switch (battle_config.death_penalty_type) {
  7014. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7015. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7016. }
  7017. if (job_penalty) {
  7018. if (battle_config.pk_mode && src && src->type==BL_PC)
  7019. job_penalty *= 2;
  7020. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7021. }
  7022. }
  7023. else
  7024. job_penalty = 0;
  7025. if (base_penalty || job_penalty)
  7026. pc_lostexp(sd, base_penalty, job_penalty);
  7027. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7028. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7029. if(zeny_penalty)
  7030. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7031. }
  7032. }
  7033. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7034. for (const auto &it : mapdata->drop_list) {
  7035. int id = it.drop_id, per = it.drop_per;
  7036. enum e_nightmare_drop_type type = it.drop_type;
  7037. if(id == 0)
  7038. continue;
  7039. if(id == -1){
  7040. int eq_num=0,eq_n[MAX_INVENTORY];
  7041. memset(eq_n,0,sizeof(eq_n));
  7042. for(i=0;i<MAX_INVENTORY;i++) {
  7043. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7044. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7045. || type&NMDT_ALL)
  7046. {
  7047. int l;
  7048. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7049. if( l < MAX_INVENTORY )
  7050. eq_n[l] = i;
  7051. eq_num++;
  7052. }
  7053. }
  7054. if(eq_num > 0){
  7055. int n = eq_n[rnd()%eq_num];
  7056. if(rnd()%10000 < per) {
  7057. if(sd->inventory.u.items_inventory[n].equip)
  7058. pc_unequipitem(sd,n,3);
  7059. pc_dropitem(sd,n,1);
  7060. }
  7061. }
  7062. }
  7063. else if(id > 0) {
  7064. for(i=0;i<MAX_INVENTORY;i++){
  7065. if(sd->inventory.u.items_inventory[i].nameid == id
  7066. && rnd()%10000 < per
  7067. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7068. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7069. || type&NMDT_ALL) ){
  7070. if(sd->inventory.u.items_inventory[i].equip)
  7071. pc_unequipitem(sd,i,3);
  7072. pc_dropitem(sd,i,1);
  7073. break;
  7074. }
  7075. }
  7076. }
  7077. }
  7078. }
  7079. // pvp
  7080. // disable certain pvp functions on pk_mode [Valaris]
  7081. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7082. sd->pvp_point -= 5;
  7083. sd->pvp_lost++;
  7084. if( src && src->type == BL_PC ) {
  7085. struct map_session_data *ssd = (struct map_session_data *)src;
  7086. ssd->pvp_point++;
  7087. ssd->pvp_won++;
  7088. }
  7089. if( sd->pvp_point < 0 ) {
  7090. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7091. return 1|8;
  7092. }
  7093. }
  7094. //GvG
  7095. if( mapdata_flag_gvg2(mapdata) ) {
  7096. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7097. return 1|8;
  7098. }
  7099. else if( sd->bg_id ) {
  7100. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7101. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7102. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7103. return 1|8;
  7104. }
  7105. }
  7106. //Reset "can log out" tick.
  7107. if( battle_config.prevent_logout )
  7108. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7109. return 1;
  7110. }
  7111. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7112. if(hp) clif_updatestatus(sd,SP_HP);
  7113. if(sp) clif_updatestatus(sd,SP_SP);
  7114. pc_setstand(sd, true);
  7115. if(battle_config.pc_invincible_time > 0)
  7116. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7117. if( sd->state.gmaster_flag ) {
  7118. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7119. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7120. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7121. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7122. }
  7123. }
  7124. // script
  7125. //
  7126. /*==========================================
  7127. * script reading pc status registry
  7128. *------------------------------------------*/
  7129. int pc_readparam(struct map_session_data* sd,int type)
  7130. {
  7131. int val = 0;
  7132. nullpo_ret(sd);
  7133. switch(type) {
  7134. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7135. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7136. case SP_ZENY: val = sd->status.zeny; break;
  7137. case SP_BASELEVEL: val = sd->status.base_level; break;
  7138. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7139. case SP_CLASS: val = sd->status.class_; break;
  7140. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7141. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7142. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7143. case SP_SEX: val = sd->status.sex; break;
  7144. case SP_WEIGHT: val = sd->weight; break;
  7145. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7146. case SP_BASEEXP: val = sd->status.base_exp; break;
  7147. case SP_JOBEXP: val = sd->status.job_exp; break;
  7148. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7149. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7150. case SP_HP: val = sd->battle_status.hp; break;
  7151. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7152. case SP_SP: val = sd->battle_status.sp; break;
  7153. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7154. case SP_STR: val = sd->status.str; break;
  7155. case SP_AGI: val = sd->status.agi; break;
  7156. case SP_VIT: val = sd->status.vit; break;
  7157. case SP_INT: val = sd->status.int_; break;
  7158. case SP_DEX: val = sd->status.dex; break;
  7159. case SP_LUK: val = sd->status.luk; break;
  7160. case SP_KARMA: val = sd->status.karma; break;
  7161. case SP_MANNER: val = sd->status.manner; break;
  7162. case SP_FAME: val = sd->status.fame; break;
  7163. case SP_KILLERRID: val = sd->killerrid; break;
  7164. case SP_KILLEDRID: val = sd->killedrid; break;
  7165. case SP_KILLEDGID: val = sd->killedgid; break;
  7166. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7167. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7168. case SP_CHARRENAME: val = sd->status.rename; break;
  7169. case SP_CHARFONT: val = sd->status.font; break;
  7170. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7171. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7172. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7173. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7174. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7175. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7176. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7177. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7178. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7179. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7180. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7181. case SP_DEF1: val = sd->battle_status.def; break;
  7182. case SP_DEF2: val = sd->battle_status.def2; break;
  7183. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7184. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7185. case SP_HIT: val = sd->battle_status.hit; break;
  7186. case SP_FLEE1: val = sd->battle_status.flee; break;
  7187. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7188. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7189. case SP_MAXHPRATE: val = sd->hprate; break;
  7190. case SP_MAXSPRATE: val = sd->sprate; break;
  7191. case SP_SPRATE: val = sd->dsprate; break;
  7192. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7193. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7194. case SP_ASPD_RATE:
  7195. #ifndef RENEWAL_ASPD
  7196. val = sd->battle_status.aspd_rate;
  7197. #else
  7198. val = sd->battle_status.aspd_rate2;
  7199. #endif
  7200. break;
  7201. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7202. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7203. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7204. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7205. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7206. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7207. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7208. case SP_MATK_RATE: val = sd->matk_rate; break;
  7209. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7210. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7211. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7212. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7213. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7214. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7215. case SP_HIT_RATE: val = sd->hit_rate; break;
  7216. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7217. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7218. case SP_DEF_RATE: val = sd->def_rate; break;
  7219. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7220. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7221. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7222. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7223. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7224. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7225. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7226. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7227. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7228. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7229. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7230. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7231. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7232. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7233. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7234. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7235. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7236. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7237. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7238. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7239. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7240. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7241. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7242. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7243. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7244. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7245. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7246. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7247. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7248. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7249. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7250. case SP_DELAYRATE: val = sd->delayrate; break;
  7251. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7252. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7253. case SP_UNSTRIPABLE:
  7254. case SP_UNSTRIPABLE_ARMOR:
  7255. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7256. break;
  7257. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7258. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7259. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7260. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7261. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7262. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7263. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7264. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7265. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7266. case SP_EMATK: val = sd->bonus.ematk; break;
  7267. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7268. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7269. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7270. case SP_CASTRATE:
  7271. case SP_VARCASTRATE:
  7272. #ifdef RENEWAL_CAST
  7273. val = sd->bonus.varcastrate; break;
  7274. #else
  7275. val = sd->castrate; break;
  7276. #endif
  7277. default:
  7278. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7279. return -1;
  7280. }
  7281. return val;
  7282. }
  7283. /*==========================================
  7284. * script set pc status registry
  7285. *------------------------------------------*/
  7286. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7287. {
  7288. nullpo_retr(false,sd);
  7289. switch(type){
  7290. case SP_BASELEVEL:
  7291. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7292. val = pc_maxbaselv(sd);
  7293. if ((unsigned int)val > sd->status.base_level) {
  7294. int i = 0;
  7295. int stat=0;
  7296. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7297. stat += pc_gets_status_point(sd->status.base_level + i);
  7298. sd->status.status_point += stat;
  7299. }
  7300. sd->status.base_level = (unsigned int)val;
  7301. sd->status.base_exp = 0;
  7302. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7303. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7304. clif_updatestatus(sd, SP_STATUSPOINT);
  7305. clif_updatestatus(sd, SP_BASEEXP);
  7306. status_calc_pc(sd, SCO_FORCE);
  7307. if(sd->status.party_id)
  7308. party_send_levelup(sd);
  7309. break;
  7310. case SP_JOBLEVEL:
  7311. if ((unsigned int)val >= sd->status.job_level) {
  7312. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7313. sd->status.skill_point += val - sd->status.job_level;
  7314. clif_updatestatus(sd, SP_SKILLPOINT);
  7315. }
  7316. sd->status.job_level = (unsigned int)val;
  7317. sd->status.job_exp = 0;
  7318. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7319. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7320. clif_updatestatus(sd, SP_JOBEXP);
  7321. status_calc_pc(sd, SCO_FORCE);
  7322. break;
  7323. case SP_SKILLPOINT:
  7324. sd->status.skill_point = val;
  7325. break;
  7326. case SP_STATUSPOINT:
  7327. sd->status.status_point = val;
  7328. break;
  7329. case SP_ZENY:
  7330. if( val < 0 )
  7331. return false;// can't set negative zeny
  7332. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7333. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7334. break;
  7335. case SP_BASEEXP:
  7336. {
  7337. val = cap_value(val, 0, INT_MAX);
  7338. if ((unsigned int)val < sd->status.base_exp) // Lost
  7339. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7340. else // Gained
  7341. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7342. }
  7343. return true;
  7344. case SP_JOBEXP:
  7345. {
  7346. val = cap_value(val, 0, INT_MAX);
  7347. if ((unsigned int)val < sd->status.job_exp) // Lost
  7348. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7349. else // Gained
  7350. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7351. }
  7352. return true;
  7353. case SP_SEX:
  7354. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7355. break;
  7356. case SP_WEIGHT:
  7357. sd->weight = val;
  7358. break;
  7359. case SP_MAXWEIGHT:
  7360. sd->max_weight = val;
  7361. break;
  7362. case SP_HP:
  7363. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7364. break;
  7365. case SP_MAXHP:
  7366. if (sd->status.base_level < 100)
  7367. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7368. else if (sd->status.base_level < 151)
  7369. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7370. else
  7371. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7372. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7373. {
  7374. sd->battle_status.hp = sd->battle_status.max_hp;
  7375. clif_updatestatus(sd, SP_HP);
  7376. }
  7377. break;
  7378. case SP_SP:
  7379. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7380. break;
  7381. case SP_MAXSP:
  7382. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7383. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7384. {
  7385. sd->battle_status.sp = sd->battle_status.max_sp;
  7386. clif_updatestatus(sd, SP_SP);
  7387. }
  7388. break;
  7389. case SP_STR:
  7390. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7391. break;
  7392. case SP_AGI:
  7393. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7394. break;
  7395. case SP_VIT:
  7396. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7397. break;
  7398. case SP_INT:
  7399. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7400. break;
  7401. case SP_DEX:
  7402. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7403. break;
  7404. case SP_LUK:
  7405. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7406. break;
  7407. case SP_KARMA:
  7408. sd->status.karma = val;
  7409. break;
  7410. case SP_MANNER:
  7411. sd->status.manner = val;
  7412. if( val < 0 )
  7413. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7414. else {
  7415. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7416. clif_manner_message(sd, 5);
  7417. }
  7418. return true; // status_change_start/status_change_end already sends packets warning the client
  7419. case SP_FAME:
  7420. sd->status.fame = val;
  7421. break;
  7422. case SP_KILLERRID:
  7423. sd->killerrid = val;
  7424. return true;
  7425. case SP_KILLEDRID:
  7426. sd->killedrid = val;
  7427. return true;
  7428. case SP_KILLEDGID:
  7429. sd->killedgid = val;
  7430. return true;
  7431. case SP_CHARMOVE:
  7432. sd->status.character_moves = val;
  7433. return true;
  7434. case SP_CHARRENAME:
  7435. sd->status.rename = val;
  7436. return true;
  7437. case SP_CHARFONT:
  7438. sd->status.font = val;
  7439. clif_font(sd);
  7440. return true;
  7441. case SP_BANK_VAULT:
  7442. if (val < 0)
  7443. return false;
  7444. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7445. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7446. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7447. return true;
  7448. case SP_ROULETTE_BRONZE:
  7449. sd->roulette_point.bronze = val;
  7450. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7451. return true;
  7452. case SP_ROULETTE_SILVER:
  7453. sd->roulette_point.silver = val;
  7454. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7455. return true;
  7456. case SP_ROULETTE_GOLD:
  7457. sd->roulette_point.gold = val;
  7458. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7459. return true;
  7460. case SP_CASHPOINTS:
  7461. if (val < 0)
  7462. return false;
  7463. if (!sd->state.connect_new)
  7464. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7465. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7466. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7467. return true;
  7468. case SP_KAFRAPOINTS:
  7469. if (val < 0)
  7470. return false;
  7471. if (!sd->state.connect_new)
  7472. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7473. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7474. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7475. return true;
  7476. case SP_PCDIECOUNTER:
  7477. if (val < 0)
  7478. return false;
  7479. if (sd->die_counter == val)
  7480. return true;
  7481. sd->die_counter = val;
  7482. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7483. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7484. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7485. return true;
  7486. case SP_COOKMASTERY:
  7487. if (val < 0)
  7488. return false;
  7489. if (sd->cook_mastery == val)
  7490. return true;
  7491. val = cap_value(val, 0, 1999);
  7492. sd->cook_mastery = val;
  7493. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7494. return true;
  7495. default:
  7496. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7497. return false;
  7498. }
  7499. clif_updatestatus(sd,type);
  7500. return true;
  7501. }
  7502. /*==========================================
  7503. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7504. *------------------------------------------*/
  7505. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7506. {
  7507. if (type&2) {
  7508. if (hp || type&4)
  7509. clif_heal(sd->fd,SP_HP,hp);
  7510. if (sp)
  7511. clif_heal(sd->fd,SP_SP,sp);
  7512. } else {
  7513. if(hp)
  7514. clif_updatestatus(sd,SP_HP);
  7515. if(sp)
  7516. clif_updatestatus(sd,SP_SP);
  7517. }
  7518. return;
  7519. }
  7520. /**
  7521. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7522. * @param sd: Player data
  7523. * @param itemid: Item ID
  7524. * @param hp: HP to heal
  7525. * @param sp: SP to heal
  7526. * @return Amount healed to an object
  7527. */
  7528. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7529. {
  7530. int bonus, tmp, penalty = 0;
  7531. if (hp) {
  7532. int i;
  7533. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7534. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7535. if (potion_flag == 2) {
  7536. bonus += 50;
  7537. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7538. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7539. }
  7540. //All item bonuses.
  7541. bonus += sd->bonus.itemhealrate2;
  7542. //Item Group bonuses
  7543. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7544. //Individual item bonuses.
  7545. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
  7546. if (sd->itemhealrate[i].nameid == itemid) {
  7547. bonus += sd->itemhealrate[i].rate;
  7548. break;
  7549. }
  7550. }
  7551. // Recovery Potion
  7552. if (sd->sc.data[SC_INCHEALRATE])
  7553. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7554. // 2014 Halloween Event : Pumpkin Bonus
  7555. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7556. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7557. tmp = hp * bonus / 100; // Overflow check
  7558. if (bonus != 100 && tmp > hp)
  7559. hp = tmp;
  7560. }
  7561. if (sp) {
  7562. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7563. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7564. if (potion_flag == 2)
  7565. bonus += 50;
  7566. tmp = sp * bonus / 100; // Overflow check
  7567. if (bonus != 100 && tmp > sp)
  7568. sp = tmp;
  7569. }
  7570. if (sd->sc.count) {
  7571. // Critical Wound and Death Hurt stack
  7572. if (sd->sc.data[SC_CRITICALWOUND])
  7573. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7574. if (sd->sc.data[SC_DEATHHURT])
  7575. penalty += 20;
  7576. if (sd->sc.data[SC_NORECOVER_STATE])
  7577. penalty = 100;
  7578. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7579. hp += hp / 2; // 1.5 times
  7580. sp -= sp / 2;
  7581. }
  7582. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7583. hp += hp / 10;
  7584. sp += sp / 10;
  7585. }
  7586. if (penalty > 0) {
  7587. hp -= hp * penalty / 100;
  7588. sp -= sp * penalty / 100;
  7589. }
  7590. #ifdef RENEWAL
  7591. if (sd->sc.data[SC_EXTREMITYFIST2])
  7592. sp = 0;
  7593. #endif
  7594. if (sd->sc.data[SC_BITESCAR])
  7595. hp = 0;
  7596. }
  7597. return status_heal(&sd->bl, hp, sp, 1);
  7598. }
  7599. /*==========================================
  7600. * HP/SP Recovery
  7601. * Heal player hp nad/or sp by rate
  7602. *------------------------------------------*/
  7603. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7604. {
  7605. nullpo_ret(sd);
  7606. if (hp > 100) hp = 100;
  7607. else if (hp <-100) hp = -100;
  7608. if (sp > 100) sp = 100;
  7609. else if (sp <-100) sp = -100;
  7610. if(hp >= 0 && sp >= 0) //Heal
  7611. return status_percent_heal(&sd->bl, hp, sp);
  7612. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7613. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7614. //Crossed signs
  7615. if(hp) {
  7616. if(hp > 0)
  7617. status_percent_heal(&sd->bl, hp, 0);
  7618. else
  7619. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7620. }
  7621. if(sp) {
  7622. if(sp > 0)
  7623. status_percent_heal(&sd->bl, 0, sp);
  7624. else
  7625. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7626. }
  7627. return 0;
  7628. }
  7629. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7630. {
  7631. struct mob_data *md;
  7632. int flag;
  7633. md = (struct mob_data *)bl;
  7634. nullpo_ret(md);
  7635. flag = va_arg(ap, int);
  7636. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7637. status_kill(&md->bl);
  7638. return 1;
  7639. }
  7640. /**
  7641. * Called when player changes job
  7642. * Rewrote to make it tidider [Celest]
  7643. * @param sd
  7644. * @param job JOB ID. See enum e_job
  7645. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7646. * @return True if success, false if failed
  7647. **/
  7648. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7649. {
  7650. int i, fame_flag = 0;
  7651. int b_class;
  7652. nullpo_retr(false,sd);
  7653. if (job < 0)
  7654. return false;
  7655. //Normalize job.
  7656. b_class = pc_jobid2mapid(job);
  7657. if (b_class == -1)
  7658. return false;
  7659. switch (upper) {
  7660. case 1:
  7661. b_class|= JOBL_UPPER;
  7662. break;
  7663. case 2:
  7664. b_class|= JOBL_BABY;
  7665. break;
  7666. }
  7667. //This will automatically adjust bard/dancer classes to the correct gender
  7668. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7669. job = pc_mapid2jobid(b_class, sd->status.sex);
  7670. if (job == -1)
  7671. return false;
  7672. if ((unsigned short)b_class == sd->class_)
  7673. return false; //Nothing to change.
  7674. // changing from 1st to 2nd job
  7675. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7676. sd->change_level_2nd = sd->status.job_level;
  7677. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7678. }
  7679. // changing from 2nd to 3rd job
  7680. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7681. sd->change_level_3rd = sd->status.job_level;
  7682. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7683. }
  7684. if(sd->cloneskill_idx > 0) {
  7685. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7686. sd->status.skill[sd->cloneskill_idx].id = 0;
  7687. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7688. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7689. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7690. }
  7691. sd->cloneskill_idx = 0;
  7692. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7693. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7694. }
  7695. if(sd->reproduceskill_idx > 0) {
  7696. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7697. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7698. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7699. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7700. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7701. }
  7702. sd->reproduceskill_idx = 0;
  7703. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7704. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7705. }
  7706. // Give or reduce transcendent status points
  7707. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7708. sd->status.status_point += battle_config.transcendent_status_points;
  7709. clif_updatestatus(sd,SP_STATUSPOINT);
  7710. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7711. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7712. // The player already used his bonus points, so we have to reset his status points
  7713. pc_resetstate(sd);
  7714. }
  7715. sd->status.status_point -= battle_config.transcendent_status_points;
  7716. clif_updatestatus(sd,SP_STATUSPOINT);
  7717. }
  7718. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7719. const int class_ = pc_class2idx(sd->status.class_);
  7720. uint16 skill_id;
  7721. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7722. //Remove status specific to your current tree skills.
  7723. enum sc_type sc = status_skill2sc(skill_id);
  7724. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7725. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7726. }
  7727. }
  7728. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7729. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7730. pc_resetfeel(sd);
  7731. }
  7732. // Reset body style to 0 before changing job to avoid
  7733. // errors since not every job has a alternate outfit.
  7734. sd->status.body = 0;
  7735. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7736. sd->status.class_ = job;
  7737. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7738. sd->class_ = (unsigned short)b_class;
  7739. sd->status.job_level=1;
  7740. sd->status.job_exp=0;
  7741. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7742. sd->status.base_level = pc_maxbaselv(sd);
  7743. sd->status.base_exp=0;
  7744. pc_resetstate(sd);
  7745. clif_updatestatus(sd,SP_STATUSPOINT);
  7746. clif_updatestatus(sd,SP_BASELEVEL);
  7747. clif_updatestatus(sd,SP_BASEEXP);
  7748. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7749. }
  7750. clif_updatestatus(sd,SP_JOBLEVEL);
  7751. clif_updatestatus(sd,SP_JOBEXP);
  7752. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7753. for(i=0;i<EQI_MAX;i++) {
  7754. if(sd->equip_index[i] >= 0)
  7755. if(pc_isequip(sd,sd->equip_index[i]))
  7756. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7757. }
  7758. //Change look, if disguised, you need to undisguise
  7759. //to correctly calculate new job sprite without
  7760. if (sd->disguise)
  7761. pc_disguise(sd, 0);
  7762. status_set_viewdata(&sd->bl, job);
  7763. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7764. #if PACKETVER >= 20151001
  7765. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7766. #endif
  7767. if(sd->vd.cloth_color)
  7768. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7769. /*
  7770. if(sd->vd.body_style)
  7771. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7772. */
  7773. //Update skill tree.
  7774. pc_calc_skilltree(sd);
  7775. clif_skillinfoblock(sd);
  7776. if (sd->ed)
  7777. elemental_delete(sd->ed);
  7778. if (sd->state.vending)
  7779. vending_closevending(sd);
  7780. if (sd->state.buyingstore)
  7781. buyingstore_close(sd);
  7782. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7783. //Remove peco/cart/falcon
  7784. i = sd->sc.option;
  7785. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7786. i&=~OPTION_RIDING;
  7787. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7788. i&=~OPTION_FALCON;
  7789. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7790. i&=~OPTION_DRAGON;
  7791. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7792. i&=~OPTION_WUGRIDER;
  7793. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7794. i&=~OPTION_WUG;
  7795. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7796. i&=~OPTION_MADOGEAR;
  7797. #ifndef NEW_CARTS
  7798. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7799. i&=~OPTION_CART;
  7800. #else
  7801. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7802. pc_setcart(sd, 0);
  7803. #endif
  7804. if(i != sd->sc.option)
  7805. pc_setoption(sd, i);
  7806. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7807. hom_vaporize(sd, HOM_ST_ACTIVE);
  7808. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7809. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7810. if(sd->status.manner < 0)
  7811. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7812. status_calc_pc(sd,SCO_FORCE);
  7813. pc_checkallowskill(sd);
  7814. pc_equiplookall(sd);
  7815. pc_show_questinfo(sd);
  7816. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7817. if( sd->status.party_id ){
  7818. struct party_data* p;
  7819. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7820. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7821. if( i < MAX_PARTY ){
  7822. p->party.member[i].class_ = sd->status.class_;
  7823. clif_party_job_and_level(sd);
  7824. }
  7825. }
  7826. }
  7827. chrif_save(sd, CSAVE_NORMAL);
  7828. //if you were previously famous, not anymore.
  7829. if (fame_flag)
  7830. chrif_buildfamelist();
  7831. else if (sd->status.fame > 0) {
  7832. //It may be that now they are famous?
  7833. switch (sd->class_&MAPID_UPPERMASK) {
  7834. case MAPID_BLACKSMITH:
  7835. case MAPID_ALCHEMIST:
  7836. case MAPID_TAEKWON:
  7837. chrif_buildfamelist();
  7838. break;
  7839. }
  7840. }
  7841. return true;
  7842. }
  7843. /*==========================================
  7844. * Tell client player sd has change equipement
  7845. *------------------------------------------*/
  7846. void pc_equiplookall(struct map_session_data *sd)
  7847. {
  7848. nullpo_retv(sd);
  7849. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7850. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7851. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7852. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7853. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7854. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7855. }
  7856. /*==========================================
  7857. * Tell client player sd has change look (hair,equip...)
  7858. *------------------------------------------*/
  7859. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7860. nullpo_retv(sd);
  7861. switch(type) {
  7862. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7863. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7864. if (sd->status.hair != val) {
  7865. sd->status.hair = val;
  7866. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7867. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7868. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7869. }
  7870. break;
  7871. case LOOK_WEAPON:
  7872. sd->status.weapon = val;
  7873. break;
  7874. case LOOK_HEAD_BOTTOM:
  7875. sd->status.head_bottom = val;
  7876. sd->setlook_head_bottom = val;
  7877. break;
  7878. case LOOK_HEAD_TOP:
  7879. sd->status.head_top = val;
  7880. sd->setlook_head_top = val;
  7881. break;
  7882. case LOOK_HEAD_MID:
  7883. sd->status.head_mid = val;
  7884. sd->setlook_head_mid = val;
  7885. break;
  7886. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7887. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7888. if (sd->status.hair_color != val) {
  7889. sd->status.hair_color = val;
  7890. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7891. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7892. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7893. }
  7894. break;
  7895. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7896. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7897. sd->status.clothes_color = val;
  7898. break;
  7899. case LOOK_SHIELD:
  7900. sd->status.shield = val;
  7901. break;
  7902. case LOOK_SHOES:
  7903. break;
  7904. case LOOK_ROBE:
  7905. sd->status.robe = val;
  7906. sd->setlook_robe = val;
  7907. break;
  7908. case LOOK_BODY2:
  7909. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7910. sd->status.body = val;
  7911. break;
  7912. }
  7913. clif_changelook(&sd->bl, type, val);
  7914. }
  7915. /*==========================================
  7916. * Give an option (type) to player (sd) and display it to client
  7917. *------------------------------------------*/
  7918. void pc_setoption(struct map_session_data *sd,int type)
  7919. {
  7920. int p_type, new_look=0;
  7921. nullpo_retv(sd);
  7922. p_type = sd->sc.option;
  7923. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7924. sd->sc.option=type;
  7925. clif_changeoption(&sd->bl);
  7926. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7927. { // Mounting
  7928. clif_status_load(&sd->bl,EFST_RIDING,1);
  7929. status_calc_pc(sd,SCO_NONE);
  7930. }
  7931. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7932. { // Dismount
  7933. clif_status_load(&sd->bl,EFST_RIDING,0);
  7934. status_calc_pc(sd,SCO_NONE);
  7935. }
  7936. #ifndef NEW_CARTS
  7937. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7938. clif_cartlist(sd);
  7939. clif_updatestatus(sd, SP_CARTINFO);
  7940. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7941. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7942. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7943. clif_clearcart(sd->fd);
  7944. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7945. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7946. }
  7947. #endif
  7948. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7949. clif_status_load(&sd->bl,EFST_FALCON,1);
  7950. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7951. clif_status_load(&sd->bl,EFST_FALCON,0);
  7952. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7953. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7954. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  7955. status_calc_pc(sd,SCO_NONE);
  7956. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7957. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  7958. status_calc_pc(sd,SCO_NONE);
  7959. }
  7960. }
  7961. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7962. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7963. status_calc_pc(sd,SCO_NONE);
  7964. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7965. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7966. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7967. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7968. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7969. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7970. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7971. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7972. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7973. status_calc_pc(sd,SCO_NONE);
  7974. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7975. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7976. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7977. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7978. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7979. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  7980. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  7981. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  7982. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7983. }
  7984. }
  7985. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7986. new_look = JOB_STAR_GLADIATOR2;
  7987. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7988. new_look = -1;
  7989. if (sd->disguise || !new_look)
  7990. return; //Disguises break sprite changes
  7991. if (new_look < 0) { //Restore normal look.
  7992. status_set_viewdata(&sd->bl, sd->status.class_);
  7993. new_look = sd->vd.class_;
  7994. }
  7995. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7996. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7997. if (sd->vd.cloth_color)
  7998. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7999. if( sd->vd.body_style )
  8000. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8001. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8002. }
  8003. /**
  8004. * Give player a cart
  8005. * @param sd Player
  8006. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8007. **/
  8008. bool pc_setcart(struct map_session_data *sd,int type) {
  8009. #ifndef NEW_CARTS
  8010. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8011. int option;
  8012. #endif
  8013. nullpo_retr(false,sd);
  8014. if( type < 0 || type > MAX_CARTS )
  8015. return false;// Never trust the values sent by the client! [Skotlex]
  8016. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8017. return false;// Push cart is required
  8018. #ifdef NEW_CARTS
  8019. switch( type ) {
  8020. case 0:
  8021. if( !sd->sc.data[SC_PUSH_CART] )
  8022. return 0;
  8023. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8024. clif_clearcart(sd->fd);
  8025. break;
  8026. default:/* everything else is an allowed ID so we can move on */
  8027. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8028. clif_cartlist(sd);
  8029. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8030. }
  8031. clif_updatestatus(sd, SP_CARTINFO);
  8032. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8033. break;
  8034. }
  8035. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8036. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8037. #else
  8038. // Update option
  8039. option = sd->sc.option;
  8040. option &= ~OPTION_CART;// clear cart bits
  8041. option |= cart[type]; // set cart
  8042. pc_setoption(sd, option);
  8043. #endif
  8044. return true;
  8045. }
  8046. /*==========================================
  8047. * Give player a falcon
  8048. *------------------------------------------*/
  8049. void pc_setfalcon(struct map_session_data* sd, int flag)
  8050. {
  8051. if( flag ){
  8052. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8053. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8054. } else if( pc_isfalcon(sd) ){
  8055. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8056. }
  8057. }
  8058. /*==========================================
  8059. * Set player riding
  8060. *------------------------------------------*/
  8061. void pc_setriding(struct map_session_data* sd, int flag)
  8062. {
  8063. if( sd->sc.data[SC_ALL_RIDING] )
  8064. return;
  8065. if( flag ){
  8066. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8067. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8068. } else if( pc_isriding(sd) ){
  8069. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8070. }
  8071. }
  8072. /*==========================================
  8073. * Give player a mado
  8074. *------------------------------------------*/
  8075. void pc_setmadogear(struct map_session_data* sd, int flag)
  8076. {
  8077. if( flag ){
  8078. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8079. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8080. } else if( pc_ismadogear(sd) ){
  8081. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8082. }
  8083. }
  8084. /*==========================================
  8085. * Check if player can drop an item
  8086. *------------------------------------------*/
  8087. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8088. {
  8089. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  8090. return false;
  8091. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8092. return false;
  8093. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8094. }
  8095. /**
  8096. * Determines whether a player can attack based on status changes
  8097. * Why not use status_check_skilluse?
  8098. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8099. * Even ground-based attacks should be blocked by these statuses
  8100. * Called from unit_attack and unit_attack_timer_sub
  8101. * @retval true Can attack
  8102. **/
  8103. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8104. nullpo_retr(false, sd);
  8105. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8106. return false;
  8107. if( sd->sc.data[SC_BASILICA] ||
  8108. sd->sc.data[SC__SHADOWFORM] ||
  8109. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8110. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8111. sd->sc.data[SC_CRYSTALIZE] ||
  8112. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8113. sd->sc.data[SC_TRICKDEAD] ||
  8114. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8115. sd->sc.data[SC_BLADESTOP] ||
  8116. sd->sc.data[SC_DEEPSLEEP] ||
  8117. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8118. sd->sc.data[SC_KINGS_GRACE] )
  8119. return false;
  8120. return true;
  8121. }
  8122. /*==========================================
  8123. * Read '@type' variables (temporary numeric char reg)
  8124. *------------------------------------------*/
  8125. int pc_readreg(struct map_session_data* sd, int64 reg)
  8126. {
  8127. return i64db_iget(sd->regs.vars, reg);
  8128. }
  8129. /*==========================================
  8130. * Set '@type' variables (temporary numeric char reg)
  8131. *------------------------------------------*/
  8132. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8133. {
  8134. unsigned int index = script_getvaridx(reg);
  8135. nullpo_retr(false, sd);
  8136. if( val ) {
  8137. i64db_iput(sd->regs.vars, reg, val);
  8138. if( index )
  8139. script_array_update(&sd->regs, reg, false);
  8140. } else {
  8141. i64db_remove(sd->regs.vars, reg);
  8142. if( index )
  8143. script_array_update(&sd->regs, reg, true);
  8144. }
  8145. return true;
  8146. }
  8147. /*==========================================
  8148. * Read '@type$' variables (temporary string char reg)
  8149. *------------------------------------------*/
  8150. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8151. {
  8152. struct script_reg_str *p = NULL;
  8153. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8154. return p ? p->value : NULL;
  8155. }
  8156. /*==========================================
  8157. * Set '@type$' variables (temporary string char reg)
  8158. *------------------------------------------*/
  8159. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8160. {
  8161. struct script_reg_str *p = NULL;
  8162. unsigned int index = script_getvaridx(reg);
  8163. DBData prev;
  8164. nullpo_retr(false, sd);
  8165. if( str[0] ) {
  8166. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8167. p->value = aStrdup(str);
  8168. p->flag.type = 1;
  8169. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8170. p = (struct script_reg_str *)db_data2ptr(&prev);
  8171. if( p->value )
  8172. aFree(p->value);
  8173. ers_free(str_reg_ers, p);
  8174. } else {
  8175. if( index )
  8176. script_array_update(&sd->regs, reg, false);
  8177. }
  8178. } else {
  8179. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8180. p = (struct script_reg_str *)db_data2ptr(&prev);
  8181. if( p->value )
  8182. aFree(p->value);
  8183. ers_free(str_reg_ers, p);
  8184. if( index )
  8185. script_array_update(&sd->regs, reg, true);
  8186. }
  8187. }
  8188. return true;
  8189. }
  8190. /**
  8191. * Serves the following variable types:
  8192. * - 'type' (permanent numeric char reg)
  8193. * - '#type' (permanent numeric account reg)
  8194. * - '##type' (permanent numeric account reg2)
  8195. **/
  8196. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8197. {
  8198. struct script_reg_num *p = NULL;
  8199. if (!sd->vars_ok) {
  8200. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8201. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8202. //intif->request_registry(sd,type==3?4:type);
  8203. set_eof(sd->fd);
  8204. return 0;
  8205. }
  8206. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8207. return p ? p->value : 0;
  8208. }
  8209. /**
  8210. * Serves the following variable types:
  8211. * - 'type$' (permanent str char reg)
  8212. * - '#type$' (permanent str account reg)
  8213. * - '##type$' (permanent str account reg2)
  8214. **/
  8215. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8216. {
  8217. struct script_reg_str *p = NULL;
  8218. if (!sd->vars_ok) {
  8219. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8220. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8221. //intif->request_registry(sd,type==3?4:type);
  8222. set_eof(sd->fd);
  8223. return NULL;
  8224. }
  8225. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8226. return p ? p->value : NULL;
  8227. }
  8228. /**
  8229. * Serves the following variable types:
  8230. * - 'type' (permanent numeric char reg)
  8231. * - '#type' (permanent numeric account reg)
  8232. * - '##type' (permanent numeric account reg2)
  8233. **/
  8234. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8235. {
  8236. struct script_reg_num *p = NULL;
  8237. const char *regname = get_str(script_getvarid(reg));
  8238. unsigned int index = script_getvaridx(reg);
  8239. if ( !reg_load && !sd->vars_ok ) {
  8240. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8241. return 0;
  8242. }
  8243. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8244. if( val ) {
  8245. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8246. script_array_update(&sd->regs, reg, false);
  8247. p->value = val;
  8248. } else {
  8249. p->value = 0;
  8250. if( index )
  8251. script_array_update(&sd->regs, reg, true);
  8252. }
  8253. if (!reg_load)
  8254. p->flag.update = 1;/* either way, it will require either delete or replace */
  8255. } else if( val ) {
  8256. DBData prev;
  8257. if( index )
  8258. script_array_update(&sd->regs, reg, false);
  8259. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8260. p->value = val;
  8261. if (!reg_load)
  8262. p->flag.update = 1;
  8263. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8264. p = (struct script_reg_num *)db_data2ptr(&prev);
  8265. ers_free(num_reg_ers, p);
  8266. }
  8267. }
  8268. if (!reg_load && p)
  8269. sd->vars_dirty = true;
  8270. return 1;
  8271. }
  8272. /**
  8273. * Serves the following variable types:
  8274. * - 'type$' (permanent str char reg)
  8275. * - '#type$' (permanent str account reg)
  8276. * - '##type$' (permanent str account reg2)
  8277. **/
  8278. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8279. {
  8280. struct script_reg_str *p = NULL;
  8281. const char *regname = get_str(script_getvarid(reg));
  8282. unsigned int index = script_getvaridx(reg);
  8283. size_t vlen = 0;
  8284. if (!reg_load && !sd->vars_ok) {
  8285. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8286. return 0;
  8287. }
  8288. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8289. {
  8290. 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);
  8291. return 0;
  8292. }
  8293. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8294. if( val[0] ) {
  8295. if( p->value )
  8296. aFree(p->value);
  8297. else if ( index ) // an entry that was deleted, so we reset
  8298. script_array_update(&sd->regs, reg, false);
  8299. p->value = aStrdup(val);
  8300. } else {
  8301. if (p->value)
  8302. aFree(p->value);
  8303. p->value = NULL;
  8304. if( index )
  8305. script_array_update(&sd->regs, reg, true);
  8306. }
  8307. if( !reg_load )
  8308. p->flag.update = 1; // either way, it will require either delete or replace
  8309. } else if( val[0] ) {
  8310. DBData prev;
  8311. if( index )
  8312. script_array_update(&sd->regs, reg, false);
  8313. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8314. p->value = aStrdup(val);
  8315. if( !reg_load )
  8316. p->flag.update = 1;
  8317. p->flag.type = 1;
  8318. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8319. p = (struct script_reg_str *)db_data2ptr(&prev);
  8320. if( p->value )
  8321. aFree(p->value);
  8322. ers_free(str_reg_ers, p);
  8323. }
  8324. }
  8325. if( !reg_load && p )
  8326. sd->vars_dirty = true;
  8327. return 1;
  8328. }
  8329. /**
  8330. * Set value of player variable
  8331. * @param sd Player
  8332. * @param reg Variable name
  8333. * @param value
  8334. * @return True if success, false if failed.
  8335. **/
  8336. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8337. char prefix = reg[0];
  8338. nullpo_retr(false, sd);
  8339. if (reg[strlen(reg)-1] == '$') {
  8340. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8341. return false;
  8342. }
  8343. val = cap_value(val, INT_MIN, INT_MAX);
  8344. switch (prefix) {
  8345. case '.':
  8346. case '\'':
  8347. case '$':
  8348. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8349. return false;
  8350. case '@':
  8351. return pc_setreg(sd, add_str(reg), val);
  8352. case '#':
  8353. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8354. default:
  8355. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8356. }
  8357. return false;
  8358. }
  8359. /**
  8360. * Get value of player variable
  8361. * @param sd Player
  8362. * @param reg Variable name
  8363. * @return Variable value or 0 if failed.
  8364. **/
  8365. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8366. char prefix = reg[0];
  8367. nullpo_ret(sd);
  8368. if (reg[strlen(reg)-1] == '$') {
  8369. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8370. return 0;
  8371. }
  8372. switch (prefix) {
  8373. case '.':
  8374. case '\'':
  8375. case '$':
  8376. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8377. return 0;
  8378. case '@':
  8379. return pc_readreg(sd, add_str(reg));
  8380. case '#':
  8381. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8382. default:
  8383. return pc_readglobalreg(sd, add_str(reg));
  8384. }
  8385. return 0;
  8386. }
  8387. /*==========================================
  8388. * Exec eventtimer for player sd (retrieved from map_session (id))
  8389. *------------------------------------------*/
  8390. static TIMER_FUNC(pc_eventtimer){
  8391. struct map_session_data *sd=map_id2sd(id);
  8392. char *p = (char *)data;
  8393. int i;
  8394. if(sd==NULL)
  8395. return 0;
  8396. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8397. if( i < MAX_EVENTTIMER )
  8398. {
  8399. sd->eventtimer[i] = INVALID_TIMER;
  8400. sd->eventcount--;
  8401. npc_event(sd,p,0);
  8402. }
  8403. else
  8404. ShowError("pc_eventtimer: no such event timer\n");
  8405. if (p) aFree(p);
  8406. return 0;
  8407. }
  8408. /*==========================================
  8409. * Add eventtimer for player sd ?
  8410. *------------------------------------------*/
  8411. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8412. {
  8413. int i;
  8414. nullpo_retr(false,sd);
  8415. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8416. if( i == MAX_EVENTTIMER )
  8417. return false;
  8418. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8419. sd->eventcount++;
  8420. return true;
  8421. }
  8422. /*==========================================
  8423. * Del eventtimer for player sd ?
  8424. *------------------------------------------*/
  8425. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8426. {
  8427. char* p = NULL;
  8428. int i;
  8429. nullpo_retr(false,sd);
  8430. if (sd->eventcount == 0)
  8431. return false;
  8432. // find the named event timer
  8433. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8434. sd->eventtimer[i] != INVALID_TIMER &&
  8435. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8436. strcmp(p, name) == 0
  8437. );
  8438. if( i == MAX_EVENTTIMER )
  8439. return false; // not found
  8440. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8441. sd->eventtimer[i] = INVALID_TIMER;
  8442. if(sd->eventcount > 0)
  8443. sd->eventcount--;
  8444. aFree(p);
  8445. return true;
  8446. }
  8447. /*==========================================
  8448. * Update eventtimer count for player sd
  8449. *------------------------------------------*/
  8450. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8451. {
  8452. int i;
  8453. nullpo_retv(sd);
  8454. for(i=0;i<MAX_EVENTTIMER;i++)
  8455. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8456. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8457. addtick_timer(sd->eventtimer[i],tick);
  8458. break;
  8459. }
  8460. }
  8461. /*==========================================
  8462. * Remove all eventtimer for player sd
  8463. *------------------------------------------*/
  8464. void pc_cleareventtimer(struct map_session_data *sd)
  8465. {
  8466. int i;
  8467. nullpo_retv(sd);
  8468. if (sd->eventcount == 0)
  8469. return;
  8470. for(i=0;i<MAX_EVENTTIMER;i++){
  8471. if( sd->eventtimer[i] != INVALID_TIMER ){
  8472. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8473. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8474. sd->eventtimer[i] = INVALID_TIMER;
  8475. if(sd->eventcount > 0) //avoid looping to max val
  8476. sd->eventcount--;
  8477. if (p) aFree(p);
  8478. }
  8479. }
  8480. }
  8481. /**
  8482. * Called when an item with combo is worn
  8483. * @param *sd
  8484. * @param *data struct item_data
  8485. * @return success numbers of succeed combo
  8486. */
  8487. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8488. uint16 i;
  8489. int success = 0;
  8490. for( i = 0; i < data->combos_count; i++ ) {
  8491. struct itemchk {
  8492. int idx;
  8493. unsigned short nameid;
  8494. short card[MAX_SLOTS];
  8495. } *combo_idx;
  8496. int idx, j;
  8497. int nb_itemCombo;
  8498. unsigned int pos = 0;
  8499. /* ensure this isn't a duplicate combo */
  8500. if( sd->combos.bonus != NULL ) {
  8501. int x;
  8502. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8503. /* found a match, skip this combo */
  8504. if( x < sd->combos.count )
  8505. continue;
  8506. }
  8507. nb_itemCombo = data->combos[i]->count;
  8508. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8509. continue;
  8510. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8511. for(j=0; j < nb_itemCombo; j++){
  8512. combo_idx[j].idx=-1;
  8513. combo_idx[j].nameid=-1;
  8514. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8515. }
  8516. for( j = 0; j < nb_itemCombo; j++ ) {
  8517. uint16 id = data->combos[i]->nameid[j], k;
  8518. bool found = false;
  8519. for( k = 0; k < EQI_MAX; k++ ) {
  8520. short index = sd->equip_index[k];
  8521. if( index < 0 )
  8522. continue;
  8523. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8524. continue;
  8525. if (!sd->inventory_data[index] )
  8526. continue;
  8527. if ( itemdb_type(id) != IT_CARD ) {
  8528. if ( sd->inventory_data[index]->nameid != id )
  8529. continue;
  8530. if(j>0){ //check if this item not already used
  8531. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8532. uint8 z;
  8533. for (z = 0; z < nb_itemCombo-1; z++)
  8534. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8535. do_continue=true;
  8536. if(do_continue)
  8537. continue;
  8538. }
  8539. combo_idx[j].nameid = id;
  8540. combo_idx[j].idx = index;
  8541. pos |= sd->inventory.u.items_inventory[index].equip;
  8542. found = true;
  8543. break;
  8544. } else { //Cards and enchants
  8545. uint16 z;
  8546. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8547. continue;
  8548. for (z = 0; z < MAX_SLOTS; z++) {
  8549. bool do_continue=false;
  8550. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8551. continue;
  8552. if(j>0){
  8553. int c1, c2;
  8554. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8555. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8556. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8557. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8558. do_continue = true;
  8559. break;
  8560. }
  8561. }
  8562. }
  8563. }
  8564. }
  8565. if(do_continue)
  8566. continue;
  8567. combo_idx[j].nameid = id;
  8568. combo_idx[j].idx = index;
  8569. combo_idx[j].card[z] = id;
  8570. pos |= sd->inventory.u.items_inventory[index].equip;
  8571. found = true;
  8572. break;
  8573. }
  8574. }
  8575. }
  8576. if( !found )
  8577. break;/* we haven't found all the ids for this combo, so we can return */
  8578. }
  8579. aFree(combo_idx);
  8580. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8581. if( j < nb_itemCombo )
  8582. continue;
  8583. /* we got here, means all items in the combo are matching */
  8584. idx = sd->combos.count;
  8585. if( sd->combos.bonus == NULL ) {
  8586. CREATE(sd->combos.bonus, struct script_code *, 1);
  8587. CREATE(sd->combos.id, unsigned short, 1);
  8588. CREATE(sd->combos.pos, unsigned int, 1);
  8589. sd->combos.count = 1;
  8590. } else {
  8591. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8592. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8593. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8594. }
  8595. /* we simply copy the pointer */
  8596. sd->combos.bonus[idx] = data->combos[i]->script;
  8597. /* save this combo's id */
  8598. sd->combos.id[idx] = data->combos[i]->id;
  8599. /* save pos of combo*/
  8600. sd->combos.pos[idx] = pos;
  8601. success++;
  8602. }
  8603. return success;
  8604. }
  8605. /**
  8606. * Called when an item with combo is removed
  8607. * @param *sd
  8608. * @param *data struct item_data
  8609. * @return retval numbers of removed combo
  8610. */
  8611. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8612. int i, retval = 0;
  8613. if( sd->combos.bonus == NULL )
  8614. return 0;/* nothing to do here, player has no combos */
  8615. for( i = 0; i < data->combos_count; i++ ) {
  8616. /* check if this combo exists in this user */
  8617. int x = 0, cursor = 0, j;
  8618. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8619. /* no match, skip this combo */
  8620. if(x >= sd->combos.count)
  8621. continue;
  8622. sd->combos.bonus[x] = NULL;
  8623. sd->combos.id[x] = 0;
  8624. sd->combos.pos[x] = 0;
  8625. retval++;
  8626. /* check if combo requirements still fit */
  8627. if( pc_checkcombo( sd, data ) )
  8628. continue;
  8629. /* move next value to empty slot */
  8630. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8631. if( sd->combos.bonus[j] == NULL )
  8632. continue;
  8633. if( cursor != j ) {
  8634. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8635. sd->combos.id[cursor] = sd->combos.id[j];
  8636. sd->combos.pos[cursor] = sd->combos.pos[j];
  8637. }
  8638. cursor++;
  8639. }
  8640. /* it's empty, we can clear all the memory */
  8641. if( (sd->combos.count = cursor) == 0 ) {
  8642. aFree(sd->combos.bonus);
  8643. aFree(sd->combos.id);
  8644. aFree(sd->combos.pos);
  8645. sd->combos.bonus = NULL;
  8646. sd->combos.id = NULL;
  8647. sd->combos.pos = NULL;
  8648. return retval; /* we also can return at this point for we have no more combos to check */
  8649. }
  8650. }
  8651. return retval;
  8652. }
  8653. /**
  8654. * Load combo data(s) of player
  8655. * @param *sd
  8656. * @return ret numbers of succeed combo
  8657. */
  8658. int pc_load_combo(struct map_session_data *sd) {
  8659. int i, ret = 0;
  8660. for( i = 0; i < EQI_MAX; i++ ) {
  8661. struct item_data *id = NULL;
  8662. short idx = sd->equip_index[i];
  8663. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8664. continue;
  8665. if( id->combos_count )
  8666. ret += pc_checkcombo(sd,id);
  8667. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8668. struct item_data *data;
  8669. int j;
  8670. for( j = 0; j < id->slot; j++ ) {
  8671. if (!sd->inventory.u.items_inventory[idx].card[j])
  8672. continue;
  8673. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8674. if( data->combos_count )
  8675. ret += pc_checkcombo(sd,data);
  8676. }
  8677. }
  8678. }
  8679. }
  8680. return ret;
  8681. }
  8682. /*==========================================
  8683. * Equip item on player sd at req_pos from inventory index n
  8684. * return: false - fail; true - success
  8685. *------------------------------------------*/
  8686. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8687. {
  8688. int i, pos, flag = 0, iflag;
  8689. struct item_data *id;
  8690. uint8 res = ITEM_EQUIP_ACK_OK;
  8691. nullpo_retr(false,sd);
  8692. if( n < 0 || n >= MAX_INVENTORY ) {
  8693. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8694. return false;
  8695. }
  8696. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8697. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8698. return false;
  8699. }
  8700. if (!(id = sd->inventory_data[n]))
  8701. return false;
  8702. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8703. if(battle_config.battle_log)
  8704. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8705. if((res = pc_isequip(sd,n))) {
  8706. clif_equipitemack(sd,n,0,res); // fail
  8707. return false;
  8708. }
  8709. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8710. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8711. return false;
  8712. }
  8713. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8714. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8715. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8716. return false;
  8717. }
  8718. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8719. /** Failing condition:
  8720. * 1. Always failed to equip ammo if no weapon equipped yet
  8721. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8722. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8723. * (4. The rest is relying on item job/class restriction).
  8724. **/
  8725. if (id->type == IT_AMMO) {
  8726. int w_idx = sd->equip_index[EQI_HAND_R];
  8727. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8728. if (w_idx == -1 ||
  8729. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8730. (id->look != A_GRENADE && w_type == W_GRENADE))
  8731. {
  8732. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8733. return false;
  8734. }
  8735. }
  8736. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8737. int a_idx = sd->equip_index[EQI_AMMO];
  8738. if (a_idx != -1) {
  8739. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8740. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8741. (a_type != A_GRENADE && id->look == W_GRENADE))
  8742. {
  8743. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8744. return false;
  8745. }
  8746. }
  8747. }
  8748. }
  8749. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8750. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8751. clif_notify_bindOnEquip(sd,n);
  8752. }
  8753. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8754. pos = req_pos&EQP_ACC;
  8755. if (pos == EQP_ACC) //User specified both slots..
  8756. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8757. }
  8758. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8759. pos = (req_pos&EQP_ARMS);
  8760. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8761. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8762. }
  8763. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8764. pos = req_pos&EQP_SHADOW_ACC;
  8765. if (pos == EQP_SHADOW_ACC)
  8766. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8767. }
  8768. if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8769. pos = (req_pos&EQP_SHADOW_ARMS);
  8770. if( pos == EQP_SHADOW_ARMS )
  8771. pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8772. }
  8773. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8774. //Update skill-block range database when weapon range changes. [Skotlex]
  8775. i = sd->equip_index[EQI_HAND_R];
  8776. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8777. flag = 1;
  8778. else
  8779. flag = id->range != sd->inventory_data[i]->range;
  8780. }
  8781. for(i=0;i<EQI_MAX;i++) {
  8782. if(pos & equip_bitmask[i]) {
  8783. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8784. pc_unequipitem(sd,sd->equip_index[i],2);
  8785. sd->equip_index[i] = n;
  8786. }
  8787. }
  8788. if(pos==EQP_AMMO) {
  8789. clif_arrowequip(sd,n);
  8790. clif_arrow_fail(sd,3);
  8791. }
  8792. else
  8793. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8794. sd->inventory.u.items_inventory[n].equip = pos;
  8795. if(pos & EQP_HAND_R) {
  8796. if(id)
  8797. sd->weapontype1 = id->look;
  8798. else
  8799. sd->weapontype1 = 0;
  8800. pc_calcweapontype(sd);
  8801. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8802. }
  8803. if(pos & EQP_HAND_L) {
  8804. if(id) {
  8805. if(id->type == IT_WEAPON) {
  8806. sd->status.shield = 0;
  8807. sd->weapontype2 = id->look;
  8808. }
  8809. else
  8810. if(id->type == IT_ARMOR) {
  8811. sd->status.shield = id->look;
  8812. sd->weapontype2 = 0;
  8813. }
  8814. }
  8815. else
  8816. sd->status.shield = sd->weapontype2 = 0;
  8817. pc_calcweapontype(sd);
  8818. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8819. }
  8820. if(pos & EQP_SHOES)
  8821. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8822. pc_set_costume_view(sd);
  8823. pc_checkallowskill(sd); //Check if status changes should be halted.
  8824. iflag = sd->npc_item_flag;
  8825. /* check for combos (MUST be before status_calc_pc) */
  8826. if( id->combos_count )
  8827. pc_checkcombo(sd,id);
  8828. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8829. ; //No cards
  8830. else {
  8831. for( i = 0; i < MAX_SLOTS; i++ ) {
  8832. struct item_data *data;
  8833. if (!sd->inventory.u.items_inventory[n].card[i])
  8834. continue;
  8835. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8836. if( data->combos_count )
  8837. pc_checkcombo(sd,data);
  8838. }
  8839. }
  8840. }
  8841. status_calc_pc(sd,SCO_NONE);
  8842. if (flag) //Update skill data
  8843. clif_skillinfoblock(sd);
  8844. //OnEquip script [Skotlex]
  8845. if (id) {
  8846. //only run the script if item isn't restricted
  8847. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8848. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8849. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8850. ; //No cards
  8851. else {
  8852. for( i = 0; i < MAX_SLOTS; i++ ) {
  8853. struct item_data *data;
  8854. if (!sd->inventory.u.items_inventory[n].card[i])
  8855. continue;
  8856. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8857. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8858. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8859. }
  8860. }
  8861. }
  8862. }
  8863. sd->npc_item_flag = iflag;
  8864. return true;
  8865. }
  8866. /*==========================================
  8867. * Called when attemting to unequip an item from player
  8868. * type:
  8869. * 0 - only unequip
  8870. * 1 - calculate status after unequipping
  8871. * 2 - force unequip
  8872. * return: false - fail; true - success
  8873. *------------------------------------------*/
  8874. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8875. int i, iflag;
  8876. bool status_calc = false;
  8877. nullpo_retr(false,sd);
  8878. if (n < 0 || n >= MAX_INVENTORY) {
  8879. clif_unequipitemack(sd,0,0,0);
  8880. return false;
  8881. }
  8882. if (!sd->inventory.u.items_inventory[n].equip) {
  8883. clif_unequipitemack(sd,n,0,0);
  8884. return false; //Nothing to unequip
  8885. }
  8886. // status change that makes player cannot unequip equipment
  8887. if (!(flag&2) && sd->sc.count &&
  8888. (sd->sc.data[SC_BERSERK] ||
  8889. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8890. sd->sc.data[SC__BLOODYLUST] ||
  8891. sd->sc.data[SC_KYOUGAKU] ||
  8892. (sd->sc.data[SC_PYROCLASTIC] &&
  8893. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8894. {
  8895. clif_unequipitemack(sd,n,0,0);
  8896. return false;
  8897. }
  8898. if (battle_config.battle_log)
  8899. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->inventory.u.items_inventory[n].equip);
  8900. if (!sd->inventory.u.items_inventory[n].equip) { //Nothing to unequip
  8901. clif_unequipitemack(sd, n, 0, 0);
  8902. return false;
  8903. }
  8904. for(i = 0; i < EQI_MAX; i++) {
  8905. if (sd->inventory.u.items_inventory[n].equip & equip_bitmask[i])
  8906. sd->equip_index[i] = -1;
  8907. }
  8908. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_R) {
  8909. sd->weapontype1 = 0;
  8910. sd->status.weapon = sd->weapontype2;
  8911. pc_calcweapontype(sd);
  8912. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8913. if( !battle_config.dancing_weaponswitch_fix )
  8914. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8915. }
  8916. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_L) {
  8917. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  8918. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  8919. sd->status.shield = sd->weapontype2 = 0;
  8920. pc_calcweapontype(sd);
  8921. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8922. }
  8923. if(sd->inventory.u.items_inventory[n].equip & EQP_SHOES)
  8924. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8925. clif_unequipitemack(sd,n,sd->inventory.u.items_inventory[n].equip,1);
  8926. pc_set_costume_view(sd);
  8927. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8928. // On weapon change (right and left hand)
  8929. if ((sd->inventory.u.items_inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8930. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8931. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8932. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8933. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8934. }
  8935. // On armor change
  8936. if (sd->inventory.u.items_inventory[n].equip & EQP_ARMOR) {
  8937. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8938. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8939. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8940. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8941. }
  8942. // On ammo change
  8943. 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_))
  8944. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8945. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8946. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8947. sd->inventory.u.items_inventory[n].equip = 0;
  8948. iflag = sd->npc_item_flag;
  8949. /* check for combos (MUST be before status_calc_pc) */
  8950. if ( sd->inventory_data[n] ) {
  8951. if( sd->inventory_data[n]->combos_count ) {
  8952. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8953. status_calc = true;
  8954. }
  8955. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8956. ; //No cards
  8957. else {
  8958. for( i = 0; i < MAX_SLOTS; i++ ) {
  8959. struct item_data *data;
  8960. if (!sd->inventory.u.items_inventory[n].card[i])
  8961. continue;
  8962. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8963. if( data->combos_count ) {
  8964. if( pc_removecombo(sd,data) )
  8965. status_calc = true;
  8966. }
  8967. }
  8968. }
  8969. }
  8970. }
  8971. if(flag&1 || status_calc) {
  8972. pc_checkallowskill(sd);
  8973. status_calc_pc(sd,SCO_NONE);
  8974. }
  8975. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8976. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8977. //OnUnEquip script [Skotlex]
  8978. if (sd->inventory_data[n]) {
  8979. if (sd->inventory_data[n]->unequip_script)
  8980. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8981. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8982. ; //No cards
  8983. else {
  8984. for( i = 0; i < MAX_SLOTS; i++ ) {
  8985. struct item_data *data;
  8986. if (!sd->inventory.u.items_inventory[n].card[i])
  8987. continue;
  8988. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8989. if( data->unequip_script )
  8990. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8991. }
  8992. }
  8993. }
  8994. }
  8995. sd->npc_item_flag = iflag;
  8996. return true;
  8997. }
  8998. /*==========================================
  8999. * Checking if player (sd) has an invalid item
  9000. * and is unequiped on map load (item_noequip)
  9001. *------------------------------------------*/
  9002. void pc_checkitem(struct map_session_data *sd) {
  9003. int i, calc_flag = 0;
  9004. struct item it;
  9005. nullpo_retv(sd);
  9006. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9007. return;
  9008. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9009. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9010. it = sd->inventory.u.items_inventory[i];
  9011. if( it.nameid == 0 )
  9012. continue;
  9013. if( !it.equip )
  9014. continue;
  9015. if( it.equip&~pc_equippoint(sd,i) ) {
  9016. pc_unequipitem(sd, i, 2);
  9017. calc_flag = 1;
  9018. continue;
  9019. }
  9020. 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) ) {
  9021. pc_unequipitem(sd, i, 2);
  9022. calc_flag = 1;
  9023. continue;
  9024. }
  9025. }
  9026. if( calc_flag && sd->state.active ) {
  9027. pc_checkallowskill(sd);
  9028. status_calc_pc(sd,SCO_NONE);
  9029. }
  9030. }
  9031. /*==========================================
  9032. * Checks for unavailable items and removes them.
  9033. * @param sd: Player data
  9034. * @param type Forced check:
  9035. * 1 - Inventory
  9036. * 2 - Cart
  9037. * 4 - Storage
  9038. *------------------------------------------*/
  9039. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9040. {
  9041. int i;
  9042. unsigned short nameid;
  9043. char output[256];
  9044. nullpo_retv(sd);
  9045. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9046. for(i = 0; i < MAX_INVENTORY; i++) {
  9047. nameid = sd->inventory.u.items_inventory[i].nameid;
  9048. if (!nameid)
  9049. continue;
  9050. if (!itemdb_available(nameid)) {
  9051. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9052. clif_displaymessage(sd->fd, output);
  9053. 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);
  9054. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9055. continue;
  9056. }
  9057. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9058. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9059. }
  9060. }
  9061. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9062. for(i = 0; i < MAX_CART; i++) {
  9063. nameid = sd->cart.u.items_cart[i].nameid;
  9064. if (!nameid)
  9065. continue;
  9066. if (!itemdb_available(nameid)) {
  9067. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9068. clif_displaymessage(sd->fd, output);
  9069. 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);
  9070. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9071. continue;
  9072. }
  9073. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9074. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9075. }
  9076. }
  9077. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9078. for(i = 0; i < sd->storage.max_amount; i++) {
  9079. nameid = sd->storage.u.items_storage[i].nameid;
  9080. if (!nameid)
  9081. continue;
  9082. if (!itemdb_available(nameid)) {
  9083. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9084. clif_displaymessage(sd->fd, output);
  9085. 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);
  9086. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9087. continue;
  9088. }
  9089. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9090. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9091. }
  9092. }
  9093. }
  9094. /*==========================================
  9095. * Update PVP rank for sd1 in cmp to sd2
  9096. *------------------------------------------*/
  9097. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9098. {
  9099. struct map_session_data *sd1,*sd2;
  9100. sd1=(struct map_session_data *)bl;
  9101. sd2=va_arg(ap,struct map_session_data *);
  9102. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9103. {// cannot register pvp rank for hidden GMs
  9104. return 0;
  9105. }
  9106. if( sd1->pvp_point > sd2->pvp_point )
  9107. sd2->pvp_rank++;
  9108. return 0;
  9109. }
  9110. /*==========================================
  9111. * Calculate new rank beetween all present players (map_foreachinallarea)
  9112. * and display result
  9113. *------------------------------------------*/
  9114. int pc_calc_pvprank(struct map_session_data *sd)
  9115. {
  9116. int old = sd->pvp_rank;
  9117. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9118. sd->pvp_rank=1;
  9119. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9120. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9121. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9122. return sd->pvp_rank;
  9123. }
  9124. /*==========================================
  9125. * Calculate next sd ranking calculation from config
  9126. *------------------------------------------*/
  9127. TIMER_FUNC(pc_calc_pvprank_timer){
  9128. struct map_session_data *sd;
  9129. sd=map_id2sd(id);
  9130. if(sd==NULL)
  9131. return 0;
  9132. sd->pvp_timer = INVALID_TIMER;
  9133. if( pc_isinvisible(sd) )
  9134. {// do not calculate the pvp rank for a hidden GM
  9135. return 0;
  9136. }
  9137. if( pc_calc_pvprank(sd) > 0 )
  9138. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9139. return 0;
  9140. }
  9141. /*==========================================
  9142. * Checking if sd is married
  9143. * Return:
  9144. * partner_id = yes
  9145. * 0 = no
  9146. *------------------------------------------*/
  9147. int pc_ismarried(struct map_session_data *sd)
  9148. {
  9149. if(sd == NULL)
  9150. return -1;
  9151. if(sd->status.partner_id > 0)
  9152. return sd->status.partner_id;
  9153. else
  9154. return 0;
  9155. }
  9156. /*==========================================
  9157. * Marry player sd to player dstsd
  9158. * Return:
  9159. * false = fail
  9160. * true = success
  9161. *------------------------------------------*/
  9162. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9163. {
  9164. if(sd == NULL || dstsd == NULL ||
  9165. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9166. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9167. return false;
  9168. sd->status.partner_id = dstsd->status.char_id;
  9169. dstsd->status.partner_id = sd->status.char_id;
  9170. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9171. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9172. return true;
  9173. }
  9174. /*==========================================
  9175. * Divorce sd from its partner
  9176. * Return:
  9177. * false = fail
  9178. * true = success
  9179. *------------------------------------------*/
  9180. bool pc_divorce(struct map_session_data *sd)
  9181. {
  9182. struct map_session_data *p_sd;
  9183. int i;
  9184. if( sd == NULL || !pc_ismarried(sd) )
  9185. return false;
  9186. if( !sd->status.partner_id )
  9187. return false; // Char is not married
  9188. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9189. { // Lets char server do the divorce
  9190. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9191. return false; // No char server connected
  9192. return true;
  9193. }
  9194. // Both players online, lets do the divorce manually
  9195. sd->status.partner_id = 0;
  9196. p_sd->status.partner_id = 0;
  9197. for( i = 0; i < MAX_INVENTORY; i++ )
  9198. {
  9199. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9200. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9201. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9202. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9203. }
  9204. clif_divorced(sd, p_sd->status.name);
  9205. clif_divorced(p_sd, sd->status.name);
  9206. return true;
  9207. }
  9208. /**
  9209. * Get the partner map_session_data of a player
  9210. * @param sd : the husband|wife session
  9211. * @return partner session or NULL
  9212. */
  9213. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9214. if (!sd || !pc_ismarried(sd))
  9215. return NULL;
  9216. return map_charid2sd(sd->status.partner_id);
  9217. }
  9218. /**
  9219. * Get the father map_session_data of a player
  9220. * @param sd : the baby session
  9221. * @return father session or NULL
  9222. */
  9223. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9224. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9225. return NULL;
  9226. return map_charid2sd(sd->status.father);
  9227. }
  9228. /**
  9229. * Get the mother map_session_data of a player
  9230. * @param sd : the baby session
  9231. * @return mother session or NULL
  9232. */
  9233. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9234. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9235. return NULL;
  9236. return map_charid2sd(sd->status.mother);
  9237. }
  9238. /*==========================================
  9239. * Get sd children charid. (Need to be married)
  9240. *------------------------------------------*/
  9241. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9242. {
  9243. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9244. // charid2sd returns NULL if not found
  9245. return NULL;
  9246. return map_charid2sd(sd->status.child);
  9247. }
  9248. /*==========================================
  9249. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9250. *------------------------------------------*/
  9251. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9252. {
  9253. int hp = 0, sp = 0;
  9254. if( pc_isdead(sd) )
  9255. return;
  9256. if (sd->hp_loss.value) {
  9257. sd->hp_loss.tick += diff_tick;
  9258. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9259. hp += sd->hp_loss.value;
  9260. sd->hp_loss.tick -= sd->hp_loss.rate;
  9261. }
  9262. if(hp >= sd->battle_status.hp)
  9263. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9264. }
  9265. if (sd->sp_loss.value) {
  9266. sd->sp_loss.tick += diff_tick;
  9267. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9268. sp += sd->sp_loss.value;
  9269. sd->sp_loss.tick -= sd->sp_loss.rate;
  9270. }
  9271. }
  9272. if (hp > 0 || sp > 0)
  9273. status_zap(&sd->bl, hp, sp);
  9274. }
  9275. //Character regen. Flag is used to know which types of regen can take place.
  9276. //&1: HP regen
  9277. //&2: SP regen
  9278. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9279. {
  9280. int hp = 0, sp = 0;
  9281. if (sd->hp_regen.value) {
  9282. sd->hp_regen.tick += diff_tick;
  9283. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9284. hp += sd->hp_regen.value;
  9285. sd->hp_regen.tick -= sd->hp_regen.rate;
  9286. }
  9287. }
  9288. if (sd->sp_regen.value) {
  9289. sd->sp_regen.tick += diff_tick;
  9290. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9291. sp += sd->sp_regen.value;
  9292. sd->sp_regen.tick -= sd->sp_regen.rate;
  9293. }
  9294. }
  9295. if (sd->percent_hp_regen.value) {
  9296. sd->percent_hp_regen.tick += diff_tick;
  9297. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9298. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9299. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9300. }
  9301. }
  9302. if (sd->percent_sp_regen.value) {
  9303. sd->percent_sp_regen.tick += diff_tick;
  9304. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9305. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9306. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9307. }
  9308. }
  9309. if (hp > 0 || sp > 0)
  9310. status_heal(&sd->bl, hp, sp, 0);
  9311. }
  9312. /*==========================================
  9313. * Memo player sd savepoint. (map,x,y)
  9314. *------------------------------------------*/
  9315. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9316. {
  9317. nullpo_retv(sd);
  9318. sd->status.save_point.map = mapindex;
  9319. sd->status.save_point.x = x;
  9320. sd->status.save_point.y = y;
  9321. }
  9322. /*==========================================
  9323. * Save 1 player data at autosave interval
  9324. *------------------------------------------*/
  9325. static TIMER_FUNC(pc_autosave){
  9326. int interval;
  9327. struct s_mapiterator* iter;
  9328. struct map_session_data* sd;
  9329. static int last_save_id = 0, save_flag = 0;
  9330. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9331. save_flag = 0;
  9332. else
  9333. save_flag = 1; //Noone was saved, so save first found char.
  9334. iter = mapit_getallusers();
  9335. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9336. {
  9337. if(sd->bl.id == last_save_id && save_flag != 1) {
  9338. save_flag = 1;
  9339. continue;
  9340. }
  9341. if(save_flag != 1) //Not our turn to save yet.
  9342. continue;
  9343. //Save char.
  9344. last_save_id = sd->bl.id;
  9345. save_flag = 2;
  9346. if (pc_isvip(sd)) // Check if we're still VIP
  9347. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9348. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9349. break;
  9350. }
  9351. mapit_free(iter);
  9352. interval = autosave_interval/(map_usercount()+1);
  9353. if(interval < minsave_interval)
  9354. interval = minsave_interval;
  9355. add_timer(gettick()+interval,pc_autosave,0,0);
  9356. return 0;
  9357. }
  9358. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9359. {
  9360. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9361. { //Night/day state does not match.
  9362. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9363. sd->state.night = night_flag;
  9364. return 1;
  9365. }
  9366. return 0;
  9367. }
  9368. /*================================================
  9369. * timer to do the day [Yor]
  9370. * data: 0 = called by timer, 1 = gmcommand/script
  9371. *------------------------------------------------*/
  9372. TIMER_FUNC(map_day_timer){
  9373. char tmp_soutput[1024];
  9374. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9375. return 0;
  9376. if (!night_flag)
  9377. return 0; //Already day.
  9378. night_flag = 0; // 0=day, 1=night [Yor]
  9379. map_foreachpc(pc_daynight_timer_sub);
  9380. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9381. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9382. return 0;
  9383. }
  9384. /*================================================
  9385. * timer to do the night [Yor]
  9386. * data: 0 = called by timer, 1 = gmcommand/script
  9387. *------------------------------------------------*/
  9388. TIMER_FUNC(map_night_timer){
  9389. char tmp_soutput[1024];
  9390. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9391. return 0;
  9392. if (night_flag)
  9393. return 0; //Already nigth.
  9394. night_flag = 1; // 0=day, 1=night [Yor]
  9395. map_foreachpc(pc_daynight_timer_sub);
  9396. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9397. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9398. return 0;
  9399. }
  9400. /**
  9401. * Attempt to stand up a player
  9402. * @param sd
  9403. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9404. * @return True if success, Fals if failed
  9405. */
  9406. bool pc_setstand(struct map_session_data *sd, bool force){
  9407. nullpo_ret(sd);
  9408. // Cannot stand yet
  9409. // TODO: Move to SCS_NOSTAND [Cydh]
  9410. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9411. return false;
  9412. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9413. clif_status_load(&sd->bl,EFST_SIT,0);
  9414. clif_standing(&sd->bl); //Inform area PC is standing
  9415. //Reset sitting tick.
  9416. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9417. if( pc_isdead( sd ) ){
  9418. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9419. clif_party_dead( sd );
  9420. }else{
  9421. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9422. }
  9423. return true;
  9424. }
  9425. /**
  9426. * Calculate Overheat value
  9427. * @param sd: Player data
  9428. * @param heat: Amount of Heat to adjust
  9429. **/
  9430. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9431. struct status_change_entry *sce = NULL;
  9432. int16 limit[] = { 150, 200, 280, 360, 450 };
  9433. uint16 skill_lv;
  9434. nullpo_retv(sd);
  9435. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9436. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9437. sce->val1 += heat;
  9438. sce->val1 = cap_value(sce->val1, 0, 1000);
  9439. if (sd->sc.data[SC_OVERHEAT])
  9440. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9441. if (sce->val1 > limit[skill_lv])
  9442. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9443. } else if (heat > 0)
  9444. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9445. }
  9446. /**
  9447. * Check if player is autolooting given itemID.
  9448. */
  9449. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9450. {
  9451. uint8 i = 0;
  9452. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9453. return true;
  9454. if (!sd->state.autolooting)
  9455. return false;
  9456. if (sd->state.autolooting)
  9457. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9458. return (i != AUTOLOOTITEM_SIZE);
  9459. }
  9460. /**
  9461. * Checks if player can use @/#command
  9462. * @param sd Player map session data
  9463. * @param command Command name without @/# and params
  9464. * @param type is it atcommand or charcommand
  9465. */
  9466. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9467. {
  9468. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9469. }
  9470. /**
  9471. * Checks if commands used by a player should be logged
  9472. * according to their group setting.
  9473. * @param sd Player map session data
  9474. */
  9475. bool pc_should_log_commands(struct map_session_data *sd)
  9476. {
  9477. return pc_group_should_log_commands(pc_get_group_id(sd));
  9478. }
  9479. /**
  9480. * Spirit Charm expiration timer.
  9481. * @see TimerFunc
  9482. */
  9483. static TIMER_FUNC(pc_spiritcharm_timer){
  9484. struct map_session_data *sd;
  9485. int i;
  9486. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9487. return 1;
  9488. if (sd->spiritcharm <= 0) {
  9489. 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);
  9490. sd->spiritcharm = 0;
  9491. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9492. return 0;
  9493. }
  9494. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9495. if (i == sd->spiritcharm) {
  9496. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9497. return 0;
  9498. }
  9499. sd->spiritcharm--;
  9500. if (i != sd->spiritcharm)
  9501. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9502. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9503. if (sd->spiritcharm <= 0)
  9504. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9505. clif_spiritcharm(sd);
  9506. return 0;
  9507. }
  9508. /**
  9509. * Adds a spirit charm.
  9510. * @param sd: Target character
  9511. * @param interval: Duration
  9512. * @param max: Maximum amount of charms to add
  9513. * @param type: Charm type (@see spirit_charm_types)
  9514. */
  9515. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9516. {
  9517. int tid, i;
  9518. nullpo_retv(sd);
  9519. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9520. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9521. if (max > MAX_SPIRITCHARM)
  9522. max = MAX_SPIRITCHARM;
  9523. if (sd->spiritcharm < 0)
  9524. sd->spiritcharm = 0;
  9525. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9526. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9527. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9528. sd->spiritcharm--;
  9529. if (sd->spiritcharm != 0)
  9530. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9531. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9532. }
  9533. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9534. 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);
  9535. if (i != sd->spiritcharm)
  9536. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9537. sd->spiritcharm_timer[i] = tid;
  9538. sd->spiritcharm++;
  9539. sd->spiritcharm_type = type;
  9540. clif_spiritcharm(sd);
  9541. }
  9542. /**
  9543. * Removes one or more spirit charms.
  9544. * @param sd: The target character
  9545. * @param count: Amount of charms to remove
  9546. * @param type: Type of charm to remove
  9547. */
  9548. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9549. {
  9550. int i;
  9551. nullpo_retv(sd);
  9552. if (sd->spiritcharm_type != type)
  9553. return;
  9554. if (sd->spiritcharm <= 0) {
  9555. sd->spiritcharm = 0;
  9556. return;
  9557. }
  9558. if (count <= 0)
  9559. return;
  9560. if (count > sd->spiritcharm)
  9561. count = sd->spiritcharm;
  9562. sd->spiritcharm -= count;
  9563. if (count > MAX_SPIRITCHARM)
  9564. count = MAX_SPIRITCHARM;
  9565. for (i = 0; i < count; i++) {
  9566. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9567. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9568. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9569. }
  9570. }
  9571. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9572. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9573. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9574. }
  9575. if (sd->spiritcharm <= 0)
  9576. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9577. clif_spiritcharm(sd);
  9578. }
  9579. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9580. /**
  9581. * Renewal EXP/Item Drop rate modifier based on level penalty
  9582. * @param level_diff: Monster and Player level difference
  9583. * @param mob_class: Monster class
  9584. * @param mode: Monster mode
  9585. * @param type: 1 - EXP, 2 - Item Drop
  9586. * @return Penalty rate
  9587. */
  9588. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9589. {
  9590. int rate = 100;
  9591. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9592. return rate;
  9593. if (level_diff < 0)
  9594. level_diff = MAX_LEVEL + (~level_diff + 1);
  9595. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9596. return rate;
  9597. return 100; // Penalty not found, return default
  9598. }
  9599. #endif
  9600. int pc_split_str(char *str,char **val,int num)
  9601. {
  9602. int i;
  9603. for (i=0; i<num && str; i++){
  9604. val[i] = str;
  9605. str = strchr(str,',');
  9606. if (str && i<num-1) //Do not remove a trailing comma.
  9607. *str++=0;
  9608. }
  9609. return i;
  9610. }
  9611. int pc_split_atoi(char* str, int* val, char sep, int max)
  9612. {
  9613. int i,j;
  9614. for (i=0; i<max; i++) {
  9615. if (!str) break;
  9616. val[i] = atoi(str);
  9617. str = strchr(str,sep);
  9618. if (str)
  9619. *str++=0;
  9620. }
  9621. //Zero up the remaining.
  9622. for(j=i; j < max; j++)
  9623. val[j] = 0;
  9624. return i;
  9625. }
  9626. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9627. {
  9628. static int warning=0;
  9629. int i,j;
  9630. for (i=0; i<max; i++) {
  9631. double f;
  9632. if (!str) break;
  9633. f = atof(str);
  9634. if (f < 0)
  9635. val[i] = 0;
  9636. else if (f > UINT_MAX) {
  9637. val[i] = UINT_MAX;
  9638. if (!warning) {
  9639. warning = 1;
  9640. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9641. }
  9642. } else
  9643. val[i] = (unsigned int)f;
  9644. str = strchr(str,sep);
  9645. if (str)
  9646. *str++=0;
  9647. }
  9648. //Zero up the remaining.
  9649. for(j=i; j < max; j++)
  9650. val[j] = 0;
  9651. return i;
  9652. }
  9653. /*==========================================
  9654. * sub DB reading.
  9655. * Function used to read skill_tree.txt
  9656. *------------------------------------------*/
  9657. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9658. {
  9659. uint32 baselv, joblv, baselv_max, joblv_max;
  9660. uint16 skill_id, skill_lv, skill_lv_max;
  9661. int idx, class_;
  9662. unsigned int i, offset, skill_idx;
  9663. class_ = atoi(fields[0]);
  9664. skill_id = (uint16)atoi(fields[1]);
  9665. skill_lv = (uint16)atoi(fields[2]);
  9666. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9667. baselv = (uint32)atoi(fields[3]);
  9668. joblv = (uint32)atoi(fields[4]);
  9669. offset = 5;
  9670. }
  9671. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9672. baselv = joblv = 0;
  9673. offset = 3;
  9674. }
  9675. else {
  9676. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9677. return false;
  9678. }
  9679. if(!pcdb_checkid(class_))
  9680. {
  9681. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9682. return false;
  9683. }
  9684. idx = pc_class2idx(class_);
  9685. if (!skill_get_index(skill_id)) {
  9686. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9687. return false;
  9688. }
  9689. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9690. 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);
  9691. skill_lv = skill_lv_max;
  9692. }
  9693. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9694. 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);
  9695. baselv = baselv_max;
  9696. }
  9697. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9698. 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);
  9699. joblv = joblv_max;
  9700. }
  9701. //This is to avoid adding two lines for the same skill. [Skotlex]
  9702. 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 );
  9703. if( skill_idx == MAX_SKILL_TREE )
  9704. {
  9705. 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_);
  9706. return false;
  9707. }
  9708. else if(skill_tree[idx][skill_idx].skill_id)
  9709. {
  9710. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9711. }
  9712. skill_tree[idx][skill_idx].skill_id = skill_id;
  9713. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9714. skill_tree[idx][skill_idx].baselv = baselv;
  9715. skill_tree[idx][skill_idx].joblv = joblv;
  9716. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9717. {
  9718. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9719. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9720. if (skill_id == 0) {
  9721. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9722. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9723. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9724. }
  9725. continue;
  9726. }
  9727. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9728. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9729. return false;
  9730. }
  9731. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9732. 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);
  9733. skill_lv = skill_lv_max;
  9734. }
  9735. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9736. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9737. }
  9738. return true;
  9739. }
  9740. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9741. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9742. {
  9743. int type, class_, diff;
  9744. type = atoi(fields[0]); //1=experience, 2=item drop
  9745. class_ = atoi(fields[1]);
  9746. diff = atoi(fields[2]);
  9747. if( type != 1 && type != 2 ){
  9748. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9749. return false;
  9750. }
  9751. if( !CHK_CLASS(class_) ){
  9752. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9753. return false;
  9754. }
  9755. diff = min(diff, MAX_LEVEL);
  9756. if( diff < 0 )
  9757. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9758. level_penalty[type][class_][diff] = atoi(fields[3]);
  9759. return true;
  9760. }
  9761. #endif
  9762. /** [Cydh]
  9763. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9764. * @param level Base level of player
  9765. * @param class_ Job ID @see enum e_job
  9766. * @return base_hp
  9767. */
  9768. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9769. double base_hp;
  9770. uint16 i, idx = pc_class2idx(class_);
  9771. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9772. #ifndef RENEWAL
  9773. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9774. #endif
  9775. for (i = 2; i <= level; i++)
  9776. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9777. if (class_ == JOB_SUMMONER)
  9778. base_hp += floor((base_hp / 2) + 0.5);
  9779. return (unsigned int)base_hp;
  9780. }
  9781. /** [Playtester]
  9782. * Calculates base sp of player.
  9783. * @param level Base level of player
  9784. * @param class_ Job ID @see enum e_job
  9785. * @return base_sp
  9786. */
  9787. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9788. double base_sp;
  9789. uint16 idx = pc_class2idx(class_);
  9790. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9791. switch (class_) {
  9792. case JOB_NINJA:
  9793. if (level >= 10)
  9794. base_sp -= 22;
  9795. else
  9796. base_sp = 11 + 3*level;
  9797. break;
  9798. case JOB_GUNSLINGER:
  9799. if (level > 10)
  9800. base_sp -= 18;
  9801. else
  9802. base_sp = 9 + 3*level;
  9803. break;
  9804. case JOB_SUMMONER:
  9805. base_sp -= floor(base_sp / 2);
  9806. break;
  9807. }
  9808. return (unsigned int)base_sp;
  9809. }
  9810. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9811. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9812. int idx, class_;
  9813. unsigned int i;
  9814. class_ = atoi(fields[0]);
  9815. if (!pcdb_checkid(class_)) {
  9816. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9817. return false;
  9818. }
  9819. idx = pc_class2idx(class_);
  9820. job_info[idx].max_weight_base = atoi(fields[1]);
  9821. job_info[idx].hp_factor = atoi(fields[2]);
  9822. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9823. job_info[idx].sp_factor = atoi(fields[4]);
  9824. #ifdef RENEWAL_ASPD
  9825. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9826. #else
  9827. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9828. #endif
  9829. {
  9830. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9831. }
  9832. return true;
  9833. }
  9834. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9835. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9836. {
  9837. int idx, class_, i;
  9838. class_ = atoi(fields[0]);
  9839. if(!pcdb_checkid(class_))
  9840. {
  9841. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9842. return false;
  9843. }
  9844. idx = pc_class2idx(class_);
  9845. for(i = 1; i < columns; i++)
  9846. {
  9847. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9848. }
  9849. return true;
  9850. }
  9851. //Reading job_exp.txt line
  9852. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9853. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9854. {
  9855. int idx, i, type;
  9856. int job_id,job_count,jobs[CLASS_COUNT];
  9857. unsigned int ui, maxlvl;
  9858. maxlvl = atoi(fields[0]);
  9859. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9860. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9861. return false;
  9862. }
  9863. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9864. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9865. return false;
  9866. }
  9867. type = atoi(fields[2]);
  9868. if(type < 0 || type > 1){
  9869. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9870. return false;
  9871. }
  9872. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9873. if (job_count < 1)
  9874. return false;
  9875. job_id = jobs[0];
  9876. if(!pcdb_checkid(job_id)){
  9877. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9878. return false;
  9879. }
  9880. idx = pc_class2idx(job_id);
  9881. job_info[idx].max_level[type] = maxlvl;
  9882. for(i=0; i<maxlvl; i++)
  9883. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9884. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9885. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9886. //should look like this:
  9887. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9888. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9889. job_info[idx].max_level[type]--;
  9890. if (job_info[idx].max_level[type] < maxlvl) {
  9891. 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]);
  9892. ShowInfo("Filling the missing values with the last exp entry.\n");
  9893. //Fill the requested values with the last entry.
  9894. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9895. for (; ui+2 < maxlvl; ui++)
  9896. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9897. job_info[idx].max_level[type] = maxlvl;
  9898. }
  9899. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9900. for (i = 1; i < job_count; i++) {
  9901. job_id = jobs[i];
  9902. if (!pcdb_checkid(job_id)) {
  9903. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9904. continue;
  9905. }
  9906. idx = pc_class2idx(job_id);
  9907. 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]));
  9908. job_info[idx].max_level[type] = maxlvl;
  9909. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9910. }
  9911. return true;
  9912. }
  9913. /**
  9914. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9915. * startlvl,endlvl,class,type,values...
  9916. */
  9917. #ifdef HP_SP_TABLES
  9918. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9919. {
  9920. int i, startlvl, endlvl;
  9921. int job_count,jobs[CLASS_COUNT];
  9922. short type;
  9923. startlvl = atoi(fields[0]);
  9924. if(startlvl<1){
  9925. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9926. return false;
  9927. }
  9928. endlvl = atoi(fields[1]);
  9929. if(endlvl<1 || endlvl<startlvl){
  9930. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9931. return false;
  9932. }
  9933. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9934. 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);
  9935. return false;
  9936. }
  9937. type = atoi(fields[3]);
  9938. if(type < 0 || type > 1){
  9939. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9940. return false;
  9941. }
  9942. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9943. if (job_count < 1)
  9944. return false;
  9945. for (i = 0; i < job_count; i++) {
  9946. int idx, job_id = jobs[i], use_endlvl;
  9947. if (!pcdb_checkid(job_id)) {
  9948. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9949. return false;
  9950. }
  9951. idx = pc_class2idx(job_id);
  9952. if (startlvl > job_info[idx].max_level[0]) {
  9953. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9954. return false;
  9955. }
  9956. //Just read until available max level for this job, don't use MAX_LEVEL!
  9957. use_endlvl = endlvl;
  9958. if (use_endlvl > job_info[idx].max_level[0])
  9959. use_endlvl = job_info[idx].max_level[0];
  9960. if(type == 0) { //hp type
  9961. uint16 j;
  9962. for(j = 0; j < use_endlvl; j++) {
  9963. if (atoi(fields[j+4])) {
  9964. uint16 lvl_idx = startlvl-1+j;
  9965. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9966. //Tells if this HP is lower than previous level (but not for 99->100)
  9967. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9968. 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",
  9969. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9970. }
  9971. }
  9972. }
  9973. else { //sp type
  9974. uint16 j;
  9975. for(j = 0; j < use_endlvl; j++) {
  9976. if (atoi(fields[j+4])) {
  9977. uint16 lvl_idx = startlvl-1+j;
  9978. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9979. //Tells if this SP is lower than previous level (but not for 99->100)
  9980. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9981. 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",
  9982. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9983. }
  9984. }
  9985. }
  9986. }
  9987. return true;
  9988. }
  9989. #endif
  9990. /** [Cydh]
  9991. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9992. */
  9993. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9994. {
  9995. int idx, class_;
  9996. uint16 str, agi, vit, int_, dex, luk;
  9997. script_get_constant(trim(fields[0]),&class_);
  9998. if ((idx = pc_class2idx(class_)) < 0) {
  9999. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10000. return false;
  10001. }
  10002. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10003. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10004. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10005. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10006. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10007. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10008. job_info[idx].max_param.str = str;
  10009. job_info[idx].max_param.agi = agi;
  10010. job_info[idx].max_param.vit = vit;
  10011. job_info[idx].max_param.int_ = int_;
  10012. job_info[idx].max_param.dex = dex;
  10013. job_info[idx].max_param.luk = luk;
  10014. return true;
  10015. }
  10016. /**
  10017. * Read job_noenter_map.txt
  10018. **/
  10019. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10020. int idx, class_ = -1;
  10021. if (ISDIGIT(str[0][0])) {
  10022. class_ = atoi(str[0]);
  10023. } else {
  10024. if (!script_get_constant(str[0], &class_)) {
  10025. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10026. return false;
  10027. }
  10028. }
  10029. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10030. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  10031. return false;
  10032. }
  10033. job_info[idx].noenter_map.zone = atoi(str[1]);
  10034. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10035. return true;
  10036. }
  10037. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10038. int i=1;
  10039. char line[24000]; //FIXME this seem too big
  10040. FILE *fp;
  10041. sprintf(line, "%s/statpoint.txt", basedir);
  10042. fp=fopen(line,"r");
  10043. if(fp == NULL){
  10044. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10045. return max(last_s,i);
  10046. } else {
  10047. int entries=0;
  10048. while(fgets(line, sizeof(line), fp))
  10049. {
  10050. int stat;
  10051. trim(line);
  10052. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10053. continue;
  10054. if ((stat=strtoul(line,NULL,10))<0)
  10055. stat=0;
  10056. if (i > MAX_LEVEL)
  10057. break;
  10058. statp[i]=stat;
  10059. i++;
  10060. entries++;
  10061. }
  10062. fclose(fp);
  10063. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10064. }
  10065. return max(last_s,i);
  10066. }
  10067. /*==========================================
  10068. * pc DB reading.
  10069. * job_exp.txt - required experience values
  10070. * skill_tree.txt - skill tree for every class
  10071. * attr_fix.txt - elemental adjustment table
  10072. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10073. * job_db2.txt - job,stats bonuses/lvl
  10074. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10075. *------------------------------------------*/
  10076. void pc_readdb(void) {
  10077. int i, k, s = 1;
  10078. const char* dbsubpath[] = {
  10079. "",
  10080. "/" DBIMPORT,
  10081. //add other path here
  10082. };
  10083. //reset
  10084. memset(job_info,0,sizeof(job_info)); // job_info table
  10085. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10086. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10087. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10088. for( k=1; k < 3; k++ ){ // fill in the blanks
  10089. int j;
  10090. for( j = 0; j < CLASS_ALL; j++ ){
  10091. int tmp = 0;
  10092. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10093. if( i == MAX_LEVEL+1 )
  10094. tmp = level_penalty[k][j][0];// reset
  10095. if( level_penalty[k][j][i] > 0 )
  10096. tmp = level_penalty[k][j][i];
  10097. else
  10098. level_penalty[k][j][i] = tmp;
  10099. }
  10100. }
  10101. }
  10102. #endif
  10103. // reset then read statspoint
  10104. memset(statp,0,sizeof(statp));
  10105. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10106. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10107. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10108. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10109. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10110. if(i==0) {
  10111. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10112. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10113. }
  10114. else {
  10115. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10116. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10117. }
  10118. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10119. if (i == 0)
  10120. #ifdef RENEWAL_ASPD
  10121. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10122. #else
  10123. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10124. #endif
  10125. else
  10126. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
  10127. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10128. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10129. #ifdef HP_SP_TABLES
  10130. 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!
  10131. #endif
  10132. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10133. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10134. aFree(dbsubpath1);
  10135. aFree(dbsubpath2);
  10136. }
  10137. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10138. memset(skill_tree, 0, sizeof(skill_tree));
  10139. 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);
  10140. 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);
  10141. // generate the remaining parts of the db if necessary
  10142. k = battle_config.use_statpoint_table; //save setting
  10143. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10144. statp[0] = 45; // seed value
  10145. for (; s <= MAX_LEVEL; s++)
  10146. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10147. battle_config.use_statpoint_table = k; //restore setting
  10148. //Checking if all class have their data
  10149. for (i = 0; i < JOB_MAX; i++) {
  10150. int idx;
  10151. uint16 j;
  10152. if (!pcdb_checkid(i))
  10153. continue;
  10154. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10155. continue; //Classes that do not need exp tables.
  10156. idx = pc_class2idx(i);
  10157. if (!job_info[idx].max_level[0])
  10158. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10159. if (!job_info[idx].max_level[1])
  10160. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10161. //Init and checking the empty value of Base HP/SP [Cydh]
  10162. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10163. if (job_info[idx].base_hp[j] == 0)
  10164. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10165. if (job_info[idx].base_sp[j] == 0)
  10166. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10167. }
  10168. }
  10169. }
  10170. // Read MOTD on startup. [Valaris]
  10171. int pc_read_motd(void)
  10172. {
  10173. FILE* fp;
  10174. // clear old MOTD
  10175. memset(motd_text, 0, sizeof(motd_text));
  10176. // read current MOTD
  10177. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10178. {
  10179. unsigned int entries = 0;
  10180. char buf[CHAT_SIZE_MAX];
  10181. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10182. {
  10183. unsigned int lines = 0;
  10184. size_t len;
  10185. lines++;
  10186. if( buf[0] == '/' && buf[1] == '/' )
  10187. continue;
  10188. len = strlen(buf);
  10189. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10190. len--;
  10191. if( len ) {
  10192. char * ptr;
  10193. buf[len] = 0;
  10194. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10195. 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);
  10196. }
  10197. else {// empty line
  10198. buf[0] = ' ';
  10199. buf[1] = 0;
  10200. }
  10201. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10202. entries++;
  10203. }
  10204. fclose(fp);
  10205. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10206. }
  10207. else
  10208. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10209. return 0;
  10210. }
  10211. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10212. int i,cursor = 0;
  10213. struct item_cd* cd = NULL;
  10214. if( load ) {
  10215. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10216. // no item cooldown is associated with this character
  10217. return;
  10218. }
  10219. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10220. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10221. sd->item_delay[cursor].tick = cd->tick[i];
  10222. sd->item_delay[cursor].nameid = cd->nameid[i];
  10223. cursor++;
  10224. }
  10225. }
  10226. idb_remove(itemcd_db,sd->status.char_id);
  10227. } else {
  10228. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10229. // create a new skill cooldown object for map storage
  10230. CREATE( cd, struct item_cd, 1 );
  10231. idb_put( itemcd_db, sd->status.char_id, cd );
  10232. }
  10233. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10234. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10235. cd->tick[cursor] = sd->item_delay[i].tick;
  10236. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10237. cursor++;
  10238. }
  10239. }
  10240. }
  10241. return;
  10242. }
  10243. /**
  10244. * Add item delay to player's item delay data
  10245. * @param sd Player
  10246. * @param id Item data
  10247. * @param tick Current tick
  10248. * @param n Item index in inventory
  10249. * @return 0: No delay, can consume item.
  10250. * 1: Has delay, cancel consumption.
  10251. **/
  10252. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10253. int i;
  10254. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10255. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10256. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10257. if( i < MAX_ITEMDELAYS ) {
  10258. if( sd->item_delay[i].nameid ) {// found
  10259. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10260. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10261. char e_msg[CHAT_SIZE_MAX];
  10262. if( e_tick > 99 )
  10263. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10264. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10265. else
  10266. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10267. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10268. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10269. return 1; // Delay has not expired yet
  10270. }
  10271. } else {// not yet used item (all slots are initially empty)
  10272. sd->item_delay[i].nameid = id->nameid;
  10273. }
  10274. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10275. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10276. } else {// should not happen
  10277. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10278. }
  10279. //clean up used delays so we can give room for more
  10280. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10281. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10282. sd->item_delay[i].tick = 0;
  10283. sd->item_delay[i].nameid = 0;
  10284. }
  10285. }
  10286. return 0;
  10287. }
  10288. /**
  10289. * Check if player has delay to reuse item
  10290. * @param sd Player
  10291. * @param id Item data
  10292. * @param tick Current tick
  10293. * @param n Item index in inventory
  10294. * @return 0: No delay, can consume item.
  10295. * 1: Has delay, cancel consumption.
  10296. **/
  10297. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10298. struct status_change *sc = NULL;
  10299. nullpo_retr(0, sd);
  10300. nullpo_retr(0, id);
  10301. // Do normal delay assignment
  10302. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10303. return pc_itemcd_add(sd, id, tick, n);
  10304. // Send reply of delay remains
  10305. if (sc->data[id->delay_sc]) {
  10306. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10307. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10308. return 1;
  10309. }
  10310. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10311. return 0;
  10312. }
  10313. /**
  10314. * Clear the dmglog data from player
  10315. * @param sd
  10316. * @param md
  10317. **/
  10318. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10319. {
  10320. uint8 i;
  10321. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10322. if (i < DAMAGELOG_SIZE) {
  10323. md->dmglog[i].id = 0;
  10324. md->dmglog[i].dmg = 0;
  10325. md->dmglog[i].flag = 0;
  10326. }
  10327. }
  10328. /**
  10329. * Add log to player's dmglog
  10330. * @param sd
  10331. * @param id Monster's GID
  10332. **/
  10333. void pc_damage_log_add(struct map_session_data *sd, int id)
  10334. {
  10335. uint8 i = 0;
  10336. if (!sd || !id)
  10337. return;
  10338. //Only store new data, don't need to renew the old one with same id
  10339. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10340. if (i < DAMAGELOG_SIZE_PC)
  10341. return;
  10342. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10343. if (sd->dmglog[i] == 0) {
  10344. sd->dmglog[i] = id;
  10345. return;
  10346. }
  10347. }
  10348. }
  10349. /**
  10350. * Clear dmglog data from player
  10351. * @param sd
  10352. * @param id Monster's id
  10353. **/
  10354. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10355. {
  10356. uint8 i;
  10357. struct mob_data *md = NULL;
  10358. if (!sd)
  10359. return;
  10360. if (!id) {
  10361. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10362. if( !sd->dmglog[i] ) //skip the empty value
  10363. continue;
  10364. if ((md = map_id2md(sd->dmglog[i])))
  10365. pc_clear_log_damage_sub(sd->status.char_id,md);
  10366. sd->dmglog[i] = 0;
  10367. }
  10368. }
  10369. else {
  10370. if ((md = map_id2md(id)))
  10371. pc_clear_log_damage_sub(sd->status.char_id,md);
  10372. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10373. if (i < DAMAGELOG_SIZE_PC)
  10374. sd->dmglog[i] = 0;
  10375. }
  10376. }
  10377. /**
  10378. * Status change data arrived from char-server
  10379. * @param sd: Player data
  10380. */
  10381. void pc_scdata_received(struct map_session_data *sd) {
  10382. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10383. clif_weight_limit( sd );
  10384. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10385. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10386. }
  10387. sd->state.pc_loaded = true;
  10388. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10389. sd->state.connect_new = 1;
  10390. clif_parse_LoadEndAck(sd->fd, sd);
  10391. }
  10392. if (pc_iscarton(sd)) {
  10393. sd->cart_weight_max = 0; // Force a client refesh
  10394. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10395. }
  10396. }
  10397. /**
  10398. * Check player account expiration time and rental item expirations
  10399. * @param sd: Player data
  10400. */
  10401. void pc_check_expiration(struct map_session_data *sd) {
  10402. #ifndef ENABLE_SC_SAVING
  10403. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10404. #endif
  10405. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10406. time_t exp_time = sd->expiration_time;
  10407. char tmpstr[1024];
  10408. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10409. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10410. pc_expire_check(sd);
  10411. }
  10412. }
  10413. TIMER_FUNC(pc_expiration_timer){
  10414. struct map_session_data *sd = map_id2sd(id);
  10415. if( !sd ) return 0;
  10416. sd->expiration_tid = INVALID_TIMER;
  10417. if( sd->fd )
  10418. clif_authfail_fd(sd->fd,10);
  10419. map_quit(sd);
  10420. return 0;
  10421. }
  10422. TIMER_FUNC(pc_autotrade_timer){
  10423. struct map_session_data *sd = map_id2sd(id);
  10424. if (!sd)
  10425. return 0;
  10426. sd->autotrade_tid = INVALID_TIMER;
  10427. if (sd->state.autotrade&2)
  10428. vending_reopen(sd);
  10429. if (sd->state.autotrade&4)
  10430. buyingstore_reopen(sd);
  10431. if (!sd->vender_id && !sd->buyer_id) {
  10432. sd->state.autotrade = 0;
  10433. map_quit(sd);
  10434. }
  10435. return 0;
  10436. }
  10437. /* this timer exists only when a character with a expire timer > 24h is online */
  10438. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10439. TIMER_FUNC(pc_global_expiration_timer){
  10440. struct s_mapiterator* iter;
  10441. struct map_session_data* sd;
  10442. iter = mapit_getallusers();
  10443. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10444. if( sd->expiration_time )
  10445. pc_expire_check(sd);
  10446. mapit_free(iter);
  10447. return 0;
  10448. }
  10449. void pc_expire_check(struct map_session_data *sd) {
  10450. /* ongoing timer */
  10451. if( sd->expiration_tid != INVALID_TIMER )
  10452. return;
  10453. /* not within the next 24h, enable the global check */
  10454. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10455. /* global check not running, enable */
  10456. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10457. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10458. return;
  10459. }
  10460. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10461. }
  10462. /**
  10463. * Deposit some money to bank
  10464. * @param sd
  10465. * @param money Amount of money to deposit
  10466. **/
  10467. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10468. unsigned int limit_check = money + sd->bank_vault;
  10469. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10470. return BDA_OVERFLOW;
  10471. } else if ( money > sd->status.zeny ) {
  10472. return BDA_NO_MONEY;
  10473. }
  10474. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10475. return BDA_NO_MONEY;
  10476. sd->bank_vault += money;
  10477. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10478. if( save_settings&CHARSAVE_BANK )
  10479. chrif_save(sd, CSAVE_NORMAL);
  10480. return BDA_SUCCESS;
  10481. }
  10482. /**
  10483. * Withdraw money from bank
  10484. * @param sd
  10485. * @param money Amount of money that will be withdrawn
  10486. **/
  10487. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10488. unsigned int limit_check = money + sd->status.zeny;
  10489. if( money <= 0 ) {
  10490. return BWA_UNKNOWN_ERROR;
  10491. } else if ( money > sd->bank_vault ) {
  10492. return BWA_NO_MONEY;
  10493. } else if ( limit_check > MAX_ZENY ) {
  10494. /* no official response for this scenario exists. */
  10495. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10496. return BWA_UNKNOWN_ERROR;
  10497. }
  10498. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10499. return BWA_NO_MONEY;
  10500. sd->bank_vault -= money;
  10501. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10502. if( save_settings&CHARSAVE_BANK )
  10503. chrif_save(sd, CSAVE_NORMAL);
  10504. return BWA_SUCCESS;
  10505. }
  10506. /**
  10507. * Clear Crimson Marker data from caster
  10508. * @param sd: Player
  10509. **/
  10510. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10511. uint8 i;
  10512. if (!sd)
  10513. return;
  10514. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10515. struct block_list *bl = NULL;
  10516. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10517. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10518. sd->c_marker[i] = 0;
  10519. }
  10520. }
  10521. /**
  10522. * Show version to player
  10523. * @param sd: Player
  10524. **/
  10525. void pc_show_version(struct map_session_data *sd) {
  10526. const char* svn = get_svn_revision();
  10527. char buf[CHAT_SIZE_MAX];
  10528. if( svn[0] != UNKNOWN_VERSION )
  10529. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10530. else {
  10531. const char* git = get_git_hash();
  10532. if( git[0] != UNKNOWN_VERSION )
  10533. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10534. else
  10535. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10536. }
  10537. clif_displaymessage(sd->fd,buf);
  10538. }
  10539. /**
  10540. * Run bonus_script on player
  10541. * @param sd
  10542. * @author [Cydh]
  10543. **/
  10544. void pc_bonus_script(struct map_session_data *sd) {
  10545. int now = gettick();
  10546. struct linkdb_node *node = NULL, *next = NULL;
  10547. if (!sd || !(node = sd->bonus_script.head))
  10548. return;
  10549. while (node) {
  10550. struct s_bonus_script_entry *entry = NULL;
  10551. next = node->next;
  10552. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10553. // Only start timer for new bonus_script
  10554. if (entry->tid == INVALID_TIMER) {
  10555. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10556. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10557. entry->tick += now;
  10558. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10559. }
  10560. if (entry->script)
  10561. run_script(entry->script, 0, sd->bl.id, 0);
  10562. else
  10563. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10564. }
  10565. node = next;
  10566. }
  10567. }
  10568. /**
  10569. * Add bonus_script to player
  10570. * @param sd Player
  10571. * @param script_str Script string
  10572. * @param dur Duration in ms
  10573. * @param icon EFST
  10574. * @param flag Flags @see enum e_bonus_script_flags
  10575. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10576. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10577. * @author [Cydh]
  10578. **/
  10579. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum efst_types icon, uint16 flag, uint8 type) {
  10580. struct script_code *script = NULL;
  10581. struct linkdb_node *node = NULL;
  10582. struct s_bonus_script_entry *entry = NULL;
  10583. if (!sd)
  10584. return NULL;
  10585. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10586. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10587. return NULL;
  10588. }
  10589. // Duplication checks
  10590. if ((node = sd->bonus_script.head)) {
  10591. while (node) {
  10592. entry = (struct s_bonus_script_entry *)node->data;
  10593. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10594. int newdur = gettick() + dur;
  10595. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10596. settick_timer(entry->tid, newdur);
  10597. script_free_code(script);
  10598. return NULL;
  10599. }
  10600. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10601. break;
  10602. else { // No duplicate bonus
  10603. script_free_code(script);
  10604. return NULL;
  10605. }
  10606. }
  10607. node = node->next;
  10608. }
  10609. }
  10610. CREATE(entry, struct s_bonus_script_entry, 1);
  10611. entry->script_buf = StringBuf_Malloc();
  10612. StringBuf_AppendStr(entry->script_buf, script_str);
  10613. entry->tid = INVALID_TIMER;
  10614. entry->flag = flag;
  10615. entry->icon = icon;
  10616. entry->tick = dur; // Use duration first, on run change to expire time
  10617. entry->type = type;
  10618. entry->script = script;
  10619. sd->bonus_script.count++;
  10620. return entry;
  10621. }
  10622. /**
  10623. * Remove bonus_script data from player
  10624. * @param sd: Target player
  10625. * @param list: Bonus script entry from player
  10626. * @author [Cydh]
  10627. **/
  10628. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10629. if (entry->tid != INVALID_TIMER)
  10630. delete_timer(entry->tid, pc_bonus_script_timer);
  10631. if (entry->script)
  10632. script_free_code(entry->script);
  10633. if (entry->script_buf)
  10634. StringBuf_Free(entry->script_buf);
  10635. if (sd) {
  10636. if (entry->icon != EFST_BLANK)
  10637. clif_status_load(&sd->bl, entry->icon, 0);
  10638. if (sd->bonus_script.count > 0)
  10639. sd->bonus_script.count--;
  10640. }
  10641. aFree(entry);
  10642. }
  10643. /**
  10644. * Do final process if no entry left
  10645. * @param sd
  10646. **/
  10647. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10648. if (sd->bonus_script.count == 0) {
  10649. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10650. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10651. linkdb_final(&sd->bonus_script.head);
  10652. }
  10653. }
  10654. /**
  10655. * Timer for bonus_script
  10656. * @param tid
  10657. * @param tick
  10658. * @param id
  10659. * @param data
  10660. * @author [Cydh]
  10661. **/
  10662. TIMER_FUNC(pc_bonus_script_timer){
  10663. struct map_session_data *sd;
  10664. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10665. sd = map_id2sd(id);
  10666. if (!sd) {
  10667. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10668. return 0;
  10669. }
  10670. if (tid == INVALID_TIMER)
  10671. return 0;
  10672. if (!sd->bonus_script.head || entry == NULL) {
  10673. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10674. return 0;
  10675. }
  10676. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10677. pc_bonus_script_free_entry(sd, entry);
  10678. pc_bonus_script_check_final(sd);
  10679. status_calc_pc(sd,SCO_NONE);
  10680. return 0;
  10681. }
  10682. /**
  10683. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10684. * @param sd: Target player
  10685. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10686. * @author [Cydh]
  10687. **/
  10688. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10689. struct linkdb_node *node = NULL;
  10690. uint16 count = 0;
  10691. if (!sd || !(node = sd->bonus_script.head))
  10692. return;
  10693. while (node) {
  10694. struct linkdb_node *next = node->next;
  10695. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10696. if (entry && (
  10697. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10698. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10699. (flag&entry->flag) || // Matched flag
  10700. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10701. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10702. )
  10703. )
  10704. {
  10705. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10706. pc_bonus_script_free_entry(sd, entry);
  10707. count++;
  10708. }
  10709. node = next;
  10710. }
  10711. pc_bonus_script_check_final(sd);
  10712. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10713. status_calc_pc(sd,SCO_NONE);
  10714. }
  10715. /** [Cydh]
  10716. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10717. * @param sd: Target player
  10718. */
  10719. void pc_cell_basilica(struct map_session_data *sd) {
  10720. nullpo_retv(sd);
  10721. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10722. if (sd->sc.data[SC_BASILICA])
  10723. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10724. }
  10725. else if (!sd->sc.data[SC_BASILICA])
  10726. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10727. }
  10728. /** [Cydh]
  10729. * Get maximum specified parameter for specified class
  10730. * @param class_: sd->class
  10731. * @param sex: sd->status.sex
  10732. * @param flag: parameter will be checked
  10733. * @return max_param
  10734. */
  10735. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10736. int idx = -1, class_ = sd->class_;
  10737. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10738. short max_param = 0;
  10739. switch (param) {
  10740. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10741. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10742. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10743. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10744. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10745. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10746. }
  10747. if (max_param > 0)
  10748. return max_param;
  10749. }
  10750. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10751. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10752. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  10753. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10754. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10755. }
  10756. /**
  10757. * Get max ASPD for player based on Class
  10758. * @param sd Player
  10759. * @return ASPD
  10760. */
  10761. short pc_maxaspd(struct map_session_data *sd) {
  10762. nullpo_ret(sd);
  10763. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10764. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10765. battle_config.max_aspd ));
  10766. }
  10767. /**
  10768. * Calculates total item-group related bonuses for the given item
  10769. * @param sd Player
  10770. * @param nameid Item ID
  10771. * @return Heal rate
  10772. **/
  10773. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10774. short bonus = 0;
  10775. uint8 i;
  10776. if (!sd->itemgrouphealrate_count)
  10777. return bonus;
  10778. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10779. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10780. struct s_item_group_db *group = NULL;
  10781. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10782. continue;
  10783. for (j = 0; j < group->random[0].data_qty; j++) {
  10784. if (group->random[0].data[j].nameid == nameid) {
  10785. bonus += sd->itemgrouphealrate[i]->rate;
  10786. break;
  10787. }
  10788. }
  10789. }
  10790. return bonus;
  10791. }
  10792. /**
  10793. * Calculates total item-group related bonuses for the given item group
  10794. * @param sd Player
  10795. * @param group_id Item Group ID
  10796. * @return Heal rate
  10797. **/
  10798. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10799. short bonus = 0;
  10800. uint8 i;
  10801. if (!sd->itemgrouphealrate_count)
  10802. return bonus;
  10803. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10804. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10805. return sd->itemgrouphealrate[i]->rate;
  10806. }
  10807. return bonus;
  10808. }
  10809. /**
  10810. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10811. * @param eqi Item EQI of enum equip_index
  10812. * @param *equip_index Player's equip_index[]
  10813. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10814. * @return True if item in same inventory index, False if doesn't
  10815. */
  10816. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10817. if (index < 0 || index >= MAX_INVENTORY)
  10818. return true;
  10819. // Dual weapon checks
  10820. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10821. return true;
  10822. // Headgear with Mid & Low location
  10823. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10824. return true;
  10825. // Headgear with Top & Mid or Low location
  10826. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10827. return true;
  10828. // Headgear with Mid & Low location
  10829. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10830. return true;
  10831. // Headgear with Top & Mid or Low location
  10832. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10833. return true;
  10834. return false;
  10835. }
  10836. /**
  10837. * Generate Unique item ID for player
  10838. * @param sd : Player
  10839. * @return A generated Unique item ID
  10840. */
  10841. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10842. nullpo_ret(sd);
  10843. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10844. }
  10845. /**
  10846. * Validating skill from player after logged on
  10847. * @param sd
  10848. **/
  10849. void pc_validate_skill(struct map_session_data *sd) {
  10850. if (sd) {
  10851. uint16 i = 0, count = 0;
  10852. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10853. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10854. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10855. for (i = 0; i < MAX_SKILL; i++) {
  10856. uint16 idx = 0;
  10857. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10858. continue;
  10859. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10860. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10861. count++;
  10862. }
  10863. else
  10864. 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);
  10865. }
  10866. }
  10867. }
  10868. /**
  10869. * Toggle to remember if the questinfo is displayed yet or not.
  10870. * @param qi_display Display flag
  10871. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10872. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10873. **/
  10874. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10875. if (show) {
  10876. // Check if need to be displayed
  10877. if ((*qi_display) != 1) {
  10878. (*qi_display) = 1;
  10879. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10880. }
  10881. }
  10882. else {
  10883. // Check if need to be hide
  10884. if ((*qi_display) != 0) {
  10885. (*qi_display) = 0;
  10886. #if PACKETVER >= 20120410
  10887. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10888. #else
  10889. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10890. #endif
  10891. }
  10892. }
  10893. }
  10894. /**
  10895. * Show available NPC Quest / Event Icon Check [Kisuka]
  10896. * @param sd Player
  10897. **/
  10898. void pc_show_questinfo(struct map_session_data *sd) {
  10899. #if PACKETVER >= 20090218
  10900. struct questinfo *qi = NULL;
  10901. unsigned short i;
  10902. uint8 j;
  10903. int8 mystate = 0;
  10904. bool failed = false;
  10905. nullpo_retv(sd);
  10906. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10907. return;
  10908. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10909. if (!mapdata->qi_count || !mapdata->qi_data)
  10910. return;
  10911. if (mapdata->qi_count != sd->qi_count)
  10912. return; // init was not called yet
  10913. for(i = 0; i < mapdata->qi_count; i++) {
  10914. qi = &mapdata->qi_data[i];
  10915. if (!qi)
  10916. continue;
  10917. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10918. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10919. continue;
  10920. }
  10921. // Level range checks
  10922. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10923. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10924. continue;
  10925. }
  10926. // Quest requirements
  10927. if (qi->req_count) {
  10928. failed = false;
  10929. for (j = 0; j < qi->req_count; j++) {
  10930. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10931. mystate = mystate + (mystate < 1);
  10932. if (mystate != qi->req[j].state) {
  10933. failed = true;
  10934. break;
  10935. }
  10936. }
  10937. if (failed) {
  10938. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10939. continue;
  10940. }
  10941. }
  10942. // Job requirements
  10943. if (qi->jobid_count) {
  10944. failed = true;
  10945. for (j = 0; j < qi->jobid_count; j++) {
  10946. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10947. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10948. failed = false;
  10949. break;
  10950. }
  10951. }
  10952. if (!failed)
  10953. continue;
  10954. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10955. }
  10956. else {
  10957. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10958. }
  10959. }
  10960. #endif
  10961. }
  10962. /**
  10963. * Reinit the questinfo for player when changing map
  10964. * @param sd Player
  10965. **/
  10966. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10967. #if PACKETVER >= 20090218
  10968. nullpo_retv(sd);
  10969. if (sd->qi_display) {
  10970. aFree(sd->qi_display);
  10971. sd->qi_display = NULL;
  10972. }
  10973. sd->qi_count = 0;
  10974. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10975. return;
  10976. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10977. if (!mapdata->qi_count || !mapdata->qi_data)
  10978. return;
  10979. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
  10980. #endif
  10981. }
  10982. /**
  10983. * Check if a job is allowed to enter the map
  10984. * @param jobid Job ID see enum e_job or sd->status.class_
  10985. * @param m ID -an index- for direct indexing map[] array
  10986. * @return 1 if job is allowed, 0 otherwise
  10987. **/
  10988. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10989. uint16 idx = 0;
  10990. // Map is other map server.
  10991. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10992. if (m < 0)
  10993. return true;
  10994. struct map_data *mapdata = map_getmapdata(m);
  10995. if (!mapdata->cell)
  10996. return false;
  10997. if (!pcdb_checkid(jobid))
  10998. return false;
  10999. idx = pc_class2idx(jobid);
  11000. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11001. return true;
  11002. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11003. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11004. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11005. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11006. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11007. (job_info[idx].noenter_map.zone&(8*mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11008. )
  11009. return false;
  11010. return true;
  11011. }
  11012. /**
  11013. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11014. * @param sd
  11015. **/
  11016. void pc_set_costume_view(struct map_session_data *sd) {
  11017. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11018. struct item_data *id = NULL;
  11019. nullpo_retv(sd);
  11020. head_low = sd->status.head_bottom;
  11021. head_mid = sd->status.head_mid;
  11022. head_top = sd->status.head_top;
  11023. robe = sd->status.robe;
  11024. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11025. //Added check to prevent sending the same look on multiple slots ->
  11026. //causes client to redraw item on top of itself. (suggested by Lupus)
  11027. // Normal headgear checks
  11028. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11029. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11030. sd->status.head_bottom = id->look;
  11031. else
  11032. sd->status.head_bottom = 0;
  11033. }
  11034. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11035. if (!(id->equip&(EQP_HEAD_TOP)))
  11036. sd->status.head_mid = id->look;
  11037. else
  11038. sd->status.head_mid = 0;
  11039. }
  11040. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11041. sd->status.head_top = id->look;
  11042. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11043. sd->status.robe = id->look;
  11044. // Costumes check
  11045. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11046. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11047. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11048. sd->status.head_bottom = id->look;
  11049. else
  11050. sd->status.head_bottom = 0;
  11051. }
  11052. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11053. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11054. sd->status.head_mid = id->look;
  11055. else
  11056. sd->status.head_mid = 0;
  11057. }
  11058. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11059. sd->status.head_top = id->look;
  11060. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11061. sd->status.robe = id->look;
  11062. }
  11063. if (sd->setlook_head_bottom)
  11064. sd->status.head_bottom = sd->setlook_head_bottom;
  11065. if (sd->setlook_head_mid)
  11066. sd->status.head_mid = sd->setlook_head_mid;
  11067. if (sd->setlook_head_top)
  11068. sd->status.head_top = sd->setlook_head_top;
  11069. if (sd->setlook_robe)
  11070. sd->status.robe = sd->setlook_robe;
  11071. if (head_low != sd->status.head_bottom)
  11072. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11073. if (head_mid != sd->status.head_mid)
  11074. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11075. if (head_top != sd->status.head_top)
  11076. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11077. if (robe != sd->status.robe)
  11078. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11079. }
  11080. struct s_attendance_period* pc_attendance_period(){
  11081. uint32 date = date_get(DT_YYYYMMDD);
  11082. for( struct s_attendance_period& period : attendance_periods ){
  11083. if( period.start <= date && period.end >= date ){
  11084. return &period;
  11085. }
  11086. }
  11087. return nullptr;
  11088. }
  11089. bool pc_attendance_enabled(){
  11090. // Check if the attendance feature is disabled
  11091. if( !battle_config.feature_attendance ){
  11092. return false;
  11093. }
  11094. // Check if there is a running attendance period
  11095. return pc_attendance_period() != nullptr;
  11096. }
  11097. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11098. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11099. }
  11100. int32 pc_attendance_counter( struct map_session_data* sd ){
  11101. struct s_attendance_period* period = pc_attendance_period();
  11102. // No running attendance period
  11103. if( period == nullptr ){
  11104. return 0;
  11105. }
  11106. // Get the counter for the current period
  11107. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11108. // Check if we have a remaining counter from a previous period
  11109. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11110. // Reset the counter to zero
  11111. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11112. return 0;
  11113. }
  11114. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11115. }
  11116. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11117. // If the user's group does not have the permission
  11118. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11119. return;
  11120. }
  11121. // Check if the attendance feature is disabled
  11122. if( !pc_attendance_enabled() ){
  11123. return;
  11124. }
  11125. // Check if the user already got his reward today
  11126. if( pc_attendance_rewarded_today( sd ) ){
  11127. return;
  11128. }
  11129. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11130. attendance_counter += 1;
  11131. struct s_attendance_period* period = pc_attendance_period();
  11132. if( period == nullptr ){
  11133. return;
  11134. }
  11135. if( period->rewards.size() < attendance_counter ){
  11136. return;
  11137. }
  11138. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11139. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11140. if( save_settings&CHARSAVE_ATTENDANCE )
  11141. chrif_save(sd, CSAVE_NORMAL);
  11142. struct s_attendance_reward& reward = period->rewards.at( attendance_counter - 1 );
  11143. struct mail_message msg;
  11144. memset( &msg, 0, sizeof( struct mail_message ) );
  11145. msg.dest_id = sd->status.char_id;
  11146. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11147. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11148. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11149. msg.item[0].nameid = reward.item_id;
  11150. msg.item[0].amount = reward.amount;
  11151. msg.item[0].identify = 1;
  11152. msg.status = MAIL_NEW;
  11153. msg.type = MAIL_INBOX_NORMAL;
  11154. msg.timestamp = time(NULL);
  11155. intif_Mail_send(0, &msg);
  11156. clif_attendence_response( sd, attendance_counter );
  11157. }
  11158. void pc_attendance_load( std::string path ){
  11159. YAML::Node root;
  11160. try{
  11161. root = YAML::LoadFile( path );
  11162. }catch( ... ){
  11163. ShowError( "pc_attendance_load: Failed to read attendance configuration file \"%s\".\n", path.c_str() );
  11164. return;
  11165. }
  11166. if( root["Attendance"] ){
  11167. YAML::Node attendance = root["Attendance"];
  11168. for( const auto &periodNode : attendance ){
  11169. if( !periodNode["Start"].IsDefined() ){
  11170. ShowError( "pc_attendance_load: Missing \"Start\" for period in line %d.\n", periodNode.Mark().line );
  11171. continue;
  11172. }
  11173. YAML::Node startNode = periodNode["Start"];
  11174. if( !periodNode["End"].IsDefined() ){
  11175. ShowError( "pc_attendance_load: Missing \"End\" for period in line %d.\n", periodNode.Mark().line );
  11176. continue;
  11177. }
  11178. YAML::Node endNode = periodNode["End"];
  11179. if( !periodNode["Rewards"].IsDefined() ){
  11180. ShowError( "pc_attendance_load: Missing \"Rewards\" for period in line %d.\n", periodNode.Mark().line );
  11181. continue;
  11182. }
  11183. YAML::Node rewardsNode = periodNode["Rewards"];
  11184. uint32 start = startNode.as<uint32>();
  11185. uint32 end = endNode.as<uint32>();
  11186. // If the period is outdated already, we do not even bother parsing
  11187. if( end < date_get( DT_YYYYMMDD ) ){
  11188. continue;
  11189. }
  11190. // Collision detection
  11191. bool collision = false;
  11192. for( struct s_attendance_period& period : attendance_periods ){
  11193. // Check if start is inside another period
  11194. if( period.start <= start && start <= period.end ){
  11195. ShowError( "pc_attendance_load: period start %u intersects with period %u-%u.\n", start, period.start, period.end );
  11196. collision = true;
  11197. break;
  11198. }
  11199. // Check if end is inside another period
  11200. if( period.start <= end && end <= period.end ){
  11201. ShowError( "pc_attendance_load: period end %u intersects with period %u-%u.\n", start, period.start, period.end );
  11202. collision = true;
  11203. break;
  11204. }
  11205. }
  11206. if( collision ){
  11207. continue;
  11208. }
  11209. struct s_attendance_period period;
  11210. period.start = start;
  11211. period.end = end;
  11212. for( const auto& rewardNode : rewardsNode ){
  11213. if( !rewardNode["Day"].IsDefined() ){
  11214. ShowError( "pc_attendance_load: No day defined for node in line %d.\n", rewardNode.Mark().line );
  11215. continue;
  11216. }
  11217. uint32 day = rewardNode["Day"].as<uint32>();
  11218. if( !rewardNode["ItemId"].IsDefined() ){
  11219. ShowError( "pc_attendance_load: No reward defined for day %d.\n", day );
  11220. continue;
  11221. }
  11222. YAML::Node itemNode = rewardNode["ItemId"];
  11223. uint16 item_id = itemNode.as<uint16>();
  11224. if( item_id == 0 || !itemdb_exists( item_id ) ){
  11225. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day );
  11226. continue;
  11227. }
  11228. uint16 amount;
  11229. if( rewardNode["Amount"] ){
  11230. amount = rewardNode["Amount"].as<uint16>();
  11231. if( amount == 0 ){
  11232. ShowError( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day );
  11233. amount = 1;
  11234. }else if( amount > MAX_AMOUNT ){
  11235. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day, MAX_AMOUNT );
  11236. amount = MAX_AMOUNT;
  11237. }
  11238. }else{
  11239. amount = 1;
  11240. }
  11241. struct s_attendance_reward* reward = &period.rewards[day - 1];
  11242. reward->item_id = item_id;
  11243. reward->amount = amount;
  11244. }
  11245. bool missing_day = false;
  11246. for( int day = 0; day < period.rewards.size(); day++ ){
  11247. if( !util::map_exists( period.rewards, day ) ){
  11248. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  11249. missing_day = true;
  11250. break;
  11251. }
  11252. }
  11253. if( missing_day ){
  11254. continue;
  11255. }
  11256. attendance_periods.push_back( period );
  11257. }
  11258. }
  11259. }
  11260. void pc_read_attendance(){
  11261. char path[1024];
  11262. sprintf( path, "%s/%sattendance.yml", db_path, DBPATH );
  11263. pc_attendance_load( path );
  11264. sprintf( path, "%s/%s/attendance.yml", db_path, DBIMPORT );
  11265. pc_attendance_load( path );
  11266. }
  11267. /*==========================================
  11268. * pc Init/Terminate
  11269. *------------------------------------------*/
  11270. void do_final_pc(void) {
  11271. db_destroy(itemcd_db);
  11272. do_final_pc_groups();
  11273. ers_destroy(pc_sc_display_ers);
  11274. ers_destroy(pc_itemgrouphealrate_ers);
  11275. ers_destroy(num_reg_ers);
  11276. ers_destroy(str_reg_ers);
  11277. attendance_periods.clear();
  11278. }
  11279. void do_init_pc(void) {
  11280. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11281. pc_readdb();
  11282. pc_read_motd(); // Read MOTD [Valaris]
  11283. pc_read_attendance();
  11284. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11285. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11286. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11287. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11288. add_timer_func_list(pc_autosave, "pc_autosave");
  11289. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11290. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11291. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11292. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11293. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11294. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11295. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11296. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11297. // 0=day, 1=night [Yor]
  11298. night_flag = battle_config.night_at_start ? 1 : 0;
  11299. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11300. int day_duration = battle_config.day_duration;
  11301. int night_duration = battle_config.night_duration;
  11302. // add night/day timer [Yor]
  11303. add_timer_func_list(map_day_timer, "map_day_timer");
  11304. add_timer_func_list(map_night_timer, "map_night_timer");
  11305. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11306. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11307. }
  11308. do_init_pc_groups();
  11309. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11310. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.cpp:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  11311. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11312. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11313. ers_chunk_size(pc_sc_display_ers, 150);
  11314. ers_chunk_size(num_reg_ers, 300);
  11315. ers_chunk_size(str_reg_ers, 50);
  11316. }