pc.c 312 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h" // get_svn_revision()
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/random.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/socket.h" // session[]
  10. #include "../common/strlib.h" // safestrncpy()
  11. #include "../common/timer.h"
  12. #include "../common/utils.h"
  13. #include "../common/mmo.h" //NAME_LENGTH
  14. #include "atcommand.h" // get_atcommand_level()
  15. #include "battle.h" // battle_config
  16. #include "battleground.h"
  17. #include "channel.h"
  18. #include "chat.h"
  19. #include "chrif.h"
  20. #include "clif.h"
  21. #include "date.h" // is_day_of_*()
  22. #include "duel.h"
  23. #include "intif.h"
  24. #include "itemdb.h"
  25. #include "log.h"
  26. #include "mail.h"
  27. #include "map.h"
  28. #include "path.h"
  29. #include "homunculus.h"
  30. #include "instance.h"
  31. #include "mercenary.h"
  32. #include "elemental.h"
  33. #include "npc.h" // fake_nd
  34. #include "pet.h" // pet_unlocktarget()
  35. #include "party.h" // party_search()
  36. #include "guild.h" // guild_search(), guild_request_info()
  37. #include "script.h" // script_config
  38. #include "skill.h"
  39. #include "status.h" // struct status_data
  40. #include "storage.h"
  41. #include "pc.h"
  42. #include "pc_groups.h"
  43. #include "quest.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <time.h>
  48. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  49. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  50. static unsigned int statp[MAX_LEVEL+1];
  51. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  52. static unsigned int level_penalty[3][RC_MAX][MAX_LEVEL*2+1];
  53. #endif
  54. // h-files are for declarations, not for implementations... [Shinomori]
  55. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  56. // timer for night.day implementation
  57. int day_timer_tid;
  58. int night_timer_tid;
  59. struct fame_list smith_fame_list[MAX_FAME_LIST];
  60. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  61. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  62. static unsigned int equip_pos[EQI_MAX]={EQP_ACC_L,EQP_ACC_R,EQP_SHOES,EQP_GARMENT,EQP_HEAD_LOW,EQP_HEAD_MID,EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_GARMENT,EQP_AMMO,EQP_SHADOW_ARMOR,EQP_SHADOW_WEAPON,EQP_SHADOW_SHIELD,EQP_SHADOW_SHOES,EQP_SHADOW_ACC_R,EQP_SHADOW_ACC_L};
  63. #define MOTD_LINE_SIZE 128
  64. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  65. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  66. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  67. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  68. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  69. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  70. };
  71. /**
  72. * Item Cool Down Delay Saving
  73. * Struct item_cd is not a member of struct map_session_data
  74. * to keep cooldowns in memory between player log-ins.
  75. * All cooldowns are reset when server is restarted.
  76. **/
  77. DBMap* itemcd_db = NULL; // char_id -> struct skill_cd
  78. struct item_cd {
  79. unsigned int tick[MAX_ITEMDELAYS];//tick
  80. short nameid[MAX_ITEMDELAYS];//skill id
  81. };
  82. //Converts a class to its array index for CLASS_COUNT defined arrays.
  83. //Note that it does not do a validity check for speed purposes, where parsing
  84. //player input make sure to use a pcdb_checkid first!
  85. int pc_class2idx(int class_) {
  86. if (class_ >= JOB_NOVICE_HIGH)
  87. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  88. return class_;
  89. }
  90. inline int pc_get_group_id(struct map_session_data *sd) {
  91. return sd->group_id;
  92. }
  93. inline int pc_get_group_level(struct map_session_data *sd) {
  94. return sd->group_level;
  95. }
  96. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  97. {
  98. struct map_session_data *sd;
  99. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  100. return 1;
  101. if(sd->invincible_timer != tid){
  102. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  103. return 0;
  104. }
  105. sd->invincible_timer = INVALID_TIMER;
  106. skill_unit_move(&sd->bl,tick,1);
  107. return 0;
  108. }
  109. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  110. nullpo_retv(sd);
  111. if( sd->invincible_timer != INVALID_TIMER )
  112. delete_timer(sd->invincible_timer,pc_invincible_timer);
  113. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  114. }
  115. void pc_delinvincibletimer(struct map_session_data* sd)
  116. {
  117. nullpo_retv(sd);
  118. if( sd->invincible_timer != INVALID_TIMER )
  119. {
  120. delete_timer(sd->invincible_timer,pc_invincible_timer);
  121. sd->invincible_timer = INVALID_TIMER;
  122. skill_unit_move(&sd->bl,gettick(),1);
  123. }
  124. }
  125. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  126. {
  127. struct map_session_data *sd;
  128. int i;
  129. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  130. return 1;
  131. if( sd->spiritball <= 0 )
  132. {
  133. 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);
  134. sd->spiritball = 0;
  135. return 0;
  136. }
  137. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  138. if( i == sd->spiritball )
  139. {
  140. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  141. return 0;
  142. }
  143. sd->spiritball--;
  144. if( i != sd->spiritball )
  145. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  146. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  147. clif_spiritball(&sd->bl);
  148. return 0;
  149. }
  150. int pc_addspiritball(struct map_session_data *sd,int interval,int max)
  151. {
  152. int tid, i;
  153. nullpo_ret(sd);
  154. if(max > MAX_SKILL_LEVEL)
  155. max = MAX_SKILL_LEVEL;
  156. if(sd->spiritball < 0)
  157. sd->spiritball = 0;
  158. if( sd->spiritball && sd->spiritball >= max )
  159. {
  160. if(sd->spirit_timer[0] != INVALID_TIMER)
  161. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  162. sd->spiritball--;
  163. if( sd->spiritball != 0 )
  164. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  165. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  166. }
  167. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  168. 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);
  169. if( i != sd->spiritball )
  170. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  171. sd->spirit_timer[i] = tid;
  172. sd->spiritball++;
  173. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  174. clif_millenniumshield(sd,sd->spiritball);
  175. else
  176. clif_spiritball(&sd->bl);
  177. return 0;
  178. }
  179. int pc_delspiritball(struct map_session_data *sd,int count,int type)
  180. {
  181. int i;
  182. nullpo_ret(sd);
  183. if(sd->spiritball <= 0) {
  184. sd->spiritball = 0;
  185. return 0;
  186. }
  187. if(count <= 0)
  188. return 0;
  189. if(count > sd->spiritball)
  190. count = sd->spiritball;
  191. sd->spiritball -= count;
  192. if(count > MAX_SKILL_LEVEL)
  193. count = MAX_SKILL_LEVEL;
  194. for(i=0;i<count;i++) {
  195. if(sd->spirit_timer[i] != INVALID_TIMER) {
  196. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  197. sd->spirit_timer[i] = INVALID_TIMER;
  198. }
  199. }
  200. for(i=count;i<MAX_SKILL_LEVEL;i++) {
  201. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  202. sd->spirit_timer[i] = INVALID_TIMER;
  203. }
  204. if(!type) {
  205. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  206. clif_millenniumshield(sd,sd->spiritball);
  207. else
  208. clif_spiritball(&sd->bl);
  209. }
  210. return 0;
  211. }
  212. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  213. int *c, *b_sd;
  214. struct block_list *src;
  215. struct map_session_data *tsd;
  216. struct status_change *sc;
  217. nullpo_ret(bl);
  218. nullpo_ret(tsd = (struct map_session_data*)bl);
  219. nullpo_ret(src = va_arg(ap,struct block_list *));
  220. c = va_arg(ap,int *);
  221. b_sd = va_arg(ap, int *);
  222. if(pc_isdead(tsd))
  223. return 0;
  224. sc = status_get_sc(bl);
  225. if( bl == src )
  226. return 0;
  227. if( sc && sc->data[SC_BANDING] )
  228. {
  229. b_sd[(*c)++] = tsd->bl.id;
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  235. int c;
  236. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  237. int i, j, hp, extra_hp = 0, tmp_qty = 0, tmp_hp;
  238. struct map_session_data *bsd;
  239. struct status_change *sc;
  240. int range = skill_get_splash(LG_BANDING,skill_lv);
  241. nullpo_ret(sd);
  242. c = 0;
  243. memset(b_sd, 0, sizeof(b_sd));
  244. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  245. if( c < 1 ) //just recalc status no need to recalc hp
  246. { // No more Royal Guards in Banding found.
  247. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  248. {
  249. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  250. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  251. }
  252. return 0;
  253. }
  254. //Add yourself
  255. hp = status_get_hp(&sd->bl);
  256. i++;
  257. // Get total HP of all Royal Guards in party.
  258. for( j = 0; j < i; j++ )
  259. {
  260. bsd = map_id2sd(b_sd[j]);
  261. if( bsd != NULL )
  262. hp += status_get_hp(&bsd->bl);
  263. }
  264. // Set average HP.
  265. hp = hp / i;
  266. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  267. for( j = 0; j < i; j++ )
  268. {
  269. bsd = map_id2sd(b_sd[j]);
  270. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 )
  271. {
  272. extra_hp += tmp_hp;
  273. tmp_qty++;
  274. }
  275. }
  276. if( extra_hp > 0 && tmp_qty > 0 )
  277. hp += extra_hp / tmp_qty;
  278. for( j = 0; j < i; j++ )
  279. {
  280. bsd = map_id2sd(b_sd[j]);
  281. if( bsd != NULL )
  282. {
  283. status_set_hp(&bsd->bl,hp,0); // Set hp
  284. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  285. {
  286. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  287. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  288. }
  289. }
  290. }
  291. return c;
  292. }
  293. // Increases a player's fame points and displays a notice to him
  294. void pc_addfame(struct map_session_data *sd,int count)
  295. {
  296. int ranktype=-1;
  297. nullpo_retv(sd);
  298. sd->status.fame += count;
  299. if(sd->status.fame > MAX_FAME)
  300. sd->status.fame = MAX_FAME;
  301. switch(sd->class_&MAPID_UPPERMASK){
  302. case MAPID_BLACKSMITH: ranktype=0; break;
  303. case MAPID_ALCHEMIST: ranktype=1; break;
  304. case MAPID_TAEKWON: ranktype=2; break;
  305. }
  306. clif_update_rankingpoint(sd,ranktype,count);
  307. chrif_updatefamelist(sd);
  308. }
  309. // Check whether a player ID is in the fame rankers' list of its job, returns his/her position if so, 0 else
  310. unsigned char pc_famerank(int char_id, int job)
  311. {
  312. int i;
  313. switch(job){
  314. case MAPID_BLACKSMITH: // Blacksmith
  315. for(i = 0; i < MAX_FAME_LIST; i++){
  316. if(smith_fame_list[i].id == char_id)
  317. return i + 1;
  318. }
  319. break;
  320. case MAPID_ALCHEMIST: // Alchemist
  321. for(i = 0; i < MAX_FAME_LIST; i++){
  322. if(chemist_fame_list[i].id == char_id)
  323. return i + 1;
  324. }
  325. break;
  326. case MAPID_TAEKWON: // Taekwon
  327. for(i = 0; i < MAX_FAME_LIST; i++){
  328. if(taekwon_fame_list[i].id == char_id)
  329. return i + 1;
  330. }
  331. break;
  332. }
  333. return 0;
  334. }
  335. int pc_setrestartvalue(struct map_session_data *sd,int type) {
  336. struct status_data *status, *b_status;
  337. nullpo_ret(sd);
  338. b_status = &sd->base_status;
  339. status = &sd->battle_status;
  340. if (type&1) { //Normal resurrection
  341. status->hp = 1; //Otherwise status_heal may fail if dead.
  342. status_heal(&sd->bl, b_status->hp, 0, 1);
  343. if( status->sp < b_status->sp )
  344. status_set_sp(&sd->bl, b_status->sp, 1);
  345. } else { //Just for saving on the char-server (with values as if respawned)
  346. sd->status.hp = b_status->hp;
  347. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  348. }
  349. return 0;
  350. }
  351. /*==========================================
  352. Rental System
  353. *------------------------------------------*/
  354. static int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  355. {
  356. struct map_session_data *sd = map_id2sd(id);
  357. if( sd == NULL )
  358. return 0;
  359. if( tid != sd->rental_timer )
  360. {
  361. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  362. return 0;
  363. }
  364. pc_inventory_rentals(sd);
  365. return 1;
  366. }
  367. int pc_inventory_rental_clear(struct map_session_data *sd)
  368. {
  369. if( sd->rental_timer != INVALID_TIMER )
  370. {
  371. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  372. sd->rental_timer = INVALID_TIMER;
  373. }
  374. return 1;
  375. }
  376. void pc_inventory_rentals(struct map_session_data *sd)
  377. {
  378. int i, c = 0;
  379. unsigned int expire_tick, next_tick = UINT_MAX;
  380. for( i = 0; i < MAX_INVENTORY; i++ )
  381. { // Check for Rentals on Inventory
  382. if( sd->status.inventory[i].nameid == 0 )
  383. continue; // Nothing here
  384. if( sd->status.inventory[i].expire_time == 0 )
  385. continue;
  386. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  387. if( sd->status.inventory[i].nameid == ITEMID_REINS_OF_MOUNT
  388. && sd->sc.option&OPTION_MOUNTING ) {
  389. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);
  390. }
  391. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  392. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  393. } else {
  394. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  395. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  396. next_tick = min(expire_tick, next_tick);
  397. c++;
  398. }
  399. }
  400. 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
  401. sd->rental_timer = add_timer(gettick() + min(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  402. else
  403. sd->rental_timer = INVALID_TIMER;
  404. }
  405. void pc_inventory_rental_add(struct map_session_data *sd, int seconds)
  406. {
  407. int tick = seconds * 1000;
  408. if( sd == NULL )
  409. return;
  410. if( sd->rental_timer != INVALID_TIMER )
  411. {
  412. const struct TimerData * td;
  413. td = get_timer(sd->rental_timer);
  414. if( DIFF_TICK(td->tick, gettick()) > tick )
  415. { // Update Timer as this one ends first than the current one
  416. pc_inventory_rental_clear(sd);
  417. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  418. }
  419. }
  420. else
  421. sd->rental_timer = add_timer(gettick() + min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  422. }
  423. /**
  424. * Determines if player can give / drop / trade / vend items
  425. */
  426. bool pc_can_give_items(struct map_session_data *sd)
  427. {
  428. return pc_has_permission(sd, PC_PERM_TRADE);
  429. }
  430. /**
  431. * Determines if player can give / drop / trade / vend bounded items
  432. */
  433. bool pc_can_give_bounded_items(struct map_session_data *sd)
  434. {
  435. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  436. }
  437. /*==========================================
  438. * prepares character for saving.
  439. *------------------------------------------*/
  440. int pc_makesavestatus(struct map_session_data *sd)
  441. {
  442. nullpo_ret(sd);
  443. if(!battle_config.save_clothcolor)
  444. sd->status.clothes_color=0;
  445. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  446. //status change load/saving. [Skotlex]
  447. #ifdef NEW_CARTS
  448. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  449. #else
  450. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  451. #endif
  452. if (sd->sc.data[SC_JAILED])
  453. { //When Jailed, do not move last point.
  454. if(pc_isdead(sd)){
  455. pc_setrestartvalue(sd,0);
  456. } else {
  457. sd->status.hp = sd->battle_status.hp;
  458. sd->status.sp = sd->battle_status.sp;
  459. }
  460. sd->status.last_point.map = sd->mapindex;
  461. sd->status.last_point.x = sd->bl.x;
  462. sd->status.last_point.y = sd->bl.y;
  463. return 0;
  464. }
  465. if(pc_isdead(sd)){
  466. pc_setrestartvalue(sd,0);
  467. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  468. } else {
  469. sd->status.hp = sd->battle_status.hp;
  470. sd->status.sp = sd->battle_status.sp;
  471. sd->status.last_point.map = sd->mapindex;
  472. sd->status.last_point.x = sd->bl.x;
  473. sd->status.last_point.y = sd->bl.y;
  474. }
  475. if(map[sd->bl.m].flag.nosave){
  476. struct map_data *m=&map[sd->bl.m];
  477. if(m->save.map)
  478. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  479. else
  480. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  481. }
  482. return 0;
  483. }
  484. /*==========================================
  485. * Off init ? Connection?
  486. *------------------------------------------*/
  487. int pc_setnewpc(struct map_session_data *sd, int account_id, int char_id, int login_id1, unsigned int client_tick, int sex, int fd)
  488. {
  489. nullpo_ret(sd);
  490. sd->bl.id = account_id;
  491. sd->status.account_id = account_id;
  492. sd->status.char_id = char_id;
  493. sd->status.sex = sex;
  494. sd->login_id1 = login_id1;
  495. sd->login_id2 = 0; // at this point, we can not know the value :(
  496. sd->client_tick = client_tick;
  497. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  498. sd->bl.type = BL_PC;
  499. sd->canlog_tick = gettick();
  500. //Required to prevent homunculus copuing a base speed of 0.
  501. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  502. return 0;
  503. }
  504. int pc_equippoint(struct map_session_data *sd,int n){
  505. int ep = 0;
  506. nullpo_ret(sd);
  507. if(!sd->inventory_data[n])
  508. return 0;
  509. if (!itemdb_isequip2(sd->inventory_data[n]))
  510. return 0; //Not equippable by players.
  511. ep = sd->inventory_data[n]->equip;
  512. if(sd->inventory_data[n]->look == W_DAGGER ||
  513. sd->inventory_data[n]->look == W_1HSWORD ||
  514. sd->inventory_data[n]->look == W_1HAXE) {
  515. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  516. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  517. return EQP_ARMS;
  518. }
  519. return ep;
  520. }
  521. int pc_setinventorydata(struct map_session_data *sd)
  522. {
  523. int i,id;
  524. nullpo_ret(sd);
  525. for(i=0;i<MAX_INVENTORY;i++) {
  526. id = sd->status.inventory[i].nameid;
  527. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  528. }
  529. return 0;
  530. }
  531. int pc_calcweapontype(struct map_session_data *sd)
  532. {
  533. nullpo_ret(sd);
  534. // single-hand
  535. if(sd->weapontype2 == W_FIST) {
  536. sd->status.weapon = sd->weapontype1;
  537. return 1;
  538. }
  539. if(sd->weapontype1 == W_FIST) {
  540. sd->status.weapon = sd->weapontype2;
  541. return 1;
  542. }
  543. // dual-wield
  544. sd->status.weapon = 0;
  545. switch (sd->weapontype1){
  546. case W_DAGGER:
  547. switch (sd->weapontype2) {
  548. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  549. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  550. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  551. }
  552. break;
  553. case W_1HSWORD:
  554. switch (sd->weapontype2) {
  555. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  556. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  557. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  558. }
  559. break;
  560. case W_1HAXE:
  561. switch (sd->weapontype2) {
  562. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  563. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  564. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  565. }
  566. }
  567. // unknown, default to right hand type
  568. if (!sd->status.weapon)
  569. sd->status.weapon = sd->weapontype1;
  570. return 2;
  571. }
  572. int pc_setequipindex(struct map_session_data *sd)
  573. {
  574. int i,j;
  575. nullpo_ret(sd);
  576. for(i=0;i<EQI_MAX;i++)
  577. sd->equip_index[i] = -1;
  578. for(i=0;i<MAX_INVENTORY;i++) {
  579. if(sd->status.inventory[i].nameid <= 0)
  580. continue;
  581. if(sd->status.inventory[i].equip) {
  582. for(j=0;j<EQI_MAX;j++)
  583. if(sd->status.inventory[i].equip & equip_pos[j])
  584. sd->equip_index[j] = i;
  585. if(sd->status.inventory[i].equip & EQP_HAND_R)
  586. {
  587. if(sd->inventory_data[i])
  588. sd->weapontype1 = sd->inventory_data[i]->look;
  589. else
  590. sd->weapontype1 = 0;
  591. }
  592. if( sd->status.inventory[i].equip & EQP_HAND_L )
  593. {
  594. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  595. sd->weapontype2 = sd->inventory_data[i]->look;
  596. else
  597. sd->weapontype2 = 0;
  598. }
  599. }
  600. }
  601. pc_calcweapontype(sd);
  602. return 0;
  603. }
  604. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  605. //{
  606. // int i;
  607. // struct item *item = &sd->status.inventory[eqindex];
  608. // struct item_data *data;
  609. //
  610. // //Crafted/made/hatched items.
  611. // if (itemdb_isspecial(item->card[0]))
  612. // return 1;
  613. //
  614. // /* scan for enchant armor gems */
  615. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  616. // s = MAX_SLOTS - 1;
  617. //
  618. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  619. // return( i < s ) ? 0 : 1;
  620. //}
  621. bool pc_isequipped(struct map_session_data *sd, int nameid)
  622. {
  623. int i, j, index;
  624. for( i = 0; i < EQI_MAX; i++ )
  625. {
  626. index = sd->equip_index[i];
  627. if( index < 0 ) continue;
  628. if( i == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  629. if( i == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  630. if( i == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  631. if( !sd->inventory_data[index] ) continue;
  632. if( sd->inventory_data[index]->nameid == nameid )
  633. return true;
  634. for( j = 0; j < sd->inventory_data[index]->slot; j++ )
  635. if( sd->status.inventory[index].card[j] == nameid )
  636. return true;
  637. }
  638. return false;
  639. }
  640. bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
  641. {
  642. if( !p1_sd || !p2_sd || !b_sd )
  643. return false;
  644. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  645. return false; // already adopted baby / in adopt request
  646. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  647. return false; // You need to be married and in party with baby to adopt
  648. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  649. return false; // Not married, wrong married
  650. if( p2_sd->status.party_id != p1_sd->status.party_id )
  651. return false; // Both parents need to be in the same party
  652. // Parents need to have their ring equipped
  653. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  654. return false;
  655. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  656. return false;
  657. // Already adopted a baby
  658. if( p1_sd->status.child || p2_sd->status.child ) {
  659. clif_Adopt_reply(p1_sd, 0);
  660. return false;
  661. }
  662. // Parents need at least lvl 70 to adopt
  663. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  664. clif_Adopt_reply(p1_sd, 1);
  665. return false;
  666. }
  667. if( b_sd->status.partner_id ) {
  668. clif_Adopt_reply(p1_sd, 2);
  669. return false;
  670. }
  671. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE ) )
  672. return false;
  673. return true;
  674. }
  675. /*==========================================
  676. * Adoption Process
  677. *------------------------------------------*/
  678. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  679. {
  680. int job, joblevel;
  681. unsigned int jobexp;
  682. if( !pc_can_Adopt(p1_sd, p2_sd, b_sd) )
  683. return false;
  684. // Preserve current job levels and progress
  685. joblevel = b_sd->status.job_level;
  686. jobexp = b_sd->status.job_exp;
  687. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  688. if( job != -1 && !pc_jobchange(b_sd, job, 0) )
  689. { // Success, proceed to configure parents and baby skills
  690. p1_sd->status.child = b_sd->status.char_id;
  691. p2_sd->status.child = b_sd->status.char_id;
  692. b_sd->status.father = p1_sd->status.char_id;
  693. b_sd->status.mother = p2_sd->status.char_id;
  694. // Restore progress
  695. b_sd->status.job_level = joblevel;
  696. clif_updatestatus(b_sd, SP_JOBLEVEL);
  697. b_sd->status.job_exp = jobexp;
  698. clif_updatestatus(b_sd, SP_JOBEXP);
  699. // Baby Skills
  700. pc_skill(b_sd, WE_BABY, 1, 0);
  701. pc_skill(b_sd, WE_CALLPARENT, 1, 0);
  702. // Parents Skills
  703. pc_skill(p1_sd, WE_CALLBABY, 1, 0);
  704. pc_skill(p2_sd, WE_CALLBABY, 1, 0);
  705. return true;
  706. }
  707. return false; // Job Change Fail
  708. }
  709. /*==========================================
  710. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  711. Returns:
  712. false : Cannot use/equip
  713. true : Can use/equip
  714. * Credits:
  715. [Inkfish] for first idea
  716. [Haru] for third-classes extension
  717. [Cydh] finishing :D
  718. *------------------------------------------*/
  719. bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  720. while (1) {
  721. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  722. break;
  723. #ifndef RENEWAL
  724. //allow third classes to use trans. class items
  725. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  726. break;
  727. //third-baby classes can use same item too
  728. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  729. break;
  730. //don't need to decide specific rules for third-classes?
  731. //items for third classes can be used for all third classes
  732. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  733. break;
  734. #else
  735. //trans. classes (exl. third-trans.)
  736. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  737. break;
  738. //baby classes (exl. third-baby)
  739. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  740. break;
  741. //third classes (exl. third-trans. and baby-third)
  742. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  743. break;
  744. //trans-third classes
  745. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  746. break;
  747. //third-baby classes
  748. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  749. break;
  750. #endif
  751. return false;
  752. }
  753. return true;
  754. }
  755. /*=================================================
  756. * Checks if the player can equip the item at index n in inventory.
  757. * Returns 0 (no) or 1 (yes).
  758. *------------------------------------------------*/
  759. int pc_isequip(struct map_session_data *sd,int n)
  760. {
  761. struct item_data *item;
  762. nullpo_ret(sd);
  763. item = sd->inventory_data[n];
  764. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  765. return 1;
  766. if(item == NULL)
  767. return 0;
  768. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  769. return 0;
  770. #ifdef RENEWAL
  771. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  772. return 0;
  773. #endif
  774. if(item->sex != 2 && sd->status.sex != item->sex)
  775. return 0;
  776. if (sd->sc.count) {
  777. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  778. return 0;
  779. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  780. return 0;
  781. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  782. return 0;
  783. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  784. return 0;
  785. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  786. return 0;
  787. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  788. return 0;
  789. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  790. //Spirit of Super Novice equip bonuses. [Skotlex]
  791. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  792. return 1; //Can equip all helms
  793. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON)
  794. switch(item->look) { //In weapons, the look determines type of weapon.
  795. case W_DAGGER: //Level 4 Knives are equippable.. this means all knives, I'd guess?
  796. case W_1HSWORD: //All 1H swords
  797. case W_1HAXE: //All 1H Axes
  798. case W_MACE: //All 1H Maces
  799. case W_STAFF: //All 1H Staves
  800. return 1;
  801. }
  802. }
  803. }
  804. //fail to equip if item is restricted
  805. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  806. return 0;
  807. //Not equipable by class. [Skotlex]
  808. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  809. return 0;
  810. if (!pc_isItemClass(sd,item))
  811. return 0;
  812. return 1;
  813. }
  814. /*==========================================
  815. * No problem with the session id
  816. * set the status that has been sent from char server
  817. *------------------------------------------*/
  818. bool pc_authok(struct map_session_data *sd, int login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  819. {
  820. int i;
  821. #ifdef BOUND_ITEMS
  822. int j;
  823. int idxlist[MAX_INVENTORY];
  824. #endif
  825. unsigned long tick = gettick();
  826. uint32 ip = session[sd->fd]->client_addr;
  827. sd->login_id2 = login_id2;
  828. sd->group_id = group_id;
  829. /* load user permissions */
  830. pc_group_pc_load(sd);
  831. memcpy(&sd->status, st, sizeof(*st));
  832. if (st->sex != sd->status.sex) {
  833. clif_authfail_fd(sd->fd, 0);
  834. return false;
  835. }
  836. //Set the map-server used job id. [Skotlex]
  837. i = pc_jobid2mapid(sd->status.class_);
  838. if (i == -1) { //Invalid class?
  839. 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);
  840. sd->status.class_ = JOB_NOVICE;
  841. sd->class_ = MAPID_NOVICE;
  842. } else
  843. sd->class_ = i;
  844. // Checks and fixes to character status data, that are required
  845. // in case of configuration change or stuff, which cannot be
  846. // checked on char-server.
  847. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  848. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  849. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  850. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  851. if(!sd->status.hp) pc_setdead(sd);
  852. sd->state.connect_new = 1;
  853. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  854. sd->invincible_timer = INVALID_TIMER;
  855. sd->npc_timer_id = INVALID_TIMER;
  856. sd->pvp_timer = INVALID_TIMER;
  857. #ifdef SECURE_NPCTIMEOUT
  858. // Initialize to defaults/expected
  859. sd->npc_idle_timer = INVALID_TIMER;
  860. sd->npc_idle_tick = tick;
  861. sd->npc_idle_type = NPCT_INPUT;
  862. #endif
  863. sd->canuseitem_tick = tick;
  864. sd->canusecashfood_tick = tick;
  865. sd->canequip_tick = tick;
  866. sd->cantalk_tick = tick;
  867. sd->canskill_tick = tick;
  868. sd->cansendmail_tick = tick;
  869. for(i = 0; i < MAX_SKILL_LEVEL; i++)
  870. sd->spirit_timer[i] = INVALID_TIMER;
  871. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  872. sd->autobonus[i].active = INVALID_TIMER;
  873. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  874. sd->autobonus2[i].active = INVALID_TIMER;
  875. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  876. sd->autobonus3[i].active = INVALID_TIMER;
  877. if (battle_config.item_auto_get)
  878. sd->state.autoloot = 10000;
  879. if (battle_config.disp_experience)
  880. sd->state.showexp = 1;
  881. if (battle_config.disp_zeny)
  882. sd->state.showzeny = 1;
  883. if (!(battle_config.display_skill_fail&2))
  884. sd->state.showdelay = 1;
  885. pc_setinventorydata(sd);
  886. pc_setequipindex(sd);
  887. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  888. sd->status.option &= ~OPTION_INVISIBLE;
  889. }
  890. status_change_init(&sd->bl);
  891. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  892. //Set here because we need the inventory data for weapon sprite parsing.
  893. status_set_viewdata(&sd->bl, sd->status.class_);
  894. unit_dataset(&sd->bl);
  895. sd->guild_x = -1;
  896. sd->guild_y = -1;
  897. // Event Timers
  898. for( i = 0; i < MAX_EVENTTIMER; i++ )
  899. sd->eventtimer[i] = INVALID_TIMER;
  900. // Rental Timer
  901. sd->rental_timer = INVALID_TIMER;
  902. for( i = 0; i < 3; i++ )
  903. sd->hate_mob[i] = -1;
  904. //warp player
  905. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
  906. 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);
  907. // try warping to a default map instead (church graveyard)
  908. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
  909. // if we fail again
  910. clif_authfail_fd(sd->fd, 0);
  911. return false;
  912. }
  913. }
  914. clif_authok(sd);
  915. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  916. sd->die_counter=-1;
  917. //display login notice
  918. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  919. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  920. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  921. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  922. sd->status.name, sd->status.account_id, sd->status.char_id,
  923. sd->packet_ver, CONVIP(ip), sd->group_id);
  924. // Send friends list
  925. clif_friendslist_send(sd);
  926. if( !changing_mapservers ) {
  927. if (battle_config.display_version == 1)
  928. pc_show_version(sd);
  929. // Message of the Day [Valaris]
  930. for(i=0; motd_text[i][0] && i < MOTD_LINE_SIZE; i++) {
  931. if (battle_config.motd_type)
  932. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  933. else
  934. clif_displaymessage(sd->fd, motd_text[i]);
  935. }
  936. // message of the limited time of the account
  937. if (expiration_time != 0) { // don't display if it's unlimited or unknow value
  938. char tmpstr[1024];
  939. strftime(tmpstr, sizeof(tmpstr) - 1, msg_txt(sd,501), localtime(&expiration_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  940. clif_wis_message(sd->fd, wisp_server_name, tmpstr, strlen(tmpstr)+1);
  941. }
  942. /**
  943. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  944. **/
  945. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  946. }
  947. /**
  948. * Check if player have any item cooldowns on
  949. **/
  950. pc_itemcd_do(sd,true);
  951. #ifdef BOUND_ITEMS
  952. // Party bound item check
  953. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,3,idxlist))) { // Party was deleted while character offline
  954. for(i=0;i<j;i++)
  955. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  956. }
  957. #endif
  958. // Request all registries (auth is considered completed whence they arrive)
  959. intif_request_registry(sd,7);
  960. return true;
  961. }
  962. /*==========================================
  963. * Closes a connection because it failed to be authenticated from the char server.
  964. *------------------------------------------*/
  965. void pc_authfail(struct map_session_data *sd)
  966. {
  967. clif_authfail_fd(sd->fd, 0);
  968. return;
  969. }
  970. //Attempts to set a mob.
  971. int pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  972. {
  973. int class_;
  974. if (!sd || !bl || pos < 0 || pos > 2)
  975. return 0;
  976. if (sd->hate_mob[pos] != -1)
  977. { //Can't change hate targets.
  978. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  979. return 0;
  980. }
  981. class_ = status_get_class(bl);
  982. if (!pcdb_checkid(class_)) {
  983. unsigned int max_hp = status_get_max_hp(bl);
  984. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  985. return 0;
  986. if (pos != status_get_size(bl))
  987. return 0; //Wrong size
  988. }
  989. sd->hate_mob[pos] = class_;
  990. pc_setglobalreg(sd,sg_info[pos].hate_var,class_+1);
  991. clif_hate_info(sd, pos, class_, 1);
  992. return 1;
  993. }
  994. /*==========================================
  995. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  996. *------------------------------------------*/
  997. int pc_reg_received(struct map_session_data *sd)
  998. {
  999. int i,j;
  1000. sd->change_level_2nd = pc_readglobalreg(sd,"jobchange_level");
  1001. sd->change_level_3rd = pc_readglobalreg(sd,"jobchange_level_3rd");
  1002. sd->die_counter = pc_readglobalreg(sd,"PC_DIE_COUNTER");
  1003. sd->langtype = pc_readaccountreg(sd,"#langtype");
  1004. if(msg_checklangtype(sd->langtype,true)<0) sd->langtype=0; //invalid langtype reset to default
  1005. // Cash shop
  1006. sd->cashPoints = pc_readaccountreg(sd,"#CASHPOINTS");
  1007. sd->kafraPoints = pc_readaccountreg(sd,"#KAFRAPOINTS");
  1008. // Cooking Exp
  1009. sd->cook_mastery = pc_readglobalreg(sd,"COOK_MASTERY");
  1010. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1011. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1012. sd->mission_mobid = pc_readglobalreg(sd,"TK_MISSION_ID");
  1013. sd->mission_count = pc_readglobalreg(sd,"TK_MISSION_COUNT");
  1014. }
  1015. //SG map and mob read [Komurka]
  1016. for(i=0;i<MAX_PC_FEELHATE;i++) //for now - someone need to make reading from txt/sql
  1017. {
  1018. if ((j = pc_readglobalreg(sd,sg_info[i].feel_var))!=0) {
  1019. sd->feel_map[i].index = j;
  1020. sd->feel_map[i].m = map_mapindex2mapid(j);
  1021. } else {
  1022. sd->feel_map[i].index = 0;
  1023. sd->feel_map[i].m = -1;
  1024. }
  1025. sd->hate_mob[i] = pc_readglobalreg(sd,sg_info[i].hate_var)-1;
  1026. }
  1027. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1028. sd->cloneskill_id = pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM);
  1029. if (sd->cloneskill_id > 0) {
  1030. sd->status.skill[sd->cloneskill_id].id = sd->cloneskill_id;
  1031. sd->status.skill[sd->cloneskill_id].lv = pc_readglobalreg(sd,SKILL_VAR_PLAGIARISM_LV);
  1032. if (sd->status.skill[sd->cloneskill_id].lv > i)
  1033. sd->status.skill[sd->cloneskill_id].lv = i;
  1034. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1035. }
  1036. }
  1037. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1038. sd->reproduceskill_id = pc_readglobalreg(sd,SKILL_VAR_REPRODUCE);
  1039. if( sd->reproduceskill_id > 0) {
  1040. sd->status.skill[sd->reproduceskill_id].id = sd->reproduceskill_id;
  1041. sd->status.skill[sd->reproduceskill_id].lv = pc_readglobalreg(sd,SKILL_VAR_REPRODUCE_LV);
  1042. if( i < sd->status.skill[sd->reproduceskill_id].lv)
  1043. sd->status.skill[sd->reproduceskill_id].lv = i;
  1044. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1045. }
  1046. }
  1047. //Weird... maybe registries were reloaded?
  1048. if (sd->state.active)
  1049. return 0;
  1050. sd->state.active = 1;
  1051. if (sd->status.party_id)
  1052. party_member_joined(sd);
  1053. if (sd->status.guild_id)
  1054. guild_member_joined(sd);
  1055. // pet
  1056. if (sd->status.pet_id > 0)
  1057. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1058. // Homunculus [albator]
  1059. if( sd->status.hom_id > 0 )
  1060. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1061. if( sd->status.mer_id > 0 )
  1062. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1063. if( sd->status.ele_id > 0 )
  1064. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1065. map_addiddb(&sd->bl);
  1066. map_delnickdb(sd->status.char_id, sd->status.name);
  1067. if (!chrif_auth_finished(sd))
  1068. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1069. pc_check_available_item(sd); // Check for invalid(ated) items.
  1070. pc_load_combo(sd);
  1071. status_calc_pc(sd,1);
  1072. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1073. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1074. chrif_bsdata_request(sd->status.char_id);
  1075. sd->storage_size = MIN_STORAGE; //default to min
  1076. if(battle_config.feature_banking)
  1077. chrif_req_login_operation(sd->status.account_id, sd->status.name, 7, 0, 1, 0); //request Bank data
  1078. #ifdef VIP_ENABLE
  1079. sd->vip.time = 0;
  1080. sd->vip.enabled = 0;
  1081. chrif_req_login_operation(sd->status.account_id, sd->status.name, 6, 0, 1, 0); // request VIP informations
  1082. #endif
  1083. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1084. intif_request_questlog(sd);
  1085. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1086. sd->state.connect_new = 1;
  1087. clif_parse_LoadEndAck(sd->fd, sd);
  1088. }
  1089. pc_inventory_rentals(sd);
  1090. if( sd->sc.option&OPTION_INVISIBLE ) {
  1091. sd->vd.class_ = INVISIBLE_CLASS;
  1092. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1093. // decrement the number of pvp players on the map
  1094. map[sd->bl.m].users_pvp--;
  1095. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1096. // unregister the player for ranking
  1097. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1098. sd->pvp_timer = INVALID_TIMER;
  1099. }
  1100. clif_changeoption( &sd->bl );
  1101. }
  1102. return 1;
  1103. }
  1104. static int pc_calc_skillpoint(struct map_session_data* sd)
  1105. {
  1106. int i,skill_lv,inf2,skill_point=0;
  1107. nullpo_ret(sd);
  1108. for(i=1;i<MAX_SKILL;i++){
  1109. if( (skill_lv = pc_checkskill(sd,i)) > 0) {
  1110. inf2 = skill_get_inf2(i);
  1111. if((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1112. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1113. ) {
  1114. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1115. skill_point += skill_lv;
  1116. else
  1117. if(sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0)
  1118. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1119. }
  1120. }
  1121. }
  1122. return skill_point;
  1123. }
  1124. /*==========================================
  1125. * Calculation of skill level.
  1126. *------------------------------------------*/
  1127. int pc_calc_skilltree(struct map_session_data *sd)
  1128. {
  1129. int i,id=0,flag;
  1130. int c=0;
  1131. nullpo_ret(sd);
  1132. i = pc_calc_skilltree_normalize_job(sd);
  1133. c = pc_mapid2jobid(i, sd->status.sex);
  1134. if( c == -1 )
  1135. { //Unable to normalize job??
  1136. 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);
  1137. return 1;
  1138. }
  1139. c = pc_class2idx(c);
  1140. for( i = 0; i < MAX_SKILL; i++ ) {
  1141. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1142. sd->status.skill[i].id = 0; //First clear skills.
  1143. /* permanent skills that must be re-checked */
  1144. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1145. switch( i ) {
  1146. case NV_TRICKDEAD:
  1147. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1148. sd->status.skill[i].id = 0;
  1149. sd->status.skill[i].lv = 0;
  1150. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1151. }
  1152. break;
  1153. }
  1154. }
  1155. }
  1156. for( i = 0; i < MAX_SKILL; i++ )
  1157. {
  1158. 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 )
  1159. { // Restore original level of skills after deleting earned skills.
  1160. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1161. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1162. }
  1163. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER && i >= DC_HUMMING && i<= DC_SERVICEFORYOU )
  1164. { //Enable Bard/Dancer spirit linked skills.
  1165. if( sd->status.sex )
  1166. { //Link dancer skills to bard.
  1167. if( sd->status.skill[i-8].lv < 10 )
  1168. continue;
  1169. sd->status.skill[i].id = i;
  1170. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1171. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1172. }
  1173. else
  1174. { //Link bard skills to dancer.
  1175. if( sd->status.skill[i].lv < 10 )
  1176. continue;
  1177. sd->status.skill[i-8].id = i - 8;
  1178. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1179. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1180. }
  1181. }
  1182. }
  1183. if( pc_has_permission(sd, PC_PERM_ALL_SKILL) ) {
  1184. for( i = 0; i < MAX_SKILL; i++ ) {
  1185. switch(i) {
  1186. /**
  1187. * Dummy skills must be added here otherwise they'll be displayed in the,
  1188. * skill tree and since they have no icons they'll give resource errors
  1189. **/
  1190. case SM_SELFPROVOKE:
  1191. case AB_DUPLELIGHT_MELEE:
  1192. case AB_DUPLELIGHT_MAGIC:
  1193. case WL_CHAINLIGHTNING_ATK:
  1194. case WL_TETRAVORTEX_FIRE:
  1195. case WL_TETRAVORTEX_WATER:
  1196. case WL_TETRAVORTEX_WIND:
  1197. case WL_TETRAVORTEX_GROUND:
  1198. case WL_SUMMON_ATK_FIRE:
  1199. case WL_SUMMON_ATK_WIND:
  1200. case WL_SUMMON_ATK_WATER:
  1201. case WL_SUMMON_ATK_GROUND:
  1202. case LG_OVERBRAND_BRANDISH:
  1203. case LG_OVERBRAND_PLUSATK:
  1204. case WM_SEVERE_RAINSTORM_MELEE:
  1205. continue;
  1206. default:
  1207. break;
  1208. }
  1209. if( skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL) )
  1210. continue; //Only skills you can't have are npc/guild ones
  1211. if( skill_get_max(i) > 0 )
  1212. sd->status.skill[i].id = i;
  1213. }
  1214. return 0;
  1215. }
  1216. do {
  1217. flag = 0;
  1218. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1219. {
  1220. int f;
  1221. if( sd->status.skill[id].id )
  1222. continue; //Skill already known.
  1223. f = 1;
  1224. if(!battle_config.skillfree) {
  1225. int j;
  1226. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1227. int k;
  1228. if((k=skill_tree[c][i].need[j].id))
  1229. {
  1230. if (sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED)
  1231. k = 0; //Not learned.
  1232. else
  1233. if (sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1234. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1235. else
  1236. k = pc_checkskill(sd,k);
  1237. if (k < skill_tree[c][i].need[j].lv)
  1238. {
  1239. f = 0;
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. if( sd->status.job_level < skill_tree[c][i].joblv ) { //We need to get the actual class in this case
  1245. int class = pc_mapid2jobid(sd->class_, sd->status.sex);
  1246. class = pc_class2idx(class);
  1247. if (class == c || (class != c && sd->status.job_level < skill_tree[class][i].joblv))
  1248. f = 0; // job level requirement wasn't satisfied
  1249. }
  1250. }
  1251. if( f ) {
  1252. int inf2;
  1253. inf2 = skill_get_inf2(id);
  1254. if(!sd->status.skill[id].lv && (
  1255. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1256. inf2&INF2_WEDDING_SKILL ||
  1257. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1258. ))
  1259. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1260. sd->status.skill[id].id = id;
  1261. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1262. sd->status.skill[id].lv = 1; // need to manually specify a skill level
  1263. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1264. }
  1265. flag = 1; // skill list has changed, perform another pass
  1266. }
  1267. }
  1268. } while(flag);
  1269. //
  1270. if( c > 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && sd->status.skill_point == 0 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  1271. {
  1272. /* Taekwon Ranger Bonus Skill Tree
  1273. ============================================
  1274. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1275. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1276. - (sd->status.skill_point == 0) to wait until all skill points are asigned to avoid problems with Job Change quest. */
  1277. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1278. {
  1279. if( (skill_get_inf2(id)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1280. continue; //Do not include Quest/Wedding skills.
  1281. if( sd->status.skill[id].id == 0 )
  1282. {
  1283. sd->status.skill[id].id = id;
  1284. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1285. }
  1286. else if( id != NV_BASIC )
  1287. {
  1288. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Remember original level
  1289. }
  1290. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_);
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. //Checks if you can learn a new skill after having leveled up a skill.
  1296. static void pc_check_skilltree(struct map_session_data *sd, int skill)
  1297. {
  1298. int i,id=0,flag;
  1299. int c=0;
  1300. if(battle_config.skillfree)
  1301. return; //Function serves no purpose if this is set
  1302. i = pc_calc_skilltree_normalize_job(sd);
  1303. c = pc_mapid2jobid(i, sd->status.sex);
  1304. if (c == -1) { //Unable to normalize job??
  1305. 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);
  1306. return;
  1307. }
  1308. c = pc_class2idx(c);
  1309. do {
  1310. flag = 0;
  1311. for( i = 0; i < MAX_SKILL_TREE && (id=skill_tree[c][i].id)>0; i++ )
  1312. {
  1313. int j, f = 1, k;
  1314. if( sd->status.skill[id].id ) //Already learned
  1315. continue;
  1316. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ )
  1317. {
  1318. if( (k = skill_tree[c][i].need[j].id) )
  1319. {
  1320. if( sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED )
  1321. k = 0; //Not learned.
  1322. else
  1323. if( sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1324. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1325. else
  1326. k = pc_checkskill(sd,k);
  1327. if( k < skill_tree[c][i].need[j].lv )
  1328. {
  1329. f = 0;
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. if( !f )
  1335. continue;
  1336. if( sd->status.job_level < skill_tree[c][i].joblv )
  1337. continue;
  1338. j = skill_get_inf2(id);
  1339. if( !sd->status.skill[id].lv && (
  1340. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1341. j&INF2_WEDDING_SKILL ||
  1342. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1343. ) )
  1344. continue; //Cannot be learned via normal means.
  1345. sd->status.skill[id].id = id;
  1346. flag = 1;
  1347. }
  1348. } while(flag);
  1349. }
  1350. // Make sure all the skills are in the correct condition
  1351. // before persisting to the backend.. [MouseJstr]
  1352. int pc_clean_skilltree(struct map_session_data *sd)
  1353. {
  1354. int i;
  1355. for (i = 0; i < MAX_SKILL; i++){
  1356. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED)
  1357. {
  1358. sd->status.skill[i].id = 0;
  1359. sd->status.skill[i].lv = 0;
  1360. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1361. }
  1362. else
  1363. if (sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0){
  1364. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1365. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1366. }
  1367. }
  1368. return 0;
  1369. }
  1370. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1371. {
  1372. int skill_point, novice_skills;
  1373. int c = sd->class_;
  1374. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1375. return c;
  1376. skill_point = pc_calc_skillpoint(sd);
  1377. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1378. // limit 1st class and above to novice job levels
  1379. if(skill_point < novice_skills)
  1380. {
  1381. c = MAPID_NOVICE;
  1382. }
  1383. // limit 2nd class and above to first class job levels (super novices are exempt)
  1384. else if ((sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1385. {
  1386. // regenerate change_level_2nd
  1387. if (!sd->change_level_2nd)
  1388. {
  1389. if (sd->class_&JOBL_THIRD)
  1390. {
  1391. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1392. if (!sd->change_level_3rd)
  1393. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1394. else
  1395. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1396. - (sd->status.job_level - 1)
  1397. - (sd->change_level_3rd - 1)
  1398. - novice_skills;
  1399. }
  1400. else
  1401. {
  1402. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1403. - (sd->status.job_level - 1)
  1404. - novice_skills;
  1405. }
  1406. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  1407. }
  1408. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1409. {
  1410. c &= MAPID_BASEMASK;
  1411. }
  1412. // limit 3rd class to 2nd class/trans job levels
  1413. else if(sd->class_&JOBL_THIRD)
  1414. {
  1415. // regenerate change_level_3rd
  1416. if (!sd->change_level_3rd)
  1417. {
  1418. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1419. - (sd->status.job_level - 1)
  1420. - (sd->change_level_2nd - 1)
  1421. - novice_skills;
  1422. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  1423. }
  1424. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1425. c &= MAPID_UPPERMASK;
  1426. }
  1427. }
  1428. // restore non-limiting flags
  1429. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1430. return c;
  1431. }
  1432. /*==========================================
  1433. * Updates the weight status
  1434. *------------------------------------------
  1435. * 1: overweight 50%
  1436. * 2: overweight 90%
  1437. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1438. */
  1439. int pc_updateweightstatus(struct map_session_data *sd)
  1440. {
  1441. int old_overweight;
  1442. int new_overweight;
  1443. nullpo_retr(1, sd);
  1444. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1445. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1446. if( old_overweight == new_overweight )
  1447. return 0; // no change
  1448. // stop old status change
  1449. if( old_overweight == 1 )
  1450. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1451. else if( old_overweight == 2 )
  1452. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1453. // start new status change
  1454. if( new_overweight == 1 )
  1455. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1456. else if( new_overweight == 2 )
  1457. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1458. // update overweight status
  1459. sd->regen.state.overweight = new_overweight;
  1460. return 0;
  1461. }
  1462. int pc_disguise(struct map_session_data *sd, int class_)
  1463. {
  1464. if (!class_ && !sd->disguise)
  1465. return 0;
  1466. if (class_ && sd->disguise == class_)
  1467. return 0;
  1468. if(sd->sc.option&OPTION_INVISIBLE)
  1469. { //Character is invisible. Stealth class-change. [Skotlex]
  1470. sd->disguise = class_; //viewdata is set on uncloaking.
  1471. return 2;
  1472. }
  1473. if (sd->bl.prev != NULL) {
  1474. pc_stop_walking(sd, 0);
  1475. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1476. }
  1477. if (!class_) {
  1478. sd->disguise = 0;
  1479. class_ = sd->status.class_;
  1480. } else
  1481. sd->disguise=class_;
  1482. status_set_viewdata(&sd->bl, class_);
  1483. clif_changeoption(&sd->bl);
  1484. if (sd->bl.prev != NULL) {
  1485. clif_spawn(&sd->bl);
  1486. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1487. { //It seems the cart info is lost on undisguise.
  1488. clif_cartlist(sd);
  1489. clif_updatestatus(sd,SP_CARTINFO);
  1490. }
  1491. if (sd->chatID) {
  1492. struct chat_data* cd;
  1493. nullpo_retr(1, sd);
  1494. cd = (struct chat_data*)map_id2bl(sd->chatID);
  1495. if( cd != NULL || (struct block_list*)sd == cd->owner )
  1496. clif_dispchat(cd,0);
  1497. }
  1498. }
  1499. return 1;
  1500. }
  1501. static int pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, short card_id)
  1502. {
  1503. int i;
  1504. if( !rate )
  1505. return 0;
  1506. for( i = 0; i < max && spell[i].id; i++ )
  1507. {
  1508. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1509. {
  1510. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1511. return 0;
  1512. rate += spell[i].rate;
  1513. break;
  1514. }
  1515. }
  1516. if (i == max) {
  1517. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1518. return 0;
  1519. }
  1520. spell[i].id = id;
  1521. spell[i].lv = lv;
  1522. spell[i].rate = rate;
  1523. //Auto-update flag value.
  1524. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1525. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1526. if (!(flag&BF_SKILLMASK)) {
  1527. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1528. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1529. }
  1530. spell[i].flag|= flag;
  1531. spell[i].card_id = card_id;
  1532. return 1;
  1533. }
  1534. static int pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, short card_id)
  1535. {
  1536. int i;
  1537. if( !rate )
  1538. return 0;
  1539. for( i = 0; i < max && spell[i].id; i++ )
  1540. {
  1541. ; // each autospell works independently
  1542. }
  1543. if( i == max )
  1544. {
  1545. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1546. return 0;
  1547. }
  1548. spell[i].flag = src_skill;
  1549. spell[i].id = id;
  1550. spell[i].lv = lv;
  1551. spell[i].rate = rate;
  1552. spell[i].card_id = card_id;
  1553. return 1;
  1554. }
  1555. static int pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, short rate, short arrow_rate, unsigned char flag)
  1556. {
  1557. int i;
  1558. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1559. flag|=ATF_SHORT|ATF_LONG; //Default range: both
  1560. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1561. flag|=ATF_TARGET; //Default target: enemy.
  1562. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1563. flag|=ATF_WEAPON; //Default type: weapon.
  1564. for (i = 0; i < max && effect[i].flag; i++) {
  1565. if (effect[i].id == id && effect[i].flag == flag)
  1566. {
  1567. effect[i].rate += rate;
  1568. effect[i].arrow_rate += arrow_rate;
  1569. return 1;
  1570. }
  1571. }
  1572. if (i == max) {
  1573. ShowWarning("pc_bonus: Reached max (%d) number of add effects per character!\n", max);
  1574. return 0;
  1575. }
  1576. effect[i].id = id;
  1577. effect[i].rate = rate;
  1578. effect[i].arrow_rate = arrow_rate;
  1579. effect[i].flag = flag;
  1580. return 1;
  1581. }
  1582. static int pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill, unsigned char target)
  1583. {
  1584. int i;
  1585. for( i = 0; i < max && effect[i].skill; i++ )
  1586. {
  1587. if( effect[i].id == id && effect[i].skill == skill && effect[i].target == target )
  1588. {
  1589. effect[i].rate += rate;
  1590. return 1;
  1591. }
  1592. }
  1593. if( i == max ) {
  1594. ShowWarning("pc_bonus: Reached max (%d) number of add effects on skill per character!\n", max);
  1595. return 0;
  1596. }
  1597. effect[i].id = id;
  1598. effect[i].rate = rate;
  1599. effect[i].skill = skill;
  1600. effect[i].target = target;
  1601. return 1;
  1602. }
  1603. static int pc_bonus_item_drop(struct s_add_drop *drop, const short max, short id, short group, int race, int rate)
  1604. {
  1605. int i;
  1606. //Apply config rate adjustment settings.
  1607. if (rate >= 0) { //Absolute drop.
  1608. if (battle_config.item_rate_adddrop != 100)
  1609. rate = rate*battle_config.item_rate_adddrop/100;
  1610. if (rate < battle_config.item_drop_adddrop_min)
  1611. rate = battle_config.item_drop_adddrop_min;
  1612. else if (rate > battle_config.item_drop_adddrop_max)
  1613. rate = battle_config.item_drop_adddrop_max;
  1614. } else { //Relative drop, max/min limits are applied at drop time.
  1615. if (battle_config.item_rate_adddrop != 100)
  1616. rate = rate*battle_config.item_rate_adddrop/100;
  1617. if (rate > -1)
  1618. rate = -1;
  1619. }
  1620. for(i = 0; i < max && (drop[i].id || drop[i].group); i++) {
  1621. if(
  1622. ((id && drop[i].id == id) ||
  1623. (group && drop[i].group == group))
  1624. && race > 0
  1625. ) {
  1626. drop[i].race |= race;
  1627. if(drop[i].rate > 0 && rate > 0)
  1628. { //Both are absolute rates.
  1629. if (drop[i].rate < rate)
  1630. drop[i].rate = rate;
  1631. } else
  1632. if(drop[i].rate < 0 && rate < 0) {
  1633. //Both are relative rates.
  1634. if (drop[i].rate > rate)
  1635. drop[i].rate = rate;
  1636. } else if (rate < 0) //Give preference to relative rate.
  1637. drop[i].rate = rate;
  1638. return 1;
  1639. }
  1640. }
  1641. if(i == max) {
  1642. ShowWarning("pc_bonus: Reached max (%d) number of added drops per character!\n", max);
  1643. return 0;
  1644. }
  1645. drop[i].id = id;
  1646. drop[i].group = group;
  1647. drop[i].race |= race;
  1648. drop[i].rate = rate;
  1649. return 1;
  1650. }
  1651. int pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned short pos,bool onskill)
  1652. {
  1653. int i;
  1654. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1655. if( i == max )
  1656. {
  1657. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1658. return 0;
  1659. }
  1660. if( !onskill )
  1661. {
  1662. if( !(flag&BF_RANGEMASK) )
  1663. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1664. if( !(flag&BF_WEAPONMASK) )
  1665. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1666. if( !(flag&BF_SKILLMASK) )
  1667. {
  1668. if( flag&(BF_MAGIC|BF_MISC) )
  1669. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1670. if( flag&BF_WEAPON )
  1671. flag|=BF_NORMAL|BF_SKILL;
  1672. }
  1673. }
  1674. bonus[i].rate = rate;
  1675. bonus[i].duration = dur;
  1676. bonus[i].active = INVALID_TIMER;
  1677. bonus[i].atk_type = flag;
  1678. bonus[i].pos = pos;
  1679. bonus[i].bonus_script = aStrdup(script);
  1680. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1681. return 1;
  1682. }
  1683. int pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1684. {
  1685. int i;
  1686. nullpo_ret(sd);
  1687. for( i = 0; i < max; i++ )
  1688. {
  1689. if( autobonus[i].active != INVALID_TIMER )
  1690. {
  1691. if( restore && sd->state.autobonus&autobonus[i].pos )
  1692. {
  1693. if( autobonus[i].bonus_script )
  1694. {
  1695. int j;
  1696. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus[i].pos );
  1697. if( j < EQI_MAX-1 )
  1698. script_run_autobonus(autobonus[i].bonus_script,sd->bl.id,sd->equip_index[j]);
  1699. }
  1700. continue;
  1701. }
  1702. else
  1703. { // Logout / Unequipped an item with an activated bonus
  1704. delete_timer(autobonus[i].active,pc_endautobonus);
  1705. autobonus[i].active = INVALID_TIMER;
  1706. }
  1707. }
  1708. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1709. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1710. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1711. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1712. autobonus[i].active = INVALID_TIMER;
  1713. }
  1714. return 0;
  1715. }
  1716. int pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1717. {
  1718. nullpo_ret(sd);
  1719. nullpo_ret(autobonus);
  1720. if( autobonus->other_script )
  1721. {
  1722. int j;
  1723. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus->pos );
  1724. if( j < EQI_MAX-1 )
  1725. script_run_autobonus(autobonus->other_script,sd->bl.id,sd->equip_index[j]);
  1726. }
  1727. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1728. sd->state.autobonus |= autobonus->pos;
  1729. status_calc_pc(sd,0);
  1730. return 0;
  1731. }
  1732. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1733. {
  1734. struct map_session_data *sd = map_id2sd(id);
  1735. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1736. nullpo_ret(sd);
  1737. nullpo_ret(autobonus);
  1738. autobonus->active = INVALID_TIMER;
  1739. sd->state.autobonus &= ~autobonus->pos;
  1740. status_calc_pc(sd,0);
  1741. return 0;
  1742. }
  1743. int pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1744. {
  1745. int i;
  1746. struct weapon_data* wd;
  1747. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  1748. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  1749. if (i == MAX_PC_BONUS)
  1750. {
  1751. ShowWarning("pc_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1752. return 0;
  1753. }
  1754. if (!(flag&BF_RANGEMASK))
  1755. flag |= BF_SHORT|BF_LONG;
  1756. if (!(flag&BF_WEAPONMASK))
  1757. flag |= BF_WEAPON;
  1758. if (!(flag&BF_SKILLMASK))
  1759. {
  1760. if (flag&(BF_MAGIC|BF_MISC))
  1761. flag |= BF_SKILL;
  1762. if (flag&BF_WEAPON)
  1763. flag |= BF_NORMAL|BF_SKILL;
  1764. }
  1765. wd->addele2[i].ele = ele;
  1766. wd->addele2[i].rate = rate;
  1767. wd->addele2[i].flag = flag;
  1768. return 0;
  1769. }
  1770. int pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1771. {
  1772. int i;
  1773. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  1774. if (i == MAX_PC_BONUS)
  1775. {
  1776. ShowWarning("pc_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1777. return 0;
  1778. }
  1779. if (!(flag&BF_RANGEMASK))
  1780. flag |= BF_SHORT|BF_LONG;
  1781. if (!(flag&BF_WEAPONMASK))
  1782. flag |= BF_WEAPON;
  1783. if (!(flag&BF_SKILLMASK))
  1784. {
  1785. if (flag&(BF_MAGIC|BF_MISC))
  1786. flag |= BF_SKILL;
  1787. if (flag&BF_WEAPON)
  1788. flag |= BF_NORMAL|BF_SKILL;
  1789. }
  1790. sd->subele2[i].ele = ele;
  1791. sd->subele2[i].rate = rate;
  1792. sd->subele2[i].flag = flag;
  1793. return 0;
  1794. }
  1795. /*==========================================
  1796. * Add a bonus(type) to player sd
  1797. *------------------------------------------*/
  1798. int pc_bonus(struct map_session_data *sd,int type,int val)
  1799. {
  1800. struct status_data *status;
  1801. int bonus;
  1802. nullpo_ret(sd);
  1803. status = &sd->base_status;
  1804. switch(type){
  1805. case SP_STR:
  1806. case SP_AGI:
  1807. case SP_VIT:
  1808. case SP_INT:
  1809. case SP_DEX:
  1810. case SP_LUK:
  1811. if(sd->state.lr_flag != 2)
  1812. sd->param_bonus[type-SP_STR]+=val;
  1813. break;
  1814. case SP_ATK1:
  1815. if(!sd->state.lr_flag) {
  1816. bonus = status->rhw.atk + val;
  1817. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1818. }
  1819. else if(sd->state.lr_flag == 1) {
  1820. bonus = status->lhw.atk + val;
  1821. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1822. }
  1823. break;
  1824. case SP_ATK2:
  1825. if(!sd->state.lr_flag) {
  1826. bonus = status->rhw.atk2 + val;
  1827. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1828. }
  1829. else if(sd->state.lr_flag == 1) {
  1830. bonus = status->lhw.atk2 + val;
  1831. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1832. }
  1833. break;
  1834. case SP_BASE_ATK:
  1835. if(sd->state.lr_flag != 2) {
  1836. #ifdef RENEWAL
  1837. sd->bonus.eatk += val;
  1838. #else
  1839. bonus = status->batk + val;
  1840. status->batk = cap_value(bonus, 0, USHRT_MAX);
  1841. #endif
  1842. }
  1843. break;
  1844. case SP_DEF1:
  1845. if(sd->state.lr_flag != 2) {
  1846. bonus = status->def + val;
  1847. #ifdef RENEWAL
  1848. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1849. #else
  1850. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1851. #endif
  1852. }
  1853. break;
  1854. case SP_DEF2:
  1855. if(sd->state.lr_flag != 2) {
  1856. bonus = status->def2 + val;
  1857. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1858. }
  1859. break;
  1860. case SP_MDEF1:
  1861. if(sd->state.lr_flag != 2) {
  1862. bonus = status->mdef + val;
  1863. #ifdef RENEWAL
  1864. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1865. #else
  1866. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1867. #endif
  1868. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  1869. sd->bonus.shieldmdef += bonus;
  1870. }
  1871. }
  1872. break;
  1873. case SP_MDEF2:
  1874. if(sd->state.lr_flag != 2) {
  1875. bonus = status->mdef2 + val;
  1876. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1877. }
  1878. break;
  1879. case SP_HIT:
  1880. if(sd->state.lr_flag != 2) {
  1881. bonus = status->hit + val;
  1882. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1883. } else
  1884. sd->bonus.arrow_hit+=val;
  1885. break;
  1886. case SP_FLEE1:
  1887. if(sd->state.lr_flag != 2) {
  1888. bonus = status->flee + val;
  1889. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1890. }
  1891. break;
  1892. case SP_FLEE2:
  1893. if(sd->state.lr_flag != 2) {
  1894. bonus = status->flee2 + val*10;
  1895. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1896. }
  1897. break;
  1898. case SP_CRITICAL:
  1899. if(sd->state.lr_flag != 2) {
  1900. bonus = status->cri + val*10;
  1901. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1902. } else
  1903. sd->bonus.arrow_cri += val*10;
  1904. break;
  1905. case SP_ATKELE:
  1906. if(val >= ELE_MAX) {
  1907. ShowError("pc_bonus: SP_ATKELE: Invalid element %d\n", val);
  1908. break;
  1909. }
  1910. switch (sd->state.lr_flag)
  1911. {
  1912. case 2:
  1913. switch (sd->status.weapon) {
  1914. case W_BOW:
  1915. case W_REVOLVER:
  1916. case W_RIFLE:
  1917. case W_GATLING:
  1918. case W_SHOTGUN:
  1919. case W_GRENADE:
  1920. //Become weapon element.
  1921. status->rhw.ele=val;
  1922. break;
  1923. default: //Become arrow element.
  1924. sd->bonus.arrow_ele=val;
  1925. break;
  1926. }
  1927. break;
  1928. case 1:
  1929. status->lhw.ele=val;
  1930. break;
  1931. default:
  1932. status->rhw.ele=val;
  1933. break;
  1934. }
  1935. break;
  1936. case SP_DEFELE:
  1937. if(val >= ELE_MAX) {
  1938. ShowError("pc_bonus: SP_DEFELE: Invalid element %d\n", val);
  1939. break;
  1940. }
  1941. if(sd->state.lr_flag != 2)
  1942. status->def_ele=val;
  1943. break;
  1944. case SP_MAXHP:
  1945. if(sd->state.lr_flag == 2)
  1946. break;
  1947. val += (int)status->max_hp;
  1948. //Negative bonuses will underflow, this will be handled in status_calc_pc through casting
  1949. //If this is called outside of status_calc_pc, you'd better pray they do not underflow and end with UINT_MAX max_hp.
  1950. status->max_hp = (unsigned int)val;
  1951. break;
  1952. case SP_MAXSP:
  1953. if(sd->state.lr_flag == 2)
  1954. break;
  1955. val += (int)status->max_sp;
  1956. status->max_sp = (unsigned int)val;
  1957. break;
  1958. #ifndef RENEWAL_CAST
  1959. case SP_VARCASTRATE:
  1960. #endif
  1961. case SP_CASTRATE:
  1962. if(sd->state.lr_flag != 2)
  1963. sd->castrate+=val;
  1964. break;
  1965. case SP_MAXHPRATE:
  1966. if(sd->state.lr_flag != 2)
  1967. sd->hprate+=val;
  1968. break;
  1969. case SP_MAXSPRATE:
  1970. if(sd->state.lr_flag != 2)
  1971. sd->sprate+=val;
  1972. break;
  1973. case SP_SPRATE:
  1974. if(sd->state.lr_flag != 2)
  1975. sd->dsprate+=val;
  1976. break;
  1977. case SP_ATTACKRANGE:
  1978. switch (sd->state.lr_flag) {
  1979. case 2:
  1980. switch (sd->status.weapon) {
  1981. case W_BOW:
  1982. case W_REVOLVER:
  1983. case W_RIFLE:
  1984. case W_GATLING:
  1985. case W_SHOTGUN:
  1986. case W_GRENADE:
  1987. status->rhw.range += val;
  1988. }
  1989. break;
  1990. case 1:
  1991. status->lhw.range += val;
  1992. break;
  1993. default:
  1994. status->rhw.range += val;
  1995. break;
  1996. }
  1997. break;
  1998. case SP_SPEED_RATE: //Non stackable increase
  1999. if(sd->state.lr_flag != 2)
  2000. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2001. break;
  2002. case SP_SPEED_ADDRATE: //Stackable increase
  2003. if(sd->state.lr_flag != 2)
  2004. sd->bonus.speed_add_rate -= val;
  2005. break;
  2006. case SP_ASPD: //Raw increase
  2007. if(sd->state.lr_flag != 2)
  2008. sd->bonus.aspd_add -= 10*val;
  2009. break;
  2010. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2011. if(sd->state.lr_flag != 2)
  2012. #ifndef RENEWAL_ASPD
  2013. status->aspd_rate -= 10*val;
  2014. #else
  2015. status->aspd_rate2 += val;
  2016. #endif
  2017. break;
  2018. case SP_HP_RECOV_RATE:
  2019. if(sd->state.lr_flag != 2)
  2020. sd->hprecov_rate += val;
  2021. break;
  2022. case SP_SP_RECOV_RATE:
  2023. if(sd->state.lr_flag != 2)
  2024. sd->sprecov_rate += val;
  2025. break;
  2026. case SP_CRITICAL_DEF:
  2027. if(sd->state.lr_flag != 2)
  2028. sd->bonus.critical_def += val;
  2029. break;
  2030. case SP_NEAR_ATK_DEF:
  2031. if(sd->state.lr_flag != 2)
  2032. sd->bonus.near_attack_def_rate += val;
  2033. break;
  2034. case SP_LONG_ATK_DEF:
  2035. if(sd->state.lr_flag != 2)
  2036. sd->bonus.long_attack_def_rate += val;
  2037. break;
  2038. case SP_DOUBLE_RATE:
  2039. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2040. sd->bonus.double_rate = val;
  2041. break;
  2042. case SP_DOUBLE_ADD_RATE:
  2043. if(sd->state.lr_flag == 0)
  2044. sd->bonus.double_add_rate += val;
  2045. break;
  2046. case SP_MATK_RATE:
  2047. if(sd->state.lr_flag != 2)
  2048. sd->matk_rate += val;
  2049. break;
  2050. case SP_IGNORE_DEF_ELE:
  2051. if(val >= ELE_MAX) {
  2052. ShowError("pc_bonus: SP_IGNORE_DEF_ELE: Invalid element %d\n", val);
  2053. break;
  2054. }
  2055. if(!sd->state.lr_flag)
  2056. sd->right_weapon.ignore_def_ele |= 1<<val;
  2057. else if(sd->state.lr_flag == 1)
  2058. sd->left_weapon.ignore_def_ele |= 1<<val;
  2059. break;
  2060. case SP_IGNORE_DEF_RACE:
  2061. if(!sd->state.lr_flag)
  2062. sd->right_weapon.ignore_def_race |= 1<<val;
  2063. else if(sd->state.lr_flag == 1)
  2064. sd->left_weapon.ignore_def_race |= 1<<val;
  2065. break;
  2066. case SP_ATK_RATE:
  2067. if(sd->state.lr_flag != 2)
  2068. sd->bonus.atk_rate += val;
  2069. break;
  2070. case SP_MAGIC_ATK_DEF:
  2071. if(sd->state.lr_flag != 2)
  2072. sd->bonus.magic_def_rate += val;
  2073. break;
  2074. case SP_MISC_ATK_DEF:
  2075. if(sd->state.lr_flag != 2)
  2076. sd->bonus.misc_def_rate += val;
  2077. break;
  2078. case SP_IGNORE_MDEF_RATE:
  2079. if(sd->state.lr_flag != 2) {
  2080. sd->ignore_mdef[RC_NONBOSS] += val;
  2081. sd->ignore_mdef[RC_BOSS] += val;
  2082. }
  2083. break;
  2084. case SP_IGNORE_MDEF_ELE:
  2085. if(val >= ELE_MAX) {
  2086. ShowError("pc_bonus: SP_IGNORE_MDEF_ELE: Invalid element %d\n", val);
  2087. break;
  2088. }
  2089. if(sd->state.lr_flag != 2)
  2090. sd->bonus.ignore_mdef_ele |= 1<<val;
  2091. break;
  2092. case SP_IGNORE_MDEF_RACE:
  2093. if(sd->state.lr_flag != 2)
  2094. sd->bonus.ignore_mdef_race |= 1<<val;
  2095. break;
  2096. case SP_PERFECT_HIT_RATE:
  2097. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2098. sd->bonus.perfect_hit = val;
  2099. break;
  2100. case SP_PERFECT_HIT_ADD_RATE:
  2101. if(sd->state.lr_flag != 2)
  2102. sd->bonus.perfect_hit_add += val;
  2103. break;
  2104. case SP_CRITICAL_RATE:
  2105. if(sd->state.lr_flag != 2)
  2106. sd->critical_rate+=val;
  2107. break;
  2108. case SP_DEF_RATIO_ATK_ELE:
  2109. if(val >= ELE_MAX) {
  2110. ShowError("pc_bonus: SP_DEF_RATIO_ATK_ELE: Invalid element %d\n", val);
  2111. break;
  2112. }
  2113. if(!sd->state.lr_flag)
  2114. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2115. else if(sd->state.lr_flag == 1)
  2116. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2117. break;
  2118. case SP_DEF_RATIO_ATK_RACE:
  2119. if(val >= RC_MAX) {
  2120. ShowError("pc_bonus: SP_DEF_RATIO_ATK_RACE: Invalid race %d\n", val);
  2121. break;
  2122. }
  2123. if(!sd->state.lr_flag)
  2124. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2125. else if(sd->state.lr_flag == 1)
  2126. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2127. break;
  2128. case SP_HIT_RATE:
  2129. if(sd->state.lr_flag != 2)
  2130. sd->hit_rate += val;
  2131. break;
  2132. case SP_FLEE_RATE:
  2133. if(sd->state.lr_flag != 2)
  2134. sd->flee_rate += val;
  2135. break;
  2136. case SP_FLEE2_RATE:
  2137. if(sd->state.lr_flag != 2)
  2138. sd->flee2_rate += val;
  2139. break;
  2140. case SP_DEF_RATE:
  2141. if(sd->state.lr_flag != 2)
  2142. sd->def_rate += val;
  2143. break;
  2144. case SP_DEF2_RATE:
  2145. if(sd->state.lr_flag != 2)
  2146. sd->def2_rate += val;
  2147. break;
  2148. case SP_MDEF_RATE:
  2149. if(sd->state.lr_flag != 2)
  2150. sd->mdef_rate += val;
  2151. break;
  2152. case SP_MDEF2_RATE:
  2153. if(sd->state.lr_flag != 2)
  2154. sd->mdef2_rate += val;
  2155. break;
  2156. case SP_RESTART_FULL_RECOVER:
  2157. if(sd->state.lr_flag != 2)
  2158. sd->special_state.restart_full_recover = 1;
  2159. break;
  2160. case SP_NO_CASTCANCEL:
  2161. if(sd->state.lr_flag != 2)
  2162. sd->special_state.no_castcancel = 1;
  2163. break;
  2164. case SP_NO_CASTCANCEL2:
  2165. if(sd->state.lr_flag != 2)
  2166. sd->special_state.no_castcancel2 = 1;
  2167. break;
  2168. case SP_NO_SIZEFIX:
  2169. if(sd->state.lr_flag != 2)
  2170. sd->special_state.no_sizefix = 1;
  2171. break;
  2172. case SP_NO_MAGIC_DAMAGE:
  2173. if(sd->state.lr_flag == 2)
  2174. break;
  2175. val+= sd->special_state.no_magic_damage;
  2176. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2177. break;
  2178. case SP_NO_WEAPON_DAMAGE:
  2179. if(sd->state.lr_flag == 2)
  2180. break;
  2181. val+= sd->special_state.no_weapon_damage;
  2182. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2183. break;
  2184. case SP_NO_MISC_DAMAGE:
  2185. if(sd->state.lr_flag == 2)
  2186. break;
  2187. val+= sd->special_state.no_misc_damage;
  2188. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2189. break;
  2190. case SP_NO_GEMSTONE:
  2191. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2192. sd->special_state.no_gemstone = 1;
  2193. break;
  2194. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2195. if(sd->state.lr_flag != 2) {
  2196. sd->special_state.intravision = 1;
  2197. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2198. }
  2199. break;
  2200. case SP_NO_KNOCKBACK:
  2201. if(sd->state.lr_flag != 2)
  2202. sd->special_state.no_knockback = 1;
  2203. break;
  2204. case SP_SPLASH_RANGE:
  2205. if(sd->bonus.splash_range < val)
  2206. sd->bonus.splash_range = val;
  2207. break;
  2208. case SP_SPLASH_ADD_RANGE:
  2209. sd->bonus.splash_add_range += val;
  2210. break;
  2211. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2212. if(sd->state.lr_flag != 2)
  2213. sd->bonus.short_weapon_damage_return += val;
  2214. break;
  2215. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2216. if(sd->state.lr_flag != 2)
  2217. sd->bonus.long_weapon_damage_return += val;
  2218. break;
  2219. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2220. if(sd->state.lr_flag != 2)
  2221. sd->bonus.magic_damage_return += val;
  2222. break;
  2223. case SP_ALL_STATS: // [Valaris]
  2224. if(sd->state.lr_flag!=2) {
  2225. sd->param_bonus[SP_STR-SP_STR]+=val;
  2226. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2227. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2228. sd->param_bonus[SP_INT-SP_STR]+=val;
  2229. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2230. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2231. }
  2232. break;
  2233. case SP_AGI_VIT: // [Valaris]
  2234. if(sd->state.lr_flag!=2) {
  2235. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2236. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2237. }
  2238. break;
  2239. case SP_AGI_DEX_STR: // [Valaris]
  2240. if(sd->state.lr_flag!=2) {
  2241. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2242. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2243. sd->param_bonus[SP_STR-SP_STR]+=val;
  2244. }
  2245. break;
  2246. case SP_PERFECT_HIDE: // [Valaris]
  2247. if(sd->state.lr_flag!=2)
  2248. sd->special_state.perfect_hiding=1;
  2249. break;
  2250. case SP_UNBREAKABLE:
  2251. if(sd->state.lr_flag!=2)
  2252. sd->bonus.unbreakable += val;
  2253. break;
  2254. case SP_UNBREAKABLE_WEAPON:
  2255. if(sd->state.lr_flag != 2)
  2256. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2257. break;
  2258. case SP_UNBREAKABLE_ARMOR:
  2259. if(sd->state.lr_flag != 2)
  2260. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2261. break;
  2262. case SP_UNBREAKABLE_HELM:
  2263. if(sd->state.lr_flag != 2)
  2264. sd->bonus.unbreakable_equip |= EQP_HELM;
  2265. break;
  2266. case SP_UNBREAKABLE_SHIELD:
  2267. if(sd->state.lr_flag != 2)
  2268. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2269. break;
  2270. case SP_UNBREAKABLE_GARMENT:
  2271. if(sd->state.lr_flag != 2)
  2272. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2273. break;
  2274. case SP_UNBREAKABLE_SHOES:
  2275. if(sd->state.lr_flag != 2)
  2276. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2277. break;
  2278. case SP_CLASSCHANGE: // [Valaris]
  2279. if(sd->state.lr_flag !=2)
  2280. sd->bonus.classchange=val;
  2281. break;
  2282. case SP_LONG_ATK_RATE:
  2283. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2284. sd->bonus.long_attack_atk_rate+=val;
  2285. break;
  2286. case SP_BREAK_WEAPON_RATE:
  2287. if(sd->state.lr_flag != 2)
  2288. sd->bonus.break_weapon_rate+=val;
  2289. break;
  2290. case SP_BREAK_ARMOR_RATE:
  2291. if(sd->state.lr_flag != 2)
  2292. sd->bonus.break_armor_rate+=val;
  2293. break;
  2294. case SP_ADD_STEAL_RATE:
  2295. if(sd->state.lr_flag != 2)
  2296. sd->bonus.add_steal_rate+=val;
  2297. break;
  2298. case SP_DELAYRATE:
  2299. if(sd->state.lr_flag != 2)
  2300. sd->delayrate+=val;
  2301. break;
  2302. case SP_CRIT_ATK_RATE:
  2303. if(sd->state.lr_flag != 2)
  2304. sd->bonus.crit_atk_rate += val;
  2305. break;
  2306. case SP_NO_REGEN:
  2307. if(sd->state.lr_flag != 2)
  2308. sd->regen.state.block|=val;
  2309. break;
  2310. case SP_UNSTRIPABLE_WEAPON:
  2311. if(sd->state.lr_flag != 2)
  2312. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2313. break;
  2314. case SP_UNSTRIPABLE:
  2315. case SP_UNSTRIPABLE_ARMOR:
  2316. if(sd->state.lr_flag != 2)
  2317. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2318. break;
  2319. case SP_UNSTRIPABLE_HELM:
  2320. if(sd->state.lr_flag != 2)
  2321. sd->bonus.unstripable_equip |= EQP_HELM;
  2322. break;
  2323. case SP_UNSTRIPABLE_SHIELD:
  2324. if(sd->state.lr_flag != 2)
  2325. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2326. break;
  2327. case SP_HP_DRAIN_VALUE:
  2328. if(!sd->state.lr_flag) {
  2329. sd->right_weapon.hp_drain[RC_NONBOSS].value += val;
  2330. sd->right_weapon.hp_drain[RC_BOSS].value += val;
  2331. }
  2332. else if(sd->state.lr_flag == 1) {
  2333. sd->left_weapon.hp_drain[RC_NONBOSS].value += val;
  2334. sd->left_weapon.hp_drain[RC_BOSS].value += val;
  2335. }
  2336. break;
  2337. case SP_SP_DRAIN_VALUE:
  2338. if(!sd->state.lr_flag) {
  2339. sd->right_weapon.sp_drain[RC_NONBOSS].value += val;
  2340. sd->right_weapon.sp_drain[RC_BOSS].value += val;
  2341. }
  2342. else if(sd->state.lr_flag == 1) {
  2343. sd->left_weapon.sp_drain[RC_NONBOSS].value += val;
  2344. sd->left_weapon.sp_drain[RC_BOSS].value += val;
  2345. }
  2346. break;
  2347. case SP_SP_GAIN_VALUE:
  2348. if(!sd->state.lr_flag)
  2349. sd->bonus.sp_gain_value += val;
  2350. break;
  2351. case SP_HP_GAIN_VALUE:
  2352. if(!sd->state.lr_flag)
  2353. sd->bonus.hp_gain_value += val;
  2354. break;
  2355. case SP_MAGIC_SP_GAIN_VALUE:
  2356. if(!sd->state.lr_flag)
  2357. sd->bonus.magic_sp_gain_value += val;
  2358. break;
  2359. case SP_MAGIC_HP_GAIN_VALUE:
  2360. if(!sd->state.lr_flag)
  2361. sd->bonus.magic_hp_gain_value += val;
  2362. break;
  2363. case SP_ADD_HEAL_RATE:
  2364. if(sd->state.lr_flag != 2)
  2365. sd->bonus.add_heal_rate += val;
  2366. break;
  2367. case SP_ADD_HEAL2_RATE:
  2368. if(sd->state.lr_flag != 2)
  2369. sd->bonus.add_heal2_rate += val;
  2370. break;
  2371. case SP_ADD_ITEM_HEAL_RATE:
  2372. if(sd->state.lr_flag != 2)
  2373. sd->bonus.itemhealrate2 += val;
  2374. break;
  2375. case SP_EMATK:
  2376. if(sd->state.lr_flag != 2)
  2377. sd->bonus.ematk += val;
  2378. break;
  2379. case SP_FIXCASTRATE:
  2380. if(sd->state.lr_flag != 2)
  2381. sd->bonus.fixcastrate -= val;
  2382. break;
  2383. case SP_ADD_FIXEDCAST:
  2384. if(sd->state.lr_flag != 2)
  2385. sd->bonus.add_fixcast += val;
  2386. break;
  2387. #ifdef RENEWAL_CAST
  2388. case SP_VARCASTRATE:
  2389. if(sd->state.lr_flag != 2)
  2390. sd->bonus.varcastrate -= val;
  2391. break;
  2392. case SP_ADD_VARIABLECAST:
  2393. if(sd->state.lr_flag != 2)
  2394. sd->bonus.add_varcast += val;
  2395. break;
  2396. #endif
  2397. default:
  2398. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2399. break;
  2400. }
  2401. return 0;
  2402. }
  2403. /*==========================================
  2404. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2405. *------------------------------------------*/
  2406. int pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2407. {
  2408. int i;
  2409. nullpo_ret(sd);
  2410. switch(type){
  2411. case SP_ADDELE:
  2412. if(type2 >= ELE_MAX) {
  2413. ShowError("pc_bonus2: SP_ADDELE: Invalid element %d\n", type2);
  2414. break;
  2415. }
  2416. if(!sd->state.lr_flag)
  2417. sd->right_weapon.addele[type2]+=val;
  2418. else if(sd->state.lr_flag == 1)
  2419. sd->left_weapon.addele[type2]+=val;
  2420. else if(sd->state.lr_flag == 2)
  2421. sd->arrow_addele[type2]+=val;
  2422. break;
  2423. case SP_ADDRACE:
  2424. if(!sd->state.lr_flag)
  2425. sd->right_weapon.addrace[type2]+=val;
  2426. else if(sd->state.lr_flag == 1)
  2427. sd->left_weapon.addrace[type2]+=val;
  2428. else if(sd->state.lr_flag == 2)
  2429. sd->arrow_addrace[type2]+=val;
  2430. break;
  2431. case SP_ADDSIZE:
  2432. if(!sd->state.lr_flag)
  2433. sd->right_weapon.addsize[type2]+=val;
  2434. else if(sd->state.lr_flag == 1)
  2435. sd->left_weapon.addsize[type2]+=val;
  2436. else if(sd->state.lr_flag == 2)
  2437. sd->arrow_addsize[type2]+=val;
  2438. break;
  2439. case SP_SUBELE:
  2440. if(type2 >= ELE_MAX) {
  2441. ShowError("pc_bonus2: SP_SUBELE: Invalid element %d\n", type2);
  2442. break;
  2443. }
  2444. if(sd->state.lr_flag != 2)
  2445. sd->subele[type2]+=val;
  2446. break;
  2447. case SP_SUBRACE:
  2448. if(sd->state.lr_flag != 2)
  2449. sd->subrace[type2]+=val;
  2450. break;
  2451. case SP_ADDEFF:
  2452. if (type2 > SC_MAX) {
  2453. ShowWarning("pc_bonus2 (Add Effect): %d is not supported.\n", type2);
  2454. break;
  2455. }
  2456. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2457. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0);
  2458. break;
  2459. case SP_ADDEFF2:
  2460. if (type2 > SC_MAX) {
  2461. ShowWarning("pc_bonus2 (Add Effect2): %d is not supported.\n", type2);
  2462. break;
  2463. }
  2464. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2465. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF);
  2466. break;
  2467. case SP_RESEFF:
  2468. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2469. ShowWarning("pc_bonus2 (Resist Effect): %d is not supported.\n", type2);
  2470. break;
  2471. }
  2472. if(sd->state.lr_flag == 2)
  2473. break;
  2474. i = sd->reseff[type2-SC_COMMON_MIN]+val;
  2475. sd->reseff[type2-SC_COMMON_MIN]= cap_value(i, 0, 10000);
  2476. break;
  2477. case SP_MAGIC_ADDELE:
  2478. if(type2 >= ELE_MAX) {
  2479. ShowError("pc_bonus2: SP_MAGIC_ADDELE: Invalid element %d\n", type2);
  2480. break;
  2481. }
  2482. if(sd->state.lr_flag != 2)
  2483. sd->magic_addele[type2]+=val;
  2484. break;
  2485. case SP_MAGIC_ADDRACE:
  2486. if(sd->state.lr_flag != 2)
  2487. sd->magic_addrace[type2]+=val;
  2488. break;
  2489. case SP_MAGIC_ADDSIZE:
  2490. if(sd->state.lr_flag != 2)
  2491. sd->magic_addsize[type2]+=val;
  2492. break;
  2493. case SP_MAGIC_ATK_ELE:
  2494. if(sd->state.lr_flag != 2)
  2495. sd->magic_atk_ele[type2]+=val;
  2496. break;
  2497. case SP_ADD_DAMAGE_CLASS:
  2498. switch (sd->state.lr_flag) {
  2499. case 0: //Right hand
  2500. 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);
  2501. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2502. {
  2503. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2504. break;
  2505. }
  2506. sd->right_weapon.add_dmg[i].class_ = type2;
  2507. sd->right_weapon.add_dmg[i].rate += val;
  2508. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2509. 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]));
  2510. break;
  2511. case 1: //Left hand
  2512. 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);
  2513. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2514. {
  2515. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2516. break;
  2517. }
  2518. sd->left_weapon.add_dmg[i].class_ = type2;
  2519. sd->left_weapon.add_dmg[i].rate += val;
  2520. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2521. 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]));
  2522. break;
  2523. }
  2524. break;
  2525. case SP_ADD_MAGIC_DAMAGE_CLASS:
  2526. if(sd->state.lr_flag == 2)
  2527. break;
  2528. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2529. if (i == ARRAYLENGTH(sd->add_mdmg))
  2530. {
  2531. ShowWarning("pc_bonus2: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2532. break;
  2533. }
  2534. sd->add_mdmg[i].class_ = type2;
  2535. sd->add_mdmg[i].rate += val;
  2536. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2537. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2538. break;
  2539. case SP_ADD_DEF_CLASS:
  2540. if(sd->state.lr_flag == 2)
  2541. break;
  2542. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2543. if (i == ARRAYLENGTH(sd->add_def))
  2544. {
  2545. ShowWarning("pc_bonus2: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2546. break;
  2547. }
  2548. sd->add_def[i].class_ = type2;
  2549. sd->add_def[i].rate += val;
  2550. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2551. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2552. break;
  2553. case SP_ADD_MDEF_CLASS:
  2554. if(sd->state.lr_flag == 2)
  2555. break;
  2556. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2557. if (i == ARRAYLENGTH(sd->add_mdef))
  2558. {
  2559. ShowWarning("pc_bonus2: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2560. break;
  2561. }
  2562. sd->add_mdef[i].class_ = type2;
  2563. sd->add_mdef[i].rate += val;
  2564. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2565. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2566. break;
  2567. case SP_HP_DRAIN_RATE:
  2568. if(!sd->state.lr_flag) {
  2569. sd->right_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2570. sd->right_weapon.hp_drain[RC_NONBOSS].per += val;
  2571. sd->right_weapon.hp_drain[RC_BOSS].rate += type2;
  2572. sd->right_weapon.hp_drain[RC_BOSS].per += val;
  2573. }
  2574. else if(sd->state.lr_flag == 1) {
  2575. sd->left_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2576. sd->left_weapon.hp_drain[RC_NONBOSS].per += val;
  2577. sd->left_weapon.hp_drain[RC_BOSS].rate += type2;
  2578. sd->left_weapon.hp_drain[RC_BOSS].per += val;
  2579. }
  2580. break;
  2581. case SP_HP_DRAIN_VALUE:
  2582. if(!sd->state.lr_flag) {
  2583. sd->right_weapon.hp_drain[RC_NONBOSS].value += type2;
  2584. sd->right_weapon.hp_drain[RC_NONBOSS].type = val;
  2585. sd->right_weapon.hp_drain[RC_BOSS].value += type2;
  2586. sd->right_weapon.hp_drain[RC_BOSS].type = val;
  2587. }
  2588. else if(sd->state.lr_flag == 1) {
  2589. sd->left_weapon.hp_drain[RC_NONBOSS].value += type2;
  2590. sd->left_weapon.hp_drain[RC_NONBOSS].type = val;
  2591. sd->left_weapon.hp_drain[RC_BOSS].value += type2;
  2592. sd->left_weapon.hp_drain[RC_BOSS].type = val;
  2593. }
  2594. break;
  2595. case SP_SP_DRAIN_RATE:
  2596. if(!sd->state.lr_flag) {
  2597. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2598. sd->right_weapon.sp_drain[RC_NONBOSS].per += val;
  2599. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2600. sd->right_weapon.sp_drain[RC_BOSS].per += val;
  2601. }
  2602. else if(sd->state.lr_flag == 1) {
  2603. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2604. sd->left_weapon.sp_drain[RC_NONBOSS].per += val;
  2605. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2606. sd->left_weapon.sp_drain[RC_BOSS].per += val;
  2607. }
  2608. break;
  2609. case SP_SP_DRAIN_VALUE:
  2610. if(!sd->state.lr_flag) {
  2611. sd->right_weapon.sp_drain[RC_NONBOSS].value += type2;
  2612. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2613. sd->right_weapon.sp_drain[RC_BOSS].value += type2;
  2614. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2615. }
  2616. else if(sd->state.lr_flag == 1) {
  2617. sd->left_weapon.sp_drain[RC_NONBOSS].value += type2;
  2618. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2619. sd->left_weapon.sp_drain[RC_BOSS].value += type2;
  2620. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2621. }
  2622. break;
  2623. case SP_SP_VANISH_RATE:
  2624. if(sd->state.lr_flag != 2) {
  2625. sd->bonus.sp_vanish_rate += type2;
  2626. sd->bonus.sp_vanish_per += val;
  2627. }
  2628. break;
  2629. case SP_HP_VANISH_RATE:
  2630. if(sd->state.lr_flag != 2) {
  2631. sd->bonus.hp_vanish_rate += type2;
  2632. sd->bonus.hp_vanish_per += val;
  2633. }
  2634. break;
  2635. case SP_GET_ZENY_NUM:
  2636. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2637. sd->bonus.get_zeny_rate = val;
  2638. sd->bonus.get_zeny_num = type2;
  2639. }
  2640. break;
  2641. case SP_ADD_GET_ZENY_NUM:
  2642. if(sd->state.lr_flag != 2) {
  2643. sd->bonus.get_zeny_rate += val;
  2644. sd->bonus.get_zeny_num += type2;
  2645. }
  2646. break;
  2647. case SP_WEAPON_COMA_ELE:
  2648. if(type2 >= ELE_MAX) {
  2649. ShowError("pc_bonus2: SP_WEAPON_COMA_ELE: Invalid element %d\n", type2);
  2650. break;
  2651. }
  2652. if(sd->state.lr_flag == 2)
  2653. break;
  2654. sd->weapon_coma_ele[type2] += val;
  2655. sd->special_state.bonus_coma = 1;
  2656. break;
  2657. case SP_WEAPON_COMA_RACE:
  2658. if(sd->state.lr_flag == 2)
  2659. break;
  2660. sd->weapon_coma_race[type2] += val;
  2661. sd->special_state.bonus_coma = 1;
  2662. break;
  2663. case SP_WEAPON_ATK:
  2664. if(sd->state.lr_flag != 2)
  2665. sd->weapon_atk[type2]+=val;
  2666. break;
  2667. case SP_WEAPON_ATK_RATE:
  2668. if(sd->state.lr_flag != 2)
  2669. sd->weapon_atk_rate[type2]+=val;
  2670. break;
  2671. case SP_CRITICAL_ADDRACE:
  2672. if(sd->state.lr_flag != 2)
  2673. sd->critaddrace[type2] += val*10;
  2674. break;
  2675. case SP_ADDEFF_WHENHIT:
  2676. if (type2 > SC_MAX) {
  2677. ShowWarning("pc_bonus2 (Add Effect when hit): %d is not supported.\n", type2);
  2678. break;
  2679. }
  2680. if(sd->state.lr_flag != 2)
  2681. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0);
  2682. break;
  2683. case SP_SKILL_ATK:
  2684. if(sd->state.lr_flag == 2)
  2685. break;
  2686. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2687. if (i == ARRAYLENGTH(sd->skillatk))
  2688. { //Better mention this so the array length can be updated. [Skotlex]
  2689. ShowDebug("run_script: bonus2 bSkillAtk reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  2690. break;
  2691. }
  2692. if (sd->skillatk[i].id == type2)
  2693. sd->skillatk[i].val += val;
  2694. else {
  2695. sd->skillatk[i].id = type2;
  2696. sd->skillatk[i].val = val;
  2697. }
  2698. break;
  2699. case SP_SKILL_HEAL:
  2700. if(sd->state.lr_flag == 2)
  2701. break;
  2702. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2703. if (i == ARRAYLENGTH(sd->skillheal))
  2704. { // Better mention this so the array length can be updated. [Skotlex]
  2705. ShowDebug("run_script: bonus2 bSkillHeal reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  2706. break;
  2707. }
  2708. if (sd->skillheal[i].id == type2)
  2709. sd->skillheal[i].val += val;
  2710. else {
  2711. sd->skillheal[i].id = type2;
  2712. sd->skillheal[i].val = val;
  2713. }
  2714. break;
  2715. case SP_SKILL_HEAL2:
  2716. if(sd->state.lr_flag == 2)
  2717. break;
  2718. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  2719. if (i == ARRAYLENGTH(sd->skillheal2))
  2720. { // Better mention this so the array length can be updated. [Skotlex]
  2721. ShowDebug("run_script: bonus2 bSkillHeal2 reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  2722. break;
  2723. }
  2724. if (sd->skillheal2[i].id == type2)
  2725. sd->skillheal2[i].val += val;
  2726. else {
  2727. sd->skillheal2[i].id = type2;
  2728. sd->skillheal2[i].val = val;
  2729. }
  2730. break;
  2731. case SP_ADD_SKILL_BLOW:
  2732. if(sd->state.lr_flag == 2)
  2733. break;
  2734. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  2735. if (i == ARRAYLENGTH(sd->skillblown))
  2736. { //Better mention this so the array length can be updated. [Skotlex]
  2737. ShowDebug("run_script: bonus2 bSkillBlown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  2738. break;
  2739. }
  2740. if(sd->skillblown[i].id == type2)
  2741. sd->skillblown[i].val += val;
  2742. else {
  2743. sd->skillblown[i].id = type2;
  2744. sd->skillblown[i].val = val;
  2745. }
  2746. break;
  2747. #ifndef RENEWAL_CAST
  2748. case SP_VARCASTRATE:
  2749. #endif
  2750. case SP_CASTRATE:
  2751. if(sd->state.lr_flag == 2)
  2752. break;
  2753. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2754. if (i == ARRAYLENGTH(sd->skillcast))
  2755. { //Better mention this so the array length can be updated. [Skotlex]
  2756. ShowDebug("run_script: bonus2 %s reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
  2757. #ifndef RENEWAL_CAST
  2758. "bCastRate",
  2759. #else
  2760. "bVariableCastrate",
  2761. #endif
  2762. ARRAYLENGTH(sd->skillcast), type2, val);
  2763. break;
  2764. }
  2765. if(sd->skillcast[i].id == type2)
  2766. sd->skillcast[i].val += val;
  2767. else {
  2768. sd->skillcast[i].id = type2;
  2769. sd->skillcast[i].val = val;
  2770. }
  2771. break;
  2772. case SP_FIXCASTRATE:
  2773. if(sd->state.lr_flag == 2)
  2774. break;
  2775. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  2776. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  2777. {
  2778. ShowDebug("run_script: bonus2 bFixedCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  2779. break;
  2780. }
  2781. if(sd->skillfixcastrate[i].id == type2)
  2782. sd->skillfixcastrate[i].val += val;
  2783. else {
  2784. sd->skillfixcastrate[i].id = type2;
  2785. sd->skillfixcastrate[i].val = val;
  2786. }
  2787. break;
  2788. case SP_HP_LOSS_RATE:
  2789. if(sd->state.lr_flag != 2) {
  2790. sd->hp_loss.value = type2;
  2791. sd->hp_loss.rate = val;
  2792. }
  2793. break;
  2794. case SP_HP_REGEN_RATE:
  2795. if(sd->state.lr_flag != 2) {
  2796. sd->hp_regen.value = type2;
  2797. sd->hp_regen.rate = val;
  2798. }
  2799. break;
  2800. case SP_ADDRACE2:
  2801. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2802. break;
  2803. if(sd->state.lr_flag != 2)
  2804. sd->right_weapon.addrace2[type2] += val;
  2805. else
  2806. sd->left_weapon.addrace2[type2] += val;
  2807. break;
  2808. case SP_SUBSIZE:
  2809. if(sd->state.lr_flag != 2)
  2810. sd->subsize[type2]+=val;
  2811. break;
  2812. case SP_SUBRACE2:
  2813. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2814. break;
  2815. if(sd->state.lr_flag != 2)
  2816. sd->subrace2[type2]+=val;
  2817. break;
  2818. case SP_ADD_ITEM_HEAL_RATE:
  2819. if(sd->state.lr_flag == 2)
  2820. break;
  2821. if (type2 < MAX_ITEMGROUP) { //Group bonus
  2822. sd->itemgrouphealrate[type2] += val;
  2823. break;
  2824. }
  2825. //Standard item bonus.
  2826. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  2827. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  2828. ShowWarning("pc_bonus2: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  2829. break;
  2830. }
  2831. sd->itemhealrate[i].nameid = type2;
  2832. sd->itemhealrate[i].rate += val;
  2833. break;
  2834. case SP_EXP_ADDRACE:
  2835. if(sd->state.lr_flag != 2)
  2836. sd->expaddrace[type2]+=val;
  2837. break;
  2838. case SP_SP_GAIN_RACE:
  2839. if(sd->state.lr_flag != 2)
  2840. sd->sp_gain_race[type2]+=val;
  2841. break;
  2842. case SP_ADD_MONSTER_DROP_ITEM:
  2843. if (sd->state.lr_flag != 2)
  2844. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2845. break;
  2846. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2847. if (sd->state.lr_flag != 2)
  2848. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2849. break;
  2850. case SP_SP_LOSS_RATE:
  2851. if(sd->state.lr_flag != 2) {
  2852. sd->sp_loss.value = type2;
  2853. sd->sp_loss.rate = val;
  2854. }
  2855. break;
  2856. case SP_SP_REGEN_RATE:
  2857. if(sd->state.lr_flag != 2) {
  2858. sd->sp_regen.value = type2;
  2859. sd->sp_regen.rate = val;
  2860. }
  2861. break;
  2862. case SP_HP_DRAIN_VALUE_RACE:
  2863. if(!sd->state.lr_flag) {
  2864. sd->right_weapon.hp_drain[type2].value += val;
  2865. }
  2866. else if(sd->state.lr_flag == 1) {
  2867. sd->left_weapon.hp_drain[type2].value += val;
  2868. }
  2869. break;
  2870. case SP_SP_DRAIN_VALUE_RACE:
  2871. if(!sd->state.lr_flag) {
  2872. sd->right_weapon.sp_drain[type2].value += val;
  2873. }
  2874. else if(sd->state.lr_flag == 1) {
  2875. sd->left_weapon.sp_drain[type2].value += val;
  2876. }
  2877. break;
  2878. case SP_IGNORE_MDEF_RATE:
  2879. if(sd->state.lr_flag != 2)
  2880. sd->ignore_mdef[type2] += val;
  2881. break;
  2882. case SP_IGNORE_DEF_RATE:
  2883. if(sd->state.lr_flag != 2)
  2884. sd->ignore_def[type2] += val;
  2885. break;
  2886. case SP_SP_GAIN_RACE_ATTACK:
  2887. if(sd->state.lr_flag != 2)
  2888. sd->sp_gain_race_attack[type2] = cap_value(sd->sp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2889. break;
  2890. case SP_HP_GAIN_RACE_ATTACK:
  2891. if(sd->state.lr_flag != 2)
  2892. sd->hp_gain_race_attack[type2] = cap_value(sd->hp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2893. break;
  2894. case SP_SKILL_USE_SP_RATE: //bonus2 bSkillUseSPrate,n,x;
  2895. if(sd->state.lr_flag == 2)
  2896. break;
  2897. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  2898. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  2899. ShowDebug("run_script: bonus2 bSkillUseSPrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  2900. break;
  2901. }
  2902. if (sd->skillusesprate[i].id == type2)
  2903. sd->skillusesprate[i].val += val;
  2904. else {
  2905. sd->skillusesprate[i].id = type2;
  2906. sd->skillusesprate[i].val = val;
  2907. }
  2908. break;
  2909. case SP_SKILL_COOLDOWN:
  2910. if(sd->state.lr_flag == 2)
  2911. break;
  2912. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  2913. if (i == ARRAYLENGTH(sd->skillcooldown))
  2914. {
  2915. ShowDebug("run_script: bonus2 bSkillCoolDown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  2916. break;
  2917. }
  2918. if (sd->skillcooldown[i].id == type2)
  2919. sd->skillcooldown[i].val += val;
  2920. else {
  2921. sd->skillcooldown[i].id = type2;
  2922. sd->skillcooldown[i].val = val;
  2923. }
  2924. break;
  2925. case SP_SKILL_FIXEDCAST:
  2926. if(sd->state.lr_flag == 2)
  2927. break;
  2928. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  2929. if (i == ARRAYLENGTH(sd->skillfixcast))
  2930. {
  2931. ShowDebug("run_script: bonus2 bSkillFixedCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  2932. break;
  2933. }
  2934. if (sd->skillfixcast[i].id == type2)
  2935. sd->skillfixcast[i].val += val;
  2936. else {
  2937. sd->skillfixcast[i].id = type2;
  2938. sd->skillfixcast[i].val = val;
  2939. }
  2940. break;
  2941. case SP_SKILL_VARIABLECAST:
  2942. if(sd->state.lr_flag == 2)
  2943. break;
  2944. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  2945. if (i == ARRAYLENGTH(sd->skillvarcast))
  2946. {
  2947. ShowDebug("run_script: bonus2 bSkillVariableCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  2948. break;
  2949. }
  2950. if (sd->skillvarcast[i].id == type2)
  2951. sd->skillvarcast[i].val += val;
  2952. else {
  2953. sd->skillvarcast[i].id = type2;
  2954. sd->skillvarcast[i].val = val;
  2955. }
  2956. break;
  2957. #ifdef RENEWAL_CAST
  2958. case SP_VARCASTRATE:
  2959. if(sd->state.lr_flag == 2)
  2960. break;
  2961. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2962. if (i == ARRAYLENGTH(sd->skillcast))
  2963. {
  2964. ShowDebug("run_script: bonus2 bVariableCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",ARRAYLENGTH(sd->skillcast), type2, val);
  2965. break;
  2966. }
  2967. if(sd->skillcast[i].id == type2)
  2968. sd->skillcast[i].val -= val;
  2969. else {
  2970. sd->skillcast[i].id = type2;
  2971. sd->skillcast[i].val -= val;
  2972. }
  2973. break;
  2974. #endif
  2975. case SP_SKILL_USE_SP: //bonus2 bSkillUseSP,n,x;
  2976. if(sd->state.lr_flag == 2)
  2977. break;
  2978. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  2979. if (i == ARRAYLENGTH(sd->skillusesp)) {
  2980. ShowDebug("run_script: bonus2 bSkillUseSP reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  2981. break;
  2982. }
  2983. if (sd->skillusesp[i].id == type2)
  2984. sd->skillusesp[i].val += val;
  2985. else {
  2986. sd->skillusesp[i].id = type2;
  2987. sd->skillusesp[i].val = val;
  2988. }
  2989. break;
  2990. default:
  2991. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  2992. break;
  2993. }
  2994. return 0;
  2995. }
  2996. int pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  2997. {
  2998. nullpo_ret(sd);
  2999. switch(type){
  3000. case SP_ADD_MONSTER_DROP_ITEM:
  3001. if(sd->state.lr_flag != 2)
  3002. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, 1<<type3, val);
  3003. break;
  3004. case SP_ADD_CLASS_DROP_ITEM:
  3005. if(sd->state.lr_flag != 2)
  3006. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, -type3, val);
  3007. break;
  3008. case SP_AUTOSPELL:
  3009. if(sd->state.lr_flag != 2)
  3010. {
  3011. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3012. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3013. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3014. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3015. }
  3016. break;
  3017. case SP_AUTOSPELL_WHENHIT:
  3018. if(sd->state.lr_flag != 2)
  3019. {
  3020. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3021. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3022. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3023. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3024. }
  3025. break;
  3026. case SP_SP_DRAIN_RATE:
  3027. if(!sd->state.lr_flag) {
  3028. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  3029. sd->right_weapon.sp_drain[RC_NONBOSS].per += type3;
  3030. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  3031. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  3032. sd->right_weapon.sp_drain[RC_BOSS].per += type3;
  3033. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  3034. }
  3035. else if(sd->state.lr_flag == 1) {
  3036. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  3037. sd->left_weapon.sp_drain[RC_NONBOSS].per += type3;
  3038. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  3039. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  3040. sd->left_weapon.sp_drain[RC_BOSS].per += type3;
  3041. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  3042. }
  3043. break;
  3044. case SP_HP_DRAIN_RATE_RACE:
  3045. if(!sd->state.lr_flag) {
  3046. sd->right_weapon.hp_drain[type2].rate += type3;
  3047. sd->right_weapon.hp_drain[type2].per += val;
  3048. }
  3049. else if(sd->state.lr_flag == 1) {
  3050. sd->left_weapon.hp_drain[type2].rate += type3;
  3051. sd->left_weapon.hp_drain[type2].per += val;
  3052. }
  3053. break;
  3054. case SP_SP_DRAIN_RATE_RACE:
  3055. if(!sd->state.lr_flag) {
  3056. sd->right_weapon.sp_drain[type2].rate += type3;
  3057. sd->right_weapon.sp_drain[type2].per += val;
  3058. }
  3059. else if(sd->state.lr_flag == 1) {
  3060. sd->left_weapon.sp_drain[type2].rate += type3;
  3061. sd->left_weapon.sp_drain[type2].per += val;
  3062. }
  3063. break;
  3064. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  3065. if (sd->state.lr_flag != 2)
  3066. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, 1<<type3, val);
  3067. break;
  3068. case SP_ADDEFF:
  3069. if (type2 > SC_MAX) {
  3070. ShowWarning("pc_bonus3 (Add Effect): %d is not supported.\n", type2);
  3071. break;
  3072. }
  3073. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3074. sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val);
  3075. break;
  3076. case SP_ADDEFF_WHENHIT:
  3077. if (type2 > SC_MAX) {
  3078. ShowWarning("pc_bonus3 (Add Effect when hit): %d is not supported.\n", type2);
  3079. break;
  3080. }
  3081. if(sd->state.lr_flag != 2)
  3082. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val);
  3083. break;
  3084. case SP_ADDEFF_ONSKILL:
  3085. if( type3 > SC_MAX ) {
  3086. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type3);
  3087. break;
  3088. }
  3089. if( sd->state.lr_flag != 2 )
  3090. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
  3091. break;
  3092. case SP_ADDELE:
  3093. if (type2 > ELE_MAX) {
  3094. ShowWarning("pc_bonus3 (SP_ADDELE): element %d is out of range.\n", type2);
  3095. break;
  3096. }
  3097. if (sd->state.lr_flag != 2)
  3098. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3099. break;
  3100. case SP_SUBELE:
  3101. if (type2 > ELE_MAX) {
  3102. ShowWarning("pc_bonus3 (SP_SUBELE): element %d is out of range.\n", type2);
  3103. break;
  3104. }
  3105. if (sd->state.lr_flag != 2)
  3106. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3107. break;
  3108. default:
  3109. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3110. break;
  3111. }
  3112. return 0;
  3113. }
  3114. int pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3115. {
  3116. nullpo_ret(sd);
  3117. switch(type){
  3118. case SP_AUTOSPELL:
  3119. if(sd->state.lr_flag != 2)
  3120. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3121. break;
  3122. case SP_AUTOSPELL_WHENHIT:
  3123. if(sd->state.lr_flag != 2)
  3124. 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);
  3125. break;
  3126. case SP_AUTOSPELL_ONSKILL:
  3127. if(sd->state.lr_flag != 2)
  3128. {
  3129. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3130. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3131. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3132. }
  3133. break;
  3134. case SP_ADDEFF_ONSKILL:
  3135. if( type2 > SC_MAX ) {
  3136. ShowWarning("pc_bonus4 (Add Effect on skill): %d is not supported.\n", type2);
  3137. break;
  3138. }
  3139. if( sd->state.lr_flag != 2 )
  3140. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
  3141. break;
  3142. case SP_DEF_SET: //bonus4 bSetDefRace,n,x,r,y;
  3143. if( type2 > RC_MAX ) {
  3144. ShowWarning("pc_bonus4 (DEF_SET): %d is not supported.\n", type2);
  3145. break;
  3146. }
  3147. if(sd->state.lr_flag == 2)
  3148. break;
  3149. sd->def_set_race[type2].rate = type3;
  3150. sd->def_set_race[type2].tick = type4;
  3151. sd->def_set_race[type2].value = val;
  3152. break;
  3153. case SP_MDEF_SET: //bonus4 bSetMDefRace,n,x,r,y;
  3154. if( type2 > RC_MAX ) {
  3155. ShowWarning("pc_bonus4 (MDEF_SET): %d is not supported.\n", type2);
  3156. break;
  3157. }
  3158. if(sd->state.lr_flag == 2)
  3159. break;
  3160. sd->mdef_set_race[type2].rate = type3;
  3161. sd->mdef_set_race[type2].tick = type4;
  3162. sd->mdef_set_race[type2].value = val;
  3163. break;
  3164. default:
  3165. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3166. break;
  3167. }
  3168. return 0;
  3169. }
  3170. int pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3171. {
  3172. nullpo_ret(sd);
  3173. switch(type){
  3174. case SP_AUTOSPELL:
  3175. if(sd->state.lr_flag != 2)
  3176. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3177. break;
  3178. case SP_AUTOSPELL_WHENHIT:
  3179. if(sd->state.lr_flag != 2)
  3180. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3181. break;
  3182. case SP_AUTOSPELL_ONSKILL:
  3183. if(sd->state.lr_flag != 2)
  3184. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3185. break;
  3186. default:
  3187. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3188. break;
  3189. }
  3190. return 0;
  3191. }
  3192. /*==========================================
  3193. * Grants a player a given skill. Flag values are:
  3194. * 0 - Grant permanent skill to be bound to skill tree
  3195. * 1 - Grant an item skill (temporary)
  3196. * 2 - Like 1, except the level granted can stack with previously learned level.
  3197. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3198. *------------------------------------------*/
  3199. int pc_skill(TBL_PC* sd, int id, int level, int flag)
  3200. {
  3201. nullpo_ret(sd);
  3202. if( id <= 0 || id >= MAX_SKILL || skill_db[id].name == NULL) {
  3203. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", id);
  3204. return 0;
  3205. }
  3206. if( level > MAX_SKILL_LEVEL ) {
  3207. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3208. return 0;
  3209. }
  3210. if( flag == 2 && sd->status.skill[id].lv + level > MAX_SKILL_LEVEL ) {
  3211. ShowError("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d\n", level, MAX_SKILL_LEVEL, sd->status.skill[id].lv);
  3212. return 0;
  3213. }
  3214. switch( flag ){
  3215. case 0: //Set skill data overwriting whatever was there before.
  3216. sd->status.skill[id].id = id;
  3217. sd->status.skill[id].lv = level;
  3218. sd->status.skill[id].flag = SKILL_FLAG_PERMANENT;
  3219. if( level == 0 ) { //Remove skill.
  3220. sd->status.skill[id].id = 0;
  3221. clif_deleteskill(sd,id);
  3222. } else
  3223. clif_addskill(sd,id);
  3224. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3225. status_calc_pc(sd, 0);
  3226. break;
  3227. case 1: //Item bonus skill.
  3228. if( sd->status.skill[id].id == id ){
  3229. if( sd->status.skill[id].lv >= level )
  3230. return 0;
  3231. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT ) //Non-granted skill, store it's level.
  3232. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv;
  3233. } else {
  3234. sd->status.skill[id].id = id;
  3235. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY;
  3236. }
  3237. sd->status.skill[id].lv = level;
  3238. break;
  3239. case 2: //Add skill bonus on top of what you had.
  3240. if( sd->status.skill[id].id == id ){
  3241. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT )
  3242. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Store previous level.
  3243. } else {
  3244. sd->status.skill[id].id = id;
  3245. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3246. }
  3247. sd->status.skill[id].lv += level;
  3248. break;
  3249. case 4: //Permanent granted skills ignore the skill tree
  3250. sd->status.skill[id].id = id;
  3251. sd->status.skill[id].lv = level;
  3252. sd->status.skill[id].flag = SKILL_FLAG_PERM_GRANTED;
  3253. if( level == 0 ) { //Remove skill.
  3254. sd->status.skill[id].id = 0;
  3255. clif_deleteskill(sd,id);
  3256. } else
  3257. clif_addskill(sd,id);
  3258. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3259. status_calc_pc(sd, 0);
  3260. break;
  3261. default: //Unknown flag?
  3262. return 0;
  3263. }
  3264. return 1;
  3265. }
  3266. /*==========================================
  3267. * Append a card to an item ?
  3268. *------------------------------------------*/
  3269. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3270. {
  3271. int i;
  3272. int nameid;
  3273. nullpo_ret(sd);
  3274. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3275. return 0; //Invalid item index.
  3276. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3277. return 0; //Invalid card index.
  3278. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3279. return 0; // target item missing
  3280. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3281. return 0; // target card missing
  3282. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3283. return 0; // only weapons and armor are allowed
  3284. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3285. return 0; // must be a card
  3286. if( sd->status.inventory[idx_equip].identify == 0 )
  3287. return 0; // target must be identified
  3288. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3289. return 0; // card slots reserved for other purposes
  3290. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3291. return 0; // card cannot be compounded on this item type
  3292. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3293. return 0; // attempted to place shield card on left-hand weapon.
  3294. if( sd->status.inventory[idx_equip].equip != 0 )
  3295. return 0; // item must be unequipped
  3296. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3297. if( i == sd->inventory_data[idx_equip]->slot )
  3298. return 0; // no free slots
  3299. // remember the card id to insert
  3300. nameid = sd->status.inventory[idx_card].nameid;
  3301. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3302. {// failed
  3303. clif_insert_card(sd,idx_equip,idx_card,1);
  3304. }
  3305. else
  3306. {// success
  3307. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3308. sd->status.inventory[idx_equip].card[i] = nameid;
  3309. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3310. clif_insert_card(sd,idx_equip,idx_card,0);
  3311. }
  3312. return 0;
  3313. }
  3314. //
  3315. // Items
  3316. //
  3317. /*==========================================
  3318. * Update buying value by skills
  3319. *------------------------------------------*/
  3320. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3321. {
  3322. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3323. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3324. rate1 = 5+skill*2-((skill==10)? 1:0);
  3325. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3326. rate2 = 5+skill*4;
  3327. if(rate1 < rate2) rate1 = rate2;
  3328. if(rate1)
  3329. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3330. if(val < 0) val = 0;
  3331. if(orig_value > 0 && val < 1) val = 1;
  3332. return val;
  3333. }
  3334. /*==========================================
  3335. * Update selling value by skills
  3336. *------------------------------------------*/
  3337. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3338. {
  3339. int skill,val = orig_value,rate = 0;
  3340. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3341. rate = 5+skill*2-((skill==10)? 1:0);
  3342. if(rate)
  3343. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3344. if(val < 0) val = 0;
  3345. if(orig_value > 0 && val < 1) val = 1;
  3346. return val;
  3347. }
  3348. /*==========================================
  3349. * Checking if we have enough place on inventory for new item
  3350. * Make sure to take 30k as limit (for client I guess)
  3351. *------------------------------------------*/
  3352. int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
  3353. {
  3354. int i;
  3355. struct item_data* data;
  3356. nullpo_ret(sd);
  3357. if(amount > MAX_AMOUNT)
  3358. return CHKADDITEM_OVERAMOUNT;
  3359. data = itemdb_search(nameid);
  3360. if(!itemdb_isstackable2(data))
  3361. return CHKADDITEM_NEW;
  3362. if( data->stack.inventory && amount > data->stack.amount )
  3363. return CHKADDITEM_OVERAMOUNT;
  3364. for(i=0;i<MAX_INVENTORY;i++){
  3365. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3366. if(sd->status.inventory[i].nameid==nameid){
  3367. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3368. return CHKADDITEM_OVERAMOUNT;
  3369. return CHKADDITEM_EXIST;
  3370. }
  3371. }
  3372. return CHKADDITEM_NEW;
  3373. }
  3374. /*==========================================
  3375. * Return number of available place in inventory
  3376. * Each non stackable item will reduce place by 1
  3377. *------------------------------------------*/
  3378. int pc_inventoryblank(struct map_session_data *sd)
  3379. {
  3380. int i,b;
  3381. nullpo_ret(sd);
  3382. for(i=0,b=0;i<MAX_INVENTORY;i++){
  3383. if(sd->status.inventory[i].nameid==0)
  3384. b++;
  3385. }
  3386. return b;
  3387. }
  3388. /*==========================================
  3389. * attempts to remove zeny from player (sd)
  3390. *------------------------------------------*/
  3391. int pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3392. {
  3393. nullpo_retr(-1,sd);
  3394. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3395. if( zeny < 0 )
  3396. {
  3397. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3398. return 1;
  3399. }
  3400. if( sd->status.zeny < zeny )
  3401. return 1; //Not enough.
  3402. sd->status.zeny -= zeny;
  3403. clif_updatestatus(sd,SP_ZENY);
  3404. if(!tsd) tsd = sd;
  3405. log_zeny(sd, type, tsd, -zeny);
  3406. if( zeny > 0 && sd->state.showzeny ) {
  3407. char output[255];
  3408. sprintf(output, "Removed %dz.", zeny);
  3409. clif_disp_onlyself(sd,output,strlen(output));
  3410. }
  3411. return 0;
  3412. }
  3413. /*==========================================
  3414. * Cash Shop
  3415. *------------------------------------------*/
  3416. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type ){
  3417. int cash;
  3418. nullpo_retr(-1,sd);
  3419. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3420. if( price < 0 || points < 0 )
  3421. {
  3422. 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);
  3423. return -2;
  3424. }
  3425. if( points > price )
  3426. {
  3427. 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);
  3428. points = price;
  3429. }
  3430. cash = price-points;
  3431. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3432. {
  3433. 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);
  3434. return -1;
  3435. }
  3436. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints-cash);
  3437. if( cash ){
  3438. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3439. }
  3440. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints-points);
  3441. if( points ){
  3442. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3443. }
  3444. if( battle_config.cashshop_show_points )
  3445. {
  3446. char output[128];
  3447. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3448. clif_disp_onlyself(sd, output, strlen(output));
  3449. }
  3450. return cash+points;
  3451. }
  3452. int pc_getcash( struct map_session_data *sd, int cash, int points, e_log_pick_type type ){
  3453. char output[128];
  3454. nullpo_retr(-1,sd);
  3455. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3456. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3457. if( cash > 0 )
  3458. {
  3459. if( cash > MAX_ZENY-sd->cashPoints )
  3460. {
  3461. 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);
  3462. cash = MAX_ZENY-sd->cashPoints;
  3463. }
  3464. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints+cash);
  3465. if( cash ){
  3466. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3467. }
  3468. if( battle_config.cashshop_show_points )
  3469. {
  3470. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3471. clif_disp_onlyself(sd, output, strlen(output));
  3472. }
  3473. return cash;
  3474. }
  3475. else if( cash < 0 )
  3476. {
  3477. 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);
  3478. return -1;
  3479. }
  3480. if( points > 0 )
  3481. {
  3482. if( points > MAX_ZENY-sd->kafraPoints )
  3483. {
  3484. 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);
  3485. points = MAX_ZENY-sd->kafraPoints;
  3486. }
  3487. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints+points);
  3488. if( points ){
  3489. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3490. }
  3491. if( battle_config.cashshop_show_points )
  3492. {
  3493. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3494. clif_disp_onlyself(sd, output, strlen(output));
  3495. }
  3496. return points;
  3497. }
  3498. else if( points < 0 )
  3499. {
  3500. 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);
  3501. return -1;
  3502. }
  3503. return -2; //shouldn't happen but jsut in case
  3504. }
  3505. /*==========================================
  3506. * Attempts to give zeny to player (sd)
  3507. * tsd (optional) from who for log (if null take sd)
  3508. *------------------------------------------*/
  3509. int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3510. {
  3511. nullpo_retr(-1,sd);
  3512. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3513. if( zeny < 0 )
  3514. {
  3515. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3516. return 1;
  3517. }
  3518. if( zeny > MAX_ZENY - sd->status.zeny )
  3519. zeny = MAX_ZENY - sd->status.zeny;
  3520. sd->status.zeny += zeny;
  3521. clif_updatestatus(sd,SP_ZENY);
  3522. if(!tsd) tsd = sd;
  3523. log_zeny(sd, type, tsd, zeny);
  3524. if( zeny > 0 && sd->state.showzeny ) {
  3525. char output[255];
  3526. sprintf(output, "Gained %dz.", zeny);
  3527. clif_disp_onlyself(sd,output,strlen(output));
  3528. }
  3529. return 0;
  3530. }
  3531. /*==========================================
  3532. * Searching a specified itemid in inventory and return his stored index
  3533. *------------------------------------------*/
  3534. int pc_search_inventory(struct map_session_data *sd,int item_id)
  3535. {
  3536. int i;
  3537. nullpo_retr(-1, sd);
  3538. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
  3539. return ( i < MAX_INVENTORY ) ? i : -1;
  3540. }
  3541. /*==========================================
  3542. * Attempt to add a new item to inventory.
  3543. * Return:
  3544. 0 = success
  3545. 1 = invalid itemid not found or negative amount
  3546. 2 = overweight
  3547. 3 = ?
  3548. 4 = no free place found
  3549. 5 = max amount reached
  3550. 6 = ?
  3551. 7 = stack limitation
  3552. *------------------------------------------*/
  3553. int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3554. {
  3555. struct item_data *data;
  3556. int i;
  3557. unsigned int w;
  3558. nullpo_retr(1, sd);
  3559. nullpo_retr(1, item_data);
  3560. if( item_data->nameid <= 0 || amount <= 0 )
  3561. return ADDITEM_INVALID;
  3562. if( amount > MAX_AMOUNT )
  3563. return ADDITEM_OVERAMOUNT;
  3564. data = itemdb_search(item_data->nameid);
  3565. if( data->stack.inventory && amount > data->stack.amount )
  3566. {// item stack limitation
  3567. return ADDITEM_STACKLIMIT;
  3568. }
  3569. w = data->weight*amount;
  3570. if(sd->weight + w > sd->max_weight)
  3571. return ADDITEM_OVERWEIGHT;
  3572. i = MAX_INVENTORY;
  3573. if( itemdb_isstackable2(data) && item_data->expire_time == 0 )
  3574. { // Stackable | Non Rental
  3575. for( i = 0; i < MAX_INVENTORY; i++ )
  3576. {
  3577. if( sd->status.inventory[i].nameid == item_data->nameid && sd->status.inventory[i].bound == item_data->bound && memcmp(&sd->status.inventory[i].card, &item_data->card, sizeof(item_data->card)) == 0 )
  3578. {
  3579. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3580. return 5;
  3581. sd->status.inventory[i].amount += amount;
  3582. clif_additem(sd,i,amount,0);
  3583. break;
  3584. }
  3585. }
  3586. }
  3587. if( i >= MAX_INVENTORY )
  3588. {
  3589. i = pc_search_inventory(sd,0);
  3590. if( i < 0 )
  3591. return ADDITEM_OVERITEM;
  3592. memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
  3593. // clear equips field first, just in case
  3594. if( item_data->equip )
  3595. sd->status.inventory[i].equip = 0;
  3596. sd->status.inventory[i].amount = amount;
  3597. sd->inventory_data[i] = data;
  3598. clif_additem(sd,i,amount,0);
  3599. }
  3600. #ifdef NSI_UNIQUE_ID
  3601. if( !itemdb_isstackable2(data) && !item_data->unique_id )
  3602. sd->status.inventory[i].unique_id = itemdb_unique_id(0,0);
  3603. #endif
  3604. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  3605. sd->weight += w;
  3606. clif_updatestatus(sd,SP_WEIGHT);
  3607. //Auto-equip
  3608. if(data->flag.autoequip)
  3609. pc_equipitem(sd, i, data->equip);
  3610. /* rental item check */
  3611. if( item_data->expire_time ) {
  3612. if( time(NULL) > item_data->expire_time ) {
  3613. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  3614. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  3615. } else {
  3616. int seconds = (int)( item_data->expire_time - time(NULL) );
  3617. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  3618. pc_inventory_rental_add(sd, seconds);
  3619. }
  3620. }
  3621. return ADDITEM_SUCCESS;
  3622. }
  3623. /*==========================================
  3624. * Remove an item at index n from inventory by amount.
  3625. * Parameters :
  3626. * @type
  3627. * 1 : don't notify deletion
  3628. * 2 : don't notify weight change
  3629. * Return:
  3630. * 0 = success
  3631. * 1 = invalid itemid or negative amount
  3632. *------------------------------------------*/
  3633. int pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  3634. {
  3635. nullpo_retr(1, sd);
  3636. if(n < 0 || sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  3637. return 1;
  3638. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  3639. sd->status.inventory[n].amount -= amount;
  3640. sd->weight -= sd->inventory_data[n]->weight*amount ;
  3641. if( sd->status.inventory[n].amount <= 0 ){
  3642. if(sd->status.inventory[n].equip)
  3643. pc_unequipitem(sd,n,3);
  3644. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  3645. sd->inventory_data[n] = NULL;
  3646. }
  3647. if(!(type&1))
  3648. clif_delitem(sd,n,amount,reason);
  3649. if(!(type&2))
  3650. clif_updatestatus(sd,SP_WEIGHT);
  3651. return 0;
  3652. }
  3653. /*==========================================
  3654. * Attempt to drop an item.
  3655. * Return:
  3656. * 0 = fail
  3657. * 1 = success
  3658. *------------------------------------------*/
  3659. int pc_dropitem(struct map_session_data *sd,int n,int amount)
  3660. {
  3661. nullpo_retr(1, sd);
  3662. if(n < 0 || n >= MAX_INVENTORY)
  3663. return 0;
  3664. if(amount <= 0)
  3665. return 0;
  3666. if(sd->status.inventory[n].nameid <= 0 ||
  3667. sd->status.inventory[n].amount <= 0 ||
  3668. sd->status.inventory[n].amount < amount ||
  3669. sd->state.trading || sd->state.vending ||
  3670. !sd->inventory_data[n] //pc_delitem would fail on this case.
  3671. )
  3672. return 0;
  3673. if( map[sd->bl.m].flag.nodrop )
  3674. {
  3675. clif_displaymessage (sd->fd, msg_txt(sd,271));
  3676. return 0; //Can't drop items in nodrop mapflag maps.
  3677. }
  3678. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  3679. {
  3680. clif_displaymessage (sd->fd, msg_txt(sd,263));
  3681. return 0;
  3682. }
  3683. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
  3684. return 0;
  3685. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  3686. clif_dropitem(sd, n, amount);
  3687. return 1;
  3688. }
  3689. /*==========================================
  3690. * Attempt to pick up an item.
  3691. * Return:
  3692. * 0 = fail
  3693. * 1 = success
  3694. *------------------------------------------*/
  3695. int pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  3696. {
  3697. int flag=0;
  3698. unsigned int tick = gettick();
  3699. struct map_session_data *first_sd = NULL,*second_sd = NULL,*third_sd = NULL;
  3700. struct party_data *p=NULL;
  3701. nullpo_ret(sd);
  3702. nullpo_ret(fitem);
  3703. if(!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  3704. return 0; // Distance is too far
  3705. if( sd->sc.cant.pickup )
  3706. return 0;
  3707. if (sd->status.party_id)
  3708. p = party_search(sd->status.party_id);
  3709. if(fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id)
  3710. {
  3711. first_sd = map_charid2sd(fitem->first_get_charid);
  3712. if(DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  3713. if (!(p && p->party.item&1 &&
  3714. first_sd && first_sd->status.party_id == sd->status.party_id
  3715. ))
  3716. return 0;
  3717. }
  3718. else
  3719. if(fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id)
  3720. {
  3721. second_sd = map_charid2sd(fitem->second_get_charid);
  3722. if(DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  3723. if(!(p && p->party.item&1 &&
  3724. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3725. (second_sd && second_sd->status.party_id == sd->status.party_id))
  3726. ))
  3727. return 0;
  3728. }
  3729. else
  3730. if(fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id)
  3731. {
  3732. third_sd = map_charid2sd(fitem->third_get_charid);
  3733. if(DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  3734. if(!(p && p->party.item&1 &&
  3735. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3736. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  3737. (third_sd && third_sd->status.party_id == sd->status.party_id))
  3738. ))
  3739. return 0;
  3740. }
  3741. }
  3742. }
  3743. }
  3744. //This function takes care of giving the item to whoever should have it, considering party-share options.
  3745. if ((flag = party_share_loot(p,sd,&fitem->item_data, fitem->first_get_charid))) {
  3746. clif_additem(sd,0,0,flag);
  3747. return 1;
  3748. }
  3749. //Display pickup animation.
  3750. pc_stop_attack(sd);
  3751. clif_takeitem(&sd->bl,&fitem->bl);
  3752. map_clearflooritem(&fitem->bl);
  3753. return 1;
  3754. }
  3755. /*==========================================
  3756. * Check if item is usable.
  3757. * Return:
  3758. * 0 = no
  3759. * 1 = yes
  3760. *------------------------------------------*/
  3761. int pc_isUseitem(struct map_session_data *sd,int n)
  3762. {
  3763. struct item_data *item;
  3764. int nameid;
  3765. nullpo_ret(sd);
  3766. item = sd->inventory_data[n];
  3767. nameid = sd->status.inventory[n].nameid;
  3768. if( item == NULL )
  3769. return 0;
  3770. //Not consumable item
  3771. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  3772. return 0;
  3773. if( !item->script ) //if it has no script, you can't really consume it!
  3774. return 0;
  3775. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  3776. return 1;
  3777. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  3778. return 0;
  3779. //Prevent mass item usage. [Skotlex]
  3780. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  3781. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  3782. )
  3783. return 0;
  3784. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  3785. return 0; // You cannot use this item while sitting.
  3786. }
  3787. switch( nameid ) //@TODO, lot of hardcoded nameid here
  3788. {
  3789. case 605: // Anodyne
  3790. if( map_flag_gvg(sd->bl.m) )
  3791. return 0;
  3792. case 606:
  3793. if( pc_issit(sd) )
  3794. return 0;
  3795. break;
  3796. case 601: // Fly Wing
  3797. case 12212: // Giant Fly Wing
  3798. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) )
  3799. {
  3800. clif_skill_teleportmessage(sd,0);
  3801. return 0;
  3802. }
  3803. case 602: // ButterFly Wing
  3804. case 14527: // Dungeon Teleport Scroll
  3805. case 14581: // Dungeon Teleport Scroll 2
  3806. case 12352: // Dungeon Teleport Scroll 3
  3807. case 14582: // Yellow Butterfly Wing
  3808. case 14583: // Green Butterfly Wing
  3809. case 14584: // Red Butterfly Wing
  3810. case 14585: // Blue Butterfly Wing
  3811. case 14591: // Siege Teleport Scroll
  3812. if( sd->duel_group && !battle_config.duel_allow_teleport )
  3813. {
  3814. clif_displaymessage(sd->fd, msg_txt(sd,663));
  3815. return 0;
  3816. }
  3817. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  3818. return 0;
  3819. break;
  3820. case 604: // Dead Branch
  3821. case 12024: // Red Pouch
  3822. case 12103: // Bloody Branch
  3823. case 12109: // Poring Box
  3824. if( map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m) )
  3825. return 0;
  3826. break;
  3827. case 12210: // Bubble Gum
  3828. case 12264: // Comp Bubble Gum
  3829. if( sd->sc.data[SC_ITEMBOOST] )
  3830. return 0;
  3831. break;
  3832. case 12208: // Battle Manual
  3833. case 12263: // Comp Battle Manual
  3834. case 12312: // Thick Battle Manual
  3835. case 12705: // Noble Nameplate
  3836. case 14532: // Battle_Manual25
  3837. case 14533: // Battle_Manual100
  3838. case 14545: // Battle_Manual300
  3839. if( sd->sc.data[SC_EXPBOOST] )
  3840. return 0;
  3841. break;
  3842. case 14592: // JOB_Battle_Manual
  3843. if( sd->sc.data[SC_JEXPBOOST] )
  3844. return 0;
  3845. break;
  3846. // Mercenary Items
  3847. case 12184: // Mercenary's Red Potion
  3848. case 12185: // Mercenary's Blue Potion
  3849. case 12241: // Mercenary's Concentration Potion
  3850. case 12242: // Mercenary's Awakening Potion
  3851. case 12243: // Mercenary's Berserk Potion
  3852. if( sd->md == NULL || sd->md->db == NULL )
  3853. return 0;
  3854. if (sd->md->sc.data[SC_BERSERK] || sd->md->sc.data[SC_SATURDAYNIGHTFEVER])
  3855. return 0;
  3856. if( nameid == 12242 && sd->md->db->lv < 40 )
  3857. return 0;
  3858. if( nameid == 12243 && sd->md->db->lv < 80 )
  3859. return 0;
  3860. break;
  3861. case 12213: //Neuralizer
  3862. if( !map[sd->bl.m].flag.reset )
  3863. return 0;
  3864. break;
  3865. }
  3866. if( nameid >= 12153 && nameid <= 12182 && sd->md != NULL )
  3867. return 0; // Mercenary Scrolls
  3868. /**
  3869. * Only Rune Knights may use runes
  3870. **/
  3871. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  3872. return 0;
  3873. /**
  3874. * Only GCross may use poisons
  3875. **/
  3876. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  3877. return 0;
  3878. //Gender check
  3879. if(item->sex != 2 && sd->status.sex != item->sex)
  3880. return 0;
  3881. //Required level check
  3882. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  3883. return 0;
  3884. #ifdef RENEWAL
  3885. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  3886. return 0;
  3887. #endif
  3888. //Not equipable by class. [Skotlex]
  3889. if (!(
  3890. (1<<(sd->class_&MAPID_BASEMASK)) &
  3891. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  3892. ))
  3893. return 0;
  3894. if (sd->sc.count && (
  3895. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  3896. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  3897. sd->sc.data[SC_TRICKDEAD] ||
  3898. sd->sc.data[SC_HIDING] ||
  3899. sd->sc.data[SC__SHADOWFORM] ||
  3900. sd->sc.data[SC__INVISIBILITY] ||
  3901. sd->sc.data[SC__MANHOLE] ||
  3902. sd->sc.data[SC_KAGEHUMI] ||
  3903. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  3904. sd->sc.data[SC_HEAT_BARREL_AFTER]))
  3905. return 0;
  3906. if (!pc_isItemClass(sd,item))
  3907. return 0;
  3908. //Dead Branch & Bloody Branch & Porings Box
  3909. // FIXME: outdated, use constants or database
  3910. if( nameid == 604 || nameid == 12103 || nameid == 12109 )
  3911. log_branch(sd);
  3912. return 1;
  3913. }
  3914. /*==========================================
  3915. * Last checks to use an item.
  3916. * Return:
  3917. * 0 = fail
  3918. * 1 = success
  3919. *------------------------------------------*/
  3920. int pc_useitem(struct map_session_data *sd,int n)
  3921. {
  3922. unsigned int tick = gettick();
  3923. int amount, nameid;
  3924. struct script_code *script;
  3925. struct item item;
  3926. struct item_data *id;
  3927. nullpo_ret(sd);
  3928. if( sd->npc_id ){
  3929. #ifdef RENEWAL
  3930. clif_msg(sd, USAGE_FAIL); // TODO look for the client date that has this message.
  3931. return 0;
  3932. #else
  3933. if( !sd->npc_item_flag )
  3934. return 0;
  3935. #endif
  3936. }
  3937. item = sd->status.inventory[n];
  3938. id = sd->inventory_data[n];
  3939. if (item.nameid <= 0 || item.amount <= 0)
  3940. return 0;
  3941. if( !pc_isUseitem(sd,n) )
  3942. return 0;
  3943. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  3944. nameid = id->nameid;
  3945. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  3946. return 0;
  3947. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  3948. if( id->flag.delay_consume ) {
  3949. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.option&OPTION_MOUNTING )
  3950. return 0;
  3951. else if( pc_issit(sd) )
  3952. return 0;
  3953. }
  3954. //Since most delay-consume items involve using a "skill-type" target cursor,
  3955. //perform a skill-use check before going through. [Skotlex]
  3956. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  3957. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  3958. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  3959. return 0;
  3960. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) ) {
  3961. int i;
  3962. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
  3963. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  3964. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  3965. if( i < MAX_ITEMDELAYS ) {
  3966. if( sd->item_delay[i].nameid ) {// found
  3967. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  3968. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  3969. char e_msg[100];
  3970. if( e_tick > 99 )
  3971. sprintf(e_msg,msg_txt(sd,379), //Item Failed. [%s] is cooling down. Wait %.1f minutes.
  3972. itemdb_jname(item.nameid),
  3973. (double)e_tick / 60);
  3974. else
  3975. sprintf(e_msg,msg_txt(sd,380), //Item Failed. [%s] is cooling down. Wait %d seconds.
  3976. itemdb_jname(item.nameid),
  3977. e_tick+1);
  3978. clif_colormes(sd,color_table[COLOR_RED],e_msg);
  3979. return 0; // Delay has not expired yet
  3980. }
  3981. } else {// not yet used item (all slots are initially empty)
  3982. sd->item_delay[i].nameid = nameid;
  3983. }
  3984. if( !(nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  3985. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  3986. } else {// should not happen
  3987. ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%d, char_id=%d)\n", nameid, sd->status.char_id);
  3988. }
  3989. //clean up used delays so we can give room for more
  3990. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  3991. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  3992. sd->item_delay[i].tick = 0;
  3993. sd->item_delay[i].nameid = 0;
  3994. }
  3995. }
  3996. }
  3997. /* on restricted maps the item is consumed but the effect is not used */
  3998. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  3999. if( battle_config.allow_consume_restricted_item ) {
  4000. clif_useitemack(sd,n,item.amount-1,true);
  4001. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4002. }
  4003. return 0;/* regardless, effect is not run */
  4004. }
  4005. sd->itemid = item.nameid;
  4006. sd->itemindex = n;
  4007. if(sd->catch_target_class != -1) //Abort pet catching.
  4008. sd->catch_target_class = -1;
  4009. amount = item.amount;
  4010. script = id->script;
  4011. //Check if the item is to be consumed immediately [Skotlex]
  4012. if (id->flag.delay_consume)
  4013. clif_useitemack(sd, n, amount, true);
  4014. else
  4015. {
  4016. if (item.expire_time == 0)
  4017. {
  4018. clif_useitemack(sd, n, amount - 1, true);
  4019. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4020. }
  4021. else
  4022. clif_useitemack(sd, n, 0, false);
  4023. }
  4024. if(item.card[0]==CARD0_CREATE &&
  4025. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4026. {
  4027. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4028. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4029. potion_flag = 3; //Even more effective potions.
  4030. }
  4031. //Update item use time.
  4032. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4033. if( itemdb_iscashfood(nameid) )
  4034. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4035. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4036. potion_flag = 0;
  4037. return 1;
  4038. }
  4039. /*==========================================
  4040. * Add item on cart for given index.
  4041. * Return:
  4042. * 0 = success
  4043. * 1 = fail
  4044. *------------------------------------------*/
  4045. int pc_cart_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  4046. {
  4047. struct item_data *data;
  4048. int i,w;
  4049. nullpo_retr(1, sd);
  4050. nullpo_retr(1, item_data);
  4051. if(item_data->nameid <= 0 || amount <= 0)
  4052. return 1;
  4053. data = itemdb_search(item_data->nameid);
  4054. if( data->stack.cart && amount > data->stack.amount )
  4055. {// item stack limitation
  4056. return 1;
  4057. }
  4058. if( !itemdb_cancartstore(item_data, pc_get_group_level(sd)) || (item_data->bound > 1 && !pc_can_give_bounded_items(sd)))
  4059. { // Check item trade restrictions [Skotlex]
  4060. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4061. return 1;
  4062. }
  4063. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4064. return 1;
  4065. i = MAX_CART;
  4066. if( itemdb_isstackable2(data) && !item_data->expire_time )
  4067. {
  4068. ARR_FIND( 0, MAX_CART, i,
  4069. sd->status.cart[i].nameid == item_data->nameid && sd->status.cart[i].bound == item_data->bound &&
  4070. sd->status.cart[i].card[0] == item_data->card[0] && sd->status.cart[i].card[1] == item_data->card[1] &&
  4071. sd->status.cart[i].card[2] == item_data->card[2] && sd->status.cart[i].card[3] == item_data->card[3] );
  4072. };
  4073. if( i < MAX_CART )
  4074. {// item already in cart, stack it
  4075. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4076. return 2; // no slot
  4077. sd->status.cart[i].amount+=amount;
  4078. clif_cart_additem(sd,i,amount,0);
  4079. }
  4080. else
  4081. {// item not stackable or not present, add it
  4082. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4083. if( i == MAX_CART )
  4084. return 2; // no slot
  4085. memcpy(&sd->status.cart[i],item_data,sizeof(sd->status.cart[0]));
  4086. sd->status.cart[i].amount=amount;
  4087. sd->cart_num++;
  4088. clif_cart_additem(sd,i,amount,0);
  4089. }
  4090. sd->status.cart[i].favorite = 0;/* clear */
  4091. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4092. sd->cart_weight += w;
  4093. clif_updatestatus(sd,SP_CARTINFO);
  4094. return 0;
  4095. }
  4096. /*==========================================
  4097. * Delete item on cart for given index.
  4098. * Return:
  4099. * 0 = success
  4100. * 1 = fail
  4101. *------------------------------------------*/
  4102. int pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4103. {
  4104. nullpo_retr(1, sd);
  4105. if(sd->status.cart[n].nameid==0 ||
  4106. sd->status.cart[n].amount<amount)
  4107. return 1;
  4108. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4109. sd->status.cart[n].amount -= amount;
  4110. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4111. if(sd->status.cart[n].amount <= 0){
  4112. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4113. sd->cart_num--;
  4114. }
  4115. if(!type) {
  4116. clif_cart_delitem(sd,n,amount);
  4117. clif_updatestatus(sd,SP_CARTINFO);
  4118. }
  4119. return 0;
  4120. }
  4121. /*==========================================
  4122. * Transfer item from inventory to cart.
  4123. * Return:
  4124. * 0 = fail
  4125. * 1 = succes
  4126. *------------------------------------------*/
  4127. int pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4128. {
  4129. struct item *item_data;
  4130. short flag;
  4131. nullpo_ret(sd);
  4132. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4133. return 1;
  4134. item_data = &sd->status.inventory[idx];
  4135. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4136. return 1;
  4137. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4138. return pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4139. return flag;
  4140. }
  4141. /*==========================================
  4142. * Get number of item in cart.
  4143. * Return:
  4144. -1 = itemid not found or no amount found
  4145. x = remaining itemid on cart after get
  4146. *------------------------------------------*/
  4147. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4148. {
  4149. struct item* item_data;
  4150. nullpo_retr(-1, sd);
  4151. item_data = &sd->status.cart[idx];
  4152. if( item_data->nameid == 0 || item_data->amount == 0 )
  4153. return -1;
  4154. return item_data->amount - amount;
  4155. }
  4156. /*==========================================
  4157. * Retrieve an item at index idx from cart.
  4158. * Return:
  4159. * 0 = player not found or (FIXME) succes (from pc_cart_delitem)
  4160. * 1 = failure
  4161. *------------------------------------------*/
  4162. int pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4163. {
  4164. struct item *item_data;
  4165. int flag;
  4166. nullpo_ret(sd);
  4167. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4168. return 1;
  4169. item_data=&sd->status.cart[idx];
  4170. if(item_data->nameid==0 || amount < 1 || item_data->amount<amount || sd->state.vending )
  4171. return 1;
  4172. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4173. return pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4174. clif_additem(sd,0,0,flag);
  4175. return 1;
  4176. }
  4177. /*==========================================
  4178. * Bound Item Check
  4179. * Type:
  4180. * 1 Account Bound
  4181. * 2 Guild Bound
  4182. * 3 Party Bound
  4183. * 4 Character Bound
  4184. *------------------------------------------*/
  4185. int pc_bound_chk(TBL_PC *sd,int type,int *idxlist)
  4186. {
  4187. int i=0, j=0;
  4188. for(i=0;i<MAX_INVENTORY;i++){
  4189. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4190. idxlist[j] = i;
  4191. j++;
  4192. }
  4193. }
  4194. return j;
  4195. }
  4196. /*==========================================
  4197. * Display item stolen msg to player sd
  4198. *------------------------------------------*/
  4199. int pc_show_steal(struct block_list *bl,va_list ap)
  4200. {
  4201. struct map_session_data *sd;
  4202. int itemid;
  4203. struct item_data *item=NULL;
  4204. char output[100];
  4205. sd=va_arg(ap,struct map_session_data *);
  4206. itemid=va_arg(ap,int);
  4207. if((item=itemdb_exists(itemid))==NULL)
  4208. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4209. else
  4210. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4211. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4212. return 0;
  4213. }
  4214. /*==========================================
  4215. * Steal an item from bl (mob).
  4216. * Return:
  4217. * 0 = fail
  4218. * 1 = succes
  4219. *------------------------------------------*/
  4220. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4221. {
  4222. int i,itemid,flag;
  4223. double rate;
  4224. struct status_data *sd_status, *md_status;
  4225. struct mob_data *md;
  4226. struct item tmp_item;
  4227. if(!sd || !bl || bl->type!=BL_MOB)
  4228. return 0;
  4229. md = (TBL_MOB *)bl;
  4230. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE ) ) //already stolen from / status change check
  4231. return 0;
  4232. sd_status= status_get_status_data(&sd->bl);
  4233. md_status= status_get_status_data(bl);
  4234. if( md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
  4235. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4236. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4237. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4238. ) { //Can't steal from
  4239. md->state.steal_flag = UCHAR_MAX;
  4240. return 0;
  4241. }
  4242. // base skill success chance (percentual)
  4243. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4244. rate += sd->bonus.add_steal_rate;
  4245. if( rate < 1 )
  4246. return 0;
  4247. // Try dropping one item, in the order from first to last possible slot.
  4248. // Droprate is affected by the skill success rate.
  4249. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4250. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4251. break;
  4252. if( i == MAX_STEAL_DROP )
  4253. return 0;
  4254. itemid = md->db->dropitem[i].nameid;
  4255. memset(&tmp_item,0,sizeof(tmp_item));
  4256. tmp_item.nameid = itemid;
  4257. tmp_item.amount = 1;
  4258. tmp_item.identify = itemdb_isidentified(itemid);
  4259. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4260. //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?
  4261. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4262. if(flag) { //Failed to steal due to overweight
  4263. clif_additem(sd,0,0,flag);
  4264. return 0;
  4265. }
  4266. if(battle_config.show_steal_in_same_party)
  4267. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4268. //Logs items, Stolen from mobs [Lupus]
  4269. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4270. //A Rare Steal Global Announce by Lupus
  4271. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4272. struct item_data *i_data;
  4273. char message[128];
  4274. i_data = itemdb_search(itemid);
  4275. 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);
  4276. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4277. intif_broadcast(message,strlen(message)+1,0);
  4278. }
  4279. return 1;
  4280. }
  4281. /*==========================================
  4282. * Stole zeny from bl (mob)
  4283. * return
  4284. * 0 = fail
  4285. * 1 = success
  4286. *------------------------------------------*/
  4287. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4288. {
  4289. int rate,skill;
  4290. struct mob_data *md;
  4291. if(!sd || !target || target->type != BL_MOB)
  4292. return 0;
  4293. md = (TBL_MOB*)target;
  4294. if( md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS )
  4295. return 0;
  4296. if( mob_is_treasure(md) )
  4297. return 0;
  4298. // FIXME: This formula is either custom or outdated.
  4299. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4300. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4301. if(rnd()%1000 < rate)
  4302. {
  4303. int amount = md->level*10 + rnd()%100;
  4304. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4305. md->state.steal_coin_flag = 1;
  4306. return 1;
  4307. }
  4308. return 0;
  4309. }
  4310. /*==========================================
  4311. * Set's a player position.
  4312. * Return values:
  4313. * 0 - Success.
  4314. * 1 - Invalid map index.
  4315. * 2 - Map not in this map-server, and failed to locate alternate map-server.
  4316. *------------------------------------------*/
  4317. int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4318. {
  4319. int16 m;
  4320. nullpo_ret(sd);
  4321. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4322. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4323. return 1;
  4324. }
  4325. if( pc_isdead(sd) ) { //Revive dead people before warping them
  4326. pc_setstand(sd);
  4327. pc_setrestartvalue(sd,1);
  4328. }
  4329. m = map_mapindex2mapid(mapindex);
  4330. sd->state.changemap = (sd->mapindex != mapindex);
  4331. sd->state.warping = 1;
  4332. if(sd->status.party_id && map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4333. struct party_data *p;
  4334. if((p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4335. instance_delusers(p->instance_id);
  4336. }
  4337. if( sd->state.changemap ) { // Misc map-changing settings
  4338. int i;
  4339. sd->state.pmap = sd->bl.m;
  4340. if (sd->sc.count) { // Cancel some map related stuff.
  4341. if (sd->sc.data[SC_JAILED])
  4342. return 1; //You may not get out!
  4343. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4344. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4345. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4346. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4347. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4348. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4349. if (sd->sc.data[SC_KNOWLEDGE]) {
  4350. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4351. if (sce->timer != INVALID_TIMER)
  4352. delete_timer(sce->timer, status_change_timer);
  4353. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4354. }
  4355. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4356. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4357. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4358. }
  4359. for( i = 0; i < EQI_MAX; i++ ) {
  4360. if( sd->equip_index[ i ] >= 0 )
  4361. if( !pc_isequip( sd , sd->equip_index[ i ] ) )
  4362. pc_unequipitem( sd , sd->equip_index[ i ] , 2 );
  4363. }
  4364. if (battle_config.clear_unit_onwarp&BL_PC)
  4365. skill_clear_unitgroup(&sd->bl);
  4366. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4367. guild_send_dot_remove(sd);
  4368. bg_send_dot_remove(sd);
  4369. if (sd->regen.state.gc)
  4370. sd->regen.state.gc = 0;
  4371. // make sure vending is allowed here
  4372. if (sd->state.vending && map[m].flag.novending) {
  4373. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4374. vending_closevending(sd);
  4375. }
  4376. channel_pcquit(sd,4); //quit map chan
  4377. }
  4378. if( m < 0 )
  4379. {
  4380. uint32 ip;
  4381. uint16 port;
  4382. //if can't find any map-servers, just abort setting position.
  4383. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4384. return 2;
  4385. if (sd->npc_id)
  4386. npc_event_dequeue(sd);
  4387. npc_script_event(sd, NPCE_LOGOUT);
  4388. //remove from map, THEN change x/y coordinates
  4389. unit_remove_map_pc(sd,clrtype);
  4390. sd->mapindex = mapindex;
  4391. sd->bl.x=x;
  4392. sd->bl.y=y;
  4393. pc_clean_skilltree(sd);
  4394. chrif_save(sd,2);
  4395. chrif_changemapserver(sd, ip, (short)port);
  4396. //Free session data from this map server [Kevin]
  4397. unit_free_pc(sd);
  4398. return 0;
  4399. }
  4400. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4401. {
  4402. 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);
  4403. x = y = 0; // make it random
  4404. }
  4405. if( x == 0 && y == 0 )
  4406. {// pick a random walkable cell
  4407. do {
  4408. x=rnd()%(map[m].xs-2)+1;
  4409. y=rnd()%(map[m].ys-2)+1;
  4410. } while(map_getcell(m,x,y,CELL_CHKNOPASS));
  4411. }
  4412. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4413. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4414. vending_closevending(sd);
  4415. }
  4416. if(sd->bl.prev != NULL){
  4417. unit_remove_map_pc(sd,clrtype);
  4418. clif_changemap(sd,m,x,y); // [MouseJstr]
  4419. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4420. sd->state.rewarp = 1;
  4421. sd->mapindex = mapindex;
  4422. sd->bl.m = m;
  4423. sd->bl.x = sd->ud.to_x = x;
  4424. sd->bl.y = sd->ud.to_y = y;
  4425. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4426. { // Increased guild castle regen [Valaris]
  4427. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4428. if(gc && gc->guild_id == sd->status.guild_id)
  4429. sd->regen.state.gc = 1;
  4430. }
  4431. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4432. {
  4433. sd->pd->bl.m = m;
  4434. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4435. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4436. sd->pd->ud.dir = sd->ud.dir;
  4437. }
  4438. if( merc_is_hom_active(sd->hd) )
  4439. {
  4440. sd->hd->bl.m = m;
  4441. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4442. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4443. sd->hd->ud.dir = sd->ud.dir;
  4444. }
  4445. if( sd->md )
  4446. {
  4447. sd->md->bl.m = m;
  4448. sd->md->bl.x = sd->md->ud.to_x = x;
  4449. sd->md->bl.y = sd->md->ud.to_y = y;
  4450. sd->md->ud.dir = sd->ud.dir;
  4451. }
  4452. return 0;
  4453. }
  4454. /*==========================================
  4455. * Warp player sd to random location on current map.
  4456. * May fail if no walkable cell found (1000 attempts).
  4457. * Return:
  4458. * 0 = fail or FIXME success (from pc_setpos)
  4459. * x(1|2) = fail
  4460. *------------------------------------------*/
  4461. int pc_randomwarp(struct map_session_data *sd, clr_type type)
  4462. {
  4463. int x,y,i=0;
  4464. int16 m;
  4465. nullpo_ret(sd);
  4466. m=sd->bl.m;
  4467. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4468. return 0;
  4469. do{
  4470. x=rnd()%(map[m].xs-2)+1;
  4471. y=rnd()%(map[m].ys-2)+1;
  4472. }while(map_getcell(m,x,y,CELL_CHKNOPASS) && (i++)<1000 );
  4473. if (i < 1000)
  4474. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4475. return 0;
  4476. }
  4477. /*==========================================
  4478. * Records a memo point at sd's current position
  4479. * pos - entry to replace, (-1: shift oldest entry out)
  4480. *------------------------------------------*/
  4481. int pc_memo(struct map_session_data* sd, int pos)
  4482. {
  4483. int skill;
  4484. nullpo_ret(sd);
  4485. // check mapflags
  4486. 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) ) {
  4487. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4488. return 0;
  4489. }
  4490. // check inputs
  4491. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4492. return 0; // invalid input
  4493. // check required skill level
  4494. skill = pc_checkskill(sd, AL_WARP);
  4495. if( skill < 1 ) {
  4496. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4497. return 0;
  4498. }
  4499. if( skill < 2 || skill - 2 < pos ) {
  4500. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4501. return 0;
  4502. }
  4503. if( pos == -1 )
  4504. {
  4505. int i;
  4506. // prevent memo-ing the same map multiple times
  4507. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4508. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4509. pos = 0;
  4510. }
  4511. if( map[sd->bl.m].instance_id ) {
  4512. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  4513. return 0;
  4514. }
  4515. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4516. sd->status.memo_point[pos].x = sd->bl.x;
  4517. sd->status.memo_point[pos].y = sd->bl.y;
  4518. clif_skill_memomessage(sd, 0);
  4519. return 1;
  4520. }
  4521. //
  4522. // Skills
  4523. //
  4524. /*==========================================
  4525. * Return player sd skill_lv learned for given skill
  4526. *------------------------------------------*/
  4527. int pc_checkskill(struct map_session_data *sd,uint16 skill_id)
  4528. {
  4529. if(sd == NULL) return 0;
  4530. if( skill_id >= GD_SKILLBASE && skill_id < GD_MAX ) {
  4531. struct guild *g;
  4532. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4533. return guild_checkskill(g,skill_id);
  4534. return 0;
  4535. } else if(skill_id >= ARRAYLENGTH(sd->status.skill) ) {
  4536. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4537. return 0;
  4538. }
  4539. if(sd->status.skill[skill_id].id == skill_id)
  4540. return (sd->status.skill[skill_id].lv);
  4541. return 0;
  4542. }
  4543. /*==========================================
  4544. * Chk if we still have the correct weapon to continue the skill (actually status)
  4545. * If not ending it
  4546. * Return
  4547. * 0 - No status found or all done
  4548. *------------------------------------------*/
  4549. int pc_checkallowskill(struct map_session_data *sd)
  4550. {
  4551. const enum sc_type scw_list[] = {
  4552. SC_TWOHANDQUICKEN,
  4553. SC_ONEHAND,
  4554. SC_AURABLADE,
  4555. SC_PARRYING,
  4556. SC_SPEARQUICKEN,
  4557. SC_ADRENALINE,
  4558. SC_ADRENALINE2,
  4559. SC_DANCING,
  4560. SC_GATLINGFEVER,
  4561. SC_FEARBREEZE
  4562. };
  4563. const enum sc_type scs_list[] = {
  4564. SC_AUTOGUARD,
  4565. SC_DEFENDER,
  4566. SC_REFLECTSHIELD,
  4567. SC_REFLECTDAMAGE
  4568. };
  4569. int i;
  4570. nullpo_ret(sd);
  4571. if(!sd->sc.count)
  4572. return 0;
  4573. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4574. { // Skills requiring specific weapon types
  4575. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4576. continue;
  4577. if(sd->sc.data[scw_list[i]] &&
  4578. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4579. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4580. }
  4581. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4582. // Spurt requires bare hands (feet, in fact xD)
  4583. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4584. if(sd->status.shield <= 0) { // Skills requiring a shield
  4585. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  4586. if(sd->sc.data[scs_list[i]])
  4587. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  4588. }
  4589. return 0;
  4590. }
  4591. /*==========================================
  4592. * Return equiped itemid? on player sd at pos
  4593. * Return
  4594. * -1 : mean nothing equiped
  4595. * idx : (this index could be used in inventory to found item_data)
  4596. *------------------------------------------*/
  4597. int pc_checkequip(struct map_session_data *sd,int pos)
  4598. {
  4599. int i;
  4600. nullpo_retr(-1, sd);
  4601. for(i=0;i<EQI_MAX;i++){
  4602. if(pos & equip_pos[i])
  4603. return sd->equip_index[i];
  4604. }
  4605. return -1;
  4606. }
  4607. /*==========================================
  4608. * Check if sd as nameid equiped somewhere
  4609. * -return true,false
  4610. *------------------------------------------*/
  4611. int pc_checkequip2(struct map_session_data *sd,int nameid){
  4612. int i;
  4613. for(i=0;i<EQI_MAX;i++){
  4614. if(equip_pos[i]){
  4615. int idx = sd->equip_index[i];
  4616. if (sd->status.inventory[idx].nameid == nameid)
  4617. return true;
  4618. }
  4619. }
  4620. return false;
  4621. }
  4622. /*==========================================
  4623. * Convert's from the client's lame Job ID system
  4624. * to the map server's 'makes sense' system. [Skotlex]
  4625. *------------------------------------------*/
  4626. int pc_jobid2mapid(unsigned short b_class)
  4627. {
  4628. switch(b_class)
  4629. {
  4630. //Novice And 1-1 Jobs
  4631. case JOB_NOVICE: return MAPID_NOVICE;
  4632. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  4633. case JOB_MAGE: return MAPID_MAGE;
  4634. case JOB_ARCHER: return MAPID_ARCHER;
  4635. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  4636. case JOB_MERCHANT: return MAPID_MERCHANT;
  4637. case JOB_THIEF: return MAPID_THIEF;
  4638. case JOB_TAEKWON: return MAPID_TAEKWON;
  4639. case JOB_WEDDING: return MAPID_WEDDING;
  4640. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  4641. case JOB_NINJA: return MAPID_NINJA;
  4642. case JOB_XMAS: return MAPID_XMAS;
  4643. case JOB_SUMMER: return MAPID_SUMMER;
  4644. case JOB_HANBOK: return MAPID_HANBOK;
  4645. case JOB_GANGSI: return MAPID_GANGSI;
  4646. //2-1 Jobs
  4647. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  4648. case JOB_KNIGHT: return MAPID_KNIGHT;
  4649. case JOB_WIZARD: return MAPID_WIZARD;
  4650. case JOB_HUNTER: return MAPID_HUNTER;
  4651. case JOB_PRIEST: return MAPID_PRIEST;
  4652. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  4653. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  4654. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  4655. case JOB_KAGEROU:
  4656. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  4657. case JOB_REBELLION: return MAPID_REBELLION;
  4658. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  4659. //2-2 Jobs
  4660. case JOB_CRUSADER: return MAPID_CRUSADER;
  4661. case JOB_SAGE: return MAPID_SAGE;
  4662. case JOB_BARD:
  4663. case JOB_DANCER: return MAPID_BARDDANCER;
  4664. case JOB_MONK: return MAPID_MONK;
  4665. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  4666. case JOB_ROGUE: return MAPID_ROGUE;
  4667. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  4668. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  4669. //Trans Novice And Trans 1-1 Jobs
  4670. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  4671. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  4672. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  4673. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  4674. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  4675. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  4676. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  4677. //Trans 2-1 Jobs
  4678. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  4679. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  4680. case JOB_SNIPER: return MAPID_SNIPER;
  4681. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  4682. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  4683. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  4684. //Trans 2-2 Jobs
  4685. case JOB_PALADIN: return MAPID_PALADIN;
  4686. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  4687. case JOB_CLOWN:
  4688. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  4689. case JOB_CHAMPION: return MAPID_CHAMPION;
  4690. case JOB_CREATOR: return MAPID_CREATOR;
  4691. case JOB_STALKER: return MAPID_STALKER;
  4692. //Baby Novice And Baby 1-1 Jobs
  4693. case JOB_BABY: return MAPID_BABY;
  4694. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  4695. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  4696. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  4697. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  4698. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  4699. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  4700. //Baby 2-1 Jobs
  4701. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  4702. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  4703. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  4704. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  4705. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  4706. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  4707. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  4708. //Baby 2-2 Jobs
  4709. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  4710. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  4711. case JOB_BABY_BARD:
  4712. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  4713. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  4714. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  4715. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  4716. //3-1 Jobs
  4717. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  4718. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  4719. case JOB_WARLOCK: return MAPID_WARLOCK;
  4720. case JOB_RANGER: return MAPID_RANGER;
  4721. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  4722. case JOB_MECHANIC: return MAPID_MECHANIC;
  4723. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  4724. //3-2 Jobs
  4725. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  4726. case JOB_SORCERER: return MAPID_SORCERER;
  4727. case JOB_MINSTREL:
  4728. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  4729. case JOB_SURA: return MAPID_SURA;
  4730. case JOB_GENETIC: return MAPID_GENETIC;
  4731. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  4732. //Trans 3-1 Jobs
  4733. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  4734. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  4735. case JOB_RANGER_T: return MAPID_RANGER_T;
  4736. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  4737. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  4738. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  4739. //Trans 3-2 Jobs
  4740. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  4741. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  4742. case JOB_MINSTREL_T:
  4743. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  4744. case JOB_SURA_T: return MAPID_SURA_T;
  4745. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  4746. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  4747. //Baby 3-1 Jobs
  4748. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  4749. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  4750. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  4751. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  4752. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  4753. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  4754. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  4755. //Baby 3-2 Jobs
  4756. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  4757. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  4758. case JOB_BABY_MINSTREL:
  4759. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  4760. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  4761. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  4762. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  4763. default:
  4764. return -1;
  4765. }
  4766. }
  4767. //Reverts the map-style class id to the client-style one.
  4768. int pc_mapid2jobid(unsigned short class_, int sex)
  4769. {
  4770. switch(class_)
  4771. {
  4772. //Novice And 1-1 Jobs
  4773. case MAPID_NOVICE: return JOB_NOVICE;
  4774. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  4775. case MAPID_MAGE: return JOB_MAGE;
  4776. case MAPID_ARCHER: return JOB_ARCHER;
  4777. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  4778. case MAPID_MERCHANT: return JOB_MERCHANT;
  4779. case MAPID_THIEF: return JOB_THIEF;
  4780. case MAPID_TAEKWON: return JOB_TAEKWON;
  4781. case MAPID_WEDDING: return JOB_WEDDING;
  4782. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  4783. case MAPID_NINJA: return JOB_NINJA;
  4784. case MAPID_XMAS: return JOB_XMAS;
  4785. case MAPID_SUMMER: return JOB_SUMMER;
  4786. case MAPID_HANBOK: return JOB_HANBOK;
  4787. case MAPID_GANGSI: return JOB_GANGSI;
  4788. //2-1 Jobs
  4789. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  4790. case MAPID_KNIGHT: return JOB_KNIGHT;
  4791. case MAPID_WIZARD: return JOB_WIZARD;
  4792. case MAPID_HUNTER: return JOB_HUNTER;
  4793. case MAPID_PRIEST: return JOB_PRIEST;
  4794. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  4795. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  4796. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  4797. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  4798. case MAPID_REBELLION: return JOB_REBELLION;
  4799. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  4800. //2-2 Jobs
  4801. case MAPID_CRUSADER: return JOB_CRUSADER;
  4802. case MAPID_SAGE: return JOB_SAGE;
  4803. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  4804. case MAPID_MONK: return JOB_MONK;
  4805. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  4806. case MAPID_ROGUE: return JOB_ROGUE;
  4807. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  4808. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  4809. //Trans Novice And Trans 2-1 Jobs
  4810. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  4811. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  4812. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  4813. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  4814. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  4815. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  4816. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  4817. //Trans 2-1 Jobs
  4818. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  4819. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  4820. case MAPID_SNIPER: return JOB_SNIPER;
  4821. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  4822. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  4823. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  4824. //Trans 2-2 Jobs
  4825. case MAPID_PALADIN: return JOB_PALADIN;
  4826. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  4827. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  4828. case MAPID_CHAMPION: return JOB_CHAMPION;
  4829. case MAPID_CREATOR: return JOB_CREATOR;
  4830. case MAPID_STALKER: return JOB_STALKER;
  4831. //Baby Novice And Baby 1-1 Jobs
  4832. case MAPID_BABY: return JOB_BABY;
  4833. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  4834. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  4835. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  4836. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  4837. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  4838. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  4839. //Baby 2-1 Jobs
  4840. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  4841. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  4842. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  4843. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  4844. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  4845. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  4846. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  4847. //Baby 2-2 Jobs
  4848. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  4849. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  4850. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  4851. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  4852. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  4853. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  4854. //3-1 Jobs
  4855. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  4856. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  4857. case MAPID_WARLOCK: return JOB_WARLOCK;
  4858. case MAPID_RANGER: return JOB_RANGER;
  4859. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  4860. case MAPID_MECHANIC: return JOB_MECHANIC;
  4861. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  4862. //3-2 Jobs
  4863. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  4864. case MAPID_SORCERER: return JOB_SORCERER;
  4865. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  4866. case MAPID_SURA: return JOB_SURA;
  4867. case MAPID_GENETIC: return JOB_GENETIC;
  4868. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  4869. //Trans 3-1 Jobs
  4870. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  4871. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  4872. case MAPID_RANGER_T: return JOB_RANGER_T;
  4873. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  4874. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  4875. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  4876. //Trans 3-2 Jobs
  4877. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  4878. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  4879. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  4880. case MAPID_SURA_T: return JOB_SURA_T;
  4881. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  4882. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  4883. //Baby 3-1 Jobs
  4884. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  4885. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  4886. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  4887. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  4888. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  4889. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  4890. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  4891. //Baby 3-2 Jobs
  4892. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  4893. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  4894. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  4895. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  4896. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  4897. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  4898. default:
  4899. return -1;
  4900. }
  4901. }
  4902. /*====================================================
  4903. * This function return the name of the job (by [Yor])
  4904. *----------------------------------------------------*/
  4905. const char* job_name(int class_)
  4906. {
  4907. switch (class_) {
  4908. case JOB_NOVICE:
  4909. case JOB_SWORDMAN:
  4910. case JOB_MAGE:
  4911. case JOB_ARCHER:
  4912. case JOB_ACOLYTE:
  4913. case JOB_MERCHANT:
  4914. case JOB_THIEF:
  4915. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  4916. case JOB_KNIGHT:
  4917. case JOB_PRIEST:
  4918. case JOB_WIZARD:
  4919. case JOB_BLACKSMITH:
  4920. case JOB_HUNTER:
  4921. case JOB_ASSASSIN:
  4922. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  4923. case JOB_KNIGHT2:
  4924. return msg_txt(NULL,557);
  4925. case JOB_CRUSADER:
  4926. case JOB_MONK:
  4927. case JOB_SAGE:
  4928. case JOB_ROGUE:
  4929. case JOB_ALCHEMIST:
  4930. case JOB_BARD:
  4931. case JOB_DANCER:
  4932. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  4933. case JOB_CRUSADER2:
  4934. return msg_txt(NULL,563);
  4935. case JOB_WEDDING:
  4936. case JOB_SUPER_NOVICE:
  4937. case JOB_GUNSLINGER:
  4938. case JOB_NINJA:
  4939. case JOB_XMAS:
  4940. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  4941. case JOB_SUMMER:
  4942. return msg_txt(NULL,621);
  4943. case JOB_HANBOK:
  4944. return msg_txt(NULL,694);
  4945. case JOB_NOVICE_HIGH:
  4946. case JOB_SWORDMAN_HIGH:
  4947. case JOB_MAGE_HIGH:
  4948. case JOB_ARCHER_HIGH:
  4949. case JOB_ACOLYTE_HIGH:
  4950. case JOB_MERCHANT_HIGH:
  4951. case JOB_THIEF_HIGH:
  4952. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  4953. case JOB_LORD_KNIGHT:
  4954. case JOB_HIGH_PRIEST:
  4955. case JOB_HIGH_WIZARD:
  4956. case JOB_WHITESMITH:
  4957. case JOB_SNIPER:
  4958. case JOB_ASSASSIN_CROSS:
  4959. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  4960. case JOB_LORD_KNIGHT2:
  4961. return msg_txt(NULL,582);
  4962. case JOB_PALADIN:
  4963. case JOB_CHAMPION:
  4964. case JOB_PROFESSOR:
  4965. case JOB_STALKER:
  4966. case JOB_CREATOR:
  4967. case JOB_CLOWN:
  4968. case JOB_GYPSY:
  4969. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  4970. case JOB_PALADIN2:
  4971. return msg_txt(NULL,588);
  4972. case JOB_BABY:
  4973. case JOB_BABY_SWORDMAN:
  4974. case JOB_BABY_MAGE:
  4975. case JOB_BABY_ARCHER:
  4976. case JOB_BABY_ACOLYTE:
  4977. case JOB_BABY_MERCHANT:
  4978. case JOB_BABY_THIEF:
  4979. return msg_txt(NULL,595 - JOB_BABY + class_);
  4980. case JOB_BABY_KNIGHT:
  4981. case JOB_BABY_PRIEST:
  4982. case JOB_BABY_WIZARD:
  4983. case JOB_BABY_BLACKSMITH:
  4984. case JOB_BABY_HUNTER:
  4985. case JOB_BABY_ASSASSIN:
  4986. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  4987. case JOB_BABY_KNIGHT2:
  4988. return msg_txt(NULL,602);
  4989. case JOB_BABY_CRUSADER:
  4990. case JOB_BABY_MONK:
  4991. case JOB_BABY_SAGE:
  4992. case JOB_BABY_ROGUE:
  4993. case JOB_BABY_ALCHEMIST:
  4994. case JOB_BABY_BARD:
  4995. case JOB_BABY_DANCER:
  4996. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  4997. case JOB_BABY_CRUSADER2:
  4998. return msg_txt(NULL,608);
  4999. case JOB_SUPER_BABY:
  5000. return msg_txt(NULL,615);
  5001. case JOB_TAEKWON:
  5002. return msg_txt(NULL,616);
  5003. case JOB_STAR_GLADIATOR:
  5004. case JOB_STAR_GLADIATOR2:
  5005. return msg_txt(NULL,617);
  5006. case JOB_SOUL_LINKER:
  5007. return msg_txt(NULL,618);
  5008. case JOB_GANGSI:
  5009. case JOB_DEATH_KNIGHT:
  5010. case JOB_DARK_COLLECTOR:
  5011. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5012. case JOB_RUNE_KNIGHT:
  5013. case JOB_WARLOCK:
  5014. case JOB_RANGER:
  5015. case JOB_ARCH_BISHOP:
  5016. case JOB_MECHANIC:
  5017. case JOB_GUILLOTINE_CROSS:
  5018. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5019. case JOB_RUNE_KNIGHT_T:
  5020. case JOB_WARLOCK_T:
  5021. case JOB_RANGER_T:
  5022. case JOB_ARCH_BISHOP_T:
  5023. case JOB_MECHANIC_T:
  5024. case JOB_GUILLOTINE_CROSS_T:
  5025. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5026. case JOB_ROYAL_GUARD:
  5027. case JOB_SORCERER:
  5028. case JOB_MINSTREL:
  5029. case JOB_WANDERER:
  5030. case JOB_SURA:
  5031. case JOB_GENETIC:
  5032. case JOB_SHADOW_CHASER:
  5033. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5034. case JOB_ROYAL_GUARD_T:
  5035. case JOB_SORCERER_T:
  5036. case JOB_MINSTREL_T:
  5037. case JOB_WANDERER_T:
  5038. case JOB_SURA_T:
  5039. case JOB_GENETIC_T:
  5040. case JOB_SHADOW_CHASER_T:
  5041. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5042. case JOB_RUNE_KNIGHT2:
  5043. case JOB_RUNE_KNIGHT_T2:
  5044. return msg_txt(NULL,625);
  5045. case JOB_ROYAL_GUARD2:
  5046. case JOB_ROYAL_GUARD_T2:
  5047. return msg_txt(NULL,631);
  5048. case JOB_RANGER2:
  5049. case JOB_RANGER_T2:
  5050. return msg_txt(NULL,627);
  5051. case JOB_MECHANIC2:
  5052. case JOB_MECHANIC_T2:
  5053. return msg_txt(NULL,629);
  5054. case JOB_BABY_RUNE:
  5055. case JOB_BABY_WARLOCK:
  5056. case JOB_BABY_RANGER:
  5057. case JOB_BABY_BISHOP:
  5058. case JOB_BABY_MECHANIC:
  5059. case JOB_BABY_CROSS:
  5060. case JOB_BABY_GUARD:
  5061. case JOB_BABY_SORCERER:
  5062. case JOB_BABY_MINSTREL:
  5063. case JOB_BABY_WANDERER:
  5064. case JOB_BABY_SURA:
  5065. case JOB_BABY_GENETIC:
  5066. case JOB_BABY_CHASER:
  5067. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5068. case JOB_BABY_RUNE2:
  5069. return msg_txt(NULL,638);
  5070. case JOB_BABY_GUARD2:
  5071. return msg_txt(NULL,644);
  5072. case JOB_BABY_RANGER2:
  5073. return msg_txt(NULL,640);
  5074. case JOB_BABY_MECHANIC2:
  5075. return msg_txt(NULL,642);
  5076. case JOB_SUPER_NOVICE_E:
  5077. case JOB_SUPER_BABY_E:
  5078. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5079. case JOB_KAGEROU:
  5080. case JOB_OBORO:
  5081. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5082. case JOB_REBELLION:
  5083. return msg_txt(NULL,695);
  5084. default:
  5085. return msg_txt(NULL,655);
  5086. }
  5087. }
  5088. /*====================================================
  5089. * Timered function to make id follow a target.
  5090. * @id = bl.id (player only atm)
  5091. * target is define in sd->followtarget (bl.id)
  5092. * used by pc_follow
  5093. *----------------------------------------------------*/
  5094. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5095. {
  5096. struct map_session_data *sd;
  5097. struct block_list *tbl;
  5098. sd = map_id2sd(id);
  5099. nullpo_ret(sd);
  5100. if (sd->followtimer != tid){
  5101. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5102. sd->followtimer = INVALID_TIMER;
  5103. return 0;
  5104. }
  5105. sd->followtimer = INVALID_TIMER;
  5106. tbl = map_id2bl(sd->followtarget);
  5107. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5108. {
  5109. pc_stop_following(sd);
  5110. return 0;
  5111. }
  5112. // either player or target is currently detached from map blocks (could be teleporting),
  5113. // but still connected to this map, so we'll just increment the timer and check back later
  5114. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5115. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5116. {
  5117. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5118. if (!check_distance_bl(&sd->bl, tbl, 5))
  5119. unit_walktobl(&sd->bl, tbl, 5, 0);
  5120. } else
  5121. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5122. }
  5123. sd->followtimer = add_timer(
  5124. tick + 1000, // increase time a bit to loosen up map's load
  5125. pc_follow_timer, sd->bl.id, 0);
  5126. return 0;
  5127. }
  5128. int pc_stop_following (struct map_session_data *sd)
  5129. {
  5130. nullpo_ret(sd);
  5131. if (sd->followtimer != INVALID_TIMER) {
  5132. delete_timer(sd->followtimer,pc_follow_timer);
  5133. sd->followtimer = INVALID_TIMER;
  5134. }
  5135. sd->followtarget = -1;
  5136. sd->ud.target_to = 0;
  5137. unit_stop_walking(&sd->bl, 1);
  5138. return 0;
  5139. }
  5140. int pc_follow(struct map_session_data *sd,int target_id)
  5141. {
  5142. struct block_list *bl = map_id2bl(target_id);
  5143. if (bl == NULL /*|| bl->type != BL_PC*/)
  5144. return 1;
  5145. if (sd->followtimer != INVALID_TIMER)
  5146. pc_stop_following(sd);
  5147. sd->followtarget = target_id;
  5148. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5149. return 0;
  5150. }
  5151. int pc_checkbaselevelup(struct map_session_data *sd) {
  5152. unsigned int next = pc_nextbaseexp(sd);
  5153. if (!next || sd->status.base_exp < next)
  5154. return 0;
  5155. do {
  5156. sd->status.base_exp -= next;
  5157. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5158. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5159. sd->status.base_exp = next-1;
  5160. next = pc_gets_status_point(sd->status.base_level);
  5161. sd->status.base_level ++;
  5162. sd->status.status_point += next;
  5163. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5164. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5165. status_calc_pet(sd->pd,0);
  5166. clif_updatestatus(sd,SP_STATUSPOINT);
  5167. clif_updatestatus(sd,SP_BASELEVEL);
  5168. clif_updatestatus(sd,SP_BASEEXP);
  5169. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5170. status_calc_pc(sd,0);
  5171. status_percent_heal(&sd->bl,100,100);
  5172. if((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5173. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5174. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5175. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5176. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5177. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5178. if (sd->state.snovice_dead_flag)
  5179. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5180. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5181. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5182. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5183. }
  5184. clif_misceffect(&sd->bl,0);
  5185. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5186. if(sd->status.party_id)
  5187. party_send_levelup(sd);
  5188. pc_baselevelchanged(sd);
  5189. return 1;
  5190. }
  5191. void pc_baselevelchanged(struct map_session_data *sd) {
  5192. #ifdef RENEWAL
  5193. int i;
  5194. for( i = 0; i < EQI_MAX; i++ ) {
  5195. if( sd->equip_index[i] >= 0 ) {
  5196. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5197. pc_unequipitem(sd, sd->equip_index[i], 3);
  5198. }
  5199. }
  5200. #endif
  5201. }
  5202. int pc_checkjoblevelup(struct map_session_data *sd)
  5203. {
  5204. unsigned int next = pc_nextjobexp(sd);
  5205. nullpo_ret(sd);
  5206. if(!next || sd->status.job_exp < next)
  5207. return 0;
  5208. do {
  5209. sd->status.job_exp -= next;
  5210. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5211. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5212. sd->status.job_exp = next-1;
  5213. sd->status.job_level ++;
  5214. sd->status.skill_point ++;
  5215. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5216. clif_updatestatus(sd,SP_JOBLEVEL);
  5217. clif_updatestatus(sd,SP_JOBEXP);
  5218. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5219. clif_updatestatus(sd,SP_SKILLPOINT);
  5220. status_calc_pc(sd,0);
  5221. clif_misceffect(&sd->bl,1);
  5222. if (pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd))
  5223. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5224. npc_script_event(sd, NPCE_JOBLVUP);
  5225. return 1;
  5226. }
  5227. /*==========================================
  5228. * Alters experienced based on self bonuses that do not get even shared to the party.
  5229. *------------------------------------------*/
  5230. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5231. {
  5232. int bonus = 0;
  5233. struct status_data *status = status_get_status_data(src);
  5234. if (sd->expaddrace[status->race])
  5235. bonus += sd->expaddrace[status->race];
  5236. bonus += sd->expaddrace[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  5237. if (battle_config.pk_mode &&
  5238. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5239. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5240. if (sd->sc.data[SC_EXPBOOST]) {
  5241. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5242. if( battle_config.vip_bm_increase && pc_isvip(sd) ) // Increase Battle Manual EXP rate for VIP.
  5243. bonus += ( sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase );
  5244. }
  5245. *base_exp = (unsigned int) cap_value(*base_exp + (double)*base_exp * bonus/100., 1, UINT_MAX);
  5246. if (sd->sc.data[SC_JEXPBOOST])
  5247. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5248. *job_exp = (unsigned int) cap_value(*job_exp + (double)*job_exp * bonus/100., 1, UINT_MAX);
  5249. return;
  5250. }
  5251. /*==========================================
  5252. * Give x exp at sd player and calculate remaining exp for next lvl
  5253. *------------------------------------------*/
  5254. int pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp,unsigned int job_exp,bool quest)
  5255. {
  5256. float nextbp=0, nextjp=0;
  5257. unsigned int nextb=0, nextj=0;
  5258. nullpo_ret(sd);
  5259. if(sd->bl.prev == NULL || pc_isdead(sd))
  5260. return 0;
  5261. if(!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5262. return 0; // no exp on pvp maps
  5263. if(sd->status.guild_id>0)
  5264. base_exp-=guild_payexp(sd,base_exp);
  5265. if(src) pc_calcexp(sd, &base_exp, &job_exp, src);
  5266. nextb = pc_nextbaseexp(sd);
  5267. nextj = pc_nextjobexp(sd);
  5268. if(sd->state.showexp || battle_config.max_exp_gain_rate){
  5269. if (nextb > 0)
  5270. nextbp = (float) base_exp / (float) nextb;
  5271. if (nextj > 0)
  5272. nextjp = (float) job_exp / (float) nextj;
  5273. if(battle_config.max_exp_gain_rate) {
  5274. if (nextbp > battle_config.max_exp_gain_rate/1000.) {
  5275. //Note that this value should never be greater than the original
  5276. //base_exp, therefore no overflow checks are needed. [Skotlex]
  5277. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5278. if (sd->state.showexp)
  5279. nextbp = (float) base_exp / (float) nextb;
  5280. }
  5281. if (nextjp > battle_config.max_exp_gain_rate/1000.) {
  5282. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5283. if (sd->state.showexp)
  5284. nextjp = (float) job_exp / (float) nextj;
  5285. }
  5286. }
  5287. }
  5288. //Cap exp to the level up requirement of the previous level when you are at max level, otherwise cap at UINT_MAX (this is required for some S. Novice bonuses). [Skotlex]
  5289. if (base_exp) {
  5290. nextb = nextb?UINT_MAX:pc_thisbaseexp(sd);
  5291. if(sd->status.base_exp > nextb - base_exp)
  5292. sd->status.base_exp = nextb;
  5293. else
  5294. sd->status.base_exp += base_exp;
  5295. pc_checkbaselevelup(sd);
  5296. clif_updatestatus(sd,SP_BASEEXP);
  5297. }
  5298. if (job_exp) {
  5299. nextj = nextj?UINT_MAX:pc_thisjobexp(sd);
  5300. if(sd->status.job_exp > nextj - job_exp)
  5301. sd->status.job_exp = nextj;
  5302. else
  5303. sd->status.job_exp += job_exp;
  5304. pc_checkjoblevelup(sd);
  5305. clif_updatestatus(sd,SP_JOBEXP);
  5306. }
  5307. if(base_exp)
  5308. clif_displayexp(sd, base_exp, SP_BASEEXP, quest);
  5309. if(job_exp)
  5310. clif_displayexp(sd, job_exp, SP_JOBEXP, quest);
  5311. if(sd->state.showexp) {
  5312. char output[256];
  5313. sprintf(output,
  5314. "Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
  5315. clif_disp_onlyself(sd,output,strlen(output));
  5316. }
  5317. return 1;
  5318. }
  5319. /*==========================================
  5320. * Returns max level for this character.
  5321. *------------------------------------------*/
  5322. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5323. return job_info[pc_class2idx(sd->status.class_)].max_level[0];
  5324. }
  5325. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5326. return job_info[pc_class2idx(sd->status.class_)].max_level[1];
  5327. }
  5328. /*==========================================
  5329. * base level exp lookup.
  5330. *------------------------------------------*/
  5331. //Base exp needed for next level.
  5332. unsigned int pc_nextbaseexp(struct map_session_data *sd)
  5333. {
  5334. nullpo_ret(sd);
  5335. if(sd->status.base_level>=pc_maxbaselv(sd) || sd->status.base_level<=0)
  5336. return 0;
  5337. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  5338. }
  5339. //Base exp needed for this level.
  5340. unsigned int pc_thisbaseexp(struct map_session_data *sd)
  5341. {
  5342. if(sd->status.base_level>pc_maxbaselv(sd) || sd->status.base_level<=1)
  5343. return 0;
  5344. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-2];
  5345. }
  5346. /*==========================================
  5347. * job level exp lookup
  5348. * Return:
  5349. * 0 = not found
  5350. * x = exp for level
  5351. *------------------------------------------*/
  5352. //Job exp needed for next level.
  5353. unsigned int pc_nextjobexp(struct map_session_data *sd)
  5354. {
  5355. nullpo_ret(sd);
  5356. if(sd->status.job_level>=pc_maxjoblv(sd) || sd->status.job_level<=0)
  5357. return 0;
  5358. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  5359. }
  5360. //Job exp needed for this level.
  5361. unsigned int pc_thisjobexp(struct map_session_data *sd)
  5362. {
  5363. if(sd->status.job_level>pc_maxjoblv(sd) || sd->status.job_level<=1)
  5364. return 0;
  5365. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-2];
  5366. }
  5367. /// Returns the value of the specified stat.
  5368. static int pc_getstat(struct map_session_data* sd, int type)
  5369. {
  5370. nullpo_retr(-1, sd);
  5371. switch( type ) {
  5372. case SP_STR: return sd->status.str;
  5373. case SP_AGI: return sd->status.agi;
  5374. case SP_VIT: return sd->status.vit;
  5375. case SP_INT: return sd->status.int_;
  5376. case SP_DEX: return sd->status.dex;
  5377. case SP_LUK: return sd->status.luk;
  5378. default:
  5379. return -1;
  5380. }
  5381. }
  5382. /// Sets the specified stat to the specified value.
  5383. /// Returns the new value.
  5384. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5385. {
  5386. nullpo_retr(-1, sd);
  5387. switch( type ) {
  5388. case SP_STR: sd->status.str = val; break;
  5389. case SP_AGI: sd->status.agi = val; break;
  5390. case SP_VIT: sd->status.vit = val; break;
  5391. case SP_INT: sd->status.int_ = val; break;
  5392. case SP_DEX: sd->status.dex = val; break;
  5393. case SP_LUK: sd->status.luk = val; break;
  5394. default:
  5395. return -1;
  5396. }
  5397. return val;
  5398. }
  5399. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5400. int pc_gets_status_point(int level)
  5401. {
  5402. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5403. return (statp[level+1] - statp[level]);
  5404. else //Default increase
  5405. return ((level+15) / 5);
  5406. }
  5407. /// Returns the number of stat points needed to change the specified stat by val.
  5408. /// If val is negative, returns the number of stat points that would be needed to
  5409. /// raise the specified stat from (current value - val) to current value.
  5410. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5411. {
  5412. int low, high, sp = 0;
  5413. if ( val == 0 )
  5414. return 0;
  5415. low = pc_getstat(sd,type);
  5416. if ( low >= pc_maxparameter(sd) && val > 0 )
  5417. return 0; // Official servers show '0' when max is reached
  5418. high = low + val;
  5419. if ( val < 0 )
  5420. swap(low, high);
  5421. for ( ; low < high; low++ )
  5422. #ifdef RENEWAL // renewal status point cost formula
  5423. sp += (low < 100) ? (2 + (low - 1) / 10) : (16 + 4 * ((low - 100) / 5));
  5424. #else
  5425. sp += ( 1 + (low + 9) / 10 );
  5426. #endif
  5427. return sp;
  5428. }
  5429. /// Raises a stat by 1.
  5430. /// Obeys max_parameter limits.
  5431. /// Subtracts stat points.
  5432. ///
  5433. /// @param type The stat to change (see enum _sp)
  5434. int pc_statusup(struct map_session_data* sd, int type)
  5435. {
  5436. int max, need, val;
  5437. nullpo_ret(sd);
  5438. // check conditions
  5439. need = pc_need_status_point(sd,type,1);
  5440. if( type < SP_STR || type > SP_LUK || need < 0 || need > sd->status.status_point )
  5441. {
  5442. clif_statusupack(sd,type,0,0);
  5443. return 1;
  5444. }
  5445. // check limits
  5446. max = pc_maxparameter(sd);
  5447. if( pc_getstat(sd,type) >= max )
  5448. {
  5449. clif_statusupack(sd,type,0,0);
  5450. return 1;
  5451. }
  5452. // set new values
  5453. val = pc_setstat(sd, type, pc_getstat(sd,type) + 1);
  5454. sd->status.status_point -= need;
  5455. status_calc_pc(sd,0);
  5456. // update increase cost indicator
  5457. if( need != pc_need_status_point(sd,type,1) )
  5458. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5459. // update statpoint count
  5460. clif_updatestatus(sd,SP_STATUSPOINT);
  5461. // update stat value
  5462. clif_statusupack(sd,type,1,val); // required
  5463. if( val > 255 )
  5464. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5465. return 0;
  5466. }
  5467. /// Raises a stat by the specified amount.
  5468. /// Obeys max_parameter limits.
  5469. /// Does not subtract stat points.
  5470. ///
  5471. /// @param type The stat to change (see enum _sp)
  5472. /// @param val The stat increase amount.
  5473. int pc_statusup2(struct map_session_data* sd, int type, int val)
  5474. {
  5475. int max, need;
  5476. nullpo_ret(sd);
  5477. if( type < SP_STR || type > SP_LUK )
  5478. {
  5479. clif_statusupack(sd,type,0,0);
  5480. return 1;
  5481. }
  5482. need = pc_need_status_point(sd,type,1);
  5483. // set new value
  5484. max = pc_maxparameter(sd);
  5485. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  5486. status_calc_pc(sd,0);
  5487. // update increase cost indicator
  5488. if( need != pc_need_status_point(sd,type,1) )
  5489. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5490. // update stat value
  5491. clif_statusupack(sd,type,1,val); // required
  5492. if( val > 255 )
  5493. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5494. return 0;
  5495. }
  5496. /*==========================================
  5497. * Update skill_lv for player sd
  5498. * Skill point allocation
  5499. *------------------------------------------*/
  5500. int pc_skillup(struct map_session_data *sd,uint16 skill_id)
  5501. {
  5502. nullpo_ret(sd);
  5503. if( skill_id >= GD_SKILLBASE && skill_id < GD_SKILLBASE+MAX_GUILDSKILL )
  5504. {
  5505. guild_skillup(sd, skill_id);
  5506. return 0;
  5507. }
  5508. if( skill_id >= HM_SKILLBASE && skill_id < HM_SKILLBASE+MAX_HOMUNSKILL && sd->hd )
  5509. {
  5510. merc_hom_skillup(sd->hd, skill_id);
  5511. return 0;
  5512. }
  5513. if(skill_id >= MAX_SKILL )
  5514. return 0;
  5515. if( sd->status.skill_point > 0 &&
  5516. sd->status.skill[skill_id].id &&
  5517. sd->status.skill[skill_id].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  5518. sd->status.skill[skill_id].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  5519. {
  5520. int lv,range, upgradable;
  5521. sd->status.skill[skill_id].lv++;
  5522. sd->status.skill_point--;
  5523. if( !skill_get_inf(skill_id) )
  5524. status_calc_pc(sd,0); // Only recalculate for passive skills.
  5525. else if( sd->status.skill_point == 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5526. pc_calc_skilltree(sd); // Required to grant all TK Ranger skills.
  5527. else
  5528. pc_check_skilltree(sd, skill_id); // Check if a new skill can Lvlup
  5529. lv = sd->status.skill[skill_id].lv;
  5530. range = skill_get_range2(&sd->bl, skill_id, lv);
  5531. upgradable = (lv < skill_tree_get_max(sd->status.skill[skill_id].id, sd->status.class_)) ? 1 : 0;
  5532. clif_skillup(sd,skill_id,lv,range,upgradable);
  5533. clif_updatestatus(sd,SP_SKILLPOINT);
  5534. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  5535. clif_updatestatus(sd,SP_CARTINFO);
  5536. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  5537. clif_skillinfoblock(sd);
  5538. }
  5539. return 0;
  5540. }
  5541. /*==========================================
  5542. * /allskill
  5543. *------------------------------------------*/
  5544. int pc_allskillup(struct map_session_data *sd)
  5545. {
  5546. int i,id;
  5547. nullpo_ret(sd);
  5548. for(i=0;i<MAX_SKILL;i++){
  5549. 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) {
  5550. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  5551. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5552. if (sd->status.skill[i].lv == 0)
  5553. sd->status.skill[i].id = 0;
  5554. }
  5555. }
  5556. if (pc_has_permission(sd, PC_PERM_ALL_SKILL))
  5557. { //Get ALL skills except npc/guild ones. [Skotlex]
  5558. //and except SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  5559. for(i=0;i<MAX_SKILL;i++){
  5560. switch( i ) {
  5561. case SG_DEVIL:
  5562. case MO_TRIPLEATTACK:
  5563. case RG_SNATCHER:
  5564. continue;
  5565. default:
  5566. if( !(skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)) )
  5567. if ( ( sd->status.skill[i].lv = skill_get_max(i) ) )//Nonexistant skills should return a max of 0 anyway.
  5568. sd->status.skill[i].id = i;
  5569. }
  5570. }
  5571. } else {
  5572. int inf2;
  5573. for(i=0;i < MAX_SKILL_TREE && (id=skill_tree[pc_class2idx(sd->status.class_)][i].id)>0;i++){
  5574. inf2 = skill_get_inf2(id);
  5575. if (
  5576. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  5577. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  5578. id==SG_DEVIL
  5579. )
  5580. continue; //Cannot be learned normally.
  5581. sd->status.skill[id].id = id;
  5582. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_); // celest
  5583. }
  5584. }
  5585. status_calc_pc(sd,0);
  5586. //Required because if you could level up all skills previously,
  5587. //the update will not be sent as only the lv variable changes.
  5588. clif_skillinfoblock(sd);
  5589. return 0;
  5590. }
  5591. /*==========================================
  5592. * /resetlvl
  5593. *------------------------------------------*/
  5594. int pc_resetlvl(struct map_session_data* sd,int type)
  5595. {
  5596. int i;
  5597. nullpo_ret(sd);
  5598. if (type != 3) //Also reset skills
  5599. pc_resetskill(sd, 0);
  5600. if(type == 1){
  5601. sd->status.skill_point=0;
  5602. sd->status.base_level=1;
  5603. sd->status.job_level=1;
  5604. sd->status.base_exp=0;
  5605. sd->status.job_exp=0;
  5606. if(sd->sc.option !=0)
  5607. sd->sc.option = 0;
  5608. sd->status.str=1;
  5609. sd->status.agi=1;
  5610. sd->status.vit=1;
  5611. sd->status.int_=1;
  5612. sd->status.dex=1;
  5613. sd->status.luk=1;
  5614. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  5615. sd->status.status_point=100; // not 88 [celest]
  5616. // give platinum skills upon changing
  5617. pc_skill(sd,142,1,0);
  5618. pc_skill(sd,143,1,0);
  5619. }
  5620. }
  5621. if(type == 2){
  5622. sd->status.skill_point=0;
  5623. sd->status.base_level=1;
  5624. sd->status.job_level=1;
  5625. sd->status.base_exp=0;
  5626. sd->status.job_exp=0;
  5627. }
  5628. if(type == 3){
  5629. sd->status.base_level=1;
  5630. sd->status.base_exp=0;
  5631. }
  5632. if(type == 4){
  5633. sd->status.job_level=1;
  5634. sd->status.job_exp=0;
  5635. }
  5636. clif_updatestatus(sd,SP_STATUSPOINT);
  5637. clif_updatestatus(sd,SP_STR);
  5638. clif_updatestatus(sd,SP_AGI);
  5639. clif_updatestatus(sd,SP_VIT);
  5640. clif_updatestatus(sd,SP_INT);
  5641. clif_updatestatus(sd,SP_DEX);
  5642. clif_updatestatus(sd,SP_LUK);
  5643. clif_updatestatus(sd,SP_BASELEVEL);
  5644. clif_updatestatus(sd,SP_JOBLEVEL);
  5645. clif_updatestatus(sd,SP_STATUSPOINT);
  5646. clif_updatestatus(sd,SP_BASEEXP);
  5647. clif_updatestatus(sd,SP_JOBEXP);
  5648. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5649. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5650. clif_updatestatus(sd,SP_SKILLPOINT);
  5651. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5652. clif_updatestatus(sd,SP_UAGI);
  5653. clif_updatestatus(sd,SP_UVIT);
  5654. clif_updatestatus(sd,SP_UINT);
  5655. clif_updatestatus(sd,SP_UDEX);
  5656. clif_updatestatus(sd,SP_ULUK); // End Addition
  5657. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  5658. if(sd->equip_index[i] >= 0)
  5659. if(!pc_isequip(sd,sd->equip_index[i]))
  5660. pc_unequipitem(sd,sd->equip_index[i],2);
  5661. }
  5662. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  5663. party_send_levelup(sd);
  5664. status_calc_pc(sd,0);
  5665. clif_skillinfoblock(sd);
  5666. return 0;
  5667. }
  5668. /*==========================================
  5669. * /resetstate
  5670. *------------------------------------------*/
  5671. int pc_resetstate(struct map_session_data* sd)
  5672. {
  5673. nullpo_ret(sd);
  5674. if (battle_config.use_statpoint_table)
  5675. { // New statpoint table used here - Dexity
  5676. if (sd->status.base_level > MAX_LEVEL)
  5677. { //statp[] goes out of bounds, can't reset!
  5678. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  5679. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  5680. return 0;
  5681. }
  5682. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  5683. }
  5684. else
  5685. {
  5686. int add=0;
  5687. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  5688. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  5689. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  5690. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  5691. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  5692. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  5693. sd->status.status_point+=add;
  5694. }
  5695. pc_setstat(sd, SP_STR, 1);
  5696. pc_setstat(sd, SP_AGI, 1);
  5697. pc_setstat(sd, SP_VIT, 1);
  5698. pc_setstat(sd, SP_INT, 1);
  5699. pc_setstat(sd, SP_DEX, 1);
  5700. pc_setstat(sd, SP_LUK, 1);
  5701. clif_updatestatus(sd,SP_STR);
  5702. clif_updatestatus(sd,SP_AGI);
  5703. clif_updatestatus(sd,SP_VIT);
  5704. clif_updatestatus(sd,SP_INT);
  5705. clif_updatestatus(sd,SP_DEX);
  5706. clif_updatestatus(sd,SP_LUK);
  5707. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5708. clif_updatestatus(sd,SP_UAGI);
  5709. clif_updatestatus(sd,SP_UVIT);
  5710. clif_updatestatus(sd,SP_UINT);
  5711. clif_updatestatus(sd,SP_UDEX);
  5712. clif_updatestatus(sd,SP_ULUK); // End Addition
  5713. clif_updatestatus(sd,SP_STATUSPOINT);
  5714. if( sd->mission_mobid ) { //bugreport:2200
  5715. sd->mission_mobid = 0;
  5716. sd->mission_count = 0;
  5717. pc_setglobalreg(sd,"TK_MISSION_ID", 0);
  5718. }
  5719. status_calc_pc(sd,0);
  5720. return 1;
  5721. }
  5722. /*==========================================
  5723. * /resetskill
  5724. * if flag&1, perform block resync and status_calc call.
  5725. * if flag&2, just count total amount of skill points used by player, do not really reset.
  5726. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  5727. *------------------------------------------*/
  5728. int pc_resetskill(struct map_session_data* sd, int flag)
  5729. {
  5730. int i, lv, inf2, skill_point=0;
  5731. nullpo_ret(sd);
  5732. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  5733. return 0;
  5734. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  5735. /**
  5736. * It has been confirmed on official server that when you reset skills with a ranked tweakwon your skills are not reset (because you have all of them anyway)
  5737. **/
  5738. if( (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5739. return 0;
  5740. if( pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd) )
  5741. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  5742. i = sd->sc.option;
  5743. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  5744. i &= ~OPTION_RIDING;
  5745. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  5746. i &= ~OPTION_FALCON;
  5747. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  5748. i &= ~OPTION_DRAGON;
  5749. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  5750. i &= ~OPTION_WUG;
  5751. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  5752. i &= ~OPTION_WUGRIDER;
  5753. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  5754. i &= ~OPTION_MADOGEAR;
  5755. if( i&OPTION_MOUNTING )
  5756. i &= ~OPTION_MOUNTING;
  5757. #ifndef NEW_CARTS
  5758. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  5759. i &= ~OPTION_CART;
  5760. #else
  5761. if( sd->sc.data[SC_PUSH_CART] )
  5762. pc_setcart(sd, 0);
  5763. #endif
  5764. if( i != sd->sc.option )
  5765. pc_setoption(sd, i);
  5766. if( merc_is_hom_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  5767. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  5768. }
  5769. for( i = 1; i < MAX_SKILL; i++ )
  5770. {
  5771. lv = sd->status.skill[i].lv;
  5772. if (lv < 1) continue;
  5773. inf2 = skill_get_inf2(i);
  5774. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  5775. continue;
  5776. // Don't reset trick dead if not a novice/baby
  5777. if( i == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5778. {
  5779. sd->status.skill[i].lv = 0;
  5780. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5781. continue;
  5782. }
  5783. // do not reset basic skill
  5784. if( i == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5785. continue;
  5786. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  5787. continue;
  5788. if( flag&4 && !skill_ischangesex(i) )
  5789. continue;
  5790. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  5791. { //Only handle quest skills in a special way when you can't learn them manually
  5792. if( battle_config.quest_skill_reset && !(flag&2) )
  5793. { //Wipe them
  5794. sd->status.skill[i].lv = 0;
  5795. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5796. }
  5797. continue;
  5798. }
  5799. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  5800. skill_point += lv;
  5801. else
  5802. if( sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0 )
  5803. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  5804. if( !(flag&2) )
  5805. {// reset
  5806. sd->status.skill[i].lv = 0;
  5807. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5808. }
  5809. }
  5810. if( flag&2 || !skill_point ) return skill_point;
  5811. sd->status.skill_point += skill_point;
  5812. if( flag&1 )
  5813. {
  5814. clif_updatestatus(sd,SP_SKILLPOINT);
  5815. clif_skillinfoblock(sd);
  5816. status_calc_pc(sd,0);
  5817. }
  5818. return skill_point;
  5819. }
  5820. /*==========================================
  5821. * /resetfeel [Komurka]
  5822. *------------------------------------------*/
  5823. int pc_resetfeel(struct map_session_data* sd)
  5824. {
  5825. int i;
  5826. nullpo_ret(sd);
  5827. for (i=0; i<MAX_PC_FEELHATE; i++)
  5828. {
  5829. sd->feel_map[i].m = -1;
  5830. sd->feel_map[i].index = 0;
  5831. pc_setglobalreg(sd,sg_info[i].feel_var,0);
  5832. }
  5833. return 0;
  5834. }
  5835. int pc_resethate(struct map_session_data* sd)
  5836. {
  5837. int i;
  5838. nullpo_ret(sd);
  5839. for (i=0; i<3; i++)
  5840. {
  5841. sd->hate_mob[i] = -1;
  5842. pc_setglobalreg(sd,sg_info[i].hate_var,0);
  5843. }
  5844. return 0;
  5845. }
  5846. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  5847. {
  5848. int i, bonus = 0;
  5849. nullpo_ret(sd);
  5850. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  5851. if( i < ARRAYLENGTH(sd->skillatk) ) bonus = sd->skillatk[i].val;
  5852. if(sd->sc.data[SC_PYROTECHNIC_OPTION] || sd->sc.data[SC_AQUAPLAY_OPTION])
  5853. bonus += 10;
  5854. return bonus;
  5855. }
  5856. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  5857. int i, bonus = sd->bonus.add_heal_rate;
  5858. if( bonus ) {
  5859. switch( skill_id ) {
  5860. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  5861. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  5862. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  5863. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  5864. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  5865. }
  5866. }
  5867. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  5868. if( i < ARRAYLENGTH(sd->skillheal) )
  5869. bonus += sd->skillheal[i].val;
  5870. return bonus;
  5871. }
  5872. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  5873. int i, bonus = sd->bonus.add_heal2_rate;
  5874. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  5875. if( i < ARRAYLENGTH(sd->skillheal2) )
  5876. bonus += sd->skillheal2[i].val;
  5877. return bonus;
  5878. }
  5879. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  5880. {
  5881. if( !pc_isdead(sd) )
  5882. return; // not applicable
  5883. if( sd->bg_id && bg_member_respawn(sd) )
  5884. return; // member revived by battleground
  5885. pc_setstand(sd);
  5886. pc_setrestartvalue(sd,3);
  5887. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
  5888. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  5889. }
  5890. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  5891. {
  5892. struct map_session_data *sd = map_id2sd(id);
  5893. if( sd != NULL )
  5894. {
  5895. sd->pvp_point=0;
  5896. pc_respawn(sd,CLR_OUTSIGHT);
  5897. }
  5898. return 0;
  5899. }
  5900. /*==========================================
  5901. * Invoked when a player has received damage
  5902. *------------------------------------------*/
  5903. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  5904. {
  5905. if (sp) clif_updatestatus(sd,SP_SP);
  5906. if (hp) clif_updatestatus(sd,SP_HP);
  5907. else return;
  5908. if( !src || src == &sd->bl )
  5909. return;
  5910. if( pc_issit(sd) ) {
  5911. pc_setstand(sd);
  5912. skill_sit(sd,0);
  5913. }
  5914. if( sd->progressbar.npc_id )
  5915. clif_progressbar_abort(sd);
  5916. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  5917. pet_target_check(sd,src,1);
  5918. if( sd->status.ele_id > 0 )
  5919. elemental_set_target(sd,src);
  5920. sd->canlog_tick = gettick();
  5921. }
  5922. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  5923. {
  5924. TBL_PC *sd = map_id2sd(id);
  5925. if(sd) pc_close_npc(sd,data);
  5926. return 0;
  5927. }
  5928. /*
  5929. * Method to properly close npc for player and clear anything related
  5930. * @flag == 1 : produce close button
  5931. * @flag == 2 : directly close it
  5932. */
  5933. void pc_close_npc(struct map_session_data *sd,int flag)
  5934. {
  5935. nullpo_retv(sd);
  5936. if (sd->npc_id || sd->npc_shopid) {
  5937. if (sd->state.using_fake_npc) {
  5938. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  5939. sd->state.using_fake_npc = 0;
  5940. }
  5941. if (sd->st) {
  5942. if(sd->st->state == RUN){ //wait ending code execution
  5943. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  5944. return;
  5945. }
  5946. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  5947. sd->st->mes_active = 0;
  5948. }
  5949. sd->state.menu_or_input = 0;
  5950. sd->npc_menu = 0;
  5951. sd->npc_shopid = 0;
  5952. #ifdef SECURE_NPCTIMEOUT
  5953. sd->npc_idle_timer = INVALID_TIMER;
  5954. #endif
  5955. clif_scriptclose(sd,sd->npc_id);
  5956. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  5957. script_free_state(sd->st);
  5958. sd->st = NULL;
  5959. sd->npc_id = 0;
  5960. }
  5961. }
  5962. }
  5963. /*==========================================
  5964. * Invoked when a player has negative current hp
  5965. *------------------------------------------*/
  5966. int pc_dead(struct map_session_data *sd,struct block_list *src)
  5967. {
  5968. int i=0,j=0,k=0;
  5969. unsigned int tick = gettick();
  5970. // Activate Steel body if a super novice dies at 99+% exp [celest]
  5971. // Super Novices have no kill or die functions attached when saved by their angel
  5972. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && !sd->state.snovice_dead_flag) {
  5973. unsigned int next = pc_nextbaseexp(sd);
  5974. if( next == 0 ) next = pc_thisbaseexp(sd);
  5975. if( get_percentage(sd->status.base_exp,next) >= 99 ) {
  5976. sd->state.snovice_dead_flag = 1;
  5977. pc_setrestartvalue(sd,1);
  5978. status_percent_heal(&sd->bl, 100, 100);
  5979. clif_resurrection(&sd->bl, 1);
  5980. if(battle_config.pc_invincible_time)
  5981. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  5982. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  5983. if(map_flag_gvg(sd->bl.m))
  5984. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5985. return 0;
  5986. }
  5987. }
  5988. for(k = 0; k < 5; k++)
  5989. if (sd->devotion[k]){
  5990. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  5991. if (devsd)
  5992. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  5993. sd->devotion[k] = 0;
  5994. }
  5995. if(sd->shadowform_id) { //if we were target of shadowform
  5996. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  5997. sd->shadowform_id = 0; //should be remove on status end anyway
  5998. }
  5999. if(sd->status.pet_id > 0 && sd->pd) {
  6000. struct pet_data *pd = sd->pd;
  6001. if( !map[sd->bl.m].flag.noexppenalty ) {
  6002. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6003. if( pd->pet.intimate < 0 )
  6004. pd->pet.intimate = 0;
  6005. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6006. }
  6007. if( sd->pd->target_id ) // Unlock all targets...
  6008. pet_unlocktarget(sd->pd);
  6009. }
  6010. if (sd->status.hom_id > 0) {
  6011. if(battle_config.homunculus_auto_vapor && sd->hd && !sd->hd->sc.data[SC_LIGHT_OF_REGENE])
  6012. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  6013. }
  6014. if( sd->md )
  6015. merc_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6016. if( sd->ed )
  6017. elemental_delete(sd->ed, 0);
  6018. // Leave duel if you die [LuzZza]
  6019. if(battle_config.duel_autoleave_when_die) {
  6020. if(sd->duel_group > 0)
  6021. duel_leave(sd->duel_group, sd);
  6022. if(sd->duel_invite > 0)
  6023. duel_reject(sd->duel_invite, sd);
  6024. }
  6025. pc_close_npc(sd,2); //close npc if we were using one
  6026. /* e.g. not killed thru pc_damage */
  6027. if( pc_issit(sd) ) {
  6028. clif_status_load(&sd->bl,SI_SIT,0);
  6029. }
  6030. pc_setdead(sd);
  6031. pc_setglobalreg(sd,"PC_DIE_COUNTER",sd->die_counter+1);
  6032. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6033. //Reset menu skills/item skills
  6034. if (sd->skillitem)
  6035. sd->skillitem = sd->skillitemlv = 0;
  6036. if (sd->menuskill_id)
  6037. sd->menuskill_id = sd->menuskill_val = 0;
  6038. //Reset ticks.
  6039. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6040. if ( sd && sd->spiritball )
  6041. pc_delspiritball(sd,sd->spiritball,0);
  6042. for(i = 1; i < 5; i++)
  6043. pc_del_talisman(sd, sd->talisman[i], i);
  6044. if (src)
  6045. switch (src->type) {
  6046. case BL_MOB:
  6047. {
  6048. struct mob_data *md=(struct mob_data *)src;
  6049. if(md->target_id==sd->bl.id)
  6050. mob_unlocktarget(md,tick);
  6051. if(battle_config.mobs_level_up && md->status.hp &&
  6052. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6053. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6054. ) { // monster level up [Valaris]
  6055. clif_misceffect(&md->bl,0);
  6056. md->level++;
  6057. status_calc_mob(md, 0);
  6058. status_percent_heal(src,10,0);
  6059. if( battle_config.show_mob_info&4 )
  6060. {// update name with new level
  6061. clif_charnameack(0, &md->bl);
  6062. }
  6063. }
  6064. src = battle_get_master(src); // Maybe Player Summon
  6065. }
  6066. break;
  6067. case BL_PET: //Pass on to master...
  6068. case BL_HOM:
  6069. case BL_MER:
  6070. src = battle_get_master(src);
  6071. break;
  6072. }
  6073. if (src && src->type == BL_PC) {
  6074. struct map_session_data *ssd = (struct map_session_data *)src;
  6075. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6076. npc_script_event(ssd, NPCE_KILLPC);
  6077. if (battle_config.pk_mode&2) {
  6078. ssd->status.manner -= 5;
  6079. if(ssd->status.manner < 0)
  6080. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6081. #if 0
  6082. // PK/Karma system code (not enabled yet) [celest]
  6083. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6084. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6085. // karma going down = more 'good' / more honourable.
  6086. // The Karma System way...
  6087. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6088. sd->status.karma--;
  6089. ssd->status.karma--;
  6090. }
  6091. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6092. ssd->status.karma++;
  6093. // or the PK System way...
  6094. if (sd->status.karma > 0) // player killed is dishonourable?
  6095. ssd->status.karma--; // honour points earned
  6096. sd->status.karma++; // honour points lost
  6097. // To-do: Receive exp on certain occasions
  6098. #endif
  6099. }
  6100. }
  6101. if(battle_config.bone_drop==2
  6102. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6103. {
  6104. struct item item_tmp;
  6105. memset(&item_tmp,0,sizeof(item_tmp));
  6106. item_tmp.nameid=ITEMID_SKULL_;
  6107. item_tmp.identify=1;
  6108. item_tmp.card[0]=CARD0_CREATE;
  6109. item_tmp.card[1]=0;
  6110. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6111. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6112. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  6113. }
  6114. //Remove bonus_script when dead
  6115. pc_bonus_script_check(sd,BONUS_FLAG_REM_ON_DEAD);
  6116. // changed penalty options, added death by player if pk_mode [Valaris]
  6117. if(battle_config.death_penalty_type
  6118. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6119. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6120. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6121. {
  6122. unsigned int base_penalty = battle_config.death_penalty_base, job_penalty = battle_config.death_penalty_job;
  6123. #ifdef VIP_ENABLE
  6124. if(pc_isvip(sd)){
  6125. base_penalty = base_penalty*battle_config.vip_exp_penalty_base;
  6126. job_penalty = job_penalty*battle_config.vip_exp_penalty_job;
  6127. }
  6128. else {
  6129. base_penalty = base_penalty*battle_config.vip_exp_penalty_base_normal;
  6130. job_penalty = job_penalty*battle_config.vip_exp_penalty_job_normal;
  6131. }
  6132. #endif
  6133. if (base_penalty > 0) {
  6134. switch (battle_config.death_penalty_type) {
  6135. case 1: base_penalty = (uint32) ((double)(pc_nextbaseexp(sd) * base_penalty)/10000); break;
  6136. case 2: base_penalty = (uint32) ((double)(sd->status.base_exp * base_penalty)/10000); break;
  6137. }
  6138. if (base_penalty > 0){ //recheck after altering to speedup
  6139. if (battle_config.pk_mode && src && src->type==BL_PC)
  6140. base_penalty*=2;
  6141. sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
  6142. clif_updatestatus(sd,SP_BASEEXP);
  6143. }
  6144. }
  6145. if(job_penalty > 0) {
  6146. switch (battle_config.death_penalty_type) {
  6147. case 1: job_penalty = (uint32) ((double)(pc_nextjobexp(sd) * job_penalty)/10000); break;
  6148. case 2: job_penalty = (uint32) ((double)(sd->status.job_exp * job_penalty)/10000); break;
  6149. }
  6150. if(job_penalty) {
  6151. if (battle_config.pk_mode && src && src->type==BL_PC)
  6152. job_penalty*=2;
  6153. sd->status.job_exp -= min(sd->status.job_exp, job_penalty);
  6154. clif_updatestatus(sd,SP_JOBEXP);
  6155. }
  6156. }
  6157. if(battle_config.zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6158. base_penalty = (unsigned int)((double)sd->status.zeny * (double)battle_config.zeny_penalty / 10000.);
  6159. if(base_penalty)
  6160. pc_payzeny(sd, base_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6161. }
  6162. }
  6163. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6164. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6165. int id = map[sd->bl.m].drop_list[j].drop_id;
  6166. int type = map[sd->bl.m].drop_list[j].drop_type;
  6167. int per = map[sd->bl.m].drop_list[j].drop_per;
  6168. if(id == 0)
  6169. continue;
  6170. if(id == -1){
  6171. int eq_num=0,eq_n[MAX_INVENTORY];
  6172. memset(eq_n,0,sizeof(eq_n));
  6173. for(i=0;i<MAX_INVENTORY;i++) {
  6174. if( (type == 1 && !sd->status.inventory[i].equip)
  6175. || (type == 2 && sd->status.inventory[i].equip)
  6176. || type == 3)
  6177. {
  6178. int k;
  6179. ARR_FIND( 0, MAX_INVENTORY, k, eq_n[k] <= 0 );
  6180. if( k < MAX_INVENTORY )
  6181. eq_n[k] = i;
  6182. eq_num++;
  6183. }
  6184. }
  6185. if(eq_num > 0){
  6186. int n = eq_n[rnd()%eq_num];
  6187. if(rnd()%10000 < per) {
  6188. if(sd->status.inventory[n].equip)
  6189. pc_unequipitem(sd,n,3);
  6190. pc_dropitem(sd,n,1);
  6191. }
  6192. }
  6193. }
  6194. else if(id > 0) {
  6195. for(i=0;i<MAX_INVENTORY;i++){
  6196. if(sd->status.inventory[i].nameid == id
  6197. && rnd()%10000 < per
  6198. && ((type == 1 && !sd->status.inventory[i].equip)
  6199. || (type == 2 && sd->status.inventory[i].equip)
  6200. || type == 3) ){
  6201. if(sd->status.inventory[i].equip)
  6202. pc_unequipitem(sd,i,3);
  6203. pc_dropitem(sd,i,1);
  6204. break;
  6205. }
  6206. }
  6207. }
  6208. }
  6209. }
  6210. // pvp
  6211. // disable certain pvp functions on pk_mode [Valaris]
  6212. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6213. sd->pvp_point -= 5;
  6214. sd->pvp_lost++;
  6215. if( src && src->type == BL_PC ) {
  6216. struct map_session_data *ssd = (struct map_session_data *)src;
  6217. ssd->pvp_point++;
  6218. ssd->pvp_won++;
  6219. }
  6220. if( sd->pvp_point < 0 ) {
  6221. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6222. return 1|8;
  6223. }
  6224. }
  6225. //GvG
  6226. if( map_flag_gvg(sd->bl.m) ) {
  6227. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6228. return 1|8;
  6229. }
  6230. else if( sd->bg_id ) {
  6231. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6232. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6233. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6234. return 1|8;
  6235. }
  6236. }
  6237. //Reset "can log out" tick.
  6238. if( battle_config.prevent_logout )
  6239. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6240. return 1;
  6241. }
  6242. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6243. if(hp) clif_updatestatus(sd,SP_HP);
  6244. if(sp) clif_updatestatus(sd,SP_SP);
  6245. pc_setstand(sd);
  6246. if(battle_config.pc_invincible_time > 0)
  6247. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6248. if( sd->state.gmaster_flag ) {
  6249. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->state.gmaster_flag,GD_LEADERSHIP));
  6250. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->state.gmaster_flag,GD_GLORYWOUNDS));
  6251. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->state.gmaster_flag,GD_SOULCOLD));
  6252. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->state.gmaster_flag,GD_HAWKEYES));
  6253. }
  6254. }
  6255. // script
  6256. //
  6257. /*==========================================
  6258. * script reading pc status registry
  6259. *------------------------------------------*/
  6260. int pc_readparam(struct map_session_data* sd,int type)
  6261. {
  6262. int val = 0;
  6263. nullpo_ret(sd);
  6264. switch(type) {
  6265. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6266. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6267. case SP_ZENY: val = sd->status.zeny; break;
  6268. case SP_BASELEVEL: val = sd->status.base_level; break;
  6269. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6270. case SP_CLASS: val = sd->status.class_; break;
  6271. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6272. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6273. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6274. case SP_SEX: val = sd->status.sex; break;
  6275. case SP_WEIGHT: val = sd->weight; break;
  6276. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6277. case SP_BASEEXP: val = sd->status.base_exp; break;
  6278. case SP_JOBEXP: val = sd->status.job_exp; break;
  6279. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6280. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6281. case SP_HP: val = sd->battle_status.hp; break;
  6282. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6283. case SP_SP: val = sd->battle_status.sp; break;
  6284. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6285. case SP_STR: val = sd->status.str; break;
  6286. case SP_AGI: val = sd->status.agi; break;
  6287. case SP_VIT: val = sd->status.vit; break;
  6288. case SP_INT: val = sd->status.int_; break;
  6289. case SP_DEX: val = sd->status.dex; break;
  6290. case SP_LUK: val = sd->status.luk; break;
  6291. case SP_KARMA: val = sd->status.karma; break;
  6292. case SP_MANNER: val = sd->status.manner; break;
  6293. case SP_FAME: val = sd->status.fame; break;
  6294. case SP_KILLERRID: val = sd->killerrid; break;
  6295. case SP_KILLEDRID: val = sd->killedrid; break;
  6296. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6297. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6298. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6299. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6300. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6301. case SP_DEF1: val = sd->battle_status.def; break;
  6302. case SP_DEF2: val = sd->battle_status.def2; break;
  6303. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6304. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6305. case SP_HIT: val = sd->battle_status.hit; break;
  6306. case SP_FLEE1: val = sd->battle_status.flee; break;
  6307. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6308. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6309. #ifndef RENEWAL_CAST
  6310. case SP_VARCASTRATE:
  6311. #endif
  6312. case SP_CASTRATE:
  6313. val = sd->castrate+=val;
  6314. break;
  6315. case SP_MAXHPRATE: val = sd->hprate; break;
  6316. case SP_MAXSPRATE: val = sd->sprate; break;
  6317. case SP_SPRATE: val = sd->dsprate; break;
  6318. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6319. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6320. case SP_ASPD_RATE:
  6321. #ifndef RENEWAL_ASPD
  6322. val = sd->battle_status.aspd_rate;
  6323. #else
  6324. val = sd->battle_status.aspd_rate2;
  6325. #endif
  6326. break;
  6327. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6328. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6329. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6330. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6331. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6332. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6333. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6334. case SP_MATK_RATE: val = sd->matk_rate; break;
  6335. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6336. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6337. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6338. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6339. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6340. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6341. case SP_HIT_RATE: val = sd->hit_rate; break;
  6342. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6343. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6344. case SP_DEF_RATE: val = sd->def_rate; break;
  6345. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6346. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6347. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6348. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6349. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6350. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6351. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6352. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6353. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6354. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6355. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6356. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6357. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6358. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6359. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6360. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6361. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6362. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6363. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6364. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6365. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6366. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6367. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6368. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6369. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6370. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6371. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6372. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6373. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6374. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6375. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6376. case SP_DELAYRATE: val = sd->delayrate; break;
  6377. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6378. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6379. case SP_UNSTRIPABLE:
  6380. case SP_UNSTRIPABLE_ARMOR:
  6381. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6382. break;
  6383. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6384. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6385. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6386. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6387. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6388. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6389. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  6390. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  6391. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  6392. case SP_EMATK: val = sd->bonus.ematk; break;
  6393. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  6394. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  6395. #ifdef RENEWAL_CAST
  6396. case SP_VARCASTRATE: val = sd->bonus.varcastrate; break;
  6397. case SP_ADD_VARIABLECAST:val = sd->bonus.add_varcast; break;
  6398. #endif
  6399. }
  6400. return val;
  6401. }
  6402. /*==========================================
  6403. * script set pc status registry
  6404. *------------------------------------------*/
  6405. int pc_setparam(struct map_session_data *sd,int type,int val)
  6406. {
  6407. int i = 0;
  6408. nullpo_ret(sd);
  6409. switch(type){
  6410. case SP_BASELEVEL:
  6411. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  6412. val = pc_maxbaselv(sd);
  6413. if ((unsigned int)val > sd->status.base_level) {
  6414. int stat=0;
  6415. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  6416. stat += pc_gets_status_point(sd->status.base_level + i);
  6417. sd->status.status_point += stat;
  6418. }
  6419. sd->status.base_level = (unsigned int)val;
  6420. sd->status.base_exp = 0;
  6421. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  6422. clif_updatestatus(sd, SP_NEXTBASEEXP);
  6423. clif_updatestatus(sd, SP_STATUSPOINT);
  6424. clif_updatestatus(sd, SP_BASEEXP);
  6425. status_calc_pc(sd, 0);
  6426. if(sd->status.party_id)
  6427. {
  6428. party_send_levelup(sd);
  6429. }
  6430. break;
  6431. case SP_JOBLEVEL:
  6432. if ((unsigned int)val >= sd->status.job_level) {
  6433. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  6434. sd->status.skill_point += val - sd->status.job_level;
  6435. clif_updatestatus(sd, SP_SKILLPOINT);
  6436. }
  6437. sd->status.job_level = (unsigned int)val;
  6438. sd->status.job_exp = 0;
  6439. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  6440. clif_updatestatus(sd, SP_NEXTJOBEXP);
  6441. clif_updatestatus(sd, SP_JOBEXP);
  6442. status_calc_pc(sd, 0);
  6443. break;
  6444. case SP_SKILLPOINT:
  6445. sd->status.skill_point = val;
  6446. break;
  6447. case SP_STATUSPOINT:
  6448. sd->status.status_point = val;
  6449. break;
  6450. case SP_ZENY:
  6451. if( val < 0 )
  6452. return 0;// can't set negative zeny
  6453. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  6454. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  6455. break;
  6456. case SP_BASEEXP:
  6457. if(pc_nextbaseexp(sd) > 0) {
  6458. sd->status.base_exp = val;
  6459. pc_checkbaselevelup(sd);
  6460. }
  6461. break;
  6462. case SP_JOBEXP:
  6463. if(pc_nextjobexp(sd) > 0) {
  6464. sd->status.job_exp = val;
  6465. pc_checkjoblevelup(sd);
  6466. }
  6467. break;
  6468. case SP_SEX:
  6469. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  6470. break;
  6471. case SP_WEIGHT:
  6472. sd->weight = val;
  6473. break;
  6474. case SP_MAXWEIGHT:
  6475. sd->max_weight = val;
  6476. break;
  6477. case SP_HP:
  6478. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  6479. break;
  6480. case SP_MAXHP:
  6481. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  6482. if( sd->battle_status.max_hp < sd->battle_status.hp )
  6483. {
  6484. sd->battle_status.hp = sd->battle_status.max_hp;
  6485. clif_updatestatus(sd, SP_HP);
  6486. }
  6487. break;
  6488. case SP_SP:
  6489. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  6490. break;
  6491. case SP_MAXSP:
  6492. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  6493. if( sd->battle_status.max_sp < sd->battle_status.sp )
  6494. {
  6495. sd->battle_status.sp = sd->battle_status.max_sp;
  6496. clif_updatestatus(sd, SP_SP);
  6497. }
  6498. break;
  6499. case SP_STR:
  6500. sd->status.str = cap_value(val, 1, pc_maxparameter(sd));
  6501. break;
  6502. case SP_AGI:
  6503. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd));
  6504. break;
  6505. case SP_VIT:
  6506. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd));
  6507. break;
  6508. case SP_INT:
  6509. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd));
  6510. break;
  6511. case SP_DEX:
  6512. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd));
  6513. break;
  6514. case SP_LUK:
  6515. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd));
  6516. break;
  6517. case SP_KARMA:
  6518. sd->status.karma = val;
  6519. break;
  6520. case SP_MANNER:
  6521. sd->status.manner = val;
  6522. break;
  6523. case SP_FAME:
  6524. sd->status.fame = val;
  6525. break;
  6526. case SP_KILLERRID:
  6527. sd->killerrid = val;
  6528. return 1;
  6529. case SP_KILLEDRID:
  6530. sd->killedrid = val;
  6531. return 1;
  6532. case SP_CHARMOVE:
  6533. sd->status.character_moves = val;
  6534. return 1;
  6535. default:
  6536. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  6537. return 0;
  6538. }
  6539. clif_updatestatus(sd,type);
  6540. return 1;
  6541. }
  6542. /*==========================================
  6543. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  6544. *------------------------------------------*/
  6545. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  6546. {
  6547. if (type) {
  6548. if (hp)
  6549. clif_heal(sd->fd,SP_HP,hp);
  6550. if (sp)
  6551. clif_heal(sd->fd,SP_SP,sp);
  6552. } else {
  6553. if(hp)
  6554. clif_updatestatus(sd,SP_HP);
  6555. if(sp)
  6556. clif_updatestatus(sd,SP_SP);
  6557. }
  6558. return;
  6559. }
  6560. /*==========================================
  6561. * HP/SP Recovery
  6562. * Heal player hp and/or sp linearly.
  6563. * Calculate bonus by status.
  6564. *------------------------------------------*/
  6565. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  6566. {
  6567. int bonus;
  6568. if(hp) {
  6569. int i;
  6570. bonus = 100 + (sd->battle_status.vit<<1)
  6571. + pc_checkskill(sd,SM_RECOVERY)*10
  6572. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6573. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  6574. if (potion_flag > 1)
  6575. bonus += bonus*(potion_flag-1)*50/100;
  6576. //All item bonuses.
  6577. bonus += sd->bonus.itemhealrate2;
  6578. //Item Group bonuses
  6579. bonus += bonus*itemdb_group_bonus(sd, itemid)/100;
  6580. //Individual item bonuses.
  6581. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  6582. {
  6583. if (sd->itemhealrate[i].nameid == itemid) {
  6584. bonus += bonus*sd->itemhealrate[i].rate/100;
  6585. break;
  6586. }
  6587. }
  6588. if(bonus!=100)
  6589. hp = hp * bonus / 100;
  6590. // Recovery Potion
  6591. if( sd->sc.data[SC_INCHEALRATE] )
  6592. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  6593. }
  6594. if(sp) {
  6595. bonus = 100 + (sd->battle_status.int_<<1)
  6596. + pc_checkskill(sd,MG_SRECOVERY)*10
  6597. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6598. if (potion_flag > 1)
  6599. bonus += bonus*(potion_flag-1)*50/100;
  6600. if(bonus != 100)
  6601. sp = sp * bonus / 100;
  6602. }
  6603. if( sd->sc.count ) {
  6604. if ( sd->sc.data[SC_CRITICALWOUND] ) {
  6605. hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6606. sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6607. }
  6608. if ( sd->sc.data[SC_DEATHHURT] ) {
  6609. hp -= hp * 20 / 100;
  6610. sp -= sp * 20 / 100;
  6611. }
  6612. if( sd->sc.data[SC_VITALITYACTIVATION] ){
  6613. hp += hp / 2; // 1.5 times
  6614. sp -= sp / 2;
  6615. }
  6616. if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
  6617. hp += hp / 10;
  6618. sp += sp / 10;
  6619. }
  6620. #ifdef RENEWAL
  6621. if( sd->sc.data[SC_EXTREMITYFIST2] )
  6622. sp = 0;
  6623. #endif
  6624. }
  6625. return status_heal(&sd->bl, hp, sp, 1);
  6626. }
  6627. /*==========================================
  6628. * HP/SP Recovery
  6629. * Heal player hp nad/or sp by rate
  6630. *------------------------------------------*/
  6631. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  6632. {
  6633. nullpo_ret(sd);
  6634. if (hp > 100) hp = 100;
  6635. else if (hp <-100) hp = -100;
  6636. if (sp > 100) sp = 100;
  6637. else if (sp <-100) sp = -100;
  6638. if(hp >= 0 && sp >= 0) //Heal
  6639. return status_percent_heal(&sd->bl, hp, sp);
  6640. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  6641. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  6642. //Crossed signs
  6643. if(hp) {
  6644. if(hp > 0)
  6645. status_percent_heal(&sd->bl, hp, 0);
  6646. else
  6647. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  6648. }
  6649. if(sp) {
  6650. if(sp > 0)
  6651. status_percent_heal(&sd->bl, 0, sp);
  6652. else
  6653. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  6654. }
  6655. return 0;
  6656. }
  6657. static int jobchange_killclone(struct block_list *bl, va_list ap)
  6658. {
  6659. struct mob_data *md;
  6660. int flag;
  6661. md = (struct mob_data *)bl;
  6662. nullpo_ret(md);
  6663. flag = va_arg(ap, int);
  6664. if (md->master_id && md->special_state.clone && md->master_id == flag)
  6665. status_kill(&md->bl);
  6666. return 1;
  6667. }
  6668. /*==========================================
  6669. * Called when player changes job
  6670. * Rewrote to make it tidider [Celest]
  6671. *------------------------------------------*/
  6672. int pc_jobchange(struct map_session_data *sd,int job, int upper)
  6673. {
  6674. int i, fame_flag=0;
  6675. int b_class;
  6676. nullpo_ret(sd);
  6677. if (job < 0)
  6678. return 1;
  6679. //Normalize job.
  6680. b_class = pc_jobid2mapid(job);
  6681. if (b_class == -1)
  6682. return 1;
  6683. switch (upper) {
  6684. case 1:
  6685. b_class|= JOBL_UPPER;
  6686. break;
  6687. case 2:
  6688. b_class|= JOBL_BABY;
  6689. break;
  6690. }
  6691. //This will automatically adjust bard/dancer classes to the correct gender
  6692. //That is, if you try to jobchange into dancer, it will turn you to bard.
  6693. job = pc_mapid2jobid(b_class, sd->status.sex);
  6694. if (job == -1)
  6695. return 1;
  6696. if ((unsigned short)b_class == sd->class_)
  6697. return 1; //Nothing to change.
  6698. // changing from 1st to 2nd job
  6699. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (b_class&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  6700. sd->change_level_2nd = sd->status.job_level;
  6701. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  6702. }
  6703. // changing from 2nd to 3rd job
  6704. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  6705. sd->change_level_3rd = sd->status.job_level;
  6706. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  6707. }
  6708. if(sd->cloneskill_id) {
  6709. if( sd->status.skill[sd->cloneskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6710. sd->status.skill[sd->cloneskill_id].id = 0;
  6711. sd->status.skill[sd->cloneskill_id].lv = 0;
  6712. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PERMANENT;
  6713. clif_deleteskill(sd,sd->cloneskill_id);
  6714. }
  6715. sd->cloneskill_id = 0;
  6716. pc_setglobalreg(sd,SKILL_VAR_PLAGIARISM, 0);
  6717. pc_setglobalreg(sd,SKILL_VAR_PLAGIARISM_LV, 0);
  6718. }
  6719. if(sd->reproduceskill_id) {
  6720. if( sd->status.skill[sd->reproduceskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6721. sd->status.skill[sd->reproduceskill_id].id = 0;
  6722. sd->status.skill[sd->reproduceskill_id].lv = 0;
  6723. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PERMANENT;
  6724. clif_deleteskill(sd,sd->reproduceskill_id);
  6725. }
  6726. sd->reproduceskill_id = 0;
  6727. pc_setglobalreg(sd,SKILL_VAR_REPRODUCE,0);
  6728. pc_setglobalreg(sd,SKILL_VAR_REPRODUCE_LV,0);
  6729. }
  6730. // Give or reduce transcendent status points
  6731. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  6732. sd->status.status_point += 52;
  6733. clif_updatestatus(sd,SP_STATUSPOINT);
  6734. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  6735. if( sd->status.status_point < 52 ){
  6736. // The player already used his bonus points, so we have to reset his status points
  6737. pc_resetstate(sd);
  6738. }
  6739. sd->status.status_point -= 52;
  6740. clif_updatestatus(sd,SP_STATUSPOINT);
  6741. }
  6742. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  6743. const int class_ = pc_class2idx(sd->status.class_);
  6744. short id;
  6745. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  6746. //Remove status specific to your current tree skills.
  6747. enum sc_type sc = status_skill2sc(id);
  6748. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  6749. status_change_end(&sd->bl, sc, INVALID_TIMER);
  6750. }
  6751. }
  6752. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  6753. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  6754. pc_resetfeel(sd);
  6755. }
  6756. sd->status.class_ = job;
  6757. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  6758. sd->class_ = (unsigned short)b_class;
  6759. sd->status.job_level=1;
  6760. sd->status.job_exp=0;
  6761. if (sd->status.base_level > pc_maxbaselv(sd)) {
  6762. sd->status.base_level = pc_maxbaselv(sd);
  6763. sd->status.base_exp=0;
  6764. pc_resetstate(sd);
  6765. clif_updatestatus(sd,SP_STATUSPOINT);
  6766. clif_updatestatus(sd,SP_BASELEVEL);
  6767. clif_updatestatus(sd,SP_BASEEXP);
  6768. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6769. }
  6770. clif_updatestatus(sd,SP_JOBLEVEL);
  6771. clif_updatestatus(sd,SP_JOBEXP);
  6772. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6773. for(i=0;i<EQI_MAX;i++) {
  6774. if(sd->equip_index[i] >= 0)
  6775. if(!pc_isequip(sd,sd->equip_index[i]))
  6776. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  6777. }
  6778. //Change look, if disguised, you need to undisguise
  6779. //to correctly calculate new job sprite without
  6780. if (sd->disguise)
  6781. pc_disguise(sd, 0);
  6782. status_set_viewdata(&sd->bl, job);
  6783. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  6784. if(sd->vd.cloth_color)
  6785. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6786. //Update skill tree.
  6787. pc_calc_skilltree(sd);
  6788. clif_skillinfoblock(sd);
  6789. if (sd->ed)
  6790. elemental_delete(sd->ed, 0);
  6791. if (sd->state.vending)
  6792. vending_closevending(sd);
  6793. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  6794. //Remove peco/cart/falcon
  6795. i = sd->sc.option;
  6796. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  6797. i&=~OPTION_RIDING;
  6798. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  6799. i&=~OPTION_FALCON;
  6800. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  6801. i&=~OPTION_DRAGON;
  6802. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  6803. i&=~OPTION_WUGRIDER;
  6804. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  6805. i&=~OPTION_WUG;
  6806. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  6807. i&=~OPTION_MADOGEAR;
  6808. #ifndef NEW_CARTS
  6809. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  6810. i&=~OPTION_CART;
  6811. #else
  6812. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  6813. pc_setcart(sd, 0);
  6814. #endif
  6815. if(i != sd->sc.option)
  6816. pc_setoption(sd, i);
  6817. if(merc_is_hom_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  6818. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  6819. if(sd->status.manner < 0)
  6820. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  6821. status_calc_pc(sd,0);
  6822. pc_checkallowskill(sd);
  6823. pc_equiplookall(sd);
  6824. //if you were previously famous, not anymore.
  6825. if (fame_flag) {
  6826. chrif_save(sd,0);
  6827. chrif_buildfamelist();
  6828. } else if (sd->status.fame > 0) {
  6829. //It may be that now they are famous?
  6830. switch (sd->class_&MAPID_UPPERMASK) {
  6831. case MAPID_BLACKSMITH:
  6832. case MAPID_ALCHEMIST:
  6833. case MAPID_TAEKWON:
  6834. chrif_save(sd,0);
  6835. chrif_buildfamelist();
  6836. break;
  6837. }
  6838. }
  6839. return 0;
  6840. }
  6841. /*==========================================
  6842. * Tell client player sd has change equipement
  6843. *------------------------------------------*/
  6844. int pc_equiplookall(struct map_session_data *sd)
  6845. {
  6846. nullpo_ret(sd);
  6847. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  6848. clif_changelook(&sd->bl,LOOK_SHOES,0);
  6849. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  6850. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  6851. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  6852. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  6853. return 0;
  6854. }
  6855. /*==========================================
  6856. * Tell client player sd has change look (hair,equip...)
  6857. *------------------------------------------*/
  6858. int pc_changelook(struct map_session_data *sd,int type,int val)
  6859. {
  6860. nullpo_ret(sd);
  6861. switch(type){
  6862. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  6863. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6864. if (sd->status.hair != val)
  6865. {
  6866. sd->status.hair=val;
  6867. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6868. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6869. GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
  6870. }
  6871. break;
  6872. case LOOK_WEAPON:
  6873. sd->status.weapon=val;
  6874. break;
  6875. case LOOK_HEAD_BOTTOM:
  6876. sd->status.head_bottom=val;
  6877. break;
  6878. case LOOK_HEAD_TOP:
  6879. sd->status.head_top=val;
  6880. break;
  6881. case LOOK_HEAD_MID:
  6882. sd->status.head_mid=val;
  6883. break;
  6884. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  6885. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6886. if (sd->status.hair_color != val) {
  6887. sd->status.hair_color=val;
  6888. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6889. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6890. GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
  6891. }
  6892. break;
  6893. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  6894. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6895. sd->status.clothes_color=val;
  6896. break;
  6897. case LOOK_SHIELD:
  6898. sd->status.shield=val;
  6899. break;
  6900. case LOOK_SHOES:
  6901. break;
  6902. case LOOK_ROBE:
  6903. sd->status.robe = val;
  6904. break;
  6905. }
  6906. clif_changelook(&sd->bl,type,val);
  6907. return 0;
  6908. }
  6909. /*==========================================
  6910. * Give an option (type) to player (sd) and display it to client
  6911. *------------------------------------------*/
  6912. int pc_setoption(struct map_session_data *sd,int type)
  6913. {
  6914. int p_type, new_look=0;
  6915. nullpo_ret(sd);
  6916. p_type = sd->sc.option;
  6917. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  6918. sd->sc.option=type;
  6919. clif_changeoption(&sd->bl);
  6920. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6921. { // Mounting
  6922. clif_status_load(&sd->bl,SI_RIDING,1);
  6923. status_calc_pc(sd,0);
  6924. }
  6925. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6926. { // Dismount
  6927. clif_status_load(&sd->bl,SI_RIDING,0);
  6928. status_calc_pc(sd,0);
  6929. }
  6930. #ifndef NEW_CARTS
  6931. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  6932. clif_cartlist(sd);
  6933. clif_updatestatus(sd, SP_CARTINFO);
  6934. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6935. status_calc_pc(sd,0); //Apply speed penalty.
  6936. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  6937. clif_clearcart(sd->fd);
  6938. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6939. status_calc_pc(sd,0); //Remove speed penalty.
  6940. }
  6941. #endif
  6942. if (type&OPTION_MOUNTING && !(p_type&OPTION_MOUNTING) ) {
  6943. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,2,1,0,0);
  6944. status_calc_pc(sd,0);
  6945. } else if (!(type&OPTION_MOUNTING) && p_type&OPTION_MOUNTING) {
  6946. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,0,0,0,0);
  6947. status_calc_pc(sd,0);
  6948. }
  6949. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  6950. clif_status_load(&sd->bl,SI_FALCON,1);
  6951. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  6952. clif_status_load(&sd->bl,SI_FALCON,0);
  6953. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  6954. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  6955. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  6956. status_calc_pc(sd,0);
  6957. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  6958. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  6959. status_calc_pc(sd,0);
  6960. }
  6961. }
  6962. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  6963. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  6964. status_calc_pc(sd, 0);
  6965. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  6966. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  6967. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  6968. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  6969. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  6970. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  6971. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  6972. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  6973. status_calc_pc(sd, 0);
  6974. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  6975. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  6976. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  6977. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  6978. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  6979. }
  6980. }
  6981. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  6982. new_look = JOB_STAR_GLADIATOR2;
  6983. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  6984. new_look = -1;
  6985. if (type&OPTION_WEDDING && !(p_type&OPTION_WEDDING))
  6986. new_look = JOB_WEDDING;
  6987. else if (!(type&OPTION_WEDDING) && p_type&OPTION_WEDDING)
  6988. new_look = -1;
  6989. if (type&OPTION_XMAS && !(p_type&OPTION_XMAS))
  6990. new_look = JOB_XMAS;
  6991. else if (!(type&OPTION_XMAS) && p_type&OPTION_XMAS)
  6992. new_look = -1;
  6993. if (type&OPTION_SUMMER && !(p_type&OPTION_SUMMER))
  6994. new_look = JOB_SUMMER;
  6995. else if (!(type&OPTION_SUMMER) && p_type&OPTION_SUMMER)
  6996. new_look = -1;
  6997. if (type&OPTION_HANBOK && !(p_type&OPTION_HANBOK))
  6998. new_look = JOB_HANBOK;
  6999. else if (!(type&OPTION_HANBOK) && p_type&OPTION_HANBOK)
  7000. new_look = -1;
  7001. if (sd->disguise || !new_look)
  7002. return 0; //Disguises break sprite changes
  7003. if (new_look < 0) { //Restore normal look.
  7004. status_set_viewdata(&sd->bl, sd->status.class_);
  7005. new_look = sd->vd.class_;
  7006. }
  7007. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7008. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7009. if (sd->vd.cloth_color)
  7010. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7011. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7012. return 0;
  7013. }
  7014. /*==========================================
  7015. * Give player a cart
  7016. *------------------------------------------*/
  7017. int pc_setcart(struct map_session_data *sd,int type) {
  7018. #ifndef NEW_CARTS
  7019. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7020. int option;
  7021. #endif
  7022. nullpo_ret(sd);
  7023. if( type < 0 || type > MAX_CARTS )
  7024. return 1;// Never trust the values sent by the client! [Skotlex]
  7025. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7026. return 1;// Push cart is required
  7027. if( type == 0 && pc_iscarton(sd) )
  7028. status_change_end(&sd->bl,SC_GN_CARTBOOST,INVALID_TIMER);
  7029. #ifdef NEW_CARTS
  7030. switch( type ) {
  7031. case 0:
  7032. if( !sd->sc.data[SC_PUSH_CART] )
  7033. return 0;
  7034. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7035. clif_clearcart(sd->fd);
  7036. break;
  7037. default:/* everything else is an allowed ID so we can move on */
  7038. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7039. clif_cartlist(sd);
  7040. clif_updatestatus(sd, SP_CARTINFO);
  7041. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  7042. clif_status_load_notick(&sd->bl, SI_ON_PUSH_CART, 2 , type, 0, 0);
  7043. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  7044. sd->sc.data[SC_PUSH_CART]->val1 = type;
  7045. break;
  7046. }
  7047. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7048. status_calc_pc(sd,0); //Recalc speed penalty.
  7049. #else
  7050. // Update option
  7051. option = sd->sc.option;
  7052. option &= ~OPTION_CART;// clear cart bits
  7053. option |= cart[type]; // set cart
  7054. pc_setoption(sd, option);
  7055. #endif
  7056. return 0;
  7057. }
  7058. /*==========================================
  7059. * Give player a falcon
  7060. *------------------------------------------*/
  7061. int pc_setfalcon(TBL_PC* sd, int flag)
  7062. {
  7063. if( flag ){
  7064. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7065. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7066. } else if( pc_isfalcon(sd) ){
  7067. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7068. }
  7069. return 0;
  7070. }
  7071. /*==========================================
  7072. * Set player riding
  7073. *------------------------------------------*/
  7074. int pc_setriding(TBL_PC* sd, int flag)
  7075. {
  7076. if( flag ){
  7077. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7078. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7079. } else if( pc_isriding(sd) ){
  7080. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7081. }
  7082. return 0;
  7083. }
  7084. /*==========================================
  7085. * Give player a mado
  7086. *------------------------------------------*/
  7087. int pc_setmadogear(TBL_PC* sd, int flag)
  7088. {
  7089. if( flag ){
  7090. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7091. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7092. } else if( pc_ismadogear(sd) ){
  7093. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7094. }
  7095. return 0;
  7096. }
  7097. /*==========================================
  7098. * Check if player can drop an item
  7099. *------------------------------------------*/
  7100. int pc_candrop(struct map_session_data *sd, struct item *item)
  7101. {
  7102. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7103. return 0;
  7104. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7105. return 0;
  7106. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7107. }
  7108. /*==========================================
  7109. * Read ram register for player sd
  7110. * get val (int) from reg for player sd
  7111. *------------------------------------------*/
  7112. int pc_readreg(struct map_session_data* sd, int reg)
  7113. {
  7114. int i;
  7115. nullpo_ret(sd);
  7116. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7117. return ( i < sd->reg_num ) ? sd->reg[i].data : 0;
  7118. }
  7119. /*==========================================
  7120. * Set ram register for player sd
  7121. * memo val(int) at reg for player sd
  7122. *------------------------------------------*/
  7123. int pc_setreg(struct map_session_data* sd, int reg, int val)
  7124. {
  7125. int i;
  7126. nullpo_ret(sd);
  7127. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7128. if( i < sd->reg_num )
  7129. {// overwrite existing entry
  7130. sd->reg[i].data = val;
  7131. return 1;
  7132. }
  7133. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].data == 0 );
  7134. if( i == sd->reg_num )
  7135. {// nothing free, increase size
  7136. sd->reg_num++;
  7137. RECREATE(sd->reg, struct script_reg, sd->reg_num);
  7138. }
  7139. sd->reg[i].index = reg;
  7140. sd->reg[i].data = val;
  7141. return 1;
  7142. }
  7143. /*==========================================
  7144. * Read ram register for player sd
  7145. * get val (str) from reg for player sd
  7146. *------------------------------------------*/
  7147. char* pc_readregstr(struct map_session_data* sd, int reg)
  7148. {
  7149. int i;
  7150. nullpo_ret(sd);
  7151. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7152. return ( i < sd->regstr_num ) ? sd->regstr[i].data : NULL;
  7153. }
  7154. /*==========================================
  7155. * Set ram register for player sd
  7156. * memo val(str) at reg for player sd
  7157. *------------------------------------------*/
  7158. int pc_setregstr(struct map_session_data* sd, int reg, const char* str)
  7159. {
  7160. int i;
  7161. nullpo_ret(sd);
  7162. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7163. if( i < sd->regstr_num )
  7164. {// found entry, update
  7165. if( str == NULL || *str == '\0' )
  7166. {// empty string
  7167. if( sd->regstr[i].data != NULL )
  7168. aFree(sd->regstr[i].data);
  7169. sd->regstr[i].data = NULL;
  7170. }
  7171. else if( sd->regstr[i].data )
  7172. {// recreate
  7173. size_t len = strlen(str)+1;
  7174. RECREATE(sd->regstr[i].data, char, len);
  7175. memcpy(sd->regstr[i].data, str, len*sizeof(char));
  7176. }
  7177. else
  7178. {// create
  7179. sd->regstr[i].data = aStrdup(str);
  7180. }
  7181. return 1;
  7182. }
  7183. if( str == NULL || *str == '\0' )
  7184. return 1;// nothing to add, empty string
  7185. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].data == NULL );
  7186. if( i == sd->regstr_num )
  7187. {// nothing free, increase size
  7188. sd->regstr_num++;
  7189. RECREATE(sd->regstr, struct script_regstr, sd->regstr_num);
  7190. }
  7191. sd->regstr[i].index = reg;
  7192. sd->regstr[i].data = aStrdup(str);
  7193. return 1;
  7194. }
  7195. int pc_readregistry(struct map_session_data *sd,const char *reg,int type)
  7196. {
  7197. struct global_reg *sd_reg;
  7198. int i,max;
  7199. nullpo_ret(sd);
  7200. switch (type) {
  7201. case 3: //Char reg
  7202. sd_reg = sd->save_reg.global;
  7203. max = sd->save_reg.global_num;
  7204. break;
  7205. case 2: //Account reg
  7206. sd_reg = sd->save_reg.account;
  7207. max = sd->save_reg.account_num;
  7208. break;
  7209. case 1: //Account2 reg
  7210. sd_reg = sd->save_reg.account2;
  7211. max = sd->save_reg.account2_num;
  7212. break;
  7213. default:
  7214. return 0;
  7215. }
  7216. if (max == -1) {
  7217. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7218. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7219. intif_request_registry(sd,type==3?4:type);
  7220. return 0;
  7221. }
  7222. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7223. return ( i < max ) ? atoi(sd_reg[i].value) : 0;
  7224. }
  7225. char* pc_readregistry_str(struct map_session_data *sd,const char *reg,int type)
  7226. {
  7227. struct global_reg *sd_reg;
  7228. int i,max;
  7229. nullpo_ret(sd);
  7230. switch (type) {
  7231. case 3: //Char reg
  7232. sd_reg = sd->save_reg.global;
  7233. max = sd->save_reg.global_num;
  7234. break;
  7235. case 2: //Account reg
  7236. sd_reg = sd->save_reg.account;
  7237. max = sd->save_reg.account_num;
  7238. break;
  7239. case 1: //Account2 reg
  7240. sd_reg = sd->save_reg.account2;
  7241. max = sd->save_reg.account2_num;
  7242. break;
  7243. default:
  7244. return NULL;
  7245. }
  7246. if (max == -1) {
  7247. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7248. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7249. intif_request_registry(sd,type==3?4:type);
  7250. return NULL;
  7251. }
  7252. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7253. return ( i < max ) ? sd_reg[i].value : NULL;
  7254. }
  7255. int pc_setregistry(struct map_session_data *sd,const char *reg,int val,int type)
  7256. {
  7257. struct global_reg *sd_reg;
  7258. int i,*max, regmax;
  7259. nullpo_ret(sd);
  7260. switch( type )
  7261. {
  7262. case 3: //Char reg
  7263. if( !strcmp(reg,"PC_DIE_COUNTER") && sd->die_counter != val )
  7264. {
  7265. i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7266. sd->die_counter = val;
  7267. if( i )
  7268. status_calc_pc(sd,0); // Lost the bonus.
  7269. }
  7270. else if( !strcmp(reg,"COOK_MASTERY") && sd->cook_mastery != val )
  7271. {
  7272. val = cap_value(val, 0, 1999);
  7273. sd->cook_mastery = val;
  7274. }
  7275. sd_reg = sd->save_reg.global;
  7276. max = &sd->save_reg.global_num;
  7277. regmax = GLOBAL_REG_NUM;
  7278. break;
  7279. case 2: //Account reg
  7280. if( !strcmp(reg,"#CASHPOINTS") && sd->cashPoints != val )
  7281. {
  7282. val = cap_value(val, 0, MAX_ZENY);
  7283. sd->cashPoints = val;
  7284. }
  7285. else if( !strcmp(reg,"#KAFRAPOINTS") && sd->kafraPoints != val )
  7286. {
  7287. val = cap_value(val, 0, MAX_ZENY);
  7288. sd->kafraPoints = val;
  7289. }
  7290. sd_reg = sd->save_reg.account;
  7291. max = &sd->save_reg.account_num;
  7292. regmax = ACCOUNT_REG_NUM;
  7293. break;
  7294. case 1: //Account2 reg
  7295. sd_reg = sd->save_reg.account2;
  7296. max = &sd->save_reg.account2_num;
  7297. regmax = ACCOUNT_REG2_NUM;
  7298. break;
  7299. default:
  7300. return 0;
  7301. }
  7302. if (*max == -1) {
  7303. ShowError("pc_setregistry : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7304. return 1;
  7305. }
  7306. // delete reg
  7307. if (val == 0) {
  7308. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7309. if( i < *max )
  7310. {
  7311. if (i != *max - 1)
  7312. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7313. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7314. (*max)--;
  7315. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7316. }
  7317. return 1;
  7318. }
  7319. // change value if found
  7320. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7321. if( i < *max )
  7322. {
  7323. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7324. sd->state.reg_dirty |= 1<<(type-1);
  7325. return 1;
  7326. }
  7327. // add value if not found
  7328. if (i < regmax) {
  7329. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7330. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7331. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7332. (*max)++;
  7333. sd->state.reg_dirty |= 1<<(type-1);
  7334. return 1;
  7335. }
  7336. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7337. return 0;
  7338. }
  7339. int pc_setregistry_str(struct map_session_data *sd,const char *reg,const char *val,int type)
  7340. {
  7341. struct global_reg *sd_reg;
  7342. int i,*max, regmax;
  7343. nullpo_ret(sd);
  7344. if (reg[strlen(reg)-1] != '$') {
  7345. ShowError("pc_setregistry_str : reg %s must be string (end in '$') to use this!\n", reg);
  7346. return 0;
  7347. }
  7348. switch (type) {
  7349. case 3: //Char reg
  7350. sd_reg = sd->save_reg.global;
  7351. max = &sd->save_reg.global_num;
  7352. regmax = GLOBAL_REG_NUM;
  7353. break;
  7354. case 2: //Account reg
  7355. sd_reg = sd->save_reg.account;
  7356. max = &sd->save_reg.account_num;
  7357. regmax = ACCOUNT_REG_NUM;
  7358. break;
  7359. case 1: //Account2 reg
  7360. sd_reg = sd->save_reg.account2;
  7361. max = &sd->save_reg.account2_num;
  7362. regmax = ACCOUNT_REG2_NUM;
  7363. break;
  7364. default:
  7365. return 0;
  7366. }
  7367. if (*max == -1) {
  7368. ShowError("pc_setregistry_str : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7369. return 0;
  7370. }
  7371. // delete reg
  7372. if (!val || strcmp(val,"")==0)
  7373. {
  7374. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7375. if( i < *max )
  7376. {
  7377. if (i != *max - 1)
  7378. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7379. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7380. (*max)--;
  7381. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7382. if (type!=3) intif_saveregistry(sd,type);
  7383. }
  7384. return 1;
  7385. }
  7386. // change value if found
  7387. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7388. if( i < *max )
  7389. {
  7390. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7391. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7392. if (type!=3) intif_saveregistry(sd,type);
  7393. return 1;
  7394. }
  7395. // add value if not found
  7396. if (i < regmax) {
  7397. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7398. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7399. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7400. (*max)++;
  7401. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7402. if (type!=3) intif_saveregistry(sd,type);
  7403. return 1;
  7404. }
  7405. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7406. return 0;
  7407. }
  7408. /*==========================================
  7409. * Exec eventtimer for player sd (retrieved from map_session (id))
  7410. *------------------------------------------*/
  7411. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  7412. {
  7413. struct map_session_data *sd=map_id2sd(id);
  7414. char *p = (char *)data;
  7415. int i;
  7416. if(sd==NULL)
  7417. return 0;
  7418. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  7419. if( i < MAX_EVENTTIMER )
  7420. {
  7421. sd->eventtimer[i] = INVALID_TIMER;
  7422. sd->eventcount--;
  7423. npc_event(sd,p,0);
  7424. }
  7425. else
  7426. ShowError("pc_eventtimer: no such event timer\n");
  7427. if (p) aFree(p);
  7428. return 0;
  7429. }
  7430. /*==========================================
  7431. * Add eventtimer for player sd ?
  7432. *------------------------------------------*/
  7433. int pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  7434. {
  7435. int i;
  7436. nullpo_ret(sd);
  7437. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  7438. if( i == MAX_EVENTTIMER )
  7439. return 0;
  7440. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  7441. sd->eventcount++;
  7442. return 1;
  7443. }
  7444. /*==========================================
  7445. * Del eventtimer for player sd ?
  7446. *------------------------------------------*/
  7447. int pc_deleventtimer(struct map_session_data *sd,const char *name)
  7448. {
  7449. char* p = NULL;
  7450. int i;
  7451. nullpo_ret(sd);
  7452. if (sd->eventcount <= 0)
  7453. return 0;
  7454. // find the named event timer
  7455. ARR_FIND( 0, MAX_EVENTTIMER, i,
  7456. sd->eventtimer[i] != INVALID_TIMER &&
  7457. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  7458. strcmp(p, name) == 0
  7459. );
  7460. if( i == MAX_EVENTTIMER )
  7461. return 0; // not found
  7462. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7463. sd->eventtimer[i] = INVALID_TIMER;
  7464. sd->eventcount--;
  7465. aFree(p);
  7466. return 1;
  7467. }
  7468. /*==========================================
  7469. * Update eventtimer count for player sd
  7470. *------------------------------------------*/
  7471. int pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  7472. {
  7473. int i;
  7474. nullpo_ret(sd);
  7475. for(i=0;i<MAX_EVENTTIMER;i++)
  7476. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  7477. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  7478. addtick_timer(sd->eventtimer[i],tick);
  7479. break;
  7480. }
  7481. return 0;
  7482. }
  7483. /*==========================================
  7484. * Remove all eventtimer for player sd
  7485. *------------------------------------------*/
  7486. int pc_cleareventtimer(struct map_session_data *sd)
  7487. {
  7488. int i;
  7489. nullpo_ret(sd);
  7490. if (sd->eventcount <= 0)
  7491. return 0;
  7492. for(i=0;i<MAX_EVENTTIMER;i++)
  7493. if( sd->eventtimer[i] != INVALID_TIMER ){
  7494. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  7495. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7496. sd->eventtimer[i] = INVALID_TIMER;
  7497. sd->eventcount--;
  7498. if (p) aFree(p);
  7499. }
  7500. return 0;
  7501. }
  7502. /**
  7503. * Called when an item with combo is worn
  7504. * @param *sd
  7505. * @param *data struct item_data
  7506. * @return success numbers of succeed combo
  7507. */
  7508. int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  7509. int i, j, k, z;
  7510. int index, idx, success = 0;
  7511. for( i = 0; i < data->combos_count; i++ ) {
  7512. int *combo_idx = aMalloc(data->combos[i]->count);
  7513. /* ensure this isn't a duplicate combo */
  7514. if( sd->combos.bonus != NULL ) {
  7515. int x;
  7516. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7517. /* found a match, skip this combo */
  7518. if( x < sd->combos.count )
  7519. continue;
  7520. }
  7521. for( j = 0; j < data->combos[i]->count; j++ ) {
  7522. int id = data->combos[i]->nameid[j];
  7523. bool found = false;
  7524. for( k = 0; k < EQI_MAX; k++ ) {
  7525. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  7526. index = sd->equip_index[k];
  7527. if( index < 0 ) continue;
  7528. if( k == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  7529. if( k == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  7530. if( k == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  7531. if(!sd->inventory_data[index])
  7532. continue;
  7533. if(j>0){
  7534. for (z = 0; z < data->combos[i]->count; z++)
  7535. if(combo_idx[z] == index) //we already have that index recorded
  7536. do_continue=true;
  7537. if(do_continue)
  7538. continue;
  7539. }
  7540. if ( itemdb_type(id) != IT_CARD ) {
  7541. if ( sd->inventory_data[index]->nameid != id )
  7542. continue;
  7543. combo_idx[j] = index;
  7544. found = true;
  7545. break;
  7546. } else { //Cards
  7547. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  7548. continue;
  7549. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  7550. if (sd->status.inventory[index].card[z] != id)
  7551. continue;
  7552. combo_idx[j] = index;
  7553. found = true;
  7554. break;
  7555. }
  7556. }
  7557. }
  7558. if( !found )
  7559. break;/* we haven't found all the ids for this combo, so we can return */
  7560. }
  7561. aFree(combo_idx);
  7562. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  7563. if( j < data->combos[i]->count )
  7564. continue;
  7565. /* we got here, means all items in the combo are matching */
  7566. idx = sd->combos.count;
  7567. if( sd->combos.bonus == NULL ) {
  7568. CREATE(sd->combos.bonus, struct script_code *, 1);
  7569. CREATE(sd->combos.id, unsigned short, 1);
  7570. sd->combos.count = 1;
  7571. } else {
  7572. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  7573. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  7574. }
  7575. /* we simply copy the pointer */
  7576. sd->combos.bonus[idx] = data->combos[i]->script;
  7577. /* save this combo's id */
  7578. sd->combos.id[idx] = data->combos[i]->id;
  7579. success++;
  7580. }
  7581. return success;
  7582. }
  7583. /**
  7584. * Called when an item with combo is removed
  7585. * @param *sd
  7586. * @param *data struct item_data
  7587. * @return retval numbers of removed combo
  7588. */
  7589. int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  7590. int i, retval = 0;
  7591. if( sd->combos.bonus == NULL )
  7592. return 0;/* nothing to do here, player has no combos */
  7593. for( i = 0; i < data->combos_count; i++ ) {
  7594. /* check if this combo exists in this user */
  7595. int x = 0, cursor = 0, j;
  7596. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7597. /* no match, skip this combo */
  7598. if( !(x < sd->combos.count) )
  7599. continue;
  7600. sd->combos.bonus[x] = NULL;
  7601. sd->combos.id[x] = 0;
  7602. retval++;
  7603. /* move next value to empty slot */
  7604. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  7605. if( sd->combos.bonus[j] == NULL )
  7606. continue;
  7607. if( cursor != j ) {
  7608. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  7609. sd->combos.id[cursor] = sd->combos.id[j];
  7610. }
  7611. cursor++;
  7612. }
  7613. /* check if combo requirements still fit */
  7614. if( pc_checkcombo( sd, data ) )
  7615. continue;
  7616. /* it's empty, we can clear all the memory */
  7617. if( (sd->combos.count = cursor) == 0 ) {
  7618. aFree(sd->combos.bonus);
  7619. aFree(sd->combos.id);
  7620. sd->combos.bonus = NULL;
  7621. sd->combos.id = NULL;
  7622. return retval; /* we also can return at this point for we have no more combos to check */
  7623. }
  7624. }
  7625. return retval;
  7626. }
  7627. /**
  7628. * Load combo data(s) of player
  7629. * @param *sd
  7630. * @return ret numbers of succeed combo
  7631. */
  7632. int pc_load_combo(struct map_session_data *sd) {
  7633. int i, ret = 0;
  7634. for( i = 0; i < EQI_MAX; i++ ) {
  7635. struct item_data *id = NULL;
  7636. int idx = sd->equip_index[i];
  7637. if( sd->equip_index[i] < 0 || !(id = sd->inventory_data[idx] ) )
  7638. continue;
  7639. if( id->combos_count )
  7640. ret += pc_checkcombo(sd,id);
  7641. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  7642. struct item_data *data;
  7643. int j;
  7644. for( j = 0; j < id->slot; j++ ) {
  7645. if (!sd->status.inventory[idx].card[j])
  7646. continue;
  7647. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  7648. if( data->combos_count )
  7649. ret += pc_checkcombo(sd,data);
  7650. }
  7651. }
  7652. }
  7653. }
  7654. return ret;
  7655. }
  7656. /*==========================================
  7657. * Equip item on player sd at req_pos from inventory index n
  7658. *------------------------------------------*/
  7659. int pc_equipitem(struct map_session_data *sd,int n,int req_pos)
  7660. {
  7661. int i,pos,flag=0,iflag;
  7662. struct item_data *id;
  7663. nullpo_ret(sd);
  7664. if( n < 0 || n >= MAX_INVENTORY ) {
  7665. clif_equipitemack(sd,0,0,0);
  7666. return 0;
  7667. }
  7668. if( sd->sc.count && (
  7669. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON) ||
  7670. sd->sc.data[SC_BERSERK] ||
  7671. sd->sc.data[SC_SATURDAYNIGHTFEVER]) )
  7672. {
  7673. clif_equipitemack(sd,0,0,0);
  7674. return 0;
  7675. }
  7676. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  7677. clif_equipitemack(sd,n,0,0);
  7678. return 0;
  7679. }
  7680. if (!(id = sd->inventory_data[n]))
  7681. return 0;
  7682. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  7683. if(battle_config.battle_log)
  7684. ShowInfo("equip %d(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  7685. if(!pc_isequip(sd,n) || !(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  7686. // FIXME: pc_isequip: equip level failure uses 2 instead of 0
  7687. clif_equipitemack(sd,n,0,0); // fail
  7688. return 0;
  7689. }
  7690. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  7691. pos = req_pos&EQP_ACC;
  7692. if (pos == EQP_ACC) //User specified both slots..
  7693. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  7694. }
  7695. if(pos == EQP_SHADOW_ACC) { // Shadow System
  7696. pos = req_pos&EQP_SHADOW_ACC;
  7697. if (pos == EQP_SHADOW_ACC)
  7698. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  7699. }
  7700. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  7701. pos = (req_pos&EQP_ARMS);
  7702. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  7703. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  7704. }
  7705. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  7706. //Update skill-block range database when weapon range changes. [Skotlex]
  7707. i = sd->equip_index[EQI_HAND_R];
  7708. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  7709. flag = 1;
  7710. else
  7711. flag = id->range != sd->inventory_data[i]->range;
  7712. }
  7713. for(i=0;i<EQI_MAX;i++) {
  7714. if(pos & equip_pos[i]) {
  7715. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  7716. pc_unequipitem(sd,sd->equip_index[i],2);
  7717. sd->equip_index[i] = n;
  7718. }
  7719. }
  7720. if(pos==EQP_AMMO) {
  7721. clif_arrowequip(sd,n);
  7722. clif_arrow_fail(sd,3);
  7723. }
  7724. else
  7725. clif_equipitemack(sd,n,pos,1);
  7726. sd->status.inventory[n].equip=pos;
  7727. if(pos & EQP_HAND_R) {
  7728. if(id)
  7729. sd->weapontype1 = id->look;
  7730. else
  7731. sd->weapontype1 = 0;
  7732. pc_calcweapontype(sd);
  7733. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7734. }
  7735. if(pos & EQP_HAND_L) {
  7736. if(id) {
  7737. if(id->type == IT_WEAPON) {
  7738. sd->status.shield = 0;
  7739. sd->weapontype2 = id->look;
  7740. }
  7741. else
  7742. if(id->type == IT_ARMOR) {
  7743. sd->status.shield = id->look;
  7744. sd->weapontype2 = 0;
  7745. }
  7746. }
  7747. else
  7748. sd->status.shield = sd->weapontype2 = 0;
  7749. pc_calcweapontype(sd);
  7750. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7751. }
  7752. //Added check to prevent sending the same look on multiple slots ->
  7753. //causes client to redraw item on top of itself. (suggested by Lupus)
  7754. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  7755. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  7756. sd->status.head_bottom = id->look;
  7757. else
  7758. sd->status.head_bottom = 0;
  7759. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7760. }
  7761. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  7762. if(id)
  7763. sd->status.head_top = id->look;
  7764. else
  7765. sd->status.head_top = 0;
  7766. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7767. }
  7768. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  7769. if(id && !(pos&EQP_HEAD_TOP))
  7770. sd->status.head_mid = id->look;
  7771. else
  7772. sd->status.head_mid = 0;
  7773. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7774. }
  7775. if(pos & EQP_COSTUME_HEAD_TOP) {
  7776. if(id){
  7777. sd->status.head_top = id->look;
  7778. } else
  7779. sd->status.head_top = 0;
  7780. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7781. }
  7782. if(pos & EQP_COSTUME_HEAD_MID) {
  7783. if(id && !(pos&EQP_HEAD_TOP)){
  7784. sd->status.head_mid = id->look;
  7785. } else
  7786. sd->status.head_mid = 0;
  7787. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7788. }
  7789. if(pos & EQP_COSTUME_HEAD_LOW) {
  7790. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  7791. sd->status.head_bottom = id->look;
  7792. } else
  7793. sd->status.head_bottom = 0;
  7794. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7795. }
  7796. if(pos & EQP_SHOES)
  7797. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7798. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7799. sd->status.robe = id ? id->look : 0;
  7800. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  7801. }
  7802. if(pos & EQP_COSTUME_GARMENT) {
  7803. sd->status.robe = id ? id->look : 0;
  7804. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7805. }
  7806. pc_checkallowskill(sd); //Check if status changes should be halted.
  7807. iflag = sd->npc_item_flag;
  7808. /* check for combos (MUST be before status_calc_pc) */
  7809. if( id->combos_count )
  7810. pc_checkcombo(sd,id);
  7811. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7812. ; //No cards
  7813. else {
  7814. for( i = 0; i < id->slot; i++ ) {
  7815. struct item_data *data;
  7816. if (!sd->status.inventory[n].card[i])
  7817. continue;
  7818. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7819. if( data->combos_count )
  7820. pc_checkcombo(sd,data);
  7821. }
  7822. }
  7823. }
  7824. status_calc_pc(sd,0);
  7825. if (flag) //Update skill data
  7826. clif_skillinfoblock(sd);
  7827. //OnEquip script [Skotlex]
  7828. if (id) {
  7829. //only run the script if item isn't restricted
  7830. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  7831. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7832. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7833. ; //No cards
  7834. else {
  7835. for( i = 0; i < id->slot; i++ ) {
  7836. struct item_data *data;
  7837. if (!sd->status.inventory[n].card[i])
  7838. continue;
  7839. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7840. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  7841. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7842. }
  7843. }
  7844. }
  7845. }
  7846. sd->npc_item_flag = iflag;
  7847. return 0;
  7848. }
  7849. /*==========================================
  7850. * Called when attemting to unequip an item from player
  7851. * type:
  7852. * 0 - only unequip
  7853. * 1 - calculate status after unequipping
  7854. * 2 - force unequip
  7855. *------------------------------------------*/
  7856. int pc_unequipitem(struct map_session_data *sd,int n,int flag) {
  7857. int i,iflag;
  7858. bool status_cacl = false;
  7859. nullpo_ret(sd);
  7860. if( n < 0 || n >= MAX_INVENTORY ) {
  7861. clif_unequipitemack(sd,0,0,0);
  7862. return 0;
  7863. }
  7864. // status change that makes player cannot unequip equipment
  7865. if( !(flag&2) && sd->sc.count && (
  7866. sd->sc.data[SC_BERSERK] ||
  7867. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  7868. sd->sc.data[SC__BLOODYLUST] ||
  7869. sd->sc.data[SC_KYOUGAKU] ||
  7870. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) // can't switch weapon
  7871. {
  7872. clif_unequipitemack(sd,n,0,0);
  7873. return 0;
  7874. }
  7875. if (&sd->sc) {
  7876. if (sd->sc.data[SC_HEAT_BARREL])
  7877. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  7878. if (sd->sc.data[SC_P_ALTER] && (sd->inventory_data[n]->type == IT_WEAPON || sd->inventory_data[n]->type == IT_AMMO))
  7879. status_change_end(&sd->bl,SC_P_ALTER,INVALID_TIMER);
  7880. }
  7881. if(battle_config.battle_log)
  7882. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  7883. if(!sd->status.inventory[n].equip){ //Nothing to unequip
  7884. clif_unequipitemack(sd,n,0,0);
  7885. return 0;
  7886. }
  7887. for(i=0;i<EQI_MAX;i++) {
  7888. if(sd->status.inventory[n].equip & equip_pos[i])
  7889. sd->equip_index[i] = -1;
  7890. }
  7891. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  7892. sd->weapontype1 = 0;
  7893. sd->status.weapon = sd->weapontype2;
  7894. pc_calcweapontype(sd);
  7895. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7896. if( !battle_config.dancing_weaponswitch_fix )
  7897. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  7898. }
  7899. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  7900. sd->status.shield = sd->weapontype2 = 0;
  7901. pc_calcweapontype(sd);
  7902. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7903. }
  7904. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  7905. sd->status.head_bottom = 0;
  7906. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7907. }
  7908. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  7909. sd->status.head_top = 0;
  7910. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7911. }
  7912. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  7913. sd->status.head_mid = 0;
  7914. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7915. }
  7916. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  7917. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  7918. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7919. }
  7920. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  7921. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  7922. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7923. }
  7924. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  7925. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  7926. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7927. }
  7928. if(sd->status.inventory[n].equip & EQP_SHOES)
  7929. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7930. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7931. sd->status.robe = 0;
  7932. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  7933. }
  7934. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  7935. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  7936. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7937. }
  7938. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  7939. if((sd->status.inventory[n].equip & EQP_ARMS) &&
  7940. sd->weapontype1 == 0 && sd->weapontype2 == 0 && (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
  7941. skill_enchant_elemental_end(&sd->bl,-1);
  7942. if(sd->status.inventory[n].equip & EQP_ARMOR) {
  7943. // On Armor Change...
  7944. status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
  7945. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  7946. }
  7947. if( sd->state.autobonus&sd->status.inventory[n].equip )
  7948. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  7949. sd->status.inventory[n].equip=0;
  7950. iflag = sd->npc_item_flag;
  7951. /* check for combos (MUST be before status_calc_pc) */
  7952. if ( sd->inventory_data[n] ) {
  7953. if( sd->inventory_data[n]->combos_count ) {
  7954. if( pc_removecombo(sd,sd->inventory_data[n]) )
  7955. status_cacl = true;
  7956. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7957. ; //No cards
  7958. else {
  7959. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7960. struct item_data *data;
  7961. if (!sd->status.inventory[n].card[i])
  7962. continue;
  7963. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7964. if( data->combos_count ) {
  7965. if( pc_removecombo(sd,data) )
  7966. status_cacl = true;
  7967. }
  7968. }
  7969. }
  7970. }
  7971. }
  7972. if(flag&1 || status_cacl) {
  7973. pc_checkallowskill(sd);
  7974. status_calc_pc(sd,0);
  7975. }
  7976. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  7977. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  7978. //OnUnEquip script [Skotlex]
  7979. if (sd->inventory_data[n]) {
  7980. if (sd->inventory_data[n]->unequip_script)
  7981. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7982. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7983. ; //No cards
  7984. else {
  7985. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7986. struct item_data *data;
  7987. if (!sd->status.inventory[n].card[i])
  7988. continue;
  7989. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7990. if( data->unequip_script )
  7991. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7992. }
  7993. }
  7994. }
  7995. }
  7996. sd->npc_item_flag = iflag;
  7997. return 0;
  7998. }
  7999. /*==========================================
  8000. * Checking if player (sd) has an invalid item
  8001. * and is unequiped on map load (item_noequip)
  8002. *------------------------------------------*/
  8003. int pc_checkitem(struct map_session_data *sd) {
  8004. int i, calc_flag = 0;
  8005. struct item it;
  8006. nullpo_ret(sd);
  8007. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8008. return 0;
  8009. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8010. it = sd->status.inventory[i];
  8011. if( it.nameid == 0 )
  8012. continue;
  8013. if( !it.equip )
  8014. continue;
  8015. if( it.equip&~pc_equippoint(sd,i) ) {
  8016. pc_unequipitem(sd, i, 2);
  8017. calc_flag = 1;
  8018. continue;
  8019. }
  8020. 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) ) {
  8021. pc_unequipitem(sd, i, 2);
  8022. calc_flag = 1;
  8023. continue;
  8024. }
  8025. }
  8026. if( calc_flag && sd->state.active ) {
  8027. pc_checkallowskill(sd);
  8028. status_calc_pc(sd,0);
  8029. }
  8030. return 0;
  8031. }
  8032. /*==========================================
  8033. * Checks for unavailable items and removes them.
  8034. *------------------------------------------*/
  8035. int pc_check_available_item(struct map_session_data *sd) {
  8036. int i, it;
  8037. char output[256];
  8038. nullpo_ret(sd);
  8039. if( battle_config.item_check&1 ) { // Check for invalid(ated) items in inventory.
  8040. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8041. it = sd->status.inventory[i].nameid;
  8042. if( it && !itemdb_available(it) ) {
  8043. sprintf(output, msg_txt(sd, 709), it); // Item %d has been removed from your inventory.
  8044. clif_displaymessage(sd->fd, output);
  8045. ShowWarning("Removed invalid/disabled item id %d from inventory (amount=%d, char_id=%d).\n", it, sd->status.inventory[i].amount, sd->status.char_id);
  8046. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8047. }
  8048. }
  8049. }
  8050. if( battle_config.item_check&2 ) { // Check for invalid(ated) items in cart.
  8051. for( i = 0; i < MAX_CART; i++ ) {
  8052. it = sd->status.cart[i].nameid;
  8053. if( it && !itemdb_available(it) ) {
  8054. sprintf(output, msg_txt(sd, 710), it); // Item %d has been removed from your cart.
  8055. clif_displaymessage(sd->fd, output);
  8056. ShowWarning("Removed invalid/disabled item id %d from cart (amount=%d, char_id=%d).\n", it, sd->status.cart[i].amount, sd->status.char_id);
  8057. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8058. }
  8059. }
  8060. }
  8061. if( battle_config.item_check&4 ) { // Check for invalid(ated) items in storage.
  8062. for( i = 0; i < sd->storage_size; i++ ) {
  8063. it = sd->status.storage.items[i].nameid;
  8064. if( it && !itemdb_available(it) ) {
  8065. sprintf(output, msg_txt(sd, 711), it); // Item %d has been removed from your storage.
  8066. clif_displaymessage(sd->fd, output);
  8067. ShowWarning("Removed invalid/disabled item id %d from storage (amount=%d, char_id=%d).\n", it, sd->status.storage.items[i].amount, sd->status.char_id);
  8068. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8069. }
  8070. }
  8071. }
  8072. return 0;
  8073. }
  8074. /*==========================================
  8075. * Update PVP rank for sd1 in cmp to sd2
  8076. *------------------------------------------*/
  8077. int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8078. {
  8079. struct map_session_data *sd1,*sd2;
  8080. sd1=(struct map_session_data *)bl;
  8081. sd2=va_arg(ap,struct map_session_data *);
  8082. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8083. {// cannot register pvp rank for hidden GMs
  8084. return 0;
  8085. }
  8086. if( sd1->pvp_point > sd2->pvp_point )
  8087. sd2->pvp_rank++;
  8088. return 0;
  8089. }
  8090. /*==========================================
  8091. * Calculate new rank beetween all present players (map_foreachinarea)
  8092. * and display result
  8093. *------------------------------------------*/
  8094. int pc_calc_pvprank(struct map_session_data *sd)
  8095. {
  8096. int old;
  8097. struct map_data *m;
  8098. m=&map[sd->bl.m];
  8099. old=sd->pvp_rank;
  8100. sd->pvp_rank=1;
  8101. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8102. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8103. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8104. return sd->pvp_rank;
  8105. }
  8106. /*==========================================
  8107. * Calculate next sd ranking calculation from config
  8108. *------------------------------------------*/
  8109. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8110. {
  8111. struct map_session_data *sd;
  8112. sd=map_id2sd(id);
  8113. if(sd==NULL)
  8114. return 0;
  8115. sd->pvp_timer = INVALID_TIMER;
  8116. if( sd->sc.option&OPTION_INVISIBLE )
  8117. {// do not calculate the pvp rank for a hidden GM
  8118. return 0;
  8119. }
  8120. if( pc_calc_pvprank(sd) > 0 )
  8121. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8122. return 0;
  8123. }
  8124. /*==========================================
  8125. * Checking if sd is married
  8126. * Return:
  8127. * partner_id = yes
  8128. * 0 = no
  8129. *------------------------------------------*/
  8130. int pc_ismarried(struct map_session_data *sd)
  8131. {
  8132. if(sd == NULL)
  8133. return -1;
  8134. if(sd->status.partner_id > 0)
  8135. return sd->status.partner_id;
  8136. else
  8137. return 0;
  8138. }
  8139. /*==========================================
  8140. * Marry player sd to player dstsd
  8141. * Return:
  8142. * -1 = fail
  8143. * 0 = success
  8144. *------------------------------------------*/
  8145. int pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  8146. {
  8147. if(sd == NULL || dstsd == NULL ||
  8148. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  8149. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  8150. return -1;
  8151. sd->status.partner_id = dstsd->status.char_id;
  8152. dstsd->status.partner_id = sd->status.char_id;
  8153. return 0;
  8154. }
  8155. /*==========================================
  8156. * Divorce sd from its partner
  8157. * Return:
  8158. * -1 = fail
  8159. * 0 = success
  8160. *------------------------------------------*/
  8161. int pc_divorce(struct map_session_data *sd)
  8162. {
  8163. struct map_session_data *p_sd;
  8164. int i;
  8165. if( sd == NULL || !pc_ismarried(sd) )
  8166. return -1;
  8167. if( !sd->status.partner_id )
  8168. return -1; // Char is not married
  8169. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8170. { // Lets char server do the divorce
  8171. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8172. return -1; // No char server connected
  8173. return 0;
  8174. }
  8175. // Both players online, lets do the divorce manually
  8176. sd->status.partner_id = 0;
  8177. p_sd->status.partner_id = 0;
  8178. for( i = 0; i < MAX_INVENTORY; i++ )
  8179. {
  8180. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8181. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8182. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8183. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8184. }
  8185. clif_divorced(sd, p_sd->status.name);
  8186. clif_divorced(p_sd, sd->status.name);
  8187. return 0;
  8188. }
  8189. /*==========================================
  8190. * Get sd partner charid. (Married partner)
  8191. *------------------------------------------*/
  8192. struct map_session_data *pc_get_partner(struct map_session_data *sd)
  8193. {
  8194. if (sd && pc_ismarried(sd))
  8195. // charid2sd returns NULL if not found
  8196. return map_charid2sd(sd->status.partner_id);
  8197. return NULL;
  8198. }
  8199. /*==========================================
  8200. * Get sd father charid. (Need to be baby)
  8201. *------------------------------------------*/
  8202. struct map_session_data *pc_get_father (struct map_session_data *sd)
  8203. {
  8204. if (sd && sd->class_&JOBL_BABY && sd->status.father > 0)
  8205. // charid2sd returns NULL if not found
  8206. return map_charid2sd(sd->status.father);
  8207. return NULL;
  8208. }
  8209. /*==========================================
  8210. * Get sd mother charid. (Need to be baby)
  8211. *------------------------------------------*/
  8212. struct map_session_data *pc_get_mother (struct map_session_data *sd)
  8213. {
  8214. if (sd && sd->class_&JOBL_BABY && sd->status.mother > 0)
  8215. // charid2sd returns NULL if not found
  8216. return map_charid2sd(sd->status.mother);
  8217. return NULL;
  8218. }
  8219. /*==========================================
  8220. * Get sd children charid. (Need to be married)
  8221. *------------------------------------------*/
  8222. struct map_session_data *pc_get_child (struct map_session_data *sd)
  8223. {
  8224. if (sd && pc_ismarried(sd) && sd->status.child > 0)
  8225. // charid2sd returns NULL if not found
  8226. return map_charid2sd(sd->status.child);
  8227. return NULL;
  8228. }
  8229. /*==========================================
  8230. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  8231. *------------------------------------------*/
  8232. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  8233. {
  8234. int hp = 0, sp = 0;
  8235. if( pc_isdead(sd) )
  8236. return;
  8237. if (sd->hp_loss.value) {
  8238. sd->hp_loss.tick += diff_tick;
  8239. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  8240. hp += sd->hp_loss.value;
  8241. sd->hp_loss.tick -= sd->hp_loss.rate;
  8242. }
  8243. if(hp >= sd->battle_status.hp)
  8244. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  8245. }
  8246. if (sd->sp_loss.value) {
  8247. sd->sp_loss.tick += diff_tick;
  8248. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  8249. sp += sd->sp_loss.value;
  8250. sd->sp_loss.tick -= sd->sp_loss.rate;
  8251. }
  8252. }
  8253. if (hp > 0 || sp > 0)
  8254. status_zap(&sd->bl, hp, sp);
  8255. return;
  8256. }
  8257. //Character regen. Flag is used to know which types of regen can take place.
  8258. //&1: HP regen
  8259. //&2: SP regen
  8260. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  8261. {
  8262. int hp = 0, sp = 0;
  8263. if (sd->hp_regen.value) {
  8264. sd->hp_regen.tick += diff_tick;
  8265. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  8266. hp += sd->hp_regen.value;
  8267. sd->hp_regen.tick -= sd->hp_regen.rate;
  8268. }
  8269. }
  8270. if (sd->sp_regen.value) {
  8271. sd->sp_regen.tick += diff_tick;
  8272. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  8273. sp += sd->sp_regen.value;
  8274. sd->sp_regen.tick -= sd->sp_regen.rate;
  8275. }
  8276. }
  8277. if (hp > 0 || sp > 0)
  8278. status_heal(&sd->bl, hp, sp, 0);
  8279. return;
  8280. }
  8281. /*==========================================
  8282. * Memo player sd savepoint. (map,x,y)
  8283. *------------------------------------------*/
  8284. int pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  8285. {
  8286. nullpo_ret(sd);
  8287. sd->status.save_point.map = mapindex;
  8288. sd->status.save_point.x = x;
  8289. sd->status.save_point.y = y;
  8290. return 0;
  8291. }
  8292. /*==========================================
  8293. * Save 1 player data at autosave intervalle
  8294. *------------------------------------------*/
  8295. int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  8296. {
  8297. int interval;
  8298. struct s_mapiterator* iter;
  8299. struct map_session_data* sd;
  8300. static int last_save_id = 0, save_flag = 0;
  8301. if(save_flag == 2) //Someone was saved on last call, normal cycle
  8302. save_flag = 0;
  8303. else
  8304. save_flag = 1; //Noone was saved, so save first found char.
  8305. iter = mapit_getallusers();
  8306. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8307. {
  8308. if(sd->bl.id == last_save_id && save_flag != 1) {
  8309. save_flag = 1;
  8310. continue;
  8311. }
  8312. if(save_flag != 1) //Not our turn to save yet.
  8313. continue;
  8314. //Save char.
  8315. last_save_id = sd->bl.id;
  8316. save_flag = 2;
  8317. chrif_save(sd,0);
  8318. break;
  8319. }
  8320. mapit_free(iter);
  8321. interval = autosave_interval/(map_usercount()+1);
  8322. if(interval < minsave_interval)
  8323. interval = minsave_interval;
  8324. add_timer(gettick()+interval,pc_autosave,0,0);
  8325. return 0;
  8326. }
  8327. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  8328. {
  8329. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  8330. { //Night/day state does not match.
  8331. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  8332. sd->state.night = night_flag;
  8333. return 1;
  8334. }
  8335. return 0;
  8336. }
  8337. /*================================================
  8338. * timer to do the day [Yor]
  8339. * data: 0 = called by timer, 1 = gmcommand/script
  8340. *------------------------------------------------*/
  8341. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  8342. {
  8343. char tmp_soutput[1024];
  8344. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  8345. return 0;
  8346. if (!night_flag)
  8347. return 0; //Already day.
  8348. night_flag = 0; // 0=day, 1=night [Yor]
  8349. map_foreachpc(pc_daynight_timer_sub);
  8350. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  8351. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8352. return 0;
  8353. }
  8354. /*================================================
  8355. * timer to do the night [Yor]
  8356. * data: 0 = called by timer, 1 = gmcommand/script
  8357. *------------------------------------------------*/
  8358. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  8359. {
  8360. char tmp_soutput[1024];
  8361. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  8362. return 0;
  8363. if (night_flag)
  8364. return 0; //Already nigth.
  8365. night_flag = 1; // 0=day, 1=night [Yor]
  8366. map_foreachpc(pc_daynight_timer_sub);
  8367. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  8368. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8369. return 0;
  8370. }
  8371. void pc_setstand(struct map_session_data *sd){
  8372. nullpo_retv(sd);
  8373. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  8374. clif_status_load(&sd->bl,SI_SIT,0);
  8375. clif_standing(&sd->bl); //Inform area PC is standing
  8376. //Reset sitting tick.
  8377. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  8378. sd->state.dead_sit = sd->vd.dead_sit = 0;
  8379. }
  8380. /**
  8381. * Mechanic (MADO GEAR)
  8382. **/
  8383. void pc_overheat(struct map_session_data *sd, int val) {
  8384. int heat = val, skill,
  8385. limit[] = { 10, 20, 28, 46, 66 };
  8386. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  8387. return; // already burning
  8388. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  8389. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  8390. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  8391. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8392. }
  8393. heat = max(0,heat); // Avoid negative HEAT
  8394. if( heat >= limit[skill] )
  8395. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  8396. else
  8397. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  8398. return;
  8399. }
  8400. /**
  8401. * Check if player is autolooting given itemID.
  8402. */
  8403. bool pc_isautolooting(struct map_session_data *sd, int nameid)
  8404. {
  8405. uint8 i = 0;
  8406. bool j = false;
  8407. if (!sd->state.autolooting && !sd->state.autolootingtype)
  8408. return false;
  8409. if (sd->state.autolooting)
  8410. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  8411. if (sd->state.autolootingtype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  8412. j = true;
  8413. return (i != AUTOLOOTITEM_SIZE || j );
  8414. }
  8415. /**
  8416. * Checks if player can use @/#command
  8417. * @param sd Player map session data
  8418. * @param command Command name without @/# and params
  8419. * @param type is it atcommand or charcommand
  8420. */
  8421. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  8422. {
  8423. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  8424. }
  8425. /**
  8426. * Checks if commands used by a player should be logged
  8427. * according to their group setting.
  8428. * @param sd Player map session data
  8429. */
  8430. bool pc_should_log_commands(struct map_session_data *sd)
  8431. {
  8432. return pc_group_should_log_commands(pc_get_group_id(sd));
  8433. }
  8434. static int pc_talisman_timer(int tid, unsigned int tick, int id, intptr_t data)
  8435. {
  8436. struct map_session_data *sd;
  8437. int i, type;
  8438. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  8439. return 1;
  8440. ARR_FIND(1, 5, type, sd->talisman[type] > 0);
  8441. if( sd->talisman[type] <= 0 )
  8442. {
  8443. ShowError("pc_talisman_timer: %d talisman's available. (aid=%d cid=%d tid=%d)\n", sd->talisman[type], sd->status.account_id, sd->status.char_id, tid);
  8444. sd->talisman[type] = 0;
  8445. return 0;
  8446. }
  8447. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == tid);
  8448. if( i == sd->talisman[type] )
  8449. {
  8450. ShowError("pc_talisman_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  8451. return 0;
  8452. }
  8453. sd->talisman[type]--;
  8454. if( i != sd->talisman[type] )
  8455. memmove(sd->talisman_timer[type]+i, sd->talisman_timer[type]+i+1, (sd->talisman[type]-i)*sizeof(int));
  8456. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8457. clif_talisman(sd, type);
  8458. return 0;
  8459. }
  8460. int pc_add_talisman(struct map_session_data *sd,int interval,int max,int type)
  8461. {
  8462. int tid, i;
  8463. nullpo_ret(sd);
  8464. if(max > 10)
  8465. max = 10;
  8466. if(sd->talisman[type] < 0)
  8467. sd->talisman[type] = 0;
  8468. if( sd->talisman[type] && sd->talisman[type] >= max )
  8469. {
  8470. if(sd->talisman_timer[type][0] != INVALID_TIMER)
  8471. delete_timer(sd->talisman_timer[type][0],pc_talisman_timer);
  8472. sd->talisman[type]--;
  8473. if( sd->talisman[type] != 0 )
  8474. memmove(sd->talisman_timer[type]+0, sd->talisman_timer[type]+1, (sd->talisman[type])*sizeof(int));
  8475. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8476. }
  8477. tid = add_timer(gettick()+interval, pc_talisman_timer, sd->bl.id, 0);
  8478. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->talisman_timer[type][i])->tick) < 0);
  8479. if( i != sd->talisman[type] )
  8480. memmove(sd->talisman_timer[type]+i+1, sd->talisman_timer[type]+i, (sd->talisman[type]-i)*sizeof(int));
  8481. sd->talisman_timer[type][i] = tid;
  8482. sd->talisman[type]++;
  8483. clif_talisman(sd, type);
  8484. return 0;
  8485. }
  8486. int pc_del_talisman(struct map_session_data *sd,int count,int type)
  8487. {
  8488. int i;
  8489. nullpo_ret(sd);
  8490. if( sd->talisman[type] <= 0 ) {
  8491. sd->talisman[type] = 0;
  8492. return 0;
  8493. }
  8494. if( count <= 0 )
  8495. return 0;
  8496. if( count > sd->talisman[type] )
  8497. count = sd->talisman[type];
  8498. sd->talisman[type] -= count;
  8499. if( count > 10 )
  8500. count = 10;
  8501. for(i = 0; i < count; i++) {
  8502. if(sd->talisman_timer[type][i] != INVALID_TIMER) {
  8503. delete_timer(sd->talisman_timer[type][i],pc_talisman_timer);
  8504. sd->talisman_timer[type][i] = INVALID_TIMER;
  8505. }
  8506. }
  8507. for(i = count; i < 10; i++) {
  8508. sd->talisman_timer[type][i-count] = sd->talisman_timer[type][i];
  8509. sd->talisman_timer[type][i] = INVALID_TIMER;
  8510. }
  8511. clif_talisman(sd, type);
  8512. return 0;
  8513. }
  8514. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8515. /*==========================================
  8516. * Renewal EXP/Itemdrop rate modifier base on level penalty
  8517. * 1=exp 2=itemdrop
  8518. *------------------------------------------*/
  8519. int pc_level_penalty_mod(struct map_session_data *sd, int mob_level, uint32 mob_race, uint32 mob_mode, int type)
  8520. {
  8521. int diff, rate = 100, i;
  8522. nullpo_ret(sd);
  8523. diff = mob_level - sd->status.base_level;
  8524. if( diff < 0 )
  8525. diff = MAX_LEVEL + ( ~diff + 1 );
  8526. for(i=0; i<RC_MAX; i++){
  8527. int tmp;
  8528. if( mob_race != i ){
  8529. if( mob_mode&MD_BOSS && i < RC_BOSS )
  8530. i = RC_BOSS;
  8531. else if( i <= RC_BOSS )
  8532. continue;
  8533. }
  8534. if( (tmp=level_penalty[type][i][diff]) > 0 ){
  8535. rate = tmp;
  8536. break;
  8537. }
  8538. }
  8539. return rate;
  8540. }
  8541. #endif
  8542. int pc_split_str(char *str,char **val,int num)
  8543. {
  8544. int i;
  8545. for (i=0; i<num && str; i++){
  8546. val[i] = str;
  8547. str = strchr(str,',');
  8548. if (str && i<num-1) //Do not remove a trailing comma.
  8549. *str++=0;
  8550. }
  8551. return i;
  8552. }
  8553. int pc_split_atoi(char* str, int* val, char sep, int max)
  8554. {
  8555. int i,j;
  8556. for (i=0; i<max; i++) {
  8557. if (!str) break;
  8558. val[i] = atoi(str);
  8559. str = strchr(str,sep);
  8560. if (str)
  8561. *str++=0;
  8562. }
  8563. //Zero up the remaining.
  8564. for(j=i; j < max; j++)
  8565. val[j] = 0;
  8566. return i;
  8567. }
  8568. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  8569. {
  8570. static int warning=0;
  8571. int i,j;
  8572. double f;
  8573. for (i=0; i<max; i++) {
  8574. if (!str) break;
  8575. f = atof(str);
  8576. if (f < 0)
  8577. val[i] = 0;
  8578. else if (f > UINT_MAX) {
  8579. val[i] = UINT_MAX;
  8580. if (!warning) {
  8581. warning = 1;
  8582. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  8583. }
  8584. } else
  8585. val[i] = (unsigned int)f;
  8586. str = strchr(str,sep);
  8587. if (str)
  8588. *str++=0;
  8589. }
  8590. //Zero up the remaining.
  8591. for(j=i; j < max; j++)
  8592. val[j] = 0;
  8593. return i;
  8594. }
  8595. /*==========================================
  8596. * sub DB reading.
  8597. * Function used to read skill_tree.txt
  8598. *------------------------------------------*/
  8599. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  8600. {
  8601. unsigned char joblv = 0, skill_lv;
  8602. uint16 skill_id;
  8603. int idx, class_;
  8604. unsigned int i, offset = 3, skill_idx;
  8605. class_ = atoi(fields[0]);
  8606. skill_id = (uint16)atoi(fields[1]);
  8607. skill_lv = (unsigned char)atoi(fields[2]);
  8608. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  8609. {// job level requirement extra column
  8610. joblv = (unsigned char)atoi(fields[3]);
  8611. offset++;
  8612. }
  8613. if(!pcdb_checkid(class_))
  8614. {
  8615. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  8616. return false;
  8617. }
  8618. idx = pc_class2idx(class_);
  8619. //This is to avoid adding two lines for the same skill. [Skotlex]
  8620. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  8621. if( skill_idx == MAX_SKILL_TREE )
  8622. {
  8623. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per class has been reached.\n", skill_id, class_);
  8624. return false;
  8625. }
  8626. else if(skill_tree[idx][skill_idx].id)
  8627. {
  8628. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  8629. }
  8630. skill_tree[idx][skill_idx].id = skill_id;
  8631. skill_tree[idx][skill_idx].max = skill_lv;
  8632. skill_tree[idx][skill_idx].joblv = joblv;
  8633. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  8634. {
  8635. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  8636. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  8637. }
  8638. return true;
  8639. }
  8640. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8641. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  8642. {
  8643. int type, race, diff;
  8644. type = atoi(fields[0]); //1=experience, 2=item drop
  8645. race = atoi(fields[1]);
  8646. diff = atoi(fields[2]);
  8647. if( type != 1 && type != 2 ){
  8648. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  8649. return false;
  8650. }
  8651. if( race < 0 || race > RC_MAX ){
  8652. ShowWarning("pc_readdb_levelpenalty: Invalid race %d specified.\n", race);
  8653. return false;
  8654. }
  8655. diff = min(diff, MAX_LEVEL);
  8656. if( diff < 0 )
  8657. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  8658. level_penalty[type][race][diff] = atoi(fields[3]);
  8659. return true;
  8660. }
  8661. #endif
  8662. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  8663. static bool pc_readdb_job1(char* fields[], int columns, int current){
  8664. int idx, class_;
  8665. unsigned int i;
  8666. #ifndef HP_SP_TABLES
  8667. unsigned int k = 0, val;
  8668. #endif
  8669. class_ = atoi(fields[0]);
  8670. if(!pcdb_checkid(class_))
  8671. {
  8672. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  8673. return false;
  8674. }
  8675. idx = pc_class2idx(class_);
  8676. job_info[idx].max_weight_base = atoi(fields[1]);
  8677. job_info[idx].hp_factor = atoi(fields[2]);
  8678. job_info[idx].hp_multiplicator = atoi(fields[3]);
  8679. job_info[idx].sp_factor = atoi(fields[4]);
  8680. #ifdef RENEWAL_ASPD
  8681. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  8682. #else
  8683. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  8684. #endif
  8685. {
  8686. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  8687. }
  8688. #ifndef HP_SP_TABLES
  8689. for(i = 0; i <= MAX_LEVEL; i++) {
  8690. k += (job_info[idx].hp_factor*(i+1) + 50) / 100;
  8691. val = 35 + ((i+1)*job_info[idx].hp_multiplicator)/100 + k;
  8692. val = min(INT_MAX,val);
  8693. job_info[idx].hp_table[i] = val;
  8694. }
  8695. for(i = 0; i <= MAX_LEVEL; i++) {
  8696. val = 10 + ((i+1)*job_info[idx].sp_factor)/100;
  8697. val = min(INT_MAX,val);
  8698. job_info[idx].sp_table[i] = val;
  8699. }
  8700. #endif
  8701. return true;
  8702. }
  8703. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  8704. static bool pc_readdb_job2(char* fields[], int columns, int current)
  8705. {
  8706. int idx, class_, i;
  8707. class_ = atoi(fields[0]);
  8708. if(!pcdb_checkid(class_))
  8709. {
  8710. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  8711. return false;
  8712. }
  8713. idx = pc_class2idx(class_);
  8714. for(i = 1; i < columns; i++)
  8715. {
  8716. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  8717. }
  8718. return true;
  8719. }
  8720. //Reading job_maxhpsp.txt line
  8721. //startlvl,maxlvl,class,type,values...
  8722. #ifdef HP_SP_TABLES
  8723. static bool pc_readdb_job_maxhpsp(char* fields[], int columns, int current)
  8724. {
  8725. int idx, i,j, maxlvl, startlvl;
  8726. int job_id,job_count,jobs[CLASS_COUNT];
  8727. int type;
  8728. startlvl = atoi(fields[0]);
  8729. if(startlvl > MAX_LEVEL || startlvl<1){
  8730. ShowError("pc_readdb_job_maxhpsp: Invalid start level %d specified.\n", startlvl);
  8731. return false;
  8732. }
  8733. maxlvl = atoi(fields[1]);
  8734. if(maxlvl > MAX_LEVEL || maxlvl<1){
  8735. ShowError("pc_readdb_job_maxhpsp: Invalid max level %d specified.\n", maxlvl);
  8736. return false;
  8737. }
  8738. if((maxlvl-startlvl+1+4) != columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  8739. ShowError("pc_readdb_job_maxhpsp: Number of columns %d defined is too low for start level %d, max level %d.\n",columns,startlvl,maxlvl);
  8740. return false;
  8741. }
  8742. type = atoi(fields[3]);
  8743. if(type < 0 || type > 1){
  8744. ShowError("pc_readdb_job_maxhpsp: Invalid type %d specified.\n", type);
  8745. return false;
  8746. }
  8747. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  8748. if (job_count < 1)
  8749. return false;
  8750. for (j = 0; j < job_count; j++) {
  8751. job_id = jobs[j];
  8752. if(!pcdb_checkid(job_id)){
  8753. ShowError("pc_readdb_job_maxhpsp: Invalid job class %d specified.\n", job_id);
  8754. return false;
  8755. }
  8756. idx = pc_class2idx(job_id);
  8757. if(type == 0) { //hp type
  8758. unsigned int k = 0, val, oldval=0;
  8759. short level = 0;
  8760. for(i = 0; i <= MAX_LEVEL; i++) {
  8761. val = 0;
  8762. k += (job_info[idx].hp_factor*(i+1) + 50) / 100;
  8763. if(i>=startlvl && i <= maxlvl) val = atoi(fields[(i-level)+4]);
  8764. if(val==0) {
  8765. val = 35 + ((i+1)*job_info[idx].hp_multiplicator)/100 + k;
  8766. level++; // This tells us when we didn't use the database so we know what field to grab
  8767. }
  8768. if(oldval > val && i > startlvl && i <= maxlvl) // Let's not warn about the formula table giving us a higher value, only DB
  8769. ShowWarning("pc_readdb_job_maxhpsp: HP value is lower than previous level (job=%d,oldval=%d,val=%d,lvl=%d hp_factor=%d,hp_multiplicator=%d,k=%d).\n",
  8770. job_id,oldval,val,i+1,job_info[idx].hp_factor,job_info[idx].hp_multiplicator,k);
  8771. val = min(INT_MAX,val);
  8772. job_info[idx].hp_table[i] = val;
  8773. oldval = val;
  8774. }
  8775. // ShowInfo("Finished reading HP table for job %d.\n{",job_id);
  8776. // for(i=0; i<=MAX_LEVEL; i++ )
  8777. // printf("%d,",job_info[idx].hp_table[i]);
  8778. // printf("\n}\n");
  8779. }
  8780. else if(type == 1){ //sp type
  8781. unsigned int val, oldval=0;
  8782. short level = 0;
  8783. for(i = 0; i <= MAX_LEVEL; i++) {
  8784. val = 0;
  8785. if(i>=startlvl && i <=maxlvl) val = atoi(fields[(i-level)+4]);
  8786. if(val==0) {
  8787. val = 10 + ((i+1)*job_info[idx].sp_factor)/100;
  8788. level++; // This tells us when we didn't use the database so we know what field to grab
  8789. }
  8790. if(oldval > val && i > startlvl && i <= maxlvl) // Let's not warn about the formula table giving us a higher value, only DB
  8791. ShowWarning("pc_readdb_job_maxhpsp: SP value is lower than previous level (job=%d,oldval=%d,val=%d,lvl=%d,sp_factor=%d).\n",
  8792. job_id,oldval,val,i+1,job_info[idx].sp_factor);
  8793. val = min(INT_MAX,val);
  8794. job_info[idx].sp_table[i] = val;
  8795. oldval = val;
  8796. }
  8797. // ShowInfo("Finished reading SP table for job %d.\n{",job_id);
  8798. // for(i=0; i<MAX_LEVEL; i++ )
  8799. // printf("%d,",job_info[idx].sp_table[i]);
  8800. // printf("\n}\n");
  8801. }
  8802. }
  8803. return true;
  8804. }
  8805. #endif
  8806. //Reading job_exp.txt line
  8807. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  8808. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  8809. {
  8810. int idx, i, type;
  8811. int job_id,job_count,jobs[CLASS_COUNT];
  8812. unsigned int ui, maxlvl;
  8813. maxlvl = atoi(fields[0]);
  8814. if(maxlvl > MAX_LEVEL || maxlvl<1){
  8815. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  8816. return false;
  8817. }
  8818. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  8819. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  8820. return false;
  8821. }
  8822. type = atoi(fields[2]);
  8823. if(type < 0 || type > 1){
  8824. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  8825. return false;
  8826. }
  8827. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  8828. if (job_count < 1)
  8829. return false;
  8830. job_id = jobs[0];
  8831. if(!pcdb_checkid(job_id)){
  8832. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  8833. return false;
  8834. }
  8835. idx = pc_class2idx(job_id);
  8836. job_info[idx].max_level[type] = maxlvl;
  8837. for(i=0; i<maxlvl; i++){
  8838. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  8839. }
  8840. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  8841. //The reasoning behind the -2 is this... if the max level is 5, then the array
  8842. //should look like this:
  8843. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  8844. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  8845. job_info[idx].max_level[type]--;
  8846. if (job_info[idx].max_level[type] < maxlvl) {
  8847. 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]);
  8848. ShowInfo("Filling the missing values with the last exp entry.\n");
  8849. //Fill the requested values with the last entry.
  8850. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  8851. for (; ui+2 < maxlvl; ui++)
  8852. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  8853. job_info[idx].max_level[type] = maxlvl;
  8854. }
  8855. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  8856. for (i = 1; i < job_count; i++) {
  8857. job_id = jobs[i];
  8858. if (!pcdb_checkid(job_id)) {
  8859. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  8860. continue;
  8861. }
  8862. idx = pc_class2idx(job_id);
  8863. 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]));
  8864. job_info[idx].max_level[type] = maxlvl;
  8865. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  8866. }
  8867. return true;
  8868. }
  8869. /*==========================================
  8870. * pc DB reading.
  8871. * job_exp.txt - required experience values
  8872. * skill_tree.txt - skill tree for every class
  8873. * attr_fix.txt - elemental adjustment table
  8874. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  8875. * job_db2.txt - job,stats bonuses/lvl
  8876. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  8877. *------------------------------------------*/
  8878. int pc_readdb(void)
  8879. {
  8880. int i,k, entries;
  8881. FILE *fp;
  8882. char line[24000];
  8883. //reset
  8884. memset(job_info,0,sizeof(job_info)); // job_info table
  8885. // Reset and read skilltree
  8886. memset(skill_tree,0,sizeof(skill_tree));
  8887. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree);
  8888. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8889. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty);
  8890. for( k=1; k < 3; k++ ){ // fill in the blanks
  8891. int j;
  8892. for( j = 0; j < RC_MAX; j++ ){
  8893. int tmp = 0;
  8894. for( i = 0; i < MAX_LEVEL*2; i++ ){
  8895. if( i == MAX_LEVEL+1 )
  8896. tmp = level_penalty[k][j][0];// reset
  8897. if( level_penalty[k][j][i] > 0 )
  8898. tmp = level_penalty[k][j][i];
  8899. else
  8900. level_penalty[k][j][i] = tmp;
  8901. }
  8902. }
  8903. }
  8904. #endif
  8905. // reset then read statspoint
  8906. memset(statp,0,sizeof(statp));
  8907. i=1;
  8908. sprintf(line, "%s/"DBPATH"statpoint.txt", db_path);
  8909. fp=fopen(line,"r");
  8910. if(fp == NULL){
  8911. ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  8912. //return 1;
  8913. } else {
  8914. entries=0;
  8915. while(fgets(line, sizeof(line), fp))
  8916. {
  8917. int stat;
  8918. if(line[0]=='/' && line[1]=='/')
  8919. continue;
  8920. if ((stat=strtoul(line,NULL,10))<0)
  8921. stat=0;
  8922. if (i > MAX_LEVEL)
  8923. break;
  8924. statp[i]=stat;
  8925. i++;
  8926. entries++;
  8927. }
  8928. fclose(fp);
  8929. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, DBPATH"statpoint.txt");
  8930. }
  8931. // generate the remaining parts of the db if necessary
  8932. k = battle_config.use_statpoint_table; //save setting
  8933. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  8934. statp[0] = 45; // seed value
  8935. for (; i <= MAX_LEVEL; i++)
  8936. statp[i] = statp[i-1] + pc_gets_status_point(i-1);
  8937. battle_config.use_statpoint_table = k; //restore setting
  8938. #ifdef RENEWAL_ASPD
  8939. sv_readdb(db_path, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1);
  8940. #else
  8941. sv_readdb(db_path, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1);
  8942. #endif
  8943. sv_readdb(db_path, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2);
  8944. #ifdef HP_SP_TABLES
  8945. sv_readdb(db_path, DBPATH"job_maxhpsp_db.txt", ',', 4, 4+MAX_LEVEL, CLASS_COUNT*2, &pc_readdb_job_maxhpsp);
  8946. #endif
  8947. sv_readdb(db_path, DBPATH"job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp); //support till 1000lvl
  8948. //Checking if all class have their data
  8949. for (i = 0; i < JOB_MAX; i++) {
  8950. int j;
  8951. if (!pcdb_checkid(i)) continue;
  8952. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK)
  8953. continue; //Classes that do not need exp tables.
  8954. j = pc_class2idx(i);
  8955. if (!job_info[j].max_level[0])
  8956. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  8957. if (!job_info[j].max_level[1])
  8958. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  8959. }
  8960. return 0;
  8961. }
  8962. // Read MOTD on startup. [Valaris]
  8963. int pc_read_motd(void)
  8964. {
  8965. FILE* fp;
  8966. // clear old MOTD
  8967. memset(motd_text, 0, sizeof(motd_text));
  8968. // read current MOTD
  8969. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  8970. {
  8971. char* buf, * ptr;
  8972. unsigned int lines = 0, entries = 0;
  8973. size_t len;
  8974. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  8975. {
  8976. lines++;
  8977. buf = motd_text[entries];
  8978. if( buf[0] == '/' && buf[1] == '/' )
  8979. {
  8980. continue;
  8981. }
  8982. len = strlen(buf);
  8983. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) )
  8984. {// strip trailing EOL characters
  8985. len--;
  8986. }
  8987. if( len )
  8988. {
  8989. buf[len] = 0;
  8990. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH )
  8991. {// crashes newer clients
  8992. 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);
  8993. }
  8994. }
  8995. else
  8996. {// empty line
  8997. buf[0] = ' ';
  8998. buf[1] = 0;
  8999. }
  9000. entries++;
  9001. }
  9002. fclose(fp);
  9003. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  9004. }
  9005. else
  9006. {
  9007. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  9008. }
  9009. return 0;
  9010. }
  9011. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  9012. int i,cursor = 0;
  9013. struct item_cd* cd = NULL;
  9014. if( load ) {
  9015. if( !(cd = idb_get(itemcd_db, sd->status.char_id)) ) {
  9016. // no skill cooldown is associated with this character
  9017. return;
  9018. }
  9019. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9020. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  9021. sd->item_delay[cursor].tick = cd->tick[i];
  9022. sd->item_delay[cursor].nameid = cd->nameid[i];
  9023. cursor++;
  9024. }
  9025. }
  9026. idb_remove(itemcd_db,sd->status.char_id);
  9027. } else {
  9028. if( !(cd = idb_get(itemcd_db,sd->status.char_id)) ) {
  9029. // create a new skill cooldown object for map storage
  9030. CREATE( cd, struct item_cd, 1 );
  9031. idb_put( itemcd_db, sd->status.char_id, cd );
  9032. }
  9033. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9034. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  9035. cd->tick[cursor] = sd->item_delay[i].tick;
  9036. cd->nameid[cursor] = sd->item_delay[i].nameid;
  9037. cursor++;
  9038. }
  9039. }
  9040. }
  9041. return;
  9042. }
  9043. /**
  9044. * Clear the dmglog data from player
  9045. * @param sd
  9046. * @param md
  9047. **/
  9048. void pc_clear_log_damage_sub(int char_id, struct mob_data *md)
  9049. {
  9050. uint8 i;
  9051. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  9052. if (i < DAMAGELOG_SIZE) {
  9053. md->dmglog[i].id=0;
  9054. md->dmglog[i].dmg=0;
  9055. md->dmglog[i].flag=0;
  9056. }
  9057. }
  9058. /**
  9059. * Add log to player's dmglog
  9060. * @param sd
  9061. * @param id Monster's id
  9062. **/
  9063. void pc_damage_log_add(struct map_session_data *sd, int id)
  9064. {
  9065. uint8 i = 0;
  9066. if (!sd)
  9067. return;
  9068. for (i = 0; i < DAMAGELOG_SIZE_PC && sd->dmglog[i].id != id; i++)
  9069. if (!sd->dmglog[i].id) {
  9070. sd->dmglog[i].id = id;
  9071. break;
  9072. }
  9073. return;
  9074. }
  9075. /**
  9076. * Clear dmglog data from player
  9077. * @param sd
  9078. * @param id Monster's id
  9079. **/
  9080. void pc_damage_log_clear(struct map_session_data *sd, int id)
  9081. {
  9082. uint8 i;
  9083. struct mob_data *md = NULL;
  9084. if (!sd)
  9085. return;
  9086. if (!id) {
  9087. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  9088. if( !sd->dmglog[i].id ) //skip the empty value
  9089. continue;
  9090. if ((md = map_id2md(sd->dmglog[i].id)))
  9091. pc_clear_log_damage_sub(sd->status.char_id,md);
  9092. }
  9093. memset(sd->dmglog,0,sizeof(sd->dmglog)); // clear all
  9094. }
  9095. else {
  9096. if ((md = map_id2md(id)))
  9097. pc_clear_log_damage_sub(sd->status.char_id,md);
  9098. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i].id == id); // find the id position
  9099. if (i < DAMAGELOG_SIZE_PC)
  9100. sd->dmglog[i].id = 0;
  9101. }
  9102. }
  9103. /**
  9104. * Deposit some money to bank
  9105. * @param sd
  9106. * @param money Amount of money to deposit
  9107. **/
  9108. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  9109. unsigned int limit_check = money+sd->status.bank_vault;
  9110. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  9111. return BDA_OVERFLOW;
  9112. } else if ( money > sd->status.zeny ) {
  9113. return BDA_NO_MONEY;
  9114. }
  9115. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  9116. return BDA_NO_MONEY;
  9117. sd->status.bank_vault += money;
  9118. if( save_settings&256 )
  9119. chrif_save(sd,0);
  9120. return BDA_SUCCESS;
  9121. }
  9122. /**
  9123. * Withdraw money from bank
  9124. * @param sd
  9125. * @param money Amount of money that will be withdrawn
  9126. **/
  9127. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  9128. unsigned int limit_check = money+sd->status.zeny;
  9129. if( money <= 0 ) {
  9130. return BWA_UNKNOWN_ERROR;
  9131. } else if ( money > sd->status.bank_vault ) {
  9132. return BWA_NO_MONEY;
  9133. } else if ( limit_check > MAX_ZENY ) {
  9134. /* no official response for this scenario exists. */
  9135. clif_colormes(sd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  9136. return BWA_UNKNOWN_ERROR;
  9137. }
  9138. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  9139. return BWA_NO_MONEY;
  9140. sd->status.bank_vault -= money;
  9141. if( save_settings&256 )
  9142. chrif_save(sd,0);
  9143. return BWA_SUCCESS;
  9144. }
  9145. /**
  9146. * Clear Cirmson Marker data from caster
  9147. * @param sd
  9148. **/
  9149. void pc_crimson_marker_clear(struct map_session_data *sd) {
  9150. uint8 i;
  9151. if (!sd || !(&sd->c_marker) || !sd->c_marker.target)
  9152. return;
  9153. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  9154. struct block_list *bl = NULL;
  9155. if (sd->c_marker.target[i] && (bl = map_id2bl(sd->c_marker.target[i])))
  9156. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  9157. }
  9158. }
  9159. /**
  9160. * Show version to player
  9161. * @param sd
  9162. **/
  9163. void pc_show_version(struct map_session_data *sd) {
  9164. const char* svn = get_svn_revision();
  9165. const char* git = get_git_hash();
  9166. char buf[CHAT_SIZE_MAX];
  9167. if( svn[0] != UNKNOWN_VERSION )
  9168. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  9169. else if( git[0] != UNKNOWN_VERSION )
  9170. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  9171. else
  9172. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  9173. clif_displaymessage(sd->fd,buf);
  9174. }
  9175. /** [Cydh]
  9176. * Timer for bonus_script
  9177. * @param tid
  9178. * @param tick
  9179. * @param id
  9180. * @param data
  9181. **/
  9182. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  9183. uint8 i = (uint8)data;
  9184. struct map_session_data *sd;
  9185. sd = map_id2sd(id);
  9186. if (!sd) {
  9187. ShowDebug("pc_bonus_script_timer: Null pointer id: %d data: %d\n",id,data);
  9188. return 0;
  9189. }
  9190. if (i > MAX_PC_BONUS_SCRIPT|| !(&sd->bonus_script[i]) || !sd->bonus_script[i].script) {
  9191. ShowDebug("pc_bonus_script_timer: Invalid index %d\n",i);
  9192. return 0;
  9193. }
  9194. pc_bonus_script_remove(sd,i);
  9195. status_calc_pc(sd,false);
  9196. return 0;
  9197. }
  9198. /** [Cydh]
  9199. * Remove bonus_script data from sd (not deleting timer)
  9200. * @param sd target
  9201. * @param i script index
  9202. **/
  9203. void pc_bonus_script_remove(struct map_session_data *sd, uint8 i) {
  9204. if (!sd || i >= MAX_PC_BONUS_SCRIPT)
  9205. return;
  9206. script_free_code(sd->bonus_script[i].script);
  9207. memset(&sd->bonus_script[i].script,0,sizeof(sd->bonus_script[i].script));
  9208. memset(sd->bonus_script[i].script_str,'\0',sizeof(sd->bonus_script[i].script_str));
  9209. sd->bonus_script[i].tick = 0;
  9210. sd->bonus_script[i].tid = 0;
  9211. sd->bonus_script[i].flag = 0;
  9212. }
  9213. /** [Cydh]
  9214. * Clear all active timer(s) of bonus_script data from sd
  9215. * @param sd target
  9216. * @param flag reason to remove the bonus_script
  9217. **/
  9218. void pc_bonus_script_check(struct map_session_data *sd, enum e_bonus_script_flags flag) {
  9219. uint8 i, count = 0;
  9220. if (!sd)
  9221. return;
  9222. for (i = 0; i < MAX_PC_BONUS_SCRIPT; i++) {
  9223. if (&sd->bonus_script[i] && sd->bonus_script[i].script && sd->bonus_script[i].flag&flag) {
  9224. delete_timer(sd->bonus_script[i].tid,pc_bonus_script_timer);
  9225. pc_bonus_script_remove(sd,i);
  9226. count++;
  9227. }
  9228. }
  9229. if (count && flag != BONUS_FLAG_REM_ON_LOGOUT) //Don't need do this if log out
  9230. status_calc_pc(sd,false);
  9231. }
  9232. /*==========================================
  9233. * pc Init/Terminate
  9234. *------------------------------------------*/
  9235. void do_final_pc(void) {
  9236. db_destroy(itemcd_db);
  9237. do_final_pc_groups();
  9238. return;
  9239. }
  9240. int do_init_pc(void) {
  9241. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  9242. pc_readdb();
  9243. pc_read_motd(); // Read MOTD [Valaris]
  9244. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  9245. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  9246. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  9247. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  9248. add_timer_func_list(pc_autosave, "pc_autosave");
  9249. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  9250. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  9251. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  9252. add_timer_func_list(pc_talisman_timer, "pc_talisman_timer");
  9253. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  9254. // 0=day, 1=night [Yor]
  9255. night_flag = battle_config.night_at_start ? 1 : 0;
  9256. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  9257. int day_duration = battle_config.day_duration;
  9258. int night_duration = battle_config.night_duration;
  9259. // add night/day timer [Yor]
  9260. add_timer_func_list(map_day_timer, "map_day_timer");
  9261. add_timer_func_list(map_night_timer, "map_night_timer");
  9262. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  9263. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  9264. }
  9265. do_init_pc_groups();
  9266. return 0;
  9267. }