pc.c 388 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.h"
  4. #include "../common/cbasetypes.h"
  5. #include "../common/core.h" // get_svn_revision()
  6. #include "../common/malloc.h"
  7. #include "../common/nullpo.h"
  8. #include "../common/random.h"
  9. #include "../common/showmsg.h"
  10. #include "../common/socket.h" // session[]
  11. #include "../common/strlib.h" // safestrncpy()
  12. #include "../common/timer.h"
  13. #include "../common/utils.h"
  14. #include "../common/mmo.h" //NAME_LENGTH
  15. #include "atcommand.h" // get_atcommand_level()
  16. #include "map.h"
  17. #include "battle.h" // battle_config
  18. #include "battleground.h"
  19. #include "channel.h"
  20. #include "chat.h"
  21. #include "chrif.h"
  22. #include "clan.h"
  23. #include "date.h" // is_day_of_*()
  24. #include "duel.h"
  25. #include "intif.h"
  26. #include "homunculus.h"
  27. #include "instance.h"
  28. #include "mercenary.h"
  29. #include "elemental.h"
  30. #include "pet.h" // pet_unlocktarget()
  31. #include "party.h" // party_search()
  32. #include "storage.h"
  33. #include "quest.h"
  34. #include "achievement.h"
  35. #include <stdlib.h>
  36. #include <math.h>
  37. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  38. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  39. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  40. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  41. static unsigned int statp[MAX_LEVEL+1];
  42. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  43. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  44. #endif
  45. // h-files are for declarations, not for implementations... [Shinomori]
  46. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  47. // timer for night.day implementation
  48. int day_timer_tid = INVALID_TIMER;
  49. int night_timer_tid = INVALID_TIMER;
  50. struct eri *pc_sc_display_ers = NULL;
  51. struct eri *pc_itemgrouphealrate_ers = NULL;
  52. struct eri *num_reg_ers;
  53. struct eri *str_reg_ers;
  54. int pc_expiration_tid = INVALID_TIMER;
  55. struct fame_list smith_fame_list[MAX_FAME_LIST];
  56. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  57. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  58. struct s_job_info job_info[CLASS_COUNT];
  59. #define MOTD_LINE_SIZE 128
  60. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  61. bool reg_load;
  62. /**
  63. * Translation table from athena equip index to aegis bitmask
  64. */
  65. unsigned int equip_bitmask[EQI_MAX] = {
  66. EQP_ACC_L, // EQI_ACC_L
  67. EQP_ACC_R, // EQI_ACC_R
  68. EQP_SHOES, // EQI_SHOES
  69. EQP_GARMENT, // EQI_GARMENT
  70. EQP_HEAD_LOW, // EQI_HEAD_LOW
  71. EQP_HEAD_MID, // EQI_HEAD_MID
  72. EQP_HEAD_TOP, // EQI_HEAD_TOP
  73. EQP_ARMOR, // EQI_ARMOR
  74. EQP_HAND_L, // EQI_HAND_L
  75. EQP_HAND_R, // EQI_HAND_R
  76. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  77. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  78. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  79. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  80. EQP_AMMO, // EQI_AMMO
  81. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  82. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  83. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  84. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  85. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  86. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  87. };
  88. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  89. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  90. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  91. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  92. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  93. };
  94. void pc_set_reg_load( bool val ){
  95. reg_load = val;
  96. }
  97. /**
  98. * Item Cool Down Delay Saving
  99. * Struct item_cd is not a member of struct map_session_data
  100. * to keep cooldowns in memory between player log-ins.
  101. * All cooldowns are reset when server is restarted.
  102. **/
  103. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  104. struct item_cd {
  105. unsigned int tick[MAX_ITEMDELAYS]; //tick
  106. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  107. };
  108. /**
  109. * Converts a class to its array index for CLASS_COUNT defined arrays.
  110. * Note that it does not do a validity check for speed purposes, where parsing
  111. * player input make sure to use a pcdb_checkid first!
  112. * @param class_ Job ID see enum e_job
  113. * @return Class Index
  114. */
  115. int pc_class2idx(int class_) {
  116. if (class_ >= JOB_NOVICE_HIGH)
  117. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  118. return class_;
  119. }
  120. /**
  121. * Get player's group ID
  122. * @param sd
  123. * @return Group ID
  124. */
  125. inline int pc_get_group_id(struct map_session_data *sd) {
  126. return sd->group_id;
  127. }
  128. /** Get player's group Level
  129. * @param sd
  130. * @return Group Level
  131. */
  132. inline int pc_get_group_level(struct map_session_data *sd) {
  133. return sd->group_level;
  134. }
  135. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  136. {
  137. struct map_session_data *sd;
  138. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  139. return 1;
  140. if(sd->invincible_timer != tid){
  141. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  142. return 0;
  143. }
  144. sd->invincible_timer = INVALID_TIMER;
  145. skill_unit_move(&sd->bl,tick,1);
  146. return 0;
  147. }
  148. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  149. nullpo_retv(sd);
  150. if( sd->invincible_timer != INVALID_TIMER )
  151. delete_timer(sd->invincible_timer,pc_invincible_timer);
  152. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  153. }
  154. void pc_delinvincibletimer(struct map_session_data* sd)
  155. {
  156. nullpo_retv(sd);
  157. if( sd->invincible_timer != INVALID_TIMER )
  158. {
  159. delete_timer(sd->invincible_timer,pc_invincible_timer);
  160. sd->invincible_timer = INVALID_TIMER;
  161. skill_unit_move(&sd->bl,gettick(),1);
  162. }
  163. }
  164. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  165. {
  166. struct map_session_data *sd;
  167. int i;
  168. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  169. return 1;
  170. if( sd->spiritball <= 0 )
  171. {
  172. 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);
  173. sd->spiritball = 0;
  174. return 0;
  175. }
  176. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  177. if( i == sd->spiritball )
  178. {
  179. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  180. return 0;
  181. }
  182. sd->spiritball--;
  183. if( i != sd->spiritball )
  184. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  185. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  186. clif_spiritball(&sd->bl);
  187. return 0;
  188. }
  189. /**
  190. * Adds a spiritball to player for 'interval' ms
  191. * @param sd
  192. * @param interval
  193. * @param max
  194. */
  195. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  196. {
  197. int tid;
  198. uint8 i;
  199. nullpo_retv(sd);
  200. if(max > MAX_SPIRITBALL)
  201. max = MAX_SPIRITBALL;
  202. if(sd->spiritball < 0)
  203. sd->spiritball = 0;
  204. if( sd->spiritball && sd->spiritball >= max )
  205. {
  206. if(sd->spirit_timer[0] != INVALID_TIMER)
  207. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  208. sd->spiritball--;
  209. if( sd->spiritball != 0 )
  210. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  211. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  212. }
  213. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  214. 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);
  215. if( i != sd->spiritball )
  216. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  217. sd->spirit_timer[i] = tid;
  218. sd->spiritball++;
  219. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  220. clif_millenniumshield(&sd->bl,sd->spiritball);
  221. else
  222. clif_spiritball(&sd->bl);
  223. }
  224. /**
  225. * Removes number of spiritball from player
  226. * @param sd
  227. * @param count
  228. * @param type 1 = doesn't give client effect
  229. */
  230. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  231. {
  232. uint8 i;
  233. nullpo_retv(sd);
  234. if(sd->spiritball <= 0) {
  235. sd->spiritball = 0;
  236. return;
  237. }
  238. if(count == 0)
  239. return;
  240. if(count > sd->spiritball)
  241. count = sd->spiritball;
  242. sd->spiritball -= count;
  243. if(count > MAX_SPIRITBALL)
  244. count = MAX_SPIRITBALL;
  245. for(i=0;i<count;i++) {
  246. if(sd->spirit_timer[i] != INVALID_TIMER) {
  247. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  248. sd->spirit_timer[i] = INVALID_TIMER;
  249. }
  250. }
  251. for(i=count;i<MAX_SPIRITBALL;i++) {
  252. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  253. sd->spirit_timer[i] = INVALID_TIMER;
  254. }
  255. if(!type) {
  256. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  257. clif_millenniumshield(&sd->bl,sd->spiritball);
  258. else
  259. clif_spiritball(&sd->bl);
  260. }
  261. }
  262. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  263. int *c, *b_sd;
  264. struct block_list *src;
  265. struct map_session_data *tsd;
  266. struct status_change *sc;
  267. nullpo_ret(bl);
  268. nullpo_ret(tsd = (struct map_session_data*)bl);
  269. nullpo_ret(src = va_arg(ap,struct block_list *));
  270. c = va_arg(ap,int *);
  271. b_sd = va_arg(ap, int *);
  272. if(pc_isdead(tsd))
  273. return 0;
  274. sc = status_get_sc(bl);
  275. if( sc && sc->data[SC_BANDING] )
  276. {
  277. b_sd[(*c)++] = tsd->bl.id;
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  283. int c;
  284. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  285. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  286. int range = skill_get_splash(LG_BANDING,skill_lv);
  287. nullpo_ret(sd);
  288. c = 0;
  289. memset(b_sd, 0, sizeof(b_sd));
  290. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  291. if( c < 1 ) //just recalc status no need to recalc hp
  292. { // No more Royal Guards in Banding found.
  293. struct status_change *sc;
  294. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  295. {
  296. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  297. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  298. }
  299. return 0;
  300. }
  301. //Add yourself
  302. hp = status_get_hp(&sd->bl);
  303. i++;
  304. // Get total HP of all Royal Guards in party.
  305. for( j = 0; j < i; j++ ){
  306. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  307. if( bsd != NULL )
  308. hp += status_get_hp(&bsd->bl);
  309. }
  310. // Set average HP.
  311. hp = hp / i;
  312. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  313. for( j = 0; j < i; j++ )
  314. {
  315. int tmp_hp=0;
  316. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  317. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  318. extra_hp += tmp_hp;
  319. tmp_qty++;
  320. }
  321. }
  322. if( extra_hp > 0 && tmp_qty > 0 )
  323. hp += extra_hp / tmp_qty;
  324. for( j = 0; j < i; j++ ){
  325. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  326. if( bsd != NULL )
  327. {
  328. struct status_change *sc;
  329. status_set_hp(&bsd->bl,hp,0); // Set hp
  330. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  331. {
  332. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  333. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  334. }
  335. }
  336. }
  337. return c;
  338. }
  339. /**
  340. * Increases a player's fame points and displays a notice to him
  341. * @param sd Player
  342. * @param count Fame point
  343. */
  344. void pc_addfame(struct map_session_data *sd,int count)
  345. {
  346. enum e_rank ranktype;
  347. nullpo_retv(sd);
  348. sd->status.fame += count;
  349. if(sd->status.fame > MAX_FAME)
  350. sd->status.fame = MAX_FAME;
  351. switch(sd->class_&MAPID_UPPERMASK){
  352. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  353. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  354. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  355. default:
  356. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  357. return;
  358. }
  359. clif_update_rankingpoint(sd,ranktype,count);
  360. chrif_updatefamelist(sd);
  361. }
  362. /**
  363. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  364. * @param sd
  365. * @param job Job use enum e_mapid
  366. * @return Rank
  367. */
  368. unsigned char pc_famerank(uint32 char_id, int job)
  369. {
  370. uint8 i;
  371. switch(job){
  372. case MAPID_BLACKSMITH: // Blacksmith
  373. for(i = 0; i < MAX_FAME_LIST; i++){
  374. if(smith_fame_list[i].id == char_id)
  375. return i + 1;
  376. }
  377. break;
  378. case MAPID_ALCHEMIST: // Alchemist
  379. for(i = 0; i < MAX_FAME_LIST; i++){
  380. if(chemist_fame_list[i].id == char_id)
  381. return i + 1;
  382. }
  383. break;
  384. case MAPID_TAEKWON: // Taekwon
  385. for(i = 0; i < MAX_FAME_LIST; i++){
  386. if(taekwon_fame_list[i].id == char_id)
  387. return i + 1;
  388. }
  389. break;
  390. }
  391. return 0;
  392. }
  393. /**
  394. * Restart player's HP & SP value
  395. * @param sd
  396. * @param type Restart type: 1 - Normal Resurection
  397. */
  398. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  399. struct status_data *status, *b_status;
  400. nullpo_retv(sd);
  401. b_status = &sd->base_status;
  402. status = &sd->battle_status;
  403. if (type&1) { //Normal resurrection
  404. status->hp = 1; //Otherwise status_heal may fail if dead.
  405. status_heal(&sd->bl, b_status->hp, 0, 1);
  406. if( status->sp < b_status->sp )
  407. status_set_sp(&sd->bl, b_status->sp, 1);
  408. } else { //Just for saving on the char-server (with values as if respawned)
  409. sd->status.hp = b_status->hp;
  410. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  411. }
  412. }
  413. /*==========================================
  414. Rental System
  415. *------------------------------------------*/
  416. /**
  417. * Ends a rental and removes the item/effect
  418. * @param tid: Tick ID
  419. * @param tick: Timer
  420. * @param id: Timer ID
  421. * @param data: Data
  422. * @return false - failure, true - success
  423. */
  424. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  425. {
  426. struct map_session_data *sd = map_id2sd(id);
  427. if( sd == NULL )
  428. return 0;
  429. if( tid != sd->rental_timer ) {
  430. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  431. return 0;
  432. }
  433. pc_inventory_rentals(sd);
  434. return 1;
  435. }
  436. /**
  437. * Removes the rental timer from the player
  438. * @param sd: Player data
  439. */
  440. void pc_inventory_rental_clear(struct map_session_data *sd)
  441. {
  442. if( sd->rental_timer != INVALID_TIMER ) {
  443. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  444. sd->rental_timer = INVALID_TIMER;
  445. }
  446. }
  447. /**
  448. * Check for items in the player's inventory that are rental type
  449. * @param sd: Player data
  450. */
  451. void pc_inventory_rentals(struct map_session_data *sd)
  452. {
  453. int i, c = 0;
  454. unsigned int next_tick = UINT_MAX;
  455. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  456. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  457. continue; // Nothing here
  458. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  459. continue;
  460. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  461. if (sd->inventory_data[i]->unequip_script)
  462. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  463. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  464. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  465. } else {
  466. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  467. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  468. next_tick = umin(expire_tick * 1000U, next_tick);
  469. c++;
  470. }
  471. }
  472. 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
  473. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  474. else
  475. sd->rental_timer = INVALID_TIMER;
  476. }
  477. /**
  478. * Add a rental item to the player and adjusts the rental timer appropriately
  479. * @param sd: Player data
  480. * @param seconds: Rental time
  481. */
  482. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  483. {
  484. unsigned int tick = seconds * 1000;
  485. if( sd == NULL )
  486. return;
  487. if( sd->rental_timer != INVALID_TIMER ) {
  488. const struct TimerData * td;
  489. td = get_timer(sd->rental_timer);
  490. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  491. pc_inventory_rental_clear(sd);
  492. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  493. }
  494. } else
  495. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  496. }
  497. /**
  498. * Check if the player can sell the current item
  499. * @param sd: map_session_data of the player
  500. * @param item: struct of the checking item
  501. * @return bool 'true' is sellable, 'false' otherwise
  502. */
  503. bool pc_can_sell_item(struct map_session_data *sd, struct item *item) {
  504. struct npc_data *nd;
  505. if (sd == NULL || item == NULL)
  506. return false;
  507. nd = map_id2nd(sd->npc_shopid);
  508. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  509. return false;
  510. if (battle_config.hide_fav_sell && item->favorite)
  511. return false; //Cannot sell favs (optional config)
  512. if (item->expire_time)
  513. return false; // Cannot Sell Rental Items
  514. if (nd && nd->subtype == NPCTYPE_ITEMSHOP && item->bound && battle_config.allow_bound_sell)
  515. return true; // NPCTYPE_ITEMSHOP and bound item config is sellable
  516. if (item->bound && !pc_can_give_bounded_items(sd))
  517. return false; // Don't allow sale of bound items
  518. return true;
  519. }
  520. /**
  521. * Determines if player can give / drop / trade / vend items
  522. */
  523. bool pc_can_give_items(struct map_session_data *sd)
  524. {
  525. return pc_has_permission(sd, PC_PERM_TRADE);
  526. }
  527. /**
  528. * Determines if player can give / drop / trade / vend bounded items
  529. */
  530. bool pc_can_give_bounded_items(struct map_session_data *sd)
  531. {
  532. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  533. }
  534. /*==========================================
  535. * Prepares character for saving.
  536. * @param sd
  537. *------------------------------------------*/
  538. void pc_makesavestatus(struct map_session_data *sd) {
  539. nullpo_retv(sd);
  540. if(!battle_config.save_clothcolor)
  541. sd->status.clothes_color = 0;
  542. // Since this is currently not officially released,
  543. // its best to have a forced option to not save body styles.
  544. if(!battle_config.save_body_style)
  545. sd->status.body = 0;
  546. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  547. //status change load/saving. [Skotlex]
  548. #ifdef NEW_CARTS
  549. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  550. #else
  551. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  552. #endif
  553. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  554. if(pc_isdead(sd)){
  555. pc_setrestartvalue(sd, 0);
  556. } else {
  557. sd->status.hp = sd->battle_status.hp;
  558. sd->status.sp = sd->battle_status.sp;
  559. }
  560. sd->status.last_point.map = sd->mapindex;
  561. sd->status.last_point.x = sd->bl.x;
  562. sd->status.last_point.y = sd->bl.y;
  563. return;
  564. }
  565. if(pc_isdead(sd)) {
  566. pc_setrestartvalue(sd, 0);
  567. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  568. } else {
  569. sd->status.hp = sd->battle_status.hp;
  570. sd->status.sp = sd->battle_status.sp;
  571. sd->status.last_point.map = sd->mapindex;
  572. sd->status.last_point.x = sd->bl.x;
  573. sd->status.last_point.y = sd->bl.y;
  574. }
  575. if(map[sd->bl.m].flag.nosave) {
  576. struct map_data *m=&map[sd->bl.m];
  577. if(m->save.map)
  578. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  579. else
  580. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  581. }
  582. }
  583. /*==========================================
  584. * Off init ? Connection?
  585. *------------------------------------------*/
  586. 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) {
  587. nullpo_retv(sd);
  588. sd->bl.id = account_id;
  589. sd->status.account_id = account_id;
  590. sd->status.char_id = char_id;
  591. sd->status.sex = sex;
  592. sd->login_id1 = login_id1;
  593. sd->login_id2 = 0; // at this point, we can not know the value :(
  594. sd->client_tick = client_tick;
  595. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  596. sd->bl.type = BL_PC;
  597. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  598. sd->canlog_tick = gettick();
  599. //Required to prevent homunculus copuing a base speed of 0.
  600. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  601. }
  602. /**
  603. * Get equip point for an equip
  604. * @param sd
  605. * @param id
  606. */
  607. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  608. int ep = 0;
  609. nullpo_ret(sd);
  610. nullpo_ret(id);
  611. if (!itemdb_isequip2(id))
  612. return 0; //Not equippable by players.
  613. ep = id->equip;
  614. if(id->look == W_DAGGER ||
  615. id->look == W_1HSWORD ||
  616. id->look == W_1HAXE) {
  617. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  618. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  619. return EQP_ARMS;
  620. }
  621. return ep;
  622. }
  623. /**
  624. * Get equip point for an equip
  625. * @param sd
  626. * @param n Equip index in inventory
  627. */
  628. int pc_equippoint(struct map_session_data *sd,int n){
  629. nullpo_ret(sd);
  630. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  631. }
  632. /**
  633. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  634. * @param sd : player session
  635. * @return 0 sucess, 1:invalid sd
  636. */
  637. void pc_setinventorydata(struct map_session_data *sd)
  638. {
  639. uint8 i;
  640. nullpo_retv(sd);
  641. for(i = 0; i < MAX_INVENTORY; i++) {
  642. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  643. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  644. }
  645. }
  646. /**
  647. * 'Calculates' weapon type
  648. * @param sd : Player
  649. */
  650. void pc_calcweapontype(struct map_session_data *sd)
  651. {
  652. nullpo_retv(sd);
  653. // single-hand
  654. if(sd->weapontype2 == W_FIST) {
  655. sd->status.weapon = sd->weapontype1;
  656. return;
  657. }
  658. if(sd->weapontype1 == W_FIST) {
  659. sd->status.weapon = sd->weapontype2;
  660. return;
  661. }
  662. // dual-wield
  663. sd->status.weapon = 0;
  664. switch (sd->weapontype1){
  665. case W_DAGGER:
  666. switch (sd->weapontype2) {
  667. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  668. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  669. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  670. }
  671. break;
  672. case W_1HSWORD:
  673. switch (sd->weapontype2) {
  674. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  675. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  676. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  677. }
  678. break;
  679. case W_1HAXE:
  680. switch (sd->weapontype2) {
  681. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  682. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  683. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  684. }
  685. }
  686. // unknown, default to right hand type
  687. if (!sd->status.weapon)
  688. sd->status.weapon = sd->weapontype1;
  689. }
  690. /**
  691. * Set equip index
  692. * @param sd : Player
  693. */
  694. void pc_setequipindex(struct map_session_data *sd)
  695. {
  696. uint16 i;
  697. nullpo_retv(sd);
  698. for (i = 0; i < EQI_MAX; i++)
  699. sd->equip_index[i] = -1;
  700. for (i = 0; i < MAX_INVENTORY; i++) {
  701. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  702. continue;
  703. if (sd->inventory.u.items_inventory[i].equip) {
  704. uint8 j;
  705. for (j = 0; j < EQI_MAX; j++)
  706. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  707. sd->equip_index[j] = i;
  708. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  709. if (sd->inventory_data[i])
  710. sd->weapontype1 = sd->inventory_data[i]->look;
  711. else
  712. sd->weapontype1 = 0;
  713. }
  714. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  715. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  716. sd->weapontype2 = sd->inventory_data[i]->look;
  717. else
  718. sd->weapontype2 = 0;
  719. }
  720. }
  721. }
  722. pc_calcweapontype(sd);
  723. }
  724. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  725. //{
  726. // int i;
  727. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  728. // struct item_data *data;
  729. //
  730. // //Crafted/made/hatched items.
  731. // if (itemdb_isspecial(item->card[0]))
  732. // return 1;
  733. //
  734. // /* scan for enchant armor gems */
  735. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  736. // s = MAX_SLOTS - 1;
  737. //
  738. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  739. // return( i < s ) ? 0 : 1;
  740. //}
  741. /**
  742. * Check if an item is equiped by player
  743. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  744. * @param sd : player session
  745. * @param nameid : itemid
  746. * @return 1:yes, 0:no
  747. */
  748. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  749. {
  750. uint8 i;
  751. for( i = 0; i < EQI_MAX; i++ )
  752. {
  753. short index = sd->equip_index[i], j;
  754. if( index < 0 )
  755. continue;
  756. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  757. continue;
  758. if( !sd->inventory_data[index] )
  759. continue;
  760. if( sd->inventory_data[index]->nameid == nameid )
  761. return true;
  762. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  763. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  764. return true;
  765. }
  766. }
  767. return false;
  768. }
  769. /**
  770. * Check adoption rules
  771. * @param p1_sd: Player 1
  772. * @param p2_sd: Player 2
  773. * @param b_sd: Player that will be adopted
  774. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  775. * ADOPT_ALREADY_ADOPTED - Already adopted
  776. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  777. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  778. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  779. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  780. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  781. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  782. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  783. */
  784. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  785. {
  786. if( !p1_sd || !p2_sd || !b_sd )
  787. return ADOPT_CHARACTER_NOT_FOUND;
  788. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  789. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  790. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  791. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  792. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  793. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  794. if( p2_sd->status.party_id != p1_sd->status.party_id )
  795. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  796. // Parents need to have their ring equipped
  797. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  798. return ADOPT_EQUIP_RINGS;
  799. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  800. return ADOPT_EQUIP_RINGS;
  801. // Already adopted a baby
  802. if( p1_sd->status.child || p2_sd->status.child ) {
  803. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  804. return ADOPT_MORE_CHILDREN;
  805. }
  806. // Parents need at least lvl 70 to adopt
  807. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  808. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  809. return ADOPT_LEVEL_70;
  810. }
  811. if( b_sd->status.partner_id ) {
  812. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  813. return ADOPT_MARRIED;
  814. }
  815. 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 ) )
  816. return ADOPT_NOT_NOVICE;
  817. return ADOPT_ALLOWED;
  818. }
  819. /*==========================================
  820. * Adoption Process
  821. *------------------------------------------*/
  822. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  823. {
  824. int job, joblevel;
  825. unsigned int jobexp;
  826. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  827. return false;
  828. // Preserve current job levels and progress
  829. joblevel = b_sd->status.job_level;
  830. jobexp = b_sd->status.job_exp;
  831. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  832. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  833. { // Success, proceed to configure parents and baby skills
  834. p1_sd->status.child = b_sd->status.char_id;
  835. p2_sd->status.child = b_sd->status.char_id;
  836. b_sd->status.father = p1_sd->status.char_id;
  837. b_sd->status.mother = p2_sd->status.char_id;
  838. // Restore progress
  839. b_sd->status.job_level = joblevel;
  840. clif_updatestatus(b_sd, SP_JOBLEVEL);
  841. b_sd->status.job_exp = jobexp;
  842. clif_updatestatus(b_sd, SP_JOBEXP);
  843. // Baby Skills
  844. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  845. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  846. // Parents Skills
  847. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  848. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  849. chrif_save(p1_sd, CSAVE_NORMAL);
  850. chrif_save(p2_sd, CSAVE_NORMAL);
  851. chrif_save(b_sd, CSAVE_NORMAL);
  852. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  853. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  854. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  855. return true;
  856. }
  857. return false; // Job Change Fail
  858. }
  859. /*==========================================
  860. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  861. Returns:
  862. false : Cannot use/equip
  863. true : Can use/equip
  864. * Credits:
  865. [Inkfish] for first idea
  866. [Haru] for third-classes extension
  867. [Cydh] finishing :D
  868. *------------------------------------------*/
  869. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  870. while (1) {
  871. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  872. break;
  873. #ifndef RENEWAL
  874. //allow third classes to use trans. class items
  875. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  876. break;
  877. //third-baby classes can use same item too
  878. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  879. break;
  880. //don't need to decide specific rules for third-classes?
  881. //items for third classes can be used for all third classes
  882. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  883. break;
  884. #else
  885. //trans. classes (exl. third-trans.)
  886. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  887. break;
  888. //baby classes (exl. third-baby)
  889. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  890. break;
  891. //third classes (exl. third-trans. and baby-third)
  892. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  893. break;
  894. //trans-third classes
  895. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  896. break;
  897. //third-baby classes
  898. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  899. break;
  900. #endif
  901. return false;
  902. }
  903. return true;
  904. }
  905. /*=================================================
  906. * Checks if the player can equip the item at index n in inventory.
  907. * @param sd
  908. * @param n Item index in inventory
  909. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  910. *------------------------------------------------*/
  911. uint8 pc_isequip(struct map_session_data *sd,int n)
  912. {
  913. struct item_data *item;
  914. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  915. item = sd->inventory_data[n];
  916. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  917. return ITEM_EQUIP_ACK_OK;
  918. if(item == NULL)
  919. return ITEM_EQUIP_ACK_FAIL;
  920. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  921. return ITEM_EQUIP_ACK_FAILLEVEL;
  922. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  923. return ITEM_EQUIP_ACK_FAILLEVEL;
  924. if(item->sex != 2 && sd->status.sex != item->sex)
  925. return ITEM_EQUIP_ACK_FAIL;
  926. if (sd->sc.count) {
  927. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  928. return ITEM_EQUIP_ACK_FAIL;
  929. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  930. return ITEM_EQUIP_ACK_FAIL;
  931. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  932. return ITEM_EQUIP_ACK_FAIL;
  933. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  934. return ITEM_EQUIP_ACK_FAIL;
  935. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  936. return ITEM_EQUIP_ACK_FAIL;
  937. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  938. return ITEM_EQUIP_ACK_FAIL;
  939. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  940. //Spirit of Super Novice equip bonuses. [Skotlex]
  941. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  942. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  943. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  944. switch(item->look) { //In weapons, the look determines type of weapon.
  945. case W_DAGGER: //All level 4 - Daggers
  946. case W_1HSWORD: //All level 4 - 1H Swords
  947. case W_1HAXE: //All level 4 - 1H Axes
  948. case W_MACE: //All level 4 - 1H Maces
  949. case W_STAFF: //All level 4 - 1H Staves
  950. case W_2HSTAFF: //All level 4 - 2H Staves
  951. return ITEM_EQUIP_ACK_OK;
  952. }
  953. }
  954. }
  955. //fail to equip if item is restricted
  956. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  957. return ITEM_EQUIP_ACK_FAIL;
  958. //Not equipable by class. [Skotlex]
  959. if (!(1ULL<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  960. return ITEM_EQUIP_ACK_FAIL;
  961. if (!pc_isItemClass(sd,item))
  962. return ITEM_EQUIP_ACK_FAIL;
  963. return ITEM_EQUIP_ACK_OK;
  964. }
  965. /*==========================================
  966. * No problem with the session id
  967. * set the status that has been sent from char server
  968. *------------------------------------------*/
  969. 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)
  970. {
  971. int i;
  972. unsigned long tick = gettick();
  973. uint32 ip = session[sd->fd]->client_addr;
  974. sd->login_id2 = login_id2;
  975. sd->group_id = group_id;
  976. /* load user permissions */
  977. pc_group_pc_load(sd);
  978. memcpy(&sd->status, st, sizeof(*st));
  979. if (st->sex != sd->status.sex) {
  980. clif_authfail_fd(sd->fd, 0);
  981. return false;
  982. }
  983. //Set the map-server used job id. [Skotlex]
  984. i = pc_jobid2mapid(sd->status.class_);
  985. if (i == -1) { //Invalid class?
  986. 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);
  987. sd->status.class_ = JOB_NOVICE;
  988. sd->class_ = MAPID_NOVICE;
  989. } else
  990. sd->class_ = i;
  991. // Checks and fixes to character status data, that are required
  992. // in case of configuration change or stuff, which cannot be
  993. // checked on char-server.
  994. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  995. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  996. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  997. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  998. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  999. sd->state.connect_new = 1;
  1000. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1001. sd->invincible_timer = INVALID_TIMER;
  1002. sd->npc_timer_id = INVALID_TIMER;
  1003. sd->pvp_timer = INVALID_TIMER;
  1004. sd->expiration_tid = INVALID_TIMER;
  1005. sd->autotrade_tid = INVALID_TIMER;
  1006. #ifdef SECURE_NPCTIMEOUT
  1007. // Initialize to defaults/expected
  1008. sd->npc_idle_timer = INVALID_TIMER;
  1009. sd->npc_idle_tick = tick;
  1010. sd->npc_idle_type = NPCT_INPUT;
  1011. sd->state.ignoretimeout = false;
  1012. #endif
  1013. sd->canuseitem_tick = tick;
  1014. sd->canusecashfood_tick = tick;
  1015. sd->canequip_tick = tick;
  1016. sd->cantalk_tick = tick;
  1017. sd->canskill_tick = tick;
  1018. sd->cansendmail_tick = tick;
  1019. sd->idletime = last_tick;
  1020. for(i = 0; i < MAX_SPIRITBALL; i++)
  1021. sd->spirit_timer[i] = INVALID_TIMER;
  1022. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  1023. sd->autobonus[i].active = INVALID_TIMER;
  1024. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  1025. sd->autobonus2[i].active = INVALID_TIMER;
  1026. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  1027. sd->autobonus3[i].active = INVALID_TIMER;
  1028. if (battle_config.item_auto_get)
  1029. sd->state.autoloot = 10000;
  1030. if (battle_config.disp_experience)
  1031. sd->state.showexp = 1;
  1032. if (battle_config.disp_zeny)
  1033. sd->state.showzeny = 1;
  1034. #ifdef VIP_ENABLE
  1035. if (!battle_config.vip_disp_rate)
  1036. sd->vip.disableshowrate = 1;
  1037. #endif
  1038. if (!(battle_config.display_skill_fail&2))
  1039. sd->state.showdelay = 1;
  1040. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1041. memset(&sd->cart, 0, sizeof(struct s_storage));
  1042. memset(&sd->storage, 0, sizeof(struct s_storage));
  1043. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1044. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1045. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1046. sd->status.option &= ~OPTION_INVISIBLE;
  1047. }
  1048. status_change_init(&sd->bl);
  1049. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1050. unit_dataset(&sd->bl);
  1051. sd->guild_x = -1;
  1052. sd->guild_y = -1;
  1053. sd->delayed_damage = 0;
  1054. // Event Timers
  1055. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1056. sd->eventtimer[i] = INVALID_TIMER;
  1057. // Rental Timer
  1058. sd->rental_timer = INVALID_TIMER;
  1059. for( i = 0; i < 3; i++ )
  1060. sd->hate_mob[i] = -1;
  1061. sd->quest_log = NULL;
  1062. sd->num_quests = 0;
  1063. sd->avail_quests = 0;
  1064. sd->save_quest = false;
  1065. sd->count_rewarp = 0;
  1066. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1067. sd->regs.arrays = NULL;
  1068. sd->vars_dirty = false;
  1069. sd->vars_ok = false;
  1070. sd->vars_received = 0x0;
  1071. sd->qi_display = NULL;
  1072. sd->qi_count = 0;
  1073. //warp player
  1074. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1075. 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);
  1076. // try warping to a default map instead (church graveyard)
  1077. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1078. // if we fail again
  1079. clif_authfail_fd(sd->fd, 0);
  1080. return false;
  1081. }
  1082. }
  1083. clif_authok(sd);
  1084. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1085. sd->die_counter=-1;
  1086. //display login notice
  1087. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1088. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1089. " IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1090. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1091. sd->status.name, sd->status.account_id, sd->status.char_id,
  1092. CONVIP(ip), sd->group_id);
  1093. // Send friends list
  1094. clif_friendslist_send(sd);
  1095. if( !changing_mapservers ) {
  1096. if (battle_config.display_version == 1)
  1097. pc_show_version(sd);
  1098. // Message of the Day [Valaris]
  1099. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1100. if (battle_config.motd_type)
  1101. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1102. else
  1103. clif_displaymessage(sd->fd, motd_text[i]);
  1104. }
  1105. if (expiration_time != 0)
  1106. sd->expiration_time = expiration_time;
  1107. /**
  1108. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1109. **/
  1110. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1111. }
  1112. pc_validate_skill(sd);
  1113. /* [Ind] */
  1114. sd->sc_display = NULL;
  1115. sd->sc_display_count = 0;
  1116. // Player has not yet received the CashShop list
  1117. sd->status.cashshop_sent = false;
  1118. sd->last_addeditem_index = -1;
  1119. sd->bonus_script.head = NULL;
  1120. sd->bonus_script.count = 0;
  1121. #if PACKETVER >= 20150513
  1122. sd->hatEffectIDs = NULL;
  1123. sd->hatEffectCount = 0;
  1124. #endif
  1125. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1126. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1127. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1128. clif_updatestatus(sd, SP_BASEEXP);
  1129. }
  1130. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1131. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1132. clif_updatestatus(sd, SP_JOBEXP);
  1133. }
  1134. // Request all registries (auth is considered completed whence they arrive)
  1135. intif_request_registry(sd,7);
  1136. return true;
  1137. }
  1138. /*==========================================
  1139. * Closes a connection because it failed to be authenticated from the char server.
  1140. *------------------------------------------*/
  1141. void pc_authfail(struct map_session_data *sd)
  1142. {
  1143. clif_authfail_fd(sd->fd, 0);
  1144. return;
  1145. }
  1146. /**
  1147. * Player register a bl as hatred
  1148. * @param sd : player session
  1149. * @param pos : hate position [0;2]
  1150. * @param bl : target bl
  1151. * @return false:failed, true:success
  1152. */
  1153. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1154. {
  1155. int class_;
  1156. if (!sd || !bl || pos < 0 || pos > 2)
  1157. return false;
  1158. if (sd->hate_mob[pos] != -1)
  1159. { //Can't change hate targets.
  1160. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1161. return false;
  1162. }
  1163. class_ = status_get_class(bl);
  1164. if (!pcdb_checkid(class_)) {
  1165. unsigned int max_hp = status_get_max_hp(bl);
  1166. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1167. return false;
  1168. if (pos != status_get_size(bl))
  1169. return false; //Wrong size
  1170. }
  1171. sd->hate_mob[pos] = class_;
  1172. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1173. clif_hate_info(sd, pos, class_, 1);
  1174. return true;
  1175. }
  1176. /*==========================================
  1177. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1178. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1179. * See intif_parse_StorageReceived() for item operations [lighta]
  1180. *------------------------------------------*/
  1181. void pc_reg_received(struct map_session_data *sd)
  1182. {
  1183. uint8 i;
  1184. sd->vars_ok = true;
  1185. sd->change_level_2nd = pc_readglobalreg(sd, add_str("jobchange_level"));
  1186. sd->change_level_3rd = pc_readglobalreg(sd, add_str("jobchange_level_3rd"));
  1187. sd->die_counter = pc_readglobalreg(sd, add_str("PC_DIE_COUNTER"));
  1188. sd->langtype = pc_readaccountreg(sd, add_str("#langtype"));
  1189. if (msg_checklangtype(sd->langtype,true) < 0)
  1190. sd->langtype = 0; //invalid langtype reset to default
  1191. // Cash shop
  1192. sd->cashPoints = pc_readaccountreg(sd, add_str("#CASHPOINTS"));
  1193. sd->kafraPoints = pc_readaccountreg(sd, add_str("#KAFRAPOINTS"));
  1194. // Cooking Exp
  1195. sd->cook_mastery = pc_readglobalreg(sd, add_str("COOK_MASTERY"));
  1196. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1197. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1198. sd->mission_mobid = pc_readglobalreg(sd, add_str("TK_MISSION_ID"));
  1199. sd->mission_count = pc_readglobalreg(sd, add_str("TK_MISSION_COUNT"));
  1200. }
  1201. if (battle_config.feature_banking)
  1202. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1203. if (battle_config.feature_roulette) {
  1204. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1205. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1206. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1207. }
  1208. sd->roulette.prizeIdx = -1;
  1209. //SG map and mob read [Komurka]
  1210. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1211. uint16 j;
  1212. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1213. sd->feel_map[i].index = j;
  1214. sd->feel_map[i].m = map_mapindex2mapid(j);
  1215. } else {
  1216. sd->feel_map[i].index = 0;
  1217. sd->feel_map[i].m = -1;
  1218. }
  1219. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1220. }
  1221. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1222. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1223. sd->cloneskill_idx = skill_get_index(skid);
  1224. if (sd->cloneskill_idx > 0) {
  1225. sd->status.skill[sd->cloneskill_idx].id = skid;
  1226. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1227. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1228. sd->status.skill[sd->cloneskill_idx].lv = i;
  1229. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1230. }
  1231. }
  1232. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1233. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1234. sd->reproduceskill_idx = skill_get_index(skid);
  1235. if (sd->reproduceskill_idx > 0) {
  1236. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1237. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1238. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1239. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1240. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1241. }
  1242. }
  1243. //Weird... maybe registries were reloaded?
  1244. if (sd->state.active)
  1245. return;
  1246. sd->state.active = 1;
  1247. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1248. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1249. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1250. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1251. if (sd->status.party_id)
  1252. party_member_joined(sd);
  1253. if (sd->status.guild_id)
  1254. guild_member_joined(sd);
  1255. if( sd->status.clan_id )
  1256. clan_member_joined(sd);
  1257. // pet
  1258. if (sd->status.pet_id > 0)
  1259. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1260. // Homunculus [albator]
  1261. if( sd->status.hom_id > 0 )
  1262. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1263. if( sd->status.mer_id > 0 )
  1264. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1265. if( sd->status.ele_id > 0 )
  1266. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1267. map_addiddb(&sd->bl);
  1268. map_delnickdb(sd->status.char_id, sd->status.name);
  1269. if (!chrif_auth_finished(sd))
  1270. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1271. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1272. chrif_bsdata_request(sd->status.char_id);
  1273. #ifdef VIP_ENABLE
  1274. sd->vip.time = 0;
  1275. sd->vip.enabled = 0;
  1276. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1277. #endif
  1278. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1279. intif_request_questlog(sd);
  1280. if (battle_config.feature_achievement) {
  1281. sd->achievement_data.total_score = 0;
  1282. sd->achievement_data.level = 0;
  1283. sd->achievement_data.save = false;
  1284. sd->achievement_data.sendlist = false;
  1285. sd->achievement_data.count = 0;
  1286. sd->achievement_data.incompleteCount = 0;
  1287. sd->achievement_data.achievements = NULL;
  1288. intif_request_achievements(sd->status.char_id);
  1289. }
  1290. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1291. sd->state.connect_new = 1;
  1292. clif_parse_LoadEndAck(sd->fd, sd);
  1293. }
  1294. if( pc_isinvisible(sd) ) {
  1295. sd->vd.class_ = INVISIBLE_CLASS;
  1296. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1297. // decrement the number of pvp players on the map
  1298. map[sd->bl.m].users_pvp--;
  1299. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1300. // unregister the player for ranking
  1301. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1302. sd->pvp_timer = INVALID_TIMER;
  1303. }
  1304. clif_changeoption( &sd->bl );
  1305. }
  1306. channel_autojoin(sd);
  1307. }
  1308. static int pc_calc_skillpoint(struct map_session_data* sd)
  1309. {
  1310. uint16 i, skill_point = 0;
  1311. nullpo_ret(sd);
  1312. for(i = 1; i < MAX_SKILL; i++) {
  1313. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1314. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1315. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1316. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1317. )
  1318. {
  1319. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1320. skill_point += sd->status.skill[i].lv;
  1321. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1322. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1323. }
  1324. }
  1325. }
  1326. return skill_point;
  1327. }
  1328. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1329. uint16 i = 0;
  1330. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1331. return false;
  1332. /**
  1333. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1334. * skill tree and since they have no icons they'll give resource errors
  1335. * Get ALL skills except npc/guild ones. [Skotlex]
  1336. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1337. **/
  1338. for( i = 0; i < MAX_SKILL; i++ ) {
  1339. uint16 skill_id = skill_idx2id(i);
  1340. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1341. continue;
  1342. switch (skill_id) {
  1343. case SM_SELFPROVOKE:
  1344. case AB_DUPLELIGHT_MELEE:
  1345. case AB_DUPLELIGHT_MAGIC:
  1346. case WL_CHAINLIGHTNING_ATK:
  1347. case WL_TETRAVORTEX_FIRE:
  1348. case WL_TETRAVORTEX_WATER:
  1349. case WL_TETRAVORTEX_WIND:
  1350. case WL_TETRAVORTEX_GROUND:
  1351. case WL_SUMMON_ATK_FIRE:
  1352. case WL_SUMMON_ATK_WIND:
  1353. case WL_SUMMON_ATK_WATER:
  1354. case WL_SUMMON_ATK_GROUND:
  1355. case LG_OVERBRAND_BRANDISH:
  1356. case LG_OVERBRAND_PLUSATK:
  1357. case WM_SEVERE_RAINSTORM_MELEE:
  1358. case RL_R_TRIP_PLUSATK:
  1359. case SG_DEVIL:
  1360. case MO_TRIPLEATTACK:
  1361. case RG_SNATCHER:
  1362. continue;
  1363. default:
  1364. {
  1365. uint8 lv = (uint8)skill_get_max(skill_id);
  1366. if (lv > 0) {
  1367. sd->status.skill[i].id = skill_id;
  1368. if (addlv)
  1369. sd->status.skill[i].lv = lv;
  1370. }
  1371. }
  1372. break;
  1373. }
  1374. }
  1375. return true;
  1376. }
  1377. /*==========================================
  1378. * Calculation of skill level.
  1379. * @param sd
  1380. *------------------------------------------*/
  1381. void pc_calc_skilltree(struct map_session_data *sd)
  1382. {
  1383. int i, flag;
  1384. int c = 0;
  1385. nullpo_retv(sd);
  1386. i = pc_calc_skilltree_normalize_job(sd);
  1387. c = pc_mapid2jobid(i, sd->status.sex);
  1388. if( c == -1 )
  1389. { //Unable to normalize job??
  1390. 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);
  1391. return;
  1392. }
  1393. c = pc_class2idx(c);
  1394. for( i = 0; i < MAX_SKILL; i++ ) {
  1395. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1396. sd->status.skill[i].id = 0; //First clear skills.
  1397. /* permanent skills that must be re-checked */
  1398. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1399. uint16 sk_id = skill_idx2id(i);
  1400. if (!sk_id) {
  1401. sd->status.skill[i].id = 0;
  1402. sd->status.skill[i].lv = 0;
  1403. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1404. continue;
  1405. }
  1406. switch (sk_id) {
  1407. case NV_TRICKDEAD:
  1408. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1409. sd->status.skill[i].id = 0;
  1410. sd->status.skill[i].lv = 0;
  1411. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1412. }
  1413. break;
  1414. }
  1415. }
  1416. }
  1417. for( i = 0; i < MAX_SKILL; i++ ) {
  1418. uint16 skill_id = 0;
  1419. // Restore original level of skills after deleting earned skills.
  1420. 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 ) {
  1421. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1422. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1423. }
  1424. //Enable Bard/Dancer spirit linked skills.
  1425. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1426. continue;
  1427. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1428. //Link Dancer skills to bard.
  1429. if( sd->status.sex ) {
  1430. if( sd->status.skill[i-8].lv < 10 )
  1431. continue;
  1432. sd->status.skill[i].id = skill_id;
  1433. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1434. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1435. }
  1436. //Link Bard skills to dancer.
  1437. else {
  1438. if( sd->status.skill[i].lv < 10 )
  1439. continue;
  1440. sd->status.skill[i-8].id = skill_id - 8;
  1441. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1442. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1443. }
  1444. }
  1445. }
  1446. // Removes Taekwon Ranker skill bonus
  1447. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1448. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1449. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1450. uint16 sk_id = skill_tree[c_][i].skill_id;
  1451. uint16 sk_idx = 0;
  1452. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1453. continue;
  1454. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1455. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1456. continue;
  1457. sd->status.skill[sk_idx].id = 0;
  1458. }
  1459. }
  1460. }
  1461. // Grant all skills
  1462. pc_grant_allskills(sd, false);
  1463. do {
  1464. uint16 skid = 0;
  1465. flag = 0;
  1466. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1467. bool fail = false;
  1468. uint16 sk_idx = skill_get_index(skid);
  1469. if (sd->status.skill[sk_idx].id)
  1470. continue; //Skill already known.
  1471. if (!battle_config.skillfree) {
  1472. uint8 j;
  1473. // Checking required skills
  1474. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1475. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1476. uint16 sk_need_idx = 0;
  1477. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1478. short sk_need = sk_need_id;
  1479. 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)
  1480. sk_need = 0; //Not learned.
  1481. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1482. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1483. else
  1484. sk_need = pc_checkskill(sd,sk_need_id);
  1485. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1486. fail = true;
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1492. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1493. class_ = pc_class2idx(class_);
  1494. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1495. fail = true; // base level requirement wasn't satisfied
  1496. }
  1497. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1498. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1499. class_ = pc_class2idx(class_);
  1500. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1501. fail = true; // job level requirement wasn't satisfied
  1502. }
  1503. }
  1504. if (!fail) {
  1505. int inf2 = skill_get_inf2(skid);
  1506. if (!sd->status.skill[sk_idx].lv && (
  1507. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1508. inf2&INF2_WEDDING_SKILL ||
  1509. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1510. ))
  1511. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1512. sd->status.skill[sk_idx].id = skid;
  1513. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1514. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1515. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1516. }
  1517. flag = 1; // skill list has changed, perform another pass
  1518. }
  1519. }
  1520. } while(flag);
  1521. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1522. unsigned short skid = 0;
  1523. /* Taekwon Ranker Bonus Skill Tree
  1524. ============================================
  1525. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1526. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1527. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1528. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1529. uint16 sk_idx = 0;
  1530. if (!(sk_idx = skill_get_index(skid)))
  1531. continue;
  1532. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1533. continue; //Do not include Quest/Wedding skills.
  1534. if( sd->status.skill[sk_idx].id == 0 ) {
  1535. sd->status.skill[sk_idx].id = skid;
  1536. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1537. } else if( skid != NV_BASIC )
  1538. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1539. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1540. }
  1541. }
  1542. }
  1543. //Checks if you can learn a new skill after having leveled up a skill.
  1544. static void pc_check_skilltree(struct map_session_data *sd)
  1545. {
  1546. int i, flag = 0;
  1547. int c = 0;
  1548. if (battle_config.skillfree)
  1549. return; //Function serves no purpose if this is set
  1550. i = pc_calc_skilltree_normalize_job(sd);
  1551. c = pc_mapid2jobid(i, sd->status.sex);
  1552. if (c == -1) { //Unable to normalize job??
  1553. 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);
  1554. return;
  1555. }
  1556. c = pc_class2idx(c);
  1557. do {
  1558. uint16 skid = 0;
  1559. flag = 0;
  1560. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1561. uint16 sk_idx = skill_get_index(skid);
  1562. bool fail = false;
  1563. uint8 j = 0;
  1564. if (sd->status.skill[sk_idx].id) //Already learned
  1565. continue;
  1566. // Checking required skills
  1567. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1568. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1569. uint16 sk_need_idx = 0;
  1570. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1571. short sk_need = sk_need_id;
  1572. 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)
  1573. sk_need = 0; //Not learned.
  1574. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1575. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1576. else
  1577. sk_need = pc_checkskill(sd,sk_need_id);
  1578. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1579. fail = true;
  1580. break;
  1581. }
  1582. }
  1583. }
  1584. if( fail )
  1585. continue;
  1586. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1587. continue;
  1588. j = skill_get_inf2(skid);
  1589. if( !sd->status.skill[sk_idx].lv && (
  1590. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1591. j&INF2_WEDDING_SKILL ||
  1592. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1593. ) )
  1594. continue; //Cannot be learned via normal means.
  1595. sd->status.skill[sk_idx].id = skid;
  1596. flag = 1;
  1597. }
  1598. } while(flag);
  1599. }
  1600. // Make sure all the skills are in the correct condition
  1601. // before persisting to the backend.. [MouseJstr]
  1602. void pc_clean_skilltree(struct map_session_data *sd)
  1603. {
  1604. uint16 i;
  1605. for (i = 0; i < MAX_SKILL; i++){
  1606. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1607. sd->status.skill[i].id = 0;
  1608. sd->status.skill[i].lv = 0;
  1609. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1610. }
  1611. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1612. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1613. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1614. }
  1615. }
  1616. }
  1617. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1618. {
  1619. int skill_point, novice_skills;
  1620. int c = sd->class_;
  1621. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1622. return c;
  1623. skill_point = pc_calc_skillpoint(sd);
  1624. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1625. // limit 1st class and above to novice job levels
  1626. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1627. {
  1628. c = MAPID_NOVICE;
  1629. }
  1630. // limit 2nd class and above to first class job levels (super novices are exempt)
  1631. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1632. {
  1633. // regenerate change_level_2nd
  1634. if (!sd->change_level_2nd)
  1635. {
  1636. if (sd->class_&JOBL_THIRD)
  1637. {
  1638. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1639. if (!sd->change_level_3rd)
  1640. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1641. else
  1642. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1643. - (sd->status.job_level - 1)
  1644. - (sd->change_level_3rd - 1)
  1645. - novice_skills;
  1646. }
  1647. else
  1648. {
  1649. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1650. - (sd->status.job_level - 1)
  1651. - novice_skills;
  1652. }
  1653. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  1654. }
  1655. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1656. {
  1657. c &= MAPID_BASEMASK;
  1658. }
  1659. // limit 3rd class to 2nd class/trans job levels
  1660. else if(sd->class_&JOBL_THIRD)
  1661. {
  1662. // regenerate change_level_3rd
  1663. if (!sd->change_level_3rd)
  1664. {
  1665. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1666. - (sd->status.job_level - 1)
  1667. - (sd->change_level_2nd - 1)
  1668. - novice_skills;
  1669. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  1670. }
  1671. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1672. c &= MAPID_UPPERMASK;
  1673. }
  1674. }
  1675. // restore non-limiting flags
  1676. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1677. return c;
  1678. }
  1679. /*==========================================
  1680. * Updates the weight status
  1681. *------------------------------------------
  1682. * 1: overweight 50%
  1683. * 2: overweight 90%
  1684. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1685. */
  1686. void pc_updateweightstatus(struct map_session_data *sd)
  1687. {
  1688. int old_overweight;
  1689. int new_overweight;
  1690. nullpo_retv(sd);
  1691. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1692. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1693. if( old_overweight == new_overweight )
  1694. return; // no change
  1695. // stop old status change
  1696. if( old_overweight == 1 )
  1697. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1698. else if( old_overweight == 2 )
  1699. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1700. // start new status change
  1701. if( new_overweight == 1 )
  1702. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1703. else if( new_overweight == 2 )
  1704. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1705. // update overweight status
  1706. sd->regen.state.overweight = new_overweight;
  1707. }
  1708. int pc_disguise(struct map_session_data *sd, int class_)
  1709. {
  1710. if (!class_ && !sd->disguise)
  1711. return 0;
  1712. if (class_ && sd->disguise == class_)
  1713. return 0;
  1714. if(pc_isinvisible(sd))
  1715. { //Character is invisible. Stealth class-change. [Skotlex]
  1716. sd->disguise = class_; //viewdata is set on uncloaking.
  1717. return 2;
  1718. }
  1719. if (sd->bl.prev != NULL) {
  1720. pc_stop_walking(sd, 0);
  1721. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1722. }
  1723. if (!class_) {
  1724. sd->disguise = 0;
  1725. class_ = sd->status.class_;
  1726. } else
  1727. sd->disguise=class_;
  1728. status_set_viewdata(&sd->bl, class_);
  1729. clif_changeoption(&sd->bl);
  1730. if (sd->bl.prev != NULL) {
  1731. clif_spawn(&sd->bl);
  1732. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1733. { //It seems the cart info is lost on undisguise.
  1734. clif_cartlist(sd);
  1735. clif_updatestatus(sd,SP_CARTINFO);
  1736. }
  1737. if (sd->chatID) {
  1738. struct chat_data* cd;
  1739. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1740. clif_dispchat(cd,0);
  1741. }
  1742. }
  1743. return 1;
  1744. }
  1745. /// Show error message
  1746. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1747. /// Check for valid Element, break & show error message if invalid Element
  1748. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1749. /// Check for valid Race, break & show error message if invalid Race
  1750. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1751. /// Check for valid Race2, break & show error message if invalid Race2
  1752. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1753. /// Check for valid Class, break & show error message if invalid Class
  1754. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1755. /// Check for valid Size, break & show error message if invalid Size
  1756. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1757. /// Check for valid SC, break & show error message if invalid SC
  1758. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1759. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1760. {
  1761. uint8 i;
  1762. if( !rate )
  1763. return;
  1764. for( i = 0; i < max && spell[i].id; i++ )
  1765. {
  1766. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1767. {
  1768. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1769. return;
  1770. rate += spell[i].rate;
  1771. break;
  1772. }
  1773. }
  1774. if (i == max) {
  1775. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1776. return;
  1777. }
  1778. spell[i].id = id;
  1779. spell[i].lv = lv;
  1780. spell[i].rate = rate;
  1781. //Auto-update flag value.
  1782. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1783. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1784. if (!(flag&BF_SKILLMASK)) {
  1785. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1786. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1787. }
  1788. spell[i].flag|= flag;
  1789. spell[i].card_id = card_id;
  1790. }
  1791. 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)
  1792. {
  1793. uint8 i;
  1794. if( !rate )
  1795. return;
  1796. for( i = 0; i < max && spell[i].id; i++ )
  1797. {
  1798. ; // each autospell works independently
  1799. }
  1800. if( i == max )
  1801. {
  1802. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1803. return;
  1804. }
  1805. spell[i].flag = src_skill;
  1806. spell[i].id = id;
  1807. spell[i].lv = lv;
  1808. spell[i].rate = rate;
  1809. spell[i].card_id = card_id;
  1810. return;
  1811. }
  1812. /**
  1813. * Add inflict effect bonus for player while attacking/atatcked
  1814. * @param effect Effect array
  1815. * @param pmax Max array
  1816. * @param sc SC/Effect type
  1817. * @param rate Success chance
  1818. * @param arrow_rate success chance if bonus comes from arrow-type item
  1819. * @param flag Target flag
  1820. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1821. **/
  1822. 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)
  1823. {
  1824. uint16 i;
  1825. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1826. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1827. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1828. flag |= ATF_TARGET; //Default target: enemy.
  1829. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1830. flag |= ATF_WEAPON; //Default type: weapon.
  1831. if (!duration)
  1832. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1833. for (i = 0; i < pmax && effect[i].flag; i++) {
  1834. if (effect[i].sc == sc && effect[i].flag == flag) {
  1835. effect[i].rate += rate;
  1836. effect[i].arrow_rate += arrow_rate;
  1837. effect[i].duration = umax(effect[i].duration, duration);
  1838. return;
  1839. }
  1840. }
  1841. if (i == pmax) {
  1842. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1843. return;
  1844. }
  1845. effect[i].sc = sc;
  1846. effect[i].rate = rate;
  1847. effect[i].arrow_rate = arrow_rate;
  1848. effect[i].flag = flag;
  1849. effect[i].duration = duration;
  1850. }
  1851. /**
  1852. * Add inflict effect bonus for player while attacking using skill
  1853. * @param effect Effect array
  1854. * @param pmax Max array
  1855. * @param sc SC/Effect type
  1856. * @param rate Success chance
  1857. * @param flag Target flag
  1858. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1859. **/
  1860. 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)
  1861. {
  1862. uint8 i;
  1863. if (!duration)
  1864. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1865. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1866. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1867. effect[i].rate += rate;
  1868. effect[i].duration = umax(effect[i].duration, duration);
  1869. return;
  1870. }
  1871. }
  1872. if( i == pmax ) {
  1873. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1874. return;
  1875. }
  1876. effect[i].sc = sc;
  1877. effect[i].rate = rate;
  1878. effect[i].skill_id = skill_id;
  1879. effect[i].target = target;
  1880. effect[i].duration = duration;
  1881. }
  1882. /** Adjust/add drop rate modifier for player
  1883. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1884. * @param max: Max bonus can be received
  1885. * @param nameid: item id that will be dropped
  1886. * @param group: group id
  1887. * @param class_: target class
  1888. * @param race: target race. if < 0, means monster_id
  1889. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1890. */
  1891. 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)
  1892. {
  1893. uint8 i;
  1894. if (!nameid && !group) {
  1895. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1896. return;
  1897. }
  1898. if (nameid && !itemdb_exists(nameid)) {
  1899. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1900. return;
  1901. }
  1902. if (group && !itemdb_group_exists(group)) {
  1903. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1904. return;
  1905. }
  1906. //Apply config rate adjustment settings.
  1907. if (rate >= 0) { //Absolute drop.
  1908. if (battle_config.item_rate_adddrop != 100)
  1909. rate = rate*battle_config.item_rate_adddrop/100;
  1910. if (rate < battle_config.item_drop_adddrop_min)
  1911. rate = battle_config.item_drop_adddrop_min;
  1912. else if (rate > battle_config.item_drop_adddrop_max)
  1913. rate = battle_config.item_drop_adddrop_max;
  1914. } else { //Relative drop, max/min limits are applied at drop time.
  1915. if (battle_config.item_rate_adddrop != 100)
  1916. rate = rate*battle_config.item_rate_adddrop/100;
  1917. if (rate > -1)
  1918. rate = -1;
  1919. }
  1920. //Find match entry, and adjust the rate only
  1921. for (i = 0; i < max; i++) {
  1922. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1923. continue;
  1924. if (drop[i].nameid == nameid &&
  1925. drop[i].group == group &&
  1926. drop[i].race == race &&
  1927. drop[i].class_ == class_
  1928. )
  1929. {
  1930. //Adjust the rate if it has same classification
  1931. if ((rate < 0 && drop[i].rate < 0) ||
  1932. (rate > 0 && drop[i].rate > 0))
  1933. {
  1934. drop[i].rate += rate;
  1935. return;
  1936. }
  1937. }
  1938. }
  1939. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1940. if (i >= max) {
  1941. 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);
  1942. return;
  1943. }
  1944. drop[i].nameid = nameid;
  1945. drop[i].group = group;
  1946. drop[i].race = race;
  1947. drop[i].class_ = class_;
  1948. drop[i].rate = rate;
  1949. }
  1950. 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)
  1951. {
  1952. int i;
  1953. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1954. if( i == max )
  1955. {
  1956. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1957. return false;
  1958. }
  1959. if( !onskill )
  1960. {
  1961. if( !(flag&BF_RANGEMASK) )
  1962. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1963. if( !(flag&BF_WEAPONMASK) )
  1964. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1965. if( !(flag&BF_SKILLMASK) )
  1966. {
  1967. if( flag&(BF_MAGIC|BF_MISC) )
  1968. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1969. if( flag&BF_WEAPON )
  1970. flag|=BF_NORMAL|BF_SKILL;
  1971. }
  1972. }
  1973. bonus[i].rate = rate;
  1974. bonus[i].duration = dur;
  1975. bonus[i].active = INVALID_TIMER;
  1976. bonus[i].atk_type = flag;
  1977. bonus[i].pos = pos;
  1978. bonus[i].bonus_script = aStrdup(script);
  1979. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1980. return true;
  1981. }
  1982. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1983. {
  1984. int i;
  1985. if (!sd)
  1986. return;
  1987. for( i = 0; i < max; i++ )
  1988. {
  1989. if( autobonus[i].active != INVALID_TIMER )
  1990. {
  1991. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1992. {
  1993. if( autobonus[i].bonus_script )
  1994. {
  1995. int j;
  1996. unsigned int equip_pos_idx = 0;
  1997. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1998. for(j = 0; j < EQI_MAX; j++) {
  1999. if(sd->equip_index[j] >= 0)
  2000. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2001. }
  2002. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  2003. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  2004. }
  2005. continue;
  2006. }
  2007. else
  2008. { // Logout / Unequipped an item with an activated bonus
  2009. delete_timer(autobonus[i].active,pc_endautobonus);
  2010. autobonus[i].active = INVALID_TIMER;
  2011. }
  2012. }
  2013. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  2014. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  2015. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  2016. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  2017. autobonus[i].active = INVALID_TIMER;
  2018. }
  2019. }
  2020. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  2021. {
  2022. if (!sd || !autobonus)
  2023. return;
  2024. if( autobonus->other_script )
  2025. {
  2026. int j;
  2027. unsigned int equip_pos_idx = 0;
  2028. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2029. for(j = 0; j < EQI_MAX; j++) {
  2030. if(sd->equip_index[j] >= 0)
  2031. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2032. }
  2033. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2034. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2035. }
  2036. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  2037. sd->state.autobonus |= autobonus->pos;
  2038. status_calc_pc(sd,SCO_FORCE);
  2039. }
  2040. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  2041. {
  2042. struct map_session_data *sd = map_id2sd(id);
  2043. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2044. nullpo_ret(sd);
  2045. nullpo_ret(autobonus);
  2046. autobonus->active = INVALID_TIMER;
  2047. sd->state.autobonus &= ~autobonus->pos;
  2048. status_calc_pc(sd,SCO_FORCE);
  2049. return 0;
  2050. }
  2051. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2052. {
  2053. uint8 i;
  2054. struct weapon_data* wd;
  2055. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2056. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2057. if (i == MAX_PC_BONUS)
  2058. {
  2059. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2060. return;
  2061. }
  2062. if (!(flag&BF_RANGEMASK))
  2063. flag |= BF_SHORT|BF_LONG;
  2064. if (!(flag&BF_WEAPONMASK))
  2065. flag |= BF_WEAPON;
  2066. if (!(flag&BF_SKILLMASK))
  2067. {
  2068. if (flag&(BF_MAGIC|BF_MISC))
  2069. flag |= BF_SKILL;
  2070. if (flag&BF_WEAPON)
  2071. flag |= BF_NORMAL|BF_SKILL;
  2072. }
  2073. wd->addele2[i].ele = ele;
  2074. wd->addele2[i].rate = rate;
  2075. wd->addele2[i].flag = flag;
  2076. }
  2077. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2078. {
  2079. uint8 i;
  2080. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2081. if (i == MAX_PC_BONUS)
  2082. {
  2083. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2084. return;
  2085. }
  2086. if (!(flag&BF_RANGEMASK))
  2087. flag |= BF_SHORT|BF_LONG;
  2088. if (!(flag&BF_WEAPONMASK))
  2089. flag |= BF_WEAPON;
  2090. if (!(flag&BF_SKILLMASK))
  2091. {
  2092. if (flag&(BF_MAGIC|BF_MISC))
  2093. flag |= BF_SKILL;
  2094. if (flag&BF_WEAPON)
  2095. flag |= BF_NORMAL|BF_SKILL;
  2096. }
  2097. sd->subele2[i].ele = ele;
  2098. sd->subele2[i].rate = rate;
  2099. sd->subele2[i].flag = flag;
  2100. }
  2101. /** Add item group heal rate bonus to player
  2102. * @param sd Player
  2103. * @param group_id Item Group ID
  2104. * @param rate
  2105. * @author Cydh
  2106. */
  2107. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2108. struct s_pc_itemgrouphealrate *entry;
  2109. uint8 i;
  2110. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2111. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2112. break;
  2113. }
  2114. if (i != sd->itemgrouphealrate_count) {
  2115. sd->itemgrouphealrate[i]->rate += rate;
  2116. return;
  2117. }
  2118. if (i >= UINT8_MAX) {
  2119. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2120. return;
  2121. }
  2122. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2123. entry->group_id = group_id;
  2124. entry->rate = rate;
  2125. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2126. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2127. }
  2128. /** Clear item group heal rate from player
  2129. * @param sd Player
  2130. * @author Cydh
  2131. */
  2132. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2133. if (!sd || !sd->itemgrouphealrate_count)
  2134. return;
  2135. else {
  2136. uint8 i;
  2137. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2138. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2139. sd->itemgrouphealrate_count = 0;
  2140. aFree(sd->itemgrouphealrate);
  2141. sd->itemgrouphealrate = NULL;
  2142. }
  2143. }
  2144. /*==========================================
  2145. * Add a bonus(type) to player sd
  2146. * format: bonus bBonusName,val;
  2147. * @param sd
  2148. * @param type Bonus type used by bBonusName
  2149. * @param val Value that usually for rate or fixed value
  2150. *------------------------------------------*/
  2151. void pc_bonus(struct map_session_data *sd,int type,int val)
  2152. {
  2153. struct status_data *status;
  2154. int bonus;
  2155. nullpo_retv(sd);
  2156. status = &sd->base_status;
  2157. switch(type){
  2158. case SP_STR:
  2159. case SP_AGI:
  2160. case SP_VIT:
  2161. case SP_INT:
  2162. case SP_DEX:
  2163. case SP_LUK:
  2164. if(sd->state.lr_flag != 2)
  2165. sd->param_bonus[type-SP_STR]+=val;
  2166. break;
  2167. case SP_ATK1:
  2168. if(!sd->state.lr_flag) {
  2169. bonus = status->rhw.atk + val;
  2170. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2171. }
  2172. else if(sd->state.lr_flag == 1) {
  2173. bonus = status->lhw.atk + val;
  2174. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2175. }
  2176. break;
  2177. case SP_ATK2:
  2178. if(!sd->state.lr_flag) {
  2179. bonus = status->rhw.atk2 + val;
  2180. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2181. }
  2182. else if(sd->state.lr_flag == 1) {
  2183. bonus = status->lhw.atk2 + val;
  2184. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2185. }
  2186. break;
  2187. case SP_BASE_ATK:
  2188. if(sd->state.lr_flag != 2) {
  2189. #ifdef RENEWAL
  2190. bonus = sd->bonus.eatk + val;
  2191. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2192. #else
  2193. bonus = status->batk + val;
  2194. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2195. #endif
  2196. }
  2197. break;
  2198. case SP_DEF1:
  2199. if(sd->state.lr_flag != 2) {
  2200. bonus = status->def + val;
  2201. #ifdef RENEWAL
  2202. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2203. #else
  2204. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2205. #endif
  2206. }
  2207. break;
  2208. case SP_DEF2:
  2209. if(sd->state.lr_flag != 2) {
  2210. bonus = status->def2 + val;
  2211. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2212. }
  2213. break;
  2214. case SP_MDEF1:
  2215. if(sd->state.lr_flag != 2) {
  2216. bonus = status->mdef + val;
  2217. #ifdef RENEWAL
  2218. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2219. #else
  2220. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2221. #endif
  2222. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2223. sd->bonus.shieldmdef += bonus;
  2224. }
  2225. }
  2226. break;
  2227. case SP_MDEF2:
  2228. if(sd->state.lr_flag != 2) {
  2229. bonus = status->mdef2 + val;
  2230. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2231. }
  2232. break;
  2233. case SP_HIT:
  2234. if(sd->state.lr_flag != 2) {
  2235. bonus = status->hit + val;
  2236. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2237. } else
  2238. sd->bonus.arrow_hit+=val;
  2239. break;
  2240. case SP_FLEE1:
  2241. if(sd->state.lr_flag != 2) {
  2242. bonus = status->flee + val;
  2243. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2244. }
  2245. break;
  2246. case SP_FLEE2:
  2247. if(sd->state.lr_flag != 2) {
  2248. bonus = status->flee2 + val*10;
  2249. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2250. }
  2251. break;
  2252. case SP_CRITICAL:
  2253. if(sd->state.lr_flag != 2) {
  2254. bonus = status->cri + val*10;
  2255. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2256. } else
  2257. sd->bonus.arrow_cri += val*10;
  2258. break;
  2259. case SP_ATKELE:
  2260. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2261. switch (sd->state.lr_flag)
  2262. {
  2263. case 2:
  2264. switch (sd->status.weapon) {
  2265. case W_BOW:
  2266. case W_REVOLVER:
  2267. case W_RIFLE:
  2268. case W_GATLING:
  2269. case W_SHOTGUN:
  2270. case W_GRENADE:
  2271. //Become weapon element.
  2272. status->rhw.ele=val;
  2273. break;
  2274. default: //Become arrow element.
  2275. sd->bonus.arrow_ele=val;
  2276. break;
  2277. }
  2278. break;
  2279. case 1:
  2280. status->lhw.ele=val;
  2281. break;
  2282. default:
  2283. status->rhw.ele=val;
  2284. break;
  2285. }
  2286. break;
  2287. case SP_DEFELE:
  2288. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2289. if(sd->state.lr_flag != 2)
  2290. status->def_ele=val;
  2291. break;
  2292. case SP_MAXHP:
  2293. if(sd->state.lr_flag == 2)
  2294. break;
  2295. sd->bonus.hp += val;
  2296. break;
  2297. case SP_MAXSP:
  2298. if(sd->state.lr_flag == 2)
  2299. break;
  2300. sd->bonus.sp += val;
  2301. break;
  2302. case SP_MAXHPRATE:
  2303. if(sd->state.lr_flag != 2)
  2304. sd->hprate+=val;
  2305. break;
  2306. case SP_MAXSPRATE:
  2307. if(sd->state.lr_flag != 2)
  2308. sd->sprate+=val;
  2309. break;
  2310. case SP_SPRATE:
  2311. if(sd->state.lr_flag != 2)
  2312. sd->dsprate+=val;
  2313. break;
  2314. case SP_ATTACKRANGE:
  2315. switch (sd->state.lr_flag) {
  2316. case 2:
  2317. switch (sd->status.weapon) {
  2318. case W_BOW:
  2319. case W_REVOLVER:
  2320. case W_RIFLE:
  2321. case W_GATLING:
  2322. case W_SHOTGUN:
  2323. case W_GRENADE:
  2324. status->rhw.range += val;
  2325. }
  2326. break;
  2327. case 1:
  2328. status->lhw.range += val;
  2329. break;
  2330. default:
  2331. status->rhw.range += val;
  2332. break;
  2333. }
  2334. break;
  2335. case SP_SPEED_RATE: //Non stackable increase
  2336. if(sd->state.lr_flag != 2)
  2337. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2338. break;
  2339. case SP_SPEED_ADDRATE: //Stackable increase
  2340. if(sd->state.lr_flag != 2)
  2341. sd->bonus.speed_add_rate -= val;
  2342. break;
  2343. case SP_ASPD: //Raw increase
  2344. if(sd->state.lr_flag != 2)
  2345. sd->bonus.aspd_add -= 10*val;
  2346. break;
  2347. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2348. if(sd->state.lr_flag != 2)
  2349. #ifndef RENEWAL_ASPD
  2350. status->aspd_rate -= 10*val;
  2351. #else
  2352. status->aspd_rate2 += val;
  2353. #endif
  2354. break;
  2355. case SP_HP_RECOV_RATE:
  2356. if(sd->state.lr_flag != 2)
  2357. sd->hprecov_rate += val;
  2358. break;
  2359. case SP_SP_RECOV_RATE:
  2360. if(sd->state.lr_flag != 2)
  2361. sd->sprecov_rate += val;
  2362. break;
  2363. case SP_CRITICAL_DEF:
  2364. if(sd->state.lr_flag != 2)
  2365. sd->bonus.critical_def += val;
  2366. break;
  2367. case SP_NEAR_ATK_DEF:
  2368. if(sd->state.lr_flag != 2)
  2369. sd->bonus.near_attack_def_rate += val;
  2370. break;
  2371. case SP_LONG_ATK_DEF:
  2372. if(sd->state.lr_flag != 2)
  2373. sd->bonus.long_attack_def_rate += val;
  2374. break;
  2375. case SP_DOUBLE_RATE:
  2376. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2377. sd->bonus.double_rate = val;
  2378. break;
  2379. case SP_DOUBLE_ADD_RATE:
  2380. if(sd->state.lr_flag == 0)
  2381. sd->bonus.double_add_rate += val;
  2382. break;
  2383. case SP_MATK_RATE:
  2384. if(sd->state.lr_flag != 2)
  2385. sd->matk_rate += val;
  2386. break;
  2387. case SP_IGNORE_DEF_ELE:
  2388. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2389. if(!sd->state.lr_flag)
  2390. sd->right_weapon.ignore_def_ele |= 1<<val;
  2391. else if(sd->state.lr_flag == 1)
  2392. sd->left_weapon.ignore_def_ele |= 1<<val;
  2393. break;
  2394. case SP_IGNORE_DEF_RACE:
  2395. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2396. if(!sd->state.lr_flag)
  2397. sd->right_weapon.ignore_def_race |= 1<<val;
  2398. else if(sd->state.lr_flag == 1)
  2399. sd->left_weapon.ignore_def_race |= 1<<val;
  2400. break;
  2401. case SP_IGNORE_DEF_CLASS:
  2402. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2403. if(!sd->state.lr_flag)
  2404. sd->right_weapon.ignore_def_class |= 1<<val;
  2405. else if(sd->state.lr_flag == 1)
  2406. sd->left_weapon.ignore_def_class |= 1<<val;
  2407. break;
  2408. case SP_ATK_RATE:
  2409. if(sd->state.lr_flag != 2)
  2410. sd->bonus.atk_rate += val;
  2411. break;
  2412. case SP_MAGIC_ATK_DEF:
  2413. if(sd->state.lr_flag != 2)
  2414. sd->bonus.magic_def_rate += val;
  2415. break;
  2416. case SP_MISC_ATK_DEF:
  2417. if(sd->state.lr_flag != 2)
  2418. sd->bonus.misc_def_rate += val;
  2419. break;
  2420. case SP_IGNORE_MDEF_ELE:
  2421. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2422. if(sd->state.lr_flag != 2)
  2423. sd->bonus.ignore_mdef_ele |= 1<<val;
  2424. break;
  2425. case SP_IGNORE_MDEF_RACE:
  2426. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2427. if(sd->state.lr_flag != 2)
  2428. sd->bonus.ignore_mdef_race |= 1<<val;
  2429. break;
  2430. case SP_PERFECT_HIT_RATE:
  2431. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2432. sd->bonus.perfect_hit = val;
  2433. break;
  2434. case SP_PERFECT_HIT_ADD_RATE:
  2435. if(sd->state.lr_flag != 2)
  2436. sd->bonus.perfect_hit_add += val;
  2437. break;
  2438. case SP_CRITICAL_RATE:
  2439. if(sd->state.lr_flag != 2)
  2440. sd->critical_rate+=val;
  2441. break;
  2442. case SP_DEF_RATIO_ATK_ELE:
  2443. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2444. if(!sd->state.lr_flag)
  2445. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2446. else if(sd->state.lr_flag == 1)
  2447. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2448. break;
  2449. case SP_DEF_RATIO_ATK_RACE:
  2450. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2451. if(!sd->state.lr_flag)
  2452. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2453. else if(sd->state.lr_flag == 1)
  2454. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2455. break;
  2456. case SP_DEF_RATIO_ATK_CLASS:
  2457. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2458. if(!sd->state.lr_flag)
  2459. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2460. else if(sd->state.lr_flag == 1)
  2461. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2462. break;
  2463. case SP_HIT_RATE:
  2464. if(sd->state.lr_flag != 2)
  2465. sd->hit_rate += val;
  2466. break;
  2467. case SP_FLEE_RATE:
  2468. if(sd->state.lr_flag != 2)
  2469. sd->flee_rate += val;
  2470. break;
  2471. case SP_FLEE2_RATE:
  2472. if(sd->state.lr_flag != 2)
  2473. sd->flee2_rate += val;
  2474. break;
  2475. case SP_DEF_RATE:
  2476. if(sd->state.lr_flag != 2)
  2477. sd->def_rate += val;
  2478. break;
  2479. case SP_DEF2_RATE:
  2480. if(sd->state.lr_flag != 2)
  2481. sd->def2_rate += val;
  2482. break;
  2483. case SP_MDEF_RATE:
  2484. if(sd->state.lr_flag != 2)
  2485. sd->mdef_rate += val;
  2486. break;
  2487. case SP_MDEF2_RATE:
  2488. if(sd->state.lr_flag != 2)
  2489. sd->mdef2_rate += val;
  2490. break;
  2491. case SP_RESTART_FULL_RECOVER:
  2492. if(sd->state.lr_flag != 2)
  2493. sd->special_state.restart_full_recover = 1;
  2494. break;
  2495. case SP_NO_CASTCANCEL:
  2496. if(sd->state.lr_flag != 2)
  2497. sd->special_state.no_castcancel = 1;
  2498. break;
  2499. case SP_NO_CASTCANCEL2:
  2500. if(sd->state.lr_flag != 2)
  2501. sd->special_state.no_castcancel2 = 1;
  2502. break;
  2503. case SP_NO_SIZEFIX:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->special_state.no_sizefix = 1;
  2506. break;
  2507. case SP_NO_MAGIC_DAMAGE:
  2508. if(sd->state.lr_flag == 2)
  2509. break;
  2510. val+= sd->special_state.no_magic_damage;
  2511. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2512. break;
  2513. case SP_NO_WEAPON_DAMAGE:
  2514. if(sd->state.lr_flag == 2)
  2515. break;
  2516. val+= sd->special_state.no_weapon_damage;
  2517. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2518. break;
  2519. case SP_NO_MISC_DAMAGE:
  2520. if(sd->state.lr_flag == 2)
  2521. break;
  2522. val+= sd->special_state.no_misc_damage;
  2523. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2524. break;
  2525. case SP_NO_GEMSTONE:
  2526. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2527. sd->special_state.no_gemstone = 1;
  2528. break;
  2529. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2530. if(sd->state.lr_flag != 2) {
  2531. sd->special_state.intravision = 1;
  2532. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2533. }
  2534. break;
  2535. case SP_NO_KNOCKBACK:
  2536. if(sd->state.lr_flag != 2)
  2537. sd->special_state.no_knockback = 1;
  2538. break;
  2539. case SP_SPLASH_RANGE:
  2540. if(sd->bonus.splash_range < val)
  2541. sd->bonus.splash_range = val;
  2542. break;
  2543. case SP_SPLASH_ADD_RANGE:
  2544. sd->bonus.splash_add_range += val;
  2545. break;
  2546. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2547. if(sd->state.lr_flag != 2)
  2548. sd->bonus.short_weapon_damage_return += val;
  2549. break;
  2550. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2551. if(sd->state.lr_flag != 2)
  2552. sd->bonus.long_weapon_damage_return += val;
  2553. break;
  2554. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2555. if(sd->state.lr_flag != 2)
  2556. sd->bonus.magic_damage_return += val;
  2557. break;
  2558. case SP_ALL_STATS: // [Valaris]
  2559. if(sd->state.lr_flag!=2) {
  2560. sd->param_bonus[SP_STR-SP_STR]+=val;
  2561. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2562. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2563. sd->param_bonus[SP_INT-SP_STR]+=val;
  2564. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2565. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2566. }
  2567. break;
  2568. case SP_AGI_VIT: // [Valaris]
  2569. if(sd->state.lr_flag!=2) {
  2570. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2571. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2572. }
  2573. break;
  2574. case SP_AGI_DEX_STR: // [Valaris]
  2575. if(sd->state.lr_flag!=2) {
  2576. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2577. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2578. sd->param_bonus[SP_STR-SP_STR]+=val;
  2579. }
  2580. break;
  2581. case SP_PERFECT_HIDE: // [Valaris]
  2582. if(sd->state.lr_flag!=2)
  2583. sd->special_state.perfect_hiding=1;
  2584. break;
  2585. case SP_UNBREAKABLE:
  2586. if(sd->state.lr_flag!=2)
  2587. sd->bonus.unbreakable += val;
  2588. break;
  2589. case SP_UNBREAKABLE_WEAPON:
  2590. if(sd->state.lr_flag != 2)
  2591. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2592. break;
  2593. case SP_UNBREAKABLE_ARMOR:
  2594. if(sd->state.lr_flag != 2)
  2595. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2596. break;
  2597. case SP_UNBREAKABLE_HELM:
  2598. if(sd->state.lr_flag != 2)
  2599. sd->bonus.unbreakable_equip |= EQP_HELM;
  2600. break;
  2601. case SP_UNBREAKABLE_SHIELD:
  2602. if(sd->state.lr_flag != 2)
  2603. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2604. break;
  2605. case SP_UNBREAKABLE_GARMENT:
  2606. if(sd->state.lr_flag != 2)
  2607. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2608. break;
  2609. case SP_UNBREAKABLE_SHOES:
  2610. if(sd->state.lr_flag != 2)
  2611. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2612. break;
  2613. case SP_CLASSCHANGE: // [Valaris]
  2614. if(sd->state.lr_flag !=2)
  2615. sd->bonus.classchange=val;
  2616. break;
  2617. case SP_LONG_ATK_RATE:
  2618. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2619. sd->bonus.long_attack_atk_rate+=val;
  2620. break;
  2621. case SP_BREAK_WEAPON_RATE:
  2622. if(sd->state.lr_flag != 2)
  2623. sd->bonus.break_weapon_rate+=val;
  2624. break;
  2625. case SP_BREAK_ARMOR_RATE:
  2626. if(sd->state.lr_flag != 2)
  2627. sd->bonus.break_armor_rate+=val;
  2628. break;
  2629. case SP_ADD_STEAL_RATE:
  2630. if(sd->state.lr_flag != 2)
  2631. sd->bonus.add_steal_rate+=val;
  2632. break;
  2633. case SP_DELAYRATE:
  2634. if(sd->state.lr_flag != 2)
  2635. sd->delayrate+=val;
  2636. break;
  2637. case SP_CRIT_ATK_RATE:
  2638. if(sd->state.lr_flag != 2)
  2639. sd->bonus.crit_atk_rate += val;
  2640. break;
  2641. case SP_NO_REGEN:
  2642. if(sd->state.lr_flag != 2)
  2643. sd->regen.state.block|=val;
  2644. break;
  2645. case SP_UNSTRIPABLE_WEAPON:
  2646. if(sd->state.lr_flag != 2)
  2647. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2648. break;
  2649. case SP_UNSTRIPABLE:
  2650. case SP_UNSTRIPABLE_ARMOR:
  2651. if(sd->state.lr_flag != 2)
  2652. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2653. break;
  2654. case SP_UNSTRIPABLE_HELM:
  2655. if(sd->state.lr_flag != 2)
  2656. sd->bonus.unstripable_equip |= EQP_HELM;
  2657. break;
  2658. case SP_UNSTRIPABLE_SHIELD:
  2659. if(sd->state.lr_flag != 2)
  2660. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2661. break;
  2662. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2663. if(!sd->state.lr_flag) {
  2664. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2665. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2666. } else if(sd->state.lr_flag == 1) {
  2667. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2668. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2669. }
  2670. break;
  2671. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2672. if(!sd->state.lr_flag) {
  2673. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2674. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2675. } else if(sd->state.lr_flag == 1) {
  2676. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2677. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2678. }
  2679. break;
  2680. case SP_SP_GAIN_VALUE:
  2681. if(!sd->state.lr_flag)
  2682. sd->bonus.sp_gain_value += val;
  2683. break;
  2684. case SP_HP_GAIN_VALUE:
  2685. if(!sd->state.lr_flag)
  2686. sd->bonus.hp_gain_value += val;
  2687. break;
  2688. case SP_MAGIC_SP_GAIN_VALUE:
  2689. if(!sd->state.lr_flag)
  2690. sd->bonus.magic_sp_gain_value += val;
  2691. break;
  2692. case SP_MAGIC_HP_GAIN_VALUE:
  2693. if(!sd->state.lr_flag)
  2694. sd->bonus.magic_hp_gain_value += val;
  2695. break;
  2696. case SP_ADD_HEAL_RATE:
  2697. if(sd->state.lr_flag != 2)
  2698. sd->bonus.add_heal_rate += val;
  2699. break;
  2700. case SP_ADD_HEAL2_RATE:
  2701. if(sd->state.lr_flag != 2)
  2702. sd->bonus.add_heal2_rate += val;
  2703. break;
  2704. case SP_ADD_ITEM_HEAL_RATE:
  2705. if(sd->state.lr_flag != 2)
  2706. sd->bonus.itemhealrate2 += val;
  2707. break;
  2708. case SP_EMATK:
  2709. if(sd->state.lr_flag != 2)
  2710. sd->bonus.ematk += val;
  2711. break;
  2712. #ifdef RENEWAL_CAST
  2713. case SP_FIXCASTRATE:
  2714. if(sd->state.lr_flag != 2)
  2715. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2716. break;
  2717. case SP_ADD_FIXEDCAST:
  2718. if(sd->state.lr_flag != 2)
  2719. sd->bonus.add_fixcast += val;
  2720. break;
  2721. case SP_CASTRATE:
  2722. case SP_VARCASTRATE:
  2723. if(sd->state.lr_flag != 2)
  2724. sd->bonus.varcastrate -= val;
  2725. break;
  2726. case SP_ADD_VARIABLECAST:
  2727. if(sd->state.lr_flag != 2)
  2728. sd->bonus.add_varcast += val;
  2729. break;
  2730. #else
  2731. case SP_ADD_FIXEDCAST:
  2732. case SP_FIXCASTRATE:
  2733. case SP_ADD_VARIABLECAST:
  2734. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2735. break;
  2736. case SP_VARCASTRATE:
  2737. case SP_CASTRATE:
  2738. if(sd->state.lr_flag != 2)
  2739. sd->castrate += val;
  2740. break;
  2741. #endif
  2742. case SP_ADDMAXWEIGHT:
  2743. if (sd->state.lr_flag != 2)
  2744. sd->add_max_weight += val;
  2745. break;
  2746. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2747. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2748. break;
  2749. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2750. if (sd->state.lr_flag != 2)
  2751. sd->bonus.critical_rangeatk += val*10;
  2752. else
  2753. sd->bonus.arrow_cri += val*10;
  2754. break;
  2755. case SP_WEAPON_ATK_RATE:
  2756. if (sd->state.lr_flag != 2)
  2757. sd->bonus.weapon_atk_rate += val;
  2758. break;
  2759. case SP_WEAPON_MATK_RATE:
  2760. if (sd->state.lr_flag != 2)
  2761. sd->bonus.weapon_matk_rate += val;
  2762. break;
  2763. case SP_NO_MADO_FUEL:
  2764. if (sd->state.lr_flag != 2)
  2765. sd->special_state.no_mado_fuel = 1;
  2766. break;
  2767. default:
  2768. if (running_npc_stat_calc_event) {
  2769. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2770. }
  2771. else if (current_equip_combo_pos > 0) {
  2772. 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);
  2773. }
  2774. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2775. 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);
  2776. }
  2777. else {
  2778. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2779. }
  2780. break;
  2781. }
  2782. }
  2783. /*==========================================
  2784. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2785. * format: bonus2 bBonusName,type2,val;
  2786. * @param sd
  2787. * @param type Bonus type used by bBonusName
  2788. * @param type2
  2789. * @param val Value that usually for rate or fixed value
  2790. *------------------------------------------*/
  2791. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2792. {
  2793. int i;
  2794. nullpo_retv(sd);
  2795. switch(type){
  2796. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2797. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2798. if(!sd->state.lr_flag)
  2799. sd->right_weapon.addele[type2]+=val;
  2800. else if(sd->state.lr_flag == 1)
  2801. sd->left_weapon.addele[type2]+=val;
  2802. else if(sd->state.lr_flag == 2)
  2803. sd->arrow_addele[type2]+=val;
  2804. break;
  2805. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2806. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2807. if(!sd->state.lr_flag)
  2808. sd->right_weapon.addrace[type2]+=val;
  2809. else if(sd->state.lr_flag == 1)
  2810. sd->left_weapon.addrace[type2]+=val;
  2811. else if(sd->state.lr_flag == 2)
  2812. sd->arrow_addrace[type2]+=val;
  2813. break;
  2814. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2815. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2816. if(!sd->state.lr_flag)
  2817. sd->right_weapon.addclass[type2]+=val;
  2818. else if(sd->state.lr_flag == 1)
  2819. sd->left_weapon.addclass[type2]+=val;
  2820. else if(sd->state.lr_flag == 2)
  2821. sd->arrow_addclass[type2]+=val;
  2822. break;
  2823. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2824. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2825. if(!sd->state.lr_flag)
  2826. sd->right_weapon.addsize[type2]+=val;
  2827. else if(sd->state.lr_flag == 1)
  2828. sd->left_weapon.addsize[type2]+=val;
  2829. else if(sd->state.lr_flag == 2)
  2830. sd->arrow_addsize[type2]+=val;
  2831. break;
  2832. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2833. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2834. if(sd->state.lr_flag != 2)
  2835. sd->subele_script[type2] += val;
  2836. break;
  2837. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2838. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2839. if(sd->state.lr_flag != 2)
  2840. sd->subrace[type2]+=val;
  2841. break;
  2842. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2843. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2844. if(sd->state.lr_flag != 2)
  2845. sd->subclass[type2]+=val;
  2846. break;
  2847. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2848. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2849. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2850. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2851. break;
  2852. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2853. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2854. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2855. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2856. break;
  2857. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2858. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2859. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2860. break;
  2861. }
  2862. if(sd->state.lr_flag == 2)
  2863. break;
  2864. i = sd->reseff[type2]+val;
  2865. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2866. break;
  2867. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2868. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2869. if(sd->state.lr_flag != 2)
  2870. sd->magic_addele_script[type2] += val;
  2871. break;
  2872. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2873. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2874. if(sd->state.lr_flag != 2)
  2875. sd->magic_addrace[type2]+=val;
  2876. break;
  2877. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2878. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2879. if(sd->state.lr_flag != 2)
  2880. sd->magic_addclass[type2]+=val;
  2881. break;
  2882. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2883. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2884. if(sd->state.lr_flag != 2)
  2885. sd->magic_addsize[type2]+=val;
  2886. break;
  2887. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2888. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2889. if(sd->state.lr_flag != 2)
  2890. sd->magic_atk_ele[type2]+=val;
  2891. break;
  2892. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2893. switch (sd->state.lr_flag) {
  2894. case 0: //Right hand
  2895. 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);
  2896. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2897. {
  2898. 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));
  2899. break;
  2900. }
  2901. sd->right_weapon.add_dmg[i].class_ = type2;
  2902. sd->right_weapon.add_dmg[i].rate += val;
  2903. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2904. 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]));
  2905. break;
  2906. case 1: //Left hand
  2907. 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);
  2908. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2909. {
  2910. 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));
  2911. break;
  2912. }
  2913. sd->left_weapon.add_dmg[i].class_ = type2;
  2914. sd->left_weapon.add_dmg[i].rate += val;
  2915. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2916. 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]));
  2917. break;
  2918. }
  2919. break;
  2920. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2921. if(sd->state.lr_flag == 2)
  2922. break;
  2923. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2924. if (i == ARRAYLENGTH(sd->add_mdmg))
  2925. {
  2926. 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));
  2927. break;
  2928. }
  2929. sd->add_mdmg[i].class_ = type2;
  2930. sd->add_mdmg[i].rate += val;
  2931. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2932. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2933. break;
  2934. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2935. if(sd->state.lr_flag == 2)
  2936. break;
  2937. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2938. if (i == ARRAYLENGTH(sd->add_def))
  2939. {
  2940. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2941. break;
  2942. }
  2943. sd->add_def[i].class_ = type2;
  2944. sd->add_def[i].rate += val;
  2945. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2946. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2947. break;
  2948. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2949. if(sd->state.lr_flag == 2)
  2950. break;
  2951. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2952. if (i == ARRAYLENGTH(sd->add_mdef))
  2953. {
  2954. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2955. break;
  2956. }
  2957. sd->add_mdef[i].class_ = type2;
  2958. sd->add_mdef[i].rate += val;
  2959. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2960. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2961. break;
  2962. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2963. if(!sd->state.lr_flag) {
  2964. sd->right_weapon.hp_drain_rate.rate += type2;
  2965. sd->right_weapon.hp_drain_rate.per += val;
  2966. }
  2967. else if(sd->state.lr_flag == 1) {
  2968. sd->left_weapon.hp_drain_rate.rate += type2;
  2969. sd->left_weapon.hp_drain_rate.per += val;
  2970. }
  2971. break;
  2972. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2973. if(!sd->state.lr_flag) {
  2974. sd->right_weapon.sp_drain_rate.rate += type2;
  2975. sd->right_weapon.sp_drain_rate.per += val;
  2976. }
  2977. else if(sd->state.lr_flag == 1) {
  2978. sd->left_weapon.sp_drain_rate.rate += type2;
  2979. sd->left_weapon.sp_drain_rate.per += val;
  2980. }
  2981. break;
  2982. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2983. if(sd->state.lr_flag != 2) {
  2984. sd->bonus.sp_vanish_rate += type2;
  2985. sd->bonus.sp_vanish_per += val;
  2986. }
  2987. break;
  2988. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2989. if(sd->state.lr_flag != 2) {
  2990. sd->bonus.hp_vanish_rate += type2;
  2991. sd->bonus.hp_vanish_per += val;
  2992. }
  2993. break;
  2994. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2995. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2996. sd->bonus.get_zeny_rate = val;
  2997. sd->bonus.get_zeny_num = type2;
  2998. }
  2999. break;
  3000. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3001. if(sd->state.lr_flag != 2) {
  3002. sd->bonus.get_zeny_rate += val;
  3003. sd->bonus.get_zeny_num += type2;
  3004. }
  3005. break;
  3006. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3007. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3008. if(sd->state.lr_flag == 2)
  3009. break;
  3010. sd->weapon_coma_ele[type2] += val;
  3011. sd->special_state.bonus_coma = 1;
  3012. break;
  3013. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3014. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3015. if(sd->state.lr_flag == 2)
  3016. break;
  3017. sd->weapon_coma_race[type2] += val;
  3018. sd->special_state.bonus_coma = 1;
  3019. break;
  3020. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3021. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3022. if(sd->state.lr_flag == 2)
  3023. break;
  3024. sd->weapon_coma_class[type2] += val;
  3025. sd->special_state.bonus_coma = 1;
  3026. break;
  3027. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3028. if(sd->state.lr_flag != 2)
  3029. sd->weapon_atk[type2]+=val;
  3030. break;
  3031. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3032. if(sd->state.lr_flag != 2)
  3033. sd->weapon_damage_rate[type2]+=val;
  3034. break;
  3035. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3036. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3037. if(sd->state.lr_flag != 2)
  3038. sd->critaddrace[type2] += val*10;
  3039. break;
  3040. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3041. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3042. if(sd->state.lr_flag != 2)
  3043. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  3044. break;
  3045. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3046. if(sd->state.lr_flag == 2)
  3047. break;
  3048. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  3049. if (i == ARRAYLENGTH(sd->skillatk))
  3050. { //Better mention this so the array length can be updated. [Skotlex]
  3051. 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);
  3052. break;
  3053. }
  3054. if (sd->skillatk[i].id == type2)
  3055. sd->skillatk[i].val += val;
  3056. else {
  3057. sd->skillatk[i].id = type2;
  3058. sd->skillatk[i].val = val;
  3059. }
  3060. break;
  3061. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3062. if(sd->state.lr_flag == 2)
  3063. break;
  3064. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3065. if (i == ARRAYLENGTH(sd->skillheal))
  3066. { // Better mention this so the array length can be updated. [Skotlex]
  3067. 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);
  3068. break;
  3069. }
  3070. if (sd->skillheal[i].id == type2)
  3071. sd->skillheal[i].val += val;
  3072. else {
  3073. sd->skillheal[i].id = type2;
  3074. sd->skillheal[i].val = val;
  3075. }
  3076. break;
  3077. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3078. if(sd->state.lr_flag == 2)
  3079. break;
  3080. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3081. if (i == ARRAYLENGTH(sd->skillheal2))
  3082. { // Better mention this so the array length can be updated. [Skotlex]
  3083. 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);
  3084. break;
  3085. }
  3086. if (sd->skillheal2[i].id == type2)
  3087. sd->skillheal2[i].val += val;
  3088. else {
  3089. sd->skillheal2[i].id = type2;
  3090. sd->skillheal2[i].val = val;
  3091. }
  3092. break;
  3093. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3094. if(sd->state.lr_flag == 2)
  3095. break;
  3096. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3097. if (i == ARRAYLENGTH(sd->skillblown))
  3098. { //Better mention this so the array length can be updated. [Skotlex]
  3099. 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);
  3100. break;
  3101. }
  3102. if(sd->skillblown[i].id == type2)
  3103. sd->skillblown[i].val += val;
  3104. else {
  3105. sd->skillblown[i].id = type2;
  3106. sd->skillblown[i].val = val;
  3107. }
  3108. break;
  3109. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3110. if(sd->state.lr_flag != 2) {
  3111. sd->hp_loss.value = type2;
  3112. sd->hp_loss.rate = val;
  3113. }
  3114. break;
  3115. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3116. if(sd->state.lr_flag != 2) {
  3117. sd->hp_regen.value = type2;
  3118. sd->hp_regen.rate = val;
  3119. }
  3120. break;
  3121. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3122. if (sd->state.lr_flag != 2) {
  3123. sd->percent_hp_regen.value = type2;
  3124. sd->percent_hp_regen.rate = val;
  3125. }
  3126. break;
  3127. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3128. if (sd->state.lr_flag != 2) {
  3129. sd->percent_sp_regen.value = type2;
  3130. sd->percent_sp_regen.rate = val;
  3131. }
  3132. break;
  3133. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3134. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3135. if(sd->state.lr_flag != 2)
  3136. sd->right_weapon.addrace2[type2] += val;
  3137. else
  3138. sd->left_weapon.addrace2[type2] += val;
  3139. break;
  3140. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3141. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3142. if(sd->state.lr_flag != 2)
  3143. sd->subsize[type2]+=val;
  3144. break;
  3145. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3146. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3147. if(sd->state.lr_flag != 2)
  3148. sd->subrace2[type2]+=val;
  3149. break;
  3150. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3151. if(sd->state.lr_flag == 2)
  3152. break;
  3153. if (!itemdb_exists(type2)) {
  3154. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3155. break;
  3156. }
  3157. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3158. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3159. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3160. break;
  3161. }
  3162. sd->itemhealrate[i].nameid = type2;
  3163. sd->itemhealrate[i].rate += val;
  3164. break;
  3165. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3166. if (sd->state.lr_flag == 2)
  3167. break;
  3168. if (!type2 || !itemdb_group_exists(type2)) {
  3169. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3170. break;
  3171. }
  3172. pc_itemgrouphealrate(sd, type2, val);
  3173. break;
  3174. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3175. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3176. if(sd->state.lr_flag != 2)
  3177. sd->expaddrace[type2]+=val;
  3178. break;
  3179. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3180. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3181. if(sd->state.lr_flag != 2)
  3182. sd->expaddclass[type2]+=val;
  3183. break;
  3184. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3185. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3186. if(sd->state.lr_flag != 2)
  3187. sd->sp_gain_race[type2]+=val;
  3188. break;
  3189. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3190. if (sd->state.lr_flag != 2)
  3191. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3192. break;
  3193. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3194. if (sd->state.lr_flag != 2)
  3195. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3196. break;
  3197. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3198. if(sd->state.lr_flag != 2) {
  3199. sd->sp_loss.value = type2;
  3200. sd->sp_loss.rate = val;
  3201. }
  3202. break;
  3203. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3204. if(sd->state.lr_flag != 2) {
  3205. sd->sp_regen.value = type2;
  3206. sd->sp_regen.rate = val;
  3207. }
  3208. break;
  3209. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3210. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3211. if(!sd->state.lr_flag) {
  3212. sd->right_weapon.hp_drain_race[type2] += val;
  3213. }
  3214. else if(sd->state.lr_flag == 1) {
  3215. sd->left_weapon.hp_drain_race[type2] += val;
  3216. }
  3217. break;
  3218. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3219. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3220. if(!sd->state.lr_flag) {
  3221. sd->right_weapon.sp_drain_race[type2] += val;
  3222. }
  3223. else if(sd->state.lr_flag == 1) {
  3224. sd->left_weapon.sp_drain_race[type2] += val;
  3225. }
  3226. break;
  3227. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3228. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3229. if(!sd->state.lr_flag) {
  3230. sd->right_weapon.hp_drain_class[type2] += val;
  3231. }
  3232. else if(sd->state.lr_flag == 1) {
  3233. sd->left_weapon.hp_drain_class[type2] += val;
  3234. }
  3235. break;
  3236. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3237. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3238. if(!sd->state.lr_flag) {
  3239. sd->right_weapon.sp_drain_class[type2] += val;
  3240. }
  3241. else if(sd->state.lr_flag == 1) {
  3242. sd->left_weapon.sp_drain_class[type2] += val;
  3243. }
  3244. break;
  3245. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3246. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3247. if(sd->state.lr_flag != 2)
  3248. sd->ignore_mdef_by_race[type2] += val;
  3249. break;
  3250. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3251. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3252. if(sd->state.lr_flag != 2)
  3253. sd->ignore_mdef_by_class[type2] += val;
  3254. break;
  3255. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3256. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3257. if(sd->state.lr_flag != 2)
  3258. sd->ignore_def_by_race[type2] += val;
  3259. break;
  3260. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3261. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3262. if (sd->state.lr_flag != 2)
  3263. sd->ignore_def_by_class[type2] += val;
  3264. break;
  3265. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3266. if(sd->state.lr_flag == 2)
  3267. break;
  3268. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3269. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3270. 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);
  3271. break;
  3272. }
  3273. if (sd->skillusesprate[i].id == type2)
  3274. sd->skillusesprate[i].val += val;
  3275. else {
  3276. sd->skillusesprate[i].id = type2;
  3277. sd->skillusesprate[i].val = val;
  3278. }
  3279. break;
  3280. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3281. if(sd->state.lr_flag == 2)
  3282. break;
  3283. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3284. if (i == ARRAYLENGTH(sd->skillcooldown))
  3285. {
  3286. 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);
  3287. break;
  3288. }
  3289. if (sd->skillcooldown[i].id == type2)
  3290. sd->skillcooldown[i].val += val;
  3291. else {
  3292. sd->skillcooldown[i].id = type2;
  3293. sd->skillcooldown[i].val = val;
  3294. }
  3295. break;
  3296. #ifdef RENEWAL_CAST
  3297. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3298. if(sd->state.lr_flag == 2)
  3299. break;
  3300. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3301. if (i == ARRAYLENGTH(sd->skillfixcast))
  3302. {
  3303. 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);
  3304. break;
  3305. }
  3306. if (sd->skillfixcast[i].id == type2)
  3307. sd->skillfixcast[i].val += val;
  3308. else {
  3309. sd->skillfixcast[i].id = type2;
  3310. sd->skillfixcast[i].val = val;
  3311. }
  3312. break;
  3313. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3314. if(sd->state.lr_flag == 2)
  3315. break;
  3316. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3317. if (i == ARRAYLENGTH(sd->skillvarcast))
  3318. {
  3319. 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);
  3320. break;
  3321. }
  3322. if (sd->skillvarcast[i].id == type2)
  3323. sd->skillvarcast[i].val += val;
  3324. else {
  3325. sd->skillvarcast[i].id = type2;
  3326. sd->skillvarcast[i].val = val;
  3327. }
  3328. break;
  3329. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3330. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3331. if(sd->state.lr_flag == 2)
  3332. break;
  3333. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3334. if (i == ARRAYLENGTH(sd->skillcastrate))
  3335. {
  3336. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3337. break;
  3338. }
  3339. if(sd->skillcastrate[i].id == type2)
  3340. sd->skillcastrate[i].val -= val;
  3341. else {
  3342. sd->skillcastrate[i].id = type2;
  3343. sd->skillcastrate[i].val -= val;
  3344. }
  3345. break;
  3346. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3347. if(sd->state.lr_flag == 2)
  3348. break;
  3349. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3350. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3351. {
  3352. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3353. break;
  3354. }
  3355. if(sd->skillfixcastrate[i].id == type2)
  3356. sd->skillfixcastrate[i].val -= val;
  3357. else {
  3358. sd->skillfixcastrate[i].id = type2;
  3359. sd->skillfixcastrate[i].val -= val;
  3360. }
  3361. break;
  3362. #else
  3363. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3364. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3365. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3366. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3367. break;
  3368. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3369. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3370. if(sd->state.lr_flag == 2)
  3371. break;
  3372. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3373. if (i == ARRAYLENGTH(sd->skillcastrate))
  3374. { //Better mention this so the array length can be updated. [Skotlex]
  3375. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3376. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3377. break;
  3378. }
  3379. if(sd->skillcastrate[i].id == type2)
  3380. sd->skillcastrate[i].val += val;
  3381. else {
  3382. sd->skillcastrate[i].id = type2;
  3383. sd->skillcastrate[i].val = val;
  3384. }
  3385. break;
  3386. #endif
  3387. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3388. if(sd->state.lr_flag == 2)
  3389. break;
  3390. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3391. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3392. 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);
  3393. break;
  3394. }
  3395. if (sd->skillusesp[i].id == type2)
  3396. sd->skillusesp[i].val += val;
  3397. else {
  3398. sd->skillusesp[i].id = type2;
  3399. sd->skillusesp[i].val = val;
  3400. }
  3401. break;
  3402. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3403. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3404. if (i == ARRAYLENGTH(sd->subskill)) {
  3405. 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);
  3406. break;
  3407. }
  3408. if (sd->subskill[i].id == type2)
  3409. sd->subskill[i].val += val;
  3410. else {
  3411. sd->subskill[i].id = type2;
  3412. sd->subskill[i].val = val;
  3413. }
  3414. break;
  3415. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3416. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3417. sd->subdefele[type2] += val;
  3418. break;
  3419. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3420. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3421. sd->coma_class[type2] += val;
  3422. sd->special_state.bonus_coma = 1;
  3423. break;
  3424. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3425. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3426. sd->coma_race[type2] += val;
  3427. sd->special_state.bonus_coma = 1;
  3428. break;
  3429. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3430. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3431. if(sd->state.lr_flag != 2)
  3432. sd->magic_addrace2[type2] += val;
  3433. break;
  3434. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3435. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3436. if (sd->state.lr_flag != 2)
  3437. sd->ignore_mdef_by_race2[type2] += val;
  3438. break;
  3439. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3440. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3441. if (sd->state.lr_flag != 2)
  3442. sd->dropaddrace[type2] += val;
  3443. break;
  3444. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3445. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3446. if (sd->state.lr_flag != 2)
  3447. sd->dropaddclass[type2] += val;
  3448. break;
  3449. default:
  3450. if (running_npc_stat_calc_event) {
  3451. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3452. }
  3453. else if (current_equip_combo_pos > 0) {
  3454. 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);
  3455. }
  3456. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3457. 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);
  3458. }
  3459. else {
  3460. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3461. }
  3462. break;
  3463. }
  3464. }
  3465. /**
  3466. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3467. * @param sd
  3468. * @param type Bonus type used by bBonusName
  3469. * @param type2
  3470. * @param val Value that usually for rate or fixed value
  3471. */
  3472. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3473. {
  3474. nullpo_retv(sd);
  3475. switch(type){
  3476. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3477. if(sd->state.lr_flag != 2)
  3478. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3479. break;
  3480. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3481. if(sd->state.lr_flag != 2)
  3482. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3483. break;
  3484. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3485. if(sd->state.lr_flag != 2)
  3486. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3487. break;
  3488. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3489. if(sd->state.lr_flag != 2)
  3490. {
  3491. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3492. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3493. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3494. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3495. }
  3496. break;
  3497. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3498. if(sd->state.lr_flag != 2)
  3499. {
  3500. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3501. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3502. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3503. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3504. }
  3505. break;
  3506. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3507. if (sd->state.lr_flag != 2)
  3508. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3509. break;
  3510. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3511. if (sd->state.lr_flag != 2)
  3512. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3513. break;
  3514. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3515. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3516. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3517. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3518. break;
  3519. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3520. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3521. if(sd->state.lr_flag != 2)
  3522. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3523. break;
  3524. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3525. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3526. if( sd->state.lr_flag != 2 )
  3527. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3528. break;
  3529. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3530. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3531. if (sd->state.lr_flag != 2)
  3532. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3533. break;
  3534. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3535. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3536. if (sd->state.lr_flag != 2)
  3537. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3538. break;
  3539. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3540. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3541. if(sd->state.lr_flag != 2) {
  3542. sd->sp_vanish_race[type2].rate += type3;
  3543. sd->sp_vanish_race[type2].per += val;
  3544. }
  3545. break;
  3546. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3547. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3548. if(sd->state.lr_flag != 2) {
  3549. sd->hp_vanish_race[type2].rate += type3;
  3550. sd->hp_vanish_race[type2].per += val;
  3551. }
  3552. break;
  3553. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3554. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3555. if (sd->state.lr_flag == 2)
  3556. break;
  3557. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3558. //if (type3 > sd->norecover_state_race[type2].rate) {
  3559. // sd->norecover_state_race[type2].rate = type3;
  3560. // sd->norecover_state_race[type2].tick = val;
  3561. // break;
  3562. //}
  3563. sd->norecover_state_race[type2].rate = type3;
  3564. sd->norecover_state_race[type2].tick = val;
  3565. break;
  3566. default:
  3567. if (running_npc_stat_calc_event) {
  3568. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3569. }
  3570. else if (current_equip_combo_pos > 0) {
  3571. 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);
  3572. }
  3573. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3574. 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);
  3575. }
  3576. else {
  3577. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3578. }
  3579. break;
  3580. }
  3581. }
  3582. /**
  3583. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3584. * @param sd
  3585. * @param type Bonus type used by bBonusName
  3586. * @param type2
  3587. * @param type3
  3588. * @param val Value that usually for rate or fixed value
  3589. */
  3590. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3591. {
  3592. nullpo_retv(sd);
  3593. switch(type){
  3594. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3595. if(sd->state.lr_flag != 2)
  3596. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3597. break;
  3598. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3599. if(sd->state.lr_flag != 2)
  3600. 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);
  3601. break;
  3602. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3603. if(sd->state.lr_flag != 2)
  3604. {
  3605. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3606. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3607. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3608. }
  3609. break;
  3610. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3611. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3612. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3613. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3614. break;
  3615. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3616. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3617. if (sd->state.lr_flag != 2)
  3618. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3619. break;
  3620. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3621. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3622. if( sd->state.lr_flag != 2 )
  3623. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3624. break;
  3625. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3626. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3627. if(sd->state.lr_flag == 2)
  3628. break;
  3629. sd->def_set_race[type2].rate = type3;
  3630. sd->def_set_race[type2].tick = type4;
  3631. sd->def_set_race[type2].value = val;
  3632. break;
  3633. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3634. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3635. if(sd->state.lr_flag == 2)
  3636. break;
  3637. sd->mdef_set_race[type2].rate = type3;
  3638. sd->mdef_set_race[type2].tick = type4;
  3639. sd->mdef_set_race[type2].value = val;
  3640. break;
  3641. default:
  3642. if (running_npc_stat_calc_event) {
  3643. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3644. }
  3645. else if (current_equip_combo_pos > 0) {
  3646. 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);
  3647. }
  3648. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3649. 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);
  3650. }
  3651. else {
  3652. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3653. }
  3654. break;
  3655. }
  3656. }
  3657. /**
  3658. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3659. * @param sd
  3660. * @param type Bonus type used by bBonusName
  3661. * @param type2
  3662. * @param type3
  3663. * @param type4
  3664. * @param val Value that usually for rate or fixed value
  3665. */
  3666. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3667. {
  3668. nullpo_retv(sd);
  3669. switch(type){
  3670. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3671. if(sd->state.lr_flag != 2)
  3672. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3673. break;
  3674. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3675. if(sd->state.lr_flag != 2)
  3676. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3677. break;
  3678. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3679. if(sd->state.lr_flag != 2)
  3680. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3681. break;
  3682. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3683. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3684. if( sd->state.lr_flag != 2 )
  3685. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3686. break;
  3687. default:
  3688. if (running_npc_stat_calc_event) {
  3689. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3690. }
  3691. else if (current_equip_combo_pos > 0) {
  3692. 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);
  3693. }
  3694. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3695. 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);
  3696. }
  3697. else {
  3698. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3699. }
  3700. break;
  3701. }
  3702. }
  3703. /*==========================================
  3704. * Grants a player a given skill. Flag values are:
  3705. * 0 - Grant permanent skill to be bound to skill tree
  3706. * 1 - Grant an item skill (temporary)
  3707. * 2 - Like 1, except the level granted can stack with previously learned level.
  3708. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3709. *------------------------------------------*/
  3710. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3711. uint16 idx = 0;
  3712. nullpo_ret(sd);
  3713. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3714. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3715. return false;
  3716. }
  3717. if (level > MAX_SKILL_LEVEL) {
  3718. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3719. return false;
  3720. }
  3721. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3722. 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);
  3723. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3724. }
  3725. switch (type) {
  3726. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3727. sd->status.skill[idx].id = skill_id;
  3728. sd->status.skill[idx].lv = level;
  3729. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3730. if (level == 0) { //Remove skill.
  3731. sd->status.skill[idx].id = 0;
  3732. clif_deleteskill(sd,skill_id);
  3733. } else
  3734. clif_addskill(sd,skill_id);
  3735. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3736. status_calc_pc(sd, SCO_NONE);
  3737. break;
  3738. case ADDSKILL_TEMP: //Item bonus skill.
  3739. if (sd->status.skill[idx].id != 0) {
  3740. if (sd->status.skill[idx].lv >= level)
  3741. return true;
  3742. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3743. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3744. } else {
  3745. sd->status.skill[idx].id = skill_id;
  3746. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3747. }
  3748. sd->status.skill[idx].lv = level;
  3749. break;
  3750. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3751. if (sd->status.skill[idx].id != 0) {
  3752. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3753. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3754. } else {
  3755. sd->status.skill[idx].id = skill_id;
  3756. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3757. }
  3758. sd->status.skill[idx].lv += level;
  3759. break;
  3760. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3761. sd->status.skill[idx].id = skill_id;
  3762. sd->status.skill[idx].lv = level;
  3763. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3764. if (level == 0) { //Remove skill.
  3765. sd->status.skill[idx].id = 0;
  3766. clif_deleteskill(sd,skill_id);
  3767. } else
  3768. clif_addskill(sd,skill_id);
  3769. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3770. status_calc_pc(sd, SCO_NONE);
  3771. break;
  3772. default:
  3773. return false;
  3774. }
  3775. return true;
  3776. }
  3777. /*==========================================
  3778. * Append a card to an item ?
  3779. *------------------------------------------*/
  3780. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3781. {
  3782. int i;
  3783. unsigned short nameid;
  3784. nullpo_ret(sd);
  3785. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3786. return 0; //Invalid item index.
  3787. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3788. return 0; //Invalid card index.
  3789. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3790. return 0; // target item missing
  3791. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3792. return 0; // target card missing
  3793. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3794. return 0; // only weapons and armor are allowed
  3795. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3796. return 0; // must be a card
  3797. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3798. return 0; // target must be identified
  3799. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3800. return 0; // card slots reserved for other purposes
  3801. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3802. return 0; // card cannot be compounded on this item type
  3803. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3804. return 0; // attempted to place shield card on left-hand weapon.
  3805. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3806. return 0; // item must be unequipped
  3807. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3808. if( i == sd->inventory_data[idx_equip]->slot )
  3809. return 0; // no free slots
  3810. // remember the card id to insert
  3811. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3812. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3813. {// failed
  3814. clif_insert_card(sd,idx_equip,idx_card,1);
  3815. }
  3816. else
  3817. {// success
  3818. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3819. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3820. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3821. clif_insert_card(sd,idx_equip,idx_card,0);
  3822. }
  3823. return 0;
  3824. }
  3825. //
  3826. // Items
  3827. //
  3828. /*==========================================
  3829. * Update buying value by skills
  3830. *------------------------------------------*/
  3831. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3832. {
  3833. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3834. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3835. rate1 = 5+skill*2-((skill==10)? 1:0);
  3836. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3837. rate2 = 5+skill*4;
  3838. if(rate1 < rate2) rate1 = rate2;
  3839. if(rate1)
  3840. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3841. if(val < 0) val = 0;
  3842. if(orig_value > 0 && val < 1) val = 1;
  3843. return val;
  3844. }
  3845. /*==========================================
  3846. * Update selling value by skills
  3847. *------------------------------------------*/
  3848. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3849. {
  3850. int skill,val = orig_value,rate = 0;
  3851. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3852. rate = 5+skill*2-((skill==10)? 1:0);
  3853. if(rate)
  3854. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3855. if(val < 0) val = 0;
  3856. if(orig_value > 0 && val < 1) val = 1;
  3857. return val;
  3858. }
  3859. /*==========================================
  3860. * Checking if we have enough place on inventory for new item
  3861. * Make sure to take 30k as limit (for client I guess)
  3862. * @param sd
  3863. * @param nameid
  3864. * @param amount
  3865. * @return e_chkitem_result
  3866. *------------------------------------------*/
  3867. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3868. {
  3869. int i;
  3870. struct item_data* data;
  3871. nullpo_ret(sd);
  3872. if(amount > MAX_AMOUNT)
  3873. return CHKADDITEM_OVERAMOUNT;
  3874. data = itemdb_search(nameid);
  3875. if(!itemdb_isstackable2(data))
  3876. return CHKADDITEM_NEW;
  3877. if( data->stack.inventory && amount > data->stack.amount )
  3878. return CHKADDITEM_OVERAMOUNT;
  3879. for(i=0;i<MAX_INVENTORY;i++){
  3880. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3881. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3882. 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 ) )
  3883. return CHKADDITEM_OVERAMOUNT;
  3884. return CHKADDITEM_EXIST;
  3885. }
  3886. }
  3887. return CHKADDITEM_NEW;
  3888. }
  3889. /*==========================================
  3890. * Return number of available place in inventory
  3891. * Each non stackable item will reduce place by 1
  3892. * @param sd
  3893. * @return Number of empty slots
  3894. *------------------------------------------*/
  3895. uint8 pc_inventoryblank(struct map_session_data *sd)
  3896. {
  3897. uint8 i, b;
  3898. nullpo_ret(sd);
  3899. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3900. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3901. b++;
  3902. }
  3903. return b;
  3904. }
  3905. /**
  3906. * Attempts to remove zeny from player
  3907. * @param sd: Player
  3908. * @param zeny: Zeny removed
  3909. * @param type: Log type
  3910. * @param tsd: (optional) From who to log (if null take sd)
  3911. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3912. */
  3913. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3914. {
  3915. nullpo_retr(2,sd);
  3916. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3917. if( zeny < 0 )
  3918. {
  3919. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3920. return 1;
  3921. }
  3922. if( sd->status.zeny < zeny )
  3923. return 1; //Not enough.
  3924. sd->status.zeny -= zeny;
  3925. clif_updatestatus(sd,SP_ZENY);
  3926. if(!tsd) tsd = sd;
  3927. log_zeny(sd, type, tsd, -zeny);
  3928. if( zeny > 0 && sd->state.showzeny ) {
  3929. char output[255];
  3930. sprintf(output, "Removed %dz.", zeny);
  3931. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3932. }
  3933. return 0;
  3934. }
  3935. /**
  3936. * Attempts to give zeny to player
  3937. * @param sd: Player
  3938. * @param type: Log type
  3939. * @param tsd: (optional) From who to log (if null take sd)
  3940. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3941. */
  3942. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3943. {
  3944. nullpo_retr(-1,sd);
  3945. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3946. if( zeny < 0 )
  3947. {
  3948. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3949. return 1;
  3950. }
  3951. if( zeny > MAX_ZENY - sd->status.zeny )
  3952. zeny = MAX_ZENY - sd->status.zeny;
  3953. sd->status.zeny += zeny;
  3954. clif_updatestatus(sd,SP_ZENY);
  3955. if(!tsd) tsd = sd;
  3956. log_zeny(sd, type, tsd, zeny);
  3957. if( zeny > 0 && sd->state.showzeny ) {
  3958. char output[255];
  3959. sprintf(output, "Gained %dz.", zeny);
  3960. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3961. }
  3962. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  3963. return 0;
  3964. }
  3965. /**
  3966. * Attempts to remove Cash Points from player
  3967. * @param sd: Player
  3968. * @param price: Points player has to pay
  3969. * @param points: Points player has
  3970. * @param type: Log type
  3971. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3972. */
  3973. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3974. {
  3975. int cash;
  3976. nullpo_retr(-1,sd);
  3977. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3978. if( price < 0 || points < 0 )
  3979. {
  3980. 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);
  3981. return -2;
  3982. }
  3983. if( points > price )
  3984. {
  3985. 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);
  3986. points = price;
  3987. }
  3988. cash = price-points;
  3989. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3990. {
  3991. 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);
  3992. return -1;
  3993. }
  3994. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints-cash);
  3995. if( cash ){
  3996. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3997. }
  3998. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints-points);
  3999. if( points ){
  4000. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4001. }
  4002. if( battle_config.cashshop_show_points )
  4003. {
  4004. char output[CHAT_SIZE_MAX];
  4005. 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.
  4006. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4007. }
  4008. return cash+points;
  4009. }
  4010. /**
  4011. * Attempts to give Cash Points to player
  4012. * @param sd: Player
  4013. * @param cash: Cash player gets
  4014. * @param points: Points player has
  4015. * @param type: Log type
  4016. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  4017. */
  4018. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4019. {
  4020. char output[CHAT_SIZE_MAX];
  4021. nullpo_retr(-1,sd);
  4022. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4023. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4024. if( cash > 0 )
  4025. {
  4026. if( cash > MAX_ZENY-sd->cashPoints )
  4027. {
  4028. 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);
  4029. cash = MAX_ZENY-sd->cashPoints;
  4030. }
  4031. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints+cash);
  4032. if( cash ){
  4033. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4034. }
  4035. if( battle_config.cashshop_show_points )
  4036. {
  4037. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4038. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4039. }
  4040. return cash;
  4041. }
  4042. else if( cash < 0 )
  4043. {
  4044. 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);
  4045. return -1;
  4046. }
  4047. if( points > 0 )
  4048. {
  4049. if( points > MAX_ZENY-sd->kafraPoints )
  4050. {
  4051. 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);
  4052. points = MAX_ZENY-sd->kafraPoints;
  4053. }
  4054. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints+points);
  4055. if( points ){
  4056. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4057. }
  4058. if( battle_config.cashshop_show_points )
  4059. {
  4060. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4061. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4062. }
  4063. return points;
  4064. }
  4065. else if( points < 0 )
  4066. {
  4067. 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);
  4068. return -1;
  4069. }
  4070. return -2; //shouldn't happen but just in case
  4071. }
  4072. /**
  4073. * Searching a specified itemid in inventory and return his stored index
  4074. * @param sd Player
  4075. * @param nameid Find this Item!
  4076. * @return Stored index in inventory, or -1 if not found.
  4077. **/
  4078. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4079. short i;
  4080. nullpo_retr(-1, sd);
  4081. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4082. return ( i < MAX_INVENTORY ) ? i : -1;
  4083. }
  4084. /** Attempt to add a new item to player inventory
  4085. * @param sd
  4086. * @param item_data
  4087. * @param amount
  4088. * @param log_type
  4089. * @return
  4090. * 0 = success
  4091. * 1 = invalid itemid not found or negative amount
  4092. * 2 = overweight
  4093. * 3 = ?
  4094. * 4 = no free place found
  4095. * 5 = max amount reached
  4096. * 6 = ?
  4097. * 7 = stack limitation
  4098. */
  4099. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4100. struct item_data *id;
  4101. int16 i;
  4102. unsigned int w;
  4103. nullpo_retr(1, sd);
  4104. nullpo_retr(1, item);
  4105. if( item->nameid == 0 || amount <= 0 )
  4106. return ADDITEM_INVALID;
  4107. if( amount > MAX_AMOUNT )
  4108. return ADDITEM_OVERAMOUNT;
  4109. id = itemdb_search(item->nameid);
  4110. if( id->stack.inventory && amount > id->stack.amount )
  4111. {// item stack limitation
  4112. return ADDITEM_STACKLIMIT;
  4113. }
  4114. w = id->weight*amount;
  4115. if(sd->weight + w > sd->max_weight)
  4116. return ADDITEM_OVERWEIGHT;
  4117. i = MAX_INVENTORY;
  4118. if (id->flag.guid && !item->unique_id)
  4119. item->unique_id = pc_generate_unique_id(sd);
  4120. // Stackable | Non Rental
  4121. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4122. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4123. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4124. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4125. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4126. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4127. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4128. 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 ) )
  4129. return ADDITEM_OVERAMOUNT;
  4130. sd->inventory.u.items_inventory[i].amount += amount;
  4131. clif_additem(sd,i,amount,0);
  4132. break;
  4133. }
  4134. }
  4135. }
  4136. if (i >= MAX_INVENTORY) {
  4137. i = pc_search_inventory(sd,0);
  4138. if( i < 0 )
  4139. return ADDITEM_OVERITEM;
  4140. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4141. // clear equip and favorite fields first, just in case
  4142. if( item->equip )
  4143. sd->inventory.u.items_inventory[i].equip = 0;
  4144. if( item->favorite )
  4145. sd->inventory.u.items_inventory[i].favorite = 0;
  4146. sd->inventory.u.items_inventory[i].amount = amount;
  4147. sd->inventory_data[i] = id;
  4148. sd->last_addeditem_index = i;
  4149. if (!itemdb_isstackable2(id) || id->flag.guid)
  4150. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4151. clif_additem(sd,i,amount,0);
  4152. }
  4153. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4154. sd->weight += w;
  4155. clif_updatestatus(sd,SP_WEIGHT);
  4156. //Auto-equip
  4157. if(id->flag.autoequip)
  4158. pc_equipitem(sd, i, id->equip);
  4159. /* rental item check */
  4160. if( item->expire_time ) {
  4161. if( time(NULL) > item->expire_time ) {
  4162. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4163. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4164. } else {
  4165. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4166. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4167. pc_inventory_rental_add(sd, seconds);
  4168. }
  4169. }
  4170. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4171. return ADDITEM_SUCCESS;
  4172. }
  4173. /*==========================================
  4174. * Remove an item at index n from inventory by amount.
  4175. * @param sd
  4176. * @param n Item index in inventory
  4177. * @param amount
  4178. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4179. * @param reason Delete reason
  4180. * @param log_type e_log_pick_type
  4181. * @return 1 - invalid itemid or negative amount; 0 - Success
  4182. *------------------------------------------*/
  4183. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4184. {
  4185. nullpo_retr(1, sd);
  4186. 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)
  4187. return 1;
  4188. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4189. sd->inventory.u.items_inventory[n].amount -= amount;
  4190. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4191. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4192. if(sd->inventory.u.items_inventory[n].equip)
  4193. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4194. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4195. sd->inventory_data[n] = NULL;
  4196. }
  4197. if(!(type&1))
  4198. clif_delitem(sd,n,amount,reason);
  4199. if(!(type&2))
  4200. clif_updatestatus(sd,SP_WEIGHT);
  4201. return 0;
  4202. }
  4203. /*==========================================
  4204. * Attempt to drop an item.
  4205. * @param sd
  4206. * @param n Item index in inventory
  4207. * @param amount Amount of item
  4208. * @return False = fail; True = success
  4209. *------------------------------------------*/
  4210. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4211. {
  4212. nullpo_retr(1, sd);
  4213. if(n < 0 || n >= MAX_INVENTORY)
  4214. return false;
  4215. if(amount <= 0)
  4216. return false;
  4217. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4218. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4219. sd->inventory.u.items_inventory[n].amount < amount ||
  4220. sd->state.trading || sd->state.vending ||
  4221. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4222. )
  4223. return false;
  4224. if( map[sd->bl.m].flag.nodrop )
  4225. {
  4226. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4227. return false; //Can't drop items in nodrop mapflag maps.
  4228. }
  4229. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4230. {
  4231. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4232. return false;
  4233. }
  4234. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4235. return false;
  4236. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4237. clif_dropitem(sd, n, amount);
  4238. return true;
  4239. }
  4240. /*==========================================
  4241. * Attempt to pick up an item.
  4242. * @param sd
  4243. * @param fitem Item that will be picked
  4244. * @return False = fail; True = success
  4245. *------------------------------------------*/
  4246. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4247. {
  4248. int flag = 0;
  4249. unsigned int tick = gettick();
  4250. struct party_data *p = NULL;
  4251. nullpo_ret(sd);
  4252. nullpo_ret(fitem);
  4253. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4254. return false; // Distance is too far
  4255. if (sd->sc.cant.pickup)
  4256. return false;
  4257. if (sd->status.party_id)
  4258. p = party_search(sd->status.party_id);
  4259. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4260. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4261. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4262. if (!(p && p->party.item&1 &&
  4263. first_sd && first_sd->status.party_id == sd->status.party_id
  4264. ))
  4265. return false;
  4266. }
  4267. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4268. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4269. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4270. if (!(p && p->party.item&1 &&
  4271. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4272. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4273. ))
  4274. return false;
  4275. }
  4276. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4277. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4278. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4279. if(!(p && p->party.item&1 &&
  4280. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4281. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4282. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4283. ))
  4284. return false;
  4285. }
  4286. }
  4287. }
  4288. }
  4289. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4290. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4291. clif_additem(sd,0,0,flag);
  4292. return true;
  4293. }
  4294. //Display pickup animation.
  4295. pc_stop_attack(sd);
  4296. clif_takeitem(&sd->bl,&fitem->bl);
  4297. if (fitem->mob_id &&
  4298. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4299. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4300. )
  4301. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4302. map_clearflooritem(&fitem->bl);
  4303. return true;
  4304. }
  4305. /*==========================================
  4306. * Check if item is usable.
  4307. * Return:
  4308. * false = no
  4309. * true = yes
  4310. *------------------------------------------*/
  4311. bool pc_isUseitem(struct map_session_data *sd,int n)
  4312. {
  4313. struct item_data *item;
  4314. unsigned short nameid;
  4315. nullpo_ret(sd);
  4316. item = sd->inventory_data[n];
  4317. nameid = sd->inventory.u.items_inventory[n].nameid;
  4318. if( item == NULL )
  4319. return false;
  4320. //Not consumable item
  4321. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4322. return false;
  4323. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4324. return true;
  4325. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4326. return false;
  4327. //Prevent mass item usage. [Skotlex]
  4328. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4329. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4330. )
  4331. return false;
  4332. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4333. clif_msg(sd,ITEM_NOUSE_SITTING);
  4334. return false; // You cannot use this item while sitting.
  4335. }
  4336. if (sd->state.storage_flag && item->type != IT_CASH) {
  4337. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4338. return false; // You cannot use this item while storage is open.
  4339. }
  4340. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg2(sd->bl.m)))
  4341. return false;
  4342. switch( nameid ) {
  4343. case ITEMID_WING_OF_FLY:
  4344. case ITEMID_GIANT_FLY_WING:
  4345. case ITEMID_N_FLY_WING:
  4346. if( map[sd->bl.m].flag.noteleport || map_flag_gvg2(sd->bl.m) ) {
  4347. clif_skill_teleportmessage(sd,0);
  4348. return false;
  4349. }
  4350. if (nameid == ITEMID_GIANT_FLY_WING) {
  4351. struct party_data *pd = party_search(sd->status.party_id);
  4352. if (pd) {
  4353. int i;
  4354. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4355. if (i == MAX_PARTY) { // User is not party leader
  4356. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4357. break;
  4358. }
  4359. 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));
  4360. if (i == MAX_PARTY) { // No party members found on same map
  4361. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4362. break;
  4363. }
  4364. } else {
  4365. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4366. break;
  4367. }
  4368. }
  4369. // Fall through
  4370. case ITEMID_WING_OF_BUTTERFLY:
  4371. case ITEMID_N_BUTTERFLY_WING:
  4372. case ITEMID_DUN_TELE_SCROLL1:
  4373. case ITEMID_DUN_TELE_SCROLL2:
  4374. case ITEMID_DUN_TELE_SCROLL3:
  4375. case ITEMID_WOB_RUNE:
  4376. case ITEMID_WOB_SCHWALTZ:
  4377. case ITEMID_WOB_RACHEL:
  4378. case ITEMID_WOB_LOCAL:
  4379. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4380. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4381. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4382. return false;
  4383. }
  4384. if( map[sd->bl.m].flag.noreturn && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4385. return false;
  4386. break;
  4387. case ITEMID_MERCENARY_RED_POTION:
  4388. case ITEMID_MERCENARY_BLUE_POTION:
  4389. case ITEMID_M_CENTER_POTION:
  4390. case ITEMID_M_AWAKENING_POTION:
  4391. case ITEMID_M_BERSERK_POTION:
  4392. if( sd->md == NULL || sd->md->db == NULL )
  4393. return false;
  4394. if( sd->md->sc.data[SC_BERSERK] )
  4395. return false;
  4396. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4397. return false;
  4398. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4399. return false;
  4400. break;
  4401. case ITEMID_NEURALIZER:
  4402. if( !map[sd->bl.m].flag.reset )
  4403. return false;
  4404. break;
  4405. }
  4406. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4407. return false; // Mercenary Scrolls
  4408. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4409. if( pc_is90overweight(sd) ) {
  4410. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4411. return false;
  4412. }
  4413. if( !pc_inventoryblank(sd) ) {
  4414. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4415. return false;
  4416. }
  4417. }
  4418. //Gender check
  4419. if(item->sex != 2 && sd->status.sex != item->sex)
  4420. return false;
  4421. //Required level check
  4422. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4423. return false;
  4424. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4425. return false;
  4426. //Not equipable by class. [Skotlex]
  4427. if (!(
  4428. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4429. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4430. ))
  4431. return false;
  4432. if (sd->sc.count && (
  4433. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4434. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4435. sd->sc.data[SC_TRICKDEAD] ||
  4436. sd->sc.data[SC_HIDING] ||
  4437. sd->sc.data[SC__SHADOWFORM] ||
  4438. sd->sc.data[SC__INVISIBILITY] ||
  4439. sd->sc.data[SC__MANHOLE] ||
  4440. sd->sc.data[SC_DEEPSLEEP] ||
  4441. sd->sc.data[SC_CRYSTALIZE] ||
  4442. sd->sc.data[SC_KAGEHUMI] ||
  4443. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4444. sd->sc.data[SC_HEAT_BARREL_AFTER] ||
  4445. sd->sc.data[SC_KINGS_GRACE] ||
  4446. sd->sc.data[SC_SUHIDE]))
  4447. return false;
  4448. if (!pc_isItemClass(sd,item))
  4449. return false;
  4450. //Dead Branch items
  4451. if( item->flag.dead_branch )
  4452. log_branch(sd);
  4453. return true;
  4454. }
  4455. /*==========================================
  4456. * Last checks to use an item.
  4457. * Return:
  4458. * 0 = fail
  4459. * 1 = success
  4460. *------------------------------------------*/
  4461. int pc_useitem(struct map_session_data *sd,int n)
  4462. {
  4463. unsigned int tick = gettick();
  4464. int amount;
  4465. unsigned short nameid;
  4466. struct script_code *script;
  4467. struct item item;
  4468. struct item_data *id;
  4469. nullpo_ret(sd);
  4470. if (sd->npc_id) {
  4471. if (sd->progressbar.npc_id) {
  4472. clif_progressbar_abort(sd);
  4473. return 0; // First item use attempt cancels the progress bar
  4474. }
  4475. #ifdef RENEWAL
  4476. if (pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  4477. clif_msg(sd, WORK_IN_PROGRESS);
  4478. return 0;
  4479. }
  4480. #else
  4481. if (!sd->npc_item_flag)
  4482. return 0;
  4483. #endif
  4484. }
  4485. item = sd->inventory.u.items_inventory[n];
  4486. id = sd->inventory_data[n];
  4487. if (item.nameid == 0 || item.amount <= 0)
  4488. return 0;
  4489. if( !pc_isUseitem(sd,n) )
  4490. return 0;
  4491. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4492. nameid = id->nameid;
  4493. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4494. return 0;
  4495. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4496. if( id->flag.delay_consume ) {
  4497. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4498. return 0;
  4499. else if( pc_issit(sd) )
  4500. return 0;
  4501. }
  4502. //Since most delay-consume items involve using a "skill-type" target cursor,
  4503. //perform a skill-use check before going through. [Skotlex]
  4504. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4505. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4506. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4507. return 0;
  4508. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4509. return 0;
  4510. /* on restricted maps the item is consumed but the effect is not used */
  4511. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4512. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4513. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4514. clif_useitemack(sd,n,item.amount-1,true);
  4515. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4516. }
  4517. return 0;/* regardless, effect is not run */
  4518. }
  4519. sd->itemid = item.nameid;
  4520. sd->itemindex = n;
  4521. if(sd->catch_target_class != -1) //Abort pet catching.
  4522. sd->catch_target_class = -1;
  4523. amount = item.amount;
  4524. script = id->script;
  4525. //Check if the item is to be consumed immediately [Skotlex]
  4526. if (id->flag.delay_consume)
  4527. clif_useitemack(sd, n, amount, true);
  4528. else
  4529. {
  4530. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4531. {
  4532. clif_useitemack(sd, n, amount - 1, true);
  4533. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4534. }
  4535. else
  4536. clif_useitemack(sd, n, 0, false);
  4537. }
  4538. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4539. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4540. //Update item use time.
  4541. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4542. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4543. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4544. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4545. potion_flag = 0;
  4546. return 1;
  4547. }
  4548. /**
  4549. * Add item on cart for given index.
  4550. * @param sd
  4551. * @param item
  4552. * @param amount
  4553. * @param log_type
  4554. * @return 0 = success; 1 = fail; 2 = no slot
  4555. */
  4556. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4557. {
  4558. struct item_data *data;
  4559. int i,w;
  4560. nullpo_retr(1, sd);
  4561. nullpo_retr(1, item);
  4562. if(item->nameid == 0 || amount <= 0)
  4563. return 1;
  4564. data = itemdb_search(item->nameid);
  4565. if( data->stack.cart && amount > data->stack.amount )
  4566. {// item stack limitation
  4567. return 1;
  4568. }
  4569. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4570. { // Check item trade restrictions [Skotlex]
  4571. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4572. return 1;
  4573. }
  4574. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4575. return 1;
  4576. i = MAX_CART;
  4577. if( itemdb_isstackable2(data) && !item->expire_time )
  4578. {
  4579. for (i = 0; i < MAX_CART; i++) {
  4580. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4581. && sd->cart.u.items_cart[i].bound == item->bound
  4582. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4583. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4584. )
  4585. break;
  4586. }
  4587. }
  4588. if( i < MAX_CART )
  4589. {// item already in cart, stack it
  4590. 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 ) )
  4591. return 2; // no slot
  4592. sd->cart.u.items_cart[i].amount += amount;
  4593. clif_cart_additem(sd,i,amount,0);
  4594. }
  4595. else
  4596. {// item not stackable or not present, add it
  4597. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4598. if( i == MAX_CART )
  4599. return 2; // no slot
  4600. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4601. sd->cart.u.items_cart[i].id = 0;
  4602. sd->cart.u.items_cart[i].amount = amount;
  4603. sd->cart_num++;
  4604. clif_cart_additem(sd,i,amount,0);
  4605. }
  4606. sd->cart.u.items_cart[i].favorite = 0; // clear
  4607. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4608. sd->cart_weight += w;
  4609. clif_updatestatus(sd,SP_CARTINFO);
  4610. return 0;
  4611. }
  4612. /*==========================================
  4613. * Delete item on cart for given index.
  4614. *------------------------------------------*/
  4615. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4616. {
  4617. nullpo_retv(sd);
  4618. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4619. sd->cart.u.items_cart[n].amount < amount)
  4620. return;
  4621. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4622. sd->cart.u.items_cart[n].amount -= amount;
  4623. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4624. if(sd->cart.u.items_cart[n].amount <= 0) {
  4625. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4626. sd->cart_num--;
  4627. }
  4628. if(!type) {
  4629. clif_cart_delitem(sd,n,amount);
  4630. clif_updatestatus(sd,SP_CARTINFO);
  4631. }
  4632. }
  4633. /*==========================================
  4634. * Transfer item from inventory to cart.
  4635. *------------------------------------------*/
  4636. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4637. {
  4638. struct item *item_data;
  4639. char flag;
  4640. nullpo_retv(sd);
  4641. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4642. return;
  4643. item_data = &sd->inventory.u.items_inventory[idx];
  4644. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4645. return;
  4646. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4647. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4648. else {
  4649. clif_dropitem(sd,idx,0);
  4650. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4651. }
  4652. }
  4653. /*==========================================
  4654. * Get number of item in cart.
  4655. * Return:
  4656. -1 = itemid not found or no amount found
  4657. x = remaining itemid on cart after get
  4658. *------------------------------------------*/
  4659. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4660. {
  4661. struct item* item_data;
  4662. nullpo_retr(-1, sd);
  4663. item_data = &sd->cart.u.items_cart[idx];
  4664. if( item_data->nameid == 0 || item_data->amount == 0 )
  4665. return -1;
  4666. return item_data->amount - amount;
  4667. }
  4668. /*==========================================
  4669. * Retrieve an item at index idx from cart.
  4670. *------------------------------------------*/
  4671. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4672. {
  4673. struct item *item_data;
  4674. unsigned char flag = 0;
  4675. nullpo_retv(sd);
  4676. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4677. return;
  4678. item_data=&sd->cart.u.items_cart[idx];
  4679. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4680. return;
  4681. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4682. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4683. else {
  4684. clif_dropitem(sd,idx,0);
  4685. clif_additem(sd,0,0,flag);
  4686. }
  4687. }
  4688. /*==========================================
  4689. * Bound Item Check
  4690. * Type:
  4691. * 1 Account Bound
  4692. * 2 Guild Bound
  4693. * 3 Party Bound
  4694. * 4 Character Bound
  4695. *------------------------------------------*/
  4696. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4697. {
  4698. int i = 0, j = 0;
  4699. for(i = 0; i < MAX_INVENTORY; i++) {
  4700. 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) {
  4701. idxlist[j] = i;
  4702. j++;
  4703. }
  4704. }
  4705. return j;
  4706. }
  4707. /*==========================================
  4708. * Display item stolen msg to player sd
  4709. *------------------------------------------*/
  4710. int pc_show_steal(struct block_list *bl,va_list ap)
  4711. {
  4712. struct map_session_data *sd;
  4713. int itemid;
  4714. struct item_data *item=NULL;
  4715. char output[100];
  4716. sd=va_arg(ap,struct map_session_data *);
  4717. itemid=va_arg(ap,int);
  4718. if((item=itemdb_exists(itemid))==NULL)
  4719. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4720. else
  4721. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4722. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4723. return 0;
  4724. }
  4725. /*==========================================
  4726. * Steal an item from bl (mob).
  4727. * Return:
  4728. * 0 = fail
  4729. * 1 = succes
  4730. *------------------------------------------*/
  4731. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4732. {
  4733. int i,itemid;
  4734. double rate;
  4735. unsigned char flag = 0;
  4736. struct status_data *sd_status, *md_status;
  4737. struct mob_data *md;
  4738. struct item tmp_item;
  4739. if(!sd || !bl || bl->type!=BL_MOB)
  4740. return 0;
  4741. md = (TBL_MOB *)bl;
  4742. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4743. return 0;
  4744. sd_status= status_get_status_data(&sd->bl);
  4745. md_status= status_get_status_data(bl);
  4746. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4747. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4748. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4749. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4750. ) { //Can't steal from
  4751. md->state.steal_flag = UCHAR_MAX;
  4752. return 0;
  4753. }
  4754. // base skill success chance (percentual)
  4755. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4756. rate += sd->bonus.add_steal_rate;
  4757. if( rate < 1 )
  4758. return 0;
  4759. // Try dropping one item, in the order from first to last possible slot.
  4760. // Droprate is affected by the skill success rate.
  4761. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4762. 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. )
  4763. break;
  4764. if( i == MAX_STEAL_DROP )
  4765. return 0;
  4766. itemid = md->db->dropitem[i].nameid;
  4767. memset(&tmp_item,0,sizeof(tmp_item));
  4768. tmp_item.nameid = itemid;
  4769. tmp_item.amount = 1;
  4770. tmp_item.identify = itemdb_isidentified(itemid);
  4771. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4772. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4773. //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?
  4774. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4775. if(flag) { //Failed to steal due to overweight
  4776. clif_additem(sd,0,0,flag);
  4777. return 0;
  4778. }
  4779. if(battle_config.show_steal_in_same_party)
  4780. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4781. //Logs items, Stolen from mobs [Lupus]
  4782. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4783. //A Rare Steal Global Announce by Lupus
  4784. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4785. struct item_data *i_data;
  4786. char message[128];
  4787. i_data = itemdb_search(itemid);
  4788. sprintf (message, msg_txt(sd,542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4789. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4790. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4791. }
  4792. return 1;
  4793. }
  4794. /*==========================================
  4795. * Stole zeny from bl (mob)
  4796. * return
  4797. * 0 = fail
  4798. * 1 = success
  4799. *------------------------------------------*/
  4800. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4801. {
  4802. int rate,skill;
  4803. struct mob_data *md;
  4804. if(!sd || !target || target->type != BL_MOB)
  4805. return 0;
  4806. md = (TBL_MOB*)target;
  4807. 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)
  4808. return 0;
  4809. // FIXME: This formula is either custom or outdated.
  4810. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4811. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4812. if(rnd()%1000 < rate)
  4813. {
  4814. int amount = md->level*10 + rnd()%100;
  4815. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4816. md->state.steal_coin_flag = 1;
  4817. return 1;
  4818. }
  4819. return 0;
  4820. }
  4821. /*==========================================
  4822. * Set's a player position.
  4823. * @param sd
  4824. * @param mapindex
  4825. * @param x
  4826. * @param y
  4827. * @param clrtype
  4828. * @return SETPOS_OK Success
  4829. * SETPOS_MAPINDEX Invalid map index
  4830. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4831. * SETPOS_AUTOTRADE Player is in autotrade state
  4832. *------------------------------------------*/
  4833. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4834. {
  4835. int16 m;
  4836. nullpo_retr(SETPOS_OK,sd);
  4837. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4838. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4839. return SETPOS_MAPINDEX;
  4840. }
  4841. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4842. return SETPOS_AUTOTRADE;
  4843. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4844. pc_setstand(sd, true);
  4845. pc_setrestartvalue(sd,1);
  4846. }
  4847. m = map_mapindex2mapid(mapindex);
  4848. sd->state.changemap = (sd->mapindex != mapindex);
  4849. sd->state.warping = 1;
  4850. sd->state.workinprogress = WIP_DISABLE_NONE;
  4851. if(map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4852. bool instance_found = false;
  4853. struct party_data *p = NULL;
  4854. struct guild *g = NULL;
  4855. if (sd->instance_id) {
  4856. instance_delusers(sd->instance_id);
  4857. instance_found = true;
  4858. }
  4859. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4860. instance_delusers(p->instance_id);
  4861. instance_found = true;
  4862. }
  4863. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4864. instance_delusers(g->instance_id);
  4865. }
  4866. if( sd->state.changemap ) { // Misc map-changing settings
  4867. int i;
  4868. sd->state.pmap = sd->bl.m;
  4869. if (sd->sc.count) { // Cancel some map related stuff.
  4870. if (sd->sc.data[SC_JAILED])
  4871. return SETPOS_MAPINDEX; //You may not get out!
  4872. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4873. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4874. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4875. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4876. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4877. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4878. if (sd->sc.data[SC_KNOWLEDGE]) {
  4879. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4880. if (sce->timer != INVALID_TIMER)
  4881. delete_timer(sce->timer, status_change_timer);
  4882. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4883. }
  4884. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4885. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4886. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4887. }
  4888. for( i = 0; i < EQI_MAX; i++ ) {
  4889. if( sd->equip_index[i] >= 0 )
  4890. if( pc_isequip(sd,sd->equip_index[i]) )
  4891. pc_unequipitem(sd,sd->equip_index[i],2);
  4892. }
  4893. if (battle_config.clear_unit_onwarp&BL_PC)
  4894. skill_clear_unitgroup(&sd->bl);
  4895. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4896. guild_send_dot_remove(sd);
  4897. bg_send_dot_remove(sd);
  4898. if (sd->regen.state.gc)
  4899. sd->regen.state.gc = 0;
  4900. // make sure vending is allowed here
  4901. if (sd->state.vending && map[m].flag.novending) {
  4902. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4903. vending_closevending(sd);
  4904. }
  4905. channel_pcquit(sd,4); //quit map chan
  4906. }
  4907. if( m < 0 )
  4908. {
  4909. uint32 ip;
  4910. uint16 port;
  4911. //if can't find any map-servers, just abort setting position.
  4912. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4913. return SETPOS_NO_MAPSERVER;
  4914. if (sd->npc_id)
  4915. npc_event_dequeue(sd);
  4916. npc_script_event(sd, NPCE_LOGOUT);
  4917. //remove from map, THEN change x/y coordinates
  4918. unit_remove_map_pc(sd,clrtype);
  4919. sd->mapindex = mapindex;
  4920. sd->bl.x=x;
  4921. sd->bl.y=y;
  4922. pc_clean_skilltree(sd);
  4923. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4924. chrif_changemapserver(sd, ip, (short)port);
  4925. //Free session data from this map server [Kevin]
  4926. unit_free_pc(sd);
  4927. return SETPOS_OK;
  4928. }
  4929. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4930. {
  4931. 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);
  4932. x = y = 0; // make it random
  4933. }
  4934. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4935. int c=0;
  4936. do {
  4937. x = rnd()%(map[m].xs-2)+1;
  4938. y = rnd()%(map[m].ys-2)+1;
  4939. c++;
  4940. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4941. 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));
  4942. return SETPOS_OK; //preventing warp
  4943. //break; //allow warp anyway
  4944. }
  4945. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4946. }
  4947. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4948. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4949. vending_closevending(sd);
  4950. }
  4951. if(sd->bl.prev != NULL){
  4952. unit_remove_map_pc(sd,clrtype);
  4953. clif_changemap(sd,m,x,y); // [MouseJstr]
  4954. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4955. sd->state.rewarp = 1;
  4956. sd->mapindex = mapindex;
  4957. sd->bl.m = m;
  4958. sd->bl.x = sd->ud.to_x = x;
  4959. sd->bl.y = sd->ud.to_y = y;
  4960. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4961. { // Increased guild castle regen [Valaris]
  4962. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4963. if(gc && gc->guild_id == sd->status.guild_id)
  4964. sd->regen.state.gc = 1;
  4965. }
  4966. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4967. {
  4968. sd->pd->bl.m = m;
  4969. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4970. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4971. sd->pd->ud.dir = sd->ud.dir;
  4972. }
  4973. if( hom_is_active(sd->hd) )
  4974. {
  4975. sd->hd->bl.m = m;
  4976. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4977. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4978. sd->hd->ud.dir = sd->ud.dir;
  4979. }
  4980. if( sd->md )
  4981. {
  4982. sd->md->bl.m = m;
  4983. sd->md->bl.x = sd->md->ud.to_x = x;
  4984. sd->md->bl.y = sd->md->ud.to_y = y;
  4985. sd->md->ud.dir = sd->ud.dir;
  4986. }
  4987. if( sd->ed ) {
  4988. sd->ed->bl.m = m;
  4989. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4990. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4991. sd->ed->ud.dir = sd->ud.dir;
  4992. }
  4993. pc_cell_basilica(sd);
  4994. //check if we gonna be rewarped [lighta]
  4995. if(npc_check_areanpc(1,m,x,y,0)){
  4996. sd->count_rewarp++;
  4997. }
  4998. else
  4999. sd->count_rewarp = 0;
  5000. return SETPOS_OK;
  5001. }
  5002. /*==========================================
  5003. * Warp player sd to random location on current map.
  5004. * May fail if no walkable cell found (1000 attempts).
  5005. * Return:
  5006. * 0 = Success
  5007. * 1,2,3 = Fail
  5008. *------------------------------------------*/
  5009. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5010. {
  5011. int x,y,i=0;
  5012. int16 m;
  5013. nullpo_ret(sd);
  5014. m=sd->bl.m;
  5015. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  5016. return 3;
  5017. do {
  5018. x = rnd()%(map[m].xs-2)+1;
  5019. y = rnd()%(map[m].ys-2)+1;
  5020. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  5021. if (i < 1000)
  5022. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  5023. return 3;
  5024. }
  5025. /*==========================================
  5026. * Records a memo point at sd's current position
  5027. * pos - entry to replace, (-1: shift oldest entry out)
  5028. *------------------------------------------*/
  5029. bool pc_memo(struct map_session_data* sd, int pos)
  5030. {
  5031. int skill;
  5032. nullpo_ret(sd);
  5033. // check mapflags
  5034. if( sd->bl.m >= 0 && (map[sd->bl.m].flag.nomemo || map[sd->bl.m].flag.nowarpto) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  5035. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5036. return false;
  5037. }
  5038. // check inputs
  5039. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5040. return false; // invalid input
  5041. // check required skill level
  5042. skill = pc_checkskill(sd, AL_WARP);
  5043. if( skill < 1 ) {
  5044. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5045. return false;
  5046. }
  5047. if( skill < 2 || skill - 2 < pos ) {
  5048. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5049. return false;
  5050. }
  5051. if( pos == -1 )
  5052. {
  5053. uint8 i;
  5054. // prevent memo-ing the same map multiple times
  5055. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5056. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5057. pos = 0;
  5058. }
  5059. if( map[sd->bl.m].instance_id ) {
  5060. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5061. return false;
  5062. }
  5063. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5064. sd->status.memo_point[pos].x = sd->bl.x;
  5065. sd->status.memo_point[pos].y = sd->bl.y;
  5066. clif_skill_memomessage(sd, 0);
  5067. return true;
  5068. }
  5069. //
  5070. // Skills
  5071. //
  5072. /**
  5073. * Get the skill current cooldown for player.
  5074. * (get the db base cooldown for skill + player specific cooldown)
  5075. * @param sd : player pointer
  5076. * @param id : skill id
  5077. * @param lv : skill lv
  5078. * @return player skill cooldown
  5079. */
  5080. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5081. uint8 i;
  5082. uint16 idx = skill_get_index(skill_id);
  5083. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5084. if (!idx) return 0;
  5085. if (skill_db[idx]->cooldown[skill_lv - 1])
  5086. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5087. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5088. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5089. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5090. if (i < cooldownlen) {
  5091. cooldown += sd->skillcooldown[i].val;
  5092. cooldown = max(0,cooldown);
  5093. }
  5094. return cooldown;
  5095. }
  5096. /*==========================================
  5097. * Return player sd skill_lv learned for given skill
  5098. *------------------------------------------*/
  5099. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5100. {
  5101. uint16 idx = 0;
  5102. if (sd == NULL)
  5103. return 0;
  5104. if ((idx = skill_get_index(skill_id)) == 0) {
  5105. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5106. return 0;
  5107. }
  5108. if (SKILL_CHK_GUILD(skill_id) ) {
  5109. struct guild *g;
  5110. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5111. return guild_checkskill(g,skill_id);
  5112. return 0;
  5113. }
  5114. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5115. }
  5116. /**
  5117. * Check if we still have the correct weapon to continue the skill (actually status)
  5118. * If not ending it
  5119. * @param sd
  5120. * @return 0:error, 1:check done
  5121. */
  5122. static void pc_checkallowskill(struct map_session_data *sd)
  5123. {
  5124. const enum sc_type scw_list[] = {
  5125. SC_TWOHANDQUICKEN,
  5126. SC_ONEHAND,
  5127. SC_AURABLADE,
  5128. SC_PARRYING,
  5129. SC_SPEARQUICKEN,
  5130. SC_ADRENALINE,
  5131. SC_ADRENALINE2,
  5132. SC_DANCING,
  5133. SC_GATLINGFEVER,
  5134. };
  5135. uint8 i;
  5136. nullpo_retv(sd);
  5137. if(!sd->sc.count)
  5138. return;
  5139. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5140. { // Skills requiring specific weapon types
  5141. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5142. continue;
  5143. if(sd->sc.data[scw_list[i]] &&
  5144. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5145. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5146. }
  5147. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5148. // Spurt requires bare hands (feet, in fact xD)
  5149. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5150. if(sd->status.shield <= 0) { // Skills requiring a shield
  5151. const enum sc_type scs_list[] = {
  5152. SC_AUTOGUARD,
  5153. SC_DEFENDER,
  5154. SC_REFLECTSHIELD,
  5155. SC_REFLECTDAMAGE
  5156. };
  5157. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5158. if(sd->sc.data[scs_list[i]])
  5159. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5160. }
  5161. }
  5162. /*==========================================
  5163. * Return equipped index of item on player sd at pos
  5164. * Return
  5165. * -1 : Nothing equipped
  5166. * idx : (this index could be used in inventory to found item_data)
  5167. *------------------------------------------*/
  5168. short pc_checkequip(struct map_session_data *sd,int pos)
  5169. {
  5170. uint8 i;
  5171. nullpo_retr(-1, sd);
  5172. for(i=0;i<EQI_MAX;i++){
  5173. if(pos & equip_bitmask[i])
  5174. return sd->equip_index[i];
  5175. }
  5176. return -1;
  5177. }
  5178. /*==========================================
  5179. * Check if sd has nameid equipped somewhere
  5180. * @sd : the player session
  5181. * @nameid : id of the item to check
  5182. * @min : : see pc.h enum equip_index from ? to @max
  5183. * @max : see pc.h enum equip_index for @min to ?
  5184. * -return true,false
  5185. *------------------------------------------*/
  5186. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5187. {
  5188. int i;
  5189. for(i = min; i < max; i++) {
  5190. if(equip_bitmask[i]) {
  5191. int idx = sd->equip_index[i];
  5192. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5193. return true;
  5194. }
  5195. }
  5196. return false;
  5197. }
  5198. /*==========================================
  5199. * Convert's from the client's lame Job ID system
  5200. * to the map server's 'makes sense' system. [Skotlex]
  5201. *------------------------------------------*/
  5202. int pc_jobid2mapid(unsigned short b_class)
  5203. {
  5204. switch(b_class)
  5205. {
  5206. //Novice And 1-1 Jobs
  5207. case JOB_NOVICE: return MAPID_NOVICE;
  5208. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5209. case JOB_MAGE: return MAPID_MAGE;
  5210. case JOB_ARCHER: return MAPID_ARCHER;
  5211. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5212. case JOB_MERCHANT: return MAPID_MERCHANT;
  5213. case JOB_THIEF: return MAPID_THIEF;
  5214. case JOB_TAEKWON: return MAPID_TAEKWON;
  5215. case JOB_WEDDING: return MAPID_WEDDING;
  5216. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5217. case JOB_NINJA: return MAPID_NINJA;
  5218. case JOB_XMAS: return MAPID_XMAS;
  5219. case JOB_SUMMER: return MAPID_SUMMER;
  5220. case JOB_HANBOK: return MAPID_HANBOK;
  5221. case JOB_GANGSI: return MAPID_GANGSI;
  5222. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5223. //2-1 Jobs
  5224. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5225. case JOB_KNIGHT: return MAPID_KNIGHT;
  5226. case JOB_WIZARD: return MAPID_WIZARD;
  5227. case JOB_HUNTER: return MAPID_HUNTER;
  5228. case JOB_PRIEST: return MAPID_PRIEST;
  5229. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5230. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5231. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5232. case JOB_KAGEROU:
  5233. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5234. case JOB_REBELLION: return MAPID_REBELLION;
  5235. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5236. //2-2 Jobs
  5237. case JOB_CRUSADER: return MAPID_CRUSADER;
  5238. case JOB_SAGE: return MAPID_SAGE;
  5239. case JOB_BARD:
  5240. case JOB_DANCER: return MAPID_BARDDANCER;
  5241. case JOB_MONK: return MAPID_MONK;
  5242. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5243. case JOB_ROGUE: return MAPID_ROGUE;
  5244. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5245. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5246. //Trans Novice And Trans 1-1 Jobs
  5247. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5248. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5249. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5250. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5251. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5252. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5253. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5254. //Trans 2-1 Jobs
  5255. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5256. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5257. case JOB_SNIPER: return MAPID_SNIPER;
  5258. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5259. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5260. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5261. //Trans 2-2 Jobs
  5262. case JOB_PALADIN: return MAPID_PALADIN;
  5263. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5264. case JOB_CLOWN:
  5265. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5266. case JOB_CHAMPION: return MAPID_CHAMPION;
  5267. case JOB_CREATOR: return MAPID_CREATOR;
  5268. case JOB_STALKER: return MAPID_STALKER;
  5269. //Baby Novice And Baby 1-1 Jobs
  5270. case JOB_BABY: return MAPID_BABY;
  5271. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5272. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5273. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5274. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5275. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5276. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5277. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5278. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5279. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5280. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5281. //Baby 2-1 Jobs
  5282. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5283. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5284. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5285. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5286. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5287. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5288. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5289. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5290. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5291. case JOB_BABY_KAGEROU:
  5292. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5293. //Baby 2-2 Jobs
  5294. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5295. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5296. case JOB_BABY_BARD:
  5297. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5298. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5299. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5300. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5301. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5302. //3-1 Jobs
  5303. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5304. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5305. case JOB_WARLOCK: return MAPID_WARLOCK;
  5306. case JOB_RANGER: return MAPID_RANGER;
  5307. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5308. case JOB_MECHANIC: return MAPID_MECHANIC;
  5309. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5310. //3-2 Jobs
  5311. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5312. case JOB_SORCERER: return MAPID_SORCERER;
  5313. case JOB_MINSTREL:
  5314. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5315. case JOB_SURA: return MAPID_SURA;
  5316. case JOB_GENETIC: return MAPID_GENETIC;
  5317. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5318. //Trans 3-1 Jobs
  5319. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5320. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5321. case JOB_RANGER_T: return MAPID_RANGER_T;
  5322. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5323. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5324. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5325. //Trans 3-2 Jobs
  5326. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5327. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5328. case JOB_MINSTREL_T:
  5329. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5330. case JOB_SURA_T: return MAPID_SURA_T;
  5331. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5332. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5333. //Baby 3-1 Jobs
  5334. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5335. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5336. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5337. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5338. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5339. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5340. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5341. //Baby 3-2 Jobs
  5342. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5343. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5344. case JOB_BABY_MINSTREL:
  5345. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5346. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5347. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5348. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5349. //Doram Jobs
  5350. case JOB_SUMMONER: return MAPID_SUMMONER;
  5351. default:
  5352. return -1;
  5353. }
  5354. }
  5355. //Reverts the map-style class id to the client-style one.
  5356. int pc_mapid2jobid(unsigned short class_, int sex)
  5357. {
  5358. switch(class_) {
  5359. //Novice And 1-1 Jobs
  5360. case MAPID_NOVICE: return JOB_NOVICE;
  5361. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5362. case MAPID_MAGE: return JOB_MAGE;
  5363. case MAPID_ARCHER: return JOB_ARCHER;
  5364. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5365. case MAPID_MERCHANT: return JOB_MERCHANT;
  5366. case MAPID_THIEF: return JOB_THIEF;
  5367. case MAPID_TAEKWON: return JOB_TAEKWON;
  5368. case MAPID_WEDDING: return JOB_WEDDING;
  5369. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5370. case MAPID_NINJA: return JOB_NINJA;
  5371. case MAPID_XMAS: return JOB_XMAS;
  5372. case MAPID_SUMMER: return JOB_SUMMER;
  5373. case MAPID_HANBOK: return JOB_HANBOK;
  5374. case MAPID_GANGSI: return JOB_GANGSI;
  5375. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5376. //2-1 Jobs
  5377. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5378. case MAPID_KNIGHT: return JOB_KNIGHT;
  5379. case MAPID_WIZARD: return JOB_WIZARD;
  5380. case MAPID_HUNTER: return JOB_HUNTER;
  5381. case MAPID_PRIEST: return JOB_PRIEST;
  5382. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5383. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5384. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5385. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5386. case MAPID_REBELLION: return JOB_REBELLION;
  5387. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5388. //2-2 Jobs
  5389. case MAPID_CRUSADER: return JOB_CRUSADER;
  5390. case MAPID_SAGE: return JOB_SAGE;
  5391. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5392. case MAPID_MONK: return JOB_MONK;
  5393. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5394. case MAPID_ROGUE: return JOB_ROGUE;
  5395. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5396. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5397. //Trans Novice And Trans 2-1 Jobs
  5398. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5399. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5400. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5401. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5402. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5403. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5404. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5405. //Trans 2-1 Jobs
  5406. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5407. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5408. case MAPID_SNIPER: return JOB_SNIPER;
  5409. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5410. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5411. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5412. //Trans 2-2 Jobs
  5413. case MAPID_PALADIN: return JOB_PALADIN;
  5414. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5415. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5416. case MAPID_CHAMPION: return JOB_CHAMPION;
  5417. case MAPID_CREATOR: return JOB_CREATOR;
  5418. case MAPID_STALKER: return JOB_STALKER;
  5419. //Baby Novice And Baby 1-1 Jobs
  5420. case MAPID_BABY: return JOB_BABY;
  5421. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5422. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5423. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5424. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5425. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5426. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5427. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5428. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5429. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5430. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5431. //Baby 2-1 Jobs
  5432. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5433. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5434. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5435. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5436. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5437. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5438. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5439. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5440. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5441. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5442. //Baby 2-2 Jobs
  5443. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5444. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5445. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5446. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5447. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5448. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5449. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5450. //3-1 Jobs
  5451. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5452. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5453. case MAPID_WARLOCK: return JOB_WARLOCK;
  5454. case MAPID_RANGER: return JOB_RANGER;
  5455. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5456. case MAPID_MECHANIC: return JOB_MECHANIC;
  5457. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5458. //3-2 Jobs
  5459. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5460. case MAPID_SORCERER: return JOB_SORCERER;
  5461. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5462. case MAPID_SURA: return JOB_SURA;
  5463. case MAPID_GENETIC: return JOB_GENETIC;
  5464. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5465. //Trans 3-1 Jobs
  5466. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5467. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5468. case MAPID_RANGER_T: return JOB_RANGER_T;
  5469. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5470. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5471. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5472. //Trans 3-2 Jobs
  5473. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5474. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5475. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5476. case MAPID_SURA_T: return JOB_SURA_T;
  5477. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5478. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5479. //Baby 3-1 Jobs
  5480. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5481. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5482. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5483. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5484. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5485. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5486. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5487. //Baby 3-2 Jobs
  5488. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5489. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5490. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5491. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5492. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5493. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5494. //Doram Jobs
  5495. case MAPID_SUMMONER: return JOB_SUMMONER;
  5496. default:
  5497. return -1;
  5498. }
  5499. }
  5500. /*====================================================
  5501. * This function return the name of the job (by [Yor])
  5502. *----------------------------------------------------*/
  5503. const char* job_name(int class_)
  5504. {
  5505. switch (class_) {
  5506. case JOB_NOVICE:
  5507. case JOB_SWORDMAN:
  5508. case JOB_MAGE:
  5509. case JOB_ARCHER:
  5510. case JOB_ACOLYTE:
  5511. case JOB_MERCHANT:
  5512. case JOB_THIEF:
  5513. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5514. case JOB_KNIGHT:
  5515. case JOB_PRIEST:
  5516. case JOB_WIZARD:
  5517. case JOB_BLACKSMITH:
  5518. case JOB_HUNTER:
  5519. case JOB_ASSASSIN:
  5520. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5521. case JOB_KNIGHT2:
  5522. return msg_txt(NULL,557);
  5523. case JOB_CRUSADER:
  5524. case JOB_MONK:
  5525. case JOB_SAGE:
  5526. case JOB_ROGUE:
  5527. case JOB_ALCHEMIST:
  5528. case JOB_BARD:
  5529. case JOB_DANCER:
  5530. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5531. case JOB_CRUSADER2:
  5532. return msg_txt(NULL,563);
  5533. case JOB_WEDDING:
  5534. case JOB_SUPER_NOVICE:
  5535. case JOB_GUNSLINGER:
  5536. case JOB_NINJA:
  5537. case JOB_XMAS:
  5538. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5539. case JOB_SUMMER:
  5540. return msg_txt(NULL,621);
  5541. case JOB_HANBOK:
  5542. return msg_txt(NULL,694);
  5543. case JOB_OKTOBERFEST:
  5544. return msg_txt(NULL,696);
  5545. case JOB_NOVICE_HIGH:
  5546. case JOB_SWORDMAN_HIGH:
  5547. case JOB_MAGE_HIGH:
  5548. case JOB_ARCHER_HIGH:
  5549. case JOB_ACOLYTE_HIGH:
  5550. case JOB_MERCHANT_HIGH:
  5551. case JOB_THIEF_HIGH:
  5552. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5553. case JOB_LORD_KNIGHT:
  5554. case JOB_HIGH_PRIEST:
  5555. case JOB_HIGH_WIZARD:
  5556. case JOB_WHITESMITH:
  5557. case JOB_SNIPER:
  5558. case JOB_ASSASSIN_CROSS:
  5559. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5560. case JOB_LORD_KNIGHT2:
  5561. return msg_txt(NULL,582);
  5562. case JOB_PALADIN:
  5563. case JOB_CHAMPION:
  5564. case JOB_PROFESSOR:
  5565. case JOB_STALKER:
  5566. case JOB_CREATOR:
  5567. case JOB_CLOWN:
  5568. case JOB_GYPSY:
  5569. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5570. case JOB_PALADIN2:
  5571. return msg_txt(NULL,588);
  5572. case JOB_BABY:
  5573. case JOB_BABY_SWORDMAN:
  5574. case JOB_BABY_MAGE:
  5575. case JOB_BABY_ARCHER:
  5576. case JOB_BABY_ACOLYTE:
  5577. case JOB_BABY_MERCHANT:
  5578. case JOB_BABY_THIEF:
  5579. return msg_txt(NULL,595 - JOB_BABY + class_);
  5580. case JOB_BABY_KNIGHT:
  5581. case JOB_BABY_PRIEST:
  5582. case JOB_BABY_WIZARD:
  5583. case JOB_BABY_BLACKSMITH:
  5584. case JOB_BABY_HUNTER:
  5585. case JOB_BABY_ASSASSIN:
  5586. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5587. case JOB_BABY_KNIGHT2:
  5588. return msg_txt(NULL,602);
  5589. case JOB_BABY_CRUSADER:
  5590. case JOB_BABY_MONK:
  5591. case JOB_BABY_SAGE:
  5592. case JOB_BABY_ROGUE:
  5593. case JOB_BABY_ALCHEMIST:
  5594. case JOB_BABY_BARD:
  5595. case JOB_BABY_DANCER:
  5596. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5597. case JOB_BABY_CRUSADER2:
  5598. return msg_txt(NULL,608);
  5599. case JOB_SUPER_BABY:
  5600. return msg_txt(NULL,615);
  5601. case JOB_TAEKWON:
  5602. return msg_txt(NULL,616);
  5603. case JOB_STAR_GLADIATOR:
  5604. case JOB_STAR_GLADIATOR2:
  5605. return msg_txt(NULL,617);
  5606. case JOB_SOUL_LINKER:
  5607. return msg_txt(NULL,618);
  5608. case JOB_GANGSI:
  5609. case JOB_DEATH_KNIGHT:
  5610. case JOB_DARK_COLLECTOR:
  5611. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5612. case JOB_RUNE_KNIGHT:
  5613. case JOB_WARLOCK:
  5614. case JOB_RANGER:
  5615. case JOB_ARCH_BISHOP:
  5616. case JOB_MECHANIC:
  5617. case JOB_GUILLOTINE_CROSS:
  5618. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5619. case JOB_RUNE_KNIGHT_T:
  5620. case JOB_WARLOCK_T:
  5621. case JOB_RANGER_T:
  5622. case JOB_ARCH_BISHOP_T:
  5623. case JOB_MECHANIC_T:
  5624. case JOB_GUILLOTINE_CROSS_T:
  5625. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5626. case JOB_ROYAL_GUARD:
  5627. case JOB_SORCERER:
  5628. case JOB_MINSTREL:
  5629. case JOB_WANDERER:
  5630. case JOB_SURA:
  5631. case JOB_GENETIC:
  5632. case JOB_SHADOW_CHASER:
  5633. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5634. case JOB_ROYAL_GUARD_T:
  5635. case JOB_SORCERER_T:
  5636. case JOB_MINSTREL_T:
  5637. case JOB_WANDERER_T:
  5638. case JOB_SURA_T:
  5639. case JOB_GENETIC_T:
  5640. case JOB_SHADOW_CHASER_T:
  5641. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5642. case JOB_RUNE_KNIGHT2:
  5643. case JOB_RUNE_KNIGHT_T2:
  5644. return msg_txt(NULL,625);
  5645. case JOB_ROYAL_GUARD2:
  5646. case JOB_ROYAL_GUARD_T2:
  5647. return msg_txt(NULL,631);
  5648. case JOB_RANGER2:
  5649. case JOB_RANGER_T2:
  5650. return msg_txt(NULL,627);
  5651. case JOB_MECHANIC2:
  5652. case JOB_MECHANIC_T2:
  5653. return msg_txt(NULL,629);
  5654. case JOB_BABY_RUNE:
  5655. case JOB_BABY_WARLOCK:
  5656. case JOB_BABY_RANGER:
  5657. case JOB_BABY_BISHOP:
  5658. case JOB_BABY_MECHANIC:
  5659. case JOB_BABY_CROSS:
  5660. case JOB_BABY_GUARD:
  5661. case JOB_BABY_SORCERER:
  5662. case JOB_BABY_MINSTREL:
  5663. case JOB_BABY_WANDERER:
  5664. case JOB_BABY_SURA:
  5665. case JOB_BABY_GENETIC:
  5666. case JOB_BABY_CHASER:
  5667. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5668. case JOB_BABY_RUNE2:
  5669. return msg_txt(NULL,638);
  5670. case JOB_BABY_GUARD2:
  5671. return msg_txt(NULL,644);
  5672. case JOB_BABY_RANGER2:
  5673. return msg_txt(NULL,640);
  5674. case JOB_BABY_MECHANIC2:
  5675. return msg_txt(NULL,642);
  5676. case JOB_SUPER_NOVICE_E:
  5677. case JOB_SUPER_BABY_E:
  5678. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5679. case JOB_KAGEROU:
  5680. case JOB_OBORO:
  5681. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5682. case JOB_REBELLION:
  5683. return msg_txt(NULL,695);
  5684. case JOB_SUMMONER:
  5685. case JOB_BABY_SUMMONER:
  5686. case JOB_BABY_NINJA:
  5687. return msg_txt(NULL,697 - JOB_SUMMONER+class_);
  5688. case JOB_BABY_KAGEROU:
  5689. case JOB_BABY_OBORO:
  5690. case JOB_BABY_TAEKWON:
  5691. case JOB_BABY_STAR_GLADIATOR:
  5692. case JOB_BABY_SOUL_LINKER:
  5693. case JOB_BABY_GUNSLINGER:
  5694. case JOB_BABY_REBELLION:
  5695. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5696. case JOB_BABY_STAR_GLADIATOR2:
  5697. return msg_txt(NULL,756);
  5698. default:
  5699. return msg_txt(NULL,655);
  5700. }
  5701. }
  5702. /*====================================================
  5703. * Timered function to make id follow a target.
  5704. * @id = bl.id (player only atm)
  5705. * target is define in sd->followtarget (bl.id)
  5706. * used by pc_follow
  5707. *----------------------------------------------------*/
  5708. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5709. {
  5710. struct map_session_data *sd;
  5711. struct block_list *tbl;
  5712. sd = map_id2sd(id);
  5713. nullpo_ret(sd);
  5714. if (sd->followtimer != tid){
  5715. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5716. sd->followtimer = INVALID_TIMER;
  5717. return 0;
  5718. }
  5719. sd->followtimer = INVALID_TIMER;
  5720. tbl = map_id2bl(sd->followtarget);
  5721. if (tbl == NULL || pc_isdead(sd))
  5722. {
  5723. pc_stop_following(sd);
  5724. return 0;
  5725. }
  5726. // either player or target is currently detached from map blocks (could be teleporting),
  5727. // but still connected to this map, so we'll just increment the timer and check back later
  5728. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5729. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5730. {
  5731. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5732. if (!check_distance_bl(&sd->bl, tbl, 5))
  5733. unit_walktobl(&sd->bl, tbl, 5, 0);
  5734. } else
  5735. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5736. }
  5737. sd->followtimer = add_timer(
  5738. tick + 1000, // increase time a bit to loosen up map's load
  5739. pc_follow_timer, sd->bl.id, 0);
  5740. return 0;
  5741. }
  5742. int pc_stop_following (struct map_session_data *sd)
  5743. {
  5744. nullpo_ret(sd);
  5745. if (sd->followtimer != INVALID_TIMER) {
  5746. delete_timer(sd->followtimer,pc_follow_timer);
  5747. sd->followtimer = INVALID_TIMER;
  5748. }
  5749. sd->followtarget = -1;
  5750. sd->ud.target_to = 0;
  5751. unit_stop_walking(&sd->bl, 1);
  5752. return 0;
  5753. }
  5754. int pc_follow(struct map_session_data *sd,int target_id)
  5755. {
  5756. struct block_list *bl = map_id2bl(target_id);
  5757. if (bl == NULL /*|| bl->type != BL_PC*/)
  5758. return 1;
  5759. if (sd->followtimer != INVALID_TIMER)
  5760. pc_stop_following(sd);
  5761. sd->followtarget = target_id;
  5762. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5763. return 0;
  5764. }
  5765. int pc_checkbaselevelup(struct map_session_data *sd) {
  5766. unsigned int next = pc_nextbaseexp(sd);
  5767. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5768. return 0;
  5769. do {
  5770. sd->status.base_exp -= next;
  5771. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5772. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5773. sd->status.base_exp = next-1;
  5774. next = pc_gets_status_point(sd->status.base_level);
  5775. sd->status.base_level++;
  5776. sd->status.status_point += next;
  5777. if( pc_is_maxbaselv(sd) ){
  5778. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5779. break;
  5780. }
  5781. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5782. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5783. status_calc_pet(sd->pd,SCO_NONE);
  5784. clif_updatestatus(sd,SP_STATUSPOINT);
  5785. clif_updatestatus(sd,SP_BASELEVEL);
  5786. clif_updatestatus(sd,SP_BASEEXP);
  5787. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5788. status_calc_pc(sd,SCO_FORCE);
  5789. status_percent_heal(&sd->bl,100,100);
  5790. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5791. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5792. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5793. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5794. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5795. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5796. if (sd->state.snovice_dead_flag)
  5797. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5798. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5799. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5800. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5801. }
  5802. clif_misceffect(&sd->bl,0);
  5803. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5804. if(sd->status.party_id)
  5805. party_send_levelup(sd);
  5806. pc_baselevelchanged(sd);
  5807. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5808. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5809. return 1;
  5810. }
  5811. void pc_baselevelchanged(struct map_session_data *sd) {
  5812. uint8 i;
  5813. for( i = 0; i < EQI_MAX; i++ ) {
  5814. if( sd->equip_index[i] >= 0 ) {
  5815. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5816. pc_unequipitem(sd, sd->equip_index[i], 3);
  5817. }
  5818. }
  5819. pc_show_questinfo(sd);
  5820. }
  5821. int pc_checkjoblevelup(struct map_session_data *sd)
  5822. {
  5823. unsigned int next = pc_nextjobexp(sd);
  5824. nullpo_ret(sd);
  5825. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5826. return 0;
  5827. do {
  5828. sd->status.job_exp -= next;
  5829. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5830. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5831. sd->status.job_exp = next-1;
  5832. sd->status.job_level ++;
  5833. sd->status.skill_point ++;
  5834. if( pc_is_maxjoblv(sd) ){
  5835. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5836. break;
  5837. }
  5838. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5839. clif_updatestatus(sd,SP_JOBLEVEL);
  5840. clif_updatestatus(sd,SP_JOBEXP);
  5841. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5842. clif_updatestatus(sd,SP_SKILLPOINT);
  5843. status_calc_pc(sd,SCO_FORCE);
  5844. clif_misceffect(&sd->bl,1);
  5845. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5846. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5847. npc_script_event(sd, NPCE_JOBLVUP);
  5848. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5849. return 1;
  5850. }
  5851. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5852. * @param sd Player
  5853. * @param base_exp Base EXP before peronal bonuses
  5854. * @param job_exp Job EXP before peronal bonuses
  5855. * @param src Block list that affecting the exp calculation
  5856. */
  5857. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5858. {
  5859. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5860. if (src) {
  5861. struct status_data *status = status_get_status_data(src);
  5862. if( sd->expaddrace[status->race] )
  5863. bonus += sd->expaddrace[status->race];
  5864. if( sd->expaddrace[RC_ALL] )
  5865. bonus += sd->expaddrace[RC_ALL];
  5866. if( sd->expaddclass[status->class_] )
  5867. bonus += sd->expaddclass[status->class_];
  5868. if( sd->expaddclass[CLASS_ALL] )
  5869. bonus += sd->expaddclass[CLASS_ALL];
  5870. if (battle_config.pk_mode &&
  5871. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5872. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5873. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5874. vip_bonus_base = battle_config.vip_base_exp_increase;
  5875. vip_bonus_job = battle_config.vip_job_exp_increase;
  5876. }
  5877. }
  5878. // Give EXPBOOST for quests even if src is NULL.
  5879. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5880. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5881. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5882. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5883. }
  5884. if (*base_exp) {
  5885. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5886. *base_exp = cap_value(exp, 1, UINT_MAX);
  5887. }
  5888. // Give JEXPBOOST for quests even if src is NULL.
  5889. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5890. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5891. if (*job_exp) {
  5892. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5893. *job_exp = cap_value(exp, 1, UINT_MAX);
  5894. }
  5895. return;
  5896. }
  5897. /**
  5898. * Show EXP gained by player in percentage by @showexp
  5899. * @param sd Player
  5900. * @param base_exp Base EXP gained/loss
  5901. * @param next_base_exp Base EXP needed for next base level
  5902. * @param job_exp Job EXP gained/loss
  5903. * @param next_job_exp Job EXP needed for next job level
  5904. * @param lost True:EXP penalty, lose EXP
  5905. **/
  5906. 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) {
  5907. char output[CHAT_SIZE_MAX];
  5908. nullpo_retv(sd);
  5909. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5910. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5911. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5912. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5913. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5914. }
  5915. /**
  5916. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5917. * @param sd Player
  5918. * @param src EXP source
  5919. * @param base_exp Base EXP gained
  5920. * @param base_exp Job EXP gained
  5921. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5922. * @return
  5923. **/
  5924. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5925. {
  5926. unsigned int nextb = 0, nextj = 0;
  5927. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5928. nullpo_retv(sd);
  5929. if(sd->bl.prev == NULL || pc_isdead(sd))
  5930. return;
  5931. if (!(exp_flag&2)) {
  5932. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5933. return; // no exp on pvp maps
  5934. if (sd->status.guild_id>0)
  5935. base_exp -= guild_payexp(sd,base_exp);
  5936. }
  5937. flag = ((base_exp) ? 1 : 0) |
  5938. ((job_exp) ? 2 : 0) |
  5939. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5940. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5941. if (!(exp_flag&2))
  5942. pc_calcexp(sd, &base_exp, &job_exp, src);
  5943. nextb = pc_nextbaseexp(sd);
  5944. nextj = pc_nextjobexp(sd);
  5945. if (flag&4){
  5946. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5947. base_exp = 0;
  5948. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5949. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5950. }
  5951. if (flag&8){
  5952. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5953. job_exp = 0;
  5954. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5955. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5956. }
  5957. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5958. //Note that this value should never be greater than the original
  5959. //therefore no overflow checks are needed. [Skotlex]
  5960. if (nextb > 0) {
  5961. float nextbp = (float) base_exp / (float) nextb;
  5962. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5963. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5964. }
  5965. if (nextj > 0) {
  5966. float nextjp = (float) job_exp / (float) nextj;
  5967. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5968. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5969. }
  5970. }
  5971. // Give EXP for Base Level
  5972. if (base_exp) {
  5973. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5974. sd->status.base_exp = UINT32_MAX;
  5975. else
  5976. sd->status.base_exp += base_exp;
  5977. if (!pc_checkbaselevelup(sd))
  5978. clif_updatestatus(sd,SP_BASEEXP);
  5979. }
  5980. // Give EXP for Job Level
  5981. if (job_exp) {
  5982. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5983. sd->status.job_exp = UINT32_MAX;
  5984. else
  5985. sd->status.job_exp += job_exp;
  5986. if (!pc_checkjoblevelup(sd))
  5987. clif_updatestatus(sd,SP_JOBEXP);
  5988. }
  5989. if (flag&1)
  5990. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5991. if (flag&2)
  5992. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5993. if (sd->state.showexp && (base_exp || job_exp))
  5994. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5995. }
  5996. /**
  5997. * Lost Base/Job EXP from a player
  5998. * @param sd Player
  5999. * @param base_exp Base EXP lost
  6000. * @param job_exp Job EXP lost
  6001. **/
  6002. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6003. nullpo_retv(sd);
  6004. if (base_exp) {
  6005. base_exp = u32min(sd->status.base_exp, base_exp);
  6006. sd->status.base_exp -= base_exp;
  6007. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6008. clif_updatestatus(sd, SP_BASEEXP);
  6009. }
  6010. if (job_exp) {
  6011. job_exp = u32min(sd->status.job_exp, job_exp);
  6012. sd->status.job_exp -= job_exp;
  6013. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6014. clif_updatestatus(sd, SP_JOBEXP);
  6015. }
  6016. if (sd->state.showexp && (base_exp || job_exp))
  6017. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6018. }
  6019. /**
  6020. * Returns max base level for this character's class.
  6021. * @param class_: Player's class
  6022. * @return Max Base Level
  6023. */
  6024. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6025. return job_info[pc_class2idx(class_)].max_level[0];
  6026. }
  6027. /**
  6028. * Returns max base level for this character.
  6029. * @param sd Player
  6030. * @return Max Base Level
  6031. **/
  6032. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6033. return pc_class_maxbaselv(sd->status.class_);
  6034. }
  6035. /**
  6036. * Returns max job level for this character's class.
  6037. * @param class_: Player's class
  6038. * @return Max Job Level
  6039. */
  6040. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6041. return job_info[pc_class2idx(class_)].max_level[1];
  6042. }
  6043. /**
  6044. * Returns max job level for this character.
  6045. * @param sd Player
  6046. * @return Max Job Level
  6047. **/
  6048. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6049. return pc_class_maxjoblv(sd->status.class_);
  6050. }
  6051. /**
  6052. * Check if player is reached max base level
  6053. * @param sd
  6054. * @return True if reached max level
  6055. **/
  6056. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6057. nullpo_retr(false, sd);
  6058. return (sd->status.base_level >= pc_maxbaselv(sd));
  6059. }
  6060. /**
  6061. * Check if player is reached max base level
  6062. * @param sd
  6063. * @return True if reached max level
  6064. **/
  6065. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6066. nullpo_retr(false, sd);
  6067. return (sd->status.job_level >= pc_maxjoblv(sd));
  6068. }
  6069. /**
  6070. * Base exp needed for player to level up.
  6071. * @param sd
  6072. * @return Base EXP needed for next base level
  6073. **/
  6074. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6075. nullpo_ret(sd);
  6076. if (sd->status.base_level == 0) // Is this something that possible?
  6077. return 0;
  6078. if (pc_is_maxbaselv(sd))
  6079. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6080. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6081. }
  6082. /**
  6083. * Job exp needed for player to level up.
  6084. * @param sd
  6085. * @return Job EXP needed for next job level
  6086. **/
  6087. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6088. nullpo_ret(sd);
  6089. if (sd->status.job_level == 0) // Is this something that possible?
  6090. return 0;
  6091. if (pc_is_maxjoblv(sd))
  6092. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6093. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6094. }
  6095. /// Returns the value of the specified stat.
  6096. static int pc_getstat(struct map_session_data* sd, int type)
  6097. {
  6098. nullpo_retr(-1, sd);
  6099. switch( type ) {
  6100. case SP_STR: return sd->status.str;
  6101. case SP_AGI: return sd->status.agi;
  6102. case SP_VIT: return sd->status.vit;
  6103. case SP_INT: return sd->status.int_;
  6104. case SP_DEX: return sd->status.dex;
  6105. case SP_LUK: return sd->status.luk;
  6106. default:
  6107. return -1;
  6108. }
  6109. }
  6110. /// Sets the specified stat to the specified value.
  6111. /// Returns the new value.
  6112. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6113. {
  6114. nullpo_retr(-1, sd);
  6115. switch( type ) {
  6116. case SP_STR: sd->status.str = val; break;
  6117. case SP_AGI: sd->status.agi = val; break;
  6118. case SP_VIT: sd->status.vit = val; break;
  6119. case SP_INT: sd->status.int_ = val; break;
  6120. case SP_DEX: sd->status.dex = val; break;
  6121. case SP_LUK: sd->status.luk = val; break;
  6122. default:
  6123. return -1;
  6124. }
  6125. return val;
  6126. }
  6127. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6128. int pc_gets_status_point(int level)
  6129. {
  6130. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6131. return (statp[level+1] - statp[level]);
  6132. else //Default increase
  6133. return ((level+15) / 5);
  6134. }
  6135. #ifdef RENEWAL_STAT
  6136. /// Renewal status point cost formula
  6137. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6138. #else
  6139. /// Pre-Renewal status point cost formula
  6140. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6141. #endif
  6142. /// Returns the number of stat points needed to change the specified stat by val.
  6143. /// If val is negative, returns the number of stat points that would be needed to
  6144. /// raise the specified stat from (current value - val) to current value.
  6145. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6146. {
  6147. int low, high, sp = 0, max = 0;
  6148. if ( val == 0 )
  6149. return 0;
  6150. low = pc_getstat(sd,type);
  6151. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6152. if ( low >= max && val > 0 )
  6153. return 0; // Official servers show '0' when max is reached
  6154. high = low + val;
  6155. if ( val < 0 )
  6156. SWAP(low, high);
  6157. for ( ; low < high; low++ )
  6158. sp += PC_STATUS_POINT_COST(low);
  6159. return sp;
  6160. }
  6161. /**
  6162. * Returns the value the specified stat can be increased by with the current
  6163. * amount of available status points for the current character's class.
  6164. *
  6165. * @param sd The target character.
  6166. * @param type Stat to verify.
  6167. * @return Maximum value the stat could grow by.
  6168. */
  6169. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6170. {
  6171. int base, final_val, status_points, max_param;
  6172. nullpo_ret(sd);
  6173. base = final_val = pc_getstat(sd, type);
  6174. status_points = sd->status.status_point;
  6175. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6176. while (final_val <= max_param && status_points >= 0) {
  6177. status_points -= PC_STATUS_POINT_COST(final_val);
  6178. final_val++;
  6179. }
  6180. final_val--;
  6181. return (final_val > base ? final_val-base : 0);
  6182. }
  6183. /**
  6184. * Raises a stat by the specified amount.
  6185. *
  6186. * Obeys max_parameter limits.
  6187. * Subtracts status points according to the cost of the increased stat points.
  6188. *
  6189. * @param sd The target character.
  6190. * @param type The stat to change (see enum _sp)
  6191. * @param increase The stat increase (strictly positive) amount.
  6192. * @retval true if the stat was increased by any amount.
  6193. * @retval false if there were no changes.
  6194. */
  6195. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6196. {
  6197. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6198. nullpo_ret(sd);
  6199. // check conditions
  6200. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6201. clif_statusupack(sd, type, 0, 0);
  6202. return false;
  6203. }
  6204. // check limits
  6205. current = pc_getstat(sd, type);
  6206. max_increase = pc_maxparameterincrease(sd, type);
  6207. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6208. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6209. clif_statusupack(sd, type, 0, 0);
  6210. return false;
  6211. }
  6212. // check status points
  6213. needed_points = pc_need_status_point(sd, type, increase);
  6214. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6215. clif_statusupack(sd, type, 0, 0);
  6216. return false;
  6217. }
  6218. // set new values
  6219. final_value = pc_setstat(sd, type, current + increase);
  6220. sd->status.status_point -= needed_points;
  6221. status_calc_pc(sd,SCO_NONE);
  6222. // update increase cost indicator
  6223. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6224. // update statpoint count
  6225. clif_updatestatus(sd, SP_STATUSPOINT);
  6226. // update stat value
  6227. clif_statusupack(sd, type, 1, final_value); // required
  6228. if( final_value > 255 )
  6229. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6230. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6231. return true;
  6232. }
  6233. /**
  6234. * Raises a stat by the specified amount.
  6235. *
  6236. * Obeys max_parameter limits.
  6237. * Does not subtract status points for the cost of the modified stat points.
  6238. *
  6239. * @param sd The target character.
  6240. * @param type The stat to change (see enum _sp)
  6241. * @param val The stat increase (or decrease) amount.
  6242. * @return the stat increase amount.
  6243. * @retval 0 if no changes were made.
  6244. */
  6245. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6246. {
  6247. int max, need;
  6248. nullpo_ret(sd);
  6249. if( type < SP_STR || type > SP_LUK )
  6250. {
  6251. clif_statusupack(sd,type,0,0);
  6252. return 0;
  6253. }
  6254. need = pc_need_status_point(sd,type,1);
  6255. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6256. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6257. status_calc_pc(sd,SCO_NONE);
  6258. // update increase cost indicator
  6259. if( need != pc_need_status_point(sd,type,1) )
  6260. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6261. // update stat value
  6262. clif_statusupack(sd,type,1,val); // required
  6263. if( val > 255 )
  6264. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6265. return val;
  6266. }
  6267. /*==========================================
  6268. * Update skill_lv for player sd
  6269. * Skill point allocation
  6270. *------------------------------------------*/
  6271. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6272. {
  6273. uint16 idx = skill_get_index(skill_id);
  6274. nullpo_retv(sd);
  6275. if (!idx) {
  6276. if (skill_id)
  6277. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6278. return;
  6279. }
  6280. // Level up guild skill
  6281. if (SKILL_CHK_GUILD(skill_id)) {
  6282. guild_skillup(sd, skill_id);
  6283. return;
  6284. }
  6285. // Level up homunculus skill
  6286. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6287. hom_skillup(sd->hd, skill_id);
  6288. return;
  6289. }
  6290. else {
  6291. if( sd->status.skill_point > 0 &&
  6292. sd->status.skill[idx].id &&
  6293. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6294. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6295. {
  6296. int lv, range, upgradable;
  6297. sd->status.skill[idx].lv++;
  6298. sd->status.skill_point--;
  6299. if( !skill_get_inf(skill_id) )
  6300. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6301. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6302. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6303. else
  6304. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6305. lv = sd->status.skill[idx].lv;
  6306. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6307. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6308. clif_skillup(sd,skill_id,lv,range,upgradable);
  6309. clif_updatestatus(sd,SP_SKILLPOINT);
  6310. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6311. clif_updatestatus(sd,SP_CARTINFO);
  6312. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6313. clif_skillinfoblock(sd);
  6314. }
  6315. //else
  6316. // 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);
  6317. }
  6318. }
  6319. /*==========================================
  6320. * /allskill
  6321. *------------------------------------------*/
  6322. int pc_allskillup(struct map_session_data *sd)
  6323. {
  6324. int i;
  6325. nullpo_ret(sd);
  6326. for (i = 0; i < MAX_SKILL; i++) {
  6327. 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) {
  6328. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6329. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6330. if (sd->status.skill[i].lv == 0)
  6331. sd->status.skill[i].id = 0;
  6332. }
  6333. }
  6334. if (!pc_grant_allskills(sd, true)) {
  6335. uint16 sk_id;
  6336. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6337. int inf2 = 0;
  6338. uint16 sk_idx = 0;
  6339. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6340. continue;
  6341. inf2 = skill_get_inf2(sk_id);
  6342. if (
  6343. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6344. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6345. sk_id == SG_DEVIL
  6346. )
  6347. continue; //Cannot be learned normally.
  6348. sd->status.skill[sk_idx].id = sk_id;
  6349. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6350. }
  6351. }
  6352. status_calc_pc(sd,SCO_NONE);
  6353. //Required because if you could level up all skills previously,
  6354. //the update will not be sent as only the lv variable changes.
  6355. clif_skillinfoblock(sd);
  6356. return 0;
  6357. }
  6358. /*==========================================
  6359. * /resetlvl
  6360. *------------------------------------------*/
  6361. int pc_resetlvl(struct map_session_data* sd,int type)
  6362. {
  6363. int i;
  6364. nullpo_ret(sd);
  6365. if (type != 3) //Also reset skills
  6366. pc_resetskill(sd, 0);
  6367. if(type == 1){
  6368. sd->status.skill_point=0;
  6369. sd->status.base_level=1;
  6370. sd->status.job_level=1;
  6371. sd->status.base_exp=0;
  6372. sd->status.job_exp=0;
  6373. if(sd->sc.option !=0)
  6374. sd->sc.option = 0;
  6375. sd->status.str=1;
  6376. sd->status.agi=1;
  6377. sd->status.vit=1;
  6378. sd->status.int_=1;
  6379. sd->status.dex=1;
  6380. sd->status.luk=1;
  6381. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6382. sd->status.status_point=100; // not 88 [celest]
  6383. // give platinum skills upon changing
  6384. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6385. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6386. }
  6387. }
  6388. if(type == 2){
  6389. sd->status.skill_point=0;
  6390. sd->status.base_level=1;
  6391. sd->status.job_level=1;
  6392. sd->status.base_exp=0;
  6393. sd->status.job_exp=0;
  6394. }
  6395. if(type == 3){
  6396. sd->status.base_level=1;
  6397. sd->status.base_exp=0;
  6398. }
  6399. if(type == 4){
  6400. sd->status.job_level=1;
  6401. sd->status.job_exp=0;
  6402. }
  6403. clif_updatestatus(sd,SP_STATUSPOINT);
  6404. clif_updatestatus(sd,SP_STR);
  6405. clif_updatestatus(sd,SP_AGI);
  6406. clif_updatestatus(sd,SP_VIT);
  6407. clif_updatestatus(sd,SP_INT);
  6408. clif_updatestatus(sd,SP_DEX);
  6409. clif_updatestatus(sd,SP_LUK);
  6410. clif_updatestatus(sd,SP_BASELEVEL);
  6411. clif_updatestatus(sd,SP_JOBLEVEL);
  6412. clif_updatestatus(sd,SP_STATUSPOINT);
  6413. clif_updatestatus(sd,SP_BASEEXP);
  6414. clif_updatestatus(sd,SP_JOBEXP);
  6415. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6416. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6417. clif_updatestatus(sd,SP_SKILLPOINT);
  6418. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6419. clif_updatestatus(sd,SP_UAGI);
  6420. clif_updatestatus(sd,SP_UVIT);
  6421. clif_updatestatus(sd,SP_UINT);
  6422. clif_updatestatus(sd,SP_UDEX);
  6423. clif_updatestatus(sd,SP_ULUK); // End Addition
  6424. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6425. if(sd->equip_index[i] >= 0)
  6426. if(pc_isequip(sd,sd->equip_index[i]))
  6427. pc_unequipitem(sd,sd->equip_index[i],2);
  6428. }
  6429. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6430. party_send_levelup(sd);
  6431. status_calc_pc(sd, SCO_FORCE);
  6432. clif_skillinfoblock(sd);
  6433. return 0;
  6434. }
  6435. /*==========================================
  6436. * /resetstate
  6437. *------------------------------------------*/
  6438. int pc_resetstate(struct map_session_data* sd)
  6439. {
  6440. nullpo_ret(sd);
  6441. if (battle_config.use_statpoint_table)
  6442. { // New statpoint table used here - Dexity
  6443. if (sd->status.base_level > MAX_LEVEL)
  6444. { //statp[] goes out of bounds, can't reset!
  6445. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6446. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6447. return 0;
  6448. }
  6449. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6450. }
  6451. else
  6452. {
  6453. int add=0;
  6454. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6455. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6456. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6457. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6458. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6459. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6460. sd->status.status_point+=add;
  6461. }
  6462. pc_setstat(sd, SP_STR, 1);
  6463. pc_setstat(sd, SP_AGI, 1);
  6464. pc_setstat(sd, SP_VIT, 1);
  6465. pc_setstat(sd, SP_INT, 1);
  6466. pc_setstat(sd, SP_DEX, 1);
  6467. pc_setstat(sd, SP_LUK, 1);
  6468. clif_updatestatus(sd,SP_STR);
  6469. clif_updatestatus(sd,SP_AGI);
  6470. clif_updatestatus(sd,SP_VIT);
  6471. clif_updatestatus(sd,SP_INT);
  6472. clif_updatestatus(sd,SP_DEX);
  6473. clif_updatestatus(sd,SP_LUK);
  6474. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6475. clif_updatestatus(sd,SP_UAGI);
  6476. clif_updatestatus(sd,SP_UVIT);
  6477. clif_updatestatus(sd,SP_UINT);
  6478. clif_updatestatus(sd,SP_UDEX);
  6479. clif_updatestatus(sd,SP_ULUK); // End Addition
  6480. clif_updatestatus(sd,SP_STATUSPOINT);
  6481. if( sd->mission_mobid ) { //bugreport:2200
  6482. sd->mission_mobid = 0;
  6483. sd->mission_count = 0;
  6484. pc_setglobalreg(sd, add_str("TK_MISSION_ID"), 0);
  6485. }
  6486. status_calc_pc(sd, SCO_NONE);
  6487. return 1;
  6488. }
  6489. /*==========================================
  6490. * /resetskill
  6491. * if flag&1, perform block resync and status_calc call.
  6492. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6493. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6494. *------------------------------------------*/
  6495. int pc_resetskill(struct map_session_data* sd, int flag)
  6496. {
  6497. int i, skill_point=0;
  6498. nullpo_ret(sd);
  6499. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6500. return 0;
  6501. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6502. /**
  6503. * 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)
  6504. **/
  6505. if( pc_is_taekwon_ranker(sd) )
  6506. return 0;
  6507. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6508. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6509. i = sd->sc.option;
  6510. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6511. i &= ~OPTION_RIDING;
  6512. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6513. i &= ~OPTION_FALCON;
  6514. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6515. i &= ~OPTION_DRAGON;
  6516. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6517. i &= ~OPTION_WUG;
  6518. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6519. i &= ~OPTION_WUGRIDER;
  6520. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6521. i &= ~OPTION_MADOGEAR;
  6522. #ifndef NEW_CARTS
  6523. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6524. i &= ~OPTION_CART;
  6525. #else
  6526. if( sd->sc.data[SC_PUSH_CART] )
  6527. pc_setcart(sd, 0);
  6528. #endif
  6529. if( i != sd->sc.option )
  6530. pc_setoption(sd, i);
  6531. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6532. hom_vaporize(sd, HOM_ST_ACTIVE);
  6533. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6534. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6535. }
  6536. for( i = 1; i < MAX_SKILL; i++ )
  6537. {
  6538. uint8 lv = sd->status.skill[i].lv;
  6539. int inf2;
  6540. uint16 skill_id = skill_idx2id(i);
  6541. if (lv == 0 || skill_id == 0)
  6542. continue;
  6543. inf2 = skill_get_inf2(skill_id);
  6544. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6545. continue;
  6546. // Don't reset trick dead if not a novice/baby
  6547. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6548. {
  6549. sd->status.skill[i].lv = 0;
  6550. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6551. continue;
  6552. }
  6553. // do not reset basic skill
  6554. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6555. continue;
  6556. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6557. continue;
  6558. if( flag&4 && !skill_ischangesex(skill_id) )
  6559. continue;
  6560. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6561. { //Only handle quest skills in a special way when you can't learn them manually
  6562. if( battle_config.quest_skill_reset && !(flag&2) )
  6563. { //Wipe them
  6564. sd->status.skill[i].lv = 0;
  6565. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6566. }
  6567. continue;
  6568. }
  6569. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6570. skill_point += lv;
  6571. else
  6572. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6573. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6574. if( !(flag&2) )
  6575. {// reset
  6576. sd->status.skill[i].lv = 0;
  6577. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6578. }
  6579. }
  6580. if( flag&2 || !skill_point ) return skill_point;
  6581. sd->status.skill_point += skill_point;
  6582. if (flag&1) {
  6583. clif_updatestatus(sd,SP_SKILLPOINT);
  6584. clif_skillinfoblock(sd);
  6585. status_calc_pc(sd, SCO_FORCE);
  6586. }
  6587. return skill_point;
  6588. }
  6589. /*==========================================
  6590. * /resetfeel [Komurka]
  6591. *------------------------------------------*/
  6592. int pc_resetfeel(struct map_session_data* sd)
  6593. {
  6594. int i;
  6595. nullpo_ret(sd);
  6596. for (i=0; i<MAX_PC_FEELHATE; i++)
  6597. {
  6598. sd->feel_map[i].m = -1;
  6599. sd->feel_map[i].index = 0;
  6600. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6601. }
  6602. return 0;
  6603. }
  6604. int pc_resethate(struct map_session_data* sd)
  6605. {
  6606. int i;
  6607. nullpo_ret(sd);
  6608. for (i=0; i<3; i++)
  6609. {
  6610. sd->hate_mob[i] = -1;
  6611. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6612. }
  6613. return 0;
  6614. }
  6615. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6616. {
  6617. int i, bonus = 0;
  6618. nullpo_ret(sd);
  6619. skill_id = skill_dummy2skill_id(skill_id);
  6620. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6621. if( i < ARRAYLENGTH(sd->skillatk) )
  6622. bonus = sd->skillatk[i].val;
  6623. return bonus;
  6624. }
  6625. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6626. {
  6627. int i, bonus = 0;
  6628. nullpo_ret(sd);
  6629. skill_id = skill_dummy2skill_id(skill_id);
  6630. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6631. if( i < ARRAYLENGTH(sd->subskill) )
  6632. bonus = sd->subskill[i].val;
  6633. return bonus;
  6634. }
  6635. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6636. int i, bonus = sd->bonus.add_heal_rate;
  6637. skill_id = skill_dummy2skill_id(skill_id);
  6638. if( bonus ) {
  6639. switch( skill_id ) {
  6640. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6641. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6642. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6643. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6644. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6645. }
  6646. }
  6647. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6648. if( i < ARRAYLENGTH(sd->skillheal) )
  6649. bonus += sd->skillheal[i].val;
  6650. return bonus;
  6651. }
  6652. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6653. int i, bonus = sd->bonus.add_heal2_rate;
  6654. skill_id = skill_dummy2skill_id(skill_id);
  6655. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6656. if( i < ARRAYLENGTH(sd->skillheal2) )
  6657. bonus += sd->skillheal2[i].val;
  6658. return bonus;
  6659. }
  6660. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6661. {
  6662. if( !pc_isdead(sd) )
  6663. return; // not applicable
  6664. if( sd->bg_id && bg_member_respawn(sd) )
  6665. return; // member revived by battleground
  6666. pc_setstand(sd, true);
  6667. pc_setrestartvalue(sd,3);
  6668. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6669. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6670. }
  6671. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6672. {
  6673. struct map_session_data *sd = map_id2sd(id);
  6674. if( sd != NULL )
  6675. {
  6676. sd->pvp_point=0;
  6677. pc_respawn(sd,CLR_OUTSIGHT);
  6678. }
  6679. return 0;
  6680. }
  6681. /*==========================================
  6682. * Invoked when a player has received damage
  6683. *------------------------------------------*/
  6684. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6685. {
  6686. if (sp) clif_updatestatus(sd,SP_SP);
  6687. if (hp) clif_updatestatus(sd,SP_HP);
  6688. else return;
  6689. if (!src)
  6690. return;
  6691. if( pc_issit(sd) ) {
  6692. pc_setstand(sd, true);
  6693. skill_sit(sd,0);
  6694. }
  6695. if (sd->progressbar.npc_id)
  6696. clif_progressbar_abort(sd);
  6697. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6698. pet_target_check(sd->pd,src,1);
  6699. if( sd->status.ele_id > 0 )
  6700. elemental_set_target(sd,src);
  6701. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6702. sd->canlog_tick = gettick();
  6703. }
  6704. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6705. {
  6706. TBL_PC *sd = map_id2sd(id);
  6707. if(sd) pc_close_npc(sd,data);
  6708. return 0;
  6709. }
  6710. /*
  6711. * Method to properly close npc for player and clear anything related
  6712. * @flag == 1 : produce close button
  6713. * @flag == 2 : directly close it
  6714. */
  6715. void pc_close_npc(struct map_session_data *sd,int flag)
  6716. {
  6717. nullpo_retv(sd);
  6718. if (sd->npc_id || sd->npc_shopid) {
  6719. if (sd->state.using_fake_npc) {
  6720. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6721. sd->state.using_fake_npc = 0;
  6722. }
  6723. if (sd->st) {
  6724. if(sd->st->state == RUN){ //wait ending code execution
  6725. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6726. return;
  6727. }
  6728. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6729. sd->st->mes_active = 0;
  6730. }
  6731. sd->state.menu_or_input = 0;
  6732. sd->npc_menu = 0;
  6733. sd->npc_shopid = 0;
  6734. #ifdef SECURE_NPCTIMEOUT
  6735. sd->npc_idle_timer = INVALID_TIMER;
  6736. #endif
  6737. clif_scriptclose(sd,sd->npc_id);
  6738. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6739. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6740. script_free_state(sd->st);
  6741. sd->st = NULL;
  6742. sd->npc_id = 0;
  6743. }
  6744. }
  6745. }
  6746. /*==========================================
  6747. * Invoked when a player has negative current hp
  6748. *------------------------------------------*/
  6749. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6750. {
  6751. int i=0,k=0;
  6752. unsigned int tick = gettick();
  6753. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6754. // Super Novices have no kill or die functions attached when saved by their angel
  6755. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6756. unsigned int exp = pc_nextbaseexp(sd);
  6757. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6758. sd->state.snovice_dead_flag = 1;
  6759. pc_setrestartvalue(sd,1);
  6760. status_percent_heal(&sd->bl, 100, 100);
  6761. clif_resurrection(&sd->bl, 1);
  6762. if(battle_config.pc_invincible_time)
  6763. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6764. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6765. if(map_flag_gvg2(sd->bl.m))
  6766. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6767. return 0;
  6768. }
  6769. }
  6770. for(k = 0; k < MAX_DEVOTION; k++) {
  6771. if (sd->devotion[k]){
  6772. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6773. if (devsd)
  6774. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6775. sd->devotion[k] = 0;
  6776. }
  6777. }
  6778. if(sd->shadowform_id) { //if we were target of shadowform
  6779. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6780. sd->shadowform_id = 0; //should be remove on status end anyway
  6781. }
  6782. if(sd->status.pet_id > 0 && sd->pd) {
  6783. struct pet_data *pd = sd->pd;
  6784. if( !map[sd->bl.m].flag.noexppenalty ) {
  6785. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6786. if( pd->pet.intimate < 0 )
  6787. pd->pet.intimate = 0;
  6788. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6789. }
  6790. if( sd->pd->target_id ) // Unlock all targets...
  6791. pet_unlocktarget(sd->pd);
  6792. }
  6793. 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)
  6794. hom_vaporize(sd, HOM_ST_ACTIVE);
  6795. if( sd->md )
  6796. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6797. if( sd->ed )
  6798. elemental_delete(sd->ed);
  6799. // Leave duel if you die [LuzZza]
  6800. if(battle_config.duel_autoleave_when_die) {
  6801. if(sd->duel_group > 0)
  6802. duel_leave(sd->duel_group, sd);
  6803. if(sd->duel_invite > 0)
  6804. duel_reject(sd->duel_invite, sd);
  6805. }
  6806. pc_close_npc(sd,2); //close npc if we were using one
  6807. /* e.g. not killed thru pc_damage */
  6808. if( pc_issit(sd) ) {
  6809. clif_status_load(&sd->bl,SI_SIT,0);
  6810. }
  6811. pc_setdead(sd);
  6812. pc_setglobalreg(sd, add_str("PC_DIE_COUNTER"), sd->die_counter+1);
  6813. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6814. //Reset menu skills/item skills
  6815. if ((sd->skillitem) != 0)
  6816. sd->skillitem = sd->skillitemlv = 0;
  6817. if ((sd->menuskill_id) != 0)
  6818. sd->menuskill_id = sd->menuskill_val = 0;
  6819. //Reset ticks.
  6820. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6821. if ( sd->spiritball !=0 )
  6822. pc_delspiritball(sd,sd->spiritball,0);
  6823. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6824. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6825. if (src)
  6826. switch (src->type) {
  6827. case BL_MOB:
  6828. {
  6829. struct mob_data *md=(struct mob_data *)src;
  6830. if(md->target_id==sd->bl.id)
  6831. mob_unlocktarget(md,tick);
  6832. if(battle_config.mobs_level_up && md->status.hp &&
  6833. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6834. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6835. ) { // monster level up [Valaris]
  6836. clif_misceffect(&md->bl,0);
  6837. md->level++;
  6838. status_calc_mob(md, SCO_NONE);
  6839. status_percent_heal(src,10,0);
  6840. if( battle_config.show_mob_info&4 )
  6841. {// update name with new level
  6842. clif_name_area(&md->bl);
  6843. }
  6844. }
  6845. src = battle_get_master(src); // Maybe Player Summon
  6846. }
  6847. break;
  6848. case BL_PET: //Pass on to master...
  6849. case BL_HOM:
  6850. case BL_MER:
  6851. src = battle_get_master(src);
  6852. break;
  6853. }
  6854. if (src && src->type == BL_PC) {
  6855. struct map_session_data *ssd = (struct map_session_data *)src;
  6856. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6857. npc_script_event(ssd, NPCE_KILLPC);
  6858. if (battle_config.pk_mode&2) {
  6859. ssd->status.manner -= 5;
  6860. if(ssd->status.manner < 0)
  6861. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6862. #if 0
  6863. // PK/Karma system code (not enabled yet) [celest]
  6864. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6865. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6866. // karma going down = more 'good' / more honourable.
  6867. // The Karma System way...
  6868. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6869. sd->status.karma--;
  6870. ssd->status.karma--;
  6871. }
  6872. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6873. ssd->status.karma++;
  6874. // or the PK System way...
  6875. if (sd->status.karma > 0) // player killed is dishonourable?
  6876. ssd->status.karma--; // honour points earned
  6877. sd->status.karma++; // honour points lost
  6878. // To-do: Receive exp on certain occasions
  6879. #endif
  6880. }
  6881. }
  6882. if(battle_config.bone_drop==2
  6883. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6884. {
  6885. struct item item_tmp;
  6886. memset(&item_tmp,0,sizeof(item_tmp));
  6887. item_tmp.nameid=ITEMID_SKULL_;
  6888. item_tmp.identify=1;
  6889. item_tmp.card[0]=CARD0_CREATE;
  6890. item_tmp.card[1]=0;
  6891. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6892. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6893. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6894. }
  6895. //Remove bonus_script when dead
  6896. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6897. // changed penalty options, added death by player if pk_mode [Valaris]
  6898. if(battle_config.death_penalty_type
  6899. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6900. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg2(sd->bl.m)
  6901. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6902. {
  6903. uint32 base_penalty = 0;
  6904. uint32 job_penalty = 0;
  6905. uint32 zeny_penalty = 0;
  6906. if (pc_isvip(sd)) { // EXP penalty for VIP
  6907. base_penalty = battle_config.vip_exp_penalty_base;
  6908. job_penalty = battle_config.vip_exp_penalty_job;
  6909. zeny_penalty = battle_config.vip_zeny_penalty;
  6910. } else {
  6911. base_penalty = battle_config.death_penalty_base;
  6912. job_penalty = battle_config.death_penalty_job;
  6913. zeny_penalty = battle_config.zeny_penalty;
  6914. }
  6915. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6916. switch (battle_config.death_penalty_type) {
  6917. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6918. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6919. }
  6920. if (base_penalty){ //recheck after altering to speedup
  6921. if (battle_config.pk_mode && src && src->type==BL_PC)
  6922. base_penalty *= 2;
  6923. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6924. }
  6925. }
  6926. else
  6927. base_penalty = 0;
  6928. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6929. switch (battle_config.death_penalty_type) {
  6930. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6931. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6932. }
  6933. if (job_penalty) {
  6934. if (battle_config.pk_mode && src && src->type==BL_PC)
  6935. job_penalty *= 2;
  6936. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6937. }
  6938. }
  6939. else
  6940. job_penalty = 0;
  6941. if (base_penalty || job_penalty)
  6942. pc_lostexp(sd, base_penalty, job_penalty);
  6943. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6944. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6945. if(zeny_penalty)
  6946. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6947. }
  6948. }
  6949. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6950. int j;
  6951. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6952. int id = map[sd->bl.m].drop_list[j].drop_id;
  6953. int type = map[sd->bl.m].drop_list[j].drop_type;
  6954. int per = map[sd->bl.m].drop_list[j].drop_per;
  6955. if(id == 0)
  6956. continue;
  6957. if(id == -1){
  6958. int eq_num=0,eq_n[MAX_INVENTORY];
  6959. memset(eq_n,0,sizeof(eq_n));
  6960. for(i=0;i<MAX_INVENTORY;i++) {
  6961. if( (type == 1 && !sd->inventory.u.items_inventory[i].equip)
  6962. || (type == 2 && sd->inventory.u.items_inventory[i].equip)
  6963. || type == 3)
  6964. {
  6965. int l;
  6966. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6967. if( l < MAX_INVENTORY )
  6968. eq_n[l] = i;
  6969. eq_num++;
  6970. }
  6971. }
  6972. if(eq_num > 0){
  6973. int n = eq_n[rnd()%eq_num];
  6974. if(rnd()%10000 < per) {
  6975. if(sd->inventory.u.items_inventory[n].equip)
  6976. pc_unequipitem(sd,n,3);
  6977. pc_dropitem(sd,n,1);
  6978. }
  6979. }
  6980. }
  6981. else if(id > 0) {
  6982. for(i=0;i<MAX_INVENTORY;i++){
  6983. if(sd->inventory.u.items_inventory[i].nameid == id
  6984. && rnd()%10000 < per
  6985. && ((type == 1 && !sd->inventory.u.items_inventory[i].equip)
  6986. || (type == 2 && sd->inventory.u.items_inventory[i].equip)
  6987. || type == 3) ){
  6988. if(sd->inventory.u.items_inventory[i].equip)
  6989. pc_unequipitem(sd,i,3);
  6990. pc_dropitem(sd,i,1);
  6991. break;
  6992. }
  6993. }
  6994. }
  6995. }
  6996. }
  6997. // pvp
  6998. // disable certain pvp functions on pk_mode [Valaris]
  6999. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  7000. sd->pvp_point -= 5;
  7001. sd->pvp_lost++;
  7002. if( src && src->type == BL_PC ) {
  7003. struct map_session_data *ssd = (struct map_session_data *)src;
  7004. ssd->pvp_point++;
  7005. ssd->pvp_won++;
  7006. }
  7007. if( sd->pvp_point < 0 ) {
  7008. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7009. return 1|8;
  7010. }
  7011. }
  7012. //GvG
  7013. if( map_flag_gvg2(sd->bl.m) ) {
  7014. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7015. return 1|8;
  7016. }
  7017. else if( sd->bg_id ) {
  7018. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7019. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7020. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7021. return 1|8;
  7022. }
  7023. }
  7024. //Reset "can log out" tick.
  7025. if( battle_config.prevent_logout )
  7026. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7027. return 1;
  7028. }
  7029. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7030. if(hp) clif_updatestatus(sd,SP_HP);
  7031. if(sp) clif_updatestatus(sd,SP_SP);
  7032. pc_setstand(sd, true);
  7033. if(battle_config.pc_invincible_time > 0)
  7034. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7035. if( sd->state.gmaster_flag ) {
  7036. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7037. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7038. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7039. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7040. }
  7041. }
  7042. // script
  7043. //
  7044. /*==========================================
  7045. * script reading pc status registry
  7046. *------------------------------------------*/
  7047. int pc_readparam(struct map_session_data* sd,int type)
  7048. {
  7049. int val = 0;
  7050. nullpo_ret(sd);
  7051. switch(type) {
  7052. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7053. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7054. case SP_ZENY: val = sd->status.zeny; break;
  7055. case SP_BASELEVEL: val = sd->status.base_level; break;
  7056. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7057. case SP_CLASS: val = sd->status.class_; break;
  7058. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7059. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7060. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7061. case SP_SEX: val = sd->status.sex; break;
  7062. case SP_WEIGHT: val = sd->weight; break;
  7063. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7064. case SP_BASEEXP: val = sd->status.base_exp; break;
  7065. case SP_JOBEXP: val = sd->status.job_exp; break;
  7066. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7067. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7068. case SP_HP: val = sd->battle_status.hp; break;
  7069. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7070. case SP_SP: val = sd->battle_status.sp; break;
  7071. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7072. case SP_STR: val = sd->status.str; break;
  7073. case SP_AGI: val = sd->status.agi; break;
  7074. case SP_VIT: val = sd->status.vit; break;
  7075. case SP_INT: val = sd->status.int_; break;
  7076. case SP_DEX: val = sd->status.dex; break;
  7077. case SP_LUK: val = sd->status.luk; break;
  7078. case SP_KARMA: val = sd->status.karma; break;
  7079. case SP_MANNER: val = sd->status.manner; break;
  7080. case SP_FAME: val = sd->status.fame; break;
  7081. case SP_KILLERRID: val = sd->killerrid; break;
  7082. case SP_KILLEDRID: val = sd->killedrid; break;
  7083. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7084. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7085. case SP_CHARRENAME: val = sd->status.rename; break;
  7086. case SP_CHARFONT: val = sd->status.font; break;
  7087. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7088. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7089. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7090. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7091. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7092. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7093. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7094. case SP_DEF1: val = sd->battle_status.def; break;
  7095. case SP_DEF2: val = sd->battle_status.def2; break;
  7096. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7097. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7098. case SP_HIT: val = sd->battle_status.hit; break;
  7099. case SP_FLEE1: val = sd->battle_status.flee; break;
  7100. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7101. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7102. case SP_MAXHPRATE: val = sd->hprate; break;
  7103. case SP_MAXSPRATE: val = sd->sprate; break;
  7104. case SP_SPRATE: val = sd->dsprate; break;
  7105. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7106. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7107. case SP_ASPD_RATE:
  7108. #ifndef RENEWAL_ASPD
  7109. val = sd->battle_status.aspd_rate;
  7110. #else
  7111. val = sd->battle_status.aspd_rate2;
  7112. #endif
  7113. break;
  7114. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7115. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7116. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7117. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7118. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7119. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7120. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7121. case SP_MATK_RATE: val = sd->matk_rate; break;
  7122. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7123. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7124. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7125. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7126. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7127. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7128. case SP_HIT_RATE: val = sd->hit_rate; break;
  7129. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7130. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7131. case SP_DEF_RATE: val = sd->def_rate; break;
  7132. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7133. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7134. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7135. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7136. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7137. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7138. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7139. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7140. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7141. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7142. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7143. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7144. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7145. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7146. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7147. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7148. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7149. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7150. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7151. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7152. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7153. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7154. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7155. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7156. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7157. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7158. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7159. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7160. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7161. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7162. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7163. case SP_DELAYRATE: val = sd->delayrate; break;
  7164. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7165. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7166. case SP_UNSTRIPABLE:
  7167. case SP_UNSTRIPABLE_ARMOR:
  7168. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7169. break;
  7170. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7171. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7172. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7173. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7174. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7175. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7176. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7177. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7178. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7179. case SP_EMATK: val = sd->bonus.ematk; break;
  7180. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7181. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7182. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7183. case SP_CASTRATE:
  7184. case SP_VARCASTRATE:
  7185. #ifdef RENEWAL_CAST
  7186. val = sd->bonus.varcastrate; break;
  7187. #else
  7188. val = sd->castrate; break;
  7189. #endif
  7190. default:
  7191. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7192. return -1;
  7193. }
  7194. return val;
  7195. }
  7196. /*==========================================
  7197. * script set pc status registry
  7198. *------------------------------------------*/
  7199. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7200. {
  7201. nullpo_retr(false,sd);
  7202. switch(type){
  7203. case SP_BASELEVEL:
  7204. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7205. val = pc_maxbaselv(sd);
  7206. if ((unsigned int)val > sd->status.base_level) {
  7207. int i = 0;
  7208. int stat=0;
  7209. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7210. stat += pc_gets_status_point(sd->status.base_level + i);
  7211. sd->status.status_point += stat;
  7212. }
  7213. sd->status.base_level = (unsigned int)val;
  7214. sd->status.base_exp = 0;
  7215. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7216. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7217. clif_updatestatus(sd, SP_STATUSPOINT);
  7218. clif_updatestatus(sd, SP_BASEEXP);
  7219. status_calc_pc(sd, SCO_FORCE);
  7220. if(sd->status.party_id)
  7221. party_send_levelup(sd);
  7222. break;
  7223. case SP_JOBLEVEL:
  7224. if ((unsigned int)val >= sd->status.job_level) {
  7225. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7226. sd->status.skill_point += val - sd->status.job_level;
  7227. clif_updatestatus(sd, SP_SKILLPOINT);
  7228. }
  7229. sd->status.job_level = (unsigned int)val;
  7230. sd->status.job_exp = 0;
  7231. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7232. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7233. clif_updatestatus(sd, SP_JOBEXP);
  7234. status_calc_pc(sd, SCO_FORCE);
  7235. break;
  7236. case SP_SKILLPOINT:
  7237. sd->status.skill_point = val;
  7238. break;
  7239. case SP_STATUSPOINT:
  7240. sd->status.status_point = val;
  7241. break;
  7242. case SP_ZENY:
  7243. if( val < 0 )
  7244. return false;// can't set negative zeny
  7245. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7246. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7247. break;
  7248. case SP_BASEEXP:
  7249. {
  7250. val = cap_value(val, 0, INT_MAX);
  7251. if ((unsigned int)val < sd->status.base_exp) // Lost
  7252. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7253. else // Gained
  7254. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7255. }
  7256. return true;
  7257. case SP_JOBEXP:
  7258. {
  7259. val = cap_value(val, 0, INT_MAX);
  7260. if ((unsigned int)val < sd->status.job_exp) // Lost
  7261. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7262. else // Gained
  7263. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7264. }
  7265. return true;
  7266. case SP_SEX:
  7267. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7268. break;
  7269. case SP_WEIGHT:
  7270. sd->weight = val;
  7271. break;
  7272. case SP_MAXWEIGHT:
  7273. sd->max_weight = val;
  7274. break;
  7275. case SP_HP:
  7276. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7277. break;
  7278. case SP_MAXHP:
  7279. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7280. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7281. {
  7282. sd->battle_status.hp = sd->battle_status.max_hp;
  7283. clif_updatestatus(sd, SP_HP);
  7284. }
  7285. break;
  7286. case SP_SP:
  7287. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7288. break;
  7289. case SP_MAXSP:
  7290. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7291. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7292. {
  7293. sd->battle_status.sp = sd->battle_status.max_sp;
  7294. clif_updatestatus(sd, SP_SP);
  7295. }
  7296. break;
  7297. case SP_STR:
  7298. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7299. break;
  7300. case SP_AGI:
  7301. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7302. break;
  7303. case SP_VIT:
  7304. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7305. break;
  7306. case SP_INT:
  7307. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7308. break;
  7309. case SP_DEX:
  7310. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7311. break;
  7312. case SP_LUK:
  7313. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7314. break;
  7315. case SP_KARMA:
  7316. sd->status.karma = val;
  7317. break;
  7318. case SP_MANNER:
  7319. sd->status.manner = val;
  7320. if( val < 0 )
  7321. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7322. else {
  7323. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7324. clif_manner_message(sd, 5);
  7325. }
  7326. return true; // status_change_start/status_change_end already sends packets warning the client
  7327. case SP_FAME:
  7328. sd->status.fame = val;
  7329. break;
  7330. case SP_KILLERRID:
  7331. sd->killerrid = val;
  7332. return true;
  7333. case SP_KILLEDRID:
  7334. sd->killedrid = val;
  7335. return true;
  7336. case SP_CHARMOVE:
  7337. sd->status.character_moves = val;
  7338. return true;
  7339. case SP_CHARRENAME:
  7340. sd->status.rename = val;
  7341. return true;
  7342. case SP_CHARFONT:
  7343. sd->status.font = val;
  7344. clif_font(sd);
  7345. return true;
  7346. case SP_BANK_VAULT:
  7347. if (val < 0)
  7348. return false;
  7349. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7350. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7351. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7352. return true;
  7353. case SP_ROULETTE_BRONZE:
  7354. sd->roulette_point.bronze = val;
  7355. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7356. return true;
  7357. case SP_ROULETTE_SILVER:
  7358. sd->roulette_point.silver = val;
  7359. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7360. return true;
  7361. case SP_ROULETTE_GOLD:
  7362. sd->roulette_point.gold = val;
  7363. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7364. return true;
  7365. default:
  7366. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7367. return false;
  7368. }
  7369. clif_updatestatus(sd,type);
  7370. return true;
  7371. }
  7372. /*==========================================
  7373. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7374. *------------------------------------------*/
  7375. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7376. {
  7377. if (type&2) {
  7378. if (hp || type&4)
  7379. clif_heal(sd->fd,SP_HP,hp);
  7380. if (sp)
  7381. clif_heal(sd->fd,SP_SP,sp);
  7382. } else {
  7383. if(hp)
  7384. clif_updatestatus(sd,SP_HP);
  7385. if(sp)
  7386. clif_updatestatus(sd,SP_SP);
  7387. }
  7388. return;
  7389. }
  7390. /**
  7391. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7392. * @param sd: Player data
  7393. * @param itemid: Item ID
  7394. * @param hp: HP to heal
  7395. * @param sp: SP to heal
  7396. * @return Amount healed to an object
  7397. */
  7398. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7399. {
  7400. int bonus, tmp, penalty = 0;
  7401. if (hp) {
  7402. int i;
  7403. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7404. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7405. if (potion_flag == 2) {
  7406. bonus += 50;
  7407. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7408. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7409. }
  7410. //All item bonuses.
  7411. bonus += sd->bonus.itemhealrate2;
  7412. //Item Group bonuses
  7413. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7414. //Individual item bonuses.
  7415. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
  7416. if (sd->itemhealrate[i].nameid == itemid) {
  7417. bonus += sd->itemhealrate[i].rate;
  7418. break;
  7419. }
  7420. }
  7421. // Recovery Potion
  7422. if (sd->sc.data[SC_INCHEALRATE])
  7423. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7424. // 2014 Halloween Event : Pumpkin Bonus
  7425. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7426. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7427. tmp = hp * bonus / 100; // Overflow check
  7428. if (bonus != 100 && tmp > hp)
  7429. hp = tmp;
  7430. }
  7431. if (sp) {
  7432. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7433. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7434. if (potion_flag == 2)
  7435. bonus += 50;
  7436. tmp = sp * bonus / 100; // Overflow check
  7437. if (bonus != 100 && tmp > sp)
  7438. sp = tmp;
  7439. }
  7440. if (sd->sc.count) {
  7441. // Critical Wound and Death Hurt stack
  7442. if (sd->sc.data[SC_CRITICALWOUND])
  7443. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7444. if (sd->sc.data[SC_DEATHHURT])
  7445. penalty += 20;
  7446. if (sd->sc.data[SC_NORECOVER_STATE])
  7447. penalty = 100;
  7448. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7449. hp += hp / 2; // 1.5 times
  7450. sp -= sp / 2;
  7451. }
  7452. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7453. hp += hp / 10;
  7454. sp += sp / 10;
  7455. }
  7456. if (penalty > 0) {
  7457. hp -= hp * penalty / 100;
  7458. sp -= sp * penalty / 100;
  7459. }
  7460. #ifdef RENEWAL
  7461. if (sd->sc.data[SC_EXTREMITYFIST2])
  7462. sp = 0;
  7463. #endif
  7464. if (sd->sc.data[SC_BITESCAR])
  7465. hp = 0;
  7466. }
  7467. return status_heal(&sd->bl, hp, sp, 1);
  7468. }
  7469. /*==========================================
  7470. * HP/SP Recovery
  7471. * Heal player hp nad/or sp by rate
  7472. *------------------------------------------*/
  7473. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7474. {
  7475. nullpo_ret(sd);
  7476. if (hp > 100) hp = 100;
  7477. else if (hp <-100) hp = -100;
  7478. if (sp > 100) sp = 100;
  7479. else if (sp <-100) sp = -100;
  7480. if(hp >= 0 && sp >= 0) //Heal
  7481. return status_percent_heal(&sd->bl, hp, sp);
  7482. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7483. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7484. //Crossed signs
  7485. if(hp) {
  7486. if(hp > 0)
  7487. status_percent_heal(&sd->bl, hp, 0);
  7488. else
  7489. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7490. }
  7491. if(sp) {
  7492. if(sp > 0)
  7493. status_percent_heal(&sd->bl, 0, sp);
  7494. else
  7495. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7496. }
  7497. return 0;
  7498. }
  7499. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7500. {
  7501. struct mob_data *md;
  7502. int flag;
  7503. md = (struct mob_data *)bl;
  7504. nullpo_ret(md);
  7505. flag = va_arg(ap, int);
  7506. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7507. status_kill(&md->bl);
  7508. return 1;
  7509. }
  7510. /**
  7511. * Called when player changes job
  7512. * Rewrote to make it tidider [Celest]
  7513. * @param sd
  7514. * @param job JOB ID. See enum e_job
  7515. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7516. * @return True if success, false if failed
  7517. **/
  7518. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7519. {
  7520. int i, fame_flag = 0;
  7521. int b_class;
  7522. nullpo_retr(false,sd);
  7523. if (job < 0)
  7524. return false;
  7525. //Normalize job.
  7526. b_class = pc_jobid2mapid(job);
  7527. if (b_class == -1)
  7528. return false;
  7529. switch (upper) {
  7530. case 1:
  7531. b_class|= JOBL_UPPER;
  7532. break;
  7533. case 2:
  7534. b_class|= JOBL_BABY;
  7535. break;
  7536. }
  7537. //This will automatically adjust bard/dancer classes to the correct gender
  7538. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7539. job = pc_mapid2jobid(b_class, sd->status.sex);
  7540. if (job == -1)
  7541. return false;
  7542. if ((unsigned short)b_class == sd->class_)
  7543. return false; //Nothing to change.
  7544. // changing from 1st to 2nd job
  7545. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7546. sd->change_level_2nd = sd->status.job_level;
  7547. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  7548. }
  7549. // changing from 2nd to 3rd job
  7550. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7551. sd->change_level_3rd = sd->status.job_level;
  7552. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  7553. }
  7554. if(sd->cloneskill_idx > 0) {
  7555. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7556. sd->status.skill[sd->cloneskill_idx].id = 0;
  7557. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7558. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7559. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7560. }
  7561. sd->cloneskill_idx = 0;
  7562. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7563. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7564. }
  7565. if(sd->reproduceskill_idx > 0) {
  7566. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7567. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7568. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7569. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7570. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7571. }
  7572. sd->reproduceskill_idx = 0;
  7573. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7574. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7575. }
  7576. // Give or reduce transcendent status points
  7577. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7578. sd->status.status_point += battle_config.transcendent_status_points;
  7579. clif_updatestatus(sd,SP_STATUSPOINT);
  7580. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7581. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7582. // The player already used his bonus points, so we have to reset his status points
  7583. pc_resetstate(sd);
  7584. }
  7585. sd->status.status_point -= battle_config.transcendent_status_points;
  7586. clif_updatestatus(sd,SP_STATUSPOINT);
  7587. }
  7588. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7589. const int class_ = pc_class2idx(sd->status.class_);
  7590. uint16 skill_id;
  7591. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7592. //Remove status specific to your current tree skills.
  7593. enum sc_type sc = status_skill2sc(skill_id);
  7594. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7595. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7596. }
  7597. }
  7598. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7599. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7600. pc_resetfeel(sd);
  7601. }
  7602. // Reset body style to 0 before changing job to avoid
  7603. // errors since not every job has a alternate outfit.
  7604. sd->status.body = 0;
  7605. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7606. sd->status.class_ = job;
  7607. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7608. sd->class_ = (unsigned short)b_class;
  7609. sd->status.job_level=1;
  7610. sd->status.job_exp=0;
  7611. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7612. sd->status.base_level = pc_maxbaselv(sd);
  7613. sd->status.base_exp=0;
  7614. pc_resetstate(sd);
  7615. clif_updatestatus(sd,SP_STATUSPOINT);
  7616. clif_updatestatus(sd,SP_BASELEVEL);
  7617. clif_updatestatus(sd,SP_BASEEXP);
  7618. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7619. }
  7620. clif_updatestatus(sd,SP_JOBLEVEL);
  7621. clif_updatestatus(sd,SP_JOBEXP);
  7622. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7623. for(i=0;i<EQI_MAX;i++) {
  7624. if(sd->equip_index[i] >= 0)
  7625. if(pc_isequip(sd,sd->equip_index[i]))
  7626. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7627. }
  7628. //Change look, if disguised, you need to undisguise
  7629. //to correctly calculate new job sprite without
  7630. if (sd->disguise)
  7631. pc_disguise(sd, 0);
  7632. status_set_viewdata(&sd->bl, job);
  7633. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7634. #if PACKETVER >= 20151001
  7635. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7636. #endif
  7637. if(sd->vd.cloth_color)
  7638. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7639. /*
  7640. if(sd->vd.body_style)
  7641. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7642. */
  7643. //Update skill tree.
  7644. pc_calc_skilltree(sd);
  7645. clif_skillinfoblock(sd);
  7646. if (sd->ed)
  7647. elemental_delete(sd->ed);
  7648. if (sd->state.vending)
  7649. vending_closevending(sd);
  7650. if (sd->state.buyingstore)
  7651. buyingstore_close(sd);
  7652. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7653. //Remove peco/cart/falcon
  7654. i = sd->sc.option;
  7655. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7656. i&=~OPTION_RIDING;
  7657. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7658. i&=~OPTION_FALCON;
  7659. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7660. i&=~OPTION_DRAGON;
  7661. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7662. i&=~OPTION_WUGRIDER;
  7663. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7664. i&=~OPTION_WUG;
  7665. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7666. i&=~OPTION_MADOGEAR;
  7667. #ifndef NEW_CARTS
  7668. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7669. i&=~OPTION_CART;
  7670. #else
  7671. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7672. pc_setcart(sd, 0);
  7673. #endif
  7674. if(i != sd->sc.option)
  7675. pc_setoption(sd, i);
  7676. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7677. hom_vaporize(sd, HOM_ST_ACTIVE);
  7678. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7679. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7680. if(sd->status.manner < 0)
  7681. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7682. status_calc_pc(sd,SCO_FORCE);
  7683. pc_checkallowskill(sd);
  7684. pc_equiplookall(sd);
  7685. pc_show_questinfo(sd);
  7686. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7687. if( sd->status.party_id ){
  7688. struct party_data* p;
  7689. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7690. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7691. if( i < MAX_PARTY ){
  7692. p->party.member[i].class_ = sd->status.class_;
  7693. clif_party_job_and_level(sd);
  7694. }
  7695. }
  7696. }
  7697. chrif_save(sd, CSAVE_NORMAL);
  7698. //if you were previously famous, not anymore.
  7699. if (fame_flag)
  7700. chrif_buildfamelist();
  7701. else if (sd->status.fame > 0) {
  7702. //It may be that now they are famous?
  7703. switch (sd->class_&MAPID_UPPERMASK) {
  7704. case MAPID_BLACKSMITH:
  7705. case MAPID_ALCHEMIST:
  7706. case MAPID_TAEKWON:
  7707. chrif_buildfamelist();
  7708. break;
  7709. }
  7710. }
  7711. return true;
  7712. }
  7713. /*==========================================
  7714. * Tell client player sd has change equipement
  7715. *------------------------------------------*/
  7716. void pc_equiplookall(struct map_session_data *sd)
  7717. {
  7718. nullpo_retv(sd);
  7719. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7720. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7721. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7722. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7723. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7724. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7725. }
  7726. /*==========================================
  7727. * Tell client player sd has change look (hair,equip...)
  7728. *------------------------------------------*/
  7729. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7730. nullpo_retv(sd);
  7731. switch(type) {
  7732. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7733. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7734. if (sd->status.hair != val) {
  7735. sd->status.hair = val;
  7736. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7737. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7738. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7739. }
  7740. break;
  7741. case LOOK_WEAPON:
  7742. sd->status.weapon = val;
  7743. break;
  7744. case LOOK_HEAD_BOTTOM:
  7745. sd->status.head_bottom = val;
  7746. sd->setlook_head_bottom = val;
  7747. break;
  7748. case LOOK_HEAD_TOP:
  7749. sd->status.head_top = val;
  7750. sd->setlook_head_top = val;
  7751. break;
  7752. case LOOK_HEAD_MID:
  7753. sd->status.head_mid = val;
  7754. sd->setlook_head_mid = val;
  7755. break;
  7756. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7757. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7758. if (sd->status.hair_color != val) {
  7759. sd->status.hair_color = val;
  7760. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7761. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7762. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7763. }
  7764. break;
  7765. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7766. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7767. sd->status.clothes_color = val;
  7768. break;
  7769. case LOOK_SHIELD:
  7770. sd->status.shield = val;
  7771. break;
  7772. case LOOK_SHOES:
  7773. break;
  7774. case LOOK_ROBE:
  7775. sd->status.robe = val;
  7776. sd->setlook_robe = val;
  7777. break;
  7778. case LOOK_BODY2:
  7779. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7780. sd->status.body = val;
  7781. break;
  7782. }
  7783. clif_changelook(&sd->bl, type, val);
  7784. }
  7785. /*==========================================
  7786. * Give an option (type) to player (sd) and display it to client
  7787. *------------------------------------------*/
  7788. void pc_setoption(struct map_session_data *sd,int type)
  7789. {
  7790. int p_type, new_look=0;
  7791. nullpo_retv(sd);
  7792. p_type = sd->sc.option;
  7793. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7794. sd->sc.option=type;
  7795. clif_changeoption(&sd->bl);
  7796. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7797. { // Mounting
  7798. clif_status_load(&sd->bl,SI_RIDING,1);
  7799. status_calc_pc(sd,SCO_NONE);
  7800. }
  7801. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7802. { // Dismount
  7803. clif_status_load(&sd->bl,SI_RIDING,0);
  7804. status_calc_pc(sd,SCO_NONE);
  7805. }
  7806. #ifndef NEW_CARTS
  7807. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7808. clif_cartlist(sd);
  7809. clif_updatestatus(sd, SP_CARTINFO);
  7810. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7811. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7812. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7813. clif_clearcart(sd->fd);
  7814. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7815. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7816. }
  7817. #endif
  7818. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7819. clif_status_load(&sd->bl,SI_FALCON,1);
  7820. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7821. clif_status_load(&sd->bl,SI_FALCON,0);
  7822. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7823. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7824. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7825. status_calc_pc(sd,SCO_NONE);
  7826. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7827. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7828. status_calc_pc(sd,SCO_NONE);
  7829. }
  7830. }
  7831. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7832. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7833. status_calc_pc(sd,SCO_NONE);
  7834. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7835. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7836. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7837. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7838. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7839. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7840. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7841. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7842. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7843. status_calc_pc(sd,SCO_NONE);
  7844. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7845. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7846. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7847. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7848. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7849. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7850. }
  7851. }
  7852. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7853. new_look = JOB_STAR_GLADIATOR2;
  7854. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7855. new_look = -1;
  7856. if (sd->disguise || !new_look)
  7857. return; //Disguises break sprite changes
  7858. if (new_look < 0) { //Restore normal look.
  7859. status_set_viewdata(&sd->bl, sd->status.class_);
  7860. new_look = sd->vd.class_;
  7861. }
  7862. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7863. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7864. if (sd->vd.cloth_color)
  7865. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7866. if( sd->vd.body_style )
  7867. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7868. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7869. }
  7870. /**
  7871. * Give player a cart
  7872. * @param sd Player
  7873. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  7874. **/
  7875. bool pc_setcart(struct map_session_data *sd,int type) {
  7876. #ifndef NEW_CARTS
  7877. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7878. int option;
  7879. #endif
  7880. nullpo_retr(false,sd);
  7881. if( type < 0 || type > MAX_CARTS )
  7882. return false;// Never trust the values sent by the client! [Skotlex]
  7883. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7884. return false;// Push cart is required
  7885. #ifdef NEW_CARTS
  7886. switch( type ) {
  7887. case 0:
  7888. if( !sd->sc.data[SC_PUSH_CART] )
  7889. return 0;
  7890. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7891. clif_clearcart(sd->fd);
  7892. break;
  7893. default:/* everything else is an allowed ID so we can move on */
  7894. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  7895. clif_cartlist(sd);
  7896. status_calc_cart_weight(sd, CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE);
  7897. }
  7898. clif_updatestatus(sd, SP_CARTINFO);
  7899. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  7900. break;
  7901. }
  7902. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7903. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7904. #else
  7905. // Update option
  7906. option = sd->sc.option;
  7907. option &= ~OPTION_CART;// clear cart bits
  7908. option |= cart[type]; // set cart
  7909. pc_setoption(sd, option);
  7910. #endif
  7911. return true;
  7912. }
  7913. /*==========================================
  7914. * Give player a falcon
  7915. *------------------------------------------*/
  7916. void pc_setfalcon(struct map_session_data* sd, int flag)
  7917. {
  7918. if( flag ){
  7919. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7920. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7921. } else if( pc_isfalcon(sd) ){
  7922. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7923. }
  7924. }
  7925. /*==========================================
  7926. * Set player riding
  7927. *------------------------------------------*/
  7928. void pc_setriding(struct map_session_data* sd, int flag)
  7929. {
  7930. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7931. return;
  7932. if( flag ){
  7933. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7934. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7935. } else if( pc_isriding(sd) ){
  7936. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7937. }
  7938. }
  7939. /*==========================================
  7940. * Give player a mado
  7941. *------------------------------------------*/
  7942. void pc_setmadogear(struct map_session_data* sd, int flag)
  7943. {
  7944. if( flag ){
  7945. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7946. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7947. } else if( pc_ismadogear(sd) ){
  7948. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7949. }
  7950. }
  7951. /*==========================================
  7952. * Check if player can drop an item
  7953. *------------------------------------------*/
  7954. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7955. {
  7956. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7957. return false;
  7958. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7959. return false;
  7960. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7961. }
  7962. /**
  7963. * Determines whether a player can attack based on status changes
  7964. * Why not use status_check_skilluse?
  7965. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7966. * Even ground-based attacks should be blocked by these statuses
  7967. * Called from unit_attack and unit_attack_timer_sub
  7968. * @retval true Can attack
  7969. **/
  7970. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7971. nullpo_retr(false, sd);
  7972. if (!&sd->sc)
  7973. return true;
  7974. if( sd->sc.data[SC_BASILICA] ||
  7975. sd->sc.data[SC__SHADOWFORM] ||
  7976. sd->sc.data[SC__MANHOLE] ||
  7977. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7978. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7979. sd->sc.data[SC_CRYSTALIZE] ||
  7980. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7981. sd->sc.data[SC_TRICKDEAD] ||
  7982. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7983. sd->sc.data[SC_BLADESTOP] ||
  7984. sd->sc.data[SC_DEEPSLEEP] ||
  7985. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  7986. sd->sc.data[SC_KINGS_GRACE] )
  7987. return false;
  7988. return true;
  7989. }
  7990. /*==========================================
  7991. * Read '@type' variables (temporary numeric char reg)
  7992. *------------------------------------------*/
  7993. int pc_readreg(struct map_session_data* sd, int64 reg)
  7994. {
  7995. return i64db_iget(sd->regs.vars, reg);
  7996. }
  7997. /*==========================================
  7998. * Set '@type' variables (temporary numeric char reg)
  7999. *------------------------------------------*/
  8000. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8001. {
  8002. unsigned int index = script_getvaridx(reg);
  8003. nullpo_retr(false, sd);
  8004. if( val ) {
  8005. i64db_iput(sd->regs.vars, reg, val);
  8006. if( index )
  8007. script_array_update(&sd->regs, reg, false);
  8008. } else {
  8009. i64db_remove(sd->regs.vars, reg);
  8010. if( index )
  8011. script_array_update(&sd->regs, reg, true);
  8012. }
  8013. return true;
  8014. }
  8015. /*==========================================
  8016. * Read '@type$' variables (temporary string char reg)
  8017. *------------------------------------------*/
  8018. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8019. {
  8020. struct script_reg_str *p = NULL;
  8021. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8022. return p ? p->value : NULL;
  8023. }
  8024. /*==========================================
  8025. * Set '@type$' variables (temporary string char reg)
  8026. *------------------------------------------*/
  8027. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8028. {
  8029. struct script_reg_str *p = NULL;
  8030. unsigned int index = script_getvaridx(reg);
  8031. DBData prev;
  8032. nullpo_retr(false, sd);
  8033. if( str[0] ) {
  8034. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8035. p->value = aStrdup(str);
  8036. p->flag.type = 1;
  8037. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8038. p = (struct script_reg_str *)db_data2ptr(&prev);
  8039. if( p->value )
  8040. aFree(p->value);
  8041. ers_free(str_reg_ers, p);
  8042. } else {
  8043. if( index )
  8044. script_array_update(&sd->regs, reg, false);
  8045. }
  8046. } else {
  8047. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8048. p = (struct script_reg_str *)db_data2ptr(&prev);
  8049. if( p->value )
  8050. aFree(p->value);
  8051. ers_free(str_reg_ers, p);
  8052. if( index )
  8053. script_array_update(&sd->regs, reg, true);
  8054. }
  8055. }
  8056. return true;
  8057. }
  8058. /**
  8059. * Serves the following variable types:
  8060. * - 'type' (permanent numeric char reg)
  8061. * - '#type' (permanent numeric account reg)
  8062. * - '##type' (permanent numeric account reg2)
  8063. **/
  8064. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8065. {
  8066. struct script_reg_num *p = NULL;
  8067. if (!sd->vars_ok) {
  8068. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8069. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8070. //intif->request_registry(sd,type==3?4:type);
  8071. set_eof(sd->fd);
  8072. return 0;
  8073. }
  8074. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8075. return p ? p->value : 0;
  8076. }
  8077. /**
  8078. * Serves the following variable types:
  8079. * - 'type$' (permanent str char reg)
  8080. * - '#type$' (permanent str account reg)
  8081. * - '##type$' (permanent str account reg2)
  8082. **/
  8083. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8084. {
  8085. struct script_reg_str *p = NULL;
  8086. if (!sd->vars_ok) {
  8087. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8088. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8089. //intif->request_registry(sd,type==3?4:type);
  8090. set_eof(sd->fd);
  8091. return NULL;
  8092. }
  8093. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8094. return p ? p->value : NULL;
  8095. }
  8096. /**
  8097. * Serves the following variable types:
  8098. * - 'type' (permanent numeric char reg)
  8099. * - '#type' (permanent numeric account reg)
  8100. * - '##type' (permanent numeric account reg2)
  8101. **/
  8102. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8103. {
  8104. struct script_reg_num *p = NULL;
  8105. const char *regname = get_str(script_getvarid(reg));
  8106. unsigned int index = script_getvaridx(reg);
  8107. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  8108. switch( regname[0] ) {
  8109. default: //Char reg
  8110. if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  8111. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  8112. sd->die_counter = val;
  8113. if( i )
  8114. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  8115. } else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
  8116. val = cap_value(val, 0, 1999);
  8117. sd->cook_mastery = val;
  8118. }
  8119. break;
  8120. case '#':
  8121. if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
  8122. val = cap_value(val, 0, MAX_ZENY);
  8123. sd->cashPoints = val;
  8124. } else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  8125. val = cap_value(val, 0, MAX_ZENY);
  8126. sd->kafraPoints = val;
  8127. }
  8128. break;
  8129. }
  8130. if ( !reg_load && !sd->vars_ok ) {
  8131. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8132. return 0;
  8133. }
  8134. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8135. if( val ) {
  8136. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8137. script_array_update(&sd->regs, reg, false);
  8138. p->value = val;
  8139. } else {
  8140. p->value = 0;
  8141. if( index )
  8142. script_array_update(&sd->regs, reg, true);
  8143. }
  8144. if (!reg_load)
  8145. p->flag.update = 1;/* either way, it will require either delete or replace */
  8146. } else if( val ) {
  8147. DBData prev;
  8148. if( index )
  8149. script_array_update(&sd->regs, reg, false);
  8150. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8151. p->value = val;
  8152. if (!reg_load)
  8153. p->flag.update = 1;
  8154. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8155. p = (struct script_reg_num *)db_data2ptr(&prev);
  8156. ers_free(num_reg_ers, p);
  8157. }
  8158. }
  8159. if (!reg_load && p)
  8160. sd->vars_dirty = true;
  8161. return 1;
  8162. }
  8163. /**
  8164. * Serves the following variable types:
  8165. * - 'type$' (permanent str char reg)
  8166. * - '#type$' (permanent str account reg)
  8167. * - '##type$' (permanent str account reg2)
  8168. **/
  8169. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8170. {
  8171. struct script_reg_str *p = NULL;
  8172. const char *regname = get_str(script_getvarid(reg));
  8173. unsigned int index = script_getvaridx(reg);
  8174. if (!reg_load && !sd->vars_ok) {
  8175. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8176. return 0;
  8177. }
  8178. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8179. if( val[0] ) {
  8180. if( p->value )
  8181. aFree(p->value);
  8182. else if ( index ) // an entry that was deleted, so we reset
  8183. script_array_update(&sd->regs, reg, false);
  8184. p->value = aStrdup(val);
  8185. } else {
  8186. if (p->value)
  8187. aFree(p->value);
  8188. p->value = NULL;
  8189. if( index )
  8190. script_array_update(&sd->regs, reg, true);
  8191. }
  8192. if( !reg_load )
  8193. p->flag.update = 1; // either way, it will require either delete or replace
  8194. } else if( val[0] ) {
  8195. DBData prev;
  8196. if( index )
  8197. script_array_update(&sd->regs, reg, false);
  8198. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8199. p->value = aStrdup(val);
  8200. if( !reg_load )
  8201. p->flag.update = 1;
  8202. p->flag.type = 1;
  8203. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8204. p = (struct script_reg_str *)db_data2ptr(&prev);
  8205. if( p->value )
  8206. aFree(p->value);
  8207. ers_free(str_reg_ers, p);
  8208. }
  8209. }
  8210. if( !reg_load && p )
  8211. sd->vars_dirty = true;
  8212. return 1;
  8213. }
  8214. /**
  8215. * Set value of player variable
  8216. * @param sd Player
  8217. * @param reg Variable name
  8218. * @param value
  8219. * @return True if success, false if failed.
  8220. **/
  8221. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8222. char prefix = reg[0];
  8223. nullpo_retr(false, sd);
  8224. if (reg[strlen(reg)-1] == '$') {
  8225. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8226. return false;
  8227. }
  8228. val = cap_value(val, INT_MIN, INT_MAX);
  8229. switch (prefix) {
  8230. case '.':
  8231. case '\'':
  8232. case '$':
  8233. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8234. return false;
  8235. case '@':
  8236. return pc_setreg(sd, add_str(reg), val);
  8237. case '#':
  8238. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8239. default:
  8240. return pc_setglobalreg(sd, add_str(reg), val);
  8241. }
  8242. return false;
  8243. }
  8244. /**
  8245. * Get value of player variable
  8246. * @param sd Player
  8247. * @param reg Variable name
  8248. * @return Variable value or 0 if failed.
  8249. **/
  8250. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8251. char prefix = reg[0];
  8252. nullpo_ret(sd);
  8253. if (reg[strlen(reg)-1] == '$') {
  8254. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8255. return 0;
  8256. }
  8257. switch (prefix) {
  8258. case '.':
  8259. case '\'':
  8260. case '$':
  8261. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8262. return 0;
  8263. case '@':
  8264. return pc_readreg(sd, add_str(reg));
  8265. case '#':
  8266. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8267. default:
  8268. return pc_readglobalreg(sd, add_str(reg));
  8269. }
  8270. return 0;
  8271. }
  8272. /*==========================================
  8273. * Exec eventtimer for player sd (retrieved from map_session (id))
  8274. *------------------------------------------*/
  8275. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8276. {
  8277. struct map_session_data *sd=map_id2sd(id);
  8278. char *p = (char *)data;
  8279. int i;
  8280. if(sd==NULL)
  8281. return 0;
  8282. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8283. if( i < MAX_EVENTTIMER )
  8284. {
  8285. sd->eventtimer[i] = INVALID_TIMER;
  8286. sd->eventcount--;
  8287. npc_event(sd,p,0);
  8288. }
  8289. else
  8290. ShowError("pc_eventtimer: no such event timer\n");
  8291. if (p) aFree(p);
  8292. return 0;
  8293. }
  8294. /*==========================================
  8295. * Add eventtimer for player sd ?
  8296. *------------------------------------------*/
  8297. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8298. {
  8299. int i;
  8300. nullpo_retr(false,sd);
  8301. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8302. if( i == MAX_EVENTTIMER )
  8303. return false;
  8304. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8305. sd->eventcount++;
  8306. return true;
  8307. }
  8308. /*==========================================
  8309. * Del eventtimer for player sd ?
  8310. *------------------------------------------*/
  8311. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8312. {
  8313. char* p = NULL;
  8314. int i;
  8315. nullpo_retr(false,sd);
  8316. if (sd->eventcount == 0)
  8317. return false;
  8318. // find the named event timer
  8319. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8320. sd->eventtimer[i] != INVALID_TIMER &&
  8321. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8322. strcmp(p, name) == 0
  8323. );
  8324. if( i == MAX_EVENTTIMER )
  8325. return false; // not found
  8326. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8327. sd->eventtimer[i] = INVALID_TIMER;
  8328. if(sd->eventcount > 0)
  8329. sd->eventcount--;
  8330. aFree(p);
  8331. return true;
  8332. }
  8333. /*==========================================
  8334. * Update eventtimer count for player sd
  8335. *------------------------------------------*/
  8336. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8337. {
  8338. int i;
  8339. nullpo_retv(sd);
  8340. for(i=0;i<MAX_EVENTTIMER;i++)
  8341. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8342. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8343. addtick_timer(sd->eventtimer[i],tick);
  8344. break;
  8345. }
  8346. }
  8347. /*==========================================
  8348. * Remove all eventtimer for player sd
  8349. *------------------------------------------*/
  8350. void pc_cleareventtimer(struct map_session_data *sd)
  8351. {
  8352. int i;
  8353. nullpo_retv(sd);
  8354. if (sd->eventcount == 0)
  8355. return;
  8356. for(i=0;i<MAX_EVENTTIMER;i++){
  8357. if( sd->eventtimer[i] != INVALID_TIMER ){
  8358. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8359. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8360. sd->eventtimer[i] = INVALID_TIMER;
  8361. if(sd->eventcount > 0) //avoid looping to max val
  8362. sd->eventcount--;
  8363. if (p) aFree(p);
  8364. }
  8365. }
  8366. }
  8367. /**
  8368. * Called when an item with combo is worn
  8369. * @param *sd
  8370. * @param *data struct item_data
  8371. * @return success numbers of succeed combo
  8372. */
  8373. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8374. uint16 i;
  8375. int success = 0;
  8376. for( i = 0; i < data->combos_count; i++ ) {
  8377. struct itemchk {
  8378. int idx;
  8379. unsigned short nameid;
  8380. short card[MAX_SLOTS];
  8381. } *combo_idx;
  8382. int idx, j;
  8383. int nb_itemCombo;
  8384. unsigned int pos = 0;
  8385. /* ensure this isn't a duplicate combo */
  8386. if( sd->combos.bonus != NULL ) {
  8387. int x;
  8388. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8389. /* found a match, skip this combo */
  8390. if( x < sd->combos.count )
  8391. continue;
  8392. }
  8393. nb_itemCombo = data->combos[i]->count;
  8394. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8395. continue;
  8396. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8397. for(j=0; j < nb_itemCombo; j++){
  8398. combo_idx[j].idx=-1;
  8399. combo_idx[j].nameid=-1;
  8400. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8401. }
  8402. for( j = 0; j < nb_itemCombo; j++ ) {
  8403. uint16 id = data->combos[i]->nameid[j], k;
  8404. bool found = false;
  8405. for( k = 0; k < EQI_MAX; k++ ) {
  8406. short index = sd->equip_index[k];
  8407. if( index < 0 )
  8408. continue;
  8409. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8410. continue;
  8411. if (!sd->inventory_data[index] )
  8412. continue;
  8413. if ( itemdb_type(id) != IT_CARD ) {
  8414. if ( sd->inventory_data[index]->nameid != id )
  8415. continue;
  8416. if(j>0){ //check if this item not already used
  8417. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8418. uint8 z;
  8419. for (z = 0; z < nb_itemCombo-1; z++)
  8420. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8421. do_continue=true;
  8422. if(do_continue)
  8423. continue;
  8424. }
  8425. combo_idx[j].nameid = id;
  8426. combo_idx[j].idx = index;
  8427. pos |= sd->inventory.u.items_inventory[index].equip;
  8428. found = true;
  8429. break;
  8430. } else { //Cards and enchants
  8431. uint16 z;
  8432. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8433. continue;
  8434. for (z = 0; z < MAX_SLOTS; z++) {
  8435. bool do_continue=false;
  8436. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8437. continue;
  8438. if(j>0){
  8439. int c1, c2;
  8440. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8441. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8442. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8443. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8444. do_continue = true;
  8445. break;
  8446. }
  8447. }
  8448. }
  8449. }
  8450. }
  8451. if(do_continue)
  8452. continue;
  8453. combo_idx[j].nameid = id;
  8454. combo_idx[j].idx = index;
  8455. combo_idx[j].card[z] = id;
  8456. pos |= sd->inventory.u.items_inventory[index].equip;
  8457. found = true;
  8458. break;
  8459. }
  8460. }
  8461. }
  8462. if( !found )
  8463. break;/* we haven't found all the ids for this combo, so we can return */
  8464. }
  8465. aFree(combo_idx);
  8466. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8467. if( j < nb_itemCombo )
  8468. continue;
  8469. /* we got here, means all items in the combo are matching */
  8470. idx = sd->combos.count;
  8471. if( sd->combos.bonus == NULL ) {
  8472. CREATE(sd->combos.bonus, struct script_code *, 1);
  8473. CREATE(sd->combos.id, unsigned short, 1);
  8474. CREATE(sd->combos.pos, unsigned int, 1);
  8475. sd->combos.count = 1;
  8476. } else {
  8477. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8478. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8479. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8480. }
  8481. /* we simply copy the pointer */
  8482. sd->combos.bonus[idx] = data->combos[i]->script;
  8483. /* save this combo's id */
  8484. sd->combos.id[idx] = data->combos[i]->id;
  8485. /* save pos of combo*/
  8486. sd->combos.pos[idx] = pos;
  8487. success++;
  8488. }
  8489. return success;
  8490. }
  8491. /**
  8492. * Called when an item with combo is removed
  8493. * @param *sd
  8494. * @param *data struct item_data
  8495. * @return retval numbers of removed combo
  8496. */
  8497. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8498. int i, retval = 0;
  8499. if( sd->combos.bonus == NULL )
  8500. return 0;/* nothing to do here, player has no combos */
  8501. for( i = 0; i < data->combos_count; i++ ) {
  8502. /* check if this combo exists in this user */
  8503. int x = 0, cursor = 0, j;
  8504. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8505. /* no match, skip this combo */
  8506. if(x >= sd->combos.count)
  8507. continue;
  8508. sd->combos.bonus[x] = NULL;
  8509. sd->combos.id[x] = 0;
  8510. sd->combos.pos[x] = 0;
  8511. retval++;
  8512. /* check if combo requirements still fit */
  8513. if( pc_checkcombo( sd, data ) )
  8514. continue;
  8515. /* move next value to empty slot */
  8516. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8517. if( sd->combos.bonus[j] == NULL )
  8518. continue;
  8519. if( cursor != j ) {
  8520. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8521. sd->combos.id[cursor] = sd->combos.id[j];
  8522. sd->combos.pos[cursor] = sd->combos.pos[j];
  8523. }
  8524. cursor++;
  8525. }
  8526. /* it's empty, we can clear all the memory */
  8527. if( (sd->combos.count = cursor) == 0 ) {
  8528. aFree(sd->combos.bonus);
  8529. aFree(sd->combos.id);
  8530. aFree(sd->combos.pos);
  8531. sd->combos.bonus = NULL;
  8532. sd->combos.id = NULL;
  8533. sd->combos.pos = NULL;
  8534. return retval; /* we also can return at this point for we have no more combos to check */
  8535. }
  8536. }
  8537. return retval;
  8538. }
  8539. /**
  8540. * Load combo data(s) of player
  8541. * @param *sd
  8542. * @return ret numbers of succeed combo
  8543. */
  8544. int pc_load_combo(struct map_session_data *sd) {
  8545. int i, ret = 0;
  8546. for( i = 0; i < EQI_MAX; i++ ) {
  8547. struct item_data *id = NULL;
  8548. short idx = sd->equip_index[i];
  8549. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8550. continue;
  8551. if( id->combos_count )
  8552. ret += pc_checkcombo(sd,id);
  8553. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8554. struct item_data *data;
  8555. int j;
  8556. for( j = 0; j < id->slot; j++ ) {
  8557. if (!sd->inventory.u.items_inventory[idx].card[j])
  8558. continue;
  8559. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8560. if( data->combos_count )
  8561. ret += pc_checkcombo(sd,data);
  8562. }
  8563. }
  8564. }
  8565. }
  8566. return ret;
  8567. }
  8568. /*==========================================
  8569. * Equip item on player sd at req_pos from inventory index n
  8570. * return: false - fail; true - success
  8571. *------------------------------------------*/
  8572. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8573. {
  8574. int i, pos, flag = 0, iflag;
  8575. struct item_data *id;
  8576. uint8 res = ITEM_EQUIP_ACK_OK;
  8577. nullpo_retr(false,sd);
  8578. if( n < 0 || n >= MAX_INVENTORY ) {
  8579. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8580. return false;
  8581. }
  8582. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8583. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8584. return false;
  8585. }
  8586. if (!(id = sd->inventory_data[n]))
  8587. return false;
  8588. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8589. if(battle_config.battle_log)
  8590. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8591. if((res = pc_isequip(sd,n))) {
  8592. clif_equipitemack(sd,n,0,res); // fail
  8593. return false;
  8594. }
  8595. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8596. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8597. return false;
  8598. }
  8599. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8600. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8601. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8602. return false;
  8603. }
  8604. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8605. /** Failing condition:
  8606. * 1. Always failed to equip ammo if no weapon equipped yet
  8607. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8608. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8609. * (4. The rest is relying on item job/class restriction).
  8610. **/
  8611. if (id->type == IT_AMMO) {
  8612. int w_idx = sd->equip_index[EQI_HAND_R];
  8613. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8614. if (w_idx == -1 ||
  8615. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8616. (id->look != A_GRENADE && w_type == W_GRENADE))
  8617. {
  8618. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8619. return false;
  8620. }
  8621. }
  8622. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8623. int a_idx = sd->equip_index[EQI_AMMO];
  8624. if (a_idx != -1) {
  8625. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8626. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8627. (a_type != A_GRENADE && id->look == W_GRENADE))
  8628. {
  8629. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8630. return false;
  8631. }
  8632. }
  8633. }
  8634. }
  8635. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8636. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8637. clif_notify_bindOnEquip(sd,n);
  8638. }
  8639. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8640. pos = req_pos&EQP_ACC;
  8641. if (pos == EQP_ACC) //User specified both slots..
  8642. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8643. }
  8644. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8645. pos = req_pos&EQP_SHADOW_ACC;
  8646. if (pos == EQP_SHADOW_ACC)
  8647. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8648. }
  8649. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8650. pos = (req_pos&EQP_ARMS);
  8651. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8652. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8653. }
  8654. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8655. //Update skill-block range database when weapon range changes. [Skotlex]
  8656. i = sd->equip_index[EQI_HAND_R];
  8657. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8658. flag = 1;
  8659. else
  8660. flag = id->range != sd->inventory_data[i]->range;
  8661. }
  8662. for(i=0;i<EQI_MAX;i++) {
  8663. if(pos & equip_bitmask[i]) {
  8664. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8665. pc_unequipitem(sd,sd->equip_index[i],2);
  8666. sd->equip_index[i] = n;
  8667. }
  8668. }
  8669. if(pos==EQP_AMMO) {
  8670. clif_arrowequip(sd,n);
  8671. clif_arrow_fail(sd,3);
  8672. }
  8673. else
  8674. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8675. sd->inventory.u.items_inventory[n].equip = pos;
  8676. if(pos & EQP_HAND_R) {
  8677. if(id)
  8678. sd->weapontype1 = id->look;
  8679. else
  8680. sd->weapontype1 = 0;
  8681. pc_calcweapontype(sd);
  8682. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8683. }
  8684. if(pos & EQP_HAND_L) {
  8685. if(id) {
  8686. if(id->type == IT_WEAPON) {
  8687. sd->status.shield = 0;
  8688. sd->weapontype2 = id->look;
  8689. }
  8690. else
  8691. if(id->type == IT_ARMOR) {
  8692. sd->status.shield = id->look;
  8693. sd->weapontype2 = 0;
  8694. }
  8695. }
  8696. else
  8697. sd->status.shield = sd->weapontype2 = 0;
  8698. pc_calcweapontype(sd);
  8699. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8700. }
  8701. if(pos & EQP_SHOES)
  8702. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8703. pc_set_costume_view(sd);
  8704. pc_checkallowskill(sd); //Check if status changes should be halted.
  8705. iflag = sd->npc_item_flag;
  8706. /* check for combos (MUST be before status_calc_pc) */
  8707. if( id->combos_count )
  8708. pc_checkcombo(sd,id);
  8709. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8710. ; //No cards
  8711. else {
  8712. for( i = 0; i < id->slot; i++ ) {
  8713. struct item_data *data;
  8714. if (!sd->inventory.u.items_inventory[n].card[i])
  8715. continue;
  8716. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8717. if( data->combos_count )
  8718. pc_checkcombo(sd,data);
  8719. }
  8720. }
  8721. }
  8722. status_calc_pc(sd,SCO_NONE);
  8723. if (flag) //Update skill data
  8724. clif_skillinfoblock(sd);
  8725. //OnEquip script [Skotlex]
  8726. if (id) {
  8727. //only run the script if item isn't restricted
  8728. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8729. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8730. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8731. ; //No cards
  8732. else {
  8733. for( i = 0; i < id->slot; i++ ) {
  8734. struct item_data *data;
  8735. if (!sd->inventory.u.items_inventory[n].card[i])
  8736. continue;
  8737. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8738. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8739. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8740. }
  8741. }
  8742. }
  8743. }
  8744. sd->npc_item_flag = iflag;
  8745. return true;
  8746. }
  8747. /*==========================================
  8748. * Called when attemting to unequip an item from player
  8749. * type:
  8750. * 0 - only unequip
  8751. * 1 - calculate status after unequipping
  8752. * 2 - force unequip
  8753. * return: false - fail; true - success
  8754. *------------------------------------------*/
  8755. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8756. int i, iflag;
  8757. bool status_cacl = false;
  8758. nullpo_retr(false,sd);
  8759. if (n < 0 || n >= MAX_INVENTORY) {
  8760. clif_unequipitemack(sd,0,0,0);
  8761. return false;
  8762. }
  8763. if (!sd->inventory.u.items_inventory[n].equip) {
  8764. clif_unequipitemack(sd,n,0,0);
  8765. return false; //Nothing to unequip
  8766. }
  8767. // status change that makes player cannot unequip equipment
  8768. if (!(flag&2) && sd->sc.count &&
  8769. (sd->sc.data[SC_BERSERK] ||
  8770. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8771. sd->sc.data[SC__BLOODYLUST] ||
  8772. sd->sc.data[SC_KYOUGAKU] ||
  8773. (sd->sc.data[SC_PYROCLASTIC] &&
  8774. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8775. {
  8776. clif_unequipitemack(sd,n,0,0);
  8777. return false;
  8778. }
  8779. if (battle_config.battle_log)
  8780. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->inventory.u.items_inventory[n].equip);
  8781. if (!sd->inventory.u.items_inventory[n].equip) { //Nothing to unequip
  8782. clif_unequipitemack(sd, n, 0, 0);
  8783. return false;
  8784. }
  8785. for(i = 0; i < EQI_MAX; i++) {
  8786. if (sd->inventory.u.items_inventory[n].equip & equip_bitmask[i])
  8787. sd->equip_index[i] = -1;
  8788. }
  8789. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_R) {
  8790. sd->weapontype1 = 0;
  8791. sd->status.weapon = sd->weapontype2;
  8792. pc_calcweapontype(sd);
  8793. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8794. if( !battle_config.dancing_weaponswitch_fix )
  8795. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8796. }
  8797. if(sd->inventory.u.items_inventory[n].equip & EQP_HAND_L) {
  8798. sd->status.shield = sd->weapontype2 = 0;
  8799. pc_calcweapontype(sd);
  8800. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8801. }
  8802. if(sd->inventory.u.items_inventory[n].equip & EQP_SHOES)
  8803. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8804. clif_unequipitemack(sd,n,sd->inventory.u.items_inventory[n].equip,1);
  8805. pc_set_costume_view(sd);
  8806. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8807. // On weapon change (right and left hand)
  8808. if ((sd->inventory.u.items_inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8809. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8810. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8811. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8812. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8813. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8814. }
  8815. // On armor change
  8816. if (sd->inventory.u.items_inventory[n].equip & EQP_ARMOR) {
  8817. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8818. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8819. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8820. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8821. }
  8822. // On ammo change
  8823. if (sd->inventory_data[n]->type == IT_AMMO)
  8824. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8825. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8826. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8827. sd->inventory.u.items_inventory[n].equip = 0;
  8828. iflag = sd->npc_item_flag;
  8829. /* check for combos (MUST be before status_calc_pc) */
  8830. if ( sd->inventory_data[n] ) {
  8831. if( sd->inventory_data[n]->combos_count ) {
  8832. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8833. status_cacl = true;
  8834. } if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8835. ; //No cards
  8836. else {
  8837. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8838. struct item_data *data;
  8839. if (!sd->inventory.u.items_inventory[n].card[i])
  8840. continue;
  8841. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8842. if( data->combos_count ) {
  8843. if( pc_removecombo(sd,data) )
  8844. status_cacl = true;
  8845. }
  8846. }
  8847. }
  8848. }
  8849. }
  8850. if(flag&1 || status_cacl) {
  8851. pc_checkallowskill(sd);
  8852. status_calc_pc(sd,SCO_NONE);
  8853. }
  8854. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8855. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8856. //OnUnEquip script [Skotlex]
  8857. if (sd->inventory_data[n]) {
  8858. if (sd->inventory_data[n]->unequip_script)
  8859. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8860. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8861. ; //No cards
  8862. else {
  8863. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8864. struct item_data *data;
  8865. if (!sd->inventory.u.items_inventory[n].card[i])
  8866. continue;
  8867. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8868. if( data->unequip_script )
  8869. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8870. }
  8871. }
  8872. }
  8873. }
  8874. sd->npc_item_flag = iflag;
  8875. return true;
  8876. }
  8877. /*==========================================
  8878. * Checking if player (sd) has an invalid item
  8879. * and is unequiped on map load (item_noequip)
  8880. *------------------------------------------*/
  8881. void pc_checkitem(struct map_session_data *sd) {
  8882. int i, calc_flag = 0;
  8883. struct item it;
  8884. nullpo_retv(sd);
  8885. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8886. return;
  8887. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  8888. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8889. it = sd->inventory.u.items_inventory[i];
  8890. if( it.nameid == 0 )
  8891. continue;
  8892. if( !it.equip )
  8893. continue;
  8894. if( it.equip&~pc_equippoint(sd,i) ) {
  8895. pc_unequipitem(sd, i, 2);
  8896. calc_flag = 1;
  8897. continue;
  8898. }
  8899. 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) ) {
  8900. pc_unequipitem(sd, i, 2);
  8901. calc_flag = 1;
  8902. continue;
  8903. }
  8904. }
  8905. if( calc_flag && sd->state.active ) {
  8906. pc_checkallowskill(sd);
  8907. status_calc_pc(sd,SCO_NONE);
  8908. }
  8909. }
  8910. /*==========================================
  8911. * Checks for unavailable items and removes them.
  8912. * @param sd: Player data
  8913. * @param type Forced check:
  8914. * 1 - Inventory
  8915. * 2 - Cart
  8916. * 4 - Storage
  8917. *------------------------------------------*/
  8918. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  8919. {
  8920. int i;
  8921. unsigned short nameid;
  8922. char output[256];
  8923. nullpo_retv(sd);
  8924. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  8925. for(i = 0; i < MAX_INVENTORY; i++) {
  8926. nameid = sd->inventory.u.items_inventory[i].nameid;
  8927. if (!nameid)
  8928. continue;
  8929. if (!itemdb_available(nameid)) {
  8930. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8931. clif_displaymessage(sd->fd, output);
  8932. 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);
  8933. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  8934. continue;
  8935. }
  8936. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  8937. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  8938. }
  8939. }
  8940. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  8941. for(i = 0; i < MAX_CART; i++) {
  8942. nameid = sd->cart.u.items_cart[i].nameid;
  8943. if (!nameid)
  8944. continue;
  8945. if (!itemdb_available(nameid)) {
  8946. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  8947. clif_displaymessage(sd->fd, output);
  8948. 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);
  8949. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  8950. continue;
  8951. }
  8952. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  8953. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  8954. }
  8955. }
  8956. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  8957. for(i = 0; i < sd->storage.max_amount; i++) {
  8958. nameid = sd->storage.u.items_storage[i].nameid;
  8959. if (!nameid)
  8960. continue;
  8961. if (!itemdb_available(nameid)) {
  8962. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  8963. clif_displaymessage(sd->fd, output);
  8964. 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);
  8965. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  8966. continue;
  8967. }
  8968. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  8969. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  8970. }
  8971. }
  8972. }
  8973. /*==========================================
  8974. * Update PVP rank for sd1 in cmp to sd2
  8975. *------------------------------------------*/
  8976. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8977. {
  8978. struct map_session_data *sd1,*sd2;
  8979. sd1=(struct map_session_data *)bl;
  8980. sd2=va_arg(ap,struct map_session_data *);
  8981. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  8982. {// cannot register pvp rank for hidden GMs
  8983. return 0;
  8984. }
  8985. if( sd1->pvp_point > sd2->pvp_point )
  8986. sd2->pvp_rank++;
  8987. return 0;
  8988. }
  8989. /*==========================================
  8990. * Calculate new rank beetween all present players (map_foreachinallarea)
  8991. * and display result
  8992. *------------------------------------------*/
  8993. int pc_calc_pvprank(struct map_session_data *sd)
  8994. {
  8995. int old = sd->pvp_rank;
  8996. struct map_data *m = &map[sd->bl.m];
  8997. sd->pvp_rank=1;
  8998. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8999. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  9000. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  9001. return sd->pvp_rank;
  9002. }
  9003. /*==========================================
  9004. * Calculate next sd ranking calculation from config
  9005. *------------------------------------------*/
  9006. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  9007. {
  9008. struct map_session_data *sd;
  9009. sd=map_id2sd(id);
  9010. if(sd==NULL)
  9011. return 0;
  9012. sd->pvp_timer = INVALID_TIMER;
  9013. if( pc_isinvisible(sd) )
  9014. {// do not calculate the pvp rank for a hidden GM
  9015. return 0;
  9016. }
  9017. if( pc_calc_pvprank(sd) > 0 )
  9018. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9019. return 0;
  9020. }
  9021. /*==========================================
  9022. * Checking if sd is married
  9023. * Return:
  9024. * partner_id = yes
  9025. * 0 = no
  9026. *------------------------------------------*/
  9027. int pc_ismarried(struct map_session_data *sd)
  9028. {
  9029. if(sd == NULL)
  9030. return -1;
  9031. if(sd->status.partner_id > 0)
  9032. return sd->status.partner_id;
  9033. else
  9034. return 0;
  9035. }
  9036. /*==========================================
  9037. * Marry player sd to player dstsd
  9038. * Return:
  9039. * false = fail
  9040. * true = success
  9041. *------------------------------------------*/
  9042. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9043. {
  9044. if(sd == NULL || dstsd == NULL ||
  9045. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9046. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9047. return false;
  9048. sd->status.partner_id = dstsd->status.char_id;
  9049. dstsd->status.partner_id = sd->status.char_id;
  9050. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9051. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9052. return true;
  9053. }
  9054. /*==========================================
  9055. * Divorce sd from its partner
  9056. * Return:
  9057. * false = fail
  9058. * true = success
  9059. *------------------------------------------*/
  9060. bool pc_divorce(struct map_session_data *sd)
  9061. {
  9062. struct map_session_data *p_sd;
  9063. int i;
  9064. if( sd == NULL || !pc_ismarried(sd) )
  9065. return false;
  9066. if( !sd->status.partner_id )
  9067. return false; // Char is not married
  9068. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9069. { // Lets char server do the divorce
  9070. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9071. return false; // No char server connected
  9072. return true;
  9073. }
  9074. // Both players online, lets do the divorce manually
  9075. sd->status.partner_id = 0;
  9076. p_sd->status.partner_id = 0;
  9077. for( i = 0; i < MAX_INVENTORY; i++ )
  9078. {
  9079. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9080. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9081. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9082. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9083. }
  9084. clif_divorced(sd, p_sd->status.name);
  9085. clif_divorced(p_sd, sd->status.name);
  9086. return true;
  9087. }
  9088. /**
  9089. * Get the partner map_session_data of a player
  9090. * @param sd : the husband|wife session
  9091. * @return partner session or NULL
  9092. */
  9093. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9094. if (!sd || !pc_ismarried(sd))
  9095. return NULL;
  9096. return map_charid2sd(sd->status.partner_id);
  9097. }
  9098. /**
  9099. * Get the father map_session_data of a player
  9100. * @param sd : the baby session
  9101. * @return father session or NULL
  9102. */
  9103. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9104. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9105. return NULL;
  9106. return map_charid2sd(sd->status.father);
  9107. }
  9108. /**
  9109. * Get the mother map_session_data of a player
  9110. * @param sd : the baby session
  9111. * @return mother session or NULL
  9112. */
  9113. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9114. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9115. return NULL;
  9116. return map_charid2sd(sd->status.mother);
  9117. }
  9118. /*==========================================
  9119. * Get sd children charid. (Need to be married)
  9120. *------------------------------------------*/
  9121. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9122. {
  9123. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9124. // charid2sd returns NULL if not found
  9125. return NULL;
  9126. return map_charid2sd(sd->status.child);
  9127. }
  9128. /*==========================================
  9129. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9130. *------------------------------------------*/
  9131. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9132. {
  9133. int hp = 0, sp = 0;
  9134. if( pc_isdead(sd) )
  9135. return;
  9136. if (sd->hp_loss.value) {
  9137. sd->hp_loss.tick += diff_tick;
  9138. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9139. hp += sd->hp_loss.value;
  9140. sd->hp_loss.tick -= sd->hp_loss.rate;
  9141. }
  9142. if(hp >= sd->battle_status.hp)
  9143. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9144. }
  9145. if (sd->sp_loss.value) {
  9146. sd->sp_loss.tick += diff_tick;
  9147. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9148. sp += sd->sp_loss.value;
  9149. sd->sp_loss.tick -= sd->sp_loss.rate;
  9150. }
  9151. }
  9152. if (hp > 0 || sp > 0)
  9153. status_zap(&sd->bl, hp, sp);
  9154. }
  9155. //Character regen. Flag is used to know which types of regen can take place.
  9156. //&1: HP regen
  9157. //&2: SP regen
  9158. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9159. {
  9160. int hp = 0, sp = 0;
  9161. if (sd->hp_regen.value) {
  9162. sd->hp_regen.tick += diff_tick;
  9163. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9164. hp += sd->hp_regen.value;
  9165. sd->hp_regen.tick -= sd->hp_regen.rate;
  9166. }
  9167. }
  9168. if (sd->sp_regen.value) {
  9169. sd->sp_regen.tick += diff_tick;
  9170. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9171. sp += sd->sp_regen.value;
  9172. sd->sp_regen.tick -= sd->sp_regen.rate;
  9173. }
  9174. }
  9175. if (sd->percent_hp_regen.value) {
  9176. sd->percent_hp_regen.tick += diff_tick;
  9177. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9178. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9179. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9180. }
  9181. }
  9182. if (sd->percent_sp_regen.value) {
  9183. sd->percent_sp_regen.tick += diff_tick;
  9184. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9185. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9186. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9187. }
  9188. }
  9189. if (hp > 0 || sp > 0)
  9190. status_heal(&sd->bl, hp, sp, 0);
  9191. }
  9192. /*==========================================
  9193. * Memo player sd savepoint. (map,x,y)
  9194. *------------------------------------------*/
  9195. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9196. {
  9197. nullpo_retv(sd);
  9198. sd->status.save_point.map = mapindex;
  9199. sd->status.save_point.x = x;
  9200. sd->status.save_point.y = y;
  9201. }
  9202. /*==========================================
  9203. * Save 1 player data at autosave interval
  9204. *------------------------------------------*/
  9205. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9206. {
  9207. int interval;
  9208. struct s_mapiterator* iter;
  9209. struct map_session_data* sd;
  9210. static int last_save_id = 0, save_flag = 0;
  9211. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9212. save_flag = 0;
  9213. else
  9214. save_flag = 1; //Noone was saved, so save first found char.
  9215. iter = mapit_getallusers();
  9216. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9217. {
  9218. if(sd->bl.id == last_save_id && save_flag != 1) {
  9219. save_flag = 1;
  9220. continue;
  9221. }
  9222. if(save_flag != 1) //Not our turn to save yet.
  9223. continue;
  9224. //Save char.
  9225. last_save_id = sd->bl.id;
  9226. save_flag = 2;
  9227. if (pc_isvip(sd)) // Check if we're still VIP
  9228. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9229. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9230. break;
  9231. }
  9232. mapit_free(iter);
  9233. interval = autosave_interval/(map_usercount()+1);
  9234. if(interval < minsave_interval)
  9235. interval = minsave_interval;
  9236. add_timer(gettick()+interval,pc_autosave,0,0);
  9237. return 0;
  9238. }
  9239. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9240. {
  9241. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9242. { //Night/day state does not match.
  9243. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  9244. sd->state.night = night_flag;
  9245. return 1;
  9246. }
  9247. return 0;
  9248. }
  9249. /*================================================
  9250. * timer to do the day [Yor]
  9251. * data: 0 = called by timer, 1 = gmcommand/script
  9252. *------------------------------------------------*/
  9253. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9254. {
  9255. char tmp_soutput[1024];
  9256. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9257. return 0;
  9258. if (!night_flag)
  9259. return 0; //Already day.
  9260. night_flag = 0; // 0=day, 1=night [Yor]
  9261. map_foreachpc(pc_daynight_timer_sub);
  9262. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9263. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9264. return 0;
  9265. }
  9266. /*================================================
  9267. * timer to do the night [Yor]
  9268. * data: 0 = called by timer, 1 = gmcommand/script
  9269. *------------------------------------------------*/
  9270. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9271. {
  9272. char tmp_soutput[1024];
  9273. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9274. return 0;
  9275. if (night_flag)
  9276. return 0; //Already nigth.
  9277. night_flag = 1; // 0=day, 1=night [Yor]
  9278. map_foreachpc(pc_daynight_timer_sub);
  9279. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9280. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9281. return 0;
  9282. }
  9283. /**
  9284. * Attempt to stand up a player
  9285. * @param sd
  9286. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9287. * @return True if success, Fals if failed
  9288. */
  9289. bool pc_setstand(struct map_session_data *sd, bool force){
  9290. nullpo_ret(sd);
  9291. // Cannot stand yet
  9292. // TODO: Move to SCS_NOSTAND [Cydh]
  9293. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9294. return false;
  9295. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9296. clif_status_load(&sd->bl,SI_SIT,0);
  9297. clif_standing(&sd->bl); //Inform area PC is standing
  9298. //Reset sitting tick.
  9299. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9300. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9301. return true;
  9302. }
  9303. /**
  9304. * Mechanic (MADO GEAR)
  9305. **/
  9306. void pc_overheat(struct map_session_data *sd, int val) {
  9307. int heat = val, skill,
  9308. limit[] = { 10, 20, 28, 46, 66 };
  9309. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9310. return; // already burning
  9311. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9312. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9313. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9314. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9315. }
  9316. heat = max(0,heat); // Avoid negative HEAT
  9317. if( heat >= limit[skill] )
  9318. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9319. else
  9320. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9321. return;
  9322. }
  9323. /**
  9324. * Check if player is autolooting given itemID.
  9325. */
  9326. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9327. {
  9328. uint8 i = 0;
  9329. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9330. return true;
  9331. if (!sd->state.autolooting)
  9332. return false;
  9333. if (sd->state.autolooting)
  9334. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9335. return (i != AUTOLOOTITEM_SIZE);
  9336. }
  9337. /**
  9338. * Checks if player can use @/#command
  9339. * @param sd Player map session data
  9340. * @param command Command name without @/# and params
  9341. * @param type is it atcommand or charcommand
  9342. */
  9343. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9344. {
  9345. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9346. }
  9347. /**
  9348. * Checks if commands used by a player should be logged
  9349. * according to their group setting.
  9350. * @param sd Player map session data
  9351. */
  9352. bool pc_should_log_commands(struct map_session_data *sd)
  9353. {
  9354. return pc_group_should_log_commands(pc_get_group_id(sd));
  9355. }
  9356. /**
  9357. * Spirit Charm expiration timer.
  9358. * @see TimerFunc
  9359. */
  9360. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9361. {
  9362. struct map_session_data *sd;
  9363. int i;
  9364. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9365. return 1;
  9366. if (sd->spiritcharm <= 0) {
  9367. 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);
  9368. sd->spiritcharm = 0;
  9369. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9370. return 0;
  9371. }
  9372. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9373. if (i == sd->spiritcharm) {
  9374. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9375. return 0;
  9376. }
  9377. sd->spiritcharm--;
  9378. if (i != sd->spiritcharm)
  9379. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9380. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9381. if (sd->spiritcharm <= 0)
  9382. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9383. clif_spiritcharm(sd);
  9384. return 0;
  9385. }
  9386. /**
  9387. * Adds a spirit charm.
  9388. * @param sd: Target character
  9389. * @param interval: Duration
  9390. * @param max: Maximum amount of charms to add
  9391. * @param type: Charm type (@see spirit_charm_types)
  9392. */
  9393. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9394. {
  9395. int tid, i;
  9396. nullpo_retv(sd);
  9397. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9398. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9399. if (max > MAX_SPIRITCHARM)
  9400. max = MAX_SPIRITCHARM;
  9401. if (sd->spiritcharm < 0)
  9402. sd->spiritcharm = 0;
  9403. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9404. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9405. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9406. sd->spiritcharm--;
  9407. if (sd->spiritcharm != 0)
  9408. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9409. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9410. }
  9411. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9412. 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);
  9413. if (i != sd->spiritcharm)
  9414. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9415. sd->spiritcharm_timer[i] = tid;
  9416. sd->spiritcharm++;
  9417. sd->spiritcharm_type = type;
  9418. clif_spiritcharm(sd);
  9419. }
  9420. /**
  9421. * Removes one or more spirit charms.
  9422. * @param sd: The target character
  9423. * @param count: Amount of charms to remove
  9424. * @param type: Type of charm to remove
  9425. */
  9426. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9427. {
  9428. int i;
  9429. nullpo_retv(sd);
  9430. if (sd->spiritcharm_type != type)
  9431. return;
  9432. if (sd->spiritcharm <= 0) {
  9433. sd->spiritcharm = 0;
  9434. return;
  9435. }
  9436. if (count <= 0)
  9437. return;
  9438. if (count > sd->spiritcharm)
  9439. count = sd->spiritcharm;
  9440. sd->spiritcharm -= count;
  9441. if (count > MAX_SPIRITCHARM)
  9442. count = MAX_SPIRITCHARM;
  9443. for (i = 0; i < count; i++) {
  9444. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9445. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9446. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9447. }
  9448. }
  9449. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9450. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9451. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9452. }
  9453. if (sd->spiritcharm <= 0)
  9454. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9455. clif_spiritcharm(sd);
  9456. }
  9457. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9458. /**
  9459. * Renewal EXP/Item Drop rate modifier based on level penalty
  9460. * @param level_diff: Monster and Player level difference
  9461. * @param mob_class: Monster class
  9462. * @param mode: Monster mode
  9463. * @param type: 1 - EXP, 2 - Item Drop
  9464. * @return Penalty rate
  9465. */
  9466. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9467. {
  9468. int rate = 100;
  9469. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9470. return rate;
  9471. if (level_diff < 0)
  9472. level_diff = MAX_LEVEL + (~level_diff + 1);
  9473. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9474. return rate;
  9475. return 100; // Penalty not found, return default
  9476. }
  9477. #endif
  9478. int pc_split_str(char *str,char **val,int num)
  9479. {
  9480. int i;
  9481. for (i=0; i<num && str; i++){
  9482. val[i] = str;
  9483. str = strchr(str,',');
  9484. if (str && i<num-1) //Do not remove a trailing comma.
  9485. *str++=0;
  9486. }
  9487. return i;
  9488. }
  9489. int pc_split_atoi(char* str, int* val, char sep, int max)
  9490. {
  9491. int i,j;
  9492. for (i=0; i<max; i++) {
  9493. if (!str) break;
  9494. val[i] = atoi(str);
  9495. str = strchr(str,sep);
  9496. if (str)
  9497. *str++=0;
  9498. }
  9499. //Zero up the remaining.
  9500. for(j=i; j < max; j++)
  9501. val[j] = 0;
  9502. return i;
  9503. }
  9504. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9505. {
  9506. static int warning=0;
  9507. int i,j;
  9508. for (i=0; i<max; i++) {
  9509. double f;
  9510. if (!str) break;
  9511. f = atof(str);
  9512. if (f < 0)
  9513. val[i] = 0;
  9514. else if (f > UINT_MAX) {
  9515. val[i] = UINT_MAX;
  9516. if (!warning) {
  9517. warning = 1;
  9518. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9519. }
  9520. } else
  9521. val[i] = (unsigned int)f;
  9522. str = strchr(str,sep);
  9523. if (str)
  9524. *str++=0;
  9525. }
  9526. //Zero up the remaining.
  9527. for(j=i; j < max; j++)
  9528. val[j] = 0;
  9529. return i;
  9530. }
  9531. /*==========================================
  9532. * sub DB reading.
  9533. * Function used to read skill_tree.txt
  9534. *------------------------------------------*/
  9535. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9536. {
  9537. uint32 baselv, joblv, baselv_max, joblv_max;
  9538. uint16 skill_id, skill_lv, skill_lv_max;
  9539. int idx, class_;
  9540. unsigned int i, offset, skill_idx;
  9541. class_ = atoi(fields[0]);
  9542. skill_id = (uint16)atoi(fields[1]);
  9543. skill_lv = (uint16)atoi(fields[2]);
  9544. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9545. baselv = (uint32)atoi(fields[3]);
  9546. joblv = (uint32)atoi(fields[4]);
  9547. offset = 5;
  9548. } else {
  9549. baselv = joblv = 0;
  9550. offset = 3;
  9551. }
  9552. if(!pcdb_checkid(class_))
  9553. {
  9554. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9555. return false;
  9556. }
  9557. idx = pc_class2idx(class_);
  9558. if (!skill_get_index(skill_id)) {
  9559. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9560. return false;
  9561. }
  9562. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9563. 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);
  9564. skill_lv = skill_lv_max;
  9565. }
  9566. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9567. 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);
  9568. baselv = baselv_max;
  9569. }
  9570. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9571. 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);
  9572. joblv = joblv_max;
  9573. }
  9574. //This is to avoid adding two lines for the same skill. [Skotlex]
  9575. 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 );
  9576. if( skill_idx == MAX_SKILL_TREE )
  9577. {
  9578. 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_);
  9579. return false;
  9580. }
  9581. else if(skill_tree[idx][skill_idx].skill_id)
  9582. {
  9583. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9584. }
  9585. skill_tree[idx][skill_idx].skill_id = skill_id;
  9586. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9587. skill_tree[idx][skill_idx].baselv = baselv;
  9588. skill_tree[idx][skill_idx].joblv = joblv;
  9589. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9590. {
  9591. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9592. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9593. if (skill_id == 0) {
  9594. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9595. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9596. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9597. }
  9598. continue;
  9599. }
  9600. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9601. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9602. return false;
  9603. }
  9604. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9605. 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);
  9606. skill_lv = skill_lv_max;
  9607. }
  9608. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9609. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9610. }
  9611. return true;
  9612. }
  9613. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9614. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9615. {
  9616. int type, class_, diff;
  9617. type = atoi(fields[0]); //1=experience, 2=item drop
  9618. class_ = atoi(fields[1]);
  9619. diff = atoi(fields[2]);
  9620. if( type != 1 && type != 2 ){
  9621. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9622. return false;
  9623. }
  9624. if( !CHK_CLASS(class_) ){
  9625. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9626. return false;
  9627. }
  9628. diff = min(diff, MAX_LEVEL);
  9629. if( diff < 0 )
  9630. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9631. level_penalty[type][class_][diff] = atoi(fields[3]);
  9632. return true;
  9633. }
  9634. #endif
  9635. /** [Cydh]
  9636. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9637. * @param level Base level of player
  9638. * @param class_ Job ID @see enum e_job
  9639. * @return base_hp
  9640. */
  9641. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9642. double base_hp;
  9643. uint16 i, idx = pc_class2idx(class_);
  9644. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9645. #ifndef RENEWAL
  9646. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9647. #endif
  9648. for (i = 2; i <= level; i++)
  9649. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9650. if (class_ == JOB_SUMMONER)
  9651. base_hp += floor((base_hp / 2) + 0.5);
  9652. return (unsigned int)base_hp;
  9653. }
  9654. /** [Playtester]
  9655. * Calculates base sp of player.
  9656. * @param level Base level of player
  9657. * @param class_ Job ID @see enum e_job
  9658. * @return base_sp
  9659. */
  9660. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9661. double base_sp;
  9662. uint16 idx = pc_class2idx(class_);
  9663. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9664. switch (class_) {
  9665. case JOB_NINJA:
  9666. if (level >= 10)
  9667. base_sp -= 22;
  9668. else
  9669. base_sp = 11 + 3*level;
  9670. break;
  9671. case JOB_GUNSLINGER:
  9672. if (level > 10)
  9673. base_sp -= 18;
  9674. else
  9675. base_sp = 9 + 3*level;
  9676. break;
  9677. case JOB_SUMMONER:
  9678. base_sp -= floor(base_sp / 2);
  9679. break;
  9680. }
  9681. return (unsigned int)base_sp;
  9682. }
  9683. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9684. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9685. int idx, class_;
  9686. unsigned int i;
  9687. class_ = atoi(fields[0]);
  9688. if (!pcdb_checkid(class_)) {
  9689. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9690. return false;
  9691. }
  9692. idx = pc_class2idx(class_);
  9693. job_info[idx].max_weight_base = atoi(fields[1]);
  9694. job_info[idx].hp_factor = atoi(fields[2]);
  9695. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9696. job_info[idx].sp_factor = atoi(fields[4]);
  9697. #ifdef RENEWAL_ASPD
  9698. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9699. #else
  9700. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9701. #endif
  9702. {
  9703. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9704. }
  9705. return true;
  9706. }
  9707. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9708. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9709. {
  9710. int idx, class_, i;
  9711. class_ = atoi(fields[0]);
  9712. if(!pcdb_checkid(class_))
  9713. {
  9714. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9715. return false;
  9716. }
  9717. idx = pc_class2idx(class_);
  9718. for(i = 1; i < columns; i++)
  9719. {
  9720. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9721. }
  9722. return true;
  9723. }
  9724. //Reading job_exp.txt line
  9725. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9726. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9727. {
  9728. int idx, i, type;
  9729. int job_id,job_count,jobs[CLASS_COUNT];
  9730. unsigned int ui, maxlvl;
  9731. maxlvl = atoi(fields[0]);
  9732. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9733. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9734. return false;
  9735. }
  9736. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9737. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9738. return false;
  9739. }
  9740. type = atoi(fields[2]);
  9741. if(type < 0 || type > 1){
  9742. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9743. return false;
  9744. }
  9745. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9746. if (job_count < 1)
  9747. return false;
  9748. job_id = jobs[0];
  9749. if(!pcdb_checkid(job_id)){
  9750. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9751. return false;
  9752. }
  9753. idx = pc_class2idx(job_id);
  9754. job_info[idx].max_level[type] = maxlvl;
  9755. for(i=0; i<maxlvl; i++)
  9756. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9757. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9758. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9759. //should look like this:
  9760. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9761. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9762. job_info[idx].max_level[type]--;
  9763. if (job_info[idx].max_level[type] < maxlvl) {
  9764. 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]);
  9765. ShowInfo("Filling the missing values with the last exp entry.\n");
  9766. //Fill the requested values with the last entry.
  9767. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9768. for (; ui+2 < maxlvl; ui++)
  9769. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9770. job_info[idx].max_level[type] = maxlvl;
  9771. }
  9772. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9773. for (i = 1; i < job_count; i++) {
  9774. job_id = jobs[i];
  9775. if (!pcdb_checkid(job_id)) {
  9776. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9777. continue;
  9778. }
  9779. idx = pc_class2idx(job_id);
  9780. 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]));
  9781. job_info[idx].max_level[type] = maxlvl;
  9782. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9783. }
  9784. return true;
  9785. }
  9786. /**
  9787. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9788. * startlvl,endlvl,class,type,values...
  9789. */
  9790. #ifdef HP_SP_TABLES
  9791. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9792. {
  9793. int i, startlvl, endlvl;
  9794. int job_count,jobs[CLASS_COUNT];
  9795. short type;
  9796. startlvl = atoi(fields[0]);
  9797. if(startlvl<1){
  9798. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9799. return false;
  9800. }
  9801. endlvl = atoi(fields[1]);
  9802. if(endlvl<1 || endlvl<startlvl){
  9803. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9804. return false;
  9805. }
  9806. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9807. 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);
  9808. return false;
  9809. }
  9810. type = atoi(fields[3]);
  9811. if(type < 0 || type > 1){
  9812. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9813. return false;
  9814. }
  9815. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9816. if (job_count < 1)
  9817. return false;
  9818. for (i = 0; i < job_count; i++) {
  9819. int idx, job_id = jobs[i], use_endlvl;
  9820. if (!pcdb_checkid(job_id)) {
  9821. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9822. return false;
  9823. }
  9824. idx = pc_class2idx(job_id);
  9825. if (startlvl > job_info[idx].max_level[0]) {
  9826. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9827. return false;
  9828. }
  9829. //Just read until available max level for this job, don't use MAX_LEVEL!
  9830. use_endlvl = endlvl;
  9831. if (use_endlvl > job_info[idx].max_level[0])
  9832. use_endlvl = job_info[idx].max_level[0];
  9833. if(type == 0) { //hp type
  9834. uint16 j;
  9835. for(j = 0; j < use_endlvl; j++) {
  9836. if (atoi(fields[j+4])) {
  9837. uint16 lvl_idx = startlvl-1+j;
  9838. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9839. //Tells if this HP is lower than previous level (but not for 99->100)
  9840. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9841. 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",
  9842. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9843. }
  9844. }
  9845. }
  9846. else { //sp type
  9847. uint16 j;
  9848. for(j = 0; j < use_endlvl; j++) {
  9849. if (atoi(fields[j+4])) {
  9850. uint16 lvl_idx = startlvl-1+j;
  9851. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9852. //Tells if this SP is lower than previous level (but not for 99->100)
  9853. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9854. 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",
  9855. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9856. }
  9857. }
  9858. }
  9859. }
  9860. return true;
  9861. }
  9862. #endif
  9863. /** [Cydh]
  9864. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9865. */
  9866. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9867. {
  9868. int idx, class_;
  9869. uint16 str, agi, vit, int_, dex, luk;
  9870. script_get_constant(trim(fields[0]),&class_);
  9871. if ((idx = pc_class2idx(class_)) < 0) {
  9872. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9873. return false;
  9874. }
  9875. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9876. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9877. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9878. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9879. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9880. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9881. job_info[idx].max_param.str = str;
  9882. job_info[idx].max_param.agi = agi;
  9883. job_info[idx].max_param.vit = vit;
  9884. job_info[idx].max_param.int_ = int_;
  9885. job_info[idx].max_param.dex = dex;
  9886. job_info[idx].max_param.luk = luk;
  9887. return true;
  9888. }
  9889. /**
  9890. * Read job_noenter_map.txt
  9891. **/
  9892. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  9893. int idx, class_ = -1;
  9894. if (ISDIGIT(str[0][0])) {
  9895. class_ = atoi(str[0]);
  9896. } else {
  9897. if (!script_get_constant(str[0], &class_)) {
  9898. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  9899. return false;
  9900. }
  9901. }
  9902. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  9903. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  9904. return false;
  9905. }
  9906. job_info[idx].noenter_map.zone = atoi(str[1]);
  9907. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  9908. return true;
  9909. }
  9910. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9911. int i=1;
  9912. char line[24000]; //FIXME this seem too big
  9913. FILE *fp;
  9914. sprintf(line, "%s/statpoint.txt", basedir);
  9915. fp=fopen(line,"r");
  9916. if(fp == NULL){
  9917. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9918. return max(last_s,i);
  9919. } else {
  9920. int entries=0;
  9921. while(fgets(line, sizeof(line), fp))
  9922. {
  9923. int stat;
  9924. trim(line);
  9925. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9926. continue;
  9927. if ((stat=strtoul(line,NULL,10))<0)
  9928. stat=0;
  9929. if (i > MAX_LEVEL)
  9930. break;
  9931. statp[i]=stat;
  9932. i++;
  9933. entries++;
  9934. }
  9935. fclose(fp);
  9936. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9937. }
  9938. return max(last_s,i);
  9939. }
  9940. /*==========================================
  9941. * pc DB reading.
  9942. * job_exp.txt - required experience values
  9943. * skill_tree.txt - skill tree for every class
  9944. * attr_fix.txt - elemental adjustment table
  9945. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9946. * job_db2.txt - job,stats bonuses/lvl
  9947. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9948. *------------------------------------------*/
  9949. void pc_readdb(void) {
  9950. int i, k, s = 1;
  9951. const char* dbsubpath[] = {
  9952. "",
  9953. "/"DBIMPORT,
  9954. //add other path here
  9955. };
  9956. //reset
  9957. memset(job_info,0,sizeof(job_info)); // job_info table
  9958. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9959. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9960. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9961. for( k=1; k < 3; k++ ){ // fill in the blanks
  9962. int j;
  9963. for( j = 0; j < CLASS_ALL; j++ ){
  9964. int tmp = 0;
  9965. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9966. if( i == MAX_LEVEL+1 )
  9967. tmp = level_penalty[k][j][0];// reset
  9968. if( level_penalty[k][j][i] > 0 )
  9969. tmp = level_penalty[k][j][i];
  9970. else
  9971. level_penalty[k][j][i] = tmp;
  9972. }
  9973. }
  9974. }
  9975. #endif
  9976. // reset then read statspoint
  9977. memset(statp,0,sizeof(statp));
  9978. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9979. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  9980. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  9981. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9982. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9983. if(i==0) {
  9984. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9985. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9986. }
  9987. else {
  9988. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9989. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9990. }
  9991. s = pc_read_statsdb(dbsubpath2,s,i);
  9992. if (i == 0)
  9993. #ifdef RENEWAL_ASPD
  9994. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9995. #else
  9996. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9997. #endif
  9998. else
  9999. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  10000. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  10001. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  10002. #ifdef HP_SP_TABLES
  10003. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  10004. #endif
  10005. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  10006. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i);
  10007. aFree(dbsubpath1);
  10008. aFree(dbsubpath2);
  10009. }
  10010. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10011. memset(skill_tree, 0, sizeof(skill_tree));
  10012. 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);
  10013. 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);
  10014. // generate the remaining parts of the db if necessary
  10015. k = battle_config.use_statpoint_table; //save setting
  10016. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10017. statp[0] = 45; // seed value
  10018. for (; s <= MAX_LEVEL; s++)
  10019. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10020. battle_config.use_statpoint_table = k; //restore setting
  10021. //Checking if all class have their data
  10022. for (i = 0; i < JOB_MAX; i++) {
  10023. int idx;
  10024. uint16 j;
  10025. if (!pcdb_checkid(i))
  10026. continue;
  10027. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  10028. continue; //Classes that do not need exp tables.
  10029. idx = pc_class2idx(i);
  10030. if (!job_info[idx].max_level[0])
  10031. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10032. if (!job_info[idx].max_level[1])
  10033. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10034. //Init and checking the empty value of Base HP/SP [Cydh]
  10035. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10036. if (job_info[idx].base_hp[j] == 0)
  10037. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10038. if (job_info[idx].base_sp[j] == 0)
  10039. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10040. }
  10041. }
  10042. }
  10043. // Read MOTD on startup. [Valaris]
  10044. int pc_read_motd(void)
  10045. {
  10046. FILE* fp;
  10047. // clear old MOTD
  10048. memset(motd_text, 0, sizeof(motd_text));
  10049. // read current MOTD
  10050. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10051. {
  10052. unsigned int entries = 0;
  10053. char buf[CHAT_SIZE_MAX];
  10054. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10055. {
  10056. unsigned int lines = 0;
  10057. size_t len;
  10058. lines++;
  10059. if( buf[0] == '/' && buf[1] == '/' )
  10060. continue;
  10061. len = strlen(buf);
  10062. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10063. len--;
  10064. if( len ) {
  10065. char * ptr;
  10066. buf[len] = 0;
  10067. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10068. 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);
  10069. }
  10070. else {// empty line
  10071. buf[0] = ' ';
  10072. buf[1] = 0;
  10073. }
  10074. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10075. entries++;
  10076. }
  10077. fclose(fp);
  10078. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  10079. }
  10080. else
  10081. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  10082. return 0;
  10083. }
  10084. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10085. int i,cursor = 0;
  10086. struct item_cd* cd = NULL;
  10087. if( load ) {
  10088. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10089. // no item cooldown is associated with this character
  10090. return;
  10091. }
  10092. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10093. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10094. sd->item_delay[cursor].tick = cd->tick[i];
  10095. sd->item_delay[cursor].nameid = cd->nameid[i];
  10096. cursor++;
  10097. }
  10098. }
  10099. idb_remove(itemcd_db,sd->status.char_id);
  10100. } else {
  10101. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10102. // create a new skill cooldown object for map storage
  10103. CREATE( cd, struct item_cd, 1 );
  10104. idb_put( itemcd_db, sd->status.char_id, cd );
  10105. }
  10106. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10107. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10108. cd->tick[cursor] = sd->item_delay[i].tick;
  10109. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10110. cursor++;
  10111. }
  10112. }
  10113. }
  10114. return;
  10115. }
  10116. /**
  10117. * Add item delay to player's item delay data
  10118. * @param sd Player
  10119. * @param id Item data
  10120. * @param tick Current tick
  10121. * @param n Item index in inventory
  10122. * @return 0: No delay, can consume item.
  10123. * 1: Has delay, cancel consumption.
  10124. **/
  10125. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10126. int i;
  10127. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10128. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10129. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10130. if( i < MAX_ITEMDELAYS ) {
  10131. if( sd->item_delay[i].nameid ) {// found
  10132. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10133. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10134. char e_msg[CHAT_SIZE_MAX];
  10135. if( e_tick > 99 )
  10136. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10137. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10138. else
  10139. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10140. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10141. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10142. return 1; // Delay has not expired yet
  10143. }
  10144. } else {// not yet used item (all slots are initially empty)
  10145. sd->item_delay[i].nameid = id->nameid;
  10146. }
  10147. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10148. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10149. } else {// should not happen
  10150. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10151. }
  10152. //clean up used delays so we can give room for more
  10153. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10154. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10155. sd->item_delay[i].tick = 0;
  10156. sd->item_delay[i].nameid = 0;
  10157. }
  10158. }
  10159. return 0;
  10160. }
  10161. /**
  10162. * Check if player has delay to reuse item
  10163. * @param sd Player
  10164. * @param id Item data
  10165. * @param tick Current tick
  10166. * @param n Item index in inventory
  10167. * @return 0: No delay, can consume item.
  10168. * 1: Has delay, cancel consumption.
  10169. **/
  10170. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10171. struct status_change *sc = NULL;
  10172. nullpo_retr(0, sd);
  10173. nullpo_retr(0, id);
  10174. // Do normal delay assignment
  10175. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10176. return pc_itemcd_add(sd, id, tick, n);
  10177. // Send reply of delay remains
  10178. if (sc->data[id->delay_sc]) {
  10179. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10180. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10181. return 1;
  10182. }
  10183. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10184. return 0;
  10185. }
  10186. /**
  10187. * Clear the dmglog data from player
  10188. * @param sd
  10189. * @param md
  10190. **/
  10191. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10192. {
  10193. uint8 i;
  10194. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10195. if (i < DAMAGELOG_SIZE) {
  10196. md->dmglog[i].id = 0;
  10197. md->dmglog[i].dmg = 0;
  10198. md->dmglog[i].flag = 0;
  10199. }
  10200. }
  10201. /**
  10202. * Add log to player's dmglog
  10203. * @param sd
  10204. * @param id Monster's GID
  10205. **/
  10206. void pc_damage_log_add(struct map_session_data *sd, int id)
  10207. {
  10208. uint8 i = 0;
  10209. if (!sd || !id)
  10210. return;
  10211. //Only store new data, don't need to renew the old one with same id
  10212. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10213. if (i < DAMAGELOG_SIZE_PC)
  10214. return;
  10215. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10216. if (sd->dmglog[i] == 0) {
  10217. sd->dmglog[i] = id;
  10218. return;
  10219. }
  10220. }
  10221. }
  10222. /**
  10223. * Clear dmglog data from player
  10224. * @param sd
  10225. * @param id Monster's id
  10226. **/
  10227. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10228. {
  10229. uint8 i;
  10230. struct mob_data *md = NULL;
  10231. if (!sd)
  10232. return;
  10233. if (!id) {
  10234. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10235. if( !sd->dmglog[i] ) //skip the empty value
  10236. continue;
  10237. if ((md = map_id2md(sd->dmglog[i])))
  10238. pc_clear_log_damage_sub(sd->status.char_id,md);
  10239. sd->dmglog[i] = 0;
  10240. }
  10241. }
  10242. else {
  10243. if ((md = map_id2md(id)))
  10244. pc_clear_log_damage_sub(sd->status.char_id,md);
  10245. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10246. if (i < DAMAGELOG_SIZE_PC)
  10247. sd->dmglog[i] = 0;
  10248. }
  10249. }
  10250. /**
  10251. * Status change data arrived from char-server
  10252. * @param sd: Player data
  10253. */
  10254. void pc_scdata_received(struct map_session_data *sd) {
  10255. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10256. sd->state.pc_loaded = true;
  10257. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10258. sd->state.connect_new = 1;
  10259. clif_parse_LoadEndAck(sd->fd, sd);
  10260. }
  10261. if (pc_iscarton(sd)) {
  10262. sd->cart_weight_max = 0; // Force a client refesh
  10263. status_calc_cart_weight(sd, CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE);
  10264. }
  10265. }
  10266. /**
  10267. * Check player account expiration time and rental item expirations
  10268. * @param sd: Player data
  10269. */
  10270. void pc_check_expiration(struct map_session_data *sd) {
  10271. #ifndef ENABLE_SC_SAVING
  10272. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10273. #endif
  10274. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10275. time_t exp_time = sd->expiration_time;
  10276. char tmpstr[1024];
  10277. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10278. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  10279. pc_expire_check(sd);
  10280. }
  10281. }
  10282. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10283. struct map_session_data *sd = map_id2sd(id);
  10284. if( !sd ) return 0;
  10285. sd->expiration_tid = INVALID_TIMER;
  10286. if( sd->fd )
  10287. clif_authfail_fd(sd->fd,10);
  10288. map_quit(sd);
  10289. return 0;
  10290. }
  10291. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10292. struct map_session_data *sd = map_id2sd(id);
  10293. if (!sd)
  10294. return 0;
  10295. sd->autotrade_tid = INVALID_TIMER;
  10296. if (sd->state.autotrade&2)
  10297. vending_reopen(sd);
  10298. if (sd->state.autotrade&4)
  10299. buyingstore_reopen(sd);
  10300. if (!sd->vender_id && !sd->buyer_id) {
  10301. sd->state.autotrade = 0;
  10302. map_quit(sd);
  10303. }
  10304. return 0;
  10305. }
  10306. /* this timer exists only when a character with a expire timer > 24h is online */
  10307. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10308. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10309. struct s_mapiterator* iter;
  10310. struct map_session_data* sd;
  10311. iter = mapit_getallusers();
  10312. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10313. if( sd->expiration_time )
  10314. pc_expire_check(sd);
  10315. mapit_free(iter);
  10316. return 0;
  10317. }
  10318. void pc_expire_check(struct map_session_data *sd) {
  10319. /* ongoing timer */
  10320. if( sd->expiration_tid != INVALID_TIMER )
  10321. return;
  10322. /* not within the next 24h, enable the global check */
  10323. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10324. /* global check not running, enable */
  10325. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10326. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10327. return;
  10328. }
  10329. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10330. }
  10331. /**
  10332. * Deposit some money to bank
  10333. * @param sd
  10334. * @param money Amount of money to deposit
  10335. **/
  10336. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10337. unsigned int limit_check = money + sd->bank_vault;
  10338. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10339. return BDA_OVERFLOW;
  10340. } else if ( money > sd->status.zeny ) {
  10341. return BDA_NO_MONEY;
  10342. }
  10343. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10344. return BDA_NO_MONEY;
  10345. sd->bank_vault += money;
  10346. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10347. if( save_settings&CHARSAVE_BANK )
  10348. chrif_save(sd, CSAVE_NORMAL);
  10349. return BDA_SUCCESS;
  10350. }
  10351. /**
  10352. * Withdraw money from bank
  10353. * @param sd
  10354. * @param money Amount of money that will be withdrawn
  10355. **/
  10356. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10357. unsigned int limit_check = money + sd->status.zeny;
  10358. if( money <= 0 ) {
  10359. return BWA_UNKNOWN_ERROR;
  10360. } else if ( money > sd->bank_vault ) {
  10361. return BWA_NO_MONEY;
  10362. } else if ( limit_check > MAX_ZENY ) {
  10363. /* no official response for this scenario exists. */
  10364. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10365. return BWA_UNKNOWN_ERROR;
  10366. }
  10367. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10368. return BWA_NO_MONEY;
  10369. sd->bank_vault -= money;
  10370. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10371. if( save_settings&CHARSAVE_BANK )
  10372. chrif_save(sd, CSAVE_NORMAL);
  10373. return BWA_SUCCESS;
  10374. }
  10375. /**
  10376. * Clear Crimson Marker data from caster
  10377. * @param sd: Player
  10378. **/
  10379. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10380. uint8 i;
  10381. if (!sd)
  10382. return;
  10383. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10384. struct block_list *bl = NULL;
  10385. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10386. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10387. sd->c_marker[i] = 0;
  10388. }
  10389. }
  10390. /**
  10391. * Show version to player
  10392. * @param sd: Player
  10393. **/
  10394. void pc_show_version(struct map_session_data *sd) {
  10395. const char* svn = get_svn_revision();
  10396. char buf[CHAT_SIZE_MAX];
  10397. if( svn[0] != UNKNOWN_VERSION )
  10398. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10399. else {
  10400. const char* git = get_git_hash();
  10401. if( git[0] != UNKNOWN_VERSION )
  10402. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10403. else
  10404. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10405. }
  10406. clif_displaymessage(sd->fd,buf);
  10407. }
  10408. /**
  10409. * Run bonus_script on player
  10410. * @param sd
  10411. * @author [Cydh]
  10412. **/
  10413. void pc_bonus_script(struct map_session_data *sd) {
  10414. int now = gettick();
  10415. struct linkdb_node *node = NULL, *next = NULL;
  10416. if (!sd || !(node = sd->bonus_script.head))
  10417. return;
  10418. while (node) {
  10419. struct s_bonus_script_entry *entry = NULL;
  10420. next = node->next;
  10421. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10422. // Only start timer for new bonus_script
  10423. if (entry->tid == INVALID_TIMER) {
  10424. if (entry->icon != SI_BLANK) // Gives status icon if exist
  10425. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10426. entry->tick += now;
  10427. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10428. }
  10429. if (entry->script)
  10430. run_script(entry->script, 0, sd->bl.id, 0);
  10431. else
  10432. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10433. }
  10434. node = next;
  10435. }
  10436. }
  10437. /**
  10438. * Add bonus_script to player
  10439. * @param sd Player
  10440. * @param script_str Script string
  10441. * @param dur Duration in ms
  10442. * @param icon SI
  10443. * @param flag Flags @see enum e_bonus_script_flags
  10444. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10445. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10446. * @author [Cydh]
  10447. **/
  10448. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum si_type icon, uint16 flag, uint8 type) {
  10449. struct script_code *script = NULL;
  10450. struct linkdb_node *node = NULL;
  10451. struct s_bonus_script_entry *entry = NULL;
  10452. if (!sd)
  10453. return NULL;
  10454. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10455. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10456. return NULL;
  10457. }
  10458. // Duplication checks
  10459. if ((node = sd->bonus_script.head)) {
  10460. while (node) {
  10461. entry = (struct s_bonus_script_entry *)node->data;
  10462. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10463. int newdur = gettick() + dur;
  10464. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10465. settick_timer(entry->tid, newdur);
  10466. script_free_code(script);
  10467. return NULL;
  10468. }
  10469. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10470. break;
  10471. else { // No duplicate bonus
  10472. script_free_code(script);
  10473. return NULL;
  10474. }
  10475. }
  10476. node = node->next;
  10477. }
  10478. }
  10479. CREATE(entry, struct s_bonus_script_entry, 1);
  10480. entry->script_buf = StringBuf_Malloc();
  10481. StringBuf_AppendStr(entry->script_buf, script_str);
  10482. entry->tid = INVALID_TIMER;
  10483. entry->flag = flag;
  10484. entry->icon = icon;
  10485. entry->tick = dur; // Use duration first, on run change to expire time
  10486. entry->type = type;
  10487. entry->script = script;
  10488. sd->bonus_script.count++;
  10489. return entry;
  10490. }
  10491. /**
  10492. * Remove bonus_script data from player
  10493. * @param sd: Target player
  10494. * @param list: Bonus script entry from player
  10495. * @author [Cydh]
  10496. **/
  10497. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10498. if (entry->tid != INVALID_TIMER)
  10499. delete_timer(entry->tid, pc_bonus_script_timer);
  10500. if (entry->script)
  10501. script_free_code(entry->script);
  10502. if (entry->script_buf)
  10503. StringBuf_Free(entry->script_buf);
  10504. if (sd) {
  10505. if (entry->icon != SI_BLANK)
  10506. clif_status_load(&sd->bl, entry->icon, 0);
  10507. if (sd->bonus_script.count > 0)
  10508. sd->bonus_script.count--;
  10509. }
  10510. aFree(entry);
  10511. }
  10512. /**
  10513. * Do final process if no entry left
  10514. * @param sd
  10515. **/
  10516. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10517. if (sd->bonus_script.count == 0) {
  10518. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10519. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10520. linkdb_final(&sd->bonus_script.head);
  10521. }
  10522. }
  10523. /**
  10524. * Timer for bonus_script
  10525. * @param tid
  10526. * @param tick
  10527. * @param id
  10528. * @param data
  10529. * @author [Cydh]
  10530. **/
  10531. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10532. struct map_session_data *sd;
  10533. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10534. sd = map_id2sd(id);
  10535. if (!sd) {
  10536. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10537. return 0;
  10538. }
  10539. if (tid == INVALID_TIMER)
  10540. return 0;
  10541. if (!sd->bonus_script.head || entry == NULL) {
  10542. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10543. return 0;
  10544. }
  10545. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10546. pc_bonus_script_free_entry(sd, entry);
  10547. pc_bonus_script_check_final(sd);
  10548. status_calc_pc(sd,SCO_NONE);
  10549. return 0;
  10550. }
  10551. /**
  10552. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10553. * @param sd: Target player
  10554. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10555. * @author [Cydh]
  10556. **/
  10557. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10558. struct linkdb_node *node = NULL;
  10559. uint16 count = 0;
  10560. if (!sd || !(node = sd->bonus_script.head))
  10561. return;
  10562. while (node) {
  10563. struct linkdb_node *next = node->next;
  10564. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10565. if (entry && (
  10566. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10567. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10568. (flag&entry->flag) || // Matched flag
  10569. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10570. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10571. )
  10572. )
  10573. {
  10574. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10575. pc_bonus_script_free_entry(sd, entry);
  10576. count++;
  10577. }
  10578. node = next;
  10579. }
  10580. pc_bonus_script_check_final(sd);
  10581. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10582. status_calc_pc(sd,SCO_NONE);
  10583. }
  10584. /** [Cydh]
  10585. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10586. * @param sd: Target player
  10587. */
  10588. void pc_cell_basilica(struct map_session_data *sd) {
  10589. nullpo_retv(sd);
  10590. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10591. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10592. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10593. }
  10594. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10595. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10596. }
  10597. /** [Cydh]
  10598. * Get maximum specified parameter for specified class
  10599. * @param class_: sd->class
  10600. * @param sex: sd->status.sex
  10601. * @param flag: parameter will be checked
  10602. * @return max_param
  10603. */
  10604. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10605. int idx = -1, class_ = sd->class_;
  10606. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10607. short max_param = 0;
  10608. switch (param) {
  10609. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10610. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10611. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10612. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10613. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10614. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10615. }
  10616. if (max_param > 0)
  10617. return max_param;
  10618. }
  10619. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10620. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10621. ((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)) :
  10622. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10623. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10624. }
  10625. /**
  10626. * Get max ASPD for player based on Class
  10627. * @param sd Player
  10628. * @return ASPD
  10629. */
  10630. short pc_maxaspd(struct map_session_data *sd) {
  10631. nullpo_ret(sd);
  10632. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10633. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10634. battle_config.max_aspd ));
  10635. }
  10636. /**
  10637. * Calculates total item-group related bonuses for the given item
  10638. * @param sd Player
  10639. * @param nameid Item ID
  10640. * @return Heal rate
  10641. **/
  10642. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10643. short bonus = 0;
  10644. uint8 i;
  10645. if (!sd->itemgrouphealrate_count)
  10646. return bonus;
  10647. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10648. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10649. struct s_item_group_db *group = NULL;
  10650. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10651. continue;
  10652. for (j = 0; j < group->random[0].data_qty; j++) {
  10653. if (group->random[0].data[j].nameid == nameid) {
  10654. bonus += sd->itemgrouphealrate[i]->rate;
  10655. break;
  10656. }
  10657. }
  10658. }
  10659. return bonus;
  10660. }
  10661. /**
  10662. * Calculates total item-group related bonuses for the given item group
  10663. * @param sd Player
  10664. * @param group_id Item Group ID
  10665. * @return Heal rate
  10666. **/
  10667. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10668. short bonus = 0;
  10669. uint8 i;
  10670. if (!sd->itemgrouphealrate_count)
  10671. return bonus;
  10672. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10673. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10674. return sd->itemgrouphealrate[i]->rate;
  10675. }
  10676. return bonus;
  10677. }
  10678. /**
  10679. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10680. * @param eqi Item EQI of enum equip_index
  10681. * @param *equip_index Player's equip_index[]
  10682. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10683. * @return True if item in same inventory index, False if doesn't
  10684. */
  10685. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10686. if (index < 0 || index >= MAX_INVENTORY)
  10687. return true;
  10688. // Dual weapon checks
  10689. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10690. return true;
  10691. // Headgear with Mid & Low location
  10692. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10693. return true;
  10694. // Headgear with Top & Mid or Low location
  10695. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10696. return true;
  10697. // Headgear with Mid & Low location
  10698. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10699. return true;
  10700. // Headgear with Top & Mid or Low location
  10701. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10702. return true;
  10703. return false;
  10704. }
  10705. /**
  10706. * Generate Unique item ID for player
  10707. * @param sd : Player
  10708. * @return A generated Unique item ID
  10709. */
  10710. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10711. nullpo_ret(sd);
  10712. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10713. }
  10714. /**
  10715. * Validating skill from player after logged on
  10716. * @param sd
  10717. **/
  10718. void pc_validate_skill(struct map_session_data *sd) {
  10719. if (sd) {
  10720. uint16 i = 0, count = 0;
  10721. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10722. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10723. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10724. for (i = 0; i < MAX_SKILL; i++) {
  10725. uint16 idx = 0;
  10726. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10727. continue;
  10728. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10729. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10730. count++;
  10731. }
  10732. else
  10733. 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);
  10734. }
  10735. }
  10736. }
  10737. /**
  10738. * Toggle to remember if the questinfo is displayed yet or not.
  10739. * @param qi_display Display flag
  10740. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10741. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10742. **/
  10743. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10744. if (show) {
  10745. // Check if need to be displayed
  10746. if ((*qi_display) != 1) {
  10747. (*qi_display) = 1;
  10748. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10749. }
  10750. }
  10751. else {
  10752. // Check if need to be hide
  10753. if ((*qi_display) != 0) {
  10754. (*qi_display) = 0;
  10755. #if PACKETVER >= 20120410
  10756. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10757. #else
  10758. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10759. #endif
  10760. }
  10761. }
  10762. }
  10763. /**
  10764. * Show available NPC Quest / Event Icon Check [Kisuka]
  10765. * @param sd Player
  10766. **/
  10767. void pc_show_questinfo(struct map_session_data *sd) {
  10768. #if PACKETVER >= 20090218
  10769. struct questinfo *qi = NULL;
  10770. unsigned short i;
  10771. uint8 j;
  10772. int8 mystate = 0;
  10773. bool failed = false;
  10774. nullpo_retv(sd);
  10775. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10776. return;
  10777. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10778. return;
  10779. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10780. qi = &map[sd->bl.m].qi_data[i];
  10781. if (!qi)
  10782. continue;
  10783. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10784. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10785. continue;
  10786. }
  10787. // Level range checks
  10788. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10789. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10790. continue;
  10791. }
  10792. // Quest requirements
  10793. if (qi->req_count) {
  10794. failed = false;
  10795. for (j = 0; j < qi->req_count; j++) {
  10796. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10797. mystate = mystate + (mystate < 1);
  10798. if (mystate != qi->req[j].state) {
  10799. failed = true;
  10800. break;
  10801. }
  10802. }
  10803. if (failed) {
  10804. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10805. continue;
  10806. }
  10807. }
  10808. // Job requirements
  10809. if (qi->jobid_count) {
  10810. failed = true;
  10811. for (j = 0; j < qi->jobid_count; j++) {
  10812. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10813. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10814. failed = false;
  10815. break;
  10816. }
  10817. }
  10818. if (!failed)
  10819. continue;
  10820. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10821. }
  10822. else {
  10823. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10824. }
  10825. }
  10826. #endif
  10827. }
  10828. /**
  10829. * Reinit the questinfo for player when changing map
  10830. * @param sd Player
  10831. **/
  10832. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10833. #if PACKETVER >= 20090218
  10834. nullpo_retv(sd);
  10835. if (sd->qi_display) {
  10836. aFree(sd->qi_display);
  10837. sd->qi_display = NULL;
  10838. }
  10839. sd->qi_count = 0;
  10840. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10841. return;
  10842. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10843. return;
  10844. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10845. #endif
  10846. }
  10847. /**
  10848. * Check if a job is allowed to enter the map
  10849. * @param jobid Job ID see enum e_job or sd->status.class_
  10850. * @param m ID -an index- for direct indexing map[] array
  10851. * @return 1 if job is allowed, 0 otherwise
  10852. **/
  10853. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10854. uint16 idx = 0;
  10855. // Map is other map server.
  10856. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10857. if (m < 0)
  10858. return true;
  10859. if (m >= MAX_MAP_PER_SERVER || !map[m].cell)
  10860. return false;
  10861. if (!pcdb_checkid(jobid))
  10862. return false;
  10863. idx = pc_class2idx(jobid);
  10864. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10865. return true;
  10866. if ((!map_flag_vs2(m) && job_info[idx].noenter_map.zone&1) || // Normal
  10867. (map[m].flag.pvp && job_info[idx].noenter_map.zone&2) || // PVP
  10868. (map_flag_gvg2_no_te(m) && job_info[idx].noenter_map.zone&4) || // GVG
  10869. (map[m].flag.battleground && job_info[idx].noenter_map.zone&8) || // Battleground
  10870. (map_flag_gvg2_te(m) && job_info[idx].noenter_map.zone&16) || // WOE:TE
  10871. (map[m].flag.restricted && job_info[idx].noenter_map.zone&(8*map[m].zone)) // Zone restriction
  10872. )
  10873. return false;
  10874. return true;
  10875. }
  10876. /**
  10877. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  10878. * @param sd
  10879. **/
  10880. void pc_set_costume_view(struct map_session_data *sd) {
  10881. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  10882. struct item_data *id = NULL;
  10883. nullpo_retv(sd);
  10884. head_low = sd->status.head_bottom;
  10885. head_mid = sd->status.head_mid;
  10886. head_top = sd->status.head_top;
  10887. robe = sd->status.robe;
  10888. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  10889. //Added check to prevent sending the same look on multiple slots ->
  10890. //causes client to redraw item on top of itself. (suggested by Lupus)
  10891. // Normal headgear checks
  10892. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10893. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  10894. sd->status.head_bottom = id->look;
  10895. else
  10896. sd->status.head_bottom = 0;
  10897. }
  10898. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10899. if (!(id->equip&(EQP_HEAD_TOP)))
  10900. sd->status.head_mid = id->look;
  10901. else
  10902. sd->status.head_mid = 0;
  10903. }
  10904. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10905. sd->status.head_top = id->look;
  10906. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10907. sd->status.robe = id->look;
  10908. // Costumes check
  10909. if (!map[sd->bl.m].flag.nocostume) {
  10910. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10911. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  10912. sd->status.head_bottom = id->look;
  10913. else
  10914. sd->status.head_bottom = 0;
  10915. }
  10916. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10917. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  10918. sd->status.head_mid = id->look;
  10919. else
  10920. sd->status.head_mid = 0;
  10921. }
  10922. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10923. sd->status.head_top = id->look;
  10924. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10925. sd->status.robe = id->look;
  10926. }
  10927. if (sd->setlook_head_bottom)
  10928. sd->status.head_bottom = sd->setlook_head_bottom;
  10929. if (sd->setlook_head_mid)
  10930. sd->status.head_mid = sd->setlook_head_mid;
  10931. if (sd->setlook_head_top)
  10932. sd->status.head_top = sd->setlook_head_top;
  10933. if (sd->setlook_robe)
  10934. sd->status.robe = sd->setlook_robe;
  10935. if (head_low != sd->status.head_bottom)
  10936. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  10937. if (head_mid != sd->status.head_mid)
  10938. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  10939. if (head_top != sd->status.head_top)
  10940. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  10941. if (robe != sd->status.robe)
  10942. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  10943. }
  10944. /*==========================================
  10945. * pc Init/Terminate
  10946. *------------------------------------------*/
  10947. void do_final_pc(void) {
  10948. db_destroy(itemcd_db);
  10949. do_final_pc_groups();
  10950. ers_destroy(pc_sc_display_ers);
  10951. ers_destroy(pc_itemgrouphealrate_ers);
  10952. ers_destroy(num_reg_ers);
  10953. ers_destroy(str_reg_ers);
  10954. }
  10955. void do_init_pc(void) {
  10956. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10957. pc_readdb();
  10958. pc_read_motd(); // Read MOTD [Valaris]
  10959. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10960. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10961. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10962. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10963. add_timer_func_list(pc_autosave, "pc_autosave");
  10964. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10965. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10966. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10967. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10968. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10969. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10970. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10971. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10972. // 0=day, 1=night [Yor]
  10973. night_flag = battle_config.night_at_start ? 1 : 0;
  10974. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10975. int day_duration = battle_config.day_duration;
  10976. int night_duration = battle_config.night_duration;
  10977. // add night/day timer [Yor]
  10978. add_timer_func_list(map_day_timer, "map_day_timer");
  10979. add_timer_func_list(map_night_timer, "map_night_timer");
  10980. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10981. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10982. }
  10983. do_init_pc_groups();
  10984. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  10985. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10986. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10987. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10988. ers_chunk_size(pc_sc_display_ers, 150);
  10989. ers_chunk_size(num_reg_ers, 300);
  10990. ers_chunk_size(str_reg_ers, 50);
  10991. }