pc.c 301 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170
  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 "pc.h"
  41. #include "pc_groups.h"
  42. #include "quest.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <time.h>
  47. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  48. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  49. static unsigned int statp[MAX_LEVEL+1];
  50. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  51. static unsigned int level_penalty[3][RC_MAX][MAX_LEVEL*2+1];
  52. #endif
  53. // h-files are for declarations, not for implementations... [Shinomori]
  54. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  55. // timer for night.day implementation
  56. int day_timer_tid;
  57. int night_timer_tid;
  58. struct fame_list smith_fame_list[MAX_FAME_LIST];
  59. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  60. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  61. static unsigned short 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};
  62. #define MOTD_LINE_SIZE 128
  63. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  64. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  65. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  66. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  67. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  68. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  69. };
  70. /**
  71. * Item Cool Down Delay Saving
  72. * Struct item_cd is not a member of struct map_session_data
  73. * to keep cooldowns in memory between player log-ins.
  74. * All cooldowns are reset when server is restarted.
  75. **/
  76. DBMap* itemcd_db = NULL; // char_id -> struct skill_cd
  77. struct item_cd {
  78. unsigned int tick[MAX_ITEMDELAYS];//tick
  79. short nameid[MAX_ITEMDELAYS];//skill id
  80. };
  81. //Converts a class to its array index for CLASS_COUNT defined arrays.
  82. //Note that it does not do a validity check for speed purposes, where parsing
  83. //player input make sure to use a pcdb_checkid first!
  84. int pc_class2idx(int class_) {
  85. if (class_ >= JOB_NOVICE_HIGH)
  86. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  87. return class_;
  88. }
  89. inline int pc_get_group_id(struct map_session_data *sd) {
  90. return sd->group_id;
  91. }
  92. inline int pc_get_group_level(struct map_session_data *sd) {
  93. return sd->group_level;
  94. }
  95. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  96. {
  97. struct map_session_data *sd;
  98. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  99. return 1;
  100. if(sd->invincible_timer != tid){
  101. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  102. return 0;
  103. }
  104. sd->invincible_timer = INVALID_TIMER;
  105. skill_unit_move(&sd->bl,tick,1);
  106. return 0;
  107. }
  108. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  109. nullpo_retv(sd);
  110. if( sd->invincible_timer != INVALID_TIMER )
  111. delete_timer(sd->invincible_timer,pc_invincible_timer);
  112. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  113. }
  114. void pc_delinvincibletimer(struct map_session_data* sd)
  115. {
  116. nullpo_retv(sd);
  117. if( sd->invincible_timer != INVALID_TIMER )
  118. {
  119. delete_timer(sd->invincible_timer,pc_invincible_timer);
  120. sd->invincible_timer = INVALID_TIMER;
  121. skill_unit_move(&sd->bl,gettick(),1);
  122. }
  123. }
  124. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  125. {
  126. struct map_session_data *sd;
  127. int i;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if( sd->spiritball <= 0 )
  131. {
  132. 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);
  133. sd->spiritball = 0;
  134. return 0;
  135. }
  136. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  137. if( i == sd->spiritball )
  138. {
  139. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  140. return 0;
  141. }
  142. sd->spiritball--;
  143. if( i != sd->spiritball )
  144. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  145. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  146. clif_spiritball(&sd->bl);
  147. return 0;
  148. }
  149. int pc_addspiritball(struct map_session_data *sd,int interval,int max)
  150. {
  151. int tid, i;
  152. nullpo_ret(sd);
  153. if(max > MAX_SKILL_LEVEL)
  154. max = MAX_SKILL_LEVEL;
  155. if(sd->spiritball < 0)
  156. sd->spiritball = 0;
  157. if( sd->spiritball && sd->spiritball >= max )
  158. {
  159. if(sd->spirit_timer[0] != INVALID_TIMER)
  160. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  161. sd->spiritball--;
  162. if( sd->spiritball != 0 )
  163. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  164. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  165. }
  166. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  167. 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);
  168. if( i != sd->spiritball )
  169. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  170. sd->spirit_timer[i] = tid;
  171. sd->spiritball++;
  172. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  173. clif_millenniumshield(sd,sd->spiritball);
  174. else
  175. clif_spiritball(&sd->bl);
  176. return 0;
  177. }
  178. int pc_delspiritball(struct map_session_data *sd,int count,int type)
  179. {
  180. int i;
  181. nullpo_ret(sd);
  182. if(sd->spiritball <= 0) {
  183. sd->spiritball = 0;
  184. return 0;
  185. }
  186. if(count <= 0)
  187. return 0;
  188. if(count > sd->spiritball)
  189. count = sd->spiritball;
  190. sd->spiritball -= count;
  191. if(count > MAX_SKILL_LEVEL)
  192. count = MAX_SKILL_LEVEL;
  193. for(i=0;i<count;i++) {
  194. if(sd->spirit_timer[i] != INVALID_TIMER) {
  195. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  196. sd->spirit_timer[i] = INVALID_TIMER;
  197. }
  198. }
  199. for(i=count;i<MAX_SKILL_LEVEL;i++) {
  200. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  201. sd->spirit_timer[i] = INVALID_TIMER;
  202. }
  203. if(!type) {
  204. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  205. clif_millenniumshield(sd,sd->spiritball);
  206. else
  207. clif_spiritball(&sd->bl);
  208. }
  209. return 0;
  210. }
  211. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  212. int *c, *b_sd;
  213. struct block_list *src;
  214. struct map_session_data *tsd;
  215. struct status_change *sc;
  216. nullpo_ret(bl);
  217. nullpo_ret(tsd = (struct map_session_data*)bl);
  218. nullpo_ret(src = va_arg(ap,struct block_list *));
  219. c = va_arg(ap,int *);
  220. b_sd = va_arg(ap, int *);
  221. if(pc_isdead(tsd))
  222. return 0;
  223. sc = status_get_sc(bl);
  224. if( bl == src )
  225. return 0;
  226. if( sc && sc->data[SC_BANDING] )
  227. {
  228. b_sd[(*c)++] = tsd->bl.id;
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  234. int c;
  235. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  236. int i, j, hp, extra_hp = 0, tmp_qty = 0, tmp_hp;
  237. struct map_session_data *bsd;
  238. struct status_change *sc;
  239. int range = skill_get_splash(LG_BANDING,skill_lv);
  240. nullpo_ret(sd);
  241. c = 0;
  242. memset(b_sd, 0, sizeof(b_sd));
  243. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  244. if( c < 1 ) //just recalc status no need to recalc hp
  245. { // No more Royal Guards in Banding found.
  246. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  247. {
  248. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  249. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  250. }
  251. return 0;
  252. }
  253. //Add yourself
  254. hp = status_get_hp(&sd->bl);
  255. i++;
  256. // Get total HP of all Royal Guards in party.
  257. for( j = 0; j < i; j++ )
  258. {
  259. bsd = map_id2sd(b_sd[j]);
  260. if( bsd != NULL )
  261. hp += status_get_hp(&bsd->bl);
  262. }
  263. // Set average HP.
  264. hp = hp / i;
  265. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  266. for( j = 0; j < i; j++ )
  267. {
  268. bsd = map_id2sd(b_sd[j]);
  269. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 )
  270. {
  271. extra_hp += tmp_hp;
  272. tmp_qty++;
  273. }
  274. }
  275. if( extra_hp > 0 && tmp_qty > 0 )
  276. hp += extra_hp / tmp_qty;
  277. for( j = 0; j < i; j++ )
  278. {
  279. bsd = map_id2sd(b_sd[j]);
  280. if( bsd != NULL )
  281. {
  282. status_set_hp(&bsd->bl,hp,0); // Set hp
  283. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  284. {
  285. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  286. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  287. }
  288. }
  289. }
  290. return c;
  291. }
  292. // Increases a player's fame points and displays a notice to him
  293. void pc_addfame(struct map_session_data *sd,int count)
  294. {
  295. nullpo_retv(sd);
  296. sd->status.fame += count;
  297. if(sd->status.fame > MAX_FAME)
  298. sd->status.fame = MAX_FAME;
  299. switch(sd->class_&MAPID_UPPERMASK){
  300. case MAPID_BLACKSMITH: // Blacksmith
  301. clif_fame_blacksmith(sd,count);
  302. break;
  303. case MAPID_ALCHEMIST: // Alchemist
  304. clif_fame_alchemist(sd,count);
  305. break;
  306. case MAPID_TAEKWON: // Taekwon
  307. clif_fame_taekwon(sd,count);
  308. break;
  309. }
  310. chrif_updatefamelist(sd);
  311. }
  312. // Check whether a player ID is in the fame rankers' list of its job, returns his/her position if so, 0 else
  313. unsigned char pc_famerank(int char_id, int job)
  314. {
  315. int i;
  316. switch(job){
  317. case MAPID_BLACKSMITH: // Blacksmith
  318. for(i = 0; i < MAX_FAME_LIST; i++){
  319. if(smith_fame_list[i].id == char_id)
  320. return i + 1;
  321. }
  322. break;
  323. case MAPID_ALCHEMIST: // Alchemist
  324. for(i = 0; i < MAX_FAME_LIST; i++){
  325. if(chemist_fame_list[i].id == char_id)
  326. return i + 1;
  327. }
  328. break;
  329. case MAPID_TAEKWON: // Taekwon
  330. for(i = 0; i < MAX_FAME_LIST; i++){
  331. if(taekwon_fame_list[i].id == char_id)
  332. return i + 1;
  333. }
  334. break;
  335. }
  336. return 0;
  337. }
  338. int pc_setrestartvalue(struct map_session_data *sd,int type) {
  339. struct status_data *status, *b_status;
  340. nullpo_ret(sd);
  341. b_status = &sd->base_status;
  342. status = &sd->battle_status;
  343. if (type&1) { //Normal resurrection
  344. status->hp = 1; //Otherwise status_heal may fail if dead.
  345. status_heal(&sd->bl, b_status->hp, 0, 1);
  346. if( status->sp < b_status->sp )
  347. status_set_sp(&sd->bl, b_status->sp, 1);
  348. } else { //Just for saving on the char-server (with values as if respawned)
  349. sd->status.hp = b_status->hp;
  350. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  351. }
  352. return 0;
  353. }
  354. /*==========================================
  355. Rental System
  356. *------------------------------------------*/
  357. static int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  358. {
  359. struct map_session_data *sd = map_id2sd(id);
  360. if( sd == NULL )
  361. return 0;
  362. if( tid != sd->rental_timer )
  363. {
  364. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  365. return 0;
  366. }
  367. pc_inventory_rentals(sd);
  368. return 1;
  369. }
  370. int pc_inventory_rental_clear(struct map_session_data *sd)
  371. {
  372. if( sd->rental_timer != INVALID_TIMER )
  373. {
  374. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  375. sd->rental_timer = INVALID_TIMER;
  376. }
  377. return 1;
  378. }
  379. void pc_inventory_rentals(struct map_session_data *sd)
  380. {
  381. int i, c = 0;
  382. unsigned int expire_tick, next_tick = UINT_MAX;
  383. for( i = 0; i < MAX_INVENTORY; i++ )
  384. { // Check for Rentals on Inventory
  385. if( sd->status.inventory[i].nameid == 0 )
  386. continue; // Nothing here
  387. if( sd->status.inventory[i].expire_time == 0 )
  388. continue;
  389. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  390. if( sd->status.inventory[i].nameid == ITEMID_REINS_OF_MOUNT
  391. && sd->sc.option&OPTION_MOUNTING ) {
  392. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);
  393. }
  394. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  395. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  396. } else {
  397. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  398. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  399. next_tick = min(expire_tick, next_tick);
  400. c++;
  401. }
  402. }
  403. 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
  404. sd->rental_timer = add_timer(gettick() + min(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  405. else
  406. sd->rental_timer = INVALID_TIMER;
  407. }
  408. void pc_inventory_rental_add(struct map_session_data *sd, int seconds)
  409. {
  410. int tick = seconds * 1000;
  411. if( sd == NULL )
  412. return;
  413. if( sd->rental_timer != INVALID_TIMER )
  414. {
  415. const struct TimerData * td;
  416. td = get_timer(sd->rental_timer);
  417. if( DIFF_TICK(td->tick, gettick()) > tick )
  418. { // Update Timer as this one ends first than the current one
  419. pc_inventory_rental_clear(sd);
  420. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  421. }
  422. }
  423. else
  424. sd->rental_timer = add_timer(gettick() + min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  425. }
  426. /**
  427. * Determines if player can give / drop / trade / vend items
  428. */
  429. bool pc_can_give_items(struct map_session_data *sd)
  430. {
  431. return pc_has_permission(sd, PC_PERM_TRADE);
  432. }
  433. /**
  434. * Determines if player can give / drop / trade / vend bounded items
  435. */
  436. bool pc_can_give_bounded_items(struct map_session_data *sd)
  437. {
  438. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  439. }
  440. /*==========================================
  441. * prepares character for saving.
  442. *------------------------------------------*/
  443. int pc_makesavestatus(struct map_session_data *sd)
  444. {
  445. nullpo_ret(sd);
  446. if(!battle_config.save_clothcolor)
  447. sd->status.clothes_color=0;
  448. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  449. //status change load/saving. [Skotlex]
  450. #ifdef NEW_CARTS
  451. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  452. #else
  453. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  454. #endif
  455. if (sd->sc.data[SC_JAILED])
  456. { //When Jailed, do not move last point.
  457. if(pc_isdead(sd)){
  458. pc_setrestartvalue(sd,0);
  459. } else {
  460. sd->status.hp = sd->battle_status.hp;
  461. sd->status.sp = sd->battle_status.sp;
  462. }
  463. sd->status.last_point.map = sd->mapindex;
  464. sd->status.last_point.x = sd->bl.x;
  465. sd->status.last_point.y = sd->bl.y;
  466. return 0;
  467. }
  468. if(pc_isdead(sd)){
  469. pc_setrestartvalue(sd,0);
  470. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  471. } else {
  472. sd->status.hp = sd->battle_status.hp;
  473. sd->status.sp = sd->battle_status.sp;
  474. sd->status.last_point.map = sd->mapindex;
  475. sd->status.last_point.x = sd->bl.x;
  476. sd->status.last_point.y = sd->bl.y;
  477. }
  478. if(map[sd->bl.m].flag.nosave){
  479. struct map_data *m=&map[sd->bl.m];
  480. if(m->save.map)
  481. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  482. else
  483. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  484. }
  485. return 0;
  486. }
  487. /*==========================================
  488. * Off init ? Connection?
  489. *------------------------------------------*/
  490. 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)
  491. {
  492. nullpo_ret(sd);
  493. sd->bl.id = account_id;
  494. sd->status.account_id = account_id;
  495. sd->status.char_id = char_id;
  496. sd->status.sex = sex;
  497. sd->login_id1 = login_id1;
  498. sd->login_id2 = 0; // at this point, we can not know the value :(
  499. sd->client_tick = client_tick;
  500. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  501. sd->bl.type = BL_PC;
  502. sd->canlog_tick = gettick();
  503. //Required to prevent homunculus copuing a base speed of 0.
  504. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  505. return 0;
  506. }
  507. int pc_equippoint(struct map_session_data *sd,int n){
  508. int ep = 0;
  509. nullpo_ret(sd);
  510. if(!sd->inventory_data[n])
  511. return 0;
  512. if (!itemdb_isequip2(sd->inventory_data[n]))
  513. return 0; //Not equippable by players.
  514. ep = sd->inventory_data[n]->equip;
  515. if(sd->inventory_data[n]->look == W_DAGGER ||
  516. sd->inventory_data[n]->look == W_1HSWORD ||
  517. sd->inventory_data[n]->look == W_1HAXE) {
  518. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  519. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  520. return EQP_ARMS;
  521. }
  522. return ep;
  523. }
  524. int pc_setinventorydata(struct map_session_data *sd)
  525. {
  526. int i,id;
  527. nullpo_ret(sd);
  528. for(i=0;i<MAX_INVENTORY;i++) {
  529. id = sd->status.inventory[i].nameid;
  530. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  531. }
  532. return 0;
  533. }
  534. int pc_calcweapontype(struct map_session_data *sd)
  535. {
  536. nullpo_ret(sd);
  537. // single-hand
  538. if(sd->weapontype2 == W_FIST) {
  539. sd->status.weapon = sd->weapontype1;
  540. return 1;
  541. }
  542. if(sd->weapontype1 == W_FIST) {
  543. sd->status.weapon = sd->weapontype2;
  544. return 1;
  545. }
  546. // dual-wield
  547. sd->status.weapon = 0;
  548. switch (sd->weapontype1){
  549. case W_DAGGER:
  550. switch (sd->weapontype2) {
  551. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  552. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  553. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  554. }
  555. break;
  556. case W_1HSWORD:
  557. switch (sd->weapontype2) {
  558. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  559. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  560. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  561. }
  562. break;
  563. case W_1HAXE:
  564. switch (sd->weapontype2) {
  565. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  566. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  567. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  568. }
  569. }
  570. // unknown, default to right hand type
  571. if (!sd->status.weapon)
  572. sd->status.weapon = sd->weapontype1;
  573. return 2;
  574. }
  575. int pc_setequipindex(struct map_session_data *sd)
  576. {
  577. int i,j;
  578. nullpo_ret(sd);
  579. for(i=0;i<EQI_MAX;i++)
  580. sd->equip_index[i] = -1;
  581. for(i=0;i<MAX_INVENTORY;i++) {
  582. if(sd->status.inventory[i].nameid <= 0)
  583. continue;
  584. if(sd->status.inventory[i].equip) {
  585. for(j=0;j<EQI_MAX;j++)
  586. if(sd->status.inventory[i].equip & equip_pos[j])
  587. sd->equip_index[j] = i;
  588. if(sd->status.inventory[i].equip & EQP_HAND_R)
  589. {
  590. if(sd->inventory_data[i])
  591. sd->weapontype1 = sd->inventory_data[i]->look;
  592. else
  593. sd->weapontype1 = 0;
  594. }
  595. if( sd->status.inventory[i].equip & EQP_HAND_L )
  596. {
  597. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  598. sd->weapontype2 = sd->inventory_data[i]->look;
  599. else
  600. sd->weapontype2 = 0;
  601. }
  602. }
  603. }
  604. pc_calcweapontype(sd);
  605. return 0;
  606. }
  607. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  608. //{
  609. // int i;
  610. // struct item *item = &sd->status.inventory[eqindex];
  611. // struct item_data *data;
  612. //
  613. // //Crafted/made/hatched items.
  614. // if (itemdb_isspecial(item->card[0]))
  615. // return 1;
  616. //
  617. // /* scan for enchant armor gems */
  618. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  619. // s = MAX_SLOTS - 1;
  620. //
  621. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  622. // return( i < s ) ? 0 : 1;
  623. //}
  624. bool pc_isequipped(struct map_session_data *sd, int nameid)
  625. {
  626. int i, j, index;
  627. for( i = 0; i < EQI_MAX; i++ )
  628. {
  629. index = sd->equip_index[i];
  630. if( index < 0 ) continue;
  631. if( i == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  632. if( i == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  633. if( i == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  634. if( !sd->inventory_data[index] ) continue;
  635. if( sd->inventory_data[index]->nameid == nameid )
  636. return true;
  637. for( j = 0; j < sd->inventory_data[index]->slot; j++ )
  638. if( sd->status.inventory[index].card[j] == nameid )
  639. return true;
  640. }
  641. return false;
  642. }
  643. bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
  644. {
  645. if( !p1_sd || !p2_sd || !b_sd )
  646. return false;
  647. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  648. return false; // already adopted baby / in adopt request
  649. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  650. return false; // You need to be married and in party with baby to adopt
  651. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  652. return false; // Not married, wrong married
  653. if( p2_sd->status.party_id != p1_sd->status.party_id )
  654. return false; // Both parents need to be in the same party
  655. // Parents need to have their ring equipped
  656. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  657. return false;
  658. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  659. return false;
  660. // Already adopted a baby
  661. if( p1_sd->status.child || p2_sd->status.child ) {
  662. clif_Adopt_reply(p1_sd, 0);
  663. return false;
  664. }
  665. // Parents need at least lvl 70 to adopt
  666. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  667. clif_Adopt_reply(p1_sd, 1);
  668. return false;
  669. }
  670. if( b_sd->status.partner_id ) {
  671. clif_Adopt_reply(p1_sd, 2);
  672. return false;
  673. }
  674. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE ) )
  675. return false;
  676. return true;
  677. }
  678. /*==========================================
  679. * Adoption Process
  680. *------------------------------------------*/
  681. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  682. {
  683. int job, joblevel;
  684. unsigned int jobexp;
  685. if( !pc_can_Adopt(p1_sd, p2_sd, b_sd) )
  686. return false;
  687. // Preserve current job levels and progress
  688. joblevel = b_sd->status.job_level;
  689. jobexp = b_sd->status.job_exp;
  690. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  691. if( job != -1 && !pc_jobchange(b_sd, job, 0) )
  692. { // Success, proceed to configure parents and baby skills
  693. p1_sd->status.child = b_sd->status.char_id;
  694. p2_sd->status.child = b_sd->status.char_id;
  695. b_sd->status.father = p1_sd->status.char_id;
  696. b_sd->status.mother = p2_sd->status.char_id;
  697. // Restore progress
  698. b_sd->status.job_level = joblevel;
  699. clif_updatestatus(b_sd, SP_JOBLEVEL);
  700. b_sd->status.job_exp = jobexp;
  701. clif_updatestatus(b_sd, SP_JOBEXP);
  702. // Baby Skills
  703. pc_skill(b_sd, WE_BABY, 1, 0);
  704. pc_skill(b_sd, WE_CALLPARENT, 1, 0);
  705. // Parents Skills
  706. pc_skill(p1_sd, WE_CALLBABY, 1, 0);
  707. pc_skill(p2_sd, WE_CALLBABY, 1, 0);
  708. return true;
  709. }
  710. return false; // Job Change Fail
  711. }
  712. /*=================================================
  713. * Checks if the player can equip the item at index n in inventory.
  714. * Returns 0 (no) or 1 (yes).
  715. *------------------------------------------------*/
  716. int pc_isequip(struct map_session_data *sd,int n)
  717. {
  718. struct item_data *item;
  719. nullpo_ret(sd);
  720. item = sd->inventory_data[n];
  721. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  722. return 1;
  723. if(item == NULL)
  724. return 0;
  725. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  726. return 0;
  727. #ifdef RENEWAL
  728. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  729. return 0;
  730. #endif
  731. if(item->sex != 2 && sd->status.sex != item->sex)
  732. return 0;
  733. if (sd->sc.count) {
  734. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  735. return 0;
  736. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  737. return 0;
  738. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  739. return 0;
  740. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  741. return 0;
  742. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  743. return 0;
  744. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  745. return 0;
  746. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  747. //Spirit of Super Novice equip bonuses. [Skotlex]
  748. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  749. return 1; //Can equip all helms
  750. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON)
  751. switch(item->look) { //In weapons, the look determines type of weapon.
  752. case W_DAGGER: //Level 4 Knives are equippable.. this means all knives, I'd guess?
  753. case W_1HSWORD: //All 1H swords
  754. case W_1HAXE: //All 1H Axes
  755. case W_MACE: //All 1H Maces
  756. case W_STAFF: //All 1H Staves
  757. return 1;
  758. }
  759. }
  760. }
  761. //Not equipable by class. [Skotlex]
  762. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  763. return 0;
  764. //Not usable by upper class. [Inkfish]
  765. while( 1 ) {
  766. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  767. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  768. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  769. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  770. return 0;
  771. }
  772. return 1;
  773. }
  774. /*==========================================
  775. * No problem with the session id
  776. * set the status that has been sent from char server
  777. *------------------------------------------*/
  778. 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)
  779. {
  780. int i;
  781. #ifdef BOUND_ITEMS
  782. int j;
  783. int idxlist[MAX_INVENTORY];
  784. #endif
  785. unsigned long tick = gettick();
  786. uint32 ip = session[sd->fd]->client_addr;
  787. sd->login_id2 = login_id2;
  788. sd->group_id = group_id;
  789. /* load user permissions */
  790. pc_group_pc_load(sd);
  791. memcpy(&sd->status, st, sizeof(*st));
  792. if (st->sex != sd->status.sex) {
  793. clif_authfail_fd(sd->fd, 0);
  794. return false;
  795. }
  796. //Set the map-server used job id. [Skotlex]
  797. i = pc_jobid2mapid(sd->status.class_);
  798. if (i == -1) { //Invalid class?
  799. 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);
  800. sd->status.class_ = JOB_NOVICE;
  801. sd->class_ = MAPID_NOVICE;
  802. } else
  803. sd->class_ = i;
  804. // Checks and fixes to character status data, that are required
  805. // in case of configuration change or stuff, which cannot be
  806. // checked on char-server.
  807. if( sd->status.hair < MIN_HAIR_STYLE || sd->status.hair > MAX_HAIR_STYLE )
  808. {
  809. sd->status.hair = MIN_HAIR_STYLE;
  810. }
  811. if( sd->status.hair_color < MIN_HAIR_COLOR || sd->status.hair_color > MAX_HAIR_COLOR )
  812. {
  813. sd->status.hair_color = MIN_HAIR_COLOR;
  814. }
  815. if( sd->status.clothes_color < MIN_CLOTH_COLOR || sd->status.clothes_color > MAX_CLOTH_COLOR )
  816. {
  817. sd->status.clothes_color = MIN_CLOTH_COLOR;
  818. }
  819. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  820. if(!sd->status.hp) pc_setdead(sd);
  821. sd->state.connect_new = 1;
  822. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  823. sd->invincible_timer = INVALID_TIMER;
  824. sd->npc_timer_id = INVALID_TIMER;
  825. sd->pvp_timer = INVALID_TIMER;
  826. #ifdef SECURE_NPCTIMEOUT
  827. // Initialize to defaults/expected
  828. sd->npc_idle_timer = INVALID_TIMER;
  829. sd->npc_idle_tick = tick;
  830. sd->npc_idle_type = NPCT_INPUT;
  831. #endif
  832. sd->canuseitem_tick = tick;
  833. sd->canusecashfood_tick = tick;
  834. sd->canequip_tick = tick;
  835. sd->cantalk_tick = tick;
  836. sd->canskill_tick = tick;
  837. sd->cansendmail_tick = tick;
  838. for(i = 0; i < MAX_SKILL_LEVEL; i++)
  839. sd->spirit_timer[i] = INVALID_TIMER;
  840. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  841. sd->autobonus[i].active = INVALID_TIMER;
  842. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  843. sd->autobonus2[i].active = INVALID_TIMER;
  844. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  845. sd->autobonus3[i].active = INVALID_TIMER;
  846. if (battle_config.item_auto_get)
  847. sd->state.autoloot = 10000;
  848. if (battle_config.disp_experience)
  849. sd->state.showexp = 1;
  850. if (battle_config.disp_zeny)
  851. sd->state.showzeny = 1;
  852. if (!(battle_config.display_skill_fail&2))
  853. sd->state.showdelay = 1;
  854. pc_setinventorydata(sd);
  855. pc_setequipindex(sd);
  856. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  857. sd->status.option &= ~OPTION_INVISIBLE;
  858. }
  859. status_change_init(&sd->bl);
  860. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  861. //Set here because we need the inventory data for weapon sprite parsing.
  862. status_set_viewdata(&sd->bl, sd->status.class_);
  863. unit_dataset(&sd->bl);
  864. sd->guild_x = -1;
  865. sd->guild_y = -1;
  866. // Event Timers
  867. for( i = 0; i < MAX_EVENTTIMER; i++ )
  868. sd->eventtimer[i] = INVALID_TIMER;
  869. // Rental Timer
  870. sd->rental_timer = INVALID_TIMER;
  871. for( i = 0; i < 3; i++ )
  872. sd->hate_mob[i] = -1;
  873. //warp player
  874. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
  875. 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);
  876. // try warping to a default map instead (church graveyard)
  877. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
  878. // if we fail again
  879. clif_authfail_fd(sd->fd, 0);
  880. return false;
  881. }
  882. }
  883. clif_authok(sd);
  884. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  885. sd->die_counter=-1;
  886. //display login notice
  887. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  888. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  889. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  890. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  891. sd->status.name, sd->status.account_id, sd->status.char_id,
  892. sd->packet_ver, CONVIP(ip), sd->group_id);
  893. // Send friends list
  894. clif_friendslist_send(sd);
  895. if( !changing_mapservers ) {
  896. if (battle_config.display_version == 1) {
  897. const char* svn = get_svn_revision();
  898. const char* git = get_git_hash();
  899. char buf[256];
  900. if( svn[0] != UNKNOWN_VERSION )
  901. sprintf(buf,"SVN Revision: %s", svn);
  902. else if( git[0] != UNKNOWN_VERSION )
  903. sprintf(buf,"Git Hash: %s", git);
  904. else
  905. sprintf(buf,"Unknown Version");
  906. clif_displaymessage(sd->fd, buf);
  907. }
  908. // Message of the Day [Valaris]
  909. for(i=0; motd_text[i][0] && i < MOTD_LINE_SIZE; i++) {
  910. if (battle_config.motd_type)
  911. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  912. else
  913. clif_displaymessage(sd->fd, motd_text[i]);
  914. }
  915. // message of the limited time of the account
  916. if (expiration_time != 0) { // don't display if it's unlimited or unknow value
  917. char tmpstr[1024];
  918. strftime(tmpstr, sizeof(tmpstr) - 1, msg_txt(sd,501), localtime(&expiration_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  919. clif_wis_message(sd->fd, wisp_server_name, tmpstr, strlen(tmpstr)+1);
  920. }
  921. /**
  922. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  923. **/
  924. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  925. }
  926. /**
  927. * Check if player have any cool downs on
  928. **/
  929. skill_cooldown_load(sd);
  930. /**
  931. * Check if player have any item cooldowns on
  932. **/
  933. pc_itemcd_do(sd,true);
  934. #ifdef BOUND_ITEMS
  935. // Party bound item check
  936. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,3,idxlist))) { // Party was deleted while character offline
  937. for(i=0;i<j;i++)
  938. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  939. }
  940. #endif
  941. // Request all registries (auth is considered completed whence they arrive)
  942. intif_request_registry(sd,7);
  943. return true;
  944. }
  945. /*==========================================
  946. * Closes a connection because it failed to be authenticated from the char server.
  947. *------------------------------------------*/
  948. void pc_authfail(struct map_session_data *sd)
  949. {
  950. clif_authfail_fd(sd->fd, 0);
  951. return;
  952. }
  953. //Attempts to set a mob.
  954. int pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  955. {
  956. int class_;
  957. if (!sd || !bl || pos < 0 || pos > 2)
  958. return 0;
  959. if (sd->hate_mob[pos] != -1)
  960. { //Can't change hate targets.
  961. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  962. return 0;
  963. }
  964. class_ = status_get_class(bl);
  965. if (!pcdb_checkid(class_)) {
  966. unsigned int max_hp = status_get_max_hp(bl);
  967. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  968. return 0;
  969. if (pos != status_get_size(bl))
  970. return 0; //Wrong size
  971. }
  972. sd->hate_mob[pos] = class_;
  973. pc_setglobalreg(sd,sg_info[pos].hate_var,class_+1);
  974. clif_hate_info(sd, pos, class_, 1);
  975. return 1;
  976. }
  977. /*==========================================
  978. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  979. *------------------------------------------*/
  980. int pc_reg_received(struct map_session_data *sd)
  981. {
  982. int i,j;
  983. sd->change_level_2nd = pc_readglobalreg(sd,"jobchange_level");
  984. sd->change_level_3rd = pc_readglobalreg(sd,"jobchange_level_3rd");
  985. sd->die_counter = pc_readglobalreg(sd,"PC_DIE_COUNTER");
  986. sd->langtype = pc_readaccountreg(sd,"#langtype");
  987. if(msg_checklangtype(sd->langtype,true)<0) sd->langtype=0; //invalid langtype reset to default
  988. // Cash shop
  989. sd->cashPoints = pc_readaccountreg(sd,"#CASHPOINTS");
  990. sd->kafraPoints = pc_readaccountreg(sd,"#KAFRAPOINTS");
  991. // Cooking Exp
  992. sd->cook_mastery = pc_readglobalreg(sd,"COOK_MASTERY");
  993. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  994. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  995. sd->mission_mobid = pc_readglobalreg(sd,"TK_MISSION_ID");
  996. sd->mission_count = pc_readglobalreg(sd,"TK_MISSION_COUNT");
  997. }
  998. //SG map and mob read [Komurka]
  999. for(i=0;i<MAX_PC_FEELHATE;i++) //for now - someone need to make reading from txt/sql
  1000. {
  1001. if ((j = pc_readglobalreg(sd,sg_info[i].feel_var))!=0) {
  1002. sd->feel_map[i].index = j;
  1003. sd->feel_map[i].m = map_mapindex2mapid(j);
  1004. } else {
  1005. sd->feel_map[i].index = 0;
  1006. sd->feel_map[i].m = -1;
  1007. }
  1008. sd->hate_mob[i] = pc_readglobalreg(sd,sg_info[i].hate_var)-1;
  1009. }
  1010. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1011. sd->cloneskill_id = pc_readglobalreg(sd,"CLONE_SKILL");
  1012. if (sd->cloneskill_id > 0) {
  1013. sd->status.skill[sd->cloneskill_id].id = sd->cloneskill_id;
  1014. sd->status.skill[sd->cloneskill_id].lv = pc_readglobalreg(sd,"CLONE_SKILL_LV");
  1015. if (sd->status.skill[sd->cloneskill_id].lv > i)
  1016. sd->status.skill[sd->cloneskill_id].lv = i;
  1017. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1018. }
  1019. }
  1020. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1021. sd->reproduceskill_id = pc_readglobalreg(sd,"REPRODUCE_SKILL");
  1022. if( sd->reproduceskill_id > 0) {
  1023. sd->status.skill[sd->reproduceskill_id].id = sd->reproduceskill_id;
  1024. sd->status.skill[sd->reproduceskill_id].lv = pc_readglobalreg(sd,"REPRODUCE_SKILL_LV");
  1025. if( i < sd->status.skill[sd->reproduceskill_id].lv)
  1026. sd->status.skill[sd->reproduceskill_id].lv = i;
  1027. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1028. }
  1029. }
  1030. //Weird... maybe registries were reloaded?
  1031. if (sd->state.active)
  1032. return 0;
  1033. sd->state.active = 1;
  1034. if (sd->status.party_id)
  1035. party_member_joined(sd);
  1036. if (sd->status.guild_id)
  1037. guild_member_joined(sd);
  1038. // pet
  1039. if (sd->status.pet_id > 0)
  1040. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1041. // Homunculus [albator]
  1042. if( sd->status.hom_id > 0 )
  1043. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1044. if( sd->status.mer_id > 0 )
  1045. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1046. if( sd->status.ele_id > 0 )
  1047. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1048. map_addiddb(&sd->bl);
  1049. map_delnickdb(sd->status.char_id, sd->status.name);
  1050. if (!chrif_auth_finished(sd))
  1051. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1052. pc_load_combo(sd);
  1053. status_calc_pc(sd,1);
  1054. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1055. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1056. intif_request_questlog(sd);
  1057. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1058. sd->state.connect_new = 1;
  1059. clif_parse_LoadEndAck(sd->fd, sd);
  1060. }
  1061. pc_inventory_rentals(sd);
  1062. if( sd->sc.option&OPTION_INVISIBLE ) {
  1063. sd->vd.class_ = INVISIBLE_CLASS;
  1064. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1065. // decrement the number of pvp players on the map
  1066. map[sd->bl.m].users_pvp--;
  1067. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1068. // unregister the player for ranking
  1069. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1070. sd->pvp_timer = INVALID_TIMER;
  1071. }
  1072. clif_changeoption( &sd->bl );
  1073. }
  1074. return 1;
  1075. }
  1076. static int pc_calc_skillpoint(struct map_session_data* sd)
  1077. {
  1078. int i,skill_lv,inf2,skill_point=0;
  1079. nullpo_ret(sd);
  1080. for(i=1;i<MAX_SKILL;i++){
  1081. if( (skill_lv = pc_checkskill(sd,i)) > 0) {
  1082. inf2 = skill_get_inf2(i);
  1083. if((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1084. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1085. ) {
  1086. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1087. skill_point += skill_lv;
  1088. else
  1089. if(sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0)
  1090. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1091. }
  1092. }
  1093. }
  1094. return skill_point;
  1095. }
  1096. /*==========================================
  1097. * Calculation of skill level.
  1098. *------------------------------------------*/
  1099. int pc_calc_skilltree(struct map_session_data *sd)
  1100. {
  1101. int i,id=0,flag;
  1102. int c=0;
  1103. nullpo_ret(sd);
  1104. i = pc_calc_skilltree_normalize_job(sd);
  1105. c = pc_mapid2jobid(i, sd->status.sex);
  1106. if( c == -1 )
  1107. { //Unable to normalize job??
  1108. 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);
  1109. return 1;
  1110. }
  1111. c = pc_class2idx(c);
  1112. for( i = 0; i < MAX_SKILL; i++ ) {
  1113. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1114. sd->status.skill[i].id = 0; //First clear skills.
  1115. /* permanent skills that must be re-checked */
  1116. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1117. switch( i ) {
  1118. case NV_TRICKDEAD:
  1119. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1120. sd->status.skill[i].id = 0;
  1121. sd->status.skill[i].lv = 0;
  1122. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1123. }
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. for( i = 0; i < MAX_SKILL; i++ )
  1129. {
  1130. 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 )
  1131. { // Restore original level of skills after deleting earned skills.
  1132. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1133. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1134. }
  1135. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER && i >= DC_HUMMING && i<= DC_SERVICEFORYOU )
  1136. { //Enable Bard/Dancer spirit linked skills.
  1137. if( sd->status.sex )
  1138. { //Link dancer skills to bard.
  1139. if( sd->status.skill[i-8].lv < 10 )
  1140. continue;
  1141. sd->status.skill[i].id = i;
  1142. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1143. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1144. }
  1145. else
  1146. { //Link bard skills to dancer.
  1147. if( sd->status.skill[i].lv < 10 )
  1148. continue;
  1149. sd->status.skill[i-8].id = i - 8;
  1150. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1151. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1152. }
  1153. }
  1154. }
  1155. if( pc_has_permission(sd, PC_PERM_ALL_SKILL) ) {
  1156. for( i = 0; i < MAX_SKILL; i++ ) {
  1157. switch(i) {
  1158. /**
  1159. * Dummy skills must be added here otherwise they'll be displayed in the,
  1160. * skill tree and since they have no icons they'll give resource errors
  1161. **/
  1162. case SM_SELFPROVOKE:
  1163. case AB_DUPLELIGHT_MELEE:
  1164. case AB_DUPLELIGHT_MAGIC:
  1165. case WL_CHAINLIGHTNING_ATK:
  1166. case WL_TETRAVORTEX_FIRE:
  1167. case WL_TETRAVORTEX_WATER:
  1168. case WL_TETRAVORTEX_WIND:
  1169. case WL_TETRAVORTEX_GROUND:
  1170. case WL_SUMMON_ATK_FIRE:
  1171. case WL_SUMMON_ATK_WIND:
  1172. case WL_SUMMON_ATK_WATER:
  1173. case WL_SUMMON_ATK_GROUND:
  1174. case LG_OVERBRAND_BRANDISH:
  1175. case LG_OVERBRAND_PLUSATK:
  1176. case WM_SEVERE_RAINSTORM_MELEE:
  1177. continue;
  1178. default:
  1179. break;
  1180. }
  1181. if( skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL) )
  1182. continue; //Only skills you can't have are npc/guild ones
  1183. if( skill_get_max(i) > 0 )
  1184. sd->status.skill[i].id = i;
  1185. }
  1186. return 0;
  1187. }
  1188. do {
  1189. flag = 0;
  1190. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1191. {
  1192. int f;
  1193. if( sd->status.skill[id].id )
  1194. continue; //Skill already known.
  1195. f = 1;
  1196. if(!battle_config.skillfree) {
  1197. int j;
  1198. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1199. int k;
  1200. if((k=skill_tree[c][i].need[j].id))
  1201. {
  1202. if (sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED)
  1203. k = 0; //Not learned.
  1204. else
  1205. if (sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1206. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1207. else
  1208. k = pc_checkskill(sd,k);
  1209. if (k < skill_tree[c][i].need[j].lv)
  1210. {
  1211. f = 0;
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. if( sd->status.job_level < skill_tree[c][i].joblv ) { //We need to get the actual class in this case
  1217. int class = pc_mapid2jobid(sd->class_, sd->status.sex);
  1218. class = pc_class2idx(class);
  1219. if (class == c || (class != c && sd->status.job_level < skill_tree[class][i].joblv))
  1220. f = 0; // job level requirement wasn't satisfied
  1221. }
  1222. }
  1223. if( f ) {
  1224. int inf2;
  1225. inf2 = skill_get_inf2(id);
  1226. if(!sd->status.skill[id].lv && (
  1227. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1228. inf2&INF2_WEDDING_SKILL ||
  1229. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1230. ))
  1231. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1232. sd->status.skill[id].id = id;
  1233. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1234. sd->status.skill[id].lv = 1; // need to manually specify a skill level
  1235. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1236. }
  1237. flag = 1; // skill list has changed, perform another pass
  1238. }
  1239. }
  1240. } while(flag);
  1241. //
  1242. 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) )
  1243. {
  1244. /* Taekwon Ranger Bonus Skill Tree
  1245. ============================================
  1246. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1247. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1248. - (sd->status.skill_point == 0) to wait until all skill points are asigned to avoid problems with Job Change quest. */
  1249. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1250. {
  1251. if( (skill_get_inf2(id)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1252. continue; //Do not include Quest/Wedding skills.
  1253. if( sd->status.skill[id].id == 0 )
  1254. {
  1255. sd->status.skill[id].id = id;
  1256. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1257. }
  1258. else if( id != NV_BASIC )
  1259. {
  1260. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Remember original level
  1261. }
  1262. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_);
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. //Checks if you can learn a new skill after having leveled up a skill.
  1268. static void pc_check_skilltree(struct map_session_data *sd, int skill)
  1269. {
  1270. int i,id=0,flag;
  1271. int c=0;
  1272. if(battle_config.skillfree)
  1273. return; //Function serves no purpose if this is set
  1274. i = pc_calc_skilltree_normalize_job(sd);
  1275. c = pc_mapid2jobid(i, sd->status.sex);
  1276. if (c == -1) { //Unable to normalize job??
  1277. 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);
  1278. return;
  1279. }
  1280. c = pc_class2idx(c);
  1281. do {
  1282. flag = 0;
  1283. for( i = 0; i < MAX_SKILL_TREE && (id=skill_tree[c][i].id)>0; i++ )
  1284. {
  1285. int j, f = 1, k;
  1286. if( sd->status.skill[id].id ) //Already learned
  1287. continue;
  1288. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ )
  1289. {
  1290. if( (k = skill_tree[c][i].need[j].id) )
  1291. {
  1292. if( sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED )
  1293. k = 0; //Not learned.
  1294. else
  1295. if( sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1296. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1297. else
  1298. k = pc_checkskill(sd,k);
  1299. if( k < skill_tree[c][i].need[j].lv )
  1300. {
  1301. f = 0;
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. if( !f )
  1307. continue;
  1308. if( sd->status.job_level < skill_tree[c][i].joblv )
  1309. continue;
  1310. j = skill_get_inf2(id);
  1311. if( !sd->status.skill[id].lv && (
  1312. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1313. j&INF2_WEDDING_SKILL ||
  1314. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1315. ) )
  1316. continue; //Cannot be learned via normal means.
  1317. sd->status.skill[id].id = id;
  1318. flag = 1;
  1319. }
  1320. } while(flag);
  1321. }
  1322. // Make sure all the skills are in the correct condition
  1323. // before persisting to the backend.. [MouseJstr]
  1324. int pc_clean_skilltree(struct map_session_data *sd)
  1325. {
  1326. int i;
  1327. for (i = 0; i < MAX_SKILL; i++){
  1328. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED)
  1329. {
  1330. sd->status.skill[i].id = 0;
  1331. sd->status.skill[i].lv = 0;
  1332. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1333. }
  1334. else
  1335. if (sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0){
  1336. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1337. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1338. }
  1339. }
  1340. return 0;
  1341. }
  1342. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1343. {
  1344. int skill_point, novice_skills;
  1345. int c = sd->class_;
  1346. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1347. return c;
  1348. skill_point = pc_calc_skillpoint(sd);
  1349. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1350. // limit 1st class and above to novice job levels
  1351. if(skill_point < novice_skills)
  1352. {
  1353. c = MAPID_NOVICE;
  1354. }
  1355. // limit 2nd class and above to first class job levels (super novices are exempt)
  1356. else if ((sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1357. {
  1358. // regenerate change_level_2nd
  1359. if (!sd->change_level_2nd)
  1360. {
  1361. if (sd->class_&JOBL_THIRD)
  1362. {
  1363. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1364. if (!sd->change_level_3rd)
  1365. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1366. else
  1367. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1368. - (sd->status.job_level - 1)
  1369. - (sd->change_level_3rd - 1)
  1370. - novice_skills;
  1371. }
  1372. else
  1373. {
  1374. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1375. - (sd->status.job_level - 1)
  1376. - novice_skills;
  1377. }
  1378. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  1379. }
  1380. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1381. {
  1382. c &= MAPID_BASEMASK;
  1383. }
  1384. // limit 3rd class to 2nd class/trans job levels
  1385. else if(sd->class_&JOBL_THIRD)
  1386. {
  1387. // regenerate change_level_3rd
  1388. if (!sd->change_level_3rd)
  1389. {
  1390. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1391. - (sd->status.job_level - 1)
  1392. - (sd->change_level_2nd - 1)
  1393. - novice_skills;
  1394. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  1395. }
  1396. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1397. c &= MAPID_UPPERMASK;
  1398. }
  1399. }
  1400. // restore non-limiting flags
  1401. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1402. return c;
  1403. }
  1404. /*==========================================
  1405. * Updates the weight status
  1406. *------------------------------------------
  1407. * 1: overweight 50%
  1408. * 2: overweight 90%
  1409. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1410. */
  1411. int pc_updateweightstatus(struct map_session_data *sd)
  1412. {
  1413. int old_overweight;
  1414. int new_overweight;
  1415. nullpo_retr(1, sd);
  1416. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1417. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1418. if( old_overweight == new_overweight )
  1419. return 0; // no change
  1420. // stop old status change
  1421. if( old_overweight == 1 )
  1422. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1423. else if( old_overweight == 2 )
  1424. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1425. // start new status change
  1426. if( new_overweight == 1 )
  1427. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1428. else if( new_overweight == 2 )
  1429. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1430. // update overweight status
  1431. sd->regen.state.overweight = new_overweight;
  1432. return 0;
  1433. }
  1434. int pc_disguise(struct map_session_data *sd, int class_)
  1435. {
  1436. if (!class_ && !sd->disguise)
  1437. return 0;
  1438. if (class_ && sd->disguise == class_)
  1439. return 0;
  1440. if(sd->sc.option&OPTION_INVISIBLE)
  1441. { //Character is invisible. Stealth class-change. [Skotlex]
  1442. sd->disguise = class_; //viewdata is set on uncloaking.
  1443. return 2;
  1444. }
  1445. if (sd->bl.prev != NULL) {
  1446. pc_stop_walking(sd, 0);
  1447. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1448. }
  1449. if (!class_) {
  1450. sd->disguise = 0;
  1451. class_ = sd->status.class_;
  1452. } else
  1453. sd->disguise=class_;
  1454. status_set_viewdata(&sd->bl, class_);
  1455. clif_changeoption(&sd->bl);
  1456. if (sd->bl.prev != NULL) {
  1457. clif_spawn(&sd->bl);
  1458. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1459. { //It seems the cart info is lost on undisguise.
  1460. clif_cartlist(sd);
  1461. clif_updatestatus(sd,SP_CARTINFO);
  1462. }
  1463. if (sd->chatID) {
  1464. struct chat_data* cd;
  1465. nullpo_retr(1, sd);
  1466. cd = (struct chat_data*)map_id2bl(sd->chatID);
  1467. if( cd != NULL || (struct block_list*)sd == cd->owner )
  1468. clif_dispchat(cd,0);
  1469. }
  1470. }
  1471. return 1;
  1472. }
  1473. static int pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, short card_id)
  1474. {
  1475. int i;
  1476. if( !rate )
  1477. return 0;
  1478. for( i = 0; i < max && spell[i].id; i++ )
  1479. {
  1480. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1481. {
  1482. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1483. return 0;
  1484. rate += spell[i].rate;
  1485. break;
  1486. }
  1487. }
  1488. if (i == max) {
  1489. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1490. return 0;
  1491. }
  1492. spell[i].id = id;
  1493. spell[i].lv = lv;
  1494. spell[i].rate = rate;
  1495. //Auto-update flag value.
  1496. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1497. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1498. if (!(flag&BF_SKILLMASK)) {
  1499. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1500. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1501. }
  1502. spell[i].flag|= flag;
  1503. spell[i].card_id = card_id;
  1504. return 1;
  1505. }
  1506. static int pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, short card_id)
  1507. {
  1508. int i;
  1509. if( !rate )
  1510. return 0;
  1511. for( i = 0; i < max && spell[i].id; i++ )
  1512. {
  1513. ; // each autospell works independently
  1514. }
  1515. if( i == max )
  1516. {
  1517. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1518. return 0;
  1519. }
  1520. spell[i].flag = src_skill;
  1521. spell[i].id = id;
  1522. spell[i].lv = lv;
  1523. spell[i].rate = rate;
  1524. spell[i].card_id = card_id;
  1525. return 1;
  1526. }
  1527. static int pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, short rate, short arrow_rate, unsigned char flag)
  1528. {
  1529. int i;
  1530. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1531. flag|=ATF_SHORT|ATF_LONG; //Default range: both
  1532. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1533. flag|=ATF_TARGET; //Default target: enemy.
  1534. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1535. flag|=ATF_WEAPON; //Default type: weapon.
  1536. for (i = 0; i < max && effect[i].flag; i++) {
  1537. if (effect[i].id == id && effect[i].flag == flag)
  1538. {
  1539. effect[i].rate += rate;
  1540. effect[i].arrow_rate += arrow_rate;
  1541. return 1;
  1542. }
  1543. }
  1544. if (i == max) {
  1545. ShowWarning("pc_bonus: Reached max (%d) number of add effects per character!\n", max);
  1546. return 0;
  1547. }
  1548. effect[i].id = id;
  1549. effect[i].rate = rate;
  1550. effect[i].arrow_rate = arrow_rate;
  1551. effect[i].flag = flag;
  1552. return 1;
  1553. }
  1554. static int pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill, unsigned char target)
  1555. {
  1556. int i;
  1557. for( i = 0; i < max && effect[i].skill; i++ )
  1558. {
  1559. if( effect[i].id == id && effect[i].skill == skill && effect[i].target == target )
  1560. {
  1561. effect[i].rate += rate;
  1562. return 1;
  1563. }
  1564. }
  1565. if( i == max ) {
  1566. ShowWarning("pc_bonus: Reached max (%d) number of add effects on skill per character!\n", max);
  1567. return 0;
  1568. }
  1569. effect[i].id = id;
  1570. effect[i].rate = rate;
  1571. effect[i].skill = skill;
  1572. effect[i].target = target;
  1573. return 1;
  1574. }
  1575. static int pc_bonus_item_drop(struct s_add_drop *drop, const short max, short id, short group, int race, int rate)
  1576. {
  1577. int i;
  1578. //Apply config rate adjustment settings.
  1579. if (rate >= 0) { //Absolute drop.
  1580. if (battle_config.item_rate_adddrop != 100)
  1581. rate = rate*battle_config.item_rate_adddrop/100;
  1582. if (rate < battle_config.item_drop_adddrop_min)
  1583. rate = battle_config.item_drop_adddrop_min;
  1584. else if (rate > battle_config.item_drop_adddrop_max)
  1585. rate = battle_config.item_drop_adddrop_max;
  1586. } else { //Relative drop, max/min limits are applied at drop time.
  1587. if (battle_config.item_rate_adddrop != 100)
  1588. rate = rate*battle_config.item_rate_adddrop/100;
  1589. if (rate > -1)
  1590. rate = -1;
  1591. }
  1592. for(i = 0; i < max && (drop[i].id || drop[i].group); i++) {
  1593. if(
  1594. ((id && drop[i].id == id) ||
  1595. (group && drop[i].group == group))
  1596. && race > 0
  1597. ) {
  1598. drop[i].race |= race;
  1599. if(drop[i].rate > 0 && rate > 0)
  1600. { //Both are absolute rates.
  1601. if (drop[i].rate < rate)
  1602. drop[i].rate = rate;
  1603. } else
  1604. if(drop[i].rate < 0 && rate < 0) {
  1605. //Both are relative rates.
  1606. if (drop[i].rate > rate)
  1607. drop[i].rate = rate;
  1608. } else if (rate < 0) //Give preference to relative rate.
  1609. drop[i].rate = rate;
  1610. return 1;
  1611. }
  1612. }
  1613. if(i == max) {
  1614. ShowWarning("pc_bonus: Reached max (%d) number of added drops per character!\n", max);
  1615. return 0;
  1616. }
  1617. drop[i].id = id;
  1618. drop[i].group = group;
  1619. drop[i].race |= race;
  1620. drop[i].rate = rate;
  1621. return 1;
  1622. }
  1623. 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)
  1624. {
  1625. int i;
  1626. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1627. if( i == max )
  1628. {
  1629. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1630. return 0;
  1631. }
  1632. if( !onskill )
  1633. {
  1634. if( !(flag&BF_RANGEMASK) )
  1635. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1636. if( !(flag&BF_WEAPONMASK) )
  1637. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1638. if( !(flag&BF_SKILLMASK) )
  1639. {
  1640. if( flag&(BF_MAGIC|BF_MISC) )
  1641. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1642. if( flag&BF_WEAPON )
  1643. flag|=BF_NORMAL|BF_SKILL;
  1644. }
  1645. }
  1646. bonus[i].rate = rate;
  1647. bonus[i].duration = dur;
  1648. bonus[i].active = INVALID_TIMER;
  1649. bonus[i].atk_type = flag;
  1650. bonus[i].pos = pos;
  1651. bonus[i].bonus_script = aStrdup(script);
  1652. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1653. return 1;
  1654. }
  1655. int pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1656. {
  1657. int i;
  1658. nullpo_ret(sd);
  1659. for( i = 0; i < max; i++ )
  1660. {
  1661. if( autobonus[i].active != INVALID_TIMER )
  1662. {
  1663. if( restore && sd->state.autobonus&autobonus[i].pos )
  1664. {
  1665. if( autobonus[i].bonus_script )
  1666. {
  1667. int j;
  1668. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus[i].pos );
  1669. if( j < EQI_MAX-1 )
  1670. script_run_autobonus(autobonus[i].bonus_script,sd->bl.id,sd->equip_index[j]);
  1671. }
  1672. continue;
  1673. }
  1674. else
  1675. { // Logout / Unequipped an item with an activated bonus
  1676. delete_timer(autobonus[i].active,pc_endautobonus);
  1677. autobonus[i].active = INVALID_TIMER;
  1678. }
  1679. }
  1680. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1681. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1682. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1683. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1684. autobonus[i].active = INVALID_TIMER;
  1685. }
  1686. return 0;
  1687. }
  1688. int pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1689. {
  1690. nullpo_ret(sd);
  1691. nullpo_ret(autobonus);
  1692. if( autobonus->other_script )
  1693. {
  1694. int j;
  1695. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus->pos );
  1696. if( j < EQI_MAX-1 )
  1697. script_run_autobonus(autobonus->other_script,sd->bl.id,sd->equip_index[j]);
  1698. }
  1699. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1700. sd->state.autobonus |= autobonus->pos;
  1701. status_calc_pc(sd,0);
  1702. return 0;
  1703. }
  1704. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1705. {
  1706. struct map_session_data *sd = map_id2sd(id);
  1707. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1708. nullpo_ret(sd);
  1709. nullpo_ret(autobonus);
  1710. autobonus->active = INVALID_TIMER;
  1711. sd->state.autobonus &= ~autobonus->pos;
  1712. status_calc_pc(sd,0);
  1713. return 0;
  1714. }
  1715. int pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1716. {
  1717. int i;
  1718. struct weapon_data* wd;
  1719. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  1720. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  1721. if (i == MAX_PC_BONUS)
  1722. {
  1723. ShowWarning("pc_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1724. return 0;
  1725. }
  1726. if (!(flag&BF_RANGEMASK))
  1727. flag |= BF_SHORT|BF_LONG;
  1728. if (!(flag&BF_WEAPONMASK))
  1729. flag |= BF_WEAPON;
  1730. if (!(flag&BF_SKILLMASK))
  1731. {
  1732. if (flag&(BF_MAGIC|BF_MISC))
  1733. flag |= BF_SKILL;
  1734. if (flag&BF_WEAPON)
  1735. flag |= BF_NORMAL|BF_SKILL;
  1736. }
  1737. wd->addele2[i].ele = ele;
  1738. wd->addele2[i].rate = rate;
  1739. wd->addele2[i].flag = flag;
  1740. return 0;
  1741. }
  1742. int pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1743. {
  1744. int i;
  1745. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  1746. if (i == MAX_PC_BONUS)
  1747. {
  1748. ShowWarning("pc_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1749. return 0;
  1750. }
  1751. if (!(flag&BF_RANGEMASK))
  1752. flag |= BF_SHORT|BF_LONG;
  1753. if (!(flag&BF_WEAPONMASK))
  1754. flag |= BF_WEAPON;
  1755. if (!(flag&BF_SKILLMASK))
  1756. {
  1757. if (flag&(BF_MAGIC|BF_MISC))
  1758. flag |= BF_SKILL;
  1759. if (flag&BF_WEAPON)
  1760. flag |= BF_NORMAL|BF_SKILL;
  1761. }
  1762. sd->subele2[i].ele = ele;
  1763. sd->subele2[i].rate = rate;
  1764. sd->subele2[i].flag = flag;
  1765. return 0;
  1766. }
  1767. /*==========================================
  1768. * Add a bonus(type) to player sd
  1769. *------------------------------------------*/
  1770. int pc_bonus(struct map_session_data *sd,int type,int val)
  1771. {
  1772. struct status_data *status;
  1773. int bonus;
  1774. nullpo_ret(sd);
  1775. status = &sd->base_status;
  1776. switch(type){
  1777. case SP_STR:
  1778. case SP_AGI:
  1779. case SP_VIT:
  1780. case SP_INT:
  1781. case SP_DEX:
  1782. case SP_LUK:
  1783. if(sd->state.lr_flag != 2)
  1784. sd->param_bonus[type-SP_STR]+=val;
  1785. break;
  1786. case SP_ATK1:
  1787. if(!sd->state.lr_flag) {
  1788. bonus = status->rhw.atk + val;
  1789. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1790. }
  1791. else if(sd->state.lr_flag == 1) {
  1792. bonus = status->lhw.atk + val;
  1793. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1794. }
  1795. break;
  1796. case SP_ATK2:
  1797. if(!sd->state.lr_flag) {
  1798. bonus = status->rhw.atk2 + val;
  1799. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1800. }
  1801. else if(sd->state.lr_flag == 1) {
  1802. bonus = status->lhw.atk2 + val;
  1803. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1804. }
  1805. break;
  1806. case SP_BASE_ATK:
  1807. if(sd->state.lr_flag != 2) {
  1808. #ifdef RENEWAL
  1809. sd->bonus.eatk += val;
  1810. #else
  1811. bonus = status->batk + val;
  1812. status->batk = cap_value(bonus, 0, USHRT_MAX);
  1813. #endif
  1814. }
  1815. break;
  1816. case SP_DEF1:
  1817. if(sd->state.lr_flag != 2) {
  1818. bonus = status->def + val;
  1819. #ifdef RENEWAL
  1820. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1821. #else
  1822. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1823. #endif
  1824. }
  1825. break;
  1826. case SP_DEF2:
  1827. if(sd->state.lr_flag != 2) {
  1828. bonus = status->def2 + val;
  1829. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1830. }
  1831. break;
  1832. case SP_MDEF1:
  1833. if(sd->state.lr_flag != 2) {
  1834. bonus = status->mdef + val;
  1835. #ifdef RENEWAL
  1836. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1837. #else
  1838. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1839. #endif
  1840. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  1841. sd->bonus.shieldmdef += bonus;
  1842. }
  1843. }
  1844. break;
  1845. case SP_MDEF2:
  1846. if(sd->state.lr_flag != 2) {
  1847. bonus = status->mdef2 + val;
  1848. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1849. }
  1850. break;
  1851. case SP_HIT:
  1852. if(sd->state.lr_flag != 2) {
  1853. bonus = status->hit + val;
  1854. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1855. } else
  1856. sd->bonus.arrow_hit+=val;
  1857. break;
  1858. case SP_FLEE1:
  1859. if(sd->state.lr_flag != 2) {
  1860. bonus = status->flee + val;
  1861. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1862. }
  1863. break;
  1864. case SP_FLEE2:
  1865. if(sd->state.lr_flag != 2) {
  1866. bonus = status->flee2 + val*10;
  1867. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1868. }
  1869. break;
  1870. case SP_CRITICAL:
  1871. if(sd->state.lr_flag != 2) {
  1872. bonus = status->cri + val*10;
  1873. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1874. } else
  1875. sd->bonus.arrow_cri += val*10;
  1876. break;
  1877. case SP_ATKELE:
  1878. if(val >= ELE_MAX) {
  1879. ShowError("pc_bonus: SP_ATKELE: Invalid element %d\n", val);
  1880. break;
  1881. }
  1882. switch (sd->state.lr_flag)
  1883. {
  1884. case 2:
  1885. switch (sd->status.weapon) {
  1886. case W_BOW:
  1887. case W_REVOLVER:
  1888. case W_RIFLE:
  1889. case W_GATLING:
  1890. case W_SHOTGUN:
  1891. case W_GRENADE:
  1892. //Become weapon element.
  1893. status->rhw.ele=val;
  1894. break;
  1895. default: //Become arrow element.
  1896. sd->bonus.arrow_ele=val;
  1897. break;
  1898. }
  1899. break;
  1900. case 1:
  1901. status->lhw.ele=val;
  1902. break;
  1903. default:
  1904. status->rhw.ele=val;
  1905. break;
  1906. }
  1907. break;
  1908. case SP_DEFELE:
  1909. if(val >= ELE_MAX) {
  1910. ShowError("pc_bonus: SP_DEFELE: Invalid element %d\n", val);
  1911. break;
  1912. }
  1913. if(sd->state.lr_flag != 2)
  1914. status->def_ele=val;
  1915. break;
  1916. case SP_MAXHP:
  1917. if(sd->state.lr_flag == 2)
  1918. break;
  1919. val += (int)status->max_hp;
  1920. //Negative bonuses will underflow, this will be handled in status_calc_pc through casting
  1921. //If this is called outside of status_calc_pc, you'd better pray they do not underflow and end with UINT_MAX max_hp.
  1922. status->max_hp = (unsigned int)val;
  1923. break;
  1924. case SP_MAXSP:
  1925. if(sd->state.lr_flag == 2)
  1926. break;
  1927. val += (int)status->max_sp;
  1928. status->max_sp = (unsigned int)val;
  1929. break;
  1930. #ifndef RENEWAL_CAST
  1931. case SP_VARCASTRATE:
  1932. #endif
  1933. case SP_CASTRATE:
  1934. if(sd->state.lr_flag != 2)
  1935. sd->castrate+=val;
  1936. break;
  1937. case SP_MAXHPRATE:
  1938. if(sd->state.lr_flag != 2)
  1939. sd->hprate+=val;
  1940. break;
  1941. case SP_MAXSPRATE:
  1942. if(sd->state.lr_flag != 2)
  1943. sd->sprate+=val;
  1944. break;
  1945. case SP_SPRATE:
  1946. if(sd->state.lr_flag != 2)
  1947. sd->dsprate+=val;
  1948. break;
  1949. case SP_ATTACKRANGE:
  1950. switch (sd->state.lr_flag) {
  1951. case 2:
  1952. switch (sd->status.weapon) {
  1953. case W_BOW:
  1954. case W_REVOLVER:
  1955. case W_RIFLE:
  1956. case W_GATLING:
  1957. case W_SHOTGUN:
  1958. case W_GRENADE:
  1959. status->rhw.range += val;
  1960. }
  1961. break;
  1962. case 1:
  1963. status->lhw.range += val;
  1964. break;
  1965. default:
  1966. status->rhw.range += val;
  1967. break;
  1968. }
  1969. break;
  1970. case SP_SPEED_RATE: //Non stackable increase
  1971. if(sd->state.lr_flag != 2)
  1972. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  1973. break;
  1974. case SP_SPEED_ADDRATE: //Stackable increase
  1975. if(sd->state.lr_flag != 2)
  1976. sd->bonus.speed_add_rate -= val;
  1977. break;
  1978. case SP_ASPD: //Raw increase
  1979. if(sd->state.lr_flag != 2)
  1980. sd->bonus.aspd_add -= 10*val;
  1981. break;
  1982. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  1983. if(sd->state.lr_flag != 2)
  1984. #ifndef RENEWAL_ASPD
  1985. status->aspd_rate -= 10*val;
  1986. #else
  1987. status->aspd_rate2 += val;
  1988. #endif
  1989. break;
  1990. case SP_HP_RECOV_RATE:
  1991. if(sd->state.lr_flag != 2)
  1992. sd->hprecov_rate += val;
  1993. break;
  1994. case SP_SP_RECOV_RATE:
  1995. if(sd->state.lr_flag != 2)
  1996. sd->sprecov_rate += val;
  1997. break;
  1998. case SP_CRITICAL_DEF:
  1999. if(sd->state.lr_flag != 2)
  2000. sd->bonus.critical_def += val;
  2001. break;
  2002. case SP_NEAR_ATK_DEF:
  2003. if(sd->state.lr_flag != 2)
  2004. sd->bonus.near_attack_def_rate += val;
  2005. break;
  2006. case SP_LONG_ATK_DEF:
  2007. if(sd->state.lr_flag != 2)
  2008. sd->bonus.long_attack_def_rate += val;
  2009. break;
  2010. case SP_DOUBLE_RATE:
  2011. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2012. sd->bonus.double_rate = val;
  2013. break;
  2014. case SP_DOUBLE_ADD_RATE:
  2015. if(sd->state.lr_flag == 0)
  2016. sd->bonus.double_add_rate += val;
  2017. break;
  2018. case SP_MATK_RATE:
  2019. if(sd->state.lr_flag != 2)
  2020. sd->matk_rate += val;
  2021. break;
  2022. case SP_IGNORE_DEF_ELE:
  2023. if(val >= ELE_MAX) {
  2024. ShowError("pc_bonus: SP_IGNORE_DEF_ELE: Invalid element %d\n", val);
  2025. break;
  2026. }
  2027. if(!sd->state.lr_flag)
  2028. sd->right_weapon.ignore_def_ele |= 1<<val;
  2029. else if(sd->state.lr_flag == 1)
  2030. sd->left_weapon.ignore_def_ele |= 1<<val;
  2031. break;
  2032. case SP_IGNORE_DEF_RACE:
  2033. if(!sd->state.lr_flag)
  2034. sd->right_weapon.ignore_def_race |= 1<<val;
  2035. else if(sd->state.lr_flag == 1)
  2036. sd->left_weapon.ignore_def_race |= 1<<val;
  2037. break;
  2038. case SP_ATK_RATE:
  2039. if(sd->state.lr_flag != 2)
  2040. sd->bonus.atk_rate += val;
  2041. break;
  2042. case SP_MAGIC_ATK_DEF:
  2043. if(sd->state.lr_flag != 2)
  2044. sd->bonus.magic_def_rate += val;
  2045. break;
  2046. case SP_MISC_ATK_DEF:
  2047. if(sd->state.lr_flag != 2)
  2048. sd->bonus.misc_def_rate += val;
  2049. break;
  2050. case SP_IGNORE_MDEF_RATE:
  2051. if(sd->state.lr_flag != 2) {
  2052. sd->ignore_mdef[RC_NONBOSS] += val;
  2053. sd->ignore_mdef[RC_BOSS] += val;
  2054. }
  2055. break;
  2056. case SP_IGNORE_MDEF_ELE:
  2057. if(val >= ELE_MAX) {
  2058. ShowError("pc_bonus: SP_IGNORE_MDEF_ELE: Invalid element %d\n", val);
  2059. break;
  2060. }
  2061. if(sd->state.lr_flag != 2)
  2062. sd->bonus.ignore_mdef_ele |= 1<<val;
  2063. break;
  2064. case SP_IGNORE_MDEF_RACE:
  2065. if(sd->state.lr_flag != 2)
  2066. sd->bonus.ignore_mdef_race |= 1<<val;
  2067. break;
  2068. case SP_PERFECT_HIT_RATE:
  2069. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2070. sd->bonus.perfect_hit = val;
  2071. break;
  2072. case SP_PERFECT_HIT_ADD_RATE:
  2073. if(sd->state.lr_flag != 2)
  2074. sd->bonus.perfect_hit_add += val;
  2075. break;
  2076. case SP_CRITICAL_RATE:
  2077. if(sd->state.lr_flag != 2)
  2078. sd->critical_rate+=val;
  2079. break;
  2080. case SP_DEF_RATIO_ATK_ELE:
  2081. if(val >= ELE_MAX) {
  2082. ShowError("pc_bonus: SP_DEF_RATIO_ATK_ELE: Invalid element %d\n", val);
  2083. break;
  2084. }
  2085. if(!sd->state.lr_flag)
  2086. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2087. else if(sd->state.lr_flag == 1)
  2088. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2089. break;
  2090. case SP_DEF_RATIO_ATK_RACE:
  2091. if(val >= RC_MAX) {
  2092. ShowError("pc_bonus: SP_DEF_RATIO_ATK_RACE: Invalid race %d\n", val);
  2093. break;
  2094. }
  2095. if(!sd->state.lr_flag)
  2096. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2097. else if(sd->state.lr_flag == 1)
  2098. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2099. break;
  2100. case SP_HIT_RATE:
  2101. if(sd->state.lr_flag != 2)
  2102. sd->hit_rate += val;
  2103. break;
  2104. case SP_FLEE_RATE:
  2105. if(sd->state.lr_flag != 2)
  2106. sd->flee_rate += val;
  2107. break;
  2108. case SP_FLEE2_RATE:
  2109. if(sd->state.lr_flag != 2)
  2110. sd->flee2_rate += val;
  2111. break;
  2112. case SP_DEF_RATE:
  2113. if(sd->state.lr_flag != 2)
  2114. sd->def_rate += val;
  2115. break;
  2116. case SP_DEF2_RATE:
  2117. if(sd->state.lr_flag != 2)
  2118. sd->def2_rate += val;
  2119. break;
  2120. case SP_MDEF_RATE:
  2121. if(sd->state.lr_flag != 2)
  2122. sd->mdef_rate += val;
  2123. break;
  2124. case SP_MDEF2_RATE:
  2125. if(sd->state.lr_flag != 2)
  2126. sd->mdef2_rate += val;
  2127. break;
  2128. case SP_RESTART_FULL_RECOVER:
  2129. if(sd->state.lr_flag != 2)
  2130. sd->special_state.restart_full_recover = 1;
  2131. break;
  2132. case SP_NO_CASTCANCEL:
  2133. if(sd->state.lr_flag != 2)
  2134. sd->special_state.no_castcancel = 1;
  2135. break;
  2136. case SP_NO_CASTCANCEL2:
  2137. if(sd->state.lr_flag != 2)
  2138. sd->special_state.no_castcancel2 = 1;
  2139. break;
  2140. case SP_NO_SIZEFIX:
  2141. if(sd->state.lr_flag != 2)
  2142. sd->special_state.no_sizefix = 1;
  2143. break;
  2144. case SP_NO_MAGIC_DAMAGE:
  2145. if(sd->state.lr_flag == 2)
  2146. break;
  2147. val+= sd->special_state.no_magic_damage;
  2148. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2149. break;
  2150. case SP_NO_WEAPON_DAMAGE:
  2151. if(sd->state.lr_flag == 2)
  2152. break;
  2153. val+= sd->special_state.no_weapon_damage;
  2154. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2155. break;
  2156. case SP_NO_MISC_DAMAGE:
  2157. if(sd->state.lr_flag == 2)
  2158. break;
  2159. val+= sd->special_state.no_misc_damage;
  2160. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2161. break;
  2162. case SP_NO_GEMSTONE:
  2163. if(sd->state.lr_flag != 2)
  2164. sd->special_state.no_gemstone = 1;
  2165. break;
  2166. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2167. if(sd->state.lr_flag != 2) {
  2168. sd->special_state.intravision = 1;
  2169. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2170. }
  2171. break;
  2172. case SP_NO_KNOCKBACK:
  2173. if(sd->state.lr_flag != 2)
  2174. sd->special_state.no_knockback = 1;
  2175. break;
  2176. case SP_SPLASH_RANGE:
  2177. if(sd->bonus.splash_range < val)
  2178. sd->bonus.splash_range = val;
  2179. break;
  2180. case SP_SPLASH_ADD_RANGE:
  2181. sd->bonus.splash_add_range += val;
  2182. break;
  2183. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2184. if(sd->state.lr_flag != 2)
  2185. sd->bonus.short_weapon_damage_return += val;
  2186. break;
  2187. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2188. if(sd->state.lr_flag != 2)
  2189. sd->bonus.long_weapon_damage_return += val;
  2190. break;
  2191. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2192. if(sd->state.lr_flag != 2)
  2193. sd->bonus.magic_damage_return += val;
  2194. break;
  2195. case SP_ALL_STATS: // [Valaris]
  2196. if(sd->state.lr_flag!=2) {
  2197. sd->param_bonus[SP_STR-SP_STR]+=val;
  2198. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2199. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2200. sd->param_bonus[SP_INT-SP_STR]+=val;
  2201. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2202. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2203. }
  2204. break;
  2205. case SP_AGI_VIT: // [Valaris]
  2206. if(sd->state.lr_flag!=2) {
  2207. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2208. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2209. }
  2210. break;
  2211. case SP_AGI_DEX_STR: // [Valaris]
  2212. if(sd->state.lr_flag!=2) {
  2213. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2214. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2215. sd->param_bonus[SP_STR-SP_STR]+=val;
  2216. }
  2217. break;
  2218. case SP_PERFECT_HIDE: // [Valaris]
  2219. if(sd->state.lr_flag!=2)
  2220. sd->special_state.perfect_hiding=1;
  2221. break;
  2222. case SP_UNBREAKABLE:
  2223. if(sd->state.lr_flag!=2)
  2224. sd->bonus.unbreakable += val;
  2225. break;
  2226. case SP_UNBREAKABLE_WEAPON:
  2227. if(sd->state.lr_flag != 2)
  2228. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2229. break;
  2230. case SP_UNBREAKABLE_ARMOR:
  2231. if(sd->state.lr_flag != 2)
  2232. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2233. break;
  2234. case SP_UNBREAKABLE_HELM:
  2235. if(sd->state.lr_flag != 2)
  2236. sd->bonus.unbreakable_equip |= EQP_HELM;
  2237. break;
  2238. case SP_UNBREAKABLE_SHIELD:
  2239. if(sd->state.lr_flag != 2)
  2240. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2241. break;
  2242. case SP_UNBREAKABLE_GARMENT:
  2243. if(sd->state.lr_flag != 2)
  2244. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2245. break;
  2246. case SP_UNBREAKABLE_SHOES:
  2247. if(sd->state.lr_flag != 2)
  2248. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2249. break;
  2250. case SP_CLASSCHANGE: // [Valaris]
  2251. if(sd->state.lr_flag !=2)
  2252. sd->bonus.classchange=val;
  2253. break;
  2254. case SP_LONG_ATK_RATE:
  2255. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2256. sd->bonus.long_attack_atk_rate+=val;
  2257. break;
  2258. case SP_BREAK_WEAPON_RATE:
  2259. if(sd->state.lr_flag != 2)
  2260. sd->bonus.break_weapon_rate+=val;
  2261. break;
  2262. case SP_BREAK_ARMOR_RATE:
  2263. if(sd->state.lr_flag != 2)
  2264. sd->bonus.break_armor_rate+=val;
  2265. break;
  2266. case SP_ADD_STEAL_RATE:
  2267. if(sd->state.lr_flag != 2)
  2268. sd->bonus.add_steal_rate+=val;
  2269. break;
  2270. case SP_DELAYRATE:
  2271. if(sd->state.lr_flag != 2)
  2272. sd->delayrate+=val;
  2273. break;
  2274. case SP_CRIT_ATK_RATE:
  2275. if(sd->state.lr_flag != 2)
  2276. sd->bonus.crit_atk_rate += val;
  2277. break;
  2278. case SP_NO_REGEN:
  2279. if(sd->state.lr_flag != 2)
  2280. sd->regen.state.block|=val;
  2281. break;
  2282. case SP_UNSTRIPABLE_WEAPON:
  2283. if(sd->state.lr_flag != 2)
  2284. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2285. break;
  2286. case SP_UNSTRIPABLE:
  2287. case SP_UNSTRIPABLE_ARMOR:
  2288. if(sd->state.lr_flag != 2)
  2289. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2290. break;
  2291. case SP_UNSTRIPABLE_HELM:
  2292. if(sd->state.lr_flag != 2)
  2293. sd->bonus.unstripable_equip |= EQP_HELM;
  2294. break;
  2295. case SP_UNSTRIPABLE_SHIELD:
  2296. if(sd->state.lr_flag != 2)
  2297. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2298. break;
  2299. case SP_HP_DRAIN_VALUE:
  2300. if(!sd->state.lr_flag) {
  2301. sd->right_weapon.hp_drain[RC_NONBOSS].value += val;
  2302. sd->right_weapon.hp_drain[RC_BOSS].value += val;
  2303. }
  2304. else if(sd->state.lr_flag == 1) {
  2305. sd->left_weapon.hp_drain[RC_NONBOSS].value += val;
  2306. sd->left_weapon.hp_drain[RC_BOSS].value += val;
  2307. }
  2308. break;
  2309. case SP_SP_DRAIN_VALUE:
  2310. if(!sd->state.lr_flag) {
  2311. sd->right_weapon.sp_drain[RC_NONBOSS].value += val;
  2312. sd->right_weapon.sp_drain[RC_BOSS].value += val;
  2313. }
  2314. else if(sd->state.lr_flag == 1) {
  2315. sd->left_weapon.sp_drain[RC_NONBOSS].value += val;
  2316. sd->left_weapon.sp_drain[RC_BOSS].value += val;
  2317. }
  2318. break;
  2319. case SP_SP_GAIN_VALUE:
  2320. if(!sd->state.lr_flag)
  2321. sd->bonus.sp_gain_value += val;
  2322. break;
  2323. case SP_HP_GAIN_VALUE:
  2324. if(!sd->state.lr_flag)
  2325. sd->bonus.hp_gain_value += val;
  2326. break;
  2327. case SP_MAGIC_SP_GAIN_VALUE:
  2328. if(!sd->state.lr_flag)
  2329. sd->bonus.magic_sp_gain_value += val;
  2330. break;
  2331. case SP_MAGIC_HP_GAIN_VALUE:
  2332. if(!sd->state.lr_flag)
  2333. sd->bonus.magic_hp_gain_value += val;
  2334. break;
  2335. case SP_ADD_HEAL_RATE:
  2336. if(sd->state.lr_flag != 2)
  2337. sd->bonus.add_heal_rate += val;
  2338. break;
  2339. case SP_ADD_HEAL2_RATE:
  2340. if(sd->state.lr_flag != 2)
  2341. sd->bonus.add_heal2_rate += val;
  2342. break;
  2343. case SP_ADD_ITEM_HEAL_RATE:
  2344. if(sd->state.lr_flag != 2)
  2345. sd->bonus.itemhealrate2 += val;
  2346. break;
  2347. case SP_EMATK:
  2348. if(sd->state.lr_flag != 2)
  2349. sd->bonus.ematk += val;
  2350. break;
  2351. case SP_FIXCASTRATE:
  2352. if(sd->state.lr_flag != 2)
  2353. sd->bonus.fixcastrate -= val;
  2354. break;
  2355. case SP_ADD_FIXEDCAST:
  2356. if(sd->state.lr_flag != 2)
  2357. sd->bonus.add_fixcast += val;
  2358. break;
  2359. #ifdef RENEWAL_CAST
  2360. case SP_VARCASTRATE:
  2361. if(sd->state.lr_flag != 2)
  2362. sd->bonus.varcastrate -= val;
  2363. break;
  2364. case SP_ADD_VARIABLECAST:
  2365. if(sd->state.lr_flag != 2)
  2366. sd->bonus.add_varcast += val;
  2367. break;
  2368. #endif
  2369. default:
  2370. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2371. break;
  2372. }
  2373. return 0;
  2374. }
  2375. /*==========================================
  2376. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2377. *------------------------------------------*/
  2378. int pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2379. {
  2380. int i;
  2381. nullpo_ret(sd);
  2382. switch(type){
  2383. case SP_ADDELE:
  2384. if(type2 >= ELE_MAX) {
  2385. ShowError("pc_bonus2: SP_ADDELE: Invalid element %d\n", type2);
  2386. break;
  2387. }
  2388. if(!sd->state.lr_flag)
  2389. sd->right_weapon.addele[type2]+=val;
  2390. else if(sd->state.lr_flag == 1)
  2391. sd->left_weapon.addele[type2]+=val;
  2392. else if(sd->state.lr_flag == 2)
  2393. sd->arrow_addele[type2]+=val;
  2394. break;
  2395. case SP_ADDRACE:
  2396. if(!sd->state.lr_flag)
  2397. sd->right_weapon.addrace[type2]+=val;
  2398. else if(sd->state.lr_flag == 1)
  2399. sd->left_weapon.addrace[type2]+=val;
  2400. else if(sd->state.lr_flag == 2)
  2401. sd->arrow_addrace[type2]+=val;
  2402. break;
  2403. case SP_ADDSIZE:
  2404. if(!sd->state.lr_flag)
  2405. sd->right_weapon.addsize[type2]+=val;
  2406. else if(sd->state.lr_flag == 1)
  2407. sd->left_weapon.addsize[type2]+=val;
  2408. else if(sd->state.lr_flag == 2)
  2409. sd->arrow_addsize[type2]+=val;
  2410. break;
  2411. case SP_SUBELE:
  2412. if(type2 >= ELE_MAX) {
  2413. ShowError("pc_bonus2: SP_SUBELE: Invalid element %d\n", type2);
  2414. break;
  2415. }
  2416. if(sd->state.lr_flag != 2)
  2417. sd->subele[type2]+=val;
  2418. break;
  2419. case SP_SUBRACE:
  2420. if(sd->state.lr_flag != 2)
  2421. sd->subrace[type2]+=val;
  2422. break;
  2423. case SP_ADDEFF:
  2424. if (type2 > SC_MAX) {
  2425. ShowWarning("pc_bonus2 (Add Effect): %d is not supported.\n", type2);
  2426. break;
  2427. }
  2428. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2429. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0);
  2430. break;
  2431. case SP_ADDEFF2:
  2432. if (type2 > SC_MAX) {
  2433. ShowWarning("pc_bonus2 (Add Effect2): %d is not supported.\n", type2);
  2434. break;
  2435. }
  2436. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2437. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF);
  2438. break;
  2439. case SP_RESEFF:
  2440. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2441. ShowWarning("pc_bonus2 (Resist Effect): %d is not supported.\n", type2);
  2442. break;
  2443. }
  2444. if(sd->state.lr_flag == 2)
  2445. break;
  2446. i = sd->reseff[type2-SC_COMMON_MIN]+val;
  2447. sd->reseff[type2-SC_COMMON_MIN]= cap_value(i, 0, 10000);
  2448. break;
  2449. case SP_MAGIC_ADDELE:
  2450. if(type2 >= ELE_MAX) {
  2451. ShowError("pc_bonus2: SP_MAGIC_ADDELE: Invalid element %d\n", type2);
  2452. break;
  2453. }
  2454. if(sd->state.lr_flag != 2)
  2455. sd->magic_addele[type2]+=val;
  2456. break;
  2457. case SP_MAGIC_ADDRACE:
  2458. if(sd->state.lr_flag != 2)
  2459. sd->magic_addrace[type2]+=val;
  2460. break;
  2461. case SP_MAGIC_ADDSIZE:
  2462. if(sd->state.lr_flag != 2)
  2463. sd->magic_addsize[type2]+=val;
  2464. break;
  2465. case SP_MAGIC_ATK_ELE:
  2466. if(sd->state.lr_flag != 2)
  2467. sd->magic_atk_ele[type2]+=val;
  2468. break;
  2469. case SP_ADD_DAMAGE_CLASS:
  2470. switch (sd->state.lr_flag) {
  2471. case 0: //Right hand
  2472. 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);
  2473. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2474. {
  2475. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2476. break;
  2477. }
  2478. sd->right_weapon.add_dmg[i].class_ = type2;
  2479. sd->right_weapon.add_dmg[i].rate += val;
  2480. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2481. 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]));
  2482. break;
  2483. case 1: //Left hand
  2484. 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);
  2485. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2486. {
  2487. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2488. break;
  2489. }
  2490. sd->left_weapon.add_dmg[i].class_ = type2;
  2491. sd->left_weapon.add_dmg[i].rate += val;
  2492. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2493. 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]));
  2494. break;
  2495. }
  2496. break;
  2497. case SP_ADD_MAGIC_DAMAGE_CLASS:
  2498. if(sd->state.lr_flag == 2)
  2499. break;
  2500. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2501. if (i == ARRAYLENGTH(sd->add_mdmg))
  2502. {
  2503. ShowWarning("pc_bonus2: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2504. break;
  2505. }
  2506. sd->add_mdmg[i].class_ = type2;
  2507. sd->add_mdmg[i].rate += val;
  2508. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2509. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2510. break;
  2511. case SP_ADD_DEF_CLASS:
  2512. if(sd->state.lr_flag == 2)
  2513. break;
  2514. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2515. if (i == ARRAYLENGTH(sd->add_def))
  2516. {
  2517. ShowWarning("pc_bonus2: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2518. break;
  2519. }
  2520. sd->add_def[i].class_ = type2;
  2521. sd->add_def[i].rate += val;
  2522. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2523. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2524. break;
  2525. case SP_ADD_MDEF_CLASS:
  2526. if(sd->state.lr_flag == 2)
  2527. break;
  2528. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2529. if (i == ARRAYLENGTH(sd->add_mdef))
  2530. {
  2531. ShowWarning("pc_bonus2: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2532. break;
  2533. }
  2534. sd->add_mdef[i].class_ = type2;
  2535. sd->add_mdef[i].rate += val;
  2536. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2537. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2538. break;
  2539. case SP_HP_DRAIN_RATE:
  2540. if(!sd->state.lr_flag) {
  2541. sd->right_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2542. sd->right_weapon.hp_drain[RC_NONBOSS].per += val;
  2543. sd->right_weapon.hp_drain[RC_BOSS].rate += type2;
  2544. sd->right_weapon.hp_drain[RC_BOSS].per += val;
  2545. }
  2546. else if(sd->state.lr_flag == 1) {
  2547. sd->left_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2548. sd->left_weapon.hp_drain[RC_NONBOSS].per += val;
  2549. sd->left_weapon.hp_drain[RC_BOSS].rate += type2;
  2550. sd->left_weapon.hp_drain[RC_BOSS].per += val;
  2551. }
  2552. break;
  2553. case SP_HP_DRAIN_VALUE:
  2554. if(!sd->state.lr_flag) {
  2555. sd->right_weapon.hp_drain[RC_NONBOSS].value += type2;
  2556. sd->right_weapon.hp_drain[RC_NONBOSS].type = val;
  2557. sd->right_weapon.hp_drain[RC_BOSS].value += type2;
  2558. sd->right_weapon.hp_drain[RC_BOSS].type = val;
  2559. }
  2560. else if(sd->state.lr_flag == 1) {
  2561. sd->left_weapon.hp_drain[RC_NONBOSS].value += type2;
  2562. sd->left_weapon.hp_drain[RC_NONBOSS].type = val;
  2563. sd->left_weapon.hp_drain[RC_BOSS].value += type2;
  2564. sd->left_weapon.hp_drain[RC_BOSS].type = val;
  2565. }
  2566. break;
  2567. case SP_SP_DRAIN_RATE:
  2568. if(!sd->state.lr_flag) {
  2569. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2570. sd->right_weapon.sp_drain[RC_NONBOSS].per += val;
  2571. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2572. sd->right_weapon.sp_drain[RC_BOSS].per += val;
  2573. }
  2574. else if(sd->state.lr_flag == 1) {
  2575. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2576. sd->left_weapon.sp_drain[RC_NONBOSS].per += val;
  2577. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2578. sd->left_weapon.sp_drain[RC_BOSS].per += val;
  2579. }
  2580. break;
  2581. case SP_SP_DRAIN_VALUE:
  2582. if(!sd->state.lr_flag) {
  2583. sd->right_weapon.sp_drain[RC_NONBOSS].value += type2;
  2584. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2585. sd->right_weapon.sp_drain[RC_BOSS].value += type2;
  2586. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2587. }
  2588. else if(sd->state.lr_flag == 1) {
  2589. sd->left_weapon.sp_drain[RC_NONBOSS].value += type2;
  2590. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2591. sd->left_weapon.sp_drain[RC_BOSS].value += type2;
  2592. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2593. }
  2594. break;
  2595. case SP_SP_VANISH_RATE:
  2596. if(sd->state.lr_flag != 2) {
  2597. sd->bonus.sp_vanish_rate += type2;
  2598. sd->bonus.sp_vanish_per += val;
  2599. }
  2600. break;
  2601. case SP_GET_ZENY_NUM:
  2602. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2603. sd->bonus.get_zeny_rate = val;
  2604. sd->bonus.get_zeny_num = type2;
  2605. }
  2606. break;
  2607. case SP_ADD_GET_ZENY_NUM:
  2608. if(sd->state.lr_flag != 2) {
  2609. sd->bonus.get_zeny_rate += val;
  2610. sd->bonus.get_zeny_num += type2;
  2611. }
  2612. break;
  2613. case SP_WEAPON_COMA_ELE:
  2614. if(type2 >= ELE_MAX) {
  2615. ShowError("pc_bonus2: SP_WEAPON_COMA_ELE: Invalid element %d\n", type2);
  2616. break;
  2617. }
  2618. if(sd->state.lr_flag == 2)
  2619. break;
  2620. sd->weapon_coma_ele[type2] += val;
  2621. sd->special_state.bonus_coma = 1;
  2622. break;
  2623. case SP_WEAPON_COMA_RACE:
  2624. if(sd->state.lr_flag == 2)
  2625. break;
  2626. sd->weapon_coma_race[type2] += val;
  2627. sd->special_state.bonus_coma = 1;
  2628. break;
  2629. case SP_WEAPON_ATK:
  2630. if(sd->state.lr_flag != 2)
  2631. sd->weapon_atk[type2]+=val;
  2632. break;
  2633. case SP_WEAPON_ATK_RATE:
  2634. if(sd->state.lr_flag != 2)
  2635. sd->weapon_atk_rate[type2]+=val;
  2636. break;
  2637. case SP_CRITICAL_ADDRACE:
  2638. if(sd->state.lr_flag != 2)
  2639. sd->critaddrace[type2] += val*10;
  2640. break;
  2641. case SP_ADDEFF_WHENHIT:
  2642. if (type2 > SC_MAX) {
  2643. ShowWarning("pc_bonus2 (Add Effect when hit): %d is not supported.\n", type2);
  2644. break;
  2645. }
  2646. if(sd->state.lr_flag != 2)
  2647. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0);
  2648. break;
  2649. case SP_SKILL_ATK:
  2650. if(sd->state.lr_flag == 2)
  2651. break;
  2652. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2653. if (i == ARRAYLENGTH(sd->skillatk))
  2654. { //Better mention this so the array length can be updated. [Skotlex]
  2655. ShowDebug("run_script: bonus2 bSkillAtk reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  2656. break;
  2657. }
  2658. if (sd->skillatk[i].id == type2)
  2659. sd->skillatk[i].val += val;
  2660. else {
  2661. sd->skillatk[i].id = type2;
  2662. sd->skillatk[i].val = val;
  2663. }
  2664. break;
  2665. case SP_SKILL_HEAL:
  2666. if(sd->state.lr_flag == 2)
  2667. break;
  2668. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2669. if (i == ARRAYLENGTH(sd->skillheal))
  2670. { // Better mention this so the array length can be updated. [Skotlex]
  2671. ShowDebug("run_script: bonus2 bSkillHeal reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  2672. break;
  2673. }
  2674. if (sd->skillheal[i].id == type2)
  2675. sd->skillheal[i].val += val;
  2676. else {
  2677. sd->skillheal[i].id = type2;
  2678. sd->skillheal[i].val = val;
  2679. }
  2680. break;
  2681. case SP_SKILL_HEAL2:
  2682. if(sd->state.lr_flag == 2)
  2683. break;
  2684. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  2685. if (i == ARRAYLENGTH(sd->skillheal2))
  2686. { // Better mention this so the array length can be updated. [Skotlex]
  2687. ShowDebug("run_script: bonus2 bSkillHeal2 reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  2688. break;
  2689. }
  2690. if (sd->skillheal2[i].id == type2)
  2691. sd->skillheal2[i].val += val;
  2692. else {
  2693. sd->skillheal2[i].id = type2;
  2694. sd->skillheal2[i].val = val;
  2695. }
  2696. break;
  2697. case SP_ADD_SKILL_BLOW:
  2698. if(sd->state.lr_flag == 2)
  2699. break;
  2700. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  2701. if (i == ARRAYLENGTH(sd->skillblown))
  2702. { //Better mention this so the array length can be updated. [Skotlex]
  2703. ShowDebug("run_script: bonus2 bSkillBlown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  2704. break;
  2705. }
  2706. if(sd->skillblown[i].id == type2)
  2707. sd->skillblown[i].val += val;
  2708. else {
  2709. sd->skillblown[i].id = type2;
  2710. sd->skillblown[i].val = val;
  2711. }
  2712. break;
  2713. #ifndef RENEWAL_CAST
  2714. case SP_VARCASTRATE:
  2715. #endif
  2716. case SP_CASTRATE:
  2717. if(sd->state.lr_flag == 2)
  2718. break;
  2719. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2720. if (i == ARRAYLENGTH(sd->skillcast))
  2721. { //Better mention this so the array length can be updated. [Skotlex]
  2722. ShowDebug("run_script: bonus2 %s reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
  2723. #ifndef RENEWAL_CAST
  2724. "bCastRate",
  2725. #else
  2726. "bVariableCastrate",
  2727. #endif
  2728. ARRAYLENGTH(sd->skillcast), type2, val);
  2729. break;
  2730. }
  2731. if(sd->skillcast[i].id == type2)
  2732. sd->skillcast[i].val += val;
  2733. else {
  2734. sd->skillcast[i].id = type2;
  2735. sd->skillcast[i].val = val;
  2736. }
  2737. break;
  2738. case SP_FIXCASTRATE:
  2739. if(sd->state.lr_flag == 2)
  2740. break;
  2741. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  2742. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  2743. {
  2744. ShowDebug("run_script: bonus2 bFixedCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  2745. break;
  2746. }
  2747. if(sd->skillfixcastrate[i].id == type2)
  2748. sd->skillfixcastrate[i].val += val;
  2749. else {
  2750. sd->skillfixcastrate[i].id = type2;
  2751. sd->skillfixcastrate[i].val = val;
  2752. }
  2753. break;
  2754. case SP_HP_LOSS_RATE:
  2755. if(sd->state.lr_flag != 2) {
  2756. sd->hp_loss.value = type2;
  2757. sd->hp_loss.rate = val;
  2758. }
  2759. break;
  2760. case SP_HP_REGEN_RATE:
  2761. if(sd->state.lr_flag != 2) {
  2762. sd->hp_regen.value = type2;
  2763. sd->hp_regen.rate = val;
  2764. }
  2765. break;
  2766. case SP_ADDRACE2:
  2767. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2768. break;
  2769. if(sd->state.lr_flag != 2)
  2770. sd->right_weapon.addrace2[type2] += val;
  2771. else
  2772. sd->left_weapon.addrace2[type2] += val;
  2773. break;
  2774. case SP_SUBSIZE:
  2775. if(sd->state.lr_flag != 2)
  2776. sd->subsize[type2]+=val;
  2777. break;
  2778. case SP_SUBRACE2:
  2779. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2780. break;
  2781. if(sd->state.lr_flag != 2)
  2782. sd->subrace2[type2]+=val;
  2783. break;
  2784. case SP_ADD_ITEM_HEAL_RATE:
  2785. if(sd->state.lr_flag == 2)
  2786. break;
  2787. if (type2 < MAX_ITEMGROUP) { //Group bonus
  2788. sd->itemgrouphealrate[type2] += val;
  2789. break;
  2790. }
  2791. //Standard item bonus.
  2792. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  2793. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  2794. ShowWarning("pc_bonus2: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  2795. break;
  2796. }
  2797. sd->itemhealrate[i].nameid = type2;
  2798. sd->itemhealrate[i].rate += val;
  2799. break;
  2800. case SP_EXP_ADDRACE:
  2801. if(sd->state.lr_flag != 2)
  2802. sd->expaddrace[type2]+=val;
  2803. break;
  2804. case SP_SP_GAIN_RACE:
  2805. if(sd->state.lr_flag != 2)
  2806. sd->sp_gain_race[type2]+=val;
  2807. break;
  2808. case SP_ADD_MONSTER_DROP_ITEM:
  2809. if (sd->state.lr_flag != 2)
  2810. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2811. break;
  2812. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2813. if (sd->state.lr_flag != 2)
  2814. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2815. break;
  2816. case SP_SP_LOSS_RATE:
  2817. if(sd->state.lr_flag != 2) {
  2818. sd->sp_loss.value = type2;
  2819. sd->sp_loss.rate = val;
  2820. }
  2821. break;
  2822. case SP_SP_REGEN_RATE:
  2823. if(sd->state.lr_flag != 2) {
  2824. sd->sp_regen.value = type2;
  2825. sd->sp_regen.rate = val;
  2826. }
  2827. break;
  2828. case SP_HP_DRAIN_VALUE_RACE:
  2829. if(!sd->state.lr_flag) {
  2830. sd->right_weapon.hp_drain[type2].value += val;
  2831. }
  2832. else if(sd->state.lr_flag == 1) {
  2833. sd->left_weapon.hp_drain[type2].value += val;
  2834. }
  2835. break;
  2836. case SP_SP_DRAIN_VALUE_RACE:
  2837. if(!sd->state.lr_flag) {
  2838. sd->right_weapon.sp_drain[type2].value += val;
  2839. }
  2840. else if(sd->state.lr_flag == 1) {
  2841. sd->left_weapon.sp_drain[type2].value += val;
  2842. }
  2843. break;
  2844. case SP_IGNORE_MDEF_RATE:
  2845. if(sd->state.lr_flag != 2)
  2846. sd->ignore_mdef[type2] += val;
  2847. break;
  2848. case SP_IGNORE_DEF_RATE:
  2849. if(sd->state.lr_flag != 2)
  2850. sd->ignore_def[type2] += val;
  2851. break;
  2852. case SP_SP_GAIN_RACE_ATTACK:
  2853. if(sd->state.lr_flag != 2)
  2854. sd->sp_gain_race_attack[type2] = cap_value(sd->sp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2855. break;
  2856. case SP_HP_GAIN_RACE_ATTACK:
  2857. if(sd->state.lr_flag != 2)
  2858. sd->hp_gain_race_attack[type2] = cap_value(sd->hp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2859. break;
  2860. case SP_SKILL_USE_SP_RATE: //bonus2 bSkillUseSPrate,n,x;
  2861. if(sd->state.lr_flag == 2)
  2862. break;
  2863. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  2864. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  2865. ShowDebug("run_script: bonus2 bSkillUseSPrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  2866. break;
  2867. }
  2868. if (sd->skillusesprate[i].id == type2)
  2869. sd->skillusesprate[i].val += val;
  2870. else {
  2871. sd->skillusesprate[i].id = type2;
  2872. sd->skillusesprate[i].val = val;
  2873. }
  2874. break;
  2875. case SP_SKILL_COOLDOWN:
  2876. if(sd->state.lr_flag == 2)
  2877. break;
  2878. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  2879. if (i == ARRAYLENGTH(sd->skillcooldown))
  2880. {
  2881. ShowDebug("run_script: bonus2 bSkillCoolDown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  2882. break;
  2883. }
  2884. if (sd->skillcooldown[i].id == type2)
  2885. sd->skillcooldown[i].val += val;
  2886. else {
  2887. sd->skillcooldown[i].id = type2;
  2888. sd->skillcooldown[i].val = val;
  2889. }
  2890. break;
  2891. case SP_SKILL_FIXEDCAST:
  2892. if(sd->state.lr_flag == 2)
  2893. break;
  2894. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  2895. if (i == ARRAYLENGTH(sd->skillfixcast))
  2896. {
  2897. ShowDebug("run_script: bonus2 bSkillFixedCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  2898. break;
  2899. }
  2900. if (sd->skillfixcast[i].id == type2)
  2901. sd->skillfixcast[i].val += val;
  2902. else {
  2903. sd->skillfixcast[i].id = type2;
  2904. sd->skillfixcast[i].val = val;
  2905. }
  2906. break;
  2907. case SP_SKILL_VARIABLECAST:
  2908. if(sd->state.lr_flag == 2)
  2909. break;
  2910. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  2911. if (i == ARRAYLENGTH(sd->skillvarcast))
  2912. {
  2913. ShowDebug("run_script: bonus2 bSkillVariableCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  2914. break;
  2915. }
  2916. if (sd->skillvarcast[i].id == type2)
  2917. sd->skillvarcast[i].val += val;
  2918. else {
  2919. sd->skillvarcast[i].id = type2;
  2920. sd->skillvarcast[i].val = val;
  2921. }
  2922. break;
  2923. #ifdef RENEWAL_CAST
  2924. case SP_VARCASTRATE:
  2925. if(sd->state.lr_flag == 2)
  2926. break;
  2927. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2928. if (i == ARRAYLENGTH(sd->skillcast))
  2929. {
  2930. ShowDebug("run_script: bonus2 bVariableCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",ARRAYLENGTH(sd->skillcast), type2, val);
  2931. break;
  2932. }
  2933. if(sd->skillcast[i].id == type2)
  2934. sd->skillcast[i].val -= val;
  2935. else {
  2936. sd->skillcast[i].id = type2;
  2937. sd->skillcast[i].val -= val;
  2938. }
  2939. break;
  2940. #endif
  2941. case SP_SKILL_USE_SP: //bonus2 bSkillUseSP,n,x;
  2942. if(sd->state.lr_flag == 2)
  2943. break;
  2944. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  2945. if (i == ARRAYLENGTH(sd->skillusesp)) {
  2946. ShowDebug("run_script: bonus2 bSkillUseSP reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  2947. break;
  2948. }
  2949. if (sd->skillusesp[i].id == type2)
  2950. sd->skillusesp[i].val += val;
  2951. else {
  2952. sd->skillusesp[i].id = type2;
  2953. sd->skillusesp[i].val = val;
  2954. }
  2955. break;
  2956. default:
  2957. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  2958. break;
  2959. }
  2960. return 0;
  2961. }
  2962. int pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  2963. {
  2964. nullpo_ret(sd);
  2965. switch(type){
  2966. case SP_ADD_MONSTER_DROP_ITEM:
  2967. if(sd->state.lr_flag != 2)
  2968. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, 1<<type3, val);
  2969. break;
  2970. case SP_ADD_CLASS_DROP_ITEM:
  2971. if(sd->state.lr_flag != 2)
  2972. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, -type3, val);
  2973. break;
  2974. case SP_AUTOSPELL:
  2975. if(sd->state.lr_flag != 2)
  2976. {
  2977. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2978. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2979. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  2980. target?-type2:type2, type3, val, 0, current_equip_card_id);
  2981. }
  2982. break;
  2983. case SP_AUTOSPELL_WHENHIT:
  2984. if(sd->state.lr_flag != 2)
  2985. {
  2986. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2987. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2988. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  2989. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  2990. }
  2991. break;
  2992. case SP_SP_DRAIN_RATE:
  2993. if(!sd->state.lr_flag) {
  2994. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2995. sd->right_weapon.sp_drain[RC_NONBOSS].per += type3;
  2996. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2997. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2998. sd->right_weapon.sp_drain[RC_BOSS].per += type3;
  2999. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  3000. }
  3001. else if(sd->state.lr_flag == 1) {
  3002. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  3003. sd->left_weapon.sp_drain[RC_NONBOSS].per += type3;
  3004. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  3005. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  3006. sd->left_weapon.sp_drain[RC_BOSS].per += type3;
  3007. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  3008. }
  3009. break;
  3010. case SP_HP_DRAIN_RATE_RACE:
  3011. if(!sd->state.lr_flag) {
  3012. sd->right_weapon.hp_drain[type2].rate += type3;
  3013. sd->right_weapon.hp_drain[type2].per += val;
  3014. }
  3015. else if(sd->state.lr_flag == 1) {
  3016. sd->left_weapon.hp_drain[type2].rate += type3;
  3017. sd->left_weapon.hp_drain[type2].per += val;
  3018. }
  3019. break;
  3020. case SP_SP_DRAIN_RATE_RACE:
  3021. if(!sd->state.lr_flag) {
  3022. sd->right_weapon.sp_drain[type2].rate += type3;
  3023. sd->right_weapon.sp_drain[type2].per += val;
  3024. }
  3025. else if(sd->state.lr_flag == 1) {
  3026. sd->left_weapon.sp_drain[type2].rate += type3;
  3027. sd->left_weapon.sp_drain[type2].per += val;
  3028. }
  3029. break;
  3030. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  3031. if (sd->state.lr_flag != 2)
  3032. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, 1<<type3, val);
  3033. break;
  3034. case SP_ADDEFF:
  3035. if (type2 > SC_MAX) {
  3036. ShowWarning("pc_bonus3 (Add Effect): %d is not supported.\n", type2);
  3037. break;
  3038. }
  3039. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3040. sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val);
  3041. break;
  3042. case SP_ADDEFF_WHENHIT:
  3043. if (type2 > SC_MAX) {
  3044. ShowWarning("pc_bonus3 (Add Effect when hit): %d is not supported.\n", type2);
  3045. break;
  3046. }
  3047. if(sd->state.lr_flag != 2)
  3048. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val);
  3049. break;
  3050. case SP_ADDEFF_ONSKILL:
  3051. if( type3 > SC_MAX ) {
  3052. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type3);
  3053. break;
  3054. }
  3055. if( sd->state.lr_flag != 2 )
  3056. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
  3057. break;
  3058. case SP_ADDELE:
  3059. if (type2 > ELE_MAX) {
  3060. ShowWarning("pc_bonus3 (SP_ADDELE): element %d is out of range.\n", type2);
  3061. break;
  3062. }
  3063. if (sd->state.lr_flag != 2)
  3064. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3065. break;
  3066. case SP_SUBELE:
  3067. if (type2 > ELE_MAX) {
  3068. ShowWarning("pc_bonus3 (SP_SUBELE): element %d is out of range.\n", type2);
  3069. break;
  3070. }
  3071. if (sd->state.lr_flag != 2)
  3072. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3073. break;
  3074. default:
  3075. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3076. break;
  3077. }
  3078. return 0;
  3079. }
  3080. int pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3081. {
  3082. nullpo_ret(sd);
  3083. switch(type){
  3084. case SP_AUTOSPELL:
  3085. if(sd->state.lr_flag != 2)
  3086. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3087. break;
  3088. case SP_AUTOSPELL_WHENHIT:
  3089. if(sd->state.lr_flag != 2)
  3090. 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);
  3091. break;
  3092. case SP_AUTOSPELL_ONSKILL:
  3093. if(sd->state.lr_flag != 2)
  3094. {
  3095. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3096. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3097. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3098. }
  3099. break;
  3100. case SP_ADDEFF_ONSKILL:
  3101. if( type2 > SC_MAX ) {
  3102. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type2);
  3103. break;
  3104. }
  3105. if( sd->state.lr_flag != 2 )
  3106. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
  3107. break;
  3108. default:
  3109. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3110. break;
  3111. }
  3112. return 0;
  3113. }
  3114. int pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,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, type5, 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, type5, current_equip_card_id);
  3125. break;
  3126. case SP_AUTOSPELL_ONSKILL:
  3127. if(sd->state.lr_flag != 2)
  3128. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3129. break;
  3130. default:
  3131. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3132. break;
  3133. }
  3134. return 0;
  3135. }
  3136. /*==========================================
  3137. * Grants a player a given skill. Flag values are:
  3138. * 0 - Grant permanent skill to be bound to skill tree
  3139. * 1 - Grant an item skill (temporary)
  3140. * 2 - Like 1, except the level granted can stack with previously learned level.
  3141. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3142. *------------------------------------------*/
  3143. int pc_skill(TBL_PC* sd, int id, int level, int flag)
  3144. {
  3145. nullpo_ret(sd);
  3146. if( id <= 0 || id >= MAX_SKILL || skill_db[id].name == NULL) {
  3147. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", id);
  3148. return 0;
  3149. }
  3150. if( level > MAX_SKILL_LEVEL ) {
  3151. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3152. return 0;
  3153. }
  3154. if( flag == 2 && sd->status.skill[id].lv + level > MAX_SKILL_LEVEL ) {
  3155. 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);
  3156. return 0;
  3157. }
  3158. switch( flag ){
  3159. case 0: //Set skill data overwriting whatever was there before.
  3160. sd->status.skill[id].id = id;
  3161. sd->status.skill[id].lv = level;
  3162. sd->status.skill[id].flag = SKILL_FLAG_PERMANENT;
  3163. if( level == 0 ) { //Remove skill.
  3164. sd->status.skill[id].id = 0;
  3165. clif_deleteskill(sd,id);
  3166. } else
  3167. clif_addskill(sd,id);
  3168. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3169. status_calc_pc(sd, 0);
  3170. break;
  3171. case 1: //Item bonus skill.
  3172. if( sd->status.skill[id].id == id ){
  3173. if( sd->status.skill[id].lv >= level )
  3174. return 0;
  3175. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT ) //Non-granted skill, store it's level.
  3176. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv;
  3177. } else {
  3178. sd->status.skill[id].id = id;
  3179. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY;
  3180. }
  3181. sd->status.skill[id].lv = level;
  3182. break;
  3183. case 2: //Add skill bonus on top of what you had.
  3184. if( sd->status.skill[id].id == id ){
  3185. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT )
  3186. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Store previous level.
  3187. } else {
  3188. sd->status.skill[id].id = id;
  3189. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3190. }
  3191. sd->status.skill[id].lv += level;
  3192. break;
  3193. case 4: //Permanent granted skills ignore the skill tree
  3194. sd->status.skill[id].id = id;
  3195. sd->status.skill[id].lv = level;
  3196. sd->status.skill[id].flag = SKILL_FLAG_PERM_GRANTED;
  3197. if( level == 0 ) { //Remove skill.
  3198. sd->status.skill[id].id = 0;
  3199. clif_deleteskill(sd,id);
  3200. } else
  3201. clif_addskill(sd,id);
  3202. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3203. status_calc_pc(sd, 0);
  3204. break;
  3205. default: //Unknown flag?
  3206. return 0;
  3207. }
  3208. return 1;
  3209. }
  3210. /*==========================================
  3211. * Append a card to an item ?
  3212. *------------------------------------------*/
  3213. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3214. {
  3215. int i;
  3216. int nameid;
  3217. nullpo_ret(sd);
  3218. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3219. return 0; //Invalid item index.
  3220. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3221. return 0; //Invalid card index.
  3222. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3223. return 0; // target item missing
  3224. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3225. return 0; // target card missing
  3226. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3227. return 0; // only weapons and armor are allowed
  3228. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3229. return 0; // must be a card
  3230. if( sd->status.inventory[idx_equip].identify == 0 )
  3231. return 0; // target must be identified
  3232. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3233. return 0; // card slots reserved for other purposes
  3234. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3235. return 0; // card cannot be compounded on this item type
  3236. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3237. return 0; // attempted to place shield card on left-hand weapon.
  3238. if( sd->status.inventory[idx_equip].equip != 0 )
  3239. return 0; // item must be unequipped
  3240. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3241. if( i == sd->inventory_data[idx_equip]->slot )
  3242. return 0; // no free slots
  3243. // remember the card id to insert
  3244. nameid = sd->status.inventory[idx_card].nameid;
  3245. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3246. {// failed
  3247. clif_insert_card(sd,idx_equip,idx_card,1);
  3248. }
  3249. else
  3250. {// success
  3251. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3252. sd->status.inventory[idx_equip].card[i] = nameid;
  3253. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3254. clif_insert_card(sd,idx_equip,idx_card,0);
  3255. }
  3256. return 0;
  3257. }
  3258. //
  3259. // Items
  3260. //
  3261. /*==========================================
  3262. * Update buying value by skills
  3263. *------------------------------------------*/
  3264. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3265. {
  3266. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3267. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3268. rate1 = 5+skill*2-((skill==10)? 1:0);
  3269. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3270. rate2 = 5+skill*4;
  3271. if(rate1 < rate2) rate1 = rate2;
  3272. if(rate1)
  3273. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3274. if(val < 0) val = 0;
  3275. if(orig_value > 0 && val < 1) val = 1;
  3276. return val;
  3277. }
  3278. /*==========================================
  3279. * Update selling value by skills
  3280. *------------------------------------------*/
  3281. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3282. {
  3283. int skill,val = orig_value,rate = 0;
  3284. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3285. rate = 5+skill*2-((skill==10)? 1:0);
  3286. if(rate)
  3287. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3288. if(val < 0) val = 0;
  3289. if(orig_value > 0 && val < 1) val = 1;
  3290. return val;
  3291. }
  3292. /*==========================================
  3293. * Checking if we have enough place on inventory for new item
  3294. * Make sure to take 30k as limit (for client I guess)
  3295. *------------------------------------------*/
  3296. int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
  3297. {
  3298. int i;
  3299. struct item_data* data;
  3300. nullpo_ret(sd);
  3301. if(amount > MAX_AMOUNT)
  3302. return CHKADDITEM_OVERAMOUNT;
  3303. data = itemdb_search(nameid);
  3304. if(!itemdb_isstackable2(data))
  3305. return CHKADDITEM_NEW;
  3306. if( data->stack.inventory && amount > data->stack.amount )
  3307. return CHKADDITEM_OVERAMOUNT;
  3308. for(i=0;i<MAX_INVENTORY;i++){
  3309. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3310. if(sd->status.inventory[i].nameid==nameid){
  3311. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3312. return CHKADDITEM_OVERAMOUNT;
  3313. return CHKADDITEM_EXIST;
  3314. }
  3315. }
  3316. return CHKADDITEM_NEW;
  3317. }
  3318. /*==========================================
  3319. * Return number of available place in inventory
  3320. * Each non stackable item will reduce place by 1
  3321. *------------------------------------------*/
  3322. int pc_inventoryblank(struct map_session_data *sd)
  3323. {
  3324. int i,b;
  3325. nullpo_ret(sd);
  3326. for(i=0,b=0;i<MAX_INVENTORY;i++){
  3327. if(sd->status.inventory[i].nameid==0)
  3328. b++;
  3329. }
  3330. return b;
  3331. }
  3332. /*==========================================
  3333. * attempts to remove zeny from player (sd)
  3334. *------------------------------------------*/
  3335. int pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3336. {
  3337. nullpo_retr(-1,sd);
  3338. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3339. if( zeny < 0 )
  3340. {
  3341. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3342. return 1;
  3343. }
  3344. if( sd->status.zeny < zeny )
  3345. return 1; //Not enough.
  3346. sd->status.zeny -= zeny;
  3347. clif_updatestatus(sd,SP_ZENY);
  3348. if(!tsd) tsd = sd;
  3349. log_zeny(sd, type, tsd, -zeny);
  3350. if( zeny > 0 && sd->state.showzeny ) {
  3351. char output[255];
  3352. sprintf(output, "Removed %dz.", zeny);
  3353. clif_disp_onlyself(sd,output,strlen(output));
  3354. }
  3355. return 0;
  3356. }
  3357. /*==========================================
  3358. * Cash Shop
  3359. *------------------------------------------*/
  3360. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type ){
  3361. int cash;
  3362. nullpo_retr(-1,sd);
  3363. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3364. if( price < 0 || points < 0 )
  3365. {
  3366. 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);
  3367. return -2;
  3368. }
  3369. if( points > price )
  3370. {
  3371. 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);
  3372. points = price;
  3373. }
  3374. cash = price-points;
  3375. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3376. {
  3377. 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);
  3378. return -1;
  3379. }
  3380. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints-cash);
  3381. if( cash ){
  3382. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3383. }
  3384. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints-points);
  3385. if( points ){
  3386. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3387. }
  3388. if( battle_config.cashshop_show_points )
  3389. {
  3390. char output[128];
  3391. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3392. clif_disp_onlyself(sd, output, strlen(output));
  3393. }
  3394. return cash+points;
  3395. }
  3396. int pc_getcash( struct map_session_data *sd, int cash, int points, e_log_pick_type type ){
  3397. char output[128];
  3398. nullpo_retr(-1,sd);
  3399. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3400. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3401. if( cash > 0 )
  3402. {
  3403. if( cash > MAX_ZENY-sd->cashPoints )
  3404. {
  3405. 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);
  3406. cash = MAX_ZENY-sd->cashPoints;
  3407. }
  3408. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints+cash);
  3409. if( cash ){
  3410. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3411. }
  3412. if( battle_config.cashshop_show_points )
  3413. {
  3414. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3415. clif_disp_onlyself(sd, output, strlen(output));
  3416. }
  3417. return cash;
  3418. }
  3419. else if( cash < 0 )
  3420. {
  3421. 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);
  3422. return -1;
  3423. }
  3424. if( points > 0 )
  3425. {
  3426. if( points > MAX_ZENY-sd->kafraPoints )
  3427. {
  3428. 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);
  3429. points = MAX_ZENY-sd->kafraPoints;
  3430. }
  3431. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints+points);
  3432. if( points ){
  3433. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3434. }
  3435. if( battle_config.cashshop_show_points )
  3436. {
  3437. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3438. clif_disp_onlyself(sd, output, strlen(output));
  3439. }
  3440. return points;
  3441. }
  3442. else if( points < 0 )
  3443. {
  3444. 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);
  3445. return -1;
  3446. }
  3447. return -2; //shouldn't happen but jsut in case
  3448. }
  3449. /*==========================================
  3450. * Attempts to give zeny to player (sd)
  3451. * tsd (optional) from who for log (if null take sd)
  3452. *------------------------------------------*/
  3453. int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3454. {
  3455. nullpo_retr(-1,sd);
  3456. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3457. if( zeny < 0 )
  3458. {
  3459. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3460. return 1;
  3461. }
  3462. if( zeny > MAX_ZENY - sd->status.zeny )
  3463. zeny = MAX_ZENY - sd->status.zeny;
  3464. sd->status.zeny += zeny;
  3465. clif_updatestatus(sd,SP_ZENY);
  3466. if(!tsd) tsd = sd;
  3467. log_zeny(sd, type, tsd, zeny);
  3468. if( zeny > 0 && sd->state.showzeny ) {
  3469. char output[255];
  3470. sprintf(output, "Gained %dz.", zeny);
  3471. clif_disp_onlyself(sd,output,strlen(output));
  3472. }
  3473. return 0;
  3474. }
  3475. /*==========================================
  3476. * Searching a specified itemid in inventory and return his stored index
  3477. *------------------------------------------*/
  3478. int pc_search_inventory(struct map_session_data *sd,int item_id)
  3479. {
  3480. int i;
  3481. nullpo_retr(-1, sd);
  3482. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
  3483. return ( i < MAX_INVENTORY ) ? i : -1;
  3484. }
  3485. /*==========================================
  3486. * Attempt to add a new item to inventory.
  3487. * Return:
  3488. 0 = success
  3489. 1 = invalid itemid not found or negative amount
  3490. 2 = overweight
  3491. 3 = ?
  3492. 4 = no free place found
  3493. 5 = max amount reached
  3494. 6 = ?
  3495. 7 = stack limitation
  3496. *------------------------------------------*/
  3497. int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3498. {
  3499. struct item_data *data;
  3500. int i;
  3501. unsigned int w;
  3502. nullpo_retr(1, sd);
  3503. nullpo_retr(1, item_data);
  3504. if( item_data->nameid <= 0 || amount <= 0 )
  3505. return ADDITEM_INVALID;
  3506. if( amount > MAX_AMOUNT )
  3507. return ADDITEM_OVERAMOUNT;
  3508. data = itemdb_search(item_data->nameid);
  3509. if( data->stack.inventory && amount > data->stack.amount )
  3510. {// item stack limitation
  3511. return ADDITEM_STACKLIMIT;
  3512. }
  3513. w = data->weight*amount;
  3514. if(sd->weight + w > sd->max_weight)
  3515. return ADDITEM_OVERWEIGHT;
  3516. i = MAX_INVENTORY;
  3517. if( itemdb_isstackable2(data) && item_data->expire_time == 0 )
  3518. { // Stackable | Non Rental
  3519. for( i = 0; i < MAX_INVENTORY; i++ )
  3520. {
  3521. 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 )
  3522. {
  3523. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3524. return 5;
  3525. sd->status.inventory[i].amount += amount;
  3526. clif_additem(sd,i,amount,0);
  3527. break;
  3528. }
  3529. }
  3530. }
  3531. if( i >= MAX_INVENTORY )
  3532. {
  3533. i = pc_search_inventory(sd,0);
  3534. if( i < 0 )
  3535. return ADDITEM_OVERITEM;
  3536. memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
  3537. // clear equips field first, just in case
  3538. if( item_data->equip )
  3539. sd->status.inventory[i].equip = 0;
  3540. sd->status.inventory[i].amount = amount;
  3541. sd->inventory_data[i] = data;
  3542. clif_additem(sd,i,amount,0);
  3543. }
  3544. #ifdef NSI_UNIQUE_ID
  3545. if( !itemdb_isstackable2(data) && !item_data->unique_id )
  3546. sd->status.inventory[i].unique_id = itemdb_unique_id(0,0);
  3547. #endif
  3548. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  3549. sd->weight += w;
  3550. clif_updatestatus(sd,SP_WEIGHT);
  3551. //Auto-equip
  3552. if(data->flag.autoequip)
  3553. pc_equipitem(sd, i, data->equip);
  3554. /* rental item check */
  3555. if( item_data->expire_time ) {
  3556. if( time(NULL) > item_data->expire_time ) {
  3557. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  3558. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  3559. } else {
  3560. int seconds = (int)( item_data->expire_time - time(NULL) );
  3561. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  3562. pc_inventory_rental_add(sd, seconds);
  3563. }
  3564. }
  3565. return ADDITEM_SUCCESS;
  3566. }
  3567. /*==========================================
  3568. * Remove an item at index n from inventory by amount.
  3569. * Parameters :
  3570. * @type
  3571. * 1 : don't notify deletion
  3572. * 2 : don't notify weight change
  3573. * Return:
  3574. * 0 = success
  3575. * 1 = invalid itemid or negative amount
  3576. *------------------------------------------*/
  3577. int pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  3578. {
  3579. nullpo_retr(1, sd);
  3580. if(sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  3581. return 1;
  3582. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  3583. sd->status.inventory[n].amount -= amount;
  3584. sd->weight -= sd->inventory_data[n]->weight*amount ;
  3585. if( sd->status.inventory[n].amount <= 0 ){
  3586. if(sd->status.inventory[n].equip)
  3587. pc_unequipitem(sd,n,3);
  3588. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  3589. sd->inventory_data[n] = NULL;
  3590. }
  3591. if(!(type&1))
  3592. clif_delitem(sd,n,amount,reason);
  3593. if(!(type&2))
  3594. clif_updatestatus(sd,SP_WEIGHT);
  3595. return 0;
  3596. }
  3597. /*==========================================
  3598. * Attempt to drop an item.
  3599. * Return:
  3600. * 0 = fail
  3601. * 1 = success
  3602. *------------------------------------------*/
  3603. int pc_dropitem(struct map_session_data *sd,int n,int amount)
  3604. {
  3605. nullpo_retr(1, sd);
  3606. if(n < 0 || n >= MAX_INVENTORY)
  3607. return 0;
  3608. if(amount <= 0)
  3609. return 0;
  3610. if(sd->status.inventory[n].nameid <= 0 ||
  3611. sd->status.inventory[n].amount <= 0 ||
  3612. sd->status.inventory[n].amount < amount ||
  3613. sd->state.trading || sd->state.vending ||
  3614. !sd->inventory_data[n] //pc_delitem would fail on this case.
  3615. )
  3616. return 0;
  3617. if( map[sd->bl.m].flag.nodrop )
  3618. {
  3619. clif_displaymessage (sd->fd, msg_txt(sd,271));
  3620. return 0; //Can't drop items in nodrop mapflag maps.
  3621. }
  3622. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  3623. {
  3624. clif_displaymessage (sd->fd, msg_txt(sd,263));
  3625. return 0;
  3626. }
  3627. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
  3628. return 0;
  3629. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  3630. clif_dropitem(sd, n, amount);
  3631. return 1;
  3632. }
  3633. /*==========================================
  3634. * Attempt to pick up an item.
  3635. * Return:
  3636. * 0 = fail
  3637. * 1 = success
  3638. *------------------------------------------*/
  3639. int pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  3640. {
  3641. int flag=0;
  3642. unsigned int tick = gettick();
  3643. struct map_session_data *first_sd = NULL,*second_sd = NULL,*third_sd = NULL;
  3644. struct party_data *p=NULL;
  3645. nullpo_ret(sd);
  3646. nullpo_ret(fitem);
  3647. if(!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  3648. return 0; // Distance is too far
  3649. if( sd->sc.cant.pickup )
  3650. return 0;
  3651. if (sd->status.party_id)
  3652. p = party_search(sd->status.party_id);
  3653. if(fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id)
  3654. {
  3655. first_sd = map_charid2sd(fitem->first_get_charid);
  3656. if(DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  3657. if (!(p && p->party.item&1 &&
  3658. first_sd && first_sd->status.party_id == sd->status.party_id
  3659. ))
  3660. return 0;
  3661. }
  3662. else
  3663. if(fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id)
  3664. {
  3665. second_sd = map_charid2sd(fitem->second_get_charid);
  3666. if(DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  3667. if(!(p && p->party.item&1 &&
  3668. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3669. (second_sd && second_sd->status.party_id == sd->status.party_id))
  3670. ))
  3671. return 0;
  3672. }
  3673. else
  3674. if(fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id)
  3675. {
  3676. third_sd = map_charid2sd(fitem->third_get_charid);
  3677. if(DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  3678. if(!(p && p->party.item&1 &&
  3679. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3680. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  3681. (third_sd && third_sd->status.party_id == sd->status.party_id))
  3682. ))
  3683. return 0;
  3684. }
  3685. }
  3686. }
  3687. }
  3688. //This function takes care of giving the item to whoever should have it, considering party-share options.
  3689. if ((flag = party_share_loot(p,sd,&fitem->item_data, fitem->first_get_charid))) {
  3690. clif_additem(sd,0,0,flag);
  3691. return 1;
  3692. }
  3693. //Display pickup animation.
  3694. pc_stop_attack(sd);
  3695. clif_takeitem(&sd->bl,&fitem->bl);
  3696. map_clearflooritem(&fitem->bl);
  3697. return 1;
  3698. }
  3699. /*==========================================
  3700. * Check if item is usable.
  3701. * Return:
  3702. * 0 = no
  3703. * 1 = yes
  3704. *------------------------------------------*/
  3705. int pc_isUseitem(struct map_session_data *sd,int n)
  3706. {
  3707. struct item_data *item;
  3708. int nameid;
  3709. nullpo_ret(sd);
  3710. item = sd->inventory_data[n];
  3711. nameid = sd->status.inventory[n].nameid;
  3712. if( item == NULL )
  3713. return 0;
  3714. //Not consumable item
  3715. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  3716. return 0;
  3717. else if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  3718. return 0;
  3719. if( !item->script ) //if it has no script, you can't really consume it!
  3720. return 0;
  3721. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  3722. return 0; // You cannot use this item while sitting.
  3723. }
  3724. switch( nameid ) //@TODO, lot of hardcoded nameid here
  3725. {
  3726. case 605: // Anodyne
  3727. if( map_flag_gvg(sd->bl.m) )
  3728. return 0;
  3729. case 606:
  3730. if( pc_issit(sd) )
  3731. return 0;
  3732. break;
  3733. case 601: // Fly Wing
  3734. case 12212: // Giant Fly Wing
  3735. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) )
  3736. {
  3737. clif_skill_teleportmessage(sd,0);
  3738. return 0;
  3739. }
  3740. case 602: // ButterFly Wing
  3741. case 14527: // Dungeon Teleport Scroll
  3742. case 14581: // Dungeon Teleport Scroll 2
  3743. case 12352: // Dungeon Teleport Scroll 3
  3744. case 14582: // Yellow Butterfly Wing
  3745. case 14583: // Green Butterfly Wing
  3746. case 14584: // Red Butterfly Wing
  3747. case 14585: // Blue Butterfly Wing
  3748. case 14591: // Siege Teleport Scroll
  3749. if( sd->duel_group && !battle_config.duel_allow_teleport )
  3750. {
  3751. clif_displaymessage(sd->fd, msg_txt(sd,663));
  3752. return 0;
  3753. }
  3754. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  3755. return 0;
  3756. break;
  3757. case 604: // Dead Branch
  3758. case 12024: // Red Pouch
  3759. case 12103: // Bloody Branch
  3760. case 12109: // Poring Box
  3761. if( map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m) )
  3762. return 0;
  3763. break;
  3764. case 12210: // Bubble Gum
  3765. case 12264: // Comp Bubble Gum
  3766. if( sd->sc.data[SC_ITEMBOOST] )
  3767. return 0;
  3768. break;
  3769. case 12208: // Battle Manual
  3770. case 12263: // Comp Battle Manual
  3771. case 12312: // Thick Battle Manual
  3772. case 12705: // Noble Nameplate
  3773. case 14532: // Battle_Manual25
  3774. case 14533: // Battle_Manual100
  3775. case 14545: // Battle_Manual300
  3776. if( sd->sc.data[SC_EXPBOOST] )
  3777. return 0;
  3778. break;
  3779. case 14592: // JOB_Battle_Manual
  3780. if( sd->sc.data[SC_JEXPBOOST] )
  3781. return 0;
  3782. break;
  3783. // Mercenary Items
  3784. case 12184: // Mercenary's Red Potion
  3785. case 12185: // Mercenary's Blue Potion
  3786. case 12241: // Mercenary's Concentration Potion
  3787. case 12242: // Mercenary's Awakening Potion
  3788. case 12243: // Mercenary's Berserk Potion
  3789. if( sd->md == NULL || sd->md->db == NULL )
  3790. return 0;
  3791. if (sd->md->sc.data[SC_BERSERK] || sd->md->sc.data[SC_SATURDAYNIGHTFEVER])
  3792. return 0;
  3793. if( nameid == 12242 && sd->md->db->lv < 40 )
  3794. return 0;
  3795. if( nameid == 12243 && sd->md->db->lv < 80 )
  3796. return 0;
  3797. break;
  3798. case 12213: //Neuralizer
  3799. if( !map[sd->bl.m].flag.reset )
  3800. return 0;
  3801. break;
  3802. }
  3803. if( nameid >= 12153 && nameid <= 12182 && sd->md != NULL )
  3804. return 0; // Mercenary Scrolls
  3805. /**
  3806. * Only Rune Knights may use runes
  3807. **/
  3808. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  3809. return 0;
  3810. /**
  3811. * Only GCross may use poisons
  3812. **/
  3813. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  3814. return 0;
  3815. //Gender check
  3816. if(item->sex != 2 && sd->status.sex != item->sex)
  3817. return 0;
  3818. //Required level check
  3819. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  3820. return 0;
  3821. #ifdef RENEWAL
  3822. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  3823. return 0;
  3824. #endif
  3825. //Not equipable by class. [Skotlex]
  3826. if (!(
  3827. (1<<(sd->class_&MAPID_BASEMASK)) &
  3828. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  3829. ))
  3830. return 0;
  3831. //Not usable by upper class. [Inkfish]
  3832. while( 1 ) {
  3833. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  3834. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  3835. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  3836. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  3837. return 0;
  3838. }
  3839. //Dead Branch & Bloody Branch & Porings Box
  3840. // FIXME: outdated, use constants or database
  3841. if( nameid == 604 || nameid == 12103 || nameid == 12109 )
  3842. log_branch(sd);
  3843. return 1;
  3844. }
  3845. /*==========================================
  3846. * Last checks to use an item.
  3847. * Return:
  3848. * 0 = fail
  3849. * 1 = success
  3850. *------------------------------------------*/
  3851. int pc_useitem(struct map_session_data *sd,int n)
  3852. {
  3853. unsigned int tick = gettick();
  3854. int amount, nameid;
  3855. struct script_code *script;
  3856. nullpo_ret(sd);
  3857. if( sd->npc_id ){
  3858. #ifdef RENEWAL
  3859. clif_msg(sd, USAGE_FAIL); // TODO look for the client date that has this message.
  3860. #endif
  3861. return 0;
  3862. }
  3863. if( sd->status.inventory[n].nameid <= 0 || sd->status.inventory[n].amount <= 0 )
  3864. return 0;
  3865. if( !pc_isUseitem(sd,n) )
  3866. return 0;
  3867. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  3868. nameid = sd->inventory_data[n]->nameid;
  3869. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  3870. return 0;
  3871. if (sd->sc.count && (
  3872. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  3873. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  3874. sd->sc.data[SC_TRICKDEAD] ||
  3875. sd->sc.data[SC_HIDING] ||
  3876. sd->sc.data[SC__SHADOWFORM] ||
  3877. sd->sc.data[SC__MANHOLE] ||
  3878. sd->sc.data[SC_KAGEHUMI] ||
  3879. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM)
  3880. ))
  3881. return 0;
  3882. //Prevent mass item usage. [Skotlex]
  3883. if( DIFF_TICK(sd->canuseitem_tick, tick) > 0 ||
  3884. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick, tick) > 0)
  3885. )
  3886. return 0;
  3887. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  3888. if( sd->inventory_data[n]->flag.delay_consume && nameid != ITEMID_REINS_OF_MOUNT ) {
  3889. if( sd->sc.option&OPTION_MOUNTING )
  3890. return 0;
  3891. else if( pc_issit(sd) )
  3892. return 0;
  3893. }
  3894. //Since most delay-consume items involve using a "skill-type" target cursor,
  3895. //perform a skill-use check before going through. [Skotlex]
  3896. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  3897. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  3898. if( sd->inventory_data[n]->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  3899. return 0;
  3900. if( sd->inventory_data[n]->delay > 0 ) {
  3901. int i;
  3902. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
  3903. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  3904. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  3905. if( i < MAX_ITEMDELAYS ) {
  3906. if( sd->item_delay[i].nameid ) {// found
  3907. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  3908. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  3909. char e_msg[100];
  3910. if( e_tick > 99 )
  3911. sprintf(e_msg,msg_txt(sd,379), //Item Failed. [%s] is cooling down. Wait %.1f minutes.
  3912. itemdb_jname(sd->status.inventory[n].nameid),
  3913. (double)e_tick / 60);
  3914. else
  3915. sprintf(e_msg,msg_txt(sd,380), //Item Failed. [%s] is cooling down. Wait %d seconds.
  3916. itemdb_jname(sd->status.inventory[n].nameid),
  3917. e_tick+1);
  3918. clif_colormes(sd,color_table[COLOR_RED],e_msg);
  3919. return 0; // Delay has not expired yet
  3920. }
  3921. } else {// not yet used item (all slots are initially empty)
  3922. sd->item_delay[i].nameid = nameid;
  3923. }
  3924. if( !(nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  3925. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  3926. } else {// should not happen
  3927. ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%d, char_id=%d)\n", nameid, sd->status.char_id);
  3928. }
  3929. //clean up used delays so we can give room for more
  3930. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  3931. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  3932. sd->item_delay[i].tick = 0;
  3933. sd->item_delay[i].nameid = 0;
  3934. }
  3935. }
  3936. }
  3937. /* on restricted maps the item is consumed but the effect is not used */
  3938. if (
  3939. (!map_flag_vs(sd->bl.m) && sd->inventory_data[n]->flag.no_equip&1) || // Normal
  3940. (map[sd->bl.m].flag.pvp && sd->inventory_data[n]->flag.no_equip&2) || // PVP
  3941. (map_flag_gvg(sd->bl.m) && sd->inventory_data[n]->flag.no_equip&4) || // GVG
  3942. (map[sd->bl.m].flag.battleground && sd->inventory_data[n]->flag.no_equip&8) || // Battleground
  3943. (map[sd->bl.m].flag.restricted && sd->inventory_data[n]->flag.no_equip&(8*map[sd->bl.m].zone)) // Zone restriction
  3944. ) {
  3945. if( battle_config.item_restricted_consumption_type ) {
  3946. clif_useitemack(sd,n,sd->status.inventory[n].amount-1,true);
  3947. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  3948. }
  3949. return 0;/* regardless, effect is not run */
  3950. }
  3951. sd->itemid = sd->status.inventory[n].nameid;
  3952. sd->itemindex = n;
  3953. if(sd->catch_target_class != -1) //Abort pet catching.
  3954. sd->catch_target_class = -1;
  3955. amount = sd->status.inventory[n].amount;
  3956. script = sd->inventory_data[n]->script;
  3957. //Check if the item is to be consumed immediately [Skotlex]
  3958. if( sd->inventory_data[n]->flag.delay_consume )
  3959. clif_useitemack(sd,n,amount,true);
  3960. else {
  3961. if( sd->status.inventory[n].expire_time == 0 ) {
  3962. clif_useitemack(sd,n,amount-1,true);
  3963. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  3964. } else
  3965. clif_useitemack(sd,n,0,false);
  3966. }
  3967. if(sd->status.inventory[n].card[0]==CARD0_CREATE &&
  3968. pc_famerank(MakeDWord(sd->status.inventory[n].card[2],sd->status.inventory[n].card[3]), MAPID_ALCHEMIST))
  3969. {
  3970. potion_flag = 2; // Famous player's potions have 50% more efficiency
  3971. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  3972. potion_flag = 3; //Even more effective potions.
  3973. }
  3974. //Update item use time.
  3975. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  3976. if( itemdb_iscashfood(nameid) )
  3977. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  3978. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  3979. potion_flag = 0;
  3980. return 1;
  3981. }
  3982. /*==========================================
  3983. * Add item on cart for given index.
  3984. * Return:
  3985. * 0 = success
  3986. * 1 = fail
  3987. *------------------------------------------*/
  3988. int pc_cart_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3989. {
  3990. struct item_data *data;
  3991. int i,w;
  3992. nullpo_retr(1, sd);
  3993. nullpo_retr(1, item_data);
  3994. if(item_data->nameid <= 0 || amount <= 0)
  3995. return 1;
  3996. data = itemdb_search(item_data->nameid);
  3997. if( data->stack.cart && amount > data->stack.amount )
  3998. {// item stack limitation
  3999. return 1;
  4000. }
  4001. if( !itemdb_cancartstore(item_data, pc_get_group_level(sd)) || (item_data->bound > 1 && !pc_can_give_bounded_items(sd)))
  4002. { // Check item trade restrictions [Skotlex]
  4003. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4004. return 1;
  4005. }
  4006. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4007. return 1;
  4008. i = MAX_CART;
  4009. if( itemdb_isstackable2(data) && !item_data->expire_time )
  4010. {
  4011. ARR_FIND( 0, MAX_CART, i,
  4012. sd->status.cart[i].nameid == item_data->nameid && sd->status.cart[i].bound == item_data->bound &&
  4013. sd->status.cart[i].card[0] == item_data->card[0] && sd->status.cart[i].card[1] == item_data->card[1] &&
  4014. sd->status.cart[i].card[2] == item_data->card[2] && sd->status.cart[i].card[3] == item_data->card[3] );
  4015. };
  4016. if( i < MAX_CART )
  4017. {// item already in cart, stack it
  4018. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4019. return 1; // no room
  4020. sd->status.cart[i].amount+=amount;
  4021. clif_cart_additem(sd,i,amount,0);
  4022. }
  4023. else
  4024. {// item not stackable or not present, add it
  4025. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4026. if( i == MAX_CART )
  4027. return 1; // no room
  4028. memcpy(&sd->status.cart[i],item_data,sizeof(sd->status.cart[0]));
  4029. sd->status.cart[i].amount=amount;
  4030. sd->cart_num++;
  4031. clif_cart_additem(sd,i,amount,0);
  4032. }
  4033. sd->status.cart[i].favorite = 0;/* clear */
  4034. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4035. sd->cart_weight += w;
  4036. clif_updatestatus(sd,SP_CARTINFO);
  4037. return 0;
  4038. }
  4039. /*==========================================
  4040. * Delete item on cart for given index.
  4041. * Return:
  4042. * 0 = success
  4043. * 1 = fail
  4044. *------------------------------------------*/
  4045. int pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4046. {
  4047. nullpo_retr(1, sd);
  4048. if(sd->status.cart[n].nameid==0 ||
  4049. sd->status.cart[n].amount<amount)
  4050. return 1;
  4051. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4052. sd->status.cart[n].amount -= amount;
  4053. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4054. if(sd->status.cart[n].amount <= 0){
  4055. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4056. sd->cart_num--;
  4057. }
  4058. if(!type) {
  4059. clif_cart_delitem(sd,n,amount);
  4060. clif_updatestatus(sd,SP_CARTINFO);
  4061. }
  4062. return 0;
  4063. }
  4064. /*==========================================
  4065. * Transfer item from inventory to cart.
  4066. * Return:
  4067. * 0 = fail
  4068. * 1 = succes
  4069. *------------------------------------------*/
  4070. int pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4071. {
  4072. struct item *item_data;
  4073. nullpo_ret(sd);
  4074. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4075. return 1;
  4076. item_data = &sd->status.inventory[idx];
  4077. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4078. return 1;
  4079. if( pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE) == 0 )
  4080. return pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4081. return 1;
  4082. }
  4083. /*==========================================
  4084. * Get number of item in cart.
  4085. * Return:
  4086. -1 = itemid not found or no amount found
  4087. x = remaining itemid on cart after get
  4088. *------------------------------------------*/
  4089. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4090. {
  4091. struct item* item_data;
  4092. nullpo_retr(-1, sd);
  4093. item_data = &sd->status.cart[idx];
  4094. if( item_data->nameid == 0 || item_data->amount == 0 )
  4095. return -1;
  4096. return item_data->amount - amount;
  4097. }
  4098. /*==========================================
  4099. * Retrieve an item at index idx from cart.
  4100. * Return:
  4101. * 0 = player not found or (FIXME) succes (from pc_cart_delitem)
  4102. * 1 = failure
  4103. *------------------------------------------*/
  4104. int pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4105. {
  4106. struct item *item_data;
  4107. int flag;
  4108. nullpo_ret(sd);
  4109. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4110. return 1;
  4111. item_data=&sd->status.cart[idx];
  4112. if(item_data->nameid==0 || amount < 1 || item_data->amount<amount || sd->state.vending )
  4113. return 1;
  4114. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4115. return pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4116. clif_additem(sd,0,0,flag);
  4117. return 1;
  4118. }
  4119. /*==========================================
  4120. * Bound Item Check
  4121. * Type:
  4122. * 1 Account Bound
  4123. * 2 Guild Bound
  4124. * 3 Party Bound
  4125. * 4 Character Bound
  4126. *------------------------------------------*/
  4127. int pc_bound_chk(TBL_PC *sd,int type,int *idxlist)
  4128. {
  4129. int i=0, j=0;
  4130. for(i=0;i<MAX_INVENTORY;i++){
  4131. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4132. idxlist[j] = i;
  4133. j++;
  4134. }
  4135. }
  4136. return j;
  4137. }
  4138. /*==========================================
  4139. * Display item stolen msg to player sd
  4140. *------------------------------------------*/
  4141. int pc_show_steal(struct block_list *bl,va_list ap)
  4142. {
  4143. struct map_session_data *sd;
  4144. int itemid;
  4145. struct item_data *item=NULL;
  4146. char output[100];
  4147. sd=va_arg(ap,struct map_session_data *);
  4148. itemid=va_arg(ap,int);
  4149. if((item=itemdb_exists(itemid))==NULL)
  4150. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4151. else
  4152. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4153. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4154. return 0;
  4155. }
  4156. /*==========================================
  4157. * Steal an item from bl (mob).
  4158. * Return:
  4159. * 0 = fail
  4160. * 1 = succes
  4161. *------------------------------------------*/
  4162. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4163. {
  4164. int i,itemid,flag;
  4165. double rate;
  4166. struct status_data *sd_status, *md_status;
  4167. struct mob_data *md;
  4168. struct item tmp_item;
  4169. if(!sd || !bl || bl->type!=BL_MOB)
  4170. return 0;
  4171. md = (TBL_MOB *)bl;
  4172. 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
  4173. return 0;
  4174. sd_status= status_get_status_data(&sd->bl);
  4175. md_status= status_get_status_data(bl);
  4176. if( md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
  4177. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4178. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4179. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4180. ) { //Can't steal from
  4181. md->state.steal_flag = UCHAR_MAX;
  4182. return 0;
  4183. }
  4184. // base skill success chance (percentual)
  4185. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4186. rate += sd->bonus.add_steal_rate;
  4187. if( rate < 1 )
  4188. return 0;
  4189. // Try dropping one item, in the order from first to last possible slot.
  4190. // Droprate is affected by the skill success rate.
  4191. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4192. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4193. break;
  4194. if( i == MAX_STEAL_DROP )
  4195. return 0;
  4196. itemid = md->db->dropitem[i].nameid;
  4197. memset(&tmp_item,0,sizeof(tmp_item));
  4198. tmp_item.nameid = itemid;
  4199. tmp_item.amount = 1;
  4200. tmp_item.identify = itemdb_isidentified(itemid);
  4201. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4202. //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?
  4203. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4204. if(flag) { //Failed to steal due to overweight
  4205. clif_additem(sd,0,0,flag);
  4206. return 0;
  4207. }
  4208. if(battle_config.show_steal_in_same_party)
  4209. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4210. //Logs items, Stolen from mobs [Lupus]
  4211. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4212. //A Rare Steal Global Announce by Lupus
  4213. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4214. struct item_data *i_data;
  4215. char message[128];
  4216. i_data = itemdb_search(itemid);
  4217. 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);
  4218. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4219. intif_broadcast(message,strlen(message)+1,0);
  4220. }
  4221. return 1;
  4222. }
  4223. /*==========================================
  4224. * Stole zeny from bl (mob)
  4225. * return
  4226. * 0 = fail
  4227. * 1 = success
  4228. *------------------------------------------*/
  4229. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4230. {
  4231. int rate,skill;
  4232. struct mob_data *md;
  4233. if(!sd || !target || target->type != BL_MOB)
  4234. return 0;
  4235. md = (TBL_MOB*)target;
  4236. if( md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS )
  4237. return 0;
  4238. if( mob_is_treasure(md) )
  4239. return 0;
  4240. // FIXME: This formula is either custom or outdated.
  4241. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4242. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4243. if(rnd()%1000 < rate)
  4244. {
  4245. int amount = md->level*10 + rnd()%100;
  4246. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4247. md->state.steal_coin_flag = 1;
  4248. return 1;
  4249. }
  4250. return 0;
  4251. }
  4252. /*==========================================
  4253. * Set's a player position.
  4254. * Return values:
  4255. * 0 - Success.
  4256. * 1 - Invalid map index.
  4257. * 2 - Map not in this map-server, and failed to locate alternate map-server.
  4258. *------------------------------------------*/
  4259. int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4260. {
  4261. int16 m;
  4262. nullpo_ret(sd);
  4263. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4264. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4265. return 1;
  4266. }
  4267. if( pc_isdead(sd) ) { //Revive dead people before warping them
  4268. pc_setstand(sd);
  4269. pc_setrestartvalue(sd,1);
  4270. }
  4271. m = map_mapindex2mapid(mapindex);
  4272. sd->state.changemap = (sd->mapindex != mapindex);
  4273. sd->state.warping = 1;
  4274. if(sd->status.party_id && map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4275. struct party_data *p;
  4276. if((p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4277. instance_delusers(p->instance_id);
  4278. }
  4279. if( sd->state.changemap ) { // Misc map-changing settings
  4280. int i;
  4281. sd->state.pmap = sd->bl.m;
  4282. if (sd->sc.count) { // Cancel some map related stuff.
  4283. if (sd->sc.data[SC_JAILED])
  4284. return 1; //You may not get out!
  4285. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4286. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4287. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4288. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4289. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4290. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4291. if (sd->sc.data[SC_KNOWLEDGE]) {
  4292. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4293. if (sce->timer != INVALID_TIMER)
  4294. delete_timer(sce->timer, status_change_timer);
  4295. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4296. }
  4297. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4298. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4299. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4300. }
  4301. for( i = 0; i < EQI_MAX; i++ ) {
  4302. if( sd->equip_index[ i ] >= 0 )
  4303. if( !pc_isequip( sd , sd->equip_index[ i ] ) )
  4304. pc_unequipitem( sd , sd->equip_index[ i ] , 2 );
  4305. }
  4306. if (battle_config.clear_unit_onwarp&BL_PC)
  4307. skill_clear_unitgroup(&sd->bl);
  4308. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4309. guild_send_dot_remove(sd);
  4310. bg_send_dot_remove(sd);
  4311. if (sd->regen.state.gc)
  4312. sd->regen.state.gc = 0;
  4313. // make sure vending is allowed here
  4314. if (sd->state.vending && map[m].flag.novending) {
  4315. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4316. vending_closevending(sd);
  4317. }
  4318. channel_pcquit(sd,4); //quit map chan
  4319. }
  4320. if( m < 0 )
  4321. {
  4322. uint32 ip;
  4323. uint16 port;
  4324. //if can't find any map-servers, just abort setting position.
  4325. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4326. return 2;
  4327. if (sd->npc_id)
  4328. npc_event_dequeue(sd);
  4329. npc_script_event(sd, NPCE_LOGOUT);
  4330. //remove from map, THEN change x/y coordinates
  4331. unit_remove_map_pc(sd,clrtype);
  4332. sd->mapindex = mapindex;
  4333. sd->bl.x=x;
  4334. sd->bl.y=y;
  4335. pc_clean_skilltree(sd);
  4336. chrif_save(sd,2);
  4337. chrif_changemapserver(sd, ip, (short)port);
  4338. //Free session data from this map server [Kevin]
  4339. unit_free_pc(sd);
  4340. return 0;
  4341. }
  4342. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4343. {
  4344. 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);
  4345. x = y = 0; // make it random
  4346. }
  4347. if( x == 0 && y == 0 )
  4348. {// pick a random walkable cell
  4349. do {
  4350. x=rnd()%(map[m].xs-2)+1;
  4351. y=rnd()%(map[m].ys-2)+1;
  4352. } while(map_getcell(m,x,y,CELL_CHKNOPASS));
  4353. }
  4354. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4355. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4356. vending_closevending(sd);
  4357. }
  4358. if(sd->bl.prev != NULL){
  4359. unit_remove_map_pc(sd,clrtype);
  4360. clif_changemap(sd,m,x,y); // [MouseJstr]
  4361. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4362. sd->state.rewarp = 1;
  4363. sd->mapindex = mapindex;
  4364. sd->bl.m = m;
  4365. sd->bl.x = sd->ud.to_x = x;
  4366. sd->bl.y = sd->ud.to_y = y;
  4367. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4368. { // Increased guild castle regen [Valaris]
  4369. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4370. if(gc && gc->guild_id == sd->status.guild_id)
  4371. sd->regen.state.gc = 1;
  4372. }
  4373. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4374. {
  4375. sd->pd->bl.m = m;
  4376. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4377. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4378. sd->pd->ud.dir = sd->ud.dir;
  4379. }
  4380. if( merc_is_hom_active(sd->hd) )
  4381. {
  4382. sd->hd->bl.m = m;
  4383. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4384. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4385. sd->hd->ud.dir = sd->ud.dir;
  4386. }
  4387. if( sd->md )
  4388. {
  4389. sd->md->bl.m = m;
  4390. sd->md->bl.x = sd->md->ud.to_x = x;
  4391. sd->md->bl.y = sd->md->ud.to_y = y;
  4392. sd->md->ud.dir = sd->ud.dir;
  4393. }
  4394. return 0;
  4395. }
  4396. /*==========================================
  4397. * Warp player sd to random location on current map.
  4398. * May fail if no walkable cell found (1000 attempts).
  4399. * Return:
  4400. * 0 = fail or FIXME success (from pc_setpos)
  4401. * x(1|2) = fail
  4402. *------------------------------------------*/
  4403. int pc_randomwarp(struct map_session_data *sd, clr_type type)
  4404. {
  4405. int x,y,i=0;
  4406. int16 m;
  4407. nullpo_ret(sd);
  4408. m=sd->bl.m;
  4409. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4410. return 0;
  4411. do{
  4412. x=rnd()%(map[m].xs-2)+1;
  4413. y=rnd()%(map[m].ys-2)+1;
  4414. }while(map_getcell(m,x,y,CELL_CHKNOPASS) && (i++)<1000 );
  4415. if (i < 1000)
  4416. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4417. return 0;
  4418. }
  4419. /*==========================================
  4420. * Records a memo point at sd's current position
  4421. * pos - entry to replace, (-1: shift oldest entry out)
  4422. *------------------------------------------*/
  4423. int pc_memo(struct map_session_data* sd, int pos)
  4424. {
  4425. int skill;
  4426. nullpo_ret(sd);
  4427. // check mapflags
  4428. 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) ) {
  4429. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4430. return 0;
  4431. }
  4432. // check inputs
  4433. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4434. return 0; // invalid input
  4435. // check required skill level
  4436. skill = pc_checkskill(sd, AL_WARP);
  4437. if( skill < 1 ) {
  4438. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4439. return 0;
  4440. }
  4441. if( skill < 2 || skill - 2 < pos ) {
  4442. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4443. return 0;
  4444. }
  4445. if( pos == -1 )
  4446. {
  4447. int i;
  4448. // prevent memo-ing the same map multiple times
  4449. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4450. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4451. pos = 0;
  4452. }
  4453. if( map[sd->bl.m].instance_id ) {
  4454. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  4455. return 0;
  4456. }
  4457. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4458. sd->status.memo_point[pos].x = sd->bl.x;
  4459. sd->status.memo_point[pos].y = sd->bl.y;
  4460. clif_skill_memomessage(sd, 0);
  4461. return 1;
  4462. }
  4463. //
  4464. // Skills
  4465. //
  4466. /*==========================================
  4467. * Return player sd skill_lv learned for given skill
  4468. *------------------------------------------*/
  4469. int pc_checkskill(struct map_session_data *sd,uint16 skill_id)
  4470. {
  4471. if(sd == NULL) return 0;
  4472. if( skill_id >= GD_SKILLBASE && skill_id < GD_MAX ) {
  4473. struct guild *g;
  4474. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4475. return guild_checkskill(g,skill_id);
  4476. return 0;
  4477. } else if(skill_id >= ARRAYLENGTH(sd->status.skill) ) {
  4478. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4479. return 0;
  4480. }
  4481. if(sd->status.skill[skill_id].id == skill_id)
  4482. return (sd->status.skill[skill_id].lv);
  4483. return 0;
  4484. }
  4485. /*==========================================
  4486. * Chk if we still have the correct weapon to continue the skill (actually status)
  4487. * If not ending it
  4488. * Return
  4489. * 0 - No status found or all done
  4490. *------------------------------------------*/
  4491. int pc_checkallowskill(struct map_session_data *sd)
  4492. {
  4493. const enum sc_type scw_list[] = {
  4494. SC_TWOHANDQUICKEN,
  4495. SC_ONEHAND,
  4496. SC_AURABLADE,
  4497. SC_PARRYING,
  4498. SC_SPEARQUICKEN,
  4499. SC_ADRENALINE,
  4500. SC_ADRENALINE2,
  4501. SC_DANCING,
  4502. SC_GATLINGFEVER,
  4503. SC_FEARBREEZE
  4504. };
  4505. const enum sc_type scs_list[] = {
  4506. SC_AUTOGUARD,
  4507. SC_DEFENDER,
  4508. SC_REFLECTSHIELD,
  4509. SC_REFLECTDAMAGE
  4510. };
  4511. int i;
  4512. nullpo_ret(sd);
  4513. if(!sd->sc.count)
  4514. return 0;
  4515. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4516. { // Skills requiring specific weapon types
  4517. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4518. continue;
  4519. if(sd->sc.data[scw_list[i]] &&
  4520. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4521. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4522. }
  4523. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4524. // Spurt requires bare hands (feet, in fact xD)
  4525. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4526. if(sd->status.shield <= 0) { // Skills requiring a shield
  4527. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  4528. if(sd->sc.data[scs_list[i]])
  4529. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  4530. }
  4531. return 0;
  4532. }
  4533. /*==========================================
  4534. * Return equiped itemid? on player sd at pos
  4535. * Return
  4536. * -1 : mean nothing equiped
  4537. * idx : (this index could be used in inventory to found item_data)
  4538. *------------------------------------------*/
  4539. int pc_checkequip(struct map_session_data *sd,int pos)
  4540. {
  4541. int i;
  4542. nullpo_retr(-1, sd);
  4543. for(i=0;i<EQI_MAX;i++){
  4544. if(pos & equip_pos[i])
  4545. return sd->equip_index[i];
  4546. }
  4547. return -1;
  4548. }
  4549. /*==========================================
  4550. * Convert's from the client's lame Job ID system
  4551. * to the map server's 'makes sense' system. [Skotlex]
  4552. *------------------------------------------*/
  4553. int pc_jobid2mapid(unsigned short b_class)
  4554. {
  4555. switch(b_class)
  4556. {
  4557. //Novice And 1-1 Jobs
  4558. case JOB_NOVICE: return MAPID_NOVICE;
  4559. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  4560. case JOB_MAGE: return MAPID_MAGE;
  4561. case JOB_ARCHER: return MAPID_ARCHER;
  4562. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  4563. case JOB_MERCHANT: return MAPID_MERCHANT;
  4564. case JOB_THIEF: return MAPID_THIEF;
  4565. case JOB_TAEKWON: return MAPID_TAEKWON;
  4566. case JOB_WEDDING: return MAPID_WEDDING;
  4567. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  4568. case JOB_NINJA: return MAPID_NINJA;
  4569. case JOB_XMAS: return MAPID_XMAS;
  4570. case JOB_SUMMER: return MAPID_SUMMER;
  4571. case JOB_HANBOK: return MAPID_HANBOK;
  4572. case JOB_GANGSI: return MAPID_GANGSI;
  4573. //2-1 Jobs
  4574. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  4575. case JOB_KNIGHT: return MAPID_KNIGHT;
  4576. case JOB_WIZARD: return MAPID_WIZARD;
  4577. case JOB_HUNTER: return MAPID_HUNTER;
  4578. case JOB_PRIEST: return MAPID_PRIEST;
  4579. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  4580. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  4581. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  4582. case JOB_KAGEROU:
  4583. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  4584. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  4585. //2-2 Jobs
  4586. case JOB_CRUSADER: return MAPID_CRUSADER;
  4587. case JOB_SAGE: return MAPID_SAGE;
  4588. case JOB_BARD:
  4589. case JOB_DANCER: return MAPID_BARDDANCER;
  4590. case JOB_MONK: return MAPID_MONK;
  4591. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  4592. case JOB_ROGUE: return MAPID_ROGUE;
  4593. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  4594. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  4595. //Trans Novice And Trans 1-1 Jobs
  4596. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  4597. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  4598. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  4599. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  4600. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  4601. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  4602. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  4603. //Trans 2-1 Jobs
  4604. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  4605. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  4606. case JOB_SNIPER: return MAPID_SNIPER;
  4607. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  4608. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  4609. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  4610. //Trans 2-2 Jobs
  4611. case JOB_PALADIN: return MAPID_PALADIN;
  4612. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  4613. case JOB_CLOWN:
  4614. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  4615. case JOB_CHAMPION: return MAPID_CHAMPION;
  4616. case JOB_CREATOR: return MAPID_CREATOR;
  4617. case JOB_STALKER: return MAPID_STALKER;
  4618. //Baby Novice And Baby 1-1 Jobs
  4619. case JOB_BABY: return MAPID_BABY;
  4620. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  4621. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  4622. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  4623. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  4624. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  4625. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  4626. //Baby 2-1 Jobs
  4627. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  4628. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  4629. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  4630. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  4631. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  4632. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  4633. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  4634. //Baby 2-2 Jobs
  4635. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  4636. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  4637. case JOB_BABY_BARD:
  4638. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  4639. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  4640. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  4641. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  4642. //3-1 Jobs
  4643. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  4644. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  4645. case JOB_WARLOCK: return MAPID_WARLOCK;
  4646. case JOB_RANGER: return MAPID_RANGER;
  4647. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  4648. case JOB_MECHANIC: return MAPID_MECHANIC;
  4649. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  4650. //3-2 Jobs
  4651. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  4652. case JOB_SORCERER: return MAPID_SORCERER;
  4653. case JOB_MINSTREL:
  4654. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  4655. case JOB_SURA: return MAPID_SURA;
  4656. case JOB_GENETIC: return MAPID_GENETIC;
  4657. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  4658. //Trans 3-1 Jobs
  4659. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  4660. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  4661. case JOB_RANGER_T: return MAPID_RANGER_T;
  4662. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  4663. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  4664. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  4665. //Trans 3-2 Jobs
  4666. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  4667. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  4668. case JOB_MINSTREL_T:
  4669. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  4670. case JOB_SURA_T: return MAPID_SURA_T;
  4671. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  4672. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  4673. //Baby 3-1 Jobs
  4674. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  4675. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  4676. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  4677. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  4678. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  4679. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  4680. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  4681. //Baby 3-2 Jobs
  4682. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  4683. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  4684. case JOB_BABY_MINSTREL:
  4685. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  4686. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  4687. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  4688. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  4689. default:
  4690. return -1;
  4691. }
  4692. }
  4693. //Reverts the map-style class id to the client-style one.
  4694. int pc_mapid2jobid(unsigned short class_, int sex)
  4695. {
  4696. switch(class_)
  4697. {
  4698. //Novice And 1-1 Jobs
  4699. case MAPID_NOVICE: return JOB_NOVICE;
  4700. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  4701. case MAPID_MAGE: return JOB_MAGE;
  4702. case MAPID_ARCHER: return JOB_ARCHER;
  4703. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  4704. case MAPID_MERCHANT: return JOB_MERCHANT;
  4705. case MAPID_THIEF: return JOB_THIEF;
  4706. case MAPID_TAEKWON: return JOB_TAEKWON;
  4707. case MAPID_WEDDING: return JOB_WEDDING;
  4708. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  4709. case MAPID_NINJA: return JOB_NINJA;
  4710. case MAPID_XMAS: return JOB_XMAS;
  4711. case MAPID_SUMMER: return JOB_SUMMER;
  4712. case MAPID_HANBOK: return JOB_HANBOK;
  4713. case MAPID_GANGSI: return JOB_GANGSI;
  4714. //2-1 Jobs
  4715. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  4716. case MAPID_KNIGHT: return JOB_KNIGHT;
  4717. case MAPID_WIZARD: return JOB_WIZARD;
  4718. case MAPID_HUNTER: return JOB_HUNTER;
  4719. case MAPID_PRIEST: return JOB_PRIEST;
  4720. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  4721. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  4722. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  4723. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  4724. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  4725. //2-2 Jobs
  4726. case MAPID_CRUSADER: return JOB_CRUSADER;
  4727. case MAPID_SAGE: return JOB_SAGE;
  4728. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  4729. case MAPID_MONK: return JOB_MONK;
  4730. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  4731. case MAPID_ROGUE: return JOB_ROGUE;
  4732. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  4733. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  4734. //Trans Novice And Trans 2-1 Jobs
  4735. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  4736. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  4737. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  4738. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  4739. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  4740. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  4741. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  4742. //Trans 2-1 Jobs
  4743. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  4744. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  4745. case MAPID_SNIPER: return JOB_SNIPER;
  4746. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  4747. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  4748. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  4749. //Trans 2-2 Jobs
  4750. case MAPID_PALADIN: return JOB_PALADIN;
  4751. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  4752. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  4753. case MAPID_CHAMPION: return JOB_CHAMPION;
  4754. case MAPID_CREATOR: return JOB_CREATOR;
  4755. case MAPID_STALKER: return JOB_STALKER;
  4756. //Baby Novice And Baby 1-1 Jobs
  4757. case MAPID_BABY: return JOB_BABY;
  4758. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  4759. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  4760. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  4761. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  4762. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  4763. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  4764. //Baby 2-1 Jobs
  4765. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  4766. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  4767. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  4768. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  4769. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  4770. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  4771. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  4772. //Baby 2-2 Jobs
  4773. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  4774. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  4775. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  4776. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  4777. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  4778. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  4779. //3-1 Jobs
  4780. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  4781. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  4782. case MAPID_WARLOCK: return JOB_WARLOCK;
  4783. case MAPID_RANGER: return JOB_RANGER;
  4784. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  4785. case MAPID_MECHANIC: return JOB_MECHANIC;
  4786. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  4787. //3-2 Jobs
  4788. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  4789. case MAPID_SORCERER: return JOB_SORCERER;
  4790. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  4791. case MAPID_SURA: return JOB_SURA;
  4792. case MAPID_GENETIC: return JOB_GENETIC;
  4793. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  4794. //Trans 3-1 Jobs
  4795. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  4796. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  4797. case MAPID_RANGER_T: return JOB_RANGER_T;
  4798. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  4799. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  4800. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  4801. //Trans 3-2 Jobs
  4802. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  4803. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  4804. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  4805. case MAPID_SURA_T: return JOB_SURA_T;
  4806. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  4807. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  4808. //Baby 3-1 Jobs
  4809. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  4810. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  4811. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  4812. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  4813. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  4814. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  4815. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  4816. //Baby 3-2 Jobs
  4817. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  4818. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  4819. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  4820. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  4821. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  4822. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  4823. default:
  4824. return -1;
  4825. }
  4826. }
  4827. /*====================================================
  4828. * This function return the name of the job (by [Yor])
  4829. *----------------------------------------------------*/
  4830. const char* job_name(int class_)
  4831. {
  4832. switch (class_) {
  4833. case JOB_NOVICE:
  4834. case JOB_SWORDMAN:
  4835. case JOB_MAGE:
  4836. case JOB_ARCHER:
  4837. case JOB_ACOLYTE:
  4838. case JOB_MERCHANT:
  4839. case JOB_THIEF:
  4840. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  4841. case JOB_KNIGHT:
  4842. case JOB_PRIEST:
  4843. case JOB_WIZARD:
  4844. case JOB_BLACKSMITH:
  4845. case JOB_HUNTER:
  4846. case JOB_ASSASSIN:
  4847. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  4848. case JOB_KNIGHT2:
  4849. return msg_txt(NULL,557);
  4850. case JOB_CRUSADER:
  4851. case JOB_MONK:
  4852. case JOB_SAGE:
  4853. case JOB_ROGUE:
  4854. case JOB_ALCHEMIST:
  4855. case JOB_BARD:
  4856. case JOB_DANCER:
  4857. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  4858. case JOB_CRUSADER2:
  4859. return msg_txt(NULL,563);
  4860. case JOB_WEDDING:
  4861. case JOB_SUPER_NOVICE:
  4862. case JOB_GUNSLINGER:
  4863. case JOB_NINJA:
  4864. case JOB_XMAS:
  4865. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  4866. case JOB_SUMMER:
  4867. return msg_txt(NULL,621);
  4868. case JOB_HANBOK:
  4869. return msg_txt(NULL,694);
  4870. case JOB_NOVICE_HIGH:
  4871. case JOB_SWORDMAN_HIGH:
  4872. case JOB_MAGE_HIGH:
  4873. case JOB_ARCHER_HIGH:
  4874. case JOB_ACOLYTE_HIGH:
  4875. case JOB_MERCHANT_HIGH:
  4876. case JOB_THIEF_HIGH:
  4877. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  4878. case JOB_LORD_KNIGHT:
  4879. case JOB_HIGH_PRIEST:
  4880. case JOB_HIGH_WIZARD:
  4881. case JOB_WHITESMITH:
  4882. case JOB_SNIPER:
  4883. case JOB_ASSASSIN_CROSS:
  4884. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  4885. case JOB_LORD_KNIGHT2:
  4886. return msg_txt(NULL,582);
  4887. case JOB_PALADIN:
  4888. case JOB_CHAMPION:
  4889. case JOB_PROFESSOR:
  4890. case JOB_STALKER:
  4891. case JOB_CREATOR:
  4892. case JOB_CLOWN:
  4893. case JOB_GYPSY:
  4894. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  4895. case JOB_PALADIN2:
  4896. return msg_txt(NULL,588);
  4897. case JOB_BABY:
  4898. case JOB_BABY_SWORDMAN:
  4899. case JOB_BABY_MAGE:
  4900. case JOB_BABY_ARCHER:
  4901. case JOB_BABY_ACOLYTE:
  4902. case JOB_BABY_MERCHANT:
  4903. case JOB_BABY_THIEF:
  4904. return msg_txt(NULL,595 - JOB_BABY + class_);
  4905. case JOB_BABY_KNIGHT:
  4906. case JOB_BABY_PRIEST:
  4907. case JOB_BABY_WIZARD:
  4908. case JOB_BABY_BLACKSMITH:
  4909. case JOB_BABY_HUNTER:
  4910. case JOB_BABY_ASSASSIN:
  4911. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  4912. case JOB_BABY_KNIGHT2:
  4913. return msg_txt(NULL,602);
  4914. case JOB_BABY_CRUSADER:
  4915. case JOB_BABY_MONK:
  4916. case JOB_BABY_SAGE:
  4917. case JOB_BABY_ROGUE:
  4918. case JOB_BABY_ALCHEMIST:
  4919. case JOB_BABY_BARD:
  4920. case JOB_BABY_DANCER:
  4921. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  4922. case JOB_BABY_CRUSADER2:
  4923. return msg_txt(NULL,608);
  4924. case JOB_SUPER_BABY:
  4925. return msg_txt(NULL,615);
  4926. case JOB_TAEKWON:
  4927. return msg_txt(NULL,616);
  4928. case JOB_STAR_GLADIATOR:
  4929. case JOB_STAR_GLADIATOR2:
  4930. return msg_txt(NULL,617);
  4931. case JOB_SOUL_LINKER:
  4932. return msg_txt(NULL,618);
  4933. case JOB_GANGSI:
  4934. case JOB_DEATH_KNIGHT:
  4935. case JOB_DARK_COLLECTOR:
  4936. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  4937. case JOB_RUNE_KNIGHT:
  4938. case JOB_WARLOCK:
  4939. case JOB_RANGER:
  4940. case JOB_ARCH_BISHOP:
  4941. case JOB_MECHANIC:
  4942. case JOB_GUILLOTINE_CROSS:
  4943. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  4944. case JOB_RUNE_KNIGHT_T:
  4945. case JOB_WARLOCK_T:
  4946. case JOB_RANGER_T:
  4947. case JOB_ARCH_BISHOP_T:
  4948. case JOB_MECHANIC_T:
  4949. case JOB_GUILLOTINE_CROSS_T:
  4950. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  4951. case JOB_ROYAL_GUARD:
  4952. case JOB_SORCERER:
  4953. case JOB_MINSTREL:
  4954. case JOB_WANDERER:
  4955. case JOB_SURA:
  4956. case JOB_GENETIC:
  4957. case JOB_SHADOW_CHASER:
  4958. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  4959. case JOB_ROYAL_GUARD_T:
  4960. case JOB_SORCERER_T:
  4961. case JOB_MINSTREL_T:
  4962. case JOB_WANDERER_T:
  4963. case JOB_SURA_T:
  4964. case JOB_GENETIC_T:
  4965. case JOB_SHADOW_CHASER_T:
  4966. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  4967. case JOB_RUNE_KNIGHT2:
  4968. case JOB_RUNE_KNIGHT_T2:
  4969. return msg_txt(NULL,625);
  4970. case JOB_ROYAL_GUARD2:
  4971. case JOB_ROYAL_GUARD_T2:
  4972. return msg_txt(NULL,631);
  4973. case JOB_RANGER2:
  4974. case JOB_RANGER_T2:
  4975. return msg_txt(NULL,627);
  4976. case JOB_MECHANIC2:
  4977. case JOB_MECHANIC_T2:
  4978. return msg_txt(NULL,629);
  4979. case JOB_BABY_RUNE:
  4980. case JOB_BABY_WARLOCK:
  4981. case JOB_BABY_RANGER:
  4982. case JOB_BABY_BISHOP:
  4983. case JOB_BABY_MECHANIC:
  4984. case JOB_BABY_CROSS:
  4985. case JOB_BABY_GUARD:
  4986. case JOB_BABY_SORCERER:
  4987. case JOB_BABY_MINSTREL:
  4988. case JOB_BABY_WANDERER:
  4989. case JOB_BABY_SURA:
  4990. case JOB_BABY_GENETIC:
  4991. case JOB_BABY_CHASER:
  4992. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  4993. case JOB_BABY_RUNE2:
  4994. return msg_txt(NULL,638);
  4995. case JOB_BABY_GUARD2:
  4996. return msg_txt(NULL,644);
  4997. case JOB_BABY_RANGER2:
  4998. return msg_txt(NULL,640);
  4999. case JOB_BABY_MECHANIC2:
  5000. return msg_txt(NULL,642);
  5001. case JOB_SUPER_NOVICE_E:
  5002. case JOB_SUPER_BABY_E:
  5003. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5004. case JOB_KAGEROU:
  5005. case JOB_OBORO:
  5006. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5007. default:
  5008. return msg_txt(NULL,655);
  5009. }
  5010. }
  5011. /*====================================================
  5012. * Timered function to make id follow a target.
  5013. * @id = bl.id (player only atm)
  5014. * target is define in sd->followtarget (bl.id)
  5015. * used by pc_follow
  5016. *----------------------------------------------------*/
  5017. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5018. {
  5019. struct map_session_data *sd;
  5020. struct block_list *tbl;
  5021. sd = map_id2sd(id);
  5022. nullpo_ret(sd);
  5023. if (sd->followtimer != tid){
  5024. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5025. sd->followtimer = INVALID_TIMER;
  5026. return 0;
  5027. }
  5028. sd->followtimer = INVALID_TIMER;
  5029. tbl = map_id2bl(sd->followtarget);
  5030. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5031. {
  5032. pc_stop_following(sd);
  5033. return 0;
  5034. }
  5035. // either player or target is currently detached from map blocks (could be teleporting),
  5036. // but still connected to this map, so we'll just increment the timer and check back later
  5037. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5038. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5039. {
  5040. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5041. if (!check_distance_bl(&sd->bl, tbl, 5))
  5042. unit_walktobl(&sd->bl, tbl, 5, 0);
  5043. } else
  5044. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5045. }
  5046. sd->followtimer = add_timer(
  5047. tick + 1000, // increase time a bit to loosen up map's load
  5048. pc_follow_timer, sd->bl.id, 0);
  5049. return 0;
  5050. }
  5051. int pc_stop_following (struct map_session_data *sd)
  5052. {
  5053. nullpo_ret(sd);
  5054. if (sd->followtimer != INVALID_TIMER) {
  5055. delete_timer(sd->followtimer,pc_follow_timer);
  5056. sd->followtimer = INVALID_TIMER;
  5057. }
  5058. sd->followtarget = -1;
  5059. sd->ud.target_to = 0;
  5060. unit_stop_walking(&sd->bl, 1);
  5061. return 0;
  5062. }
  5063. int pc_follow(struct map_session_data *sd,int target_id)
  5064. {
  5065. struct block_list *bl = map_id2bl(target_id);
  5066. if (bl == NULL /*|| bl->type != BL_PC*/)
  5067. return 1;
  5068. if (sd->followtimer != INVALID_TIMER)
  5069. pc_stop_following(sd);
  5070. sd->followtarget = target_id;
  5071. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5072. return 0;
  5073. }
  5074. int pc_checkbaselevelup(struct map_session_data *sd) {
  5075. unsigned int next = pc_nextbaseexp(sd);
  5076. if (!next || sd->status.base_exp < next)
  5077. return 0;
  5078. do {
  5079. sd->status.base_exp -= next;
  5080. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5081. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5082. sd->status.base_exp = next-1;
  5083. next = pc_gets_status_point(sd->status.base_level);
  5084. sd->status.base_level ++;
  5085. sd->status.status_point += next;
  5086. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5087. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5088. status_calc_pet(sd->pd,0);
  5089. clif_updatestatus(sd,SP_STATUSPOINT);
  5090. clif_updatestatus(sd,SP_BASELEVEL);
  5091. clif_updatestatus(sd,SP_BASEEXP);
  5092. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5093. status_calc_pc(sd,0);
  5094. status_percent_heal(&sd->bl,100,100);
  5095. if((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5096. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5097. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5098. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5099. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5100. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5101. if (sd->state.snovice_dead_flag)
  5102. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5103. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5104. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5105. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5106. }
  5107. clif_misceffect(&sd->bl,0);
  5108. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5109. if(sd->status.party_id)
  5110. party_send_levelup(sd);
  5111. pc_baselevelchanged(sd);
  5112. return 1;
  5113. }
  5114. void pc_baselevelchanged(struct map_session_data *sd) {
  5115. #ifdef RENEWAL
  5116. int i;
  5117. for( i = 0; i < EQI_MAX; i++ ) {
  5118. if( sd->equip_index[i] >= 0 ) {
  5119. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5120. pc_unequipitem(sd, sd->equip_index[i], 3);
  5121. }
  5122. }
  5123. #endif
  5124. }
  5125. int pc_checkjoblevelup(struct map_session_data *sd)
  5126. {
  5127. unsigned int next = pc_nextjobexp(sd);
  5128. nullpo_ret(sd);
  5129. if(!next || sd->status.job_exp < next)
  5130. return 0;
  5131. do {
  5132. sd->status.job_exp -= next;
  5133. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5134. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5135. sd->status.job_exp = next-1;
  5136. sd->status.job_level ++;
  5137. sd->status.skill_point ++;
  5138. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5139. clif_updatestatus(sd,SP_JOBLEVEL);
  5140. clif_updatestatus(sd,SP_JOBEXP);
  5141. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5142. clif_updatestatus(sd,SP_SKILLPOINT);
  5143. status_calc_pc(sd,0);
  5144. clif_misceffect(&sd->bl,1);
  5145. if (pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd))
  5146. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5147. npc_script_event(sd, NPCE_JOBLVUP);
  5148. return 1;
  5149. }
  5150. /*==========================================
  5151. * Alters experienced based on self bonuses that do not get even shared to the party.
  5152. *------------------------------------------*/
  5153. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5154. {
  5155. int bonus = 0;
  5156. struct status_data *status = status_get_status_data(src);
  5157. if (sd->expaddrace[status->race])
  5158. bonus += sd->expaddrace[status->race];
  5159. bonus += sd->expaddrace[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  5160. if (battle_config.pk_mode &&
  5161. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5162. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5163. if (sd->sc.data[SC_EXPBOOST])
  5164. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5165. *base_exp = (unsigned int) cap_value(*base_exp + (double)*base_exp * bonus/100., 1, UINT_MAX);
  5166. if (sd->sc.data[SC_JEXPBOOST])
  5167. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5168. *job_exp = (unsigned int) cap_value(*job_exp + (double)*job_exp * bonus/100., 1, UINT_MAX);
  5169. return;
  5170. }
  5171. /*==========================================
  5172. * Give x exp at sd player and calculate remaining exp for next lvl
  5173. *------------------------------------------*/
  5174. int pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp,unsigned int job_exp,bool quest)
  5175. {
  5176. float nextbp=0, nextjp=0;
  5177. unsigned int nextb=0, nextj=0;
  5178. nullpo_ret(sd);
  5179. if(sd->bl.prev == NULL || pc_isdead(sd))
  5180. return 0;
  5181. if(!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5182. return 0; // no exp on pvp maps
  5183. if(sd->status.guild_id>0)
  5184. base_exp-=guild_payexp(sd,base_exp);
  5185. if(src) pc_calcexp(sd, &base_exp, &job_exp, src);
  5186. nextb = pc_nextbaseexp(sd);
  5187. nextj = pc_nextjobexp(sd);
  5188. if(sd->state.showexp || battle_config.max_exp_gain_rate){
  5189. if (nextb > 0)
  5190. nextbp = (float) base_exp / (float) nextb;
  5191. if (nextj > 0)
  5192. nextjp = (float) job_exp / (float) nextj;
  5193. if(battle_config.max_exp_gain_rate) {
  5194. if (nextbp > battle_config.max_exp_gain_rate/1000.) {
  5195. //Note that this value should never be greater than the original
  5196. //base_exp, therefore no overflow checks are needed. [Skotlex]
  5197. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5198. if (sd->state.showexp)
  5199. nextbp = (float) base_exp / (float) nextb;
  5200. }
  5201. if (nextjp > battle_config.max_exp_gain_rate/1000.) {
  5202. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5203. if (sd->state.showexp)
  5204. nextjp = (float) job_exp / (float) nextj;
  5205. }
  5206. }
  5207. }
  5208. //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]
  5209. if (base_exp) {
  5210. nextb = nextb?UINT_MAX:pc_thisbaseexp(sd);
  5211. if(sd->status.base_exp > nextb - base_exp)
  5212. sd->status.base_exp = nextb;
  5213. else
  5214. sd->status.base_exp += base_exp;
  5215. pc_checkbaselevelup(sd);
  5216. clif_updatestatus(sd,SP_BASEEXP);
  5217. }
  5218. if (job_exp) {
  5219. nextj = nextj?UINT_MAX:pc_thisjobexp(sd);
  5220. if(sd->status.job_exp > nextj - job_exp)
  5221. sd->status.job_exp = nextj;
  5222. else
  5223. sd->status.job_exp += job_exp;
  5224. pc_checkjoblevelup(sd);
  5225. clif_updatestatus(sd,SP_JOBEXP);
  5226. }
  5227. if(base_exp)
  5228. clif_displayexp(sd, base_exp, SP_BASEEXP, quest);
  5229. if(job_exp)
  5230. clif_displayexp(sd, job_exp, SP_JOBEXP, quest);
  5231. if(sd->state.showexp) {
  5232. char output[256];
  5233. sprintf(output,
  5234. "Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
  5235. clif_disp_onlyself(sd,output,strlen(output));
  5236. }
  5237. return 1;
  5238. }
  5239. /*==========================================
  5240. * Returns max level for this character.
  5241. *------------------------------------------*/
  5242. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5243. return job_info[pc_class2idx(sd->status.class_)].max_level[0];
  5244. }
  5245. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5246. return job_info[pc_class2idx(sd->status.class_)].max_level[1];
  5247. }
  5248. /*==========================================
  5249. * base level exp lookup.
  5250. *------------------------------------------*/
  5251. //Base exp needed for next level.
  5252. unsigned int pc_nextbaseexp(struct map_session_data *sd)
  5253. {
  5254. nullpo_ret(sd);
  5255. if(sd->status.base_level>=pc_maxbaselv(sd) || sd->status.base_level<=0)
  5256. return 0;
  5257. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  5258. }
  5259. //Base exp needed for this level.
  5260. unsigned int pc_thisbaseexp(struct map_session_data *sd)
  5261. {
  5262. if(sd->status.base_level>pc_maxbaselv(sd) || sd->status.base_level<=1)
  5263. return 0;
  5264. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-2];
  5265. }
  5266. /*==========================================
  5267. * job level exp lookup
  5268. * Return:
  5269. * 0 = not found
  5270. * x = exp for level
  5271. *------------------------------------------*/
  5272. //Job exp needed for next level.
  5273. unsigned int pc_nextjobexp(struct map_session_data *sd)
  5274. {
  5275. nullpo_ret(sd);
  5276. if(sd->status.job_level>=pc_maxjoblv(sd) || sd->status.job_level<=0)
  5277. return 0;
  5278. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  5279. }
  5280. //Job exp needed for this level.
  5281. unsigned int pc_thisjobexp(struct map_session_data *sd)
  5282. {
  5283. if(sd->status.job_level>pc_maxjoblv(sd) || sd->status.job_level<=1)
  5284. return 0;
  5285. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-2];
  5286. }
  5287. /// Returns the value of the specified stat.
  5288. static int pc_getstat(struct map_session_data* sd, int type)
  5289. {
  5290. nullpo_retr(-1, sd);
  5291. switch( type ) {
  5292. case SP_STR: return sd->status.str;
  5293. case SP_AGI: return sd->status.agi;
  5294. case SP_VIT: return sd->status.vit;
  5295. case SP_INT: return sd->status.int_;
  5296. case SP_DEX: return sd->status.dex;
  5297. case SP_LUK: return sd->status.luk;
  5298. default:
  5299. return -1;
  5300. }
  5301. }
  5302. /// Sets the specified stat to the specified value.
  5303. /// Returns the new value.
  5304. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5305. {
  5306. nullpo_retr(-1, sd);
  5307. switch( type ) {
  5308. case SP_STR: sd->status.str = val; break;
  5309. case SP_AGI: sd->status.agi = val; break;
  5310. case SP_VIT: sd->status.vit = val; break;
  5311. case SP_INT: sd->status.int_ = val; break;
  5312. case SP_DEX: sd->status.dex = val; break;
  5313. case SP_LUK: sd->status.luk = val; break;
  5314. default:
  5315. return -1;
  5316. }
  5317. return val;
  5318. }
  5319. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5320. int pc_gets_status_point(int level)
  5321. {
  5322. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5323. return (statp[level+1] - statp[level]);
  5324. else //Default increase
  5325. return ((level+15) / 5);
  5326. }
  5327. /// Returns the number of stat points needed to change the specified stat by val.
  5328. /// If val is negative, returns the number of stat points that would be needed to
  5329. /// raise the specified stat from (current value - val) to current value.
  5330. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5331. {
  5332. int low, high, sp = 0;
  5333. if ( val == 0 )
  5334. return 0;
  5335. low = pc_getstat(sd,type);
  5336. if ( low >= pc_maxparameter(sd) && val > 0 )
  5337. return 0; // Official servers show '0' when max is reached
  5338. high = low + val;
  5339. if ( val < 0 )
  5340. swap(low, high);
  5341. for ( ; low < high; low++ )
  5342. #ifdef RENEWAL // renewal status point cost formula
  5343. sp += (low < 100) ? (2 + (low - 1) / 10) : (16 + 4 * ((low - 100) / 5));
  5344. #else
  5345. sp += ( 1 + (low + 9) / 10 );
  5346. #endif
  5347. return sp;
  5348. }
  5349. /// Raises a stat by 1.
  5350. /// Obeys max_parameter limits.
  5351. /// Subtracts stat points.
  5352. ///
  5353. /// @param type The stat to change (see enum _sp)
  5354. int pc_statusup(struct map_session_data* sd, int type)
  5355. {
  5356. int max, need, val;
  5357. nullpo_ret(sd);
  5358. // check conditions
  5359. need = pc_need_status_point(sd,type,1);
  5360. if( type < SP_STR || type > SP_LUK || need < 0 || need > sd->status.status_point )
  5361. {
  5362. clif_statusupack(sd,type,0,0);
  5363. return 1;
  5364. }
  5365. // check limits
  5366. max = pc_maxparameter(sd);
  5367. if( pc_getstat(sd,type) >= max )
  5368. {
  5369. clif_statusupack(sd,type,0,0);
  5370. return 1;
  5371. }
  5372. // set new values
  5373. val = pc_setstat(sd, type, pc_getstat(sd,type) + 1);
  5374. sd->status.status_point -= need;
  5375. status_calc_pc(sd,0);
  5376. // update increase cost indicator
  5377. if( need != pc_need_status_point(sd,type,1) )
  5378. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5379. // update statpoint count
  5380. clif_updatestatus(sd,SP_STATUSPOINT);
  5381. // update stat value
  5382. clif_statusupack(sd,type,1,val); // required
  5383. if( val > 255 )
  5384. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5385. return 0;
  5386. }
  5387. /// Raises a stat by the specified amount.
  5388. /// Obeys max_parameter limits.
  5389. /// Does not subtract stat points.
  5390. ///
  5391. /// @param type The stat to change (see enum _sp)
  5392. /// @param val The stat increase amount.
  5393. int pc_statusup2(struct map_session_data* sd, int type, int val)
  5394. {
  5395. int max, need;
  5396. nullpo_ret(sd);
  5397. if( type < SP_STR || type > SP_LUK )
  5398. {
  5399. clif_statusupack(sd,type,0,0);
  5400. return 1;
  5401. }
  5402. need = pc_need_status_point(sd,type,1);
  5403. // set new value
  5404. max = pc_maxparameter(sd);
  5405. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  5406. status_calc_pc(sd,0);
  5407. // update increase cost indicator
  5408. if( need != pc_need_status_point(sd,type,1) )
  5409. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5410. // update stat value
  5411. clif_statusupack(sd,type,1,val); // required
  5412. if( val > 255 )
  5413. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5414. return 0;
  5415. }
  5416. /*==========================================
  5417. * Update skill_lv for player sd
  5418. * Skill point allocation
  5419. *------------------------------------------*/
  5420. int pc_skillup(struct map_session_data *sd,uint16 skill_id)
  5421. {
  5422. nullpo_ret(sd);
  5423. if( skill_id >= GD_SKILLBASE && skill_id < GD_SKILLBASE+MAX_GUILDSKILL )
  5424. {
  5425. guild_skillup(sd, skill_id);
  5426. return 0;
  5427. }
  5428. if( skill_id >= HM_SKILLBASE && skill_id < HM_SKILLBASE+MAX_HOMUNSKILL && sd->hd )
  5429. {
  5430. merc_hom_skillup(sd->hd, skill_id);
  5431. return 0;
  5432. }
  5433. if(skill_id >= MAX_SKILL )
  5434. return 0;
  5435. if( sd->status.skill_point > 0 &&
  5436. sd->status.skill[skill_id].id &&
  5437. sd->status.skill[skill_id].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  5438. sd->status.skill[skill_id].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  5439. {
  5440. int lv,range, upgradable;
  5441. sd->status.skill[skill_id].lv++;
  5442. sd->status.skill_point--;
  5443. if( !skill_get_inf(skill_id) )
  5444. status_calc_pc(sd,0); // Only recalculate for passive skills.
  5445. 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) )
  5446. pc_calc_skilltree(sd); // Required to grant all TK Ranger skills.
  5447. else
  5448. pc_check_skilltree(sd, skill_id); // Check if a new skill can Lvlup
  5449. lv = sd->status.skill[skill_id].lv;
  5450. range = skill_get_range2(&sd->bl, skill_id, lv);
  5451. upgradable = (lv < skill_tree_get_max(sd->status.skill[skill_id].id, sd->status.class_)) ? 1 : 0;
  5452. clif_skillup(sd,skill_id,lv,range,upgradable);
  5453. clif_updatestatus(sd,SP_SKILLPOINT);
  5454. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  5455. clif_updatestatus(sd,SP_CARTINFO);
  5456. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  5457. clif_skillinfoblock(sd);
  5458. }
  5459. return 0;
  5460. }
  5461. /*==========================================
  5462. * /allskill
  5463. *------------------------------------------*/
  5464. int pc_allskillup(struct map_session_data *sd)
  5465. {
  5466. int i,id;
  5467. nullpo_ret(sd);
  5468. for(i=0;i<MAX_SKILL;i++){
  5469. 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) {
  5470. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  5471. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5472. if (sd->status.skill[i].lv == 0)
  5473. sd->status.skill[i].id = 0;
  5474. }
  5475. }
  5476. if (pc_has_permission(sd, PC_PERM_ALL_SKILL))
  5477. { //Get ALL skills except npc/guild ones. [Skotlex]
  5478. //and except SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  5479. for(i=0;i<MAX_SKILL;i++){
  5480. switch( i ) {
  5481. case SG_DEVIL:
  5482. case MO_TRIPLEATTACK:
  5483. case RG_SNATCHER:
  5484. continue;
  5485. default:
  5486. if( !(skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)) )
  5487. if ( ( sd->status.skill[i].lv = skill_get_max(i) ) )//Nonexistant skills should return a max of 0 anyway.
  5488. sd->status.skill[i].id = i;
  5489. }
  5490. }
  5491. } else {
  5492. int inf2;
  5493. for(i=0;i < MAX_SKILL_TREE && (id=skill_tree[pc_class2idx(sd->status.class_)][i].id)>0;i++){
  5494. inf2 = skill_get_inf2(id);
  5495. if (
  5496. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  5497. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  5498. id==SG_DEVIL
  5499. )
  5500. continue; //Cannot be learned normally.
  5501. sd->status.skill[id].id = id;
  5502. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_); // celest
  5503. }
  5504. }
  5505. status_calc_pc(sd,0);
  5506. //Required because if you could level up all skills previously,
  5507. //the update will not be sent as only the lv variable changes.
  5508. clif_skillinfoblock(sd);
  5509. return 0;
  5510. }
  5511. /*==========================================
  5512. * /resetlvl
  5513. *------------------------------------------*/
  5514. int pc_resetlvl(struct map_session_data* sd,int type)
  5515. {
  5516. int i;
  5517. nullpo_ret(sd);
  5518. if (type != 3) //Also reset skills
  5519. pc_resetskill(sd, 0);
  5520. if(type == 1){
  5521. sd->status.skill_point=0;
  5522. sd->status.base_level=1;
  5523. sd->status.job_level=1;
  5524. sd->status.base_exp=0;
  5525. sd->status.job_exp=0;
  5526. if(sd->sc.option !=0)
  5527. sd->sc.option = 0;
  5528. sd->status.str=1;
  5529. sd->status.agi=1;
  5530. sd->status.vit=1;
  5531. sd->status.int_=1;
  5532. sd->status.dex=1;
  5533. sd->status.luk=1;
  5534. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  5535. sd->status.status_point=100; // not 88 [celest]
  5536. // give platinum skills upon changing
  5537. pc_skill(sd,142,1,0);
  5538. pc_skill(sd,143,1,0);
  5539. }
  5540. }
  5541. if(type == 2){
  5542. sd->status.skill_point=0;
  5543. sd->status.base_level=1;
  5544. sd->status.job_level=1;
  5545. sd->status.base_exp=0;
  5546. sd->status.job_exp=0;
  5547. }
  5548. if(type == 3){
  5549. sd->status.base_level=1;
  5550. sd->status.base_exp=0;
  5551. }
  5552. if(type == 4){
  5553. sd->status.job_level=1;
  5554. sd->status.job_exp=0;
  5555. }
  5556. clif_updatestatus(sd,SP_STATUSPOINT);
  5557. clif_updatestatus(sd,SP_STR);
  5558. clif_updatestatus(sd,SP_AGI);
  5559. clif_updatestatus(sd,SP_VIT);
  5560. clif_updatestatus(sd,SP_INT);
  5561. clif_updatestatus(sd,SP_DEX);
  5562. clif_updatestatus(sd,SP_LUK);
  5563. clif_updatestatus(sd,SP_BASELEVEL);
  5564. clif_updatestatus(sd,SP_JOBLEVEL);
  5565. clif_updatestatus(sd,SP_STATUSPOINT);
  5566. clif_updatestatus(sd,SP_BASEEXP);
  5567. clif_updatestatus(sd,SP_JOBEXP);
  5568. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5569. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5570. clif_updatestatus(sd,SP_SKILLPOINT);
  5571. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5572. clif_updatestatus(sd,SP_UAGI);
  5573. clif_updatestatus(sd,SP_UVIT);
  5574. clif_updatestatus(sd,SP_UINT);
  5575. clif_updatestatus(sd,SP_UDEX);
  5576. clif_updatestatus(sd,SP_ULUK); // End Addition
  5577. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  5578. if(sd->equip_index[i] >= 0)
  5579. if(!pc_isequip(sd,sd->equip_index[i]))
  5580. pc_unequipitem(sd,sd->equip_index[i],2);
  5581. }
  5582. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  5583. party_send_levelup(sd);
  5584. status_calc_pc(sd,0);
  5585. clif_skillinfoblock(sd);
  5586. return 0;
  5587. }
  5588. /*==========================================
  5589. * /resetstate
  5590. *------------------------------------------*/
  5591. int pc_resetstate(struct map_session_data* sd)
  5592. {
  5593. nullpo_ret(sd);
  5594. if (battle_config.use_statpoint_table)
  5595. { // New statpoint table used here - Dexity
  5596. if (sd->status.base_level > MAX_LEVEL)
  5597. { //statp[] goes out of bounds, can't reset!
  5598. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  5599. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  5600. return 0;
  5601. }
  5602. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  5603. }
  5604. else
  5605. {
  5606. int add=0;
  5607. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  5608. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  5609. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  5610. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  5611. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  5612. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  5613. sd->status.status_point+=add;
  5614. }
  5615. pc_setstat(sd, SP_STR, 1);
  5616. pc_setstat(sd, SP_AGI, 1);
  5617. pc_setstat(sd, SP_VIT, 1);
  5618. pc_setstat(sd, SP_INT, 1);
  5619. pc_setstat(sd, SP_DEX, 1);
  5620. pc_setstat(sd, SP_LUK, 1);
  5621. clif_updatestatus(sd,SP_STR);
  5622. clif_updatestatus(sd,SP_AGI);
  5623. clif_updatestatus(sd,SP_VIT);
  5624. clif_updatestatus(sd,SP_INT);
  5625. clif_updatestatus(sd,SP_DEX);
  5626. clif_updatestatus(sd,SP_LUK);
  5627. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5628. clif_updatestatus(sd,SP_UAGI);
  5629. clif_updatestatus(sd,SP_UVIT);
  5630. clif_updatestatus(sd,SP_UINT);
  5631. clif_updatestatus(sd,SP_UDEX);
  5632. clif_updatestatus(sd,SP_ULUK); // End Addition
  5633. clif_updatestatus(sd,SP_STATUSPOINT);
  5634. if( sd->mission_mobid ) { //bugreport:2200
  5635. sd->mission_mobid = 0;
  5636. sd->mission_count = 0;
  5637. pc_setglobalreg(sd,"TK_MISSION_ID", 0);
  5638. }
  5639. status_calc_pc(sd,0);
  5640. return 1;
  5641. }
  5642. /*==========================================
  5643. * /resetskill
  5644. * if flag&1, perform block resync and status_calc call.
  5645. * if flag&2, just count total amount of skill points used by player, do not really reset.
  5646. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  5647. *------------------------------------------*/
  5648. int pc_resetskill(struct map_session_data* sd, int flag)
  5649. {
  5650. int i, lv, inf2, skill_point=0;
  5651. nullpo_ret(sd);
  5652. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  5653. return 0;
  5654. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  5655. /**
  5656. * 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)
  5657. **/
  5658. if( (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5659. return 0;
  5660. if( pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd) )
  5661. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  5662. i = sd->sc.option;
  5663. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  5664. i &= ~OPTION_RIDING;
  5665. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  5666. i &= ~OPTION_FALCON;
  5667. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  5668. i &= ~OPTION_DRAGON;
  5669. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  5670. i &= ~OPTION_WUG;
  5671. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  5672. i &= ~OPTION_WUGRIDER;
  5673. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  5674. i &= ~OPTION_MADOGEAR;
  5675. if( i&OPTION_MOUNTING )
  5676. i &= ~OPTION_MOUNTING;
  5677. #ifndef NEW_CARTS
  5678. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  5679. i &= ~OPTION_CART;
  5680. #else
  5681. if( sd->sc.data[SC_PUSH_CART] )
  5682. pc_setcart(sd, 0);
  5683. #endif
  5684. if( i != sd->sc.option )
  5685. pc_setoption(sd, i);
  5686. if( merc_is_hom_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  5687. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  5688. }
  5689. for( i = 1; i < MAX_SKILL; i++ )
  5690. {
  5691. lv = sd->status.skill[i].lv;
  5692. if (lv < 1) continue;
  5693. inf2 = skill_get_inf2(i);
  5694. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  5695. continue;
  5696. // Don't reset trick dead if not a novice/baby
  5697. if( i == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5698. {
  5699. sd->status.skill[i].lv = 0;
  5700. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5701. continue;
  5702. }
  5703. // do not reset basic skill
  5704. if( i == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5705. continue;
  5706. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  5707. continue;
  5708. if( flag&4 && !skill_ischangesex(i) )
  5709. continue;
  5710. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  5711. { //Only handle quest skills in a special way when you can't learn them manually
  5712. if( battle_config.quest_skill_reset && !(flag&2) )
  5713. { //Wipe them
  5714. sd->status.skill[i].lv = 0;
  5715. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5716. }
  5717. continue;
  5718. }
  5719. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  5720. skill_point += lv;
  5721. else
  5722. if( sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0 )
  5723. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  5724. if( !(flag&2) )
  5725. {// reset
  5726. sd->status.skill[i].lv = 0;
  5727. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5728. }
  5729. }
  5730. if( flag&2 || !skill_point ) return skill_point;
  5731. sd->status.skill_point += skill_point;
  5732. if( flag&1 )
  5733. {
  5734. clif_updatestatus(sd,SP_SKILLPOINT);
  5735. clif_skillinfoblock(sd);
  5736. status_calc_pc(sd,0);
  5737. }
  5738. return skill_point;
  5739. }
  5740. /*==========================================
  5741. * /resetfeel [Komurka]
  5742. *------------------------------------------*/
  5743. int pc_resetfeel(struct map_session_data* sd)
  5744. {
  5745. int i;
  5746. nullpo_ret(sd);
  5747. for (i=0; i<MAX_PC_FEELHATE; i++)
  5748. {
  5749. sd->feel_map[i].m = -1;
  5750. sd->feel_map[i].index = 0;
  5751. pc_setglobalreg(sd,sg_info[i].feel_var,0);
  5752. }
  5753. return 0;
  5754. }
  5755. int pc_resethate(struct map_session_data* sd)
  5756. {
  5757. int i;
  5758. nullpo_ret(sd);
  5759. for (i=0; i<3; i++)
  5760. {
  5761. sd->hate_mob[i] = -1;
  5762. pc_setglobalreg(sd,sg_info[i].hate_var,0);
  5763. }
  5764. return 0;
  5765. }
  5766. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  5767. {
  5768. int i, bonus = 0;
  5769. nullpo_ret(sd);
  5770. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  5771. if( i < ARRAYLENGTH(sd->skillatk) ) bonus = sd->skillatk[i].val;
  5772. if(sd->sc.data[SC_PYROTECHNIC_OPTION] || sd->sc.data[SC_AQUAPLAY_OPTION])
  5773. bonus += 10;
  5774. return bonus;
  5775. }
  5776. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  5777. int i, bonus = sd->bonus.add_heal_rate;
  5778. if( bonus ) {
  5779. switch( skill_id ) {
  5780. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  5781. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  5782. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  5783. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  5784. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  5785. }
  5786. }
  5787. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  5788. if( i < ARRAYLENGTH(sd->skillheal) )
  5789. bonus += sd->skillheal[i].val;
  5790. return bonus;
  5791. }
  5792. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  5793. int i, bonus = sd->bonus.add_heal2_rate;
  5794. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  5795. if( i < ARRAYLENGTH(sd->skillheal2) )
  5796. bonus += sd->skillheal2[i].val;
  5797. return bonus;
  5798. }
  5799. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  5800. {
  5801. if( !pc_isdead(sd) )
  5802. return; // not applicable
  5803. if( sd->bg_id && bg_member_respawn(sd) )
  5804. return; // member revived by battleground
  5805. pc_setstand(sd);
  5806. pc_setrestartvalue(sd,3);
  5807. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
  5808. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  5809. }
  5810. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  5811. {
  5812. struct map_session_data *sd = map_id2sd(id);
  5813. if( sd != NULL )
  5814. {
  5815. sd->pvp_point=0;
  5816. pc_respawn(sd,CLR_OUTSIGHT);
  5817. }
  5818. return 0;
  5819. }
  5820. /*==========================================
  5821. * Invoked when a player has received damage
  5822. *------------------------------------------*/
  5823. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  5824. {
  5825. if (sp) clif_updatestatus(sd,SP_SP);
  5826. if (hp) clif_updatestatus(sd,SP_HP);
  5827. else return;
  5828. if( !src || src == &sd->bl )
  5829. return;
  5830. if( pc_issit(sd) ) {
  5831. pc_setstand(sd);
  5832. skill_sit(sd,0);
  5833. }
  5834. if( sd->progressbar.npc_id )
  5835. clif_progressbar_abort(sd);
  5836. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  5837. pet_target_check(sd,src,1);
  5838. if( sd->status.ele_id > 0 )
  5839. elemental_set_target(sd,src);
  5840. sd->canlog_tick = gettick();
  5841. }
  5842. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  5843. {
  5844. TBL_PC *sd = map_id2sd(id);
  5845. if(sd) pc_close_npc(sd,data);
  5846. return 0;
  5847. }
  5848. /*
  5849. * Method to properly close npc for player and clear anything related
  5850. * @flag == 1 : produce close button
  5851. * @flag == 2 : directly close it
  5852. */
  5853. void pc_close_npc(struct map_session_data *sd,int flag)
  5854. {
  5855. nullpo_retv(sd);
  5856. if (sd->npc_id || sd->npc_shopid) {
  5857. if (sd->state.using_fake_npc) {
  5858. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  5859. sd->state.using_fake_npc = 0;
  5860. }
  5861. if (sd->st) {
  5862. if(sd->st->state == RUN){ //wait ending code execution
  5863. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  5864. return;
  5865. }
  5866. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  5867. sd->st->mes_active = 0;
  5868. }
  5869. sd->state.menu_or_input = 0;
  5870. sd->npc_menu = 0;
  5871. sd->npc_shopid = 0;
  5872. #ifdef SECURE_NPCTIMEOUT
  5873. sd->npc_idle_timer = INVALID_TIMER;
  5874. #endif
  5875. clif_scriptclose(sd,sd->npc_id);
  5876. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  5877. script_free_state(sd->st);
  5878. sd->st = NULL;
  5879. sd->npc_id = 0;
  5880. }
  5881. }
  5882. }
  5883. /*==========================================
  5884. * Invoked when a player has negative current hp
  5885. *------------------------------------------*/
  5886. int pc_dead(struct map_session_data *sd,struct block_list *src)
  5887. {
  5888. int i=0,j=0,k=0;
  5889. unsigned int tick = gettick();
  5890. // Activate Steel body if a super novice dies at 99+% exp [celest]
  5891. // Super Novices have no kill or die functions attached when saved by their angel
  5892. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && !sd->state.snovice_dead_flag) {
  5893. unsigned int next = pc_nextbaseexp(sd);
  5894. if( next == 0 ) next = pc_thisbaseexp(sd);
  5895. if( get_percentage(sd->status.base_exp,next) >= 99 ) {
  5896. sd->state.snovice_dead_flag = 1;
  5897. pc_setrestartvalue(sd,1);
  5898. status_percent_heal(&sd->bl, 100, 100);
  5899. clif_resurrection(&sd->bl, 1);
  5900. if(battle_config.pc_invincible_time)
  5901. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  5902. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  5903. if(map_flag_gvg(sd->bl.m))
  5904. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5905. return 0;
  5906. }
  5907. }
  5908. for(k = 0; k < 5; k++)
  5909. if (sd->devotion[k]){
  5910. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  5911. if (devsd)
  5912. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  5913. sd->devotion[k] = 0;
  5914. }
  5915. if(sd->shadowform_id) { //if we were target of shadowform
  5916. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  5917. sd->shadowform_id = 0; //should be remove on status end anyway
  5918. }
  5919. if(sd->status.pet_id > 0 && sd->pd) {
  5920. struct pet_data *pd = sd->pd;
  5921. if( !map[sd->bl.m].flag.noexppenalty ) {
  5922. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  5923. if( pd->pet.intimate < 0 )
  5924. pd->pet.intimate = 0;
  5925. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  5926. }
  5927. if( sd->pd->target_id ) // Unlock all targets...
  5928. pet_unlocktarget(sd->pd);
  5929. }
  5930. if (sd->status.hom_id > 0) {
  5931. if(battle_config.homunculus_auto_vapor && sd->hd && !sd->hd->sc.data[SC_LIGHT_OF_REGENE])
  5932. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  5933. }
  5934. if( sd->md )
  5935. merc_delete(sd->md, 3); // Your mercenary soldier has ran away.
  5936. if( sd->ed )
  5937. elemental_delete(sd->ed, 0);
  5938. // Leave duel if you die [LuzZza]
  5939. if(battle_config.duel_autoleave_when_die) {
  5940. if(sd->duel_group > 0)
  5941. duel_leave(sd->duel_group, sd);
  5942. if(sd->duel_invite > 0)
  5943. duel_reject(sd->duel_invite, sd);
  5944. }
  5945. pc_close_npc(sd,2); //close npc if we were using one
  5946. /* e.g. not killed thru pc_damage */
  5947. if( pc_issit(sd) ) {
  5948. clif_status_load(&sd->bl,SI_SIT,0);
  5949. }
  5950. pc_setdead(sd);
  5951. pc_setglobalreg(sd,"PC_DIE_COUNTER",sd->die_counter+1);
  5952. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  5953. //Reset menu skills/item skills
  5954. if (sd->skillitem)
  5955. sd->skillitem = sd->skillitemlv = 0;
  5956. if (sd->menuskill_id)
  5957. sd->menuskill_id = sd->menuskill_val = 0;
  5958. //Reset ticks.
  5959. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  5960. if ( sd && sd->spiritball )
  5961. pc_delspiritball(sd,sd->spiritball,0);
  5962. for(i = 1; i < 5; i++)
  5963. pc_del_talisman(sd, sd->talisman[i], i);
  5964. if (src)
  5965. switch (src->type) {
  5966. case BL_MOB:
  5967. {
  5968. struct mob_data *md=(struct mob_data *)src;
  5969. if(md->target_id==sd->bl.id)
  5970. mob_unlocktarget(md,tick);
  5971. if(battle_config.mobs_level_up && md->status.hp &&
  5972. (unsigned int)md->level < pc_maxbaselv(sd) &&
  5973. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  5974. ) { // monster level up [Valaris]
  5975. clif_misceffect(&md->bl,0);
  5976. md->level++;
  5977. status_calc_mob(md, 0);
  5978. status_percent_heal(src,10,0);
  5979. if( battle_config.show_mob_info&4 )
  5980. {// update name with new level
  5981. clif_charnameack(0, &md->bl);
  5982. }
  5983. }
  5984. src = battle_get_master(src); // Maybe Player Summon
  5985. }
  5986. break;
  5987. case BL_PET: //Pass on to master...
  5988. case BL_HOM:
  5989. case BL_MER:
  5990. src = battle_get_master(src);
  5991. break;
  5992. }
  5993. if (src && src->type == BL_PC) {
  5994. struct map_session_data *ssd = (struct map_session_data *)src;
  5995. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  5996. npc_script_event(ssd, NPCE_KILLPC);
  5997. if (battle_config.pk_mode&2) {
  5998. ssd->status.manner -= 5;
  5999. if(ssd->status.manner < 0)
  6000. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6001. #if 0
  6002. // PK/Karma system code (not enabled yet) [celest]
  6003. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6004. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6005. // karma going down = more 'good' / more honourable.
  6006. // The Karma System way...
  6007. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6008. sd->status.karma--;
  6009. ssd->status.karma--;
  6010. }
  6011. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6012. ssd->status.karma++;
  6013. // or the PK System way...
  6014. if (sd->status.karma > 0) // player killed is dishonourable?
  6015. ssd->status.karma--; // honour points earned
  6016. sd->status.karma++; // honour points lost
  6017. // To-do: Receive exp on certain occasions
  6018. #endif
  6019. }
  6020. }
  6021. if(battle_config.bone_drop==2
  6022. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6023. {
  6024. struct item item_tmp;
  6025. memset(&item_tmp,0,sizeof(item_tmp));
  6026. item_tmp.nameid=ITEMID_SKULL_;
  6027. item_tmp.identify=1;
  6028. item_tmp.card[0]=CARD0_CREATE;
  6029. item_tmp.card[1]=0;
  6030. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6031. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6032. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  6033. }
  6034. // changed penalty options, added death by player if pk_mode [Valaris]
  6035. if(battle_config.death_penalty_type
  6036. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6037. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6038. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6039. {
  6040. unsigned int base_penalty =0;
  6041. if (battle_config.death_penalty_base > 0) {
  6042. switch (battle_config.death_penalty_type) {
  6043. case 1:
  6044. base_penalty = (unsigned int) ((double)pc_nextbaseexp(sd) * (double)battle_config.death_penalty_base/10000);
  6045. break;
  6046. case 2:
  6047. base_penalty = (unsigned int) ((double)sd->status.base_exp * (double)battle_config.death_penalty_base/10000);
  6048. break;
  6049. }
  6050. if(base_penalty) {
  6051. if (battle_config.pk_mode && src && src->type==BL_PC)
  6052. base_penalty*=2;
  6053. sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
  6054. clif_updatestatus(sd,SP_BASEEXP);
  6055. }
  6056. }
  6057. if(battle_config.death_penalty_job > 0) {
  6058. base_penalty = 0;
  6059. switch (battle_config.death_penalty_type) {
  6060. case 1:
  6061. base_penalty = (unsigned int) ((double)pc_nextjobexp(sd) * (double)battle_config.death_penalty_job/10000);
  6062. break;
  6063. case 2:
  6064. base_penalty = (unsigned int) ((double)sd->status.job_exp * (double)battle_config.death_penalty_job/10000);
  6065. break;
  6066. }
  6067. if(base_penalty) {
  6068. if (battle_config.pk_mode && src && src->type==BL_PC)
  6069. base_penalty*=2;
  6070. sd->status.job_exp -= min(sd->status.job_exp, base_penalty);
  6071. clif_updatestatus(sd,SP_JOBEXP);
  6072. }
  6073. }
  6074. if(battle_config.zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6075. base_penalty = (unsigned int)((double)sd->status.zeny * (double)battle_config.zeny_penalty / 10000.);
  6076. if(base_penalty)
  6077. pc_payzeny(sd, base_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6078. }
  6079. }
  6080. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6081. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6082. int id = map[sd->bl.m].drop_list[j].drop_id;
  6083. int type = map[sd->bl.m].drop_list[j].drop_type;
  6084. int per = map[sd->bl.m].drop_list[j].drop_per;
  6085. if(id == 0)
  6086. continue;
  6087. if(id == -1){
  6088. int eq_num=0,eq_n[MAX_INVENTORY];
  6089. memset(eq_n,0,sizeof(eq_n));
  6090. for(i=0;i<MAX_INVENTORY;i++) {
  6091. if( (type == 1 && !sd->status.inventory[i].equip)
  6092. || (type == 2 && sd->status.inventory[i].equip)
  6093. || type == 3)
  6094. {
  6095. int k;
  6096. ARR_FIND( 0, MAX_INVENTORY, k, eq_n[k] <= 0 );
  6097. if( k < MAX_INVENTORY )
  6098. eq_n[k] = i;
  6099. eq_num++;
  6100. }
  6101. }
  6102. if(eq_num > 0){
  6103. int n = eq_n[rnd()%eq_num];
  6104. if(rnd()%10000 < per) {
  6105. if(sd->status.inventory[n].equip)
  6106. pc_unequipitem(sd,n,3);
  6107. pc_dropitem(sd,n,1);
  6108. }
  6109. }
  6110. }
  6111. else if(id > 0) {
  6112. for(i=0;i<MAX_INVENTORY;i++){
  6113. if(sd->status.inventory[i].nameid == id
  6114. && rnd()%10000 < per
  6115. && ((type == 1 && !sd->status.inventory[i].equip)
  6116. || (type == 2 && sd->status.inventory[i].equip)
  6117. || type == 3) ){
  6118. if(sd->status.inventory[i].equip)
  6119. pc_unequipitem(sd,i,3);
  6120. pc_dropitem(sd,i,1);
  6121. break;
  6122. }
  6123. }
  6124. }
  6125. }
  6126. }
  6127. // pvp
  6128. // disable certain pvp functions on pk_mode [Valaris]
  6129. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6130. sd->pvp_point -= 5;
  6131. sd->pvp_lost++;
  6132. if( src && src->type == BL_PC ) {
  6133. struct map_session_data *ssd = (struct map_session_data *)src;
  6134. ssd->pvp_point++;
  6135. ssd->pvp_won++;
  6136. }
  6137. if( sd->pvp_point < 0 ) {
  6138. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6139. return 1|8;
  6140. }
  6141. }
  6142. //GvG
  6143. if( map_flag_gvg(sd->bl.m) ) {
  6144. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6145. return 1|8;
  6146. }
  6147. else if( sd->bg_id ) {
  6148. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6149. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6150. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6151. return 1|8;
  6152. }
  6153. }
  6154. //Reset "can log out" tick.
  6155. if( battle_config.prevent_logout )
  6156. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6157. return 1;
  6158. }
  6159. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6160. if(hp) clif_updatestatus(sd,SP_HP);
  6161. if(sp) clif_updatestatus(sd,SP_SP);
  6162. pc_setstand(sd);
  6163. if(battle_config.pc_invincible_time > 0)
  6164. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6165. if( sd->state.gmaster_flag ) {
  6166. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->state.gmaster_flag,GD_LEADERSHIP));
  6167. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->state.gmaster_flag,GD_GLORYWOUNDS));
  6168. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->state.gmaster_flag,GD_SOULCOLD));
  6169. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->state.gmaster_flag,GD_HAWKEYES));
  6170. }
  6171. }
  6172. // script
  6173. //
  6174. /*==========================================
  6175. * script reading pc status registry
  6176. *------------------------------------------*/
  6177. int pc_readparam(struct map_session_data* sd,int type)
  6178. {
  6179. int val = 0;
  6180. nullpo_ret(sd);
  6181. switch(type) {
  6182. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6183. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6184. case SP_ZENY: val = sd->status.zeny; break;
  6185. case SP_BASELEVEL: val = sd->status.base_level; break;
  6186. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6187. case SP_CLASS: val = sd->status.class_; break;
  6188. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6189. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6190. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6191. case SP_SEX: val = sd->status.sex; break;
  6192. case SP_WEIGHT: val = sd->weight; break;
  6193. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6194. case SP_BASEEXP: val = sd->status.base_exp; break;
  6195. case SP_JOBEXP: val = sd->status.job_exp; break;
  6196. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6197. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6198. case SP_HP: val = sd->battle_status.hp; break;
  6199. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6200. case SP_SP: val = sd->battle_status.sp; break;
  6201. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6202. case SP_STR: val = sd->status.str; break;
  6203. case SP_AGI: val = sd->status.agi; break;
  6204. case SP_VIT: val = sd->status.vit; break;
  6205. case SP_INT: val = sd->status.int_; break;
  6206. case SP_DEX: val = sd->status.dex; break;
  6207. case SP_LUK: val = sd->status.luk; break;
  6208. case SP_KARMA: val = sd->status.karma; break;
  6209. case SP_MANNER: val = sd->status.manner; break;
  6210. case SP_FAME: val = sd->status.fame; break;
  6211. case SP_KILLERRID: val = sd->killerrid; break;
  6212. case SP_KILLEDRID: val = sd->killedrid; break;
  6213. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6214. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6215. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6216. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6217. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6218. case SP_DEF1: val = sd->battle_status.def; break;
  6219. case SP_DEF2: val = sd->battle_status.def2; break;
  6220. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6221. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6222. case SP_HIT: val = sd->battle_status.hit; break;
  6223. case SP_FLEE1: val = sd->battle_status.flee; break;
  6224. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6225. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6226. #ifndef RENEWAL_CAST
  6227. case SP_VARCASTRATE:
  6228. #endif
  6229. case SP_CASTRATE:
  6230. val = sd->castrate+=val;
  6231. break;
  6232. case SP_MAXHPRATE: val = sd->hprate; break;
  6233. case SP_MAXSPRATE: val = sd->sprate; break;
  6234. case SP_SPRATE: val = sd->dsprate; break;
  6235. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6236. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6237. case SP_ASPD_RATE:
  6238. #ifndef RENEWAL_ASPD
  6239. val = sd->battle_status.aspd_rate;
  6240. #else
  6241. val = sd->battle_status.aspd_rate2;
  6242. #endif
  6243. break;
  6244. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6245. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6246. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6247. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6248. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6249. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6250. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6251. case SP_MATK_RATE: val = sd->matk_rate; break;
  6252. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6253. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6254. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6255. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6256. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6257. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6258. case SP_HIT_RATE: val = sd->hit_rate; break;
  6259. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6260. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6261. case SP_DEF_RATE: val = sd->def_rate; break;
  6262. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6263. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6264. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6265. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6266. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6267. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6268. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6269. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6270. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6271. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6272. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6273. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6274. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6275. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6276. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6277. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6278. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6279. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6280. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6281. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6282. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6283. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6284. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6285. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6286. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6287. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6288. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6289. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6290. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6291. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6292. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6293. case SP_DELAYRATE: val = sd->delayrate; break;
  6294. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6295. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6296. case SP_UNSTRIPABLE:
  6297. case SP_UNSTRIPABLE_ARMOR:
  6298. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6299. break;
  6300. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6301. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6302. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6303. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6304. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6305. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6306. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  6307. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  6308. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  6309. case SP_EMATK: val = sd->bonus.ematk; break;
  6310. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  6311. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  6312. #ifdef RENEWAL_CAST
  6313. case SP_VARCASTRATE: val = sd->bonus.varcastrate; break;
  6314. case SP_ADD_VARIABLECAST:val = sd->bonus.add_varcast; break;
  6315. #endif
  6316. }
  6317. return val;
  6318. }
  6319. /*==========================================
  6320. * script set pc status registry
  6321. *------------------------------------------*/
  6322. int pc_setparam(struct map_session_data *sd,int type,int val)
  6323. {
  6324. int i = 0;
  6325. nullpo_ret(sd);
  6326. switch(type){
  6327. case SP_BASELEVEL:
  6328. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  6329. val = pc_maxbaselv(sd);
  6330. if ((unsigned int)val > sd->status.base_level) {
  6331. int stat=0;
  6332. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  6333. stat += pc_gets_status_point(sd->status.base_level + i);
  6334. sd->status.status_point += stat;
  6335. }
  6336. sd->status.base_level = (unsigned int)val;
  6337. sd->status.base_exp = 0;
  6338. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  6339. clif_updatestatus(sd, SP_NEXTBASEEXP);
  6340. clif_updatestatus(sd, SP_STATUSPOINT);
  6341. clif_updatestatus(sd, SP_BASEEXP);
  6342. status_calc_pc(sd, 0);
  6343. if(sd->status.party_id)
  6344. {
  6345. party_send_levelup(sd);
  6346. }
  6347. break;
  6348. case SP_JOBLEVEL:
  6349. if ((unsigned int)val >= sd->status.job_level) {
  6350. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  6351. sd->status.skill_point += val - sd->status.job_level;
  6352. clif_updatestatus(sd, SP_SKILLPOINT);
  6353. }
  6354. sd->status.job_level = (unsigned int)val;
  6355. sd->status.job_exp = 0;
  6356. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  6357. clif_updatestatus(sd, SP_NEXTJOBEXP);
  6358. clif_updatestatus(sd, SP_JOBEXP);
  6359. status_calc_pc(sd, 0);
  6360. break;
  6361. case SP_SKILLPOINT:
  6362. sd->status.skill_point = val;
  6363. break;
  6364. case SP_STATUSPOINT:
  6365. sd->status.status_point = val;
  6366. break;
  6367. case SP_ZENY:
  6368. if( val < 0 )
  6369. return 0;// can't set negative zeny
  6370. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  6371. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  6372. break;
  6373. case SP_BASEEXP:
  6374. if(pc_nextbaseexp(sd) > 0) {
  6375. sd->status.base_exp = val;
  6376. pc_checkbaselevelup(sd);
  6377. }
  6378. break;
  6379. case SP_JOBEXP:
  6380. if(pc_nextjobexp(sd) > 0) {
  6381. sd->status.job_exp = val;
  6382. pc_checkjoblevelup(sd);
  6383. }
  6384. break;
  6385. case SP_SEX:
  6386. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  6387. break;
  6388. case SP_WEIGHT:
  6389. sd->weight = val;
  6390. break;
  6391. case SP_MAXWEIGHT:
  6392. sd->max_weight = val;
  6393. break;
  6394. case SP_HP:
  6395. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  6396. break;
  6397. case SP_MAXHP:
  6398. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  6399. if( sd->battle_status.max_hp < sd->battle_status.hp )
  6400. {
  6401. sd->battle_status.hp = sd->battle_status.max_hp;
  6402. clif_updatestatus(sd, SP_HP);
  6403. }
  6404. break;
  6405. case SP_SP:
  6406. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  6407. break;
  6408. case SP_MAXSP:
  6409. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  6410. if( sd->battle_status.max_sp < sd->battle_status.sp )
  6411. {
  6412. sd->battle_status.sp = sd->battle_status.max_sp;
  6413. clif_updatestatus(sd, SP_SP);
  6414. }
  6415. break;
  6416. case SP_STR:
  6417. sd->status.str = cap_value(val, 1, pc_maxparameter(sd));
  6418. break;
  6419. case SP_AGI:
  6420. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd));
  6421. break;
  6422. case SP_VIT:
  6423. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd));
  6424. break;
  6425. case SP_INT:
  6426. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd));
  6427. break;
  6428. case SP_DEX:
  6429. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd));
  6430. break;
  6431. case SP_LUK:
  6432. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd));
  6433. break;
  6434. case SP_KARMA:
  6435. sd->status.karma = val;
  6436. break;
  6437. case SP_MANNER:
  6438. sd->status.manner = val;
  6439. break;
  6440. case SP_FAME:
  6441. sd->status.fame = val;
  6442. break;
  6443. case SP_KILLERRID:
  6444. sd->killerrid = val;
  6445. return 1;
  6446. case SP_KILLEDRID:
  6447. sd->killedrid = val;
  6448. return 1;
  6449. case SP_CHARMOVE:
  6450. sd->status.character_moves = val;
  6451. return 1;
  6452. default:
  6453. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  6454. return 0;
  6455. }
  6456. clif_updatestatus(sd,type);
  6457. return 1;
  6458. }
  6459. /*==========================================
  6460. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  6461. *------------------------------------------*/
  6462. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  6463. {
  6464. if (type) {
  6465. if (hp)
  6466. clif_heal(sd->fd,SP_HP,hp);
  6467. if (sp)
  6468. clif_heal(sd->fd,SP_SP,sp);
  6469. } else {
  6470. if(hp)
  6471. clif_updatestatus(sd,SP_HP);
  6472. if(sp)
  6473. clif_updatestatus(sd,SP_SP);
  6474. }
  6475. return;
  6476. }
  6477. /*==========================================
  6478. * HP/SP Recovery
  6479. * Heal player hp and/or sp linearly.
  6480. * Calculate bonus by status.
  6481. *------------------------------------------*/
  6482. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  6483. {
  6484. int bonus;
  6485. if(hp) {
  6486. int i;
  6487. bonus = 100 + (sd->battle_status.vit<<1)
  6488. + pc_checkskill(sd,SM_RECOVERY)*10
  6489. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6490. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  6491. if (potion_flag > 1)
  6492. bonus += bonus*(potion_flag-1)*50/100;
  6493. //All item bonuses.
  6494. bonus += sd->bonus.itemhealrate2;
  6495. //Item Group bonuses
  6496. bonus += bonus*itemdb_group_bonus(sd, itemid)/100;
  6497. //Individual item bonuses.
  6498. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  6499. {
  6500. if (sd->itemhealrate[i].nameid == itemid) {
  6501. bonus += bonus*sd->itemhealrate[i].rate/100;
  6502. break;
  6503. }
  6504. }
  6505. if(bonus!=100)
  6506. hp = hp * bonus / 100;
  6507. // Recovery Potion
  6508. if( sd->sc.data[SC_INCHEALRATE] )
  6509. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  6510. }
  6511. if(sp) {
  6512. bonus = 100 + (sd->battle_status.int_<<1)
  6513. + pc_checkskill(sd,MG_SRECOVERY)*10
  6514. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6515. if (potion_flag > 1)
  6516. bonus += bonus*(potion_flag-1)*50/100;
  6517. if(bonus != 100)
  6518. sp = sp * bonus / 100;
  6519. }
  6520. if( sd->sc.count ) {
  6521. if ( sd->sc.data[SC_CRITICALWOUND] ) {
  6522. hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6523. sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6524. }
  6525. if ( sd->sc.data[SC_DEATHHURT] ) {
  6526. hp -= hp * 20 / 100;
  6527. sp -= sp * 20 / 100;
  6528. }
  6529. if( sd->sc.data[SC_VITALITYACTIVATION] ){
  6530. hp += hp / 2; // 1.5 times
  6531. sp -= sp / 2;
  6532. }
  6533. if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
  6534. hp += hp / 10;
  6535. sp += sp / 10;
  6536. }
  6537. #ifdef RENEWAL
  6538. if( sd->sc.data[SC_EXTREMITYFIST2] )
  6539. sp = 0;
  6540. #endif
  6541. }
  6542. return status_heal(&sd->bl, hp, sp, 1);
  6543. }
  6544. /*==========================================
  6545. * HP/SP Recovery
  6546. * Heal player hp nad/or sp by rate
  6547. *------------------------------------------*/
  6548. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  6549. {
  6550. nullpo_ret(sd);
  6551. if (hp > 100) hp = 100;
  6552. else if (hp <-100) hp = -100;
  6553. if (sp > 100) sp = 100;
  6554. else if (sp <-100) sp = -100;
  6555. if(hp >= 0 && sp >= 0) //Heal
  6556. return status_percent_heal(&sd->bl, hp, sp);
  6557. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  6558. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  6559. //Crossed signs
  6560. if(hp) {
  6561. if(hp > 0)
  6562. status_percent_heal(&sd->bl, hp, 0);
  6563. else
  6564. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  6565. }
  6566. if(sp) {
  6567. if(sp > 0)
  6568. status_percent_heal(&sd->bl, 0, sp);
  6569. else
  6570. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  6571. }
  6572. return 0;
  6573. }
  6574. static int jobchange_killclone(struct block_list *bl, va_list ap)
  6575. {
  6576. struct mob_data *md;
  6577. int flag;
  6578. md = (struct mob_data *)bl;
  6579. nullpo_ret(md);
  6580. flag = va_arg(ap, int);
  6581. if (md->master_id && md->special_state.clone && md->master_id == flag)
  6582. status_kill(&md->bl);
  6583. return 1;
  6584. }
  6585. /*==========================================
  6586. * Called when player changes job
  6587. * Rewrote to make it tidider [Celest]
  6588. *------------------------------------------*/
  6589. int pc_jobchange(struct map_session_data *sd,int job, int upper)
  6590. {
  6591. int i, fame_flag=0;
  6592. int b_class;
  6593. nullpo_ret(sd);
  6594. if (job < 0)
  6595. return 1;
  6596. //Normalize job.
  6597. b_class = pc_jobid2mapid(job);
  6598. if (b_class == -1)
  6599. return 1;
  6600. switch (upper) {
  6601. case 1:
  6602. b_class|= JOBL_UPPER;
  6603. break;
  6604. case 2:
  6605. b_class|= JOBL_BABY;
  6606. break;
  6607. }
  6608. //This will automatically adjust bard/dancer classes to the correct gender
  6609. //That is, if you try to jobchange into dancer, it will turn you to bard.
  6610. job = pc_mapid2jobid(b_class, sd->status.sex);
  6611. if (job == -1)
  6612. return 1;
  6613. if ((unsigned short)b_class == sd->class_)
  6614. return 1; //Nothing to change.
  6615. // changing from 1st to 2nd job
  6616. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (b_class&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  6617. sd->change_level_2nd = sd->status.job_level;
  6618. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  6619. }
  6620. // changing from 2nd to 3rd job
  6621. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  6622. sd->change_level_3rd = sd->status.job_level;
  6623. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  6624. }
  6625. if(sd->cloneskill_id) {
  6626. if( sd->status.skill[sd->cloneskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6627. sd->status.skill[sd->cloneskill_id].id = 0;
  6628. sd->status.skill[sd->cloneskill_id].lv = 0;
  6629. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PERMANENT;
  6630. clif_deleteskill(sd,sd->cloneskill_id);
  6631. }
  6632. sd->cloneskill_id = 0;
  6633. pc_setglobalreg(sd, "CLONE_SKILL", 0);
  6634. pc_setglobalreg(sd, "CLONE_SKILL_LV", 0);
  6635. }
  6636. if(sd->reproduceskill_id) {
  6637. if( sd->status.skill[sd->reproduceskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6638. sd->status.skill[sd->reproduceskill_id].id = 0;
  6639. sd->status.skill[sd->reproduceskill_id].lv = 0;
  6640. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PERMANENT;
  6641. clif_deleteskill(sd,sd->reproduceskill_id);
  6642. }
  6643. sd->reproduceskill_id = 0;
  6644. pc_setglobalreg(sd, "REPRODUCE_SKILL",0);
  6645. pc_setglobalreg(sd, "REPRODUCE_SKILL_LV",0);
  6646. }
  6647. // Give or reduce transcendent status points
  6648. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  6649. sd->status.status_point += 52;
  6650. clif_updatestatus(sd,SP_STATUSPOINT);
  6651. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  6652. if( sd->status.status_point < 52 ){
  6653. // The player already used his bonus points, so we have to reset his status points
  6654. pc_resetstate(sd);
  6655. }
  6656. sd->status.status_point -= 52;
  6657. clif_updatestatus(sd,SP_STATUSPOINT);
  6658. }
  6659. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  6660. const int class_ = pc_class2idx(sd->status.class_);
  6661. short id;
  6662. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  6663. //Remove status specific to your current tree skills.
  6664. enum sc_type sc = status_skill2sc(id);
  6665. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  6666. status_change_end(&sd->bl, sc, INVALID_TIMER);
  6667. }
  6668. }
  6669. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  6670. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  6671. pc_resetfeel(sd);
  6672. }
  6673. sd->status.class_ = job;
  6674. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  6675. sd->class_ = (unsigned short)b_class;
  6676. sd->status.job_level=1;
  6677. sd->status.job_exp=0;
  6678. if (sd->status.base_level > pc_maxbaselv(sd)) {
  6679. sd->status.base_level = pc_maxbaselv(sd);
  6680. sd->status.base_exp=0;
  6681. pc_resetstate(sd);
  6682. clif_updatestatus(sd,SP_STATUSPOINT);
  6683. clif_updatestatus(sd,SP_BASELEVEL);
  6684. clif_updatestatus(sd,SP_BASEEXP);
  6685. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6686. }
  6687. clif_updatestatus(sd,SP_JOBLEVEL);
  6688. clif_updatestatus(sd,SP_JOBEXP);
  6689. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6690. for(i=0;i<EQI_MAX;i++) {
  6691. if(sd->equip_index[i] >= 0)
  6692. if(!pc_isequip(sd,sd->equip_index[i]))
  6693. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  6694. }
  6695. //Change look, if disguised, you need to undisguise
  6696. //to correctly calculate new job sprite without
  6697. if (sd->disguise)
  6698. pc_disguise(sd, 0);
  6699. status_set_viewdata(&sd->bl, job);
  6700. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  6701. if(sd->vd.cloth_color)
  6702. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6703. //Update skill tree.
  6704. pc_calc_skilltree(sd);
  6705. clif_skillinfoblock(sd);
  6706. if (sd->ed)
  6707. elemental_delete(sd->ed, 0);
  6708. if (sd->state.vending)
  6709. vending_closevending(sd);
  6710. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  6711. //Remove peco/cart/falcon
  6712. i = sd->sc.option;
  6713. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  6714. i&=~OPTION_RIDING;
  6715. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  6716. i&=~OPTION_FALCON;
  6717. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  6718. i&=~OPTION_DRAGON;
  6719. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  6720. i&=~OPTION_WUGRIDER;
  6721. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  6722. i&=~OPTION_WUG;
  6723. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  6724. i&=~OPTION_MADOGEAR;
  6725. #ifndef NEW_CARTS
  6726. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  6727. i&=~OPTION_CART;
  6728. #else
  6729. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  6730. pc_setcart(sd, 0);
  6731. #endif
  6732. if(i != sd->sc.option)
  6733. pc_setoption(sd, i);
  6734. if(merc_is_hom_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  6735. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  6736. if(sd->status.manner < 0)
  6737. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  6738. status_calc_pc(sd,0);
  6739. pc_checkallowskill(sd);
  6740. pc_equiplookall(sd);
  6741. //if you were previously famous, not anymore.
  6742. if (fame_flag) {
  6743. chrif_save(sd,0);
  6744. chrif_buildfamelist();
  6745. } else if (sd->status.fame > 0) {
  6746. //It may be that now they are famous?
  6747. switch (sd->class_&MAPID_UPPERMASK) {
  6748. case MAPID_BLACKSMITH:
  6749. case MAPID_ALCHEMIST:
  6750. case MAPID_TAEKWON:
  6751. chrif_save(sd,0);
  6752. chrif_buildfamelist();
  6753. break;
  6754. }
  6755. }
  6756. return 0;
  6757. }
  6758. /*==========================================
  6759. * Tell client player sd has change equipement
  6760. *------------------------------------------*/
  6761. int pc_equiplookall(struct map_session_data *sd)
  6762. {
  6763. nullpo_ret(sd);
  6764. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  6765. clif_changelook(&sd->bl,LOOK_SHOES,0);
  6766. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  6767. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  6768. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  6769. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  6770. return 0;
  6771. }
  6772. /*==========================================
  6773. * Tell client player sd has change look (hair,equip...)
  6774. *------------------------------------------*/
  6775. int pc_changelook(struct map_session_data *sd,int type,int val)
  6776. {
  6777. nullpo_ret(sd);
  6778. switch(type){
  6779. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  6780. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6781. if (sd->status.hair != val)
  6782. {
  6783. sd->status.hair=val;
  6784. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6785. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6786. GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
  6787. }
  6788. break;
  6789. case LOOK_WEAPON:
  6790. sd->status.weapon=val;
  6791. break;
  6792. case LOOK_HEAD_BOTTOM:
  6793. sd->status.head_bottom=val;
  6794. break;
  6795. case LOOK_HEAD_TOP:
  6796. sd->status.head_top=val;
  6797. break;
  6798. case LOOK_HEAD_MID:
  6799. sd->status.head_mid=val;
  6800. break;
  6801. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  6802. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6803. if (sd->status.hair_color != val) {
  6804. sd->status.hair_color=val;
  6805. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6806. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6807. GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
  6808. }
  6809. break;
  6810. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  6811. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6812. sd->status.clothes_color=val;
  6813. break;
  6814. case LOOK_SHIELD:
  6815. sd->status.shield=val;
  6816. break;
  6817. case LOOK_SHOES:
  6818. break;
  6819. case LOOK_ROBE:
  6820. sd->status.robe = val;
  6821. break;
  6822. }
  6823. clif_changelook(&sd->bl,type,val);
  6824. return 0;
  6825. }
  6826. /*==========================================
  6827. * Give an option (type) to player (sd) and display it to client
  6828. *------------------------------------------*/
  6829. int pc_setoption(struct map_session_data *sd,int type)
  6830. {
  6831. int p_type, new_look=0;
  6832. nullpo_ret(sd);
  6833. p_type = sd->sc.option;
  6834. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  6835. sd->sc.option=type;
  6836. clif_changeoption(&sd->bl);
  6837. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6838. { // Mounting
  6839. clif_status_load(&sd->bl,SI_RIDING,1);
  6840. status_calc_pc(sd,0);
  6841. }
  6842. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6843. { // Dismount
  6844. clif_status_load(&sd->bl,SI_RIDING,0);
  6845. status_calc_pc(sd,0);
  6846. }
  6847. #ifndef NEW_CARTS
  6848. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  6849. clif_cartlist(sd);
  6850. clif_updatestatus(sd, SP_CARTINFO);
  6851. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6852. status_calc_pc(sd,0); //Apply speed penalty.
  6853. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  6854. clif_clearcart(sd->fd);
  6855. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6856. status_calc_pc(sd,0); //Remove speed penalty.
  6857. }
  6858. #endif
  6859. if (type&OPTION_MOUNTING && !(p_type&OPTION_MOUNTING) ) {
  6860. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,2,1,0,0);
  6861. status_calc_pc(sd,0);
  6862. } else if (!(type&OPTION_MOUNTING) && p_type&OPTION_MOUNTING) {
  6863. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,0,0,0,0);
  6864. status_calc_pc(sd,0);
  6865. }
  6866. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  6867. clif_status_load(&sd->bl,SI_FALCON,1);
  6868. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  6869. clif_status_load(&sd->bl,SI_FALCON,0);
  6870. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  6871. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  6872. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  6873. status_calc_pc(sd,0);
  6874. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  6875. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  6876. status_calc_pc(sd,0);
  6877. }
  6878. }
  6879. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  6880. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  6881. status_calc_pc(sd, 0);
  6882. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  6883. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  6884. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  6885. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  6886. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  6887. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  6888. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  6889. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  6890. status_calc_pc(sd, 0);
  6891. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  6892. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  6893. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  6894. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  6895. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  6896. }
  6897. }
  6898. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  6899. new_look = JOB_STAR_GLADIATOR2;
  6900. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  6901. new_look = -1;
  6902. if (type&OPTION_WEDDING && !(p_type&OPTION_WEDDING))
  6903. new_look = JOB_WEDDING;
  6904. else if (!(type&OPTION_WEDDING) && p_type&OPTION_WEDDING)
  6905. new_look = -1;
  6906. if (type&OPTION_XMAS && !(p_type&OPTION_XMAS))
  6907. new_look = JOB_XMAS;
  6908. else if (!(type&OPTION_XMAS) && p_type&OPTION_XMAS)
  6909. new_look = -1;
  6910. if (type&OPTION_SUMMER && !(p_type&OPTION_SUMMER))
  6911. new_look = JOB_SUMMER;
  6912. else if (!(type&OPTION_SUMMER) && p_type&OPTION_SUMMER)
  6913. new_look = -1;
  6914. if (type&OPTION_HANBOK && !(p_type&OPTION_HANBOK))
  6915. new_look = JOB_HANBOK;
  6916. else if (!(type&OPTION_HANBOK) && p_type&OPTION_HANBOK)
  6917. new_look = -1;
  6918. if (sd->disguise || !new_look)
  6919. return 0; //Disguises break sprite changes
  6920. if (new_look < 0) { //Restore normal look.
  6921. status_set_viewdata(&sd->bl, sd->status.class_);
  6922. new_look = sd->vd.class_;
  6923. }
  6924. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  6925. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  6926. if (sd->vd.cloth_color)
  6927. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6928. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  6929. return 0;
  6930. }
  6931. /*==========================================
  6932. * Give player a cart
  6933. *------------------------------------------*/
  6934. int pc_setcart(struct map_session_data *sd,int type) {
  6935. #ifndef NEW_CARTS
  6936. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  6937. int option;
  6938. #endif
  6939. nullpo_ret(sd);
  6940. if( type < 0 || type > MAX_CARTS )
  6941. return 1;// Never trust the values sent by the client! [Skotlex]
  6942. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  6943. return 1;// Push cart is required
  6944. if( type == 0 && pc_iscarton(sd) )
  6945. status_change_end(&sd->bl,SC_GN_CARTBOOST,INVALID_TIMER);
  6946. #ifdef NEW_CARTS
  6947. switch( type ) {
  6948. case 0:
  6949. if( !sd->sc.data[SC_PUSH_CART] )
  6950. return 0;
  6951. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  6952. clif_clearcart(sd->fd);
  6953. break;
  6954. default:/* everything else is an allowed ID so we can move on */
  6955. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  6956. clif_cartlist(sd);
  6957. clif_updatestatus(sd, SP_CARTINFO);
  6958. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  6959. clif_status_load_notick(&sd->bl, SI_ON_PUSH_CART, 2 , type, 0, 0);
  6960. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  6961. sd->sc.data[SC_PUSH_CART]->val1 = type;
  6962. break;
  6963. }
  6964. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6965. status_calc_pc(sd,0); //Recalc speed penalty.
  6966. #else
  6967. // Update option
  6968. option = sd->sc.option;
  6969. option &= ~OPTION_CART;// clear cart bits
  6970. option |= cart[type]; // set cart
  6971. pc_setoption(sd, option);
  6972. #endif
  6973. return 0;
  6974. }
  6975. /*==========================================
  6976. * Give player a falcon
  6977. *------------------------------------------*/
  6978. int pc_setfalcon(TBL_PC* sd, int flag)
  6979. {
  6980. if( flag ){
  6981. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  6982. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  6983. } else if( pc_isfalcon(sd) ){
  6984. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  6985. }
  6986. return 0;
  6987. }
  6988. /*==========================================
  6989. * Set player riding
  6990. *------------------------------------------*/
  6991. int pc_setriding(TBL_PC* sd, int flag)
  6992. {
  6993. if( flag ){
  6994. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  6995. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  6996. } else if( pc_isriding(sd) ){
  6997. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  6998. }
  6999. return 0;
  7000. }
  7001. /*==========================================
  7002. * Give player a mado
  7003. *------------------------------------------*/
  7004. int pc_setmadogear(TBL_PC* sd, int flag)
  7005. {
  7006. if( flag ){
  7007. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7008. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7009. } else if( pc_ismadogear(sd) ){
  7010. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7011. }
  7012. return 0;
  7013. }
  7014. /*==========================================
  7015. * Check if player can drop an item
  7016. *------------------------------------------*/
  7017. int pc_candrop(struct map_session_data *sd, struct item *item)
  7018. {
  7019. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7020. return 0;
  7021. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7022. return 0;
  7023. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7024. }
  7025. /*==========================================
  7026. * Read ram register for player sd
  7027. * get val (int) from reg for player sd
  7028. *------------------------------------------*/
  7029. int pc_readreg(struct map_session_data* sd, int reg)
  7030. {
  7031. int i;
  7032. nullpo_ret(sd);
  7033. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7034. return ( i < sd->reg_num ) ? sd->reg[i].data : 0;
  7035. }
  7036. /*==========================================
  7037. * Set ram register for player sd
  7038. * memo val(int) at reg for player sd
  7039. *------------------------------------------*/
  7040. int pc_setreg(struct map_session_data* sd, int reg, int val)
  7041. {
  7042. int i;
  7043. nullpo_ret(sd);
  7044. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  7045. if( i < sd->reg_num )
  7046. {// overwrite existing entry
  7047. sd->reg[i].data = val;
  7048. return 1;
  7049. }
  7050. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].data == 0 );
  7051. if( i == sd->reg_num )
  7052. {// nothing free, increase size
  7053. sd->reg_num++;
  7054. RECREATE(sd->reg, struct script_reg, sd->reg_num);
  7055. }
  7056. sd->reg[i].index = reg;
  7057. sd->reg[i].data = val;
  7058. return 1;
  7059. }
  7060. /*==========================================
  7061. * Read ram register for player sd
  7062. * get val (str) from reg for player sd
  7063. *------------------------------------------*/
  7064. char* pc_readregstr(struct map_session_data* sd, int reg)
  7065. {
  7066. int i;
  7067. nullpo_ret(sd);
  7068. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7069. return ( i < sd->regstr_num ) ? sd->regstr[i].data : NULL;
  7070. }
  7071. /*==========================================
  7072. * Set ram register for player sd
  7073. * memo val(str) at reg for player sd
  7074. *------------------------------------------*/
  7075. int pc_setregstr(struct map_session_data* sd, int reg, const char* str)
  7076. {
  7077. int i;
  7078. nullpo_ret(sd);
  7079. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  7080. if( i < sd->regstr_num )
  7081. {// found entry, update
  7082. if( str == NULL || *str == '\0' )
  7083. {// empty string
  7084. if( sd->regstr[i].data != NULL )
  7085. aFree(sd->regstr[i].data);
  7086. sd->regstr[i].data = NULL;
  7087. }
  7088. else if( sd->regstr[i].data )
  7089. {// recreate
  7090. size_t len = strlen(str)+1;
  7091. RECREATE(sd->regstr[i].data, char, len);
  7092. memcpy(sd->regstr[i].data, str, len*sizeof(char));
  7093. }
  7094. else
  7095. {// create
  7096. sd->regstr[i].data = aStrdup(str);
  7097. }
  7098. return 1;
  7099. }
  7100. if( str == NULL || *str == '\0' )
  7101. return 1;// nothing to add, empty string
  7102. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].data == NULL );
  7103. if( i == sd->regstr_num )
  7104. {// nothing free, increase size
  7105. sd->regstr_num++;
  7106. RECREATE(sd->regstr, struct script_regstr, sd->regstr_num);
  7107. }
  7108. sd->regstr[i].index = reg;
  7109. sd->regstr[i].data = aStrdup(str);
  7110. return 1;
  7111. }
  7112. int pc_readregistry(struct map_session_data *sd,const char *reg,int type)
  7113. {
  7114. struct global_reg *sd_reg;
  7115. int i,max;
  7116. nullpo_ret(sd);
  7117. switch (type) {
  7118. case 3: //Char reg
  7119. sd_reg = sd->save_reg.global;
  7120. max = sd->save_reg.global_num;
  7121. break;
  7122. case 2: //Account reg
  7123. sd_reg = sd->save_reg.account;
  7124. max = sd->save_reg.account_num;
  7125. break;
  7126. case 1: //Account2 reg
  7127. sd_reg = sd->save_reg.account2;
  7128. max = sd->save_reg.account2_num;
  7129. break;
  7130. default:
  7131. return 0;
  7132. }
  7133. if (max == -1) {
  7134. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7135. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7136. intif_request_registry(sd,type==3?4:type);
  7137. return 0;
  7138. }
  7139. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7140. return ( i < max ) ? atoi(sd_reg[i].value) : 0;
  7141. }
  7142. char* pc_readregistry_str(struct map_session_data *sd,const char *reg,int type)
  7143. {
  7144. struct global_reg *sd_reg;
  7145. int i,max;
  7146. nullpo_ret(sd);
  7147. switch (type) {
  7148. case 3: //Char reg
  7149. sd_reg = sd->save_reg.global;
  7150. max = sd->save_reg.global_num;
  7151. break;
  7152. case 2: //Account reg
  7153. sd_reg = sd->save_reg.account;
  7154. max = sd->save_reg.account_num;
  7155. break;
  7156. case 1: //Account2 reg
  7157. sd_reg = sd->save_reg.account2;
  7158. max = sd->save_reg.account2_num;
  7159. break;
  7160. default:
  7161. return NULL;
  7162. }
  7163. if (max == -1) {
  7164. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7165. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7166. intif_request_registry(sd,type==3?4:type);
  7167. return NULL;
  7168. }
  7169. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7170. return ( i < max ) ? sd_reg[i].value : NULL;
  7171. }
  7172. int pc_setregistry(struct map_session_data *sd,const char *reg,int val,int type)
  7173. {
  7174. struct global_reg *sd_reg;
  7175. int i,*max, regmax;
  7176. nullpo_ret(sd);
  7177. switch( type )
  7178. {
  7179. case 3: //Char reg
  7180. if( !strcmp(reg,"PC_DIE_COUNTER") && sd->die_counter != val )
  7181. {
  7182. i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7183. sd->die_counter = val;
  7184. if( i )
  7185. status_calc_pc(sd,0); // Lost the bonus.
  7186. }
  7187. else if( !strcmp(reg,"COOK_MASTERY") && sd->cook_mastery != val )
  7188. {
  7189. val = cap_value(val, 0, 1999);
  7190. sd->cook_mastery = val;
  7191. }
  7192. sd_reg = sd->save_reg.global;
  7193. max = &sd->save_reg.global_num;
  7194. regmax = GLOBAL_REG_NUM;
  7195. break;
  7196. case 2: //Account reg
  7197. if( !strcmp(reg,"#CASHPOINTS") && sd->cashPoints != val )
  7198. {
  7199. val = cap_value(val, 0, MAX_ZENY);
  7200. sd->cashPoints = val;
  7201. }
  7202. else if( !strcmp(reg,"#KAFRAPOINTS") && sd->kafraPoints != val )
  7203. {
  7204. val = cap_value(val, 0, MAX_ZENY);
  7205. sd->kafraPoints = val;
  7206. }
  7207. sd_reg = sd->save_reg.account;
  7208. max = &sd->save_reg.account_num;
  7209. regmax = ACCOUNT_REG_NUM;
  7210. break;
  7211. case 1: //Account2 reg
  7212. sd_reg = sd->save_reg.account2;
  7213. max = &sd->save_reg.account2_num;
  7214. regmax = ACCOUNT_REG2_NUM;
  7215. break;
  7216. default:
  7217. return 0;
  7218. }
  7219. if (*max == -1) {
  7220. ShowError("pc_setregistry : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7221. return 1;
  7222. }
  7223. // delete reg
  7224. if (val == 0) {
  7225. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7226. if( i < *max )
  7227. {
  7228. if (i != *max - 1)
  7229. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7230. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7231. (*max)--;
  7232. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7233. }
  7234. return 1;
  7235. }
  7236. // change value if found
  7237. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7238. if( i < *max )
  7239. {
  7240. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7241. sd->state.reg_dirty |= 1<<(type-1);
  7242. return 1;
  7243. }
  7244. // add value if not found
  7245. if (i < regmax) {
  7246. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7247. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7248. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7249. (*max)++;
  7250. sd->state.reg_dirty |= 1<<(type-1);
  7251. return 1;
  7252. }
  7253. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7254. return 0;
  7255. }
  7256. int pc_setregistry_str(struct map_session_data *sd,const char *reg,const char *val,int type)
  7257. {
  7258. struct global_reg *sd_reg;
  7259. int i,*max, regmax;
  7260. nullpo_ret(sd);
  7261. if (reg[strlen(reg)-1] != '$') {
  7262. ShowError("pc_setregistry_str : reg %s must be string (end in '$') to use this!\n", reg);
  7263. return 0;
  7264. }
  7265. switch (type) {
  7266. case 3: //Char reg
  7267. sd_reg = sd->save_reg.global;
  7268. max = &sd->save_reg.global_num;
  7269. regmax = GLOBAL_REG_NUM;
  7270. break;
  7271. case 2: //Account reg
  7272. sd_reg = sd->save_reg.account;
  7273. max = &sd->save_reg.account_num;
  7274. regmax = ACCOUNT_REG_NUM;
  7275. break;
  7276. case 1: //Account2 reg
  7277. sd_reg = sd->save_reg.account2;
  7278. max = &sd->save_reg.account2_num;
  7279. regmax = ACCOUNT_REG2_NUM;
  7280. break;
  7281. default:
  7282. return 0;
  7283. }
  7284. if (*max == -1) {
  7285. ShowError("pc_setregistry_str : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7286. return 0;
  7287. }
  7288. // delete reg
  7289. if (!val || strcmp(val,"")==0)
  7290. {
  7291. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7292. if( i < *max )
  7293. {
  7294. if (i != *max - 1)
  7295. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7296. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7297. (*max)--;
  7298. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7299. if (type!=3) intif_saveregistry(sd,type);
  7300. }
  7301. return 1;
  7302. }
  7303. // change value if found
  7304. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7305. if( i < *max )
  7306. {
  7307. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7308. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7309. if (type!=3) intif_saveregistry(sd,type);
  7310. return 1;
  7311. }
  7312. // add value if not found
  7313. if (i < regmax) {
  7314. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7315. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7316. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7317. (*max)++;
  7318. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7319. if (type!=3) intif_saveregistry(sd,type);
  7320. return 1;
  7321. }
  7322. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7323. return 0;
  7324. }
  7325. /*==========================================
  7326. * Exec eventtimer for player sd (retrieved from map_session (id))
  7327. *------------------------------------------*/
  7328. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  7329. {
  7330. struct map_session_data *sd=map_id2sd(id);
  7331. char *p = (char *)data;
  7332. int i;
  7333. if(sd==NULL)
  7334. return 0;
  7335. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  7336. if( i < MAX_EVENTTIMER )
  7337. {
  7338. sd->eventtimer[i] = INVALID_TIMER;
  7339. sd->eventcount--;
  7340. npc_event(sd,p,0);
  7341. }
  7342. else
  7343. ShowError("pc_eventtimer: no such event timer\n");
  7344. if (p) aFree(p);
  7345. return 0;
  7346. }
  7347. /*==========================================
  7348. * Add eventtimer for player sd ?
  7349. *------------------------------------------*/
  7350. int pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  7351. {
  7352. int i;
  7353. nullpo_ret(sd);
  7354. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  7355. if( i == MAX_EVENTTIMER )
  7356. return 0;
  7357. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  7358. sd->eventcount++;
  7359. return 1;
  7360. }
  7361. /*==========================================
  7362. * Del eventtimer for player sd ?
  7363. *------------------------------------------*/
  7364. int pc_deleventtimer(struct map_session_data *sd,const char *name)
  7365. {
  7366. char* p = NULL;
  7367. int i;
  7368. nullpo_ret(sd);
  7369. if (sd->eventcount <= 0)
  7370. return 0;
  7371. // find the named event timer
  7372. ARR_FIND( 0, MAX_EVENTTIMER, i,
  7373. sd->eventtimer[i] != INVALID_TIMER &&
  7374. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  7375. strcmp(p, name) == 0
  7376. );
  7377. if( i == MAX_EVENTTIMER )
  7378. return 0; // not found
  7379. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7380. sd->eventtimer[i] = INVALID_TIMER;
  7381. sd->eventcount--;
  7382. aFree(p);
  7383. return 1;
  7384. }
  7385. /*==========================================
  7386. * Update eventtimer count for player sd
  7387. *------------------------------------------*/
  7388. int pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  7389. {
  7390. int i;
  7391. nullpo_ret(sd);
  7392. for(i=0;i<MAX_EVENTTIMER;i++)
  7393. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  7394. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  7395. addtick_timer(sd->eventtimer[i],tick);
  7396. break;
  7397. }
  7398. return 0;
  7399. }
  7400. /*==========================================
  7401. * Remove all eventtimer for player sd
  7402. *------------------------------------------*/
  7403. int pc_cleareventtimer(struct map_session_data *sd)
  7404. {
  7405. int i;
  7406. nullpo_ret(sd);
  7407. if (sd->eventcount <= 0)
  7408. return 0;
  7409. for(i=0;i<MAX_EVENTTIMER;i++)
  7410. if( sd->eventtimer[i] != INVALID_TIMER ){
  7411. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  7412. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7413. sd->eventtimer[i] = INVALID_TIMER;
  7414. sd->eventcount--;
  7415. if (p) aFree(p);
  7416. }
  7417. return 0;
  7418. }
  7419. /* called when a item with combo is worn */
  7420. int pc_checkcombo(struct map_session_data *sd, struct item_data *data ) {
  7421. int i, j, k, z;
  7422. int index, idx, success = 0;
  7423. for( i = 0; i < data->combos_count; i++ ) {
  7424. /* ensure this isn't a duplicate combo */
  7425. if( sd->combos.bonus != NULL ) {
  7426. int x;
  7427. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7428. /* found a match, skip this combo */
  7429. if( x < sd->combos.count )
  7430. continue;
  7431. }
  7432. for( j = 0; j < data->combos[i]->count; j++ ) {
  7433. int id = data->combos[i]->nameid[j];
  7434. bool found = false;
  7435. for( k = 0; k < EQI_MAX; k++ ) {
  7436. index = sd->equip_index[k];
  7437. if( index < 0 ) continue;
  7438. if( k == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  7439. if( k == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  7440. if( k == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  7441. if(!sd->inventory_data[index])
  7442. continue;
  7443. if ( itemdb_type(id) != IT_CARD ) {
  7444. if ( sd->inventory_data[index]->nameid != id )
  7445. continue;
  7446. found = true;
  7447. break;
  7448. } else { //Cards
  7449. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  7450. continue;
  7451. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  7452. if (sd->status.inventory[index].card[z] != id)
  7453. continue;
  7454. // We have found a match
  7455. found = true;
  7456. break;
  7457. }
  7458. }
  7459. }
  7460. if( !found )
  7461. break;/* we haven't found all the ids for this combo, so we can return */
  7462. }
  7463. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  7464. if( j < data->combos[i]->count )
  7465. continue;
  7466. /* we got here, means all items in the combo are matching */
  7467. idx = sd->combos.count;
  7468. if( sd->combos.bonus == NULL ) {
  7469. CREATE(sd->combos.bonus, struct script_code *, 1);
  7470. CREATE(sd->combos.id, unsigned short, 1);
  7471. sd->combos.count = 1;
  7472. } else {
  7473. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  7474. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  7475. }
  7476. /* we simply copy the pointer */
  7477. sd->combos.bonus[idx] = data->combos[i]->script;
  7478. /* save this combo's id */
  7479. sd->combos.id[idx] = data->combos[i]->id;
  7480. success++;
  7481. }
  7482. return success;
  7483. }
  7484. /* called when a item with combo is removed */
  7485. int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  7486. int i, retval = 0;
  7487. if( sd->combos.bonus == NULL )
  7488. return 0;/* nothing to do here, player has no combos */
  7489. for( i = 0; i < data->combos_count; i++ ) {
  7490. /* check if this combo exists in this user */
  7491. int x = 0, cursor = 0, j;
  7492. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7493. /* no match, skip this combo */
  7494. if( !(x < sd->combos.count) )
  7495. continue;
  7496. sd->combos.bonus[x] = NULL;
  7497. sd->combos.id[x] = 0;
  7498. retval++;
  7499. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  7500. if( sd->combos.bonus[j] == NULL )
  7501. continue;
  7502. if( cursor != j ) {
  7503. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  7504. sd->combos.id[cursor] = sd->combos.id[j];
  7505. }
  7506. cursor++;
  7507. }
  7508. /* check if combo requirements still fit */
  7509. if( pc_checkcombo( sd, data ) )
  7510. continue;
  7511. /* it's empty, we can clear all the memory */
  7512. if( (sd->combos.count = cursor) == 0 ) {
  7513. aFree(sd->combos.bonus);
  7514. aFree(sd->combos.id);
  7515. sd->combos.bonus = NULL;
  7516. sd->combos.id = NULL;
  7517. return retval; /* we also can return at this point for we have no more combos to check */
  7518. }
  7519. }
  7520. return retval;
  7521. }
  7522. int pc_load_combo(struct map_session_data *sd) {
  7523. int i, ret = 0;
  7524. for( i = 0; i < EQI_MAX; i++ ) {
  7525. struct item_data *id = NULL;
  7526. int idx = sd->equip_index[i];
  7527. if( sd->equip_index[i] < 0 || !(id = sd->inventory_data[idx] ) )
  7528. continue;
  7529. if( id->combos_count )
  7530. ret += pc_checkcombo(sd,id);
  7531. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  7532. struct item_data *data;
  7533. int j;
  7534. for( j = 0; j < id->slot; j++ ) {
  7535. if (!sd->status.inventory[idx].card[j])
  7536. continue;
  7537. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  7538. if( data->combos_count )
  7539. ret += pc_checkcombo(sd,data);
  7540. }
  7541. }
  7542. }
  7543. }
  7544. return ret;
  7545. }
  7546. /*==========================================
  7547. * Equip item on player sd at req_pos from inventory index n
  7548. *------------------------------------------*/
  7549. int pc_equipitem(struct map_session_data *sd,int n,int req_pos)
  7550. {
  7551. int i,pos,flag=0,iflag;
  7552. struct item_data *id;
  7553. nullpo_ret(sd);
  7554. if( n < 0 || n >= MAX_INVENTORY ) {
  7555. clif_equipitemack(sd,0,0,0);
  7556. return 0;
  7557. }
  7558. if( sd->sc.count && sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON ) {
  7559. clif_equipitemack(sd,0,0,0);
  7560. return 0;
  7561. }
  7562. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  7563. clif_equipitemack(sd,n,0,0);
  7564. return 0;
  7565. }
  7566. id = sd->inventory_data[n];
  7567. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  7568. if(battle_config.battle_log)
  7569. ShowInfo("equip %d(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  7570. if(!pc_isequip(sd,n) || !(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  7571. // FIXME: pc_isequip: equip level failure uses 2 instead of 0
  7572. clif_equipitemack(sd,n,0,0); // fail
  7573. return 0;
  7574. }
  7575. if (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER]) {
  7576. clif_equipitemack(sd,n,0,0); // fail
  7577. return 0;
  7578. }
  7579. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  7580. pos = req_pos&EQP_ACC;
  7581. if (pos == EQP_ACC) //User specified both slots..
  7582. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  7583. }
  7584. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  7585. pos = (req_pos&EQP_ARMS);
  7586. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  7587. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  7588. }
  7589. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  7590. //Update skill-block range database when weapon range changes. [Skotlex]
  7591. i = sd->equip_index[EQI_HAND_R];
  7592. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  7593. flag = 1;
  7594. else
  7595. flag = id->range != sd->inventory_data[i]->range;
  7596. }
  7597. for(i=0;i<EQI_MAX;i++) {
  7598. if(pos & equip_pos[i]) {
  7599. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  7600. pc_unequipitem(sd,sd->equip_index[i],2);
  7601. sd->equip_index[i] = n;
  7602. }
  7603. }
  7604. if(pos==EQP_AMMO) {
  7605. clif_arrowequip(sd,n);
  7606. clif_arrow_fail(sd,3);
  7607. }
  7608. else
  7609. clif_equipitemack(sd,n,pos,1);
  7610. sd->status.inventory[n].equip=pos;
  7611. if(pos & EQP_HAND_R) {
  7612. if(id)
  7613. sd->weapontype1 = id->look;
  7614. else
  7615. sd->weapontype1 = 0;
  7616. pc_calcweapontype(sd);
  7617. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7618. }
  7619. if(pos & EQP_HAND_L) {
  7620. if(id) {
  7621. if(id->type == IT_WEAPON) {
  7622. sd->status.shield = 0;
  7623. sd->weapontype2 = id->look;
  7624. }
  7625. else
  7626. if(id->type == IT_ARMOR) {
  7627. sd->status.shield = id->look;
  7628. sd->weapontype2 = 0;
  7629. }
  7630. }
  7631. else
  7632. sd->status.shield = sd->weapontype2 = 0;
  7633. pc_calcweapontype(sd);
  7634. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7635. }
  7636. //Added check to prevent sending the same look on multiple slots ->
  7637. //causes client to redraw item on top of itself. (suggested by Lupus)
  7638. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  7639. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  7640. sd->status.head_bottom = id->look;
  7641. else
  7642. sd->status.head_bottom = 0;
  7643. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7644. }
  7645. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  7646. if(id)
  7647. sd->status.head_top = id->look;
  7648. else
  7649. sd->status.head_top = 0;
  7650. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7651. }
  7652. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  7653. if(id && !(pos&EQP_HEAD_TOP))
  7654. sd->status.head_mid = id->look;
  7655. else
  7656. sd->status.head_mid = 0;
  7657. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7658. }
  7659. if(pos & EQP_COSTUME_HEAD_TOP) {
  7660. if(id){
  7661. sd->status.head_top = id->look;
  7662. } else
  7663. sd->status.head_top = 0;
  7664. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7665. }
  7666. if(pos & EQP_COSTUME_HEAD_MID) {
  7667. if(id && !(pos&EQP_HEAD_TOP)){
  7668. sd->status.head_mid = id->look;
  7669. } else
  7670. sd->status.head_mid = 0;
  7671. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7672. }
  7673. if(pos & EQP_COSTUME_HEAD_LOW) {
  7674. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  7675. sd->status.head_bottom = id->look;
  7676. } else
  7677. sd->status.head_bottom = 0;
  7678. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7679. }
  7680. if(pos & EQP_SHOES)
  7681. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7682. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7683. sd->status.robe = id ? id->look : 0;
  7684. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  7685. }
  7686. if(pos & EQP_COSTUME_GARMENT) {
  7687. sd->status.robe = id ? id->look : 0;
  7688. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7689. }
  7690. pc_checkallowskill(sd); //Check if status changes should be halted.
  7691. iflag = sd->npc_item_flag;
  7692. /* check for combos (MUST be before status_calc_pc) */
  7693. if ( id ) {
  7694. if( id->combos_count )
  7695. pc_checkcombo(sd,id);
  7696. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7697. ; //No cards
  7698. else {
  7699. for( i = 0; i < id->slot; i++ ) {
  7700. struct item_data *data;
  7701. if (!sd->status.inventory[n].card[i])
  7702. continue;
  7703. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7704. if( data->combos_count )
  7705. pc_checkcombo(sd,data);
  7706. }
  7707. }
  7708. }
  7709. }
  7710. status_calc_pc(sd,0);
  7711. if (flag) //Update skill data
  7712. clif_skillinfoblock(sd);
  7713. //OnEquip script [Skotlex]
  7714. if (id) {
  7715. if (id->equip_script)
  7716. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7717. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7718. ; //No cards
  7719. else {
  7720. for( i = 0; i < id->slot; i++ ) {
  7721. struct item_data *data;
  7722. if (!sd->status.inventory[n].card[i])
  7723. continue;
  7724. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7725. if( data->equip_script )
  7726. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7727. }
  7728. }
  7729. }
  7730. }
  7731. sd->npc_item_flag = iflag;
  7732. return 0;
  7733. }
  7734. /*==========================================
  7735. * Called when attemting to unequip an item from player
  7736. * type:
  7737. * 0 - only unequip
  7738. * 1 - calculate status after unequipping
  7739. * 2 - force unequip
  7740. *------------------------------------------*/
  7741. int pc_unequipitem(struct map_session_data *sd,int n,int flag) {
  7742. int i,iflag;
  7743. bool status_cacl = false;
  7744. nullpo_ret(sd);
  7745. if( n < 0 || n >= MAX_INVENTORY ) {
  7746. clif_unequipitemack(sd,0,0,0);
  7747. return 0;
  7748. }
  7749. // status change that makes player cannot unequip equipment
  7750. if( !(flag&2) && sd->sc.count && (
  7751. sd->sc.data[SC_BERSERK] ||
  7752. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  7753. sd->sc.data[SC__BLOODYLUST] ||
  7754. sd->sc.data[SC_KYOUGAKU] ||
  7755. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) // can't switch weapon
  7756. {
  7757. clif_unequipitemack(sd,n,0,0);
  7758. return 0;
  7759. }
  7760. if(battle_config.battle_log)
  7761. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  7762. if(!sd->status.inventory[n].equip){ //Nothing to unequip
  7763. clif_unequipitemack(sd,n,0,0);
  7764. return 0;
  7765. }
  7766. for(i=0;i<EQI_MAX;i++) {
  7767. if(sd->status.inventory[n].equip & equip_pos[i])
  7768. sd->equip_index[i] = -1;
  7769. }
  7770. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  7771. sd->weapontype1 = 0;
  7772. sd->status.weapon = sd->weapontype2;
  7773. pc_calcweapontype(sd);
  7774. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7775. if( !battle_config.dancing_weaponswitch_fix )
  7776. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  7777. }
  7778. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  7779. sd->status.shield = sd->weapontype2 = 0;
  7780. pc_calcweapontype(sd);
  7781. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7782. }
  7783. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  7784. sd->status.head_bottom = 0;
  7785. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7786. }
  7787. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  7788. sd->status.head_top = 0;
  7789. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7790. }
  7791. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  7792. sd->status.head_mid = 0;
  7793. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7794. }
  7795. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  7796. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  7797. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7798. }
  7799. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  7800. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  7801. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7802. }
  7803. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  7804. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  7805. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7806. }
  7807. if(sd->status.inventory[n].equip & EQP_SHOES)
  7808. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7809. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7810. sd->status.robe = 0;
  7811. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  7812. }
  7813. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  7814. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  7815. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7816. }
  7817. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  7818. if((sd->status.inventory[n].equip & EQP_ARMS) &&
  7819. sd->weapontype1 == 0 && sd->weapontype2 == 0 && (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
  7820. skill_enchant_elemental_end(&sd->bl,-1);
  7821. if(sd->status.inventory[n].equip & EQP_ARMOR) {
  7822. // On Armor Change...
  7823. status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
  7824. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  7825. }
  7826. if( sd->state.autobonus&sd->status.inventory[n].equip )
  7827. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  7828. sd->status.inventory[n].equip=0;
  7829. iflag = sd->npc_item_flag;
  7830. /* check for combos (MUST be before status_calc_pc) */
  7831. if ( sd->inventory_data[n] ) {
  7832. if( sd->inventory_data[n]->combos_count ) {
  7833. if( pc_removecombo(sd,sd->inventory_data[n]) )
  7834. status_cacl = true;
  7835. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7836. ; //No cards
  7837. else {
  7838. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7839. struct item_data *data;
  7840. if (!sd->status.inventory[n].card[i])
  7841. continue;
  7842. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7843. if( data->combos_count ) {
  7844. if( pc_removecombo(sd,data) )
  7845. status_cacl = true;
  7846. }
  7847. }
  7848. }
  7849. }
  7850. }
  7851. if(flag&1 || status_cacl) {
  7852. pc_checkallowskill(sd);
  7853. status_calc_pc(sd,0);
  7854. }
  7855. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  7856. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  7857. //OnUnEquip script [Skotlex]
  7858. if (sd->inventory_data[n]) {
  7859. if (sd->inventory_data[n]->unequip_script)
  7860. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7861. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7862. ; //No cards
  7863. else {
  7864. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7865. struct item_data *data;
  7866. if (!sd->status.inventory[n].card[i])
  7867. continue;
  7868. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7869. if( data->unequip_script )
  7870. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7871. }
  7872. }
  7873. }
  7874. }
  7875. sd->npc_item_flag = iflag;
  7876. return 0;
  7877. }
  7878. /*==========================================
  7879. * Checking if player (sd) have unauthorize, invalide item
  7880. * on inventory, cart, equiped for the map (item_noequip)
  7881. *------------------------------------------*/
  7882. int pc_checkitem(struct map_session_data *sd)
  7883. {
  7884. int i,id,calc_flag = 0;
  7885. nullpo_ret(sd);
  7886. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  7887. return 0;
  7888. if( battle_config.item_check ) {// check for invalid(ated) items
  7889. for( i = 0; i < MAX_INVENTORY; i++ ) {
  7890. id = sd->status.inventory[i].nameid;
  7891. if( id && !itemdb_available(id) ) {
  7892. ShowWarning("Removed invalid/disabled item id %d from inventory (amount=%d, char_id=%d).\n", id, sd->status.inventory[i].amount, sd->status.char_id);
  7893. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  7894. }
  7895. }
  7896. for( i = 0; i < MAX_CART; i++ ) {
  7897. id = sd->status.cart[i].nameid;
  7898. if( id && !itemdb_available(id) ) {
  7899. ShowWarning("Removed invalid/disabled item id %d from cart (amount=%d, char_id=%d).\n", id, sd->status.cart[i].amount, sd->status.char_id);
  7900. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  7901. }
  7902. }
  7903. }
  7904. for( i = 0; i < MAX_INVENTORY; i++) {
  7905. if( sd->status.inventory[i].nameid == 0 )
  7906. continue;
  7907. if( !sd->status.inventory[i].equip )
  7908. continue;
  7909. if( sd->status.inventory[i].equip&~pc_equippoint(sd,i) ) {
  7910. pc_unequipitem(sd, i, 2);
  7911. calc_flag = 1;
  7912. continue;
  7913. }
  7914. }
  7915. if( calc_flag && sd->state.active ) {
  7916. pc_checkallowskill(sd);
  7917. status_calc_pc(sd,0);
  7918. }
  7919. return 0;
  7920. }
  7921. /*==========================================
  7922. * Update PVP rank for sd1 in cmp to sd2
  7923. *------------------------------------------*/
  7924. int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  7925. {
  7926. struct map_session_data *sd1,*sd2;
  7927. sd1=(struct map_session_data *)bl;
  7928. sd2=va_arg(ap,struct map_session_data *);
  7929. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  7930. {// cannot register pvp rank for hidden GMs
  7931. return 0;
  7932. }
  7933. if( sd1->pvp_point > sd2->pvp_point )
  7934. sd2->pvp_rank++;
  7935. return 0;
  7936. }
  7937. /*==========================================
  7938. * Calculate new rank beetween all present players (map_foreachinarea)
  7939. * and display result
  7940. *------------------------------------------*/
  7941. int pc_calc_pvprank(struct map_session_data *sd)
  7942. {
  7943. int old;
  7944. struct map_data *m;
  7945. m=&map[sd->bl.m];
  7946. old=sd->pvp_rank;
  7947. sd->pvp_rank=1;
  7948. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  7949. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  7950. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  7951. return sd->pvp_rank;
  7952. }
  7953. /*==========================================
  7954. * Calculate next sd ranking calculation from config
  7955. *------------------------------------------*/
  7956. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  7957. {
  7958. struct map_session_data *sd;
  7959. sd=map_id2sd(id);
  7960. if(sd==NULL)
  7961. return 0;
  7962. sd->pvp_timer = INVALID_TIMER;
  7963. if( sd->sc.option&OPTION_INVISIBLE )
  7964. {// do not calculate the pvp rank for a hidden GM
  7965. return 0;
  7966. }
  7967. if( pc_calc_pvprank(sd) > 0 )
  7968. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  7969. return 0;
  7970. }
  7971. /*==========================================
  7972. * Checking if sd is married
  7973. * Return:
  7974. * partner_id = yes
  7975. * 0 = no
  7976. *------------------------------------------*/
  7977. int pc_ismarried(struct map_session_data *sd)
  7978. {
  7979. if(sd == NULL)
  7980. return -1;
  7981. if(sd->status.partner_id > 0)
  7982. return sd->status.partner_id;
  7983. else
  7984. return 0;
  7985. }
  7986. /*==========================================
  7987. * Marry player sd to player dstsd
  7988. * Return:
  7989. * -1 = fail
  7990. * 0 = success
  7991. *------------------------------------------*/
  7992. int pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  7993. {
  7994. if(sd == NULL || dstsd == NULL ||
  7995. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  7996. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  7997. return -1;
  7998. sd->status.partner_id = dstsd->status.char_id;
  7999. dstsd->status.partner_id = sd->status.char_id;
  8000. return 0;
  8001. }
  8002. /*==========================================
  8003. * Divorce sd from its partner
  8004. * Return:
  8005. * -1 = fail
  8006. * 0 = success
  8007. *------------------------------------------*/
  8008. int pc_divorce(struct map_session_data *sd)
  8009. {
  8010. struct map_session_data *p_sd;
  8011. int i;
  8012. if( sd == NULL || !pc_ismarried(sd) )
  8013. return -1;
  8014. if( !sd->status.partner_id )
  8015. return -1; // Char is not married
  8016. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8017. { // Lets char server do the divorce
  8018. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8019. return -1; // No char server connected
  8020. return 0;
  8021. }
  8022. // Both players online, lets do the divorce manually
  8023. sd->status.partner_id = 0;
  8024. p_sd->status.partner_id = 0;
  8025. for( i = 0; i < MAX_INVENTORY; i++ )
  8026. {
  8027. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8028. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8029. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8030. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8031. }
  8032. clif_divorced(sd, p_sd->status.name);
  8033. clif_divorced(p_sd, sd->status.name);
  8034. return 0;
  8035. }
  8036. /*==========================================
  8037. * Get sd partner charid. (Married partner)
  8038. *------------------------------------------*/
  8039. struct map_session_data *pc_get_partner(struct map_session_data *sd)
  8040. {
  8041. if (sd && pc_ismarried(sd))
  8042. // charid2sd returns NULL if not found
  8043. return map_charid2sd(sd->status.partner_id);
  8044. return NULL;
  8045. }
  8046. /*==========================================
  8047. * Get sd father charid. (Need to be baby)
  8048. *------------------------------------------*/
  8049. struct map_session_data *pc_get_father (struct map_session_data *sd)
  8050. {
  8051. if (sd && sd->class_&JOBL_BABY && sd->status.father > 0)
  8052. // charid2sd returns NULL if not found
  8053. return map_charid2sd(sd->status.father);
  8054. return NULL;
  8055. }
  8056. /*==========================================
  8057. * Get sd mother charid. (Need to be baby)
  8058. *------------------------------------------*/
  8059. struct map_session_data *pc_get_mother (struct map_session_data *sd)
  8060. {
  8061. if (sd && sd->class_&JOBL_BABY && sd->status.mother > 0)
  8062. // charid2sd returns NULL if not found
  8063. return map_charid2sd(sd->status.mother);
  8064. return NULL;
  8065. }
  8066. /*==========================================
  8067. * Get sd children charid. (Need to be married)
  8068. *------------------------------------------*/
  8069. struct map_session_data *pc_get_child (struct map_session_data *sd)
  8070. {
  8071. if (sd && pc_ismarried(sd) && sd->status.child > 0)
  8072. // charid2sd returns NULL if not found
  8073. return map_charid2sd(sd->status.child);
  8074. return NULL;
  8075. }
  8076. /*==========================================
  8077. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  8078. *------------------------------------------*/
  8079. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  8080. {
  8081. int hp = 0, sp = 0;
  8082. if( pc_isdead(sd) )
  8083. return;
  8084. if (sd->hp_loss.value) {
  8085. sd->hp_loss.tick += diff_tick;
  8086. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  8087. hp += sd->hp_loss.value;
  8088. sd->hp_loss.tick -= sd->hp_loss.rate;
  8089. }
  8090. if(hp >= sd->battle_status.hp)
  8091. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  8092. }
  8093. if (sd->sp_loss.value) {
  8094. sd->sp_loss.tick += diff_tick;
  8095. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  8096. sp += sd->sp_loss.value;
  8097. sd->sp_loss.tick -= sd->sp_loss.rate;
  8098. }
  8099. }
  8100. if (hp > 0 || sp > 0)
  8101. status_zap(&sd->bl, hp, sp);
  8102. return;
  8103. }
  8104. //Character regen. Flag is used to know which types of regen can take place.
  8105. //&1: HP regen
  8106. //&2: SP regen
  8107. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  8108. {
  8109. int hp = 0, sp = 0;
  8110. if (sd->hp_regen.value) {
  8111. sd->hp_regen.tick += diff_tick;
  8112. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  8113. hp += sd->hp_regen.value;
  8114. sd->hp_regen.tick -= sd->hp_regen.rate;
  8115. }
  8116. }
  8117. if (sd->sp_regen.value) {
  8118. sd->sp_regen.tick += diff_tick;
  8119. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  8120. sp += sd->sp_regen.value;
  8121. sd->sp_regen.tick -= sd->sp_regen.rate;
  8122. }
  8123. }
  8124. if (hp > 0 || sp > 0)
  8125. status_heal(&sd->bl, hp, sp, 0);
  8126. return;
  8127. }
  8128. /*==========================================
  8129. * Memo player sd savepoint. (map,x,y)
  8130. *------------------------------------------*/
  8131. int pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  8132. {
  8133. nullpo_ret(sd);
  8134. sd->status.save_point.map = mapindex;
  8135. sd->status.save_point.x = x;
  8136. sd->status.save_point.y = y;
  8137. return 0;
  8138. }
  8139. /*==========================================
  8140. * Save 1 player data at autosave intervalle
  8141. *------------------------------------------*/
  8142. int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  8143. {
  8144. int interval;
  8145. struct s_mapiterator* iter;
  8146. struct map_session_data* sd;
  8147. static int last_save_id = 0, save_flag = 0;
  8148. if(save_flag == 2) //Someone was saved on last call, normal cycle
  8149. save_flag = 0;
  8150. else
  8151. save_flag = 1; //Noone was saved, so save first found char.
  8152. iter = mapit_getallusers();
  8153. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8154. {
  8155. if(sd->bl.id == last_save_id && save_flag != 1) {
  8156. save_flag = 1;
  8157. continue;
  8158. }
  8159. if(save_flag != 1) //Not our turn to save yet.
  8160. continue;
  8161. //Save char.
  8162. last_save_id = sd->bl.id;
  8163. save_flag = 2;
  8164. chrif_save(sd,0);
  8165. break;
  8166. }
  8167. mapit_free(iter);
  8168. interval = autosave_interval/(map_usercount()+1);
  8169. if(interval < minsave_interval)
  8170. interval = minsave_interval;
  8171. add_timer(gettick()+interval,pc_autosave,0,0);
  8172. return 0;
  8173. }
  8174. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  8175. {
  8176. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  8177. { //Night/day state does not match.
  8178. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  8179. sd->state.night = night_flag;
  8180. return 1;
  8181. }
  8182. return 0;
  8183. }
  8184. /*================================================
  8185. * timer to do the day [Yor]
  8186. * data: 0 = called by timer, 1 = gmcommand/script
  8187. *------------------------------------------------*/
  8188. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  8189. {
  8190. char tmp_soutput[1024];
  8191. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  8192. return 0;
  8193. if (!night_flag)
  8194. return 0; //Already day.
  8195. night_flag = 0; // 0=day, 1=night [Yor]
  8196. map_foreachpc(pc_daynight_timer_sub);
  8197. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  8198. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8199. return 0;
  8200. }
  8201. /*================================================
  8202. * timer to do the night [Yor]
  8203. * data: 0 = called by timer, 1 = gmcommand/script
  8204. *------------------------------------------------*/
  8205. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  8206. {
  8207. char tmp_soutput[1024];
  8208. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  8209. return 0;
  8210. if (night_flag)
  8211. return 0; //Already nigth.
  8212. night_flag = 1; // 0=day, 1=night [Yor]
  8213. map_foreachpc(pc_daynight_timer_sub);
  8214. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  8215. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8216. return 0;
  8217. }
  8218. void pc_setstand(struct map_session_data *sd){
  8219. nullpo_retv(sd);
  8220. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  8221. clif_status_load(&sd->bl,SI_SIT,0);
  8222. clif_standing(&sd->bl); //Inform area PC is standing
  8223. //Reset sitting tick.
  8224. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  8225. sd->state.dead_sit = sd->vd.dead_sit = 0;
  8226. }
  8227. /**
  8228. * Mechanic (MADO GEAR)
  8229. **/
  8230. void pc_overheat(struct map_session_data *sd, int val) {
  8231. int heat = val, skill,
  8232. limit[] = { 10, 20, 28, 46, 66 };
  8233. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  8234. return; // already burning
  8235. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  8236. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  8237. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  8238. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8239. }
  8240. heat = max(0,heat); // Avoid negative HEAT
  8241. if( heat >= limit[skill] )
  8242. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  8243. else
  8244. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  8245. return;
  8246. }
  8247. /**
  8248. * Check if player is autolooting given itemID.
  8249. */
  8250. bool pc_isautolooting(struct map_session_data *sd, int nameid)
  8251. {
  8252. int i;
  8253. if( !sd->state.autolooting )
  8254. return false;
  8255. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  8256. return (i != AUTOLOOTITEM_SIZE);
  8257. }
  8258. /**
  8259. * Checks if player can use @/#command
  8260. * @param sd Player map session data
  8261. * @param command Command name without @/# and params
  8262. * @param type is it atcommand or charcommand
  8263. */
  8264. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  8265. {
  8266. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  8267. }
  8268. /**
  8269. * Checks if commands used by a player should be logged
  8270. * according to their group setting.
  8271. * @param sd Player map session data
  8272. */
  8273. bool pc_should_log_commands(struct map_session_data *sd)
  8274. {
  8275. return pc_group_should_log_commands(pc_get_group_id(sd));
  8276. }
  8277. static int pc_talisman_timer(int tid, unsigned int tick, int id, intptr_t data)
  8278. {
  8279. struct map_session_data *sd;
  8280. int i, type;
  8281. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  8282. return 1;
  8283. ARR_FIND(1, 5, type, sd->talisman[type] > 0);
  8284. if( sd->talisman[type] <= 0 )
  8285. {
  8286. 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);
  8287. sd->talisman[type] = 0;
  8288. return 0;
  8289. }
  8290. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == tid);
  8291. if( i == sd->talisman[type] )
  8292. {
  8293. ShowError("pc_talisman_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  8294. return 0;
  8295. }
  8296. sd->talisman[type]--;
  8297. if( i != sd->talisman[type] )
  8298. memmove(sd->talisman_timer[type]+i, sd->talisman_timer[type]+i+1, (sd->talisman[type]-i)*sizeof(int));
  8299. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8300. clif_talisman(sd, type);
  8301. return 0;
  8302. }
  8303. int pc_add_talisman(struct map_session_data *sd,int interval,int max,int type)
  8304. {
  8305. int tid, i;
  8306. nullpo_ret(sd);
  8307. if(max > 10)
  8308. max = 10;
  8309. if(sd->talisman[type] < 0)
  8310. sd->talisman[type] = 0;
  8311. if( sd->talisman[type] && sd->talisman[type] >= max )
  8312. {
  8313. if(sd->talisman_timer[type][0] != INVALID_TIMER)
  8314. delete_timer(sd->talisman_timer[type][0],pc_talisman_timer);
  8315. sd->talisman[type]--;
  8316. if( sd->talisman[type] != 0 )
  8317. memmove(sd->talisman_timer[type]+0, sd->talisman_timer[type]+1, (sd->talisman[type])*sizeof(int));
  8318. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8319. }
  8320. tid = add_timer(gettick()+interval, pc_talisman_timer, sd->bl.id, 0);
  8321. 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);
  8322. if( i != sd->talisman[type] )
  8323. memmove(sd->talisman_timer[type]+i+1, sd->talisman_timer[type]+i, (sd->talisman[type]-i)*sizeof(int));
  8324. sd->talisman_timer[type][i] = tid;
  8325. sd->talisman[type]++;
  8326. clif_talisman(sd, type);
  8327. return 0;
  8328. }
  8329. int pc_del_talisman(struct map_session_data *sd,int count,int type)
  8330. {
  8331. int i;
  8332. nullpo_ret(sd);
  8333. if( sd->talisman[type] <= 0 ) {
  8334. sd->talisman[type] = 0;
  8335. return 0;
  8336. }
  8337. if( count <= 0 )
  8338. return 0;
  8339. if( count > sd->talisman[type] )
  8340. count = sd->talisman[type];
  8341. sd->talisman[type] -= count;
  8342. if( count > 10 )
  8343. count = 10;
  8344. for(i = 0; i < count; i++) {
  8345. if(sd->talisman_timer[type][i] != INVALID_TIMER) {
  8346. delete_timer(sd->talisman_timer[type][i],pc_talisman_timer);
  8347. sd->talisman_timer[type][i] = INVALID_TIMER;
  8348. }
  8349. }
  8350. for(i = count; i < 10; i++) {
  8351. sd->talisman_timer[type][i-count] = sd->talisman_timer[type][i];
  8352. sd->talisman_timer[type][i] = INVALID_TIMER;
  8353. }
  8354. clif_talisman(sd, type);
  8355. return 0;
  8356. }
  8357. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8358. /*==========================================
  8359. * Renewal EXP/Itemdrop rate modifier base on level penalty
  8360. * 1=exp 2=itemdrop
  8361. *------------------------------------------*/
  8362. int pc_level_penalty_mod(struct map_session_data *sd, int mob_level, uint32 mob_race, uint32 mob_mode, int type)
  8363. {
  8364. int diff, rate = 100, i;
  8365. nullpo_ret(sd);
  8366. diff = mob_level - sd->status.base_level;
  8367. if( diff < 0 )
  8368. diff = MAX_LEVEL + ( ~diff + 1 );
  8369. for(i=0; i<RC_MAX; i++){
  8370. int tmp;
  8371. if( mob_race != i ){
  8372. if( mob_mode&MD_BOSS && i < RC_BOSS )
  8373. i = RC_BOSS;
  8374. else if( i <= RC_BOSS )
  8375. continue;
  8376. }
  8377. if( (tmp=level_penalty[type][i][diff]) > 0 ){
  8378. rate = tmp;
  8379. break;
  8380. }
  8381. }
  8382. return rate;
  8383. }
  8384. #endif
  8385. int pc_split_str(char *str,char **val,int num)
  8386. {
  8387. int i;
  8388. for (i=0; i<num && str; i++){
  8389. val[i] = str;
  8390. str = strchr(str,',');
  8391. if (str && i<num-1) //Do not remove a trailing comma.
  8392. *str++=0;
  8393. }
  8394. return i;
  8395. }
  8396. int pc_split_atoi(char* str, int* val, char sep, int max)
  8397. {
  8398. int i,j;
  8399. for (i=0; i<max; i++) {
  8400. if (!str) break;
  8401. val[i] = atoi(str);
  8402. str = strchr(str,sep);
  8403. if (str)
  8404. *str++=0;
  8405. }
  8406. //Zero up the remaining.
  8407. for(j=i; j < max; j++)
  8408. val[j] = 0;
  8409. return i;
  8410. }
  8411. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  8412. {
  8413. static int warning=0;
  8414. int i,j;
  8415. double f;
  8416. for (i=0; i<max; i++) {
  8417. if (!str) break;
  8418. f = atof(str);
  8419. if (f < 0)
  8420. val[i] = 0;
  8421. else if (f > UINT_MAX) {
  8422. val[i] = UINT_MAX;
  8423. if (!warning) {
  8424. warning = 1;
  8425. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  8426. }
  8427. } else
  8428. val[i] = (unsigned int)f;
  8429. str = strchr(str,sep);
  8430. if (str)
  8431. *str++=0;
  8432. }
  8433. //Zero up the remaining.
  8434. for(j=i; j < max; j++)
  8435. val[j] = 0;
  8436. return i;
  8437. }
  8438. /*==========================================
  8439. * sub DB reading.
  8440. * Function used to read skill_tree.txt
  8441. *------------------------------------------*/
  8442. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  8443. {
  8444. unsigned char joblv = 0, skill_lv;
  8445. uint16 skill_id;
  8446. int idx, class_;
  8447. unsigned int i, offset = 3, skill_idx;
  8448. class_ = atoi(fields[0]);
  8449. skill_id = (uint16)atoi(fields[1]);
  8450. skill_lv = (unsigned char)atoi(fields[2]);
  8451. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  8452. {// job level requirement extra column
  8453. joblv = (unsigned char)atoi(fields[3]);
  8454. offset++;
  8455. }
  8456. if(!pcdb_checkid(class_))
  8457. {
  8458. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  8459. return false;
  8460. }
  8461. idx = pc_class2idx(class_);
  8462. //This is to avoid adding two lines for the same skill. [Skotlex]
  8463. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  8464. if( skill_idx == MAX_SKILL_TREE )
  8465. {
  8466. 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_);
  8467. return false;
  8468. }
  8469. else if(skill_tree[idx][skill_idx].id)
  8470. {
  8471. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  8472. }
  8473. skill_tree[idx][skill_idx].id = skill_id;
  8474. skill_tree[idx][skill_idx].max = skill_lv;
  8475. skill_tree[idx][skill_idx].joblv = joblv;
  8476. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  8477. {
  8478. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  8479. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  8480. }
  8481. return true;
  8482. }
  8483. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8484. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  8485. {
  8486. int type, race, diff;
  8487. type = atoi(fields[0]); //1=experience, 2=item drop
  8488. race = atoi(fields[1]);
  8489. diff = atoi(fields[2]);
  8490. if( type != 1 && type != 2 ){
  8491. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  8492. return false;
  8493. }
  8494. if( race < 0 || race > RC_MAX ){
  8495. ShowWarning("pc_readdb_levelpenalty: Invalid race %d specified.\n", race);
  8496. return false;
  8497. }
  8498. diff = min(diff, MAX_LEVEL);
  8499. if( diff < 0 )
  8500. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  8501. level_penalty[type][race][diff] = atoi(fields[3]);
  8502. return true;
  8503. }
  8504. #endif
  8505. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  8506. static bool pc_readdb_job1(char* fields[], int columns, int current){
  8507. int idx, class_;
  8508. unsigned int i;
  8509. class_ = atoi(fields[0]);
  8510. if(!pcdb_checkid(class_))
  8511. {
  8512. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  8513. return false;
  8514. }
  8515. idx = pc_class2idx(class_);
  8516. job_info[idx].max_weight_base = atoi(fields[1]);
  8517. job_info[idx].hp_factor = atoi(fields[2]);
  8518. job_info[idx].hp_multiplicator = atoi(fields[3]);
  8519. job_info[idx].sp_factor = atoi(fields[4]);
  8520. #ifdef RENEWAL_ASPD
  8521. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  8522. #else
  8523. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  8524. #endif
  8525. {
  8526. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  8527. }
  8528. return true;
  8529. }
  8530. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  8531. static bool pc_readdb_job2(char* fields[], int columns, int current)
  8532. {
  8533. int idx, class_, i;
  8534. class_ = atoi(fields[0]);
  8535. if(!pcdb_checkid(class_))
  8536. {
  8537. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  8538. return false;
  8539. }
  8540. idx = pc_class2idx(class_);
  8541. for(i = 1; i < columns; i++)
  8542. {
  8543. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  8544. }
  8545. return true;
  8546. }
  8547. //Reading job_maxhpsp.txt line
  8548. //startlvl,maxlvl,class,type,values...
  8549. static bool pc_readdb_job_maxhpsp(char* fields[], int columns, int current)
  8550. {
  8551. int idx, i,j, maxlvl, startlvl;
  8552. int job_id,job_count,jobs[CLASS_COUNT];
  8553. int type;
  8554. startlvl = atoi(fields[0]);
  8555. if(startlvl > MAX_LEVEL || startlvl<1){
  8556. ShowError("pc_readdb_job_maxhpsp: Invalid start level %d specified.\n", startlvl);
  8557. return false;
  8558. }
  8559. maxlvl = atoi(fields[1]);
  8560. if(maxlvl > MAX_LEVEL || maxlvl<1){
  8561. ShowError("pc_readdb_job_maxhpsp: Invalid max level %d specified.\n", maxlvl);
  8562. return false;
  8563. }
  8564. if((maxlvl-startlvl+1+4) != columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  8565. ShowError("pc_readdb_job_maxhpsp: Number of columns %d defined is too low for start level %d, max level %d.\n",columns,startlvl,maxlvl);
  8566. return false;
  8567. }
  8568. type = atoi(fields[3]);
  8569. if(type < 0 || type > 1){
  8570. ShowError("pc_readdb_job_maxhpsp: Invalid type %d specified.\n", type);
  8571. return false;
  8572. }
  8573. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  8574. if (job_count < 1)
  8575. return false;
  8576. for (j = 0; j < job_count; j++) {
  8577. job_id = jobs[j];
  8578. if(!pcdb_checkid(job_id)){
  8579. ShowError("pc_readdb_job_maxhpsp: Invalid job class %d specified.\n", job_id);
  8580. return false;
  8581. }
  8582. idx = pc_class2idx(job_id);
  8583. if(type == 0) { //hp type
  8584. unsigned int k = 0;
  8585. unsigned int val, oldval=0;
  8586. short level = 0;
  8587. for(i = 0; i <= MAX_LEVEL; i++) {
  8588. val = 0;
  8589. k += (job_info[idx].hp_factor*(i+1) + 50) / 100;
  8590. if(i>=startlvl && i <= maxlvl) val = atoi(fields[(i-level)+4]);
  8591. if(val==0) {
  8592. val = 35 + ((i+1)*job_info[idx].hp_multiplicator)/100 + k;
  8593. level++; // This tells us when we didn't use the database so we know what field to grab
  8594. }
  8595. if(oldval > val && i > startlvl && i <= maxlvl) // Let's not warn about the formula table giving us a higher value, only DB
  8596. 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",
  8597. job_id,oldval,val,i+1,job_info[idx].hp_factor,job_info[idx].hp_multiplicator,k);
  8598. val = min(INT_MAX,val);
  8599. job_info[idx].hp_table[i] = val;
  8600. oldval = val;
  8601. }
  8602. // ShowInfo("Finished reading HP table for job %d.\n{",job_id);
  8603. // for(i=0; i<=MAX_LEVEL; i++ )
  8604. // printf("%d,",job_info[idx].hp_table[i]);
  8605. // printf("\n}\n");
  8606. }
  8607. else if(type == 1){ //sp type
  8608. unsigned int val, oldval=0;
  8609. short level = 0;
  8610. for(i = 0; i <= MAX_LEVEL; i++) {
  8611. val = 0;
  8612. if(i>=startlvl && i <=maxlvl) val = atoi(fields[(i-level)+4]);
  8613. if(val==0) {
  8614. val = 10 + ((i+1)*job_info[idx].sp_factor)/100;
  8615. level++; // This tells us when we didn't use the database so we know what field to grab
  8616. }
  8617. if(oldval > val && i > startlvl && i <= maxlvl) // Let's not warn about the formula table giving us a higher value, only DB
  8618. ShowWarning("pc_readdb_job_maxhpsp: SP value is lower than previous level (job=%d,oldval=%d,val=%d,lvl=%d,sp_factor=%d).\n",
  8619. job_id,oldval,val,i+1,job_info[idx].sp_factor);
  8620. val = min(INT_MAX,val);
  8621. job_info[idx].sp_table[i] = val;
  8622. oldval = val;
  8623. }
  8624. // ShowInfo("Finished reading SP table for job %d.\n{",job_id);
  8625. // for(i=0; i<MAX_LEVEL; i++ )
  8626. // printf("%d,",job_info[idx].sp_table[i]);
  8627. // printf("\n}\n");
  8628. }
  8629. }
  8630. return true;
  8631. }
  8632. //Reading job_exp.txt line
  8633. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  8634. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  8635. {
  8636. int idx, i, type;
  8637. int job_id,job_count,jobs[CLASS_COUNT];
  8638. unsigned int ui, maxlvl;
  8639. maxlvl = atoi(fields[0]);
  8640. if(maxlvl > MAX_LEVEL || maxlvl<1){
  8641. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  8642. return false;
  8643. }
  8644. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  8645. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  8646. return false;
  8647. }
  8648. type = atoi(fields[2]);
  8649. if(type < 0 || type > 1){
  8650. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  8651. return false;
  8652. }
  8653. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  8654. if (job_count < 1)
  8655. return false;
  8656. job_id = jobs[0];
  8657. if(!pcdb_checkid(job_id)){
  8658. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  8659. return false;
  8660. }
  8661. idx = pc_class2idx(job_id);
  8662. job_info[idx].max_level[type] = maxlvl;
  8663. for(i=0; i<maxlvl; i++){
  8664. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  8665. }
  8666. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  8667. //The reasoning behind the -2 is this... if the max level is 5, then the array
  8668. //should look like this:
  8669. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  8670. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  8671. job_info[idx].max_level[type]--;
  8672. if (job_info[idx].max_level[type] < maxlvl) {
  8673. 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]);
  8674. ShowInfo("Filling the missing values with the last exp entry.\n");
  8675. //Fill the requested values with the last entry.
  8676. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  8677. for (; ui+2 < maxlvl; ui++)
  8678. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  8679. job_info[idx].max_level[type] = maxlvl;
  8680. }
  8681. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  8682. for (i = 1; i < job_count; i++) {
  8683. job_id = jobs[i];
  8684. if (!pcdb_checkid(job_id)) {
  8685. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  8686. continue;
  8687. }
  8688. idx = pc_class2idx(job_id);
  8689. 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]));
  8690. job_info[idx].max_level[type] = maxlvl;
  8691. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  8692. }
  8693. return true;
  8694. }
  8695. /*==========================================
  8696. * pc DB reading.
  8697. * job_exp.txt - required experience values
  8698. * skill_tree.txt - skill tree for every class
  8699. * attr_fix.txt - elemental adjustment table
  8700. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  8701. * job_db2.txt - job,stats bonuses/lvl
  8702. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  8703. *------------------------------------------*/
  8704. int pc_readdb(void)
  8705. {
  8706. int i,k, entries;
  8707. FILE *fp;
  8708. char line[24000];
  8709. //reset
  8710. memset(job_info,0,sizeof(job_info)); // job_info table
  8711. // Reset and read skilltree
  8712. memset(skill_tree,0,sizeof(skill_tree));
  8713. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree);
  8714. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8715. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty);
  8716. for( k=1; k < 3; k++ ){ // fill in the blanks
  8717. int j;
  8718. for( j = 0; j < RC_MAX; j++ ){
  8719. int tmp = 0;
  8720. for( i = 0; i < MAX_LEVEL*2; i++ ){
  8721. if( i == MAX_LEVEL+1 )
  8722. tmp = level_penalty[k][j][0];// reset
  8723. if( level_penalty[k][j][i] > 0 )
  8724. tmp = level_penalty[k][j][i];
  8725. else
  8726. level_penalty[k][j][i] = tmp;
  8727. }
  8728. }
  8729. }
  8730. #endif
  8731. // reset then read statspoint
  8732. memset(statp,0,sizeof(statp));
  8733. i=1;
  8734. sprintf(line, "%s/"DBPATH"statpoint.txt", db_path);
  8735. fp=fopen(line,"r");
  8736. if(fp == NULL){
  8737. ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  8738. //return 1;
  8739. } else {
  8740. entries=0;
  8741. while(fgets(line, sizeof(line), fp))
  8742. {
  8743. int stat;
  8744. if(line[0]=='/' && line[1]=='/')
  8745. continue;
  8746. if ((stat=strtoul(line,NULL,10))<0)
  8747. stat=0;
  8748. if (i > MAX_LEVEL)
  8749. break;
  8750. statp[i]=stat;
  8751. i++;
  8752. entries++;
  8753. }
  8754. fclose(fp);
  8755. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, DBPATH"statpoint.txt");
  8756. }
  8757. // generate the remaining parts of the db if necessary
  8758. k = battle_config.use_statpoint_table; //save setting
  8759. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  8760. statp[0] = 45; // seed value
  8761. for (; i <= MAX_LEVEL; i++)
  8762. statp[i] = statp[i-1] + pc_gets_status_point(i-1);
  8763. battle_config.use_statpoint_table = k; //restore setting
  8764. #ifdef RENEWAL_ASPD
  8765. sv_readdb(db_path, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1);
  8766. #else
  8767. sv_readdb(db_path, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1);
  8768. #endif
  8769. sv_readdb(db_path, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2);
  8770. sv_readdb(db_path, DBPATH"job_maxhpsp_db.txt", ',', 4, 4+MAX_LEVEL, CLASS_COUNT*2, &pc_readdb_job_maxhpsp);
  8771. sv_readdb(db_path, DBPATH"job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp); //support till 1000lvl
  8772. //Checking if all class have their data
  8773. for (i = 0; i < JOB_MAX; i++) {
  8774. int j;
  8775. if (!pcdb_checkid(i)) continue;
  8776. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK)
  8777. continue; //Classes that do not need exp tables.
  8778. j = pc_class2idx(i);
  8779. if (!job_info[j].max_level[0])
  8780. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  8781. if (!job_info[j].max_level[1])
  8782. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  8783. }
  8784. return 0;
  8785. }
  8786. // Read MOTD on startup. [Valaris]
  8787. int pc_read_motd(void)
  8788. {
  8789. FILE* fp;
  8790. // clear old MOTD
  8791. memset(motd_text, 0, sizeof(motd_text));
  8792. // read current MOTD
  8793. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  8794. {
  8795. char* buf, * ptr;
  8796. unsigned int lines = 0, entries = 0;
  8797. size_t len;
  8798. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  8799. {
  8800. lines++;
  8801. buf = motd_text[entries];
  8802. if( buf[0] == '/' && buf[1] == '/' )
  8803. {
  8804. continue;
  8805. }
  8806. len = strlen(buf);
  8807. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) )
  8808. {// strip trailing EOL characters
  8809. len--;
  8810. }
  8811. if( len )
  8812. {
  8813. buf[len] = 0;
  8814. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH )
  8815. {// crashes newer clients
  8816. 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);
  8817. }
  8818. }
  8819. else
  8820. {// empty line
  8821. buf[0] = ' ';
  8822. buf[1] = 0;
  8823. }
  8824. entries++;
  8825. }
  8826. fclose(fp);
  8827. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  8828. }
  8829. else
  8830. {
  8831. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  8832. }
  8833. return 0;
  8834. }
  8835. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  8836. int i,cursor = 0;
  8837. struct item_cd* cd = NULL;
  8838. if( load ) {
  8839. if( !(cd = idb_get(itemcd_db, sd->status.char_id)) ) {
  8840. // no skill cooldown is associated with this character
  8841. return;
  8842. }
  8843. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8844. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  8845. sd->item_delay[cursor].tick = cd->tick[i];
  8846. sd->item_delay[cursor].nameid = cd->nameid[i];
  8847. cursor++;
  8848. }
  8849. }
  8850. idb_remove(itemcd_db,sd->status.char_id);
  8851. } else {
  8852. if( !(cd = idb_get(itemcd_db,sd->status.char_id)) ) {
  8853. // create a new skill cooldown object for map storage
  8854. CREATE( cd, struct item_cd, 1 );
  8855. idb_put( itemcd_db, sd->status.char_id, cd );
  8856. }
  8857. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8858. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  8859. cd->tick[cursor] = sd->item_delay[i].tick;
  8860. cd->nameid[cursor] = sd->item_delay[i].nameid;
  8861. cursor++;
  8862. }
  8863. }
  8864. }
  8865. return;
  8866. }
  8867. void pc_clear_log_damage_sub(int char_id, struct mob_data *md)
  8868. {
  8869. int i;
  8870. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  8871. if( i < DAMAGELOG_SIZE )
  8872. {
  8873. md->dmglog[i].id=0;
  8874. md->dmglog[i].dmg=0;
  8875. md->dmglog[i].flag=0;
  8876. }
  8877. }
  8878. void pc_damage_log_add(struct map_session_data *sd, int id)
  8879. {
  8880. int i = 0;
  8881. if( !sd )
  8882. return;
  8883. for(i = 0; i < DAMAGELOG_SIZE_PC && sd->dmglog[i].id != id; i++)
  8884. if( !sd->dmglog[i].id )
  8885. {
  8886. sd->dmglog[i].id = id;
  8887. break;
  8888. }
  8889. return;
  8890. }
  8891. void pc_damage_log_clear(struct map_session_data *sd, int id)
  8892. {
  8893. int i;
  8894. struct mob_data *md = NULL;
  8895. if( !sd )
  8896. return;
  8897. if( !id )
  8898. {
  8899. for(i = 0; i < DAMAGELOG_SIZE_PC; i++) // track every id
  8900. {
  8901. if( !sd->dmglog[i].id ) //skip the empty value
  8902. continue;
  8903. if( (md = map_id2md(sd->dmglog[i].id)) )
  8904. pc_clear_log_damage_sub(sd->status.char_id,md);
  8905. }
  8906. memset(sd->dmglog,0,sizeof(sd->dmglog)); // clear all
  8907. }
  8908. else
  8909. {
  8910. if( (md = map_id2md(id)) )
  8911. pc_clear_log_damage_sub(sd->status.char_id,md);
  8912. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i].id == id); // find the id position
  8913. if( i < DAMAGELOG_SIZE_PC )
  8914. sd->dmglog[i].id = 0;
  8915. }
  8916. }
  8917. /*==========================================
  8918. * pc Init/Terminate
  8919. *------------------------------------------*/
  8920. void do_final_pc(void) {
  8921. db_destroy(itemcd_db);
  8922. do_final_pc_groups();
  8923. return;
  8924. }
  8925. int do_init_pc(void) {
  8926. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  8927. pc_readdb();
  8928. pc_read_motd(); // Read MOTD [Valaris]
  8929. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  8930. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  8931. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  8932. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  8933. add_timer_func_list(pc_autosave, "pc_autosave");
  8934. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  8935. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  8936. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  8937. add_timer_func_list(pc_talisman_timer, "pc_talisman_timer");
  8938. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  8939. // 0=day, 1=night [Yor]
  8940. night_flag = battle_config.night_at_start ? 1 : 0;
  8941. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  8942. int day_duration = battle_config.day_duration;
  8943. int night_duration = battle_config.night_duration;
  8944. // add night/day timer [Yor]
  8945. add_timer_func_list(map_day_timer, "map_day_timer");
  8946. add_timer_func_list(map_night_timer, "map_night_timer");
  8947. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  8948. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  8949. }
  8950. do_init_pc_groups();
  8951. return 0;
  8952. }