pc.c 281 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720
  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 "chrif.h"
  18. #include "clif.h"
  19. #include "date.h" // is_day_of_*()
  20. #include "duel.h"
  21. #include "intif.h"
  22. #include "itemdb.h"
  23. #include "log.h"
  24. #include "mail.h"
  25. #include "map.h"
  26. #include "path.h"
  27. #include "homunculus.h"
  28. #include "instance.h"
  29. #include "mercenary.h"
  30. #include "elemental.h"
  31. #include "npc.h" // fake_nd
  32. #include "pet.h" // pet_unlocktarget()
  33. #include "party.h" // party_search()
  34. #include "guild.h" // guild_search(), guild_request_info()
  35. #include "script.h" // script_config
  36. #include "skill.h"
  37. #include "status.h" // struct status_data
  38. #include "pc.h"
  39. #include "pc_groups.h"
  40. #include "quest.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  46. static unsigned int exp_table[CLASS_COUNT][2][MAX_LEVEL];
  47. static unsigned int max_level[CLASS_COUNT][2];
  48. static unsigned int statp[MAX_LEVEL+1];
  49. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  50. static unsigned int level_penalty[3][RC_MAX][MAX_LEVEL*2+1];
  51. #endif
  52. // h-files are for declarations, not for implementations... [Shinomori]
  53. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  54. // timer for night.day implementation
  55. int day_timer_tid;
  56. int night_timer_tid;
  57. struct fame_list smith_fame_list[MAX_FAME_LIST];
  58. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  59. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  60. 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_AMMO};
  61. #define MOTD_LINE_SIZE 128
  62. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  63. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  64. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  65. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  66. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  67. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  68. };
  69. /**
  70. * Item Cool Down Delay Saving
  71. * Struct item_cd is not a member of struct map_session_data
  72. * to keep cooldowns in memory between player log-ins.
  73. * All cooldowns are reset when server is restarted.
  74. **/
  75. DBMap* itemcd_db = NULL; // char_id -> struct skill_cd
  76. struct item_cd {
  77. unsigned int tick[MAX_ITEMDELAYS];//tick
  78. short nameid[MAX_ITEMDELAYS];//skill id
  79. };
  80. //Converts a class to its array index for CLASS_COUNT defined arrays.
  81. //Note that it does not do a validity check for speed purposes, where parsing
  82. //player input make sure to use a pcdb_checkid first!
  83. int pc_class2idx(int class_) {
  84. if (class_ >= JOB_NOVICE_HIGH)
  85. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  86. return class_;
  87. }
  88. inline int pc_get_group_id(struct map_session_data *sd) {
  89. return sd->group_id;
  90. }
  91. inline int pc_get_group_level(struct map_session_data *sd) {
  92. return sd->group_level;
  93. }
  94. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  95. {
  96. struct map_session_data *sd;
  97. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  98. return 1;
  99. if(sd->invincible_timer != tid){
  100. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  101. return 0;
  102. }
  103. sd->invincible_timer = INVALID_TIMER;
  104. skill_unit_move(&sd->bl,tick,1);
  105. return 0;
  106. }
  107. void pc_setinvincibletimer(struct map_session_data* sd, int val)
  108. {
  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. const struct TimerData * td;
  411. int tick = seconds * 1000;
  412. if( sd == NULL )
  413. return;
  414. if( sd->rental_timer != INVALID_TIMER )
  415. {
  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. * prepares character for saving.
  435. *------------------------------------------*/
  436. int pc_makesavestatus(struct map_session_data *sd)
  437. {
  438. nullpo_ret(sd);
  439. if(!battle_config.save_clothcolor)
  440. sd->status.clothes_color=0;
  441. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  442. //status change load/saving. [Skotlex]
  443. #ifdef NEW_CARTS
  444. sd->status.option = sd->sc.option&(OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  445. #else
  446. sd->status.option = sd->sc.option&(OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  447. #endif
  448. if (sd->sc.data[SC_JAILED])
  449. { //When Jailed, do not move last point.
  450. if(pc_isdead(sd)){
  451. pc_setrestartvalue(sd,0);
  452. } else {
  453. sd->status.hp = sd->battle_status.hp;
  454. sd->status.sp = sd->battle_status.sp;
  455. }
  456. sd->status.last_point.map = sd->mapindex;
  457. sd->status.last_point.x = sd->bl.x;
  458. sd->status.last_point.y = sd->bl.y;
  459. return 0;
  460. }
  461. if(pc_isdead(sd)){
  462. pc_setrestartvalue(sd,0);
  463. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  464. } else {
  465. sd->status.hp = sd->battle_status.hp;
  466. sd->status.sp = sd->battle_status.sp;
  467. sd->status.last_point.map = sd->mapindex;
  468. sd->status.last_point.x = sd->bl.x;
  469. sd->status.last_point.y = sd->bl.y;
  470. }
  471. if(map[sd->bl.m].flag.nosave){
  472. struct map_data *m=&map[sd->bl.m];
  473. if(m->save.map)
  474. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  475. else
  476. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  477. }
  478. return 0;
  479. }
  480. /*==========================================
  481. * Off init ? Connection?
  482. *------------------------------------------*/
  483. 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)
  484. {
  485. nullpo_ret(sd);
  486. sd->bl.id = account_id;
  487. sd->status.account_id = account_id;
  488. sd->status.char_id = char_id;
  489. sd->status.sex = sex;
  490. sd->login_id1 = login_id1;
  491. sd->login_id2 = 0; // at this point, we can not know the value :(
  492. sd->client_tick = client_tick;
  493. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  494. sd->bl.type = BL_PC;
  495. sd->canlog_tick = gettick();
  496. //Required to prevent homunculus copuing a base speed of 0.
  497. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  498. return 0;
  499. }
  500. int pc_equippoint(struct map_session_data *sd,int n)
  501. {
  502. int ep = 0;
  503. nullpo_ret(sd);
  504. if(!sd->inventory_data[n])
  505. return 0;
  506. if (!itemdb_isequip2(sd->inventory_data[n]))
  507. return 0; //Not equippable by players.
  508. ep = sd->inventory_data[n]->equip;
  509. if(sd->inventory_data[n]->look == W_DAGGER ||
  510. sd->inventory_data[n]->look == W_1HSWORD ||
  511. sd->inventory_data[n]->look == W_1HAXE) {
  512. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  513. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  514. return EQP_ARMS;
  515. }
  516. return ep;
  517. }
  518. int pc_setinventorydata(struct map_session_data *sd)
  519. {
  520. int i,id;
  521. nullpo_ret(sd);
  522. for(i=0;i<MAX_INVENTORY;i++) {
  523. id = sd->status.inventory[i].nameid;
  524. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  525. }
  526. return 0;
  527. }
  528. int pc_calcweapontype(struct map_session_data *sd)
  529. {
  530. nullpo_ret(sd);
  531. // single-hand
  532. if(sd->weapontype2 == W_FIST) {
  533. sd->status.weapon = sd->weapontype1;
  534. return 1;
  535. }
  536. if(sd->weapontype1 == W_FIST) {
  537. sd->status.weapon = sd->weapontype2;
  538. return 1;
  539. }
  540. // dual-wield
  541. sd->status.weapon = 0;
  542. switch (sd->weapontype1){
  543. case W_DAGGER:
  544. switch (sd->weapontype2) {
  545. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  546. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  547. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  548. }
  549. break;
  550. case W_1HSWORD:
  551. switch (sd->weapontype2) {
  552. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  553. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  554. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  555. }
  556. break;
  557. case W_1HAXE:
  558. switch (sd->weapontype2) {
  559. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  560. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  561. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  562. }
  563. }
  564. // unknown, default to right hand type
  565. if (!sd->status.weapon)
  566. sd->status.weapon = sd->weapontype1;
  567. return 2;
  568. }
  569. int pc_setequipindex(struct map_session_data *sd)
  570. {
  571. int i,j;
  572. nullpo_ret(sd);
  573. for(i=0;i<EQI_MAX;i++)
  574. sd->equip_index[i] = -1;
  575. for(i=0;i<MAX_INVENTORY;i++) {
  576. if(sd->status.inventory[i].nameid <= 0)
  577. continue;
  578. if(sd->status.inventory[i].equip) {
  579. for(j=0;j<EQI_MAX;j++)
  580. if(sd->status.inventory[i].equip & equip_pos[j])
  581. sd->equip_index[j] = i;
  582. if(sd->status.inventory[i].equip & EQP_HAND_R)
  583. {
  584. if(sd->inventory_data[i])
  585. sd->weapontype1 = sd->inventory_data[i]->look;
  586. else
  587. sd->weapontype1 = 0;
  588. }
  589. if( sd->status.inventory[i].equip & EQP_HAND_L )
  590. {
  591. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  592. sd->weapontype2 = sd->inventory_data[i]->look;
  593. else
  594. sd->weapontype2 = 0;
  595. }
  596. }
  597. }
  598. pc_calcweapontype(sd);
  599. return 0;
  600. }
  601. static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  602. {
  603. int i;
  604. struct item *item = &sd->status.inventory[eqindex];
  605. struct item_data *data;
  606. //Crafted/made/hatched items.
  607. if (itemdb_isspecial(item->card[0]))
  608. return 1;
  609. /* scan for enchant armor gems */
  610. if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  611. s = MAX_SLOTS - 1;
  612. ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  613. return( i < s ) ? 0 : 1;
  614. }
  615. bool pc_isequipped(struct map_session_data *sd, int nameid)
  616. {
  617. int i, j, index;
  618. for( i = 0; i < EQI_MAX; i++ )
  619. {
  620. index = sd->equip_index[i];
  621. if( index < 0 ) continue;
  622. if( i == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  623. if( i == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  624. if( i == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  625. if( !sd->inventory_data[index] ) continue;
  626. if( sd->inventory_data[index]->nameid == nameid )
  627. return true;
  628. for( j = 0; j < sd->inventory_data[index]->slot; j++ )
  629. if( sd->status.inventory[index].card[j] == nameid )
  630. return true;
  631. }
  632. return false;
  633. }
  634. bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
  635. {
  636. if( !p1_sd || !p2_sd || !b_sd )
  637. return false;
  638. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  639. return false; // already adopted baby / in adopt request
  640. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  641. return false; // You need to be married and in party with baby to adopt
  642. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  643. return false; // Not married, wrong married
  644. if( p2_sd->status.party_id != p1_sd->status.party_id )
  645. return false; // Both parents need to be in the same party
  646. // Parents need to have their ring equipped
  647. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  648. return false;
  649. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  650. return false;
  651. // Already adopted a baby
  652. if( p1_sd->status.child || p2_sd->status.child ) {
  653. clif_Adopt_reply(p1_sd, 0);
  654. return false;
  655. }
  656. // Parents need at least lvl 70 to adopt
  657. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  658. clif_Adopt_reply(p1_sd, 1);
  659. return false;
  660. }
  661. if( b_sd->status.partner_id ) {
  662. clif_Adopt_reply(p1_sd, 2);
  663. return false;
  664. }
  665. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE ) )
  666. return false;
  667. return true;
  668. }
  669. /*==========================================
  670. * Adoption Process
  671. *------------------------------------------*/
  672. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  673. {
  674. int job, joblevel;
  675. unsigned int jobexp;
  676. if( !pc_can_Adopt(p1_sd, p2_sd, b_sd) )
  677. return false;
  678. // Preserve current job levels and progress
  679. joblevel = b_sd->status.job_level;
  680. jobexp = b_sd->status.job_exp;
  681. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  682. if( job != -1 && !pc_jobchange(b_sd, job, 0) )
  683. { // Success, proceed to configure parents and baby skills
  684. p1_sd->status.child = b_sd->status.char_id;
  685. p2_sd->status.child = b_sd->status.char_id;
  686. b_sd->status.father = p1_sd->status.char_id;
  687. b_sd->status.mother = p2_sd->status.char_id;
  688. // Restore progress
  689. b_sd->status.job_level = joblevel;
  690. clif_updatestatus(b_sd, SP_JOBLEVEL);
  691. b_sd->status.job_exp = jobexp;
  692. clif_updatestatus(b_sd, SP_JOBEXP);
  693. // Baby Skills
  694. pc_skill(b_sd, WE_BABY, 1, 0);
  695. pc_skill(b_sd, WE_CALLPARENT, 1, 0);
  696. // Parents Skills
  697. pc_skill(p1_sd, WE_CALLBABY, 1, 0);
  698. pc_skill(p2_sd, WE_CALLBABY, 1, 0);
  699. return true;
  700. }
  701. return false; // Job Change Fail
  702. }
  703. /*=================================================
  704. * Checks if the player can equip the item at index n in inventory.
  705. * Returns 0 (no) or 1 (yes).
  706. *------------------------------------------------*/
  707. int pc_isequip(struct map_session_data *sd,int n)
  708. {
  709. struct item_data *item;
  710. nullpo_ret(sd);
  711. item = sd->inventory_data[n];
  712. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  713. return 1;
  714. if(item == NULL)
  715. return 0;
  716. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  717. return 0;
  718. #ifdef RENEWAL
  719. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  720. return 0;
  721. #endif
  722. if(item->sex != 2 && sd->status.sex != item->sex)
  723. return 0;
  724. if(!map_flag_vs(sd->bl.m) && ((item->flag.no_equip&1) || !pc_isAllowedCardOn(sd,item->slot,n,1)))
  725. return 0;
  726. if(map[sd->bl.m].flag.pvp && ((item->flag.no_equip&2) || !pc_isAllowedCardOn(sd,item->slot,n,2)))
  727. return 0;
  728. if(map_flag_gvg(sd->bl.m) && ((item->flag.no_equip&4) || !pc_isAllowedCardOn(sd,item->slot,n,4)))
  729. return 0;
  730. if(map[sd->bl.m].flag.battleground && ((item->flag.no_equip&8) || !pc_isAllowedCardOn(sd,item->slot,n,8)))
  731. return 0;
  732. if(map[sd->bl.m].flag.restricted)
  733. {
  734. int flag =8*map[sd->bl.m].zone;
  735. if (item->flag.no_equip&flag || !pc_isAllowedCardOn(sd,item->slot,n,flag))
  736. return 0;
  737. }
  738. if (sd->sc.count) {
  739. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  740. return 0;
  741. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  742. return 0;
  743. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  744. return 0;
  745. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  746. return 0;
  747. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  748. return 0;
  749. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  750. return 0;
  751. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  752. //Spirit of Super Novice equip bonuses. [Skotlex]
  753. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  754. return 1; //Can equip all helms
  755. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON)
  756. switch(item->look) { //In weapons, the look determines type of weapon.
  757. case W_DAGGER: //Level 4 Knives are equippable.. this means all knives, I'd guess?
  758. case W_1HSWORD: //All 1H swords
  759. case W_1HAXE: //All 1H Axes
  760. case W_MACE: //All 1H Maces
  761. case W_STAFF: //All 1H Staves
  762. return 1;
  763. }
  764. }
  765. }
  766. //Not equipable by class. [Skotlex]
  767. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  768. return 0;
  769. //Not usable by upper class. [Inkfish]
  770. while( 1 ) {
  771. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  772. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  773. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  774. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  775. return 0;
  776. }
  777. return 1;
  778. }
  779. /*==========================================
  780. * No problem with the session id
  781. * set the status that has been sent from char server
  782. *------------------------------------------*/
  783. 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)
  784. {
  785. int i;
  786. unsigned long tick = gettick();
  787. uint32 ip = session[sd->fd]->client_addr;
  788. sd->login_id2 = login_id2;
  789. sd->group_id = group_id;
  790. /* load user permissions */
  791. pc_group_pc_load(sd);
  792. memcpy(&sd->status, st, sizeof(*st));
  793. if (st->sex != sd->status.sex) {
  794. clif_authfail_fd(sd->fd, 0);
  795. return false;
  796. }
  797. //Set the map-server used job id. [Skotlex]
  798. i = pc_jobid2mapid(sd->status.class_);
  799. if (i == -1) { //Invalid class?
  800. 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);
  801. sd->status.class_ = JOB_NOVICE;
  802. sd->class_ = MAPID_NOVICE;
  803. } else
  804. sd->class_ = i;
  805. // Checks and fixes to character status data, that are required
  806. // in case of configuration change or stuff, which cannot be
  807. // checked on char-server.
  808. if( sd->status.hair < MIN_HAIR_STYLE || sd->status.hair > MAX_HAIR_STYLE )
  809. {
  810. sd->status.hair = MIN_HAIR_STYLE;
  811. }
  812. if( sd->status.hair_color < MIN_HAIR_COLOR || sd->status.hair_color > MAX_HAIR_COLOR )
  813. {
  814. sd->status.hair_color = MIN_HAIR_COLOR;
  815. }
  816. if( sd->status.clothes_color < MIN_CLOTH_COLOR || sd->status.clothes_color > MAX_CLOTH_COLOR )
  817. {
  818. sd->status.clothes_color = MIN_CLOTH_COLOR;
  819. }
  820. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  821. if(!sd->status.hp) pc_setdead(sd);
  822. sd->state.connect_new = 1;
  823. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  824. sd->invincible_timer = INVALID_TIMER;
  825. sd->npc_timer_id = INVALID_TIMER;
  826. sd->pvp_timer = INVALID_TIMER;
  827. /**
  828. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  829. **/
  830. #if SECURE_NPCTIMEOUT
  831. /**
  832. * Initialize to defaults/expected
  833. **/
  834. sd->npc_idle_timer = INVALID_TIMER;
  835. sd->npc_idle_tick = tick;
  836. #endif
  837. sd->canuseitem_tick = tick;
  838. sd->canusecashfood_tick = tick;
  839. sd->canequip_tick = tick;
  840. sd->cantalk_tick = tick;
  841. sd->canskill_tick = tick;
  842. sd->cansendmail_tick = tick;
  843. for(i = 0; i < MAX_SKILL_LEVEL; i++)
  844. sd->spirit_timer[i] = INVALID_TIMER;
  845. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  846. sd->autobonus[i].active = INVALID_TIMER;
  847. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  848. sd->autobonus2[i].active = INVALID_TIMER;
  849. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  850. sd->autobonus3[i].active = INVALID_TIMER;
  851. if (battle_config.item_auto_get)
  852. sd->state.autoloot = 10000;
  853. if (battle_config.disp_experience)
  854. sd->state.showexp = 1;
  855. if (battle_config.disp_zeny)
  856. sd->state.showzeny = 1;
  857. if (!(battle_config.display_skill_fail&2))
  858. sd->state.showdelay = 1;
  859. pc_setinventorydata(sd);
  860. pc_setequipindex(sd);
  861. status_change_init(&sd->bl);
  862. if (pc_can_use_command(sd, "hide", COMMAND_ATCOMMAND))
  863. sd->status.option &= (OPTION_MASK | OPTION_INVISIBLE);
  864. else
  865. sd->status.option &= OPTION_MASK;
  866. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  867. //Set here because we need the inventory data for weapon sprite parsing.
  868. status_set_viewdata(&sd->bl, sd->status.class_);
  869. unit_dataset(&sd->bl);
  870. sd->guild_x = -1;
  871. sd->guild_y = -1;
  872. // Event Timers
  873. for( i = 0; i < MAX_EVENTTIMER; i++ )
  874. sd->eventtimer[i] = INVALID_TIMER;
  875. // Rental Timer
  876. sd->rental_timer = INVALID_TIMER;
  877. for( i = 0; i < 3; i++ )
  878. sd->hate_mob[i] = -1;
  879. //warp player
  880. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
  881. 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);
  882. // try warping to a default map instead (church graveyard)
  883. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
  884. // if we fail again
  885. clif_authfail_fd(sd->fd, 0);
  886. return false;
  887. }
  888. }
  889. clif_authok(sd);
  890. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  891. sd->die_counter=-1;
  892. //display login notice
  893. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  894. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  895. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  896. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  897. sd->status.name, sd->status.account_id, sd->status.char_id,
  898. sd->packet_ver, CONVIP(ip), sd->group_id);
  899. // Send friends list
  900. clif_friendslist_send(sd);
  901. if( !changing_mapservers ) {
  902. if (battle_config.display_version == 1){
  903. char buf[256];
  904. sprintf(buf, "SVN version: %s", get_svn_revision());
  905. clif_displaymessage(sd->fd, buf);
  906. }
  907. // Message of the Day [Valaris]
  908. for(i=0; motd_text[i][0] && i < MOTD_LINE_SIZE; i++) {
  909. if (battle_config.motd_type)
  910. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  911. else
  912. clif_displaymessage(sd->fd, motd_text[i]);
  913. }
  914. // message of the limited time of the account
  915. if (expiration_time != 0) { // don't display if it's unlimited or unknow value
  916. char tmpstr[1024];
  917. strftime(tmpstr, sizeof(tmpstr) - 1, msg_txt(501), localtime(&expiration_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  918. clif_wis_message(sd->fd, wisp_server_name, tmpstr, strlen(tmpstr)+1);
  919. }
  920. /**
  921. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  922. **/
  923. clif_changemap(sd,sd->mapindex,sd->bl.x,sd->bl.y);
  924. }
  925. /**
  926. * Check if player have any cool downs on
  927. **/
  928. skill_cooldown_load(sd);
  929. /**
  930. * Check if player have any item cooldowns on
  931. **/
  932. pc_itemcd_do(sd,true);
  933. // Request all registries (auth is considered completed whence they arrive)
  934. intif_request_registry(sd,7);
  935. return true;
  936. }
  937. /*==========================================
  938. * Closes a connection because it failed to be authenticated from the char server.
  939. *------------------------------------------*/
  940. void pc_authfail(struct map_session_data *sd)
  941. {
  942. clif_authfail_fd(sd->fd, 0);
  943. return;
  944. }
  945. //Attempts to set a mob.
  946. int pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  947. {
  948. int class_;
  949. if (!sd || !bl || pos < 0 || pos > 2)
  950. return 0;
  951. if (sd->hate_mob[pos] != -1)
  952. { //Can't change hate targets.
  953. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  954. return 0;
  955. }
  956. class_ = status_get_class(bl);
  957. if (!pcdb_checkid(class_)) {
  958. unsigned int max_hp = status_get_max_hp(bl);
  959. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  960. return 0;
  961. if (pos != status_get_size(bl))
  962. return 0; //Wrong size
  963. }
  964. sd->hate_mob[pos] = class_;
  965. pc_setglobalreg(sd,sg_info[pos].hate_var,class_+1);
  966. clif_hate_info(sd, pos, class_, 1);
  967. return 1;
  968. }
  969. /*==========================================
  970. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  971. *------------------------------------------*/
  972. int pc_reg_received(struct map_session_data *sd)
  973. {
  974. int i,j;
  975. sd->change_level_2nd = pc_readglobalreg(sd,"jobchange_level");
  976. sd->change_level_3rd = pc_readglobalreg(sd,"jobchange_level_3rd");
  977. sd->die_counter = pc_readglobalreg(sd,"PC_DIE_COUNTER");
  978. // Cash shop
  979. sd->cashPoints = pc_readaccountreg(sd,"#CASHPOINTS");
  980. sd->kafraPoints = pc_readaccountreg(sd,"#KAFRAPOINTS");
  981. // Cooking Exp
  982. sd->cook_mastery = pc_readglobalreg(sd,"COOK_MASTERY");
  983. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  984. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  985. sd->mission_mobid = pc_readglobalreg(sd,"TK_MISSION_ID");
  986. sd->mission_count = pc_readglobalreg(sd,"TK_MISSION_COUNT");
  987. }
  988. //SG map and mob read [Komurka]
  989. for(i=0;i<MAX_PC_FEELHATE;i++) //for now - someone need to make reading from txt/sql
  990. {
  991. if ((j = pc_readglobalreg(sd,sg_info[i].feel_var))!=0) {
  992. sd->feel_map[i].index = j;
  993. sd->feel_map[i].m = map_mapindex2mapid(j);
  994. } else {
  995. sd->feel_map[i].index = 0;
  996. sd->feel_map[i].m = -1;
  997. }
  998. sd->hate_mob[i] = pc_readglobalreg(sd,sg_info[i].hate_var)-1;
  999. }
  1000. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1001. sd->cloneskill_id = pc_readglobalreg(sd,"CLONE_SKILL");
  1002. if (sd->cloneskill_id > 0) {
  1003. sd->status.skill[sd->cloneskill_id].id = sd->cloneskill_id;
  1004. sd->status.skill[sd->cloneskill_id].lv = pc_readglobalreg(sd,"CLONE_SKILL_LV");
  1005. if (sd->status.skill[sd->cloneskill_id].lv > i)
  1006. sd->status.skill[sd->cloneskill_id].lv = i;
  1007. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1008. }
  1009. }
  1010. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1011. sd->reproduceskill_id = pc_readglobalreg(sd,"REPRODUCE_SKILL");
  1012. if( sd->reproduceskill_id > 0) {
  1013. sd->status.skill[sd->reproduceskill_id].id = sd->reproduceskill_id;
  1014. sd->status.skill[sd->reproduceskill_id].lv = pc_readglobalreg(sd,"REPRODUCE_SKILL_LV");
  1015. if( i < sd->status.skill[sd->reproduceskill_id].lv)
  1016. sd->status.skill[sd->reproduceskill_id].lv = i;
  1017. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1018. }
  1019. }
  1020. //Weird... maybe registries were reloaded?
  1021. if (sd->state.active)
  1022. return 0;
  1023. sd->state.active = 1;
  1024. if (sd->status.party_id)
  1025. party_member_joined(sd);
  1026. if (sd->status.guild_id)
  1027. guild_member_joined(sd);
  1028. // pet
  1029. if (sd->status.pet_id > 0)
  1030. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1031. // Homunculus [albator]
  1032. if( sd->status.hom_id > 0 )
  1033. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1034. if( sd->status.mer_id > 0 )
  1035. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1036. if( sd->status.ele_id > 0 )
  1037. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1038. map_addiddb(&sd->bl);
  1039. map_delnickdb(sd->status.char_id, sd->status.name);
  1040. if (!chrif_auth_finished(sd))
  1041. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1042. pc_load_combo(sd);
  1043. status_calc_pc(sd,1);
  1044. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1045. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1046. intif_request_questlog(sd);
  1047. if (sd->state.connect_new == 0 && sd->fd)
  1048. { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1049. sd->state.connect_new = 1;
  1050. clif_parse_LoadEndAck(sd->fd, sd);
  1051. }
  1052. pc_inventory_rentals(sd);
  1053. return 1;
  1054. }
  1055. static int pc_calc_skillpoint(struct map_session_data* sd)
  1056. {
  1057. int i,skill,inf2,skill_point=0;
  1058. nullpo_ret(sd);
  1059. for(i=1;i<MAX_SKILL;i++){
  1060. if( (skill = pc_checkskill(sd,i)) > 0) {
  1061. inf2 = skill_get_inf2(i);
  1062. if((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1063. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1064. ) {
  1065. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1066. skill_point += skill;
  1067. else
  1068. if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1069. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1070. }
  1071. }
  1072. }
  1073. return skill_point;
  1074. }
  1075. /*==========================================
  1076. * Calculation of skill level.
  1077. *------------------------------------------*/
  1078. int pc_calc_skilltree(struct map_session_data *sd)
  1079. {
  1080. int i,id=0,flag;
  1081. int c=0;
  1082. nullpo_ret(sd);
  1083. i = pc_calc_skilltree_normalize_job(sd);
  1084. c = pc_mapid2jobid(i, sd->status.sex);
  1085. if( c == -1 )
  1086. { //Unable to normalize job??
  1087. 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);
  1088. return 1;
  1089. }
  1090. c = pc_class2idx(c);
  1091. for( i = 0; i < MAX_SKILL; i++ )
  1092. {
  1093. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED ) //Don't touch plagiarized skills
  1094. sd->status.skill[i].id = 0; //First clear skills.
  1095. }
  1096. for( i = 0; i < MAX_SKILL; i++ )
  1097. {
  1098. if( sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED )
  1099. { // Restore original level of skills after deleting earned skills.
  1100. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1101. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1102. }
  1103. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER && i >= DC_HUMMING && i<= DC_SERVICEFORYOU )
  1104. { //Enable Bard/Dancer spirit linked skills.
  1105. if( sd->status.sex )
  1106. { //Link dancer skills to bard.
  1107. if( sd->status.skill[i-8].lv < 10 )
  1108. continue;
  1109. sd->status.skill[i].id = i;
  1110. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1111. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1112. }
  1113. else
  1114. { //Link bard skills to dancer.
  1115. if( sd->status.skill[i].lv < 10 )
  1116. continue;
  1117. sd->status.skill[i-8].id = i - 8;
  1118. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1119. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1120. }
  1121. }
  1122. }
  1123. if( pc_has_permission(sd, PC_PERM_ALL_SKILL) ) {
  1124. for( i = 0; i < MAX_SKILL; i++ ) {
  1125. switch(i) {
  1126. /**
  1127. * Dummy skills must be added here otherwise they'll be displayed in the,
  1128. * skill tree and since they have no icons they'll give resource errors
  1129. **/
  1130. case SM_SELFPROVOKE:
  1131. case AB_DUPLELIGHT_MELEE:
  1132. case AB_DUPLELIGHT_MAGIC:
  1133. case WL_CHAINLIGHTNING_ATK:
  1134. case WL_TETRAVORTEX_FIRE:
  1135. case WL_TETRAVORTEX_WATER:
  1136. case WL_TETRAVORTEX_WIND:
  1137. case WL_TETRAVORTEX_GROUND:
  1138. case WL_SUMMON_ATK_FIRE:
  1139. case WL_SUMMON_ATK_WIND:
  1140. case WL_SUMMON_ATK_WATER:
  1141. case WL_SUMMON_ATK_GROUND:
  1142. case LG_OVERBRAND_BRANDISH:
  1143. case LG_OVERBRAND_PLUSATK:
  1144. case WM_SEVERE_RAINSTORM_MELEE:
  1145. continue;
  1146. default:
  1147. break;
  1148. }
  1149. if( skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL) )
  1150. continue; //Only skills you can't have are npc/guild ones
  1151. if( skill_get_max(i) > 0 )
  1152. sd->status.skill[i].id = i;
  1153. }
  1154. return 0;
  1155. }
  1156. do {
  1157. flag = 0;
  1158. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1159. {
  1160. int j, f, k, inf2;
  1161. if( sd->status.skill[id].id )
  1162. continue; //Skill already known.
  1163. f = 1;
  1164. if(!battle_config.skillfree) {
  1165. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1166. if((k=skill_tree[c][i].need[j].id))
  1167. {
  1168. if (sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED)
  1169. k = 0; //Not learned.
  1170. else
  1171. if (sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1172. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1173. else
  1174. k = pc_checkskill(sd,k);
  1175. if (k < skill_tree[c][i].need[j].lv)
  1176. {
  1177. f = 0;
  1178. break;
  1179. }
  1180. }
  1181. }
  1182. if( sd->status.job_level < skill_tree[c][i].joblv )
  1183. f = 0; // job level requirement wasn't satisfied
  1184. }
  1185. if( f ) {
  1186. inf2 = skill_get_inf2(id);
  1187. if(!sd->status.skill[id].lv && (
  1188. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1189. inf2&INF2_WEDDING_SKILL ||
  1190. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1191. ))
  1192. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1193. sd->status.skill[id].id = id;
  1194. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1195. sd->status.skill[id].lv = 1; // need to manually specify a skill level
  1196. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1197. }
  1198. flag = 1; // skill list has changed, perform another pass
  1199. }
  1200. }
  1201. } while(flag);
  1202. //
  1203. 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) )
  1204. {
  1205. /* Taekwon Ranger Bonus Skill Tree
  1206. ============================================
  1207. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1208. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1209. - (sd->status.skill_point == 0) to wait until all skill points are asigned to avoid problems with Job Change quest. */
  1210. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1211. {
  1212. if( (skill_get_inf2(id)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1213. continue; //Do not include Quest/Wedding skills.
  1214. if( sd->status.skill[id].id == 0 )
  1215. {
  1216. sd->status.skill[id].id = id;
  1217. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1218. }
  1219. else
  1220. {
  1221. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Remember original level
  1222. }
  1223. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_);
  1224. }
  1225. }
  1226. return 0;
  1227. }
  1228. //Checks if you can learn a new skill after having leveled up a skill.
  1229. static void pc_check_skilltree(struct map_session_data *sd, int skill)
  1230. {
  1231. int i,id=0,flag;
  1232. int c=0;
  1233. if(battle_config.skillfree)
  1234. return; //Function serves no purpose if this is set
  1235. i = pc_calc_skilltree_normalize_job(sd);
  1236. c = pc_mapid2jobid(i, sd->status.sex);
  1237. if (c == -1) { //Unable to normalize job??
  1238. 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);
  1239. return;
  1240. }
  1241. c = pc_class2idx(c);
  1242. do {
  1243. flag = 0;
  1244. for( i = 0; i < MAX_SKILL_TREE && (id=skill_tree[c][i].id)>0; i++ )
  1245. {
  1246. int j, f = 1, k;
  1247. if( sd->status.skill[id].id ) //Already learned
  1248. continue;
  1249. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ )
  1250. {
  1251. if( (k = skill_tree[c][i].need[j].id) )
  1252. {
  1253. if( sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED )
  1254. k = 0; //Not learned.
  1255. else
  1256. if( sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1257. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1258. else
  1259. k = pc_checkskill(sd,k);
  1260. if( k < skill_tree[c][i].need[j].lv )
  1261. {
  1262. f = 0;
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. if( !f )
  1268. continue;
  1269. if( sd->status.job_level < skill_tree[c][i].joblv )
  1270. continue;
  1271. j = skill_get_inf2(id);
  1272. if( !sd->status.skill[id].lv && (
  1273. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1274. j&INF2_WEDDING_SKILL ||
  1275. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1276. ) )
  1277. continue; //Cannot be learned via normal means.
  1278. sd->status.skill[id].id = id;
  1279. flag = 1;
  1280. }
  1281. } while(flag);
  1282. }
  1283. // Make sure all the skills are in the correct condition
  1284. // before persisting to the backend.. [MouseJstr]
  1285. int pc_clean_skilltree(struct map_session_data *sd)
  1286. {
  1287. int i;
  1288. for (i = 0; i < MAX_SKILL; i++){
  1289. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED)
  1290. {
  1291. sd->status.skill[i].id = 0;
  1292. sd->status.skill[i].lv = 0;
  1293. sd->status.skill[i].flag = 0;
  1294. }
  1295. else
  1296. if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1297. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1298. sd->status.skill[i].flag = 0;
  1299. }
  1300. }
  1301. return 0;
  1302. }
  1303. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1304. {
  1305. int skill_point, novice_skills;
  1306. int c = sd->class_;
  1307. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1308. return c;
  1309. skill_point = pc_calc_skillpoint(sd);
  1310. novice_skills = max_level[pc_class2idx(JOB_NOVICE)][1] - 1;
  1311. // limit 1st class and above to novice job levels
  1312. if(skill_point < novice_skills)
  1313. {
  1314. c = MAPID_NOVICE;
  1315. }
  1316. // limit 2nd class and above to first class job levels (super novices are exempt)
  1317. else if ((sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1318. {
  1319. // regenerate change_level_2nd
  1320. if (!sd->change_level_2nd)
  1321. {
  1322. if (sd->class_&JOBL_THIRD)
  1323. {
  1324. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1325. if (!sd->change_level_3rd)
  1326. sd->change_level_2nd = max_level[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))][1];
  1327. else
  1328. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1329. - (sd->status.job_level - 1)
  1330. - (sd->change_level_3rd - 1)
  1331. - novice_skills;
  1332. }
  1333. else
  1334. {
  1335. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1336. - (sd->status.job_level - 1)
  1337. - novice_skills;
  1338. }
  1339. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  1340. }
  1341. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1342. {
  1343. c &= MAPID_BASEMASK;
  1344. }
  1345. // limit 3rd class to 2nd class/trans job levels
  1346. else if(sd->class_&JOBL_THIRD)
  1347. {
  1348. // regenerate change_level_3rd
  1349. if (!sd->change_level_3rd)
  1350. {
  1351. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1352. - (sd->status.job_level - 1)
  1353. - (sd->change_level_2nd - 1)
  1354. - novice_skills;
  1355. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  1356. }
  1357. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1358. c &= MAPID_UPPERMASK;
  1359. }
  1360. }
  1361. // restore non-limiting flags
  1362. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1363. return c;
  1364. }
  1365. /*==========================================
  1366. * Updates the weight status
  1367. *------------------------------------------
  1368. * 1: overweight 50%
  1369. * 2: overweight 90%
  1370. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1371. */
  1372. int pc_updateweightstatus(struct map_session_data *sd)
  1373. {
  1374. int old_overweight;
  1375. int new_overweight;
  1376. nullpo_retr(1, sd);
  1377. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1378. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1379. if( old_overweight == new_overweight )
  1380. return 0; // no change
  1381. // stop old status change
  1382. if( old_overweight == 1 )
  1383. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1384. else if( old_overweight == 2 )
  1385. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1386. // start new status change
  1387. if( new_overweight == 1 )
  1388. sc_start(&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1389. else if( new_overweight == 2 )
  1390. sc_start(&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1391. // update overweight status
  1392. sd->regen.state.overweight = new_overweight;
  1393. return 0;
  1394. }
  1395. int pc_disguise(struct map_session_data *sd, int class_)
  1396. {
  1397. if (!class_ && !sd->disguise)
  1398. return 0;
  1399. if (class_ && sd->disguise == class_)
  1400. return 0;
  1401. if(sd->sc.option&OPTION_INVISIBLE)
  1402. { //Character is invisible. Stealth class-change. [Skotlex]
  1403. sd->disguise = class_; //viewdata is set on uncloaking.
  1404. return 2;
  1405. }
  1406. if (sd->bl.prev != NULL) {
  1407. pc_stop_walking(sd, 0);
  1408. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1409. }
  1410. if (!class_) {
  1411. sd->disguise = 0;
  1412. class_ = sd->status.class_;
  1413. } else
  1414. sd->disguise=class_;
  1415. status_set_viewdata(&sd->bl, class_);
  1416. clif_changeoption(&sd->bl);
  1417. if (sd->bl.prev != NULL) {
  1418. clif_spawn(&sd->bl);
  1419. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1420. { //It seems the cart info is lost on undisguise.
  1421. clif_cartlist(sd);
  1422. clif_updatestatus(sd,SP_CARTINFO);
  1423. }
  1424. }
  1425. return 1;
  1426. }
  1427. static int pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, short card_id)
  1428. {
  1429. int i;
  1430. if( !rate )
  1431. return 0;
  1432. for( i = 0; i < max && spell[i].id; i++ )
  1433. {
  1434. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1435. {
  1436. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1437. return 0;
  1438. rate += spell[i].rate;
  1439. break;
  1440. }
  1441. }
  1442. if (i == max) {
  1443. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1444. return 0;
  1445. }
  1446. spell[i].id = id;
  1447. spell[i].lv = lv;
  1448. spell[i].rate = rate;
  1449. //Auto-update flag value.
  1450. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1451. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1452. if (!(flag&BF_SKILLMASK)) {
  1453. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1454. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1455. }
  1456. spell[i].flag|= flag;
  1457. spell[i].card_id = card_id;
  1458. return 1;
  1459. }
  1460. static int pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, short card_id)
  1461. {
  1462. int i;
  1463. if( !rate )
  1464. return 0;
  1465. for( i = 0; i < max && spell[i].id; i++ )
  1466. {
  1467. ; // each autospell works independently
  1468. }
  1469. if( i == max )
  1470. {
  1471. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1472. return 0;
  1473. }
  1474. spell[i].flag = src_skill;
  1475. spell[i].id = id;
  1476. spell[i].lv = lv;
  1477. spell[i].rate = rate;
  1478. spell[i].card_id = card_id;
  1479. return 1;
  1480. }
  1481. static int pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, short rate, short arrow_rate, unsigned char flag)
  1482. {
  1483. int i;
  1484. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1485. flag|=ATF_SHORT|ATF_LONG; //Default range: both
  1486. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1487. flag|=ATF_TARGET; //Default target: enemy.
  1488. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1489. flag|=ATF_WEAPON; //Default type: weapon.
  1490. for (i = 0; i < max && effect[i].flag; i++) {
  1491. if (effect[i].id == id && effect[i].flag == flag)
  1492. {
  1493. effect[i].rate += rate;
  1494. effect[i].arrow_rate += arrow_rate;
  1495. return 1;
  1496. }
  1497. }
  1498. if (i == max) {
  1499. ShowWarning("pc_bonus: Reached max (%d) number of add effects per character!\n", max);
  1500. return 0;
  1501. }
  1502. effect[i].id = id;
  1503. effect[i].rate = rate;
  1504. effect[i].arrow_rate = arrow_rate;
  1505. effect[i].flag = flag;
  1506. return 1;
  1507. }
  1508. static int pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill, unsigned char target)
  1509. {
  1510. int i;
  1511. for( i = 0; i < max && effect[i].skill; i++ )
  1512. {
  1513. if( effect[i].id == id && effect[i].skill == skill && effect[i].target == target )
  1514. {
  1515. effect[i].rate += rate;
  1516. return 1;
  1517. }
  1518. }
  1519. if( i == max ) {
  1520. ShowWarning("pc_bonus: Reached max (%d) number of add effects on skill per character!\n", max);
  1521. return 0;
  1522. }
  1523. effect[i].id = id;
  1524. effect[i].rate = rate;
  1525. effect[i].skill = skill;
  1526. effect[i].target = target;
  1527. return 1;
  1528. }
  1529. static int pc_bonus_item_drop(struct s_add_drop *drop, const short max, short id, short group, int race, int rate)
  1530. {
  1531. int i;
  1532. //Apply config rate adjustment settings.
  1533. if (rate >= 0) { //Absolute drop.
  1534. if (battle_config.item_rate_adddrop != 100)
  1535. rate = rate*battle_config.item_rate_adddrop/100;
  1536. if (rate < battle_config.item_drop_adddrop_min)
  1537. rate = battle_config.item_drop_adddrop_min;
  1538. else if (rate > battle_config.item_drop_adddrop_max)
  1539. rate = battle_config.item_drop_adddrop_max;
  1540. } else { //Relative drop, max/min limits are applied at drop time.
  1541. if (battle_config.item_rate_adddrop != 100)
  1542. rate = rate*battle_config.item_rate_adddrop/100;
  1543. if (rate > -1)
  1544. rate = -1;
  1545. }
  1546. for(i = 0; i < max && (drop[i].id || drop[i].group); i++) {
  1547. if(
  1548. ((id && drop[i].id == id) ||
  1549. (group && drop[i].group == group))
  1550. && race > 0
  1551. ) {
  1552. drop[i].race |= race;
  1553. if(drop[i].rate > 0 && rate > 0)
  1554. { //Both are absolute rates.
  1555. if (drop[i].rate < rate)
  1556. drop[i].rate = rate;
  1557. } else
  1558. if(drop[i].rate < 0 && rate < 0) {
  1559. //Both are relative rates.
  1560. if (drop[i].rate > rate)
  1561. drop[i].rate = rate;
  1562. } else if (rate < 0) //Give preference to relative rate.
  1563. drop[i].rate = rate;
  1564. return 1;
  1565. }
  1566. }
  1567. if(i == max) {
  1568. ShowWarning("pc_bonus: Reached max (%d) number of added drops per character!\n", max);
  1569. return 0;
  1570. }
  1571. drop[i].id = id;
  1572. drop[i].group = group;
  1573. drop[i].race |= race;
  1574. drop[i].rate = rate;
  1575. return 1;
  1576. }
  1577. 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)
  1578. {
  1579. int i;
  1580. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1581. if( i == max )
  1582. {
  1583. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1584. return 0;
  1585. }
  1586. if( !onskill )
  1587. {
  1588. if( !(flag&BF_RANGEMASK) )
  1589. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1590. if( !(flag&BF_WEAPONMASK) )
  1591. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1592. if( !(flag&BF_SKILLMASK) )
  1593. {
  1594. if( flag&(BF_MAGIC|BF_MISC) )
  1595. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1596. if( flag&BF_WEAPON )
  1597. flag|=BF_NORMAL|BF_SKILL;
  1598. }
  1599. }
  1600. bonus[i].rate = rate;
  1601. bonus[i].duration = dur;
  1602. bonus[i].active = INVALID_TIMER;
  1603. bonus[i].atk_type = flag;
  1604. bonus[i].pos = pos;
  1605. bonus[i].bonus_script = aStrdup(script);
  1606. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1607. return 1;
  1608. }
  1609. int pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1610. {
  1611. int i;
  1612. nullpo_ret(sd);
  1613. for( i = 0; i < max; i++ )
  1614. {
  1615. if( autobonus[i].active != INVALID_TIMER )
  1616. {
  1617. if( restore && sd->state.autobonus&autobonus[i].pos )
  1618. {
  1619. if( autobonus[i].bonus_script )
  1620. {
  1621. int j;
  1622. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus[i].pos );
  1623. if( j < EQI_MAX-1 )
  1624. script_run_autobonus(autobonus[i].bonus_script,sd->bl.id,sd->equip_index[j]);
  1625. }
  1626. continue;
  1627. }
  1628. else
  1629. { // Logout / Unequipped an item with an activated bonus
  1630. delete_timer(autobonus[i].active,pc_endautobonus);
  1631. autobonus[i].active = INVALID_TIMER;
  1632. }
  1633. }
  1634. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1635. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1636. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1637. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1638. autobonus[i].active = INVALID_TIMER;
  1639. }
  1640. return 0;
  1641. }
  1642. int pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1643. {
  1644. nullpo_ret(sd);
  1645. nullpo_ret(autobonus);
  1646. if( autobonus->other_script )
  1647. {
  1648. int j;
  1649. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus->pos );
  1650. if( j < EQI_MAX-1 )
  1651. script_run_autobonus(autobonus->other_script,sd->bl.id,sd->equip_index[j]);
  1652. }
  1653. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1654. sd->state.autobonus |= autobonus->pos;
  1655. status_calc_pc(sd,0);
  1656. return 0;
  1657. }
  1658. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1659. {
  1660. struct map_session_data *sd = map_id2sd(id);
  1661. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1662. nullpo_ret(sd);
  1663. nullpo_ret(autobonus);
  1664. autobonus->active = INVALID_TIMER;
  1665. sd->state.autobonus &= ~autobonus->pos;
  1666. status_calc_pc(sd,0);
  1667. return 0;
  1668. }
  1669. int pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1670. {
  1671. int i;
  1672. struct weapon_data* wd;
  1673. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  1674. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  1675. if (i == MAX_PC_BONUS)
  1676. {
  1677. ShowWarning("pc_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1678. return 0;
  1679. }
  1680. if (!(flag&BF_RANGEMASK))
  1681. flag |= BF_SHORT|BF_LONG;
  1682. if (!(flag&BF_WEAPONMASK))
  1683. flag |= BF_WEAPON;
  1684. if (!(flag&BF_SKILLMASK))
  1685. {
  1686. if (flag&(BF_MAGIC|BF_MISC))
  1687. flag |= BF_SKILL;
  1688. if (flag&BF_WEAPON)
  1689. flag |= BF_NORMAL|BF_SKILL;
  1690. }
  1691. wd->addele2[i].ele = ele;
  1692. wd->addele2[i].rate = rate;
  1693. wd->addele2[i].flag = flag;
  1694. return 0;
  1695. }
  1696. int pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1697. {
  1698. int i;
  1699. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  1700. if (i == MAX_PC_BONUS)
  1701. {
  1702. ShowWarning("pc_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1703. return 0;
  1704. }
  1705. if (!(flag&BF_RANGEMASK))
  1706. flag |= BF_SHORT|BF_LONG;
  1707. if (!(flag&BF_WEAPONMASK))
  1708. flag |= BF_WEAPON;
  1709. if (!(flag&BF_SKILLMASK))
  1710. {
  1711. if (flag&(BF_MAGIC|BF_MISC))
  1712. flag |= BF_SKILL;
  1713. if (flag&BF_WEAPON)
  1714. flag |= BF_NORMAL|BF_SKILL;
  1715. }
  1716. sd->subele2[i].ele = ele;
  1717. sd->subele2[i].rate = rate;
  1718. sd->subele2[i].flag = flag;
  1719. return 0;
  1720. }
  1721. /*==========================================
  1722. * Add a bonus(type) to player sd
  1723. *------------------------------------------*/
  1724. int pc_bonus(struct map_session_data *sd,int type,int val)
  1725. {
  1726. struct status_data *status;
  1727. int bonus;
  1728. nullpo_ret(sd);
  1729. status = &sd->base_status;
  1730. switch(type){
  1731. case SP_STR:
  1732. case SP_AGI:
  1733. case SP_VIT:
  1734. case SP_INT:
  1735. case SP_DEX:
  1736. case SP_LUK:
  1737. if(sd->state.lr_flag != 2)
  1738. sd->param_bonus[type-SP_STR]+=val;
  1739. break;
  1740. case SP_ATK1:
  1741. if(!sd->state.lr_flag) {
  1742. bonus = status->rhw.atk + val;
  1743. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1744. }
  1745. else if(sd->state.lr_flag == 1) {
  1746. bonus = status->lhw.atk + val;
  1747. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1748. }
  1749. break;
  1750. case SP_ATK2:
  1751. if(!sd->state.lr_flag) {
  1752. bonus = status->rhw.atk2 + val;
  1753. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1754. }
  1755. else if(sd->state.lr_flag == 1) {
  1756. bonus = status->lhw.atk2 + val;
  1757. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1758. }
  1759. break;
  1760. case SP_BASE_ATK:
  1761. if(sd->state.lr_flag != 2) {
  1762. //#ifdef RENEWAL
  1763. // sd->bonus.eatk += val;
  1764. //#else
  1765. bonus = status->batk + val;
  1766. status->batk = cap_value(bonus, 0, USHRT_MAX);
  1767. //#endif
  1768. }
  1769. break;
  1770. case SP_DEF1:
  1771. if(sd->state.lr_flag != 2) {
  1772. bonus = status->def + val;
  1773. #ifdef RENEWAL
  1774. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1775. #else
  1776. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1777. #endif
  1778. }
  1779. break;
  1780. case SP_DEF2:
  1781. if(sd->state.lr_flag != 2) {
  1782. bonus = status->def2 + val;
  1783. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1784. }
  1785. break;
  1786. case SP_MDEF1:
  1787. if(sd->state.lr_flag != 2) {
  1788. bonus = status->mdef + val;
  1789. #ifdef RENEWAL
  1790. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1791. #else
  1792. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1793. #endif
  1794. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  1795. sd->bonus.shieldmdef += bonus;
  1796. }
  1797. }
  1798. break;
  1799. case SP_MDEF2:
  1800. if(sd->state.lr_flag != 2) {
  1801. bonus = status->mdef2 + val;
  1802. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1803. }
  1804. break;
  1805. case SP_HIT:
  1806. if(sd->state.lr_flag != 2) {
  1807. bonus = status->hit + val;
  1808. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1809. } else
  1810. sd->bonus.arrow_hit+=val;
  1811. break;
  1812. case SP_FLEE1:
  1813. if(sd->state.lr_flag != 2) {
  1814. bonus = status->flee + val;
  1815. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1816. }
  1817. break;
  1818. case SP_FLEE2:
  1819. if(sd->state.lr_flag != 2) {
  1820. bonus = status->flee2 + val*10;
  1821. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1822. }
  1823. break;
  1824. case SP_CRITICAL:
  1825. if(sd->state.lr_flag != 2) {
  1826. bonus = status->cri + val*10;
  1827. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1828. } else
  1829. sd->bonus.arrow_cri += val*10;
  1830. break;
  1831. case SP_ATKELE:
  1832. if(val >= ELE_MAX) {
  1833. ShowError("pc_bonus: SP_ATKELE: Invalid element %d\n", val);
  1834. break;
  1835. }
  1836. switch (sd->state.lr_flag)
  1837. {
  1838. case 2:
  1839. switch (sd->status.weapon) {
  1840. case W_BOW:
  1841. case W_REVOLVER:
  1842. case W_RIFLE:
  1843. case W_GATLING:
  1844. case W_SHOTGUN:
  1845. case W_GRENADE:
  1846. //Become weapon element.
  1847. status->rhw.ele=val;
  1848. break;
  1849. default: //Become arrow element.
  1850. sd->bonus.arrow_ele=val;
  1851. break;
  1852. }
  1853. break;
  1854. case 1:
  1855. status->lhw.ele=val;
  1856. break;
  1857. default:
  1858. status->rhw.ele=val;
  1859. break;
  1860. }
  1861. break;
  1862. case SP_DEFELE:
  1863. if(val >= ELE_MAX) {
  1864. ShowError("pc_bonus: SP_DEFELE: Invalid element %d\n", val);
  1865. break;
  1866. }
  1867. if(sd->state.lr_flag != 2)
  1868. status->def_ele=val;
  1869. break;
  1870. case SP_MAXHP:
  1871. if(sd->state.lr_flag == 2)
  1872. break;
  1873. val += (int)status->max_hp;
  1874. //Negative bonuses will underflow, this will be handled in status_calc_pc through casting
  1875. //If this is called outside of status_calc_pc, you'd better pray they do not underflow and end with UINT_MAX max_hp.
  1876. status->max_hp = (unsigned int)val;
  1877. break;
  1878. case SP_MAXSP:
  1879. if(sd->state.lr_flag == 2)
  1880. break;
  1881. val += (int)status->max_sp;
  1882. status->max_sp = (unsigned int)val;
  1883. break;
  1884. #ifndef RENEWAL_CAST
  1885. case SP_VARCASTRATE:
  1886. #endif
  1887. case SP_CASTRATE:
  1888. if(sd->state.lr_flag != 2)
  1889. sd->castrate+=val;
  1890. break;
  1891. case SP_MAXHPRATE:
  1892. if(sd->state.lr_flag != 2)
  1893. sd->hprate+=val;
  1894. break;
  1895. case SP_MAXSPRATE:
  1896. if(sd->state.lr_flag != 2)
  1897. sd->sprate+=val;
  1898. break;
  1899. case SP_SPRATE:
  1900. if(sd->state.lr_flag != 2)
  1901. sd->dsprate+=val;
  1902. break;
  1903. case SP_ATTACKRANGE:
  1904. switch (sd->state.lr_flag) {
  1905. case 2:
  1906. switch (sd->status.weapon) {
  1907. case W_BOW:
  1908. case W_REVOLVER:
  1909. case W_RIFLE:
  1910. case W_GATLING:
  1911. case W_SHOTGUN:
  1912. case W_GRENADE:
  1913. status->rhw.range += val;
  1914. }
  1915. break;
  1916. case 1:
  1917. status->lhw.range += val;
  1918. break;
  1919. default:
  1920. status->rhw.range += val;
  1921. break;
  1922. }
  1923. break;
  1924. case SP_SPEED_RATE: //Non stackable increase
  1925. if(sd->state.lr_flag != 2)
  1926. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  1927. break;
  1928. case SP_SPEED_ADDRATE: //Stackable increase
  1929. if(sd->state.lr_flag != 2)
  1930. sd->bonus.speed_add_rate -= val;
  1931. break;
  1932. case SP_ASPD: //Raw increase
  1933. if(sd->state.lr_flag != 2)
  1934. sd->bonus.aspd_add -= 10*val;
  1935. break;
  1936. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  1937. if(sd->state.lr_flag != 2)
  1938. #ifndef RENEWAL_ASPD
  1939. status->aspd_rate -= 10*val;
  1940. #else
  1941. status->aspd_rate2 += val;
  1942. #endif
  1943. break;
  1944. case SP_HP_RECOV_RATE:
  1945. if(sd->state.lr_flag != 2)
  1946. sd->hprecov_rate += val;
  1947. break;
  1948. case SP_SP_RECOV_RATE:
  1949. if(sd->state.lr_flag != 2)
  1950. sd->sprecov_rate += val;
  1951. break;
  1952. case SP_CRITICAL_DEF:
  1953. if(sd->state.lr_flag != 2)
  1954. sd->bonus.critical_def += val;
  1955. break;
  1956. case SP_NEAR_ATK_DEF:
  1957. if(sd->state.lr_flag != 2)
  1958. sd->bonus.near_attack_def_rate += val;
  1959. break;
  1960. case SP_LONG_ATK_DEF:
  1961. if(sd->state.lr_flag != 2)
  1962. sd->bonus.long_attack_def_rate += val;
  1963. break;
  1964. case SP_DOUBLE_RATE:
  1965. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  1966. sd->bonus.double_rate = val;
  1967. break;
  1968. case SP_DOUBLE_ADD_RATE:
  1969. if(sd->state.lr_flag == 0)
  1970. sd->bonus.double_add_rate += val;
  1971. break;
  1972. case SP_MATK_RATE:
  1973. if(sd->state.lr_flag != 2)
  1974. sd->matk_rate += val;
  1975. break;
  1976. case SP_IGNORE_DEF_ELE:
  1977. if(val >= ELE_MAX) {
  1978. ShowError("pc_bonus: SP_IGNORE_DEF_ELE: Invalid element %d\n", val);
  1979. break;
  1980. }
  1981. if(!sd->state.lr_flag)
  1982. sd->right_weapon.ignore_def_ele |= 1<<val;
  1983. else if(sd->state.lr_flag == 1)
  1984. sd->left_weapon.ignore_def_ele |= 1<<val;
  1985. break;
  1986. case SP_IGNORE_DEF_RACE:
  1987. if(!sd->state.lr_flag)
  1988. sd->right_weapon.ignore_def_race |= 1<<val;
  1989. else if(sd->state.lr_flag == 1)
  1990. sd->left_weapon.ignore_def_race |= 1<<val;
  1991. break;
  1992. case SP_ATK_RATE:
  1993. if(sd->state.lr_flag != 2)
  1994. sd->bonus.atk_rate += val;
  1995. break;
  1996. case SP_MAGIC_ATK_DEF:
  1997. if(sd->state.lr_flag != 2)
  1998. sd->bonus.magic_def_rate += val;
  1999. break;
  2000. case SP_MISC_ATK_DEF:
  2001. if(sd->state.lr_flag != 2)
  2002. sd->bonus.misc_def_rate += val;
  2003. break;
  2004. case SP_IGNORE_MDEF_RATE:
  2005. if(sd->state.lr_flag != 2) {
  2006. sd->ignore_mdef[RC_NONBOSS] += val;
  2007. sd->ignore_mdef[RC_BOSS] += val;
  2008. }
  2009. break;
  2010. case SP_IGNORE_MDEF_ELE:
  2011. if(val >= ELE_MAX) {
  2012. ShowError("pc_bonus: SP_IGNORE_MDEF_ELE: Invalid element %d\n", val);
  2013. break;
  2014. }
  2015. if(sd->state.lr_flag != 2)
  2016. sd->bonus.ignore_mdef_ele |= 1<<val;
  2017. break;
  2018. case SP_IGNORE_MDEF_RACE:
  2019. if(sd->state.lr_flag != 2)
  2020. sd->bonus.ignore_mdef_race |= 1<<val;
  2021. break;
  2022. case SP_PERFECT_HIT_RATE:
  2023. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2024. sd->bonus.perfect_hit = val;
  2025. break;
  2026. case SP_PERFECT_HIT_ADD_RATE:
  2027. if(sd->state.lr_flag != 2)
  2028. sd->bonus.perfect_hit_add += val;
  2029. break;
  2030. case SP_CRITICAL_RATE:
  2031. if(sd->state.lr_flag != 2)
  2032. sd->critical_rate+=val;
  2033. break;
  2034. case SP_DEF_RATIO_ATK_ELE:
  2035. if(val >= ELE_MAX) {
  2036. ShowError("pc_bonus: SP_DEF_RATIO_ATK_ELE: Invalid element %d\n", val);
  2037. break;
  2038. }
  2039. if(!sd->state.lr_flag)
  2040. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2041. else if(sd->state.lr_flag == 1)
  2042. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2043. break;
  2044. case SP_DEF_RATIO_ATK_RACE:
  2045. if(val >= RC_MAX) {
  2046. ShowError("pc_bonus: SP_DEF_RATIO_ATK_RACE: Invalid race %d\n", val);
  2047. break;
  2048. }
  2049. if(!sd->state.lr_flag)
  2050. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2051. else if(sd->state.lr_flag == 1)
  2052. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2053. break;
  2054. case SP_HIT_RATE:
  2055. if(sd->state.lr_flag != 2)
  2056. sd->hit_rate += val;
  2057. break;
  2058. case SP_FLEE_RATE:
  2059. if(sd->state.lr_flag != 2)
  2060. sd->flee_rate += val;
  2061. break;
  2062. case SP_FLEE2_RATE:
  2063. if(sd->state.lr_flag != 2)
  2064. sd->flee2_rate += val;
  2065. break;
  2066. case SP_DEF_RATE:
  2067. if(sd->state.lr_flag != 2)
  2068. sd->def_rate += val;
  2069. break;
  2070. case SP_DEF2_RATE:
  2071. if(sd->state.lr_flag != 2)
  2072. sd->def2_rate += val;
  2073. break;
  2074. case SP_MDEF_RATE:
  2075. if(sd->state.lr_flag != 2)
  2076. sd->mdef_rate += val;
  2077. break;
  2078. case SP_MDEF2_RATE:
  2079. if(sd->state.lr_flag != 2)
  2080. sd->mdef2_rate += val;
  2081. break;
  2082. case SP_RESTART_FULL_RECOVER:
  2083. if(sd->state.lr_flag != 2)
  2084. sd->special_state.restart_full_recover = 1;
  2085. break;
  2086. case SP_NO_CASTCANCEL:
  2087. if(sd->state.lr_flag != 2)
  2088. sd->special_state.no_castcancel = 1;
  2089. break;
  2090. case SP_NO_CASTCANCEL2:
  2091. if(sd->state.lr_flag != 2)
  2092. sd->special_state.no_castcancel2 = 1;
  2093. break;
  2094. case SP_NO_SIZEFIX:
  2095. if(sd->state.lr_flag != 2)
  2096. sd->special_state.no_sizefix = 1;
  2097. break;
  2098. case SP_NO_MAGIC_DAMAGE:
  2099. if(sd->state.lr_flag == 2)
  2100. break;
  2101. val+= sd->special_state.no_magic_damage;
  2102. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2103. break;
  2104. case SP_NO_WEAPON_DAMAGE:
  2105. if(sd->state.lr_flag == 2)
  2106. break;
  2107. val+= sd->special_state.no_weapon_damage;
  2108. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2109. break;
  2110. case SP_NO_MISC_DAMAGE:
  2111. if(sd->state.lr_flag == 2)
  2112. break;
  2113. val+= sd->special_state.no_misc_damage;
  2114. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2115. break;
  2116. case SP_NO_GEMSTONE:
  2117. if(sd->state.lr_flag != 2)
  2118. sd->special_state.no_gemstone = 1;
  2119. break;
  2120. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2121. if(sd->state.lr_flag != 2) {
  2122. sd->special_state.intravision = 1;
  2123. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2124. }
  2125. break;
  2126. case SP_NO_KNOCKBACK:
  2127. if(sd->state.lr_flag != 2)
  2128. sd->special_state.no_knockback = 1;
  2129. break;
  2130. case SP_SPLASH_RANGE:
  2131. if(sd->bonus.splash_range < val)
  2132. sd->bonus.splash_range = val;
  2133. break;
  2134. case SP_SPLASH_ADD_RANGE:
  2135. sd->bonus.splash_add_range += val;
  2136. break;
  2137. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2138. if(sd->state.lr_flag != 2)
  2139. sd->bonus.short_weapon_damage_return += val;
  2140. break;
  2141. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2142. if(sd->state.lr_flag != 2)
  2143. sd->bonus.long_weapon_damage_return += val;
  2144. break;
  2145. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2146. if(sd->state.lr_flag != 2)
  2147. sd->bonus.magic_damage_return += val;
  2148. break;
  2149. case SP_ALL_STATS: // [Valaris]
  2150. if(sd->state.lr_flag!=2) {
  2151. sd->param_bonus[SP_STR-SP_STR]+=val;
  2152. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2153. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2154. sd->param_bonus[SP_INT-SP_STR]+=val;
  2155. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2156. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2157. }
  2158. break;
  2159. case SP_AGI_VIT: // [Valaris]
  2160. if(sd->state.lr_flag!=2) {
  2161. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2162. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2163. }
  2164. break;
  2165. case SP_AGI_DEX_STR: // [Valaris]
  2166. if(sd->state.lr_flag!=2) {
  2167. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2168. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2169. sd->param_bonus[SP_STR-SP_STR]+=val;
  2170. }
  2171. break;
  2172. case SP_PERFECT_HIDE: // [Valaris]
  2173. if(sd->state.lr_flag!=2)
  2174. sd->special_state.perfect_hiding=1;
  2175. break;
  2176. case SP_UNBREAKABLE:
  2177. if(sd->state.lr_flag!=2)
  2178. sd->bonus.unbreakable += val;
  2179. break;
  2180. case SP_UNBREAKABLE_WEAPON:
  2181. if(sd->state.lr_flag != 2)
  2182. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2183. break;
  2184. case SP_UNBREAKABLE_ARMOR:
  2185. if(sd->state.lr_flag != 2)
  2186. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2187. break;
  2188. case SP_UNBREAKABLE_HELM:
  2189. if(sd->state.lr_flag != 2)
  2190. sd->bonus.unbreakable_equip |= EQP_HELM;
  2191. break;
  2192. case SP_UNBREAKABLE_SHIELD:
  2193. if(sd->state.lr_flag != 2)
  2194. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2195. break;
  2196. case SP_UNBREAKABLE_GARMENT:
  2197. if(sd->state.lr_flag != 2)
  2198. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2199. break;
  2200. case SP_UNBREAKABLE_SHOES:
  2201. if(sd->state.lr_flag != 2)
  2202. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2203. break;
  2204. case SP_CLASSCHANGE: // [Valaris]
  2205. if(sd->state.lr_flag !=2)
  2206. sd->bonus.classchange=val;
  2207. break;
  2208. case SP_LONG_ATK_RATE:
  2209. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2210. sd->bonus.long_attack_atk_rate+=val;
  2211. break;
  2212. case SP_BREAK_WEAPON_RATE:
  2213. if(sd->state.lr_flag != 2)
  2214. sd->bonus.break_weapon_rate+=val;
  2215. break;
  2216. case SP_BREAK_ARMOR_RATE:
  2217. if(sd->state.lr_flag != 2)
  2218. sd->bonus.break_armor_rate+=val;
  2219. break;
  2220. case SP_ADD_STEAL_RATE:
  2221. if(sd->state.lr_flag != 2)
  2222. sd->bonus.add_steal_rate+=val;
  2223. break;
  2224. case SP_DELAYRATE:
  2225. if(sd->state.lr_flag != 2)
  2226. sd->delayrate+=val;
  2227. break;
  2228. case SP_CRIT_ATK_RATE:
  2229. if(sd->state.lr_flag != 2)
  2230. sd->bonus.crit_atk_rate += val;
  2231. break;
  2232. case SP_NO_REGEN:
  2233. if(sd->state.lr_flag != 2)
  2234. sd->regen.state.block|=val;
  2235. break;
  2236. case SP_UNSTRIPABLE_WEAPON:
  2237. if(sd->state.lr_flag != 2)
  2238. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2239. break;
  2240. case SP_UNSTRIPABLE:
  2241. case SP_UNSTRIPABLE_ARMOR:
  2242. if(sd->state.lr_flag != 2)
  2243. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2244. break;
  2245. case SP_UNSTRIPABLE_HELM:
  2246. if(sd->state.lr_flag != 2)
  2247. sd->bonus.unstripable_equip |= EQP_HELM;
  2248. break;
  2249. case SP_UNSTRIPABLE_SHIELD:
  2250. if(sd->state.lr_flag != 2)
  2251. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2252. break;
  2253. case SP_HP_DRAIN_VALUE:
  2254. if(!sd->state.lr_flag) {
  2255. sd->right_weapon.hp_drain[RC_NONBOSS].value += val;
  2256. sd->right_weapon.hp_drain[RC_BOSS].value += val;
  2257. }
  2258. else if(sd->state.lr_flag == 1) {
  2259. sd->left_weapon.hp_drain[RC_NONBOSS].value += val;
  2260. sd->left_weapon.hp_drain[RC_BOSS].value += val;
  2261. }
  2262. break;
  2263. case SP_SP_DRAIN_VALUE:
  2264. if(!sd->state.lr_flag) {
  2265. sd->right_weapon.sp_drain[RC_NONBOSS].value += val;
  2266. sd->right_weapon.sp_drain[RC_BOSS].value += val;
  2267. }
  2268. else if(sd->state.lr_flag == 1) {
  2269. sd->left_weapon.sp_drain[RC_NONBOSS].value += val;
  2270. sd->left_weapon.sp_drain[RC_BOSS].value += val;
  2271. }
  2272. break;
  2273. case SP_SP_GAIN_VALUE:
  2274. if(!sd->state.lr_flag)
  2275. sd->bonus.sp_gain_value += val;
  2276. break;
  2277. case SP_HP_GAIN_VALUE:
  2278. if(!sd->state.lr_flag)
  2279. sd->bonus.hp_gain_value += val;
  2280. break;
  2281. case SP_MAGIC_SP_GAIN_VALUE:
  2282. if(!sd->state.lr_flag)
  2283. sd->bonus.magic_sp_gain_value += val;
  2284. break;
  2285. case SP_MAGIC_HP_GAIN_VALUE:
  2286. if(!sd->state.lr_flag)
  2287. sd->bonus.magic_hp_gain_value += val;
  2288. break;
  2289. case SP_ADD_HEAL_RATE:
  2290. if(sd->state.lr_flag != 2)
  2291. sd->bonus.add_heal_rate += val;
  2292. break;
  2293. case SP_ADD_HEAL2_RATE:
  2294. if(sd->state.lr_flag != 2)
  2295. sd->bonus.add_heal2_rate += val;
  2296. break;
  2297. case SP_ADD_ITEM_HEAL_RATE:
  2298. if(sd->state.lr_flag != 2)
  2299. sd->bonus.itemhealrate2 += val;
  2300. break;
  2301. case SP_EMATK:
  2302. if(sd->state.lr_flag != 2)
  2303. sd->bonus.ematk += val;
  2304. break;
  2305. case SP_FIXCASTRATE:
  2306. if(sd->state.lr_flag != 2)
  2307. sd->bonus.fixcastrate -= val;
  2308. break;
  2309. #ifdef RENEWAL_CAST
  2310. case SP_VARCASTRATE:
  2311. if(sd->state.lr_flag != 2)
  2312. sd->bonus.varcastrate -= val;
  2313. break;
  2314. #endif
  2315. default:
  2316. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2317. break;
  2318. }
  2319. return 0;
  2320. }
  2321. /*==========================================
  2322. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2323. *------------------------------------------*/
  2324. int pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2325. {
  2326. int i;
  2327. nullpo_ret(sd);
  2328. switch(type){
  2329. case SP_ADDELE:
  2330. if(type2 >= ELE_MAX) {
  2331. ShowError("pc_bonus2: SP_ADDELE: Invalid element %d\n", type2);
  2332. break;
  2333. }
  2334. if(!sd->state.lr_flag)
  2335. sd->right_weapon.addele[type2]+=val;
  2336. else if(sd->state.lr_flag == 1)
  2337. sd->left_weapon.addele[type2]+=val;
  2338. else if(sd->state.lr_flag == 2)
  2339. sd->arrow_addele[type2]+=val;
  2340. break;
  2341. case SP_ADDRACE:
  2342. if(!sd->state.lr_flag)
  2343. sd->right_weapon.addrace[type2]+=val;
  2344. else if(sd->state.lr_flag == 1)
  2345. sd->left_weapon.addrace[type2]+=val;
  2346. else if(sd->state.lr_flag == 2)
  2347. sd->arrow_addrace[type2]+=val;
  2348. break;
  2349. case SP_ADDSIZE:
  2350. if(!sd->state.lr_flag)
  2351. sd->right_weapon.addsize[type2]+=val;
  2352. else if(sd->state.lr_flag == 1)
  2353. sd->left_weapon.addsize[type2]+=val;
  2354. else if(sd->state.lr_flag == 2)
  2355. sd->arrow_addsize[type2]+=val;
  2356. break;
  2357. case SP_SUBELE:
  2358. if(type2 >= ELE_MAX) {
  2359. ShowError("pc_bonus2: SP_SUBELE: Invalid element %d\n", type2);
  2360. break;
  2361. }
  2362. if(sd->state.lr_flag != 2)
  2363. sd->subele[type2]+=val;
  2364. break;
  2365. case SP_SUBRACE:
  2366. if(sd->state.lr_flag != 2)
  2367. sd->subrace[type2]+=val;
  2368. break;
  2369. case SP_ADDEFF:
  2370. if (type2 > SC_MAX) {
  2371. ShowWarning("pc_bonus2 (Add Effect): %d is not supported.\n", type2);
  2372. break;
  2373. }
  2374. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2375. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0);
  2376. break;
  2377. case SP_ADDEFF2:
  2378. if (type2 > SC_MAX) {
  2379. ShowWarning("pc_bonus2 (Add Effect2): %d is not supported.\n", type2);
  2380. break;
  2381. }
  2382. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2383. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF);
  2384. break;
  2385. case SP_RESEFF:
  2386. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2387. ShowWarning("pc_bonus2 (Resist Effect): %d is not supported.\n", type2);
  2388. break;
  2389. }
  2390. if(sd->state.lr_flag == 2)
  2391. break;
  2392. i = sd->reseff[type2-SC_COMMON_MIN]+val;
  2393. sd->reseff[type2-SC_COMMON_MIN]= cap_value(i, 0, 10000);
  2394. break;
  2395. case SP_MAGIC_ADDELE:
  2396. if(type2 >= ELE_MAX) {
  2397. ShowError("pc_bonus2: SP_MAGIC_ADDELE: Invalid element %d\n", type2);
  2398. break;
  2399. }
  2400. if(sd->state.lr_flag != 2)
  2401. sd->magic_addele[type2]+=val;
  2402. break;
  2403. case SP_MAGIC_ADDRACE:
  2404. if(sd->state.lr_flag != 2)
  2405. sd->magic_addrace[type2]+=val;
  2406. break;
  2407. case SP_MAGIC_ADDSIZE:
  2408. if(sd->state.lr_flag != 2)
  2409. sd->magic_addsize[type2]+=val;
  2410. break;
  2411. case SP_MAGIC_ATK_ELE:
  2412. if(sd->state.lr_flag != 2)
  2413. sd->magic_atk_ele[type2]+=val;
  2414. break;
  2415. case SP_ADD_DAMAGE_CLASS:
  2416. switch (sd->state.lr_flag) {
  2417. case 0: //Right hand
  2418. 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);
  2419. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2420. {
  2421. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2422. break;
  2423. }
  2424. sd->right_weapon.add_dmg[i].class_ = type2;
  2425. sd->right_weapon.add_dmg[i].rate += val;
  2426. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2427. 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]));
  2428. break;
  2429. case 1: //Left hand
  2430. 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);
  2431. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2432. {
  2433. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2434. break;
  2435. }
  2436. sd->left_weapon.add_dmg[i].class_ = type2;
  2437. sd->left_weapon.add_dmg[i].rate += val;
  2438. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2439. 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]));
  2440. break;
  2441. }
  2442. break;
  2443. case SP_ADD_MAGIC_DAMAGE_CLASS:
  2444. if(sd->state.lr_flag == 2)
  2445. break;
  2446. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2447. if (i == ARRAYLENGTH(sd->add_mdmg))
  2448. {
  2449. ShowWarning("pc_bonus2: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2450. break;
  2451. }
  2452. sd->add_mdmg[i].class_ = type2;
  2453. sd->add_mdmg[i].rate += val;
  2454. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2455. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2456. break;
  2457. case SP_ADD_DEF_CLASS:
  2458. if(sd->state.lr_flag == 2)
  2459. break;
  2460. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2461. if (i == ARRAYLENGTH(sd->add_def))
  2462. {
  2463. ShowWarning("pc_bonus2: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2464. break;
  2465. }
  2466. sd->add_def[i].class_ = type2;
  2467. sd->add_def[i].rate += val;
  2468. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2469. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2470. break;
  2471. case SP_ADD_MDEF_CLASS:
  2472. if(sd->state.lr_flag == 2)
  2473. break;
  2474. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2475. if (i == ARRAYLENGTH(sd->add_mdef))
  2476. {
  2477. ShowWarning("pc_bonus2: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2478. break;
  2479. }
  2480. sd->add_mdef[i].class_ = type2;
  2481. sd->add_mdef[i].rate += val;
  2482. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2483. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2484. break;
  2485. case SP_HP_DRAIN_RATE:
  2486. if(!sd->state.lr_flag) {
  2487. sd->right_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2488. sd->right_weapon.hp_drain[RC_NONBOSS].per += val;
  2489. sd->right_weapon.hp_drain[RC_BOSS].rate += type2;
  2490. sd->right_weapon.hp_drain[RC_BOSS].per += val;
  2491. }
  2492. else if(sd->state.lr_flag == 1) {
  2493. sd->left_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2494. sd->left_weapon.hp_drain[RC_NONBOSS].per += val;
  2495. sd->left_weapon.hp_drain[RC_BOSS].rate += type2;
  2496. sd->left_weapon.hp_drain[RC_BOSS].per += val;
  2497. }
  2498. break;
  2499. case SP_HP_DRAIN_VALUE:
  2500. if(!sd->state.lr_flag) {
  2501. sd->right_weapon.hp_drain[RC_NONBOSS].value += type2;
  2502. sd->right_weapon.hp_drain[RC_NONBOSS].type = val;
  2503. sd->right_weapon.hp_drain[RC_BOSS].value += type2;
  2504. sd->right_weapon.hp_drain[RC_BOSS].type = val;
  2505. }
  2506. else if(sd->state.lr_flag == 1) {
  2507. sd->left_weapon.hp_drain[RC_NONBOSS].value += type2;
  2508. sd->left_weapon.hp_drain[RC_NONBOSS].type = val;
  2509. sd->left_weapon.hp_drain[RC_BOSS].value += type2;
  2510. sd->left_weapon.hp_drain[RC_BOSS].type = val;
  2511. }
  2512. break;
  2513. case SP_SP_DRAIN_RATE:
  2514. if(!sd->state.lr_flag) {
  2515. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2516. sd->right_weapon.sp_drain[RC_NONBOSS].per += val;
  2517. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2518. sd->right_weapon.sp_drain[RC_BOSS].per += val;
  2519. }
  2520. else if(sd->state.lr_flag == 1) {
  2521. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2522. sd->left_weapon.sp_drain[RC_NONBOSS].per += val;
  2523. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2524. sd->left_weapon.sp_drain[RC_BOSS].per += val;
  2525. }
  2526. break;
  2527. case SP_SP_DRAIN_VALUE:
  2528. if(!sd->state.lr_flag) {
  2529. sd->right_weapon.sp_drain[RC_NONBOSS].value += type2;
  2530. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2531. sd->right_weapon.sp_drain[RC_BOSS].value += type2;
  2532. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2533. }
  2534. else if(sd->state.lr_flag == 1) {
  2535. sd->left_weapon.sp_drain[RC_NONBOSS].value += type2;
  2536. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2537. sd->left_weapon.sp_drain[RC_BOSS].value += type2;
  2538. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2539. }
  2540. break;
  2541. case SP_SP_VANISH_RATE:
  2542. if(sd->state.lr_flag != 2) {
  2543. sd->bonus.sp_vanish_rate += type2;
  2544. sd->bonus.sp_vanish_per += val;
  2545. }
  2546. break;
  2547. case SP_GET_ZENY_NUM:
  2548. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2549. sd->bonus.get_zeny_rate = val;
  2550. sd->bonus.get_zeny_num = type2;
  2551. }
  2552. break;
  2553. case SP_ADD_GET_ZENY_NUM:
  2554. if(sd->state.lr_flag != 2) {
  2555. sd->bonus.get_zeny_rate += val;
  2556. sd->bonus.get_zeny_num += type2;
  2557. }
  2558. break;
  2559. case SP_WEAPON_COMA_ELE:
  2560. if(type2 >= ELE_MAX) {
  2561. ShowError("pc_bonus2: SP_WEAPON_COMA_ELE: Invalid element %d\n", type2);
  2562. break;
  2563. }
  2564. if(sd->state.lr_flag == 2)
  2565. break;
  2566. sd->weapon_coma_ele[type2] += val;
  2567. sd->special_state.bonus_coma = 1;
  2568. break;
  2569. case SP_WEAPON_COMA_RACE:
  2570. if(sd->state.lr_flag == 2)
  2571. break;
  2572. sd->weapon_coma_race[type2] += val;
  2573. sd->special_state.bonus_coma = 1;
  2574. break;
  2575. case SP_WEAPON_ATK:
  2576. if(sd->state.lr_flag != 2)
  2577. sd->weapon_atk[type2]+=val;
  2578. break;
  2579. case SP_WEAPON_ATK_RATE:
  2580. if(sd->state.lr_flag != 2)
  2581. sd->weapon_atk_rate[type2]+=val;
  2582. break;
  2583. case SP_CRITICAL_ADDRACE:
  2584. if(sd->state.lr_flag != 2)
  2585. sd->critaddrace[type2] += val*10;
  2586. break;
  2587. case SP_ADDEFF_WHENHIT:
  2588. if (type2 > SC_MAX) {
  2589. ShowWarning("pc_bonus2 (Add Effect when hit): %d is not supported.\n", type2);
  2590. break;
  2591. }
  2592. if(sd->state.lr_flag != 2)
  2593. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0);
  2594. break;
  2595. case SP_SKILL_ATK:
  2596. if(sd->state.lr_flag == 2)
  2597. break;
  2598. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2599. if (i == ARRAYLENGTH(sd->skillatk))
  2600. { //Better mention this so the array length can be updated. [Skotlex]
  2601. ShowDebug("run_script: bonus2 bSkillAtk reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  2602. break;
  2603. }
  2604. if (sd->skillatk[i].id == type2)
  2605. sd->skillatk[i].val += val;
  2606. else {
  2607. sd->skillatk[i].id = type2;
  2608. sd->skillatk[i].val = val;
  2609. }
  2610. break;
  2611. case SP_SKILL_HEAL:
  2612. if(sd->state.lr_flag == 2)
  2613. break;
  2614. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2615. if (i == ARRAYLENGTH(sd->skillheal))
  2616. { // Better mention this so the array length can be updated. [Skotlex]
  2617. ShowDebug("run_script: bonus2 bSkillHeal reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  2618. break;
  2619. }
  2620. if (sd->skillheal[i].id == type2)
  2621. sd->skillheal[i].val += val;
  2622. else {
  2623. sd->skillheal[i].id = type2;
  2624. sd->skillheal[i].val = val;
  2625. }
  2626. break;
  2627. case SP_SKILL_HEAL2:
  2628. if(sd->state.lr_flag == 2)
  2629. break;
  2630. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  2631. if (i == ARRAYLENGTH(sd->skillheal2))
  2632. { // Better mention this so the array length can be updated. [Skotlex]
  2633. ShowDebug("run_script: bonus2 bSkillHeal2 reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  2634. break;
  2635. }
  2636. if (sd->skillheal2[i].id == type2)
  2637. sd->skillheal2[i].val += val;
  2638. else {
  2639. sd->skillheal2[i].id = type2;
  2640. sd->skillheal2[i].val = val;
  2641. }
  2642. break;
  2643. case SP_ADD_SKILL_BLOW:
  2644. if(sd->state.lr_flag == 2)
  2645. break;
  2646. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  2647. if (i == ARRAYLENGTH(sd->skillblown))
  2648. { //Better mention this so the array length can be updated. [Skotlex]
  2649. ShowDebug("run_script: bonus2 bSkillBlown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  2650. break;
  2651. }
  2652. if(sd->skillblown[i].id == type2)
  2653. sd->skillblown[i].val += val;
  2654. else {
  2655. sd->skillblown[i].id = type2;
  2656. sd->skillblown[i].val = val;
  2657. }
  2658. break;
  2659. #ifndef RENEWAL_CAST
  2660. case SP_VARCASTRATE:
  2661. #endif
  2662. case SP_CASTRATE:
  2663. if(sd->state.lr_flag == 2)
  2664. break;
  2665. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2666. if (i == ARRAYLENGTH(sd->skillcast))
  2667. { //Better mention this so the array length can be updated. [Skotlex]
  2668. ShowDebug("run_script: bonus2 bCastRate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillcast), type2, val);
  2669. break;
  2670. }
  2671. if(sd->skillcast[i].id == type2)
  2672. sd->skillcast[i].val += val;
  2673. else {
  2674. sd->skillcast[i].id = type2;
  2675. sd->skillcast[i].val = val;
  2676. }
  2677. break;
  2678. case SP_HP_LOSS_RATE:
  2679. if(sd->state.lr_flag != 2) {
  2680. sd->hp_loss.value = type2;
  2681. sd->hp_loss.rate = val;
  2682. }
  2683. break;
  2684. case SP_HP_REGEN_RATE:
  2685. if(sd->state.lr_flag != 2) {
  2686. sd->hp_regen.value = type2;
  2687. sd->hp_regen.rate = val;
  2688. }
  2689. break;
  2690. case SP_ADDRACE2:
  2691. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2692. break;
  2693. if(sd->state.lr_flag != 2)
  2694. sd->right_weapon.addrace2[type2] += val;
  2695. else
  2696. sd->left_weapon.addrace2[type2] += val;
  2697. break;
  2698. case SP_SUBSIZE:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->subsize[type2]+=val;
  2701. break;
  2702. case SP_SUBRACE2:
  2703. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2704. break;
  2705. if(sd->state.lr_flag != 2)
  2706. sd->subrace2[type2]+=val;
  2707. break;
  2708. case SP_ADD_ITEM_HEAL_RATE:
  2709. if(sd->state.lr_flag == 2)
  2710. break;
  2711. if (type2 < MAX_ITEMGROUP) { //Group bonus
  2712. sd->itemgrouphealrate[type2] += val;
  2713. break;
  2714. }
  2715. //Standard item bonus.
  2716. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  2717. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  2718. ShowWarning("pc_bonus2: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  2719. break;
  2720. }
  2721. sd->itemhealrate[i].nameid = type2;
  2722. sd->itemhealrate[i].rate += val;
  2723. break;
  2724. case SP_EXP_ADDRACE:
  2725. if(sd->state.lr_flag != 2)
  2726. sd->expaddrace[type2]+=val;
  2727. break;
  2728. case SP_SP_GAIN_RACE:
  2729. if(sd->state.lr_flag != 2)
  2730. sd->sp_gain_race[type2]+=val;
  2731. break;
  2732. case SP_ADD_MONSTER_DROP_ITEM:
  2733. if (sd->state.lr_flag != 2)
  2734. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2735. break;
  2736. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2737. if (sd->state.lr_flag != 2)
  2738. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2739. break;
  2740. case SP_SP_LOSS_RATE:
  2741. if(sd->state.lr_flag != 2) {
  2742. sd->sp_loss.value = type2;
  2743. sd->sp_loss.rate = val;
  2744. }
  2745. break;
  2746. case SP_SP_REGEN_RATE:
  2747. if(sd->state.lr_flag != 2) {
  2748. sd->sp_regen.value = type2;
  2749. sd->sp_regen.rate = val;
  2750. }
  2751. break;
  2752. case SP_HP_DRAIN_VALUE_RACE:
  2753. if(!sd->state.lr_flag) {
  2754. sd->right_weapon.hp_drain[type2].value += val;
  2755. }
  2756. else if(sd->state.lr_flag == 1) {
  2757. sd->left_weapon.hp_drain[type2].value += val;
  2758. }
  2759. break;
  2760. case SP_SP_DRAIN_VALUE_RACE:
  2761. if(!sd->state.lr_flag) {
  2762. sd->right_weapon.sp_drain[type2].value += val;
  2763. }
  2764. else if(sd->state.lr_flag == 1) {
  2765. sd->left_weapon.sp_drain[type2].value += val;
  2766. }
  2767. break;
  2768. case SP_IGNORE_MDEF_RATE:
  2769. if(sd->state.lr_flag != 2)
  2770. sd->ignore_mdef[type2] += val;
  2771. break;
  2772. case SP_IGNORE_DEF_RATE:
  2773. if(sd->state.lr_flag != 2)
  2774. sd->ignore_def[type2] += val;
  2775. break;
  2776. case SP_SP_GAIN_RACE_ATTACK:
  2777. if(sd->state.lr_flag != 2)
  2778. sd->sp_gain_race_attack[type2] = cap_value(sd->sp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2779. break;
  2780. case SP_HP_GAIN_RACE_ATTACK:
  2781. if(sd->state.lr_flag != 2)
  2782. sd->hp_gain_race_attack[type2] = cap_value(sd->hp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2783. break;
  2784. case SP_SKILL_USE_SP_RATE: //bonus2 bSkillUseSPrate,n,x;
  2785. if(sd->state.lr_flag == 2)
  2786. break;
  2787. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  2788. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  2789. ShowDebug("run_script: bonus2 bSkillUseSPrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  2790. break;
  2791. }
  2792. if (sd->skillusesprate[i].id == type2)
  2793. sd->skillusesprate[i].val += val;
  2794. else {
  2795. sd->skillusesprate[i].id = type2;
  2796. sd->skillusesprate[i].val = val;
  2797. }
  2798. break;
  2799. case SP_SKILL_COOLDOWN:
  2800. if(sd->state.lr_flag == 2)
  2801. break;
  2802. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  2803. if (i == ARRAYLENGTH(sd->skillcooldown))
  2804. {
  2805. ShowDebug("run_script: bonus2 bSkillCoolDown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  2806. break;
  2807. }
  2808. if (sd->skillcooldown[i].id == type2)
  2809. sd->skillcooldown[i].val += val;
  2810. else {
  2811. sd->skillcooldown[i].id = type2;
  2812. sd->skillcooldown[i].val = val;
  2813. }
  2814. break;
  2815. case SP_SKILL_FIXEDCAST:
  2816. if(sd->state.lr_flag == 2)
  2817. break;
  2818. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  2819. if (i == ARRAYLENGTH(sd->skillfixcast))
  2820. {
  2821. ShowDebug("run_script: bonus2 bSkillFixedCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  2822. break;
  2823. }
  2824. if (sd->skillfixcast[i].id == type2)
  2825. sd->skillfixcast[i].val += val;
  2826. else {
  2827. sd->skillfixcast[i].id = type2;
  2828. sd->skillfixcast[i].val = val;
  2829. }
  2830. break;
  2831. case SP_SKILL_VARIABLECAST:
  2832. if(sd->state.lr_flag == 2)
  2833. break;
  2834. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  2835. if (i == ARRAYLENGTH(sd->skillvarcast))
  2836. {
  2837. ShowDebug("run_script: bonus2 bSkillVariableCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  2838. break;
  2839. }
  2840. if (sd->skillvarcast[i].id == type2)
  2841. sd->skillvarcast[i].val += val;
  2842. else {
  2843. sd->skillvarcast[i].id = type2;
  2844. sd->skillvarcast[i].val = val;
  2845. }
  2846. break;
  2847. #ifdef RENEWAL_CAST
  2848. case SP_VARCASTRATE:
  2849. if(sd->state.lr_flag == 2)
  2850. break;
  2851. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2852. if (i == ARRAYLENGTH(sd->skillcast))
  2853. {
  2854. ShowDebug("run_script: bonus2 bVariableCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",ARRAYLENGTH(sd->skillcast), type2, val);
  2855. break;
  2856. }
  2857. if(sd->skillcast[i].id == type2)
  2858. sd->skillcast[i].val -= val;
  2859. else {
  2860. sd->skillcast[i].id = type2;
  2861. sd->skillcast[i].val -= val;
  2862. }
  2863. break;
  2864. #endif
  2865. case SP_SKILL_USE_SP: //bonus2 bSkillUseSP,n,x;
  2866. if(sd->state.lr_flag == 2)
  2867. break;
  2868. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  2869. if (i == ARRAYLENGTH(sd->skillusesp)) {
  2870. ShowDebug("run_script: bonus2 bSkillUseSP reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  2871. break;
  2872. }
  2873. if (sd->skillusesp[i].id == type2)
  2874. sd->skillusesp[i].val += val;
  2875. else {
  2876. sd->skillusesp[i].id = type2;
  2877. sd->skillusesp[i].val = val;
  2878. }
  2879. break;
  2880. default:
  2881. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  2882. break;
  2883. }
  2884. return 0;
  2885. }
  2886. int pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  2887. {
  2888. nullpo_ret(sd);
  2889. switch(type){
  2890. case SP_ADD_MONSTER_DROP_ITEM:
  2891. if(sd->state.lr_flag != 2)
  2892. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, 1<<type3, val);
  2893. break;
  2894. case SP_ADD_CLASS_DROP_ITEM:
  2895. if(sd->state.lr_flag != 2)
  2896. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, -type3, val);
  2897. break;
  2898. case SP_AUTOSPELL:
  2899. if(sd->state.lr_flag != 2)
  2900. {
  2901. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2902. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2903. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  2904. target?-type2:type2, type3, val, 0, current_equip_card_id);
  2905. }
  2906. break;
  2907. case SP_AUTOSPELL_WHENHIT:
  2908. if(sd->state.lr_flag != 2)
  2909. {
  2910. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2911. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2912. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  2913. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  2914. }
  2915. break;
  2916. case SP_SP_DRAIN_RATE:
  2917. if(!sd->state.lr_flag) {
  2918. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2919. sd->right_weapon.sp_drain[RC_NONBOSS].per += type3;
  2920. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2921. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2922. sd->right_weapon.sp_drain[RC_BOSS].per += type3;
  2923. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2924. }
  2925. else if(sd->state.lr_flag == 1) {
  2926. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2927. sd->left_weapon.sp_drain[RC_NONBOSS].per += type3;
  2928. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2929. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2930. sd->left_weapon.sp_drain[RC_BOSS].per += type3;
  2931. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2932. }
  2933. break;
  2934. case SP_HP_DRAIN_RATE_RACE:
  2935. if(!sd->state.lr_flag) {
  2936. sd->right_weapon.hp_drain[type2].rate += type3;
  2937. sd->right_weapon.hp_drain[type2].per += val;
  2938. }
  2939. else if(sd->state.lr_flag == 1) {
  2940. sd->left_weapon.hp_drain[type2].rate += type3;
  2941. sd->left_weapon.hp_drain[type2].per += val;
  2942. }
  2943. break;
  2944. case SP_SP_DRAIN_RATE_RACE:
  2945. if(!sd->state.lr_flag) {
  2946. sd->right_weapon.sp_drain[type2].rate += type3;
  2947. sd->right_weapon.sp_drain[type2].per += val;
  2948. }
  2949. else if(sd->state.lr_flag == 1) {
  2950. sd->left_weapon.sp_drain[type2].rate += type3;
  2951. sd->left_weapon.sp_drain[type2].per += val;
  2952. }
  2953. break;
  2954. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2955. if (sd->state.lr_flag != 2)
  2956. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, 1<<type3, val);
  2957. break;
  2958. case SP_ADDEFF:
  2959. if (type2 > SC_MAX) {
  2960. ShowWarning("pc_bonus3 (Add Effect): %d is not supported.\n", type2);
  2961. break;
  2962. }
  2963. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2964. sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val);
  2965. break;
  2966. case SP_ADDEFF_WHENHIT:
  2967. if (type2 > SC_MAX) {
  2968. ShowWarning("pc_bonus3 (Add Effect when hit): %d is not supported.\n", type2);
  2969. break;
  2970. }
  2971. if(sd->state.lr_flag != 2)
  2972. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val);
  2973. break;
  2974. case SP_ADDEFF_ONSKILL:
  2975. if( type3 > SC_MAX ) {
  2976. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type3);
  2977. break;
  2978. }
  2979. if( sd->state.lr_flag != 2 )
  2980. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
  2981. break;
  2982. case SP_ADDELE:
  2983. if (type2 > ELE_MAX) {
  2984. ShowWarning("pc_bonus3 (SP_ADDELE): element %d is out of range.\n", type2);
  2985. break;
  2986. }
  2987. if (sd->state.lr_flag != 2)
  2988. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  2989. break;
  2990. case SP_SUBELE:
  2991. if (type2 > ELE_MAX) {
  2992. ShowWarning("pc_bonus3 (SP_SUBELE): element %d is out of range.\n", type2);
  2993. break;
  2994. }
  2995. if (sd->state.lr_flag != 2)
  2996. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  2997. break;
  2998. default:
  2999. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3000. break;
  3001. }
  3002. return 0;
  3003. }
  3004. int pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3005. {
  3006. nullpo_ret(sd);
  3007. switch(type){
  3008. case SP_AUTOSPELL:
  3009. if(sd->state.lr_flag != 2)
  3010. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3011. break;
  3012. case SP_AUTOSPELL_WHENHIT:
  3013. if(sd->state.lr_flag != 2)
  3014. 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);
  3015. break;
  3016. case SP_AUTOSPELL_ONSKILL:
  3017. if(sd->state.lr_flag != 2)
  3018. {
  3019. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3020. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3021. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3022. }
  3023. break;
  3024. case SP_ADDEFF_ONSKILL:
  3025. if( type2 > SC_MAX ) {
  3026. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type2);
  3027. break;
  3028. }
  3029. if( sd->state.lr_flag != 2 )
  3030. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
  3031. break;
  3032. default:
  3033. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3034. break;
  3035. }
  3036. return 0;
  3037. }
  3038. int pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3039. {
  3040. nullpo_ret(sd);
  3041. switch(type){
  3042. case SP_AUTOSPELL:
  3043. if(sd->state.lr_flag != 2)
  3044. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3045. break;
  3046. case SP_AUTOSPELL_WHENHIT:
  3047. if(sd->state.lr_flag != 2)
  3048. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3049. break;
  3050. case SP_AUTOSPELL_ONSKILL:
  3051. if(sd->state.lr_flag != 2)
  3052. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3053. break;
  3054. default:
  3055. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3056. break;
  3057. }
  3058. return 0;
  3059. }
  3060. /*==========================================
  3061. * Grants a player a given skill. Flag values are:
  3062. * 0 - Grant skill unconditionally and forever (only this one invokes status_calc_pc,
  3063. * as the other two are assumed to be invoked from within it)
  3064. * 1 - Grant an item skill (temporary)
  3065. * 2 - Like 1, except the level granted can stack with previously learned level.
  3066. *------------------------------------------*/
  3067. int pc_skill(TBL_PC* sd, int id, int level, int flag)
  3068. {
  3069. nullpo_ret(sd);
  3070. if( id <= 0 || id >= MAX_SKILL || skill_db[id].name == NULL) {
  3071. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", id);
  3072. return 0;
  3073. }
  3074. if( level > MAX_SKILL_LEVEL ) {
  3075. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3076. return 0;
  3077. }
  3078. if( flag == 2 && sd->status.skill[id].lv + level > MAX_SKILL_LEVEL ) {
  3079. 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);
  3080. return 0;
  3081. }
  3082. switch( flag ){
  3083. case 0: //Set skill data overwriting whatever was there before.
  3084. sd->status.skill[id].id = id;
  3085. sd->status.skill[id].lv = level;
  3086. sd->status.skill[id].flag = SKILL_FLAG_PERMANENT;
  3087. if( level == 0 ) //Remove skill.
  3088. {
  3089. sd->status.skill[id].id = 0;
  3090. clif_deleteskill(sd,id);
  3091. }
  3092. else
  3093. clif_addskill(sd,id);
  3094. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3095. status_calc_pc(sd, 0);
  3096. break;
  3097. case 1: //Item bonus skill.
  3098. if( sd->status.skill[id].id == id ){
  3099. if( sd->status.skill[id].lv >= level )
  3100. return 0;
  3101. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT ) //Non-granted skill, store it's level.
  3102. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv;
  3103. } else {
  3104. sd->status.skill[id].id = id;
  3105. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY;
  3106. }
  3107. sd->status.skill[id].lv = level;
  3108. break;
  3109. case 2: //Add skill bonus on top of what you had.
  3110. if( sd->status.skill[id].id == id ){
  3111. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT )
  3112. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Store previous level.
  3113. } else {
  3114. sd->status.skill[id].id = id;
  3115. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3116. }
  3117. sd->status.skill[id].lv += level;
  3118. break;
  3119. default: //Unknown flag?
  3120. return 0;
  3121. }
  3122. return 1;
  3123. }
  3124. /*==========================================
  3125. * Append a card to an item ?
  3126. *------------------------------------------*/
  3127. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3128. {
  3129. int i;
  3130. int nameid;
  3131. nullpo_ret(sd);
  3132. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3133. return 0; //Invalid item index.
  3134. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3135. return 0; //Invalid card index.
  3136. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3137. return 0; // target item missing
  3138. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3139. return 0; // target card missing
  3140. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3141. return 0; // only weapons and armor are allowed
  3142. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3143. return 0; // must be a card
  3144. if( sd->status.inventory[idx_equip].identify == 0 )
  3145. return 0; // target must be identified
  3146. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3147. return 0; // card slots reserved for other purposes
  3148. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3149. return 0; // card cannot be compounded on this item type
  3150. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3151. return 0; // attempted to place shield card on left-hand weapon.
  3152. if( sd->status.inventory[idx_equip].equip != 0 )
  3153. return 0; // item must be unequipped
  3154. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3155. if( i == sd->inventory_data[idx_equip]->slot )
  3156. return 0; // no free slots
  3157. // remember the card id to insert
  3158. nameid = sd->status.inventory[idx_card].nameid;
  3159. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3160. {// failed
  3161. clif_insert_card(sd,idx_equip,idx_card,1);
  3162. }
  3163. else
  3164. {// success
  3165. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3166. sd->status.inventory[idx_equip].card[i] = nameid;
  3167. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3168. clif_insert_card(sd,idx_equip,idx_card,0);
  3169. }
  3170. return 0;
  3171. }
  3172. //
  3173. // Items
  3174. //
  3175. /*==========================================
  3176. * Update buying value by skills
  3177. *------------------------------------------*/
  3178. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3179. {
  3180. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3181. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3182. rate1 = 5+skill*2-((skill==10)? 1:0);
  3183. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3184. rate2 = 5+skill*4;
  3185. if(rate1 < rate2) rate1 = rate2;
  3186. if(rate1)
  3187. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3188. if(val < 0) val = 0;
  3189. if(orig_value > 0 && val < 1) val = 1;
  3190. return val;
  3191. }
  3192. /*==========================================
  3193. * Update selling value by skills
  3194. *------------------------------------------*/
  3195. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3196. {
  3197. int skill,val = orig_value,rate = 0;
  3198. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3199. rate = 5+skill*2-((skill==10)? 1:0);
  3200. if(rate)
  3201. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3202. if(val < 0) val = 0;
  3203. if(orig_value > 0 && val < 1) val = 1;
  3204. return val;
  3205. }
  3206. /*==========================================
  3207. * Checking if we have enough place on inventory for new item
  3208. * Make sure to take 30k as limit (for client I guess)
  3209. *------------------------------------------*/
  3210. int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
  3211. {
  3212. int i;
  3213. struct item_data* data;
  3214. nullpo_ret(sd);
  3215. if(amount > MAX_AMOUNT)
  3216. return ADDITEM_OVERAMOUNT;
  3217. data = itemdb_search(nameid);
  3218. if(!itemdb_isstackable2(data))
  3219. return ADDITEM_NEW;
  3220. if( data->stack.inventory && amount > data->stack.amount )
  3221. return ADDITEM_OVERAMOUNT;
  3222. for(i=0;i<MAX_INVENTORY;i++){
  3223. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3224. if(sd->status.inventory[i].nameid==nameid){
  3225. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3226. return ADDITEM_OVERAMOUNT;
  3227. return ADDITEM_EXIST;
  3228. }
  3229. }
  3230. return ADDITEM_NEW;
  3231. }
  3232. /*==========================================
  3233. * Return number of available place in inventory
  3234. * Each non stackable item will reduce place by 1
  3235. *------------------------------------------*/
  3236. int pc_inventoryblank(struct map_session_data *sd)
  3237. {
  3238. int i,b;
  3239. nullpo_ret(sd);
  3240. for(i=0,b=0;i<MAX_INVENTORY;i++){
  3241. if(sd->status.inventory[i].nameid==0)
  3242. b++;
  3243. }
  3244. return b;
  3245. }
  3246. /*==========================================
  3247. * attempts to remove zeny from player (sd)
  3248. *------------------------------------------*/
  3249. int pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3250. {
  3251. nullpo_retr(-1,sd);
  3252. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3253. if( zeny < 0 )
  3254. {
  3255. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3256. return 1;
  3257. }
  3258. if( sd->status.zeny < zeny )
  3259. return 1; //Not enough.
  3260. sd->status.zeny -= zeny;
  3261. clif_updatestatus(sd,SP_ZENY);
  3262. if(!tsd) tsd = sd;
  3263. log_zeny(sd, type, tsd, -zeny);
  3264. if( zeny > 0 && sd->state.showzeny ) {
  3265. char output[255];
  3266. sprintf(output, "Removed %dz.", zeny);
  3267. clif_disp_onlyself(sd,output,strlen(output));
  3268. }
  3269. return 0;
  3270. }
  3271. /*==========================================
  3272. * Cash Shop
  3273. *------------------------------------------*/
  3274. int pc_paycash(struct map_session_data *sd, int price, int points)
  3275. {
  3276. char output[128];
  3277. int cash;
  3278. nullpo_retr(-1,sd);
  3279. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3280. if( price < 0 || points < 0 )
  3281. {
  3282. 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);
  3283. return -2;
  3284. }
  3285. if( points > price )
  3286. {
  3287. 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);
  3288. points = price;
  3289. }
  3290. cash = price-points;
  3291. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3292. {
  3293. 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);
  3294. return -1;
  3295. }
  3296. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints-cash);
  3297. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints-points);
  3298. if( battle_config.cashshop_show_points )
  3299. {
  3300. sprintf(output, msg_txt(504), points, cash, sd->kafraPoints, sd->cashPoints);
  3301. clif_disp_onlyself(sd, output, strlen(output));
  3302. }
  3303. return cash+points;
  3304. }
  3305. int pc_getcash(struct map_session_data *sd, int cash, int points)
  3306. {
  3307. char output[128];
  3308. nullpo_retr(-1,sd);
  3309. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3310. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3311. if( cash > 0 )
  3312. {
  3313. if( cash > MAX_ZENY-sd->cashPoints )
  3314. {
  3315. 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);
  3316. cash = MAX_ZENY-sd->cashPoints;
  3317. }
  3318. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints+cash);
  3319. if( battle_config.cashshop_show_points )
  3320. {
  3321. sprintf(output, msg_txt(505), cash, sd->cashPoints);
  3322. clif_disp_onlyself(sd, output, strlen(output));
  3323. }
  3324. return cash;
  3325. }
  3326. else if( cash < 0 )
  3327. {
  3328. 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);
  3329. return -1;
  3330. }
  3331. if( points > 0 )
  3332. {
  3333. if( points > MAX_ZENY-sd->kafraPoints )
  3334. {
  3335. 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);
  3336. points = MAX_ZENY-sd->kafraPoints;
  3337. }
  3338. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints+points);
  3339. if( battle_config.cashshop_show_points )
  3340. {
  3341. sprintf(output, msg_txt(506), points, sd->kafraPoints);
  3342. clif_disp_onlyself(sd, output, strlen(output));
  3343. }
  3344. return points;
  3345. }
  3346. else if( points < 0 )
  3347. {
  3348. 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);
  3349. return -1;
  3350. }
  3351. return -2; //shouldn't happen but jsut in case
  3352. }
  3353. /*==========================================
  3354. * Attempts to give zeny to player (sd)
  3355. * tsd (optional) from who for log (if null take sd)
  3356. *------------------------------------------*/
  3357. int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3358. {
  3359. nullpo_retr(-1,sd);
  3360. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3361. if( zeny < 0 )
  3362. {
  3363. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3364. return 1;
  3365. }
  3366. if( zeny > MAX_ZENY - sd->status.zeny )
  3367. zeny = MAX_ZENY - sd->status.zeny;
  3368. sd->status.zeny += zeny;
  3369. clif_updatestatus(sd,SP_ZENY);
  3370. if(!tsd) tsd = sd;
  3371. log_zeny(sd, type, tsd, zeny);
  3372. if( zeny > 0 && sd->state.showzeny ) {
  3373. char output[255];
  3374. sprintf(output, "Gained %dz.", zeny);
  3375. clif_disp_onlyself(sd,output,strlen(output));
  3376. }
  3377. return 0;
  3378. }
  3379. /*==========================================
  3380. * Searching a specified itemid in inventory and return his stored index
  3381. *------------------------------------------*/
  3382. int pc_search_inventory(struct map_session_data *sd,int item_id)
  3383. {
  3384. int i;
  3385. nullpo_retr(-1, sd);
  3386. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
  3387. return ( i < MAX_INVENTORY ) ? i : -1;
  3388. }
  3389. /*==========================================
  3390. * Attempt to add a new item to inventory.
  3391. * Return:
  3392. 0 = success
  3393. 1 = invalid itemid not found or negative amount
  3394. 2 = overweight
  3395. 3 = ?
  3396. 4 = no free place found
  3397. 5 = max amount reached
  3398. 6 = ?
  3399. 7 = stack limitation
  3400. *------------------------------------------*/
  3401. int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3402. {
  3403. struct item_data *data;
  3404. int i;
  3405. unsigned int w;
  3406. nullpo_retr(1, sd);
  3407. nullpo_retr(1, item_data);
  3408. if( item_data->nameid <= 0 || amount <= 0 )
  3409. return 1;
  3410. if( amount > MAX_AMOUNT )
  3411. return 5;
  3412. data = itemdb_search(item_data->nameid);
  3413. if( data->stack.inventory && amount > data->stack.amount )
  3414. {// item stack limitation
  3415. return 7;
  3416. }
  3417. w = data->weight*amount;
  3418. if(sd->weight + w > sd->max_weight)
  3419. return 2;
  3420. i = MAX_INVENTORY;
  3421. if( itemdb_isstackable2(data) && item_data->expire_time == 0 )
  3422. { // Stackable | Non Rental
  3423. for( i = 0; i < MAX_INVENTORY; i++ )
  3424. {
  3425. if( sd->status.inventory[i].nameid == item_data->nameid && memcmp(&sd->status.inventory[i].card, &item_data->card, sizeof(item_data->card)) == 0 )
  3426. {
  3427. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3428. return 5;
  3429. sd->status.inventory[i].amount += amount;
  3430. clif_additem(sd,i,amount,0);
  3431. break;
  3432. }
  3433. }
  3434. }
  3435. if( i >= MAX_INVENTORY )
  3436. {
  3437. i = pc_search_inventory(sd,0);
  3438. if( i < 0 )
  3439. return 4;
  3440. memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
  3441. // clear equips field first, just in case
  3442. if( item_data->equip )
  3443. sd->status.inventory[i].equip = 0;
  3444. sd->status.inventory[i].amount = amount;
  3445. sd->inventory_data[i] = data;
  3446. clif_additem(sd,i,amount,0);
  3447. }
  3448. #ifdef NSI_UNIQUE_ID
  3449. if( !itemdb_isstackable2(data) && !item_data->unique_id )
  3450. sd->status.inventory[i].unique_id = itemdb_unique_id(0,0);
  3451. #endif
  3452. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  3453. sd->weight += w;
  3454. clif_updatestatus(sd,SP_WEIGHT);
  3455. //Auto-equip
  3456. if(data->flag.autoequip)
  3457. pc_equipitem(sd, i, data->equip);
  3458. /* rental item check */
  3459. if( item_data->expire_time ) {
  3460. if( time(NULL) > item_data->expire_time ) {
  3461. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  3462. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  3463. } else {
  3464. int seconds = (int)( item_data->expire_time - time(NULL) );
  3465. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  3466. pc_inventory_rental_add(sd, seconds);
  3467. }
  3468. }
  3469. return 0;
  3470. }
  3471. /*==========================================
  3472. * Remove an item at index n from inventory by amount.
  3473. * Parameters :
  3474. * @type
  3475. * 1 : don't notify deletion
  3476. * 2 : don't notify weight change
  3477. * Return:
  3478. * 0 = success
  3479. * 1 = invalid itemid or negative amount
  3480. *------------------------------------------*/
  3481. int pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  3482. {
  3483. nullpo_retr(1, sd);
  3484. if(sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  3485. return 1;
  3486. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  3487. sd->status.inventory[n].amount -= amount;
  3488. sd->weight -= sd->inventory_data[n]->weight*amount ;
  3489. if( sd->status.inventory[n].amount <= 0 ){
  3490. if(sd->status.inventory[n].equip)
  3491. pc_unequipitem(sd,n,3);
  3492. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  3493. sd->inventory_data[n] = NULL;
  3494. }
  3495. if(!(type&1))
  3496. clif_delitem(sd,n,amount,reason);
  3497. if(!(type&2))
  3498. clif_updatestatus(sd,SP_WEIGHT);
  3499. return 0;
  3500. }
  3501. /*==========================================
  3502. * Attempt to drop an item.
  3503. * Return:
  3504. * 0 = fail
  3505. * 1 = success
  3506. *------------------------------------------*/
  3507. int pc_dropitem(struct map_session_data *sd,int n,int amount)
  3508. {
  3509. nullpo_retr(1, sd);
  3510. if(n < 0 || n >= MAX_INVENTORY)
  3511. return 0;
  3512. if(amount <= 0)
  3513. return 0;
  3514. if(sd->status.inventory[n].nameid <= 0 ||
  3515. sd->status.inventory[n].amount <= 0 ||
  3516. sd->status.inventory[n].amount < amount ||
  3517. sd->state.trading || sd->state.vending ||
  3518. !sd->inventory_data[n] //pc_delitem would fail on this case.
  3519. )
  3520. return 0;
  3521. if( map[sd->bl.m].flag.nodrop )
  3522. {
  3523. clif_displaymessage (sd->fd, msg_txt(271));
  3524. return 0; //Can't drop items in nodrop mapflag maps.
  3525. }
  3526. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  3527. {
  3528. clif_displaymessage (sd->fd, msg_txt(263));
  3529. return 0;
  3530. }
  3531. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
  3532. return 0;
  3533. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  3534. clif_dropitem(sd, n, amount);
  3535. return 1;
  3536. }
  3537. /*==========================================
  3538. * Attempt to pick up an item.
  3539. * Return:
  3540. * 0 = fail
  3541. * 1 = success
  3542. *------------------------------------------*/
  3543. int pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  3544. {
  3545. int flag=0;
  3546. unsigned int tick = gettick();
  3547. struct map_session_data *first_sd = NULL,*second_sd = NULL,*third_sd = NULL;
  3548. struct party_data *p=NULL;
  3549. nullpo_ret(sd);
  3550. nullpo_ret(fitem);
  3551. if(!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  3552. return 0; // Distance is too far
  3553. if (sd->status.party_id)
  3554. p = party_search(sd->status.party_id);
  3555. if(fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id)
  3556. {
  3557. first_sd = map_charid2sd(fitem->first_get_charid);
  3558. if(DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  3559. if (!(p && p->party.item&1 &&
  3560. first_sd && first_sd->status.party_id == sd->status.party_id
  3561. ))
  3562. return 0;
  3563. }
  3564. else
  3565. if(fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id)
  3566. {
  3567. second_sd = map_charid2sd(fitem->second_get_charid);
  3568. if(DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  3569. if(!(p && p->party.item&1 &&
  3570. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3571. (second_sd && second_sd->status.party_id == sd->status.party_id))
  3572. ))
  3573. return 0;
  3574. }
  3575. else
  3576. if(fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id)
  3577. {
  3578. third_sd = map_charid2sd(fitem->third_get_charid);
  3579. if(DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  3580. if(!(p && p->party.item&1 &&
  3581. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3582. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  3583. (third_sd && third_sd->status.party_id == sd->status.party_id))
  3584. ))
  3585. return 0;
  3586. }
  3587. }
  3588. }
  3589. }
  3590. //This function takes care of giving the item to whoever should have it, considering party-share options.
  3591. if ((flag = party_share_loot(p,sd,&fitem->item_data, fitem->first_get_charid))) {
  3592. clif_additem(sd,0,0,flag);
  3593. return 1;
  3594. }
  3595. //Display pickup animation.
  3596. pc_stop_attack(sd);
  3597. clif_takeitem(&sd->bl,&fitem->bl);
  3598. map_clearflooritem(&fitem->bl);
  3599. return 1;
  3600. }
  3601. /*==========================================
  3602. * Check if item is usable.
  3603. * Return:
  3604. * 0 = no
  3605. * 1 = yes
  3606. *------------------------------------------*/
  3607. int pc_isUseitem(struct map_session_data *sd,int n)
  3608. {
  3609. struct item_data *item;
  3610. int nameid;
  3611. nullpo_ret(sd);
  3612. item = sd->inventory_data[n];
  3613. nameid = sd->status.inventory[n].nameid;
  3614. if( item == NULL )
  3615. return 0;
  3616. //Not consumable item
  3617. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  3618. return 0;
  3619. if( !item->script ) //if it has no script, you can't really consume it!
  3620. return 0;
  3621. switch( nameid ) //@TODO, lot oh harcoded nameid here
  3622. {
  3623. case 605: // Anodyne
  3624. if( map_flag_gvg(sd->bl.m) )
  3625. return 0;
  3626. case 606:
  3627. if( pc_issit(sd) )
  3628. return 0;
  3629. break;
  3630. case 601: // Fly Wing
  3631. case 12212: // Giant Fly Wing
  3632. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) )
  3633. {
  3634. clif_skill_teleportmessage(sd,0);
  3635. return 0;
  3636. }
  3637. case 602: // ButterFly Wing
  3638. case 14527: // Dungeon Teleport Scroll
  3639. case 14581: // Dungeon Teleport Scroll
  3640. case 14582: // Yellow Butterfly Wing
  3641. case 14583: // Green Butterfly Wing
  3642. case 14584: // Red Butterfly Wing
  3643. case 14585: // Blue Butterfly Wing
  3644. case 14591: // Siege Teleport Scroll
  3645. if( sd->duel_group && !battle_config.duel_allow_teleport )
  3646. {
  3647. clif_displaymessage(sd->fd, msg_txt(663));
  3648. return 0;
  3649. }
  3650. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  3651. return 0;
  3652. break;
  3653. case 604: // Dead Branch
  3654. case 12024: // Red Pouch
  3655. case 12103: // Bloody Branch
  3656. case 12109: // Poring Box
  3657. if( map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m) )
  3658. return 0;
  3659. break;
  3660. case 12210: // Bubble Gum
  3661. case 12264: // Comp Bubble Gum
  3662. if( sd->sc.data[SC_ITEMBOOST] )
  3663. return 0;
  3664. break;
  3665. case 12208: // Battle Manual
  3666. case 12263: // Comp Battle Manual
  3667. case 12312: // Thick Battle Manual
  3668. case 12705: // Noble Nameplate
  3669. case 14532: // Battle_Manual25
  3670. case 14533: // Battle_Manual100
  3671. case 14545: // Battle_Manual300
  3672. if( sd->sc.data[SC_EXPBOOST] )
  3673. return 0;
  3674. break;
  3675. case 14592: // JOB_Battle_Manual
  3676. if( sd->sc.data[SC_JEXPBOOST] )
  3677. return 0;
  3678. break;
  3679. // Mercenary Items
  3680. case 12184: // Mercenary's Red Potion
  3681. case 12185: // Mercenary's Blue Potion
  3682. case 12241: // Mercenary's Concentration Potion
  3683. case 12242: // Mercenary's Awakening Potion
  3684. case 12243: // Mercenary's Berserk Potion
  3685. if( sd->md == NULL || sd->md->db == NULL )
  3686. return 0;
  3687. if (sd->md->sc.data[SC_BERSERK] || sd->md->sc.data[SC_SATURDAYNIGHTFEVER] || sd->md->sc.data[SC__BLOODYLUST])
  3688. return 0;
  3689. if( nameid == 12242 && sd->md->db->lv < 40 )
  3690. return 0;
  3691. if( nameid == 12243 && sd->md->db->lv < 80 )
  3692. return 0;
  3693. break;
  3694. case 12213: //Neuralizer
  3695. if( !map[sd->bl.m].flag.reset )
  3696. return 0;
  3697. break;
  3698. }
  3699. if( nameid >= 12153 && nameid <= 12182 && sd->md != NULL )
  3700. return 0; // Mercenary Scrolls
  3701. /**
  3702. * Only Rune Knights may use runes
  3703. **/
  3704. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  3705. return 0;
  3706. /**
  3707. * Only GCross may use poisons
  3708. **/
  3709. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  3710. return 0;
  3711. //added item_noequip.txt items check by Maya&[Lupus]
  3712. if (
  3713. (!map_flag_vs(sd->bl.m) && item->flag.no_equip&1) || // Normal
  3714. (map[sd->bl.m].flag.pvp && item->flag.no_equip&2) || // PVP
  3715. (map_flag_gvg(sd->bl.m) && item->flag.no_equip&4) || // GVG
  3716. (map[sd->bl.m].flag.battleground && item->flag.no_equip&8) || // Battleground
  3717. (map[sd->bl.m].flag.restricted && item->flag.no_equip&(8*map[sd->bl.m].zone)) // Zone restriction
  3718. )
  3719. return 0;
  3720. //Gender check
  3721. if(item->sex != 2 && sd->status.sex != item->sex)
  3722. return 0;
  3723. //Required level check
  3724. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  3725. return 0;
  3726. #ifdef RENEWAL
  3727. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  3728. return 0;
  3729. #endif
  3730. //Not equipable by class. [Skotlex]
  3731. if (!(
  3732. (1<<(sd->class_&MAPID_BASEMASK)) &
  3733. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  3734. ))
  3735. return 0;
  3736. //Not usable by upper class. [Inkfish]
  3737. while( 1 ) {
  3738. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  3739. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  3740. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  3741. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  3742. return 0;
  3743. }
  3744. //Dead Branch & Bloody Branch & Porings Box
  3745. // FIXME: outdated, use constants or database
  3746. if( nameid == 604 || nameid == 12103 || nameid == 12109 )
  3747. log_branch(sd);
  3748. return 1;
  3749. }
  3750. /*==========================================
  3751. * Last checks to use an item.
  3752. * Return:
  3753. * 0 = fail
  3754. * 1 = success
  3755. *------------------------------------------*/
  3756. int pc_useitem(struct map_session_data *sd,int n)
  3757. {
  3758. unsigned int tick = gettick();
  3759. int amount, i, nameid;
  3760. struct script_code *script;
  3761. nullpo_ret(sd);
  3762. if( sd->status.inventory[n].nameid <= 0 || sd->status.inventory[n].amount <= 0 )
  3763. return 0;
  3764. if( !pc_isUseitem(sd,n) )
  3765. return 0;
  3766. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  3767. nameid = sd->inventory_data[n]->nameid;
  3768. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  3769. return 0;
  3770. if (sd->sc.count && (
  3771. sd->sc.data[SC_BERSERK] || sd->sc.data[SC__BLOODYLUST] ||
  3772. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  3773. sd->sc.data[SC_TRICKDEAD] ||
  3774. sd->sc.data[SC_HIDING] ||
  3775. sd->sc.data[SC__SHADOWFORM] ||
  3776. sd->sc.data[SC__MANHOLE] ||
  3777. sd->sc.data[SC_KAGEHUMI] ||
  3778. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM)
  3779. ))
  3780. return 0;
  3781. //Prevent mass item usage. [Skotlex]
  3782. if( DIFF_TICK(sd->canuseitem_tick, tick) > 0 ||
  3783. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick, tick) > 0)
  3784. )
  3785. return 0;
  3786. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  3787. if( sd->inventory_data[n]->flag.delay_consume ) {
  3788. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.option&OPTION_MOUNTING )
  3789. return 0;
  3790. else if( pc_issit(sd) )
  3791. return 0;
  3792. }
  3793. //Since most delay-consume items involve using a "skill-type" target cursor,
  3794. //perform a skill-use check before going through. [Skotlex]
  3795. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  3796. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  3797. if( sd->inventory_data[n]->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  3798. return 0;
  3799. if( sd->inventory_data[n]->delay > 0 ) {
  3800. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
  3801. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  3802. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  3803. if( i < MAX_ITEMDELAYS ) {
  3804. if( sd->item_delay[i].nameid ) {// found
  3805. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  3806. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  3807. char e_msg[100];
  3808. if( e_tick > 99 )
  3809. sprintf(e_msg,"Item Failed. [%s] is cooling down. wait %.1f minutes.",
  3810. itemdb_jname(sd->status.inventory[n].nameid),
  3811. (double)e_tick / 60);
  3812. else
  3813. sprintf(e_msg,"Item Failed. [%s] is cooling down. wait %d seconds.",
  3814. itemdb_jname(sd->status.inventory[n].nameid),
  3815. e_tick+1);
  3816. clif_colormes(sd,COLOR_RED,e_msg);
  3817. return 0; // Delay has not expired yet
  3818. }
  3819. } else {// not yet used item (all slots are initially empty)
  3820. sd->item_delay[i].nameid = nameid;
  3821. }
  3822. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  3823. } else {// should not happen
  3824. ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%d, char_id=%d)\n", nameid, sd->status.char_id);
  3825. }
  3826. //clean up used delays so we can give room for more
  3827. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  3828. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  3829. sd->item_delay[i].tick = 0;
  3830. sd->item_delay[i].nameid = 0;
  3831. }
  3832. }
  3833. }
  3834. sd->itemid = sd->status.inventory[n].nameid;
  3835. sd->itemindex = n;
  3836. if(sd->catch_target_class != -1) //Abort pet catching.
  3837. sd->catch_target_class = -1;
  3838. amount = sd->status.inventory[n].amount;
  3839. script = sd->inventory_data[n]->script;
  3840. //Check if the item is to be consumed immediately [Skotlex]
  3841. if( sd->inventory_data[n]->flag.delay_consume )
  3842. clif_useitemack(sd,n,amount,true);
  3843. else {
  3844. if( sd->status.inventory[n].expire_time == 0 ) {
  3845. clif_useitemack(sd,n,amount-1,true);
  3846. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  3847. } else
  3848. clif_useitemack(sd,n,0,false);
  3849. }
  3850. if(sd->status.inventory[n].card[0]==CARD0_CREATE &&
  3851. pc_famerank(MakeDWord(sd->status.inventory[n].card[2],sd->status.inventory[n].card[3]), MAPID_ALCHEMIST))
  3852. {
  3853. potion_flag = 2; // Famous player's potions have 50% more efficiency
  3854. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  3855. potion_flag = 3; //Even more effective potions.
  3856. }
  3857. //Update item use time.
  3858. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  3859. if( itemdb_iscashfood(nameid) )
  3860. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  3861. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  3862. potion_flag = 0;
  3863. return 1;
  3864. }
  3865. /*==========================================
  3866. * Add item on cart for given index.
  3867. * Return:
  3868. * 0 = success
  3869. * 1 = fail
  3870. *------------------------------------------*/
  3871. int pc_cart_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3872. {
  3873. struct item_data *data;
  3874. int i,w;
  3875. nullpo_retr(1, sd);
  3876. nullpo_retr(1, item_data);
  3877. if(item_data->nameid <= 0 || amount <= 0)
  3878. return 1;
  3879. data = itemdb_search(item_data->nameid);
  3880. if( data->stack.cart && amount > data->stack.amount )
  3881. {// item stack limitation
  3882. return 1;
  3883. }
  3884. if( !itemdb_cancartstore(item_data, pc_get_group_level(sd)) )
  3885. { // Check item trade restrictions [Skotlex]
  3886. clif_displaymessage (sd->fd, msg_txt(264));
  3887. return 1;
  3888. }
  3889. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  3890. return 1;
  3891. i = MAX_CART;
  3892. if( itemdb_isstackable2(data) && !item_data->expire_time )
  3893. {
  3894. ARR_FIND( 0, MAX_CART, i,
  3895. sd->status.cart[i].nameid == item_data->nameid &&
  3896. sd->status.cart[i].card[0] == item_data->card[0] && sd->status.cart[i].card[1] == item_data->card[1] &&
  3897. sd->status.cart[i].card[2] == item_data->card[2] && sd->status.cart[i].card[3] == item_data->card[3] );
  3898. };
  3899. if( i < MAX_CART )
  3900. {// item already in cart, stack it
  3901. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  3902. return 1; // no room
  3903. sd->status.cart[i].amount+=amount;
  3904. clif_cart_additem(sd,i,amount,0);
  3905. }
  3906. else
  3907. {// item not stackable or not present, add it
  3908. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  3909. if( i == MAX_CART )
  3910. return 1; // no room
  3911. memcpy(&sd->status.cart[i],item_data,sizeof(sd->status.cart[0]));
  3912. sd->status.cart[i].amount=amount;
  3913. sd->cart_num++;
  3914. clif_cart_additem(sd,i,amount,0);
  3915. }
  3916. sd->status.cart[i].favorite = 0;/* clear */
  3917. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  3918. sd->cart_weight += w;
  3919. clif_updatestatus(sd,SP_CARTINFO);
  3920. return 0;
  3921. }
  3922. /*==========================================
  3923. * Delete item on cart for given index.
  3924. * Return:
  3925. * 0 = success
  3926. * 1 = fail
  3927. *------------------------------------------*/
  3928. int pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  3929. {
  3930. nullpo_retr(1, sd);
  3931. if(sd->status.cart[n].nameid==0 ||
  3932. sd->status.cart[n].amount<amount)
  3933. return 1;
  3934. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  3935. sd->status.cart[n].amount -= amount;
  3936. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  3937. if(sd->status.cart[n].amount <= 0){
  3938. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  3939. sd->cart_num--;
  3940. }
  3941. if(!type) {
  3942. clif_cart_delitem(sd,n,amount);
  3943. clif_updatestatus(sd,SP_CARTINFO);
  3944. }
  3945. return 0;
  3946. }
  3947. /*==========================================
  3948. * Transfer item from inventory to cart.
  3949. * Return:
  3950. * 0 = fail
  3951. * 1 = succes
  3952. *------------------------------------------*/
  3953. int pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  3954. {
  3955. struct item *item_data;
  3956. nullpo_ret(sd);
  3957. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  3958. return 1;
  3959. item_data = &sd->status.inventory[idx];
  3960. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  3961. return 1;
  3962. if( pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE) == 0 )
  3963. return pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  3964. return 1;
  3965. }
  3966. /*==========================================
  3967. * Get number of item in cart.
  3968. * Return:
  3969. -1 = itemid not found or no amount found
  3970. x = remaining itemid on cart after get
  3971. *------------------------------------------*/
  3972. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  3973. {
  3974. struct item* item_data;
  3975. nullpo_retr(-1, sd);
  3976. item_data = &sd->status.cart[idx];
  3977. if( item_data->nameid == 0 || item_data->amount == 0 )
  3978. return -1;
  3979. return item_data->amount - amount;
  3980. }
  3981. /*==========================================
  3982. * Retrieve an item at index idx from cart.
  3983. * Return:
  3984. * 0 = player not found or (FIXME) succes (from pc_cart_delitem)
  3985. * 1 = failure
  3986. *------------------------------------------*/
  3987. int pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  3988. {
  3989. struct item *item_data;
  3990. int flag;
  3991. nullpo_ret(sd);
  3992. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  3993. return 1;
  3994. item_data=&sd->status.cart[idx];
  3995. if(item_data->nameid==0 || amount < 1 || item_data->amount<amount || sd->state.vending )
  3996. return 1;
  3997. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  3998. return pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  3999. clif_additem(sd,0,0,flag);
  4000. return 1;
  4001. }
  4002. /*==========================================
  4003. * Display item stolen msg to player sd
  4004. *------------------------------------------*/
  4005. int pc_show_steal(struct block_list *bl,va_list ap)
  4006. {
  4007. struct map_session_data *sd;
  4008. int itemid;
  4009. struct item_data *item=NULL;
  4010. char output[100];
  4011. sd=va_arg(ap,struct map_session_data *);
  4012. itemid=va_arg(ap,int);
  4013. if((item=itemdb_exists(itemid))==NULL)
  4014. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4015. else
  4016. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4017. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4018. return 0;
  4019. }
  4020. /*==========================================
  4021. * Steal an item from bl (mob).
  4022. * Return:
  4023. * 0 = fail
  4024. * 1 = succes
  4025. *------------------------------------------*/
  4026. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4027. {
  4028. int i,itemid,flag;
  4029. double rate;
  4030. struct status_data *sd_status, *md_status;
  4031. struct mob_data *md;
  4032. struct item tmp_item;
  4033. if(!sd || !bl || bl->type!=BL_MOB)
  4034. return 0;
  4035. md = (TBL_MOB *)bl;
  4036. 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
  4037. return 0;
  4038. sd_status= status_get_status_data(&sd->bl);
  4039. md_status= status_get_status_data(bl);
  4040. if( md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
  4041. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4042. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4043. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4044. ) { //Can't steal from
  4045. md->state.steal_flag = UCHAR_MAX;
  4046. return 0;
  4047. }
  4048. // base skill success chance (percentual)
  4049. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4050. rate += sd->bonus.add_steal_rate;
  4051. if( rate < 1 )
  4052. return 0;
  4053. // Try dropping one item, in the order from first to last possible slot.
  4054. // Droprate is affected by the skill success rate.
  4055. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4056. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4057. break;
  4058. if( i == MAX_STEAL_DROP )
  4059. return 0;
  4060. itemid = md->db->dropitem[i].nameid;
  4061. memset(&tmp_item,0,sizeof(tmp_item));
  4062. tmp_item.nameid = itemid;
  4063. tmp_item.amount = 1;
  4064. tmp_item.identify = itemdb_isidentified(itemid);
  4065. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4066. //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?
  4067. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4068. if(flag) { //Failed to steal due to overweight
  4069. clif_additem(sd,0,0,flag);
  4070. return 0;
  4071. }
  4072. if(battle_config.show_steal_in_same_party)
  4073. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4074. //Logs items, Stolen from mobs [Lupus]
  4075. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4076. //A Rare Steal Global Announce by Lupus
  4077. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4078. struct item_data *i_data;
  4079. char message[128];
  4080. i_data = itemdb_search(itemid);
  4081. sprintf (message, msg_txt(542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4082. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4083. intif_broadcast(message,strlen(message)+1,0);
  4084. }
  4085. return 1;
  4086. }
  4087. /*==========================================
  4088. * Stole zeny from bl (mob)
  4089. * return
  4090. * 0 = fail
  4091. * 1 = success
  4092. *------------------------------------------*/
  4093. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4094. {
  4095. int rate,skill;
  4096. struct mob_data *md;
  4097. if(!sd || !target || target->type != BL_MOB)
  4098. return 0;
  4099. md = (TBL_MOB*)target;
  4100. if( md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS )
  4101. return 0;
  4102. if( mob_is_treasure(md) )
  4103. return 0;
  4104. // FIXME: This formula is either custom or outdated.
  4105. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4106. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4107. if(rnd()%1000 < rate)
  4108. {
  4109. int amount = md->level*10 + rnd()%100;
  4110. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4111. md->state.steal_coin_flag = 1;
  4112. return 1;
  4113. }
  4114. return 0;
  4115. }
  4116. /*==========================================
  4117. * Set's a player position.
  4118. * Return values:
  4119. * 0 - Success.
  4120. * 1 - Invalid map index.
  4121. * 2 - Map not in this map-server, and failed to locate alternate map-server.
  4122. *------------------------------------------*/
  4123. int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4124. {
  4125. struct party_data *p;
  4126. int16 m;
  4127. nullpo_ret(sd);
  4128. if( !mapindex || !mapindex_id2name(mapindex) )
  4129. {
  4130. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4131. return 1;
  4132. }
  4133. if( pc_isdead(sd) )
  4134. { //Revive dead people before warping them
  4135. pc_setstand(sd);
  4136. pc_setrestartvalue(sd,1);
  4137. }
  4138. m = map_mapindex2mapid(mapindex);
  4139. if( map[m].flag.src4instance && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4140. {
  4141. // Request the mapid of this src map into the instance of the party
  4142. int im = instance_map2imap(m, p->instance_id);
  4143. if( im < 0 )
  4144. ; // Player will enter the src map for instances
  4145. else
  4146. { // Changes destiny to the instance map, not the source map
  4147. m = im;
  4148. mapindex = map_id2index(m);
  4149. }
  4150. }
  4151. sd->state.changemap = (sd->mapindex != mapindex);
  4152. sd->state.warping = 1;
  4153. if( sd->state.changemap ) { // Misc map-changing settings
  4154. int i;
  4155. sd->state.pmap = sd->bl.m;
  4156. if (sd->sc.count) { // Cancel some map related stuff.
  4157. if (sd->sc.data[SC_JAILED])
  4158. return 1; //You may not get out!
  4159. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4160. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4161. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4162. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4163. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4164. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4165. if (sd->sc.data[SC_KNOWLEDGE]) {
  4166. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4167. if (sce->timer != INVALID_TIMER)
  4168. delete_timer(sce->timer, status_change_timer);
  4169. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4170. }
  4171. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4172. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4173. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4174. }
  4175. for( i = 0; i < EQI_MAX; i++ ) {
  4176. if( sd->equip_index[ i ] >= 0 )
  4177. if( !pc_isequip( sd , sd->equip_index[ i ] ) )
  4178. pc_unequipitem( sd , sd->equip_index[ i ] , 2 );
  4179. }
  4180. if (battle_config.clear_unit_onwarp&BL_PC)
  4181. skill_clear_unitgroup(&sd->bl);
  4182. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4183. guild_send_dot_remove(sd);
  4184. bg_send_dot_remove(sd);
  4185. if (sd->regen.state.gc)
  4186. sd->regen.state.gc = 0;
  4187. // make sure vending is allowed here
  4188. if (sd->state.vending && map[m].flag.novending) {
  4189. clif_displaymessage (sd->fd, msg_txt(276)); // "You can't open a shop on this map"
  4190. vending_closevending(sd);
  4191. }
  4192. }
  4193. if( m < 0 )
  4194. {
  4195. uint32 ip;
  4196. uint16 port;
  4197. //if can't find any map-servers, just abort setting position.
  4198. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4199. return 2;
  4200. if (sd->npc_id)
  4201. npc_event_dequeue(sd);
  4202. npc_script_event(sd, NPCE_LOGOUT);
  4203. //remove from map, THEN change x/y coordinates
  4204. unit_remove_map_pc(sd,clrtype);
  4205. sd->mapindex = mapindex;
  4206. sd->bl.x=x;
  4207. sd->bl.y=y;
  4208. pc_clean_skilltree(sd);
  4209. chrif_save(sd,2);
  4210. chrif_changemapserver(sd, ip, (short)port);
  4211. //Free session data from this map server [Kevin]
  4212. unit_free_pc(sd);
  4213. return 0;
  4214. }
  4215. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4216. {
  4217. 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);
  4218. x = y = 0; // make it random
  4219. }
  4220. if( x == 0 && y == 0 )
  4221. {// pick a random walkable cell
  4222. do {
  4223. x=rnd()%(map[m].xs-2)+1;
  4224. y=rnd()%(map[m].ys-2)+1;
  4225. } while(map_getcell(m,x,y,CELL_CHKNOPASS));
  4226. }
  4227. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4228. clif_displaymessage (sd->fd, msg_txt(204)); // "You can't open a shop on this cell."
  4229. vending_closevending(sd);
  4230. }
  4231. if(sd->bl.prev != NULL){
  4232. unit_remove_map_pc(sd,clrtype);
  4233. clif_changemap(sd,map[m].index,x,y); // [MouseJstr]
  4234. } else if(sd->state.active)
  4235. //Tag player for rewarping after map-loading is done. [Skotlex]
  4236. sd->state.rewarp = 1;
  4237. sd->mapindex = mapindex;
  4238. sd->bl.m = m;
  4239. sd->bl.x = sd->ud.to_x = x;
  4240. sd->bl.y = sd->ud.to_y = y;
  4241. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4242. { // Increased guild castle regen [Valaris]
  4243. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4244. if(gc && gc->guild_id == sd->status.guild_id)
  4245. sd->regen.state.gc = 1;
  4246. }
  4247. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4248. {
  4249. sd->pd->bl.m = m;
  4250. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4251. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4252. sd->pd->ud.dir = sd->ud.dir;
  4253. }
  4254. if( merc_is_hom_active(sd->hd) )
  4255. {
  4256. sd->hd->bl.m = m;
  4257. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4258. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4259. sd->hd->ud.dir = sd->ud.dir;
  4260. }
  4261. if( sd->md )
  4262. {
  4263. sd->md->bl.m = m;
  4264. sd->md->bl.x = sd->md->ud.to_x = x;
  4265. sd->md->bl.y = sd->md->ud.to_y = y;
  4266. sd->md->ud.dir = sd->ud.dir;
  4267. }
  4268. return 0;
  4269. }
  4270. /*==========================================
  4271. * Warp player sd to random location on current map.
  4272. * May fail if no walkable cell found (1000 attempts).
  4273. * Return:
  4274. * 0 = fail or FIXME success (from pc_setpos)
  4275. * x(1|2) = fail
  4276. *------------------------------------------*/
  4277. int pc_randomwarp(struct map_session_data *sd, clr_type type)
  4278. {
  4279. int x,y,i=0;
  4280. int16 m;
  4281. nullpo_ret(sd);
  4282. m=sd->bl.m;
  4283. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4284. return 0;
  4285. do{
  4286. x=rnd()%(map[m].xs-2)+1;
  4287. y=rnd()%(map[m].ys-2)+1;
  4288. }while(map_getcell(m,x,y,CELL_CHKNOPASS) && (i++)<1000 );
  4289. if (i < 1000)
  4290. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4291. return 0;
  4292. }
  4293. /*==========================================
  4294. * Records a memo point at sd's current position
  4295. * pos - entry to replace, (-1: shift oldest entry out)
  4296. *------------------------------------------*/
  4297. int pc_memo(struct map_session_data* sd, int pos)
  4298. {
  4299. int skill;
  4300. nullpo_ret(sd);
  4301. // check mapflags
  4302. 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) ) {
  4303. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4304. return 0;
  4305. }
  4306. // check inputs
  4307. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4308. return 0; // invalid input
  4309. // check required skill level
  4310. skill = pc_checkskill(sd, AL_WARP);
  4311. if( skill < 1 ) {
  4312. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4313. return 0;
  4314. }
  4315. if( skill < 2 || skill - 2 < pos ) {
  4316. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4317. return 0;
  4318. }
  4319. if( pos == -1 )
  4320. {
  4321. int i;
  4322. // prevent memo-ing the same map multiple times
  4323. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4324. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4325. pos = 0;
  4326. }
  4327. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4328. sd->status.memo_point[pos].x = sd->bl.x;
  4329. sd->status.memo_point[pos].y = sd->bl.y;
  4330. clif_skill_memomessage(sd, 0);
  4331. return 1;
  4332. }
  4333. //
  4334. // Skills
  4335. //
  4336. /*==========================================
  4337. * Return player sd skill_lv learned for given skill
  4338. *------------------------------------------*/
  4339. int pc_checkskill(struct map_session_data *sd,uint16 skill_id)
  4340. {
  4341. if(sd == NULL) return 0;
  4342. if( skill_id >= GD_SKILLBASE && skill_id < GD_MAX )
  4343. {
  4344. struct guild *g;
  4345. if( sd->status.guild_id>0 && (g=guild_search(sd->status.guild_id))!=NULL)
  4346. return guild_checkskill(g,skill_id);
  4347. return 0;
  4348. }
  4349. else if(skill_id >= ARRAYLENGTH(sd->status.skill) )
  4350. {
  4351. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4352. return 0;
  4353. }
  4354. if(sd->status.skill[skill_id].id == skill_id)
  4355. return (sd->status.skill[skill_id].lv);
  4356. return 0;
  4357. }
  4358. /*==========================================
  4359. * Chk if we still have the correct weapon to continue the skill (actually status)
  4360. * If not ending it
  4361. * Return
  4362. * 0 - No status found or all done
  4363. *------------------------------------------*/
  4364. int pc_checkallowskill(struct map_session_data *sd)
  4365. {
  4366. const enum sc_type scw_list[] = {
  4367. SC_TWOHANDQUICKEN,
  4368. SC_ONEHAND,
  4369. SC_AURABLADE,
  4370. SC_PARRYING,
  4371. SC_SPEARQUICKEN,
  4372. SC_ADRENALINE,
  4373. SC_ADRENALINE2,
  4374. SC_DANCING,
  4375. SC_GATLINGFEVER,
  4376. SC_FEARBREEZE
  4377. };
  4378. const enum sc_type scs_list[] = {
  4379. SC_AUTOGUARD,
  4380. SC_DEFENDER,
  4381. SC_REFLECTSHIELD,
  4382. SC_REFLECTDAMAGE
  4383. };
  4384. int i;
  4385. nullpo_ret(sd);
  4386. if(!sd->sc.count)
  4387. return 0;
  4388. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4389. { // Skills requiring specific weapon types
  4390. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4391. continue;
  4392. if(sd->sc.data[scw_list[i]] &&
  4393. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4394. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4395. }
  4396. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4397. // Spurt requires bare hands (feet, in fact xD)
  4398. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4399. if(sd->status.shield <= 0) { // Skills requiring a shield
  4400. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  4401. if(sd->sc.data[scs_list[i]])
  4402. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  4403. }
  4404. return 0;
  4405. }
  4406. /*==========================================
  4407. * Return equiped itemid? on player sd at pos
  4408. * Return
  4409. * -1 : mean nothing equiped
  4410. * idx : (this index could be used in inventory to found item_data)
  4411. *------------------------------------------*/
  4412. int pc_checkequip(struct map_session_data *sd,int pos)
  4413. {
  4414. int i;
  4415. nullpo_retr(-1, sd);
  4416. for(i=0;i<EQI_MAX;i++){
  4417. if(pos & equip_pos[i])
  4418. return sd->equip_index[i];
  4419. }
  4420. return -1;
  4421. }
  4422. /*==========================================
  4423. * Convert's from the client's lame Job ID system
  4424. * to the map server's 'makes sense' system. [Skotlex]
  4425. *------------------------------------------*/
  4426. int pc_jobid2mapid(unsigned short b_class)
  4427. {
  4428. switch(b_class)
  4429. {
  4430. //Novice And 1-1 Jobs
  4431. case JOB_NOVICE: return MAPID_NOVICE;
  4432. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  4433. case JOB_MAGE: return MAPID_MAGE;
  4434. case JOB_ARCHER: return MAPID_ARCHER;
  4435. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  4436. case JOB_MERCHANT: return MAPID_MERCHANT;
  4437. case JOB_THIEF: return MAPID_THIEF;
  4438. case JOB_TAEKWON: return MAPID_TAEKWON;
  4439. case JOB_WEDDING: return MAPID_WEDDING;
  4440. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  4441. case JOB_NINJA: return MAPID_NINJA;
  4442. case JOB_XMAS: return MAPID_XMAS;
  4443. case JOB_SUMMER: return MAPID_SUMMER;
  4444. case JOB_GANGSI: return MAPID_GANGSI;
  4445. //2-1 Jobs
  4446. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  4447. case JOB_KNIGHT: return MAPID_KNIGHT;
  4448. case JOB_WIZARD: return MAPID_WIZARD;
  4449. case JOB_HUNTER: return MAPID_HUNTER;
  4450. case JOB_PRIEST: return MAPID_PRIEST;
  4451. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  4452. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  4453. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  4454. case JOB_KAGEROU:
  4455. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  4456. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  4457. //2-2 Jobs
  4458. case JOB_CRUSADER: return MAPID_CRUSADER;
  4459. case JOB_SAGE: return MAPID_SAGE;
  4460. case JOB_BARD:
  4461. case JOB_DANCER: return MAPID_BARDDANCER;
  4462. case JOB_MONK: return MAPID_MONK;
  4463. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  4464. case JOB_ROGUE: return MAPID_ROGUE;
  4465. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  4466. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  4467. //Trans Novice And Trans 1-1 Jobs
  4468. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  4469. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  4470. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  4471. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  4472. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  4473. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  4474. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  4475. //Trans 2-1 Jobs
  4476. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  4477. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  4478. case JOB_SNIPER: return MAPID_SNIPER;
  4479. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  4480. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  4481. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  4482. //Trans 2-2 Jobs
  4483. case JOB_PALADIN: return MAPID_PALADIN;
  4484. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  4485. case JOB_CLOWN:
  4486. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  4487. case JOB_CHAMPION: return MAPID_CHAMPION;
  4488. case JOB_CREATOR: return MAPID_CREATOR;
  4489. case JOB_STALKER: return MAPID_STALKER;
  4490. //Baby Novice And Baby 1-1 Jobs
  4491. case JOB_BABY: return MAPID_BABY;
  4492. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  4493. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  4494. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  4495. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  4496. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  4497. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  4498. //Baby 2-1 Jobs
  4499. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  4500. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  4501. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  4502. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  4503. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  4504. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  4505. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  4506. //Baby 2-2 Jobs
  4507. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  4508. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  4509. case JOB_BABY_BARD:
  4510. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  4511. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  4512. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  4513. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  4514. //3-1 Jobs
  4515. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  4516. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  4517. case JOB_WARLOCK: return MAPID_WARLOCK;
  4518. case JOB_RANGER: return MAPID_RANGER;
  4519. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  4520. case JOB_MECHANIC: return MAPID_MECHANIC;
  4521. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  4522. //3-2 Jobs
  4523. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  4524. case JOB_SORCERER: return MAPID_SORCERER;
  4525. case JOB_MINSTREL:
  4526. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  4527. case JOB_SURA: return MAPID_SURA;
  4528. case JOB_GENETIC: return MAPID_GENETIC;
  4529. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  4530. //Trans 3-1 Jobs
  4531. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  4532. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  4533. case JOB_RANGER_T: return MAPID_RANGER_T;
  4534. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  4535. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  4536. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  4537. //Trans 3-2 Jobs
  4538. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  4539. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  4540. case JOB_MINSTREL_T:
  4541. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  4542. case JOB_SURA_T: return MAPID_SURA_T;
  4543. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  4544. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  4545. //Baby 3-1 Jobs
  4546. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  4547. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  4548. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  4549. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  4550. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  4551. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  4552. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  4553. //Baby 3-2 Jobs
  4554. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  4555. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  4556. case JOB_BABY_MINSTREL:
  4557. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  4558. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  4559. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  4560. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  4561. default:
  4562. return -1;
  4563. }
  4564. }
  4565. //Reverts the map-style class id to the client-style one.
  4566. int pc_mapid2jobid(unsigned short class_, int sex)
  4567. {
  4568. switch(class_)
  4569. {
  4570. //Novice And 1-1 Jobs
  4571. case MAPID_NOVICE: return JOB_NOVICE;
  4572. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  4573. case MAPID_MAGE: return JOB_MAGE;
  4574. case MAPID_ARCHER: return JOB_ARCHER;
  4575. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  4576. case MAPID_MERCHANT: return JOB_MERCHANT;
  4577. case MAPID_THIEF: return JOB_THIEF;
  4578. case MAPID_TAEKWON: return JOB_TAEKWON;
  4579. case MAPID_WEDDING: return JOB_WEDDING;
  4580. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  4581. case MAPID_NINJA: return JOB_NINJA;
  4582. case MAPID_XMAS: return JOB_XMAS;
  4583. case MAPID_SUMMER: return JOB_SUMMER;
  4584. case MAPID_GANGSI: return JOB_GANGSI;
  4585. //2-1 Jobs
  4586. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  4587. case MAPID_KNIGHT: return JOB_KNIGHT;
  4588. case MAPID_WIZARD: return JOB_WIZARD;
  4589. case MAPID_HUNTER: return JOB_HUNTER;
  4590. case MAPID_PRIEST: return JOB_PRIEST;
  4591. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  4592. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  4593. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  4594. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  4595. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  4596. //2-2 Jobs
  4597. case MAPID_CRUSADER: return JOB_CRUSADER;
  4598. case MAPID_SAGE: return JOB_SAGE;
  4599. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  4600. case MAPID_MONK: return JOB_MONK;
  4601. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  4602. case MAPID_ROGUE: return JOB_ROGUE;
  4603. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  4604. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  4605. //Trans Novice And Trans 2-1 Jobs
  4606. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  4607. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  4608. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  4609. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  4610. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  4611. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  4612. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  4613. //Trans 2-1 Jobs
  4614. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  4615. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  4616. case MAPID_SNIPER: return JOB_SNIPER;
  4617. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  4618. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  4619. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  4620. //Trans 2-2 Jobs
  4621. case MAPID_PALADIN: return JOB_PALADIN;
  4622. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  4623. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  4624. case MAPID_CHAMPION: return JOB_CHAMPION;
  4625. case MAPID_CREATOR: return JOB_CREATOR;
  4626. case MAPID_STALKER: return JOB_STALKER;
  4627. //Baby Novice And Baby 1-1 Jobs
  4628. case MAPID_BABY: return JOB_BABY;
  4629. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  4630. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  4631. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  4632. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  4633. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  4634. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  4635. //Baby 2-1 Jobs
  4636. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  4637. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  4638. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  4639. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  4640. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  4641. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  4642. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  4643. //Baby 2-2 Jobs
  4644. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  4645. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  4646. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  4647. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  4648. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  4649. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  4650. //3-1 Jobs
  4651. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  4652. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  4653. case MAPID_WARLOCK: return JOB_WARLOCK;
  4654. case MAPID_RANGER: return JOB_RANGER;
  4655. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  4656. case MAPID_MECHANIC: return JOB_MECHANIC;
  4657. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  4658. //3-2 Jobs
  4659. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  4660. case MAPID_SORCERER: return JOB_SORCERER;
  4661. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  4662. case MAPID_SURA: return JOB_SURA;
  4663. case MAPID_GENETIC: return JOB_GENETIC;
  4664. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  4665. //Trans 3-1 Jobs
  4666. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  4667. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  4668. case MAPID_RANGER_T: return JOB_RANGER_T;
  4669. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  4670. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  4671. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  4672. //Trans 3-2 Jobs
  4673. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  4674. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  4675. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  4676. case MAPID_SURA_T: return JOB_SURA_T;
  4677. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  4678. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  4679. //Baby 3-1 Jobs
  4680. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  4681. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  4682. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  4683. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  4684. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  4685. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  4686. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  4687. //Baby 3-2 Jobs
  4688. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  4689. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  4690. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  4691. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  4692. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  4693. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  4694. default:
  4695. return -1;
  4696. }
  4697. }
  4698. /*====================================================
  4699. * This function return the name of the job (by [Yor])
  4700. *----------------------------------------------------*/
  4701. const char* job_name(int class_)
  4702. {
  4703. switch (class_) {
  4704. case JOB_NOVICE:
  4705. case JOB_SWORDMAN:
  4706. case JOB_MAGE:
  4707. case JOB_ARCHER:
  4708. case JOB_ACOLYTE:
  4709. case JOB_MERCHANT:
  4710. case JOB_THIEF:
  4711. return msg_txt(550 - JOB_NOVICE+class_);
  4712. case JOB_KNIGHT:
  4713. case JOB_PRIEST:
  4714. case JOB_WIZARD:
  4715. case JOB_BLACKSMITH:
  4716. case JOB_HUNTER:
  4717. case JOB_ASSASSIN:
  4718. return msg_txt(557 - JOB_KNIGHT+class_);
  4719. case JOB_KNIGHT2:
  4720. return msg_txt(557);
  4721. case JOB_CRUSADER:
  4722. case JOB_MONK:
  4723. case JOB_SAGE:
  4724. case JOB_ROGUE:
  4725. case JOB_ALCHEMIST:
  4726. case JOB_BARD:
  4727. case JOB_DANCER:
  4728. return msg_txt(563 - JOB_CRUSADER+class_);
  4729. case JOB_CRUSADER2:
  4730. return msg_txt(563);
  4731. case JOB_WEDDING:
  4732. case JOB_SUPER_NOVICE:
  4733. case JOB_GUNSLINGER:
  4734. case JOB_NINJA:
  4735. case JOB_XMAS:
  4736. return msg_txt(570 - JOB_WEDDING+class_);
  4737. case JOB_SUMMER:
  4738. return msg_txt(621);
  4739. case JOB_NOVICE_HIGH:
  4740. case JOB_SWORDMAN_HIGH:
  4741. case JOB_MAGE_HIGH:
  4742. case JOB_ARCHER_HIGH:
  4743. case JOB_ACOLYTE_HIGH:
  4744. case JOB_MERCHANT_HIGH:
  4745. case JOB_THIEF_HIGH:
  4746. return msg_txt(575 - JOB_NOVICE_HIGH+class_);
  4747. case JOB_LORD_KNIGHT:
  4748. case JOB_HIGH_PRIEST:
  4749. case JOB_HIGH_WIZARD:
  4750. case JOB_WHITESMITH:
  4751. case JOB_SNIPER:
  4752. case JOB_ASSASSIN_CROSS:
  4753. return msg_txt(582 - JOB_LORD_KNIGHT+class_);
  4754. case JOB_LORD_KNIGHT2:
  4755. return msg_txt(582);
  4756. case JOB_PALADIN:
  4757. case JOB_CHAMPION:
  4758. case JOB_PROFESSOR:
  4759. case JOB_STALKER:
  4760. case JOB_CREATOR:
  4761. case JOB_CLOWN:
  4762. case JOB_GYPSY:
  4763. return msg_txt(588 - JOB_PALADIN + class_);
  4764. case JOB_PALADIN2:
  4765. return msg_txt(588);
  4766. case JOB_BABY:
  4767. case JOB_BABY_SWORDMAN:
  4768. case JOB_BABY_MAGE:
  4769. case JOB_BABY_ARCHER:
  4770. case JOB_BABY_ACOLYTE:
  4771. case JOB_BABY_MERCHANT:
  4772. case JOB_BABY_THIEF:
  4773. return msg_txt(595 - JOB_BABY + class_);
  4774. case JOB_BABY_KNIGHT:
  4775. case JOB_BABY_PRIEST:
  4776. case JOB_BABY_WIZARD:
  4777. case JOB_BABY_BLACKSMITH:
  4778. case JOB_BABY_HUNTER:
  4779. case JOB_BABY_ASSASSIN:
  4780. return msg_txt(602 - JOB_BABY_KNIGHT + class_);
  4781. case JOB_BABY_KNIGHT2:
  4782. return msg_txt(602);
  4783. case JOB_BABY_CRUSADER:
  4784. case JOB_BABY_MONK:
  4785. case JOB_BABY_SAGE:
  4786. case JOB_BABY_ROGUE:
  4787. case JOB_BABY_ALCHEMIST:
  4788. case JOB_BABY_BARD:
  4789. case JOB_BABY_DANCER:
  4790. return msg_txt(608 - JOB_BABY_CRUSADER + class_);
  4791. case JOB_BABY_CRUSADER2:
  4792. return msg_txt(608);
  4793. case JOB_SUPER_BABY:
  4794. return msg_txt(615);
  4795. case JOB_TAEKWON:
  4796. return msg_txt(616);
  4797. case JOB_STAR_GLADIATOR:
  4798. case JOB_STAR_GLADIATOR2:
  4799. return msg_txt(617);
  4800. case JOB_SOUL_LINKER:
  4801. return msg_txt(618);
  4802. case JOB_GANGSI:
  4803. case JOB_DEATH_KNIGHT:
  4804. case JOB_DARK_COLLECTOR:
  4805. return msg_txt(622 - JOB_GANGSI+class_);
  4806. case JOB_RUNE_KNIGHT:
  4807. case JOB_WARLOCK:
  4808. case JOB_RANGER:
  4809. case JOB_ARCH_BISHOP:
  4810. case JOB_MECHANIC:
  4811. case JOB_GUILLOTINE_CROSS:
  4812. return msg_txt(625 - JOB_RUNE_KNIGHT+class_);
  4813. case JOB_RUNE_KNIGHT_T:
  4814. case JOB_WARLOCK_T:
  4815. case JOB_RANGER_T:
  4816. case JOB_ARCH_BISHOP_T:
  4817. case JOB_MECHANIC_T:
  4818. case JOB_GUILLOTINE_CROSS_T:
  4819. return msg_txt(681 - JOB_RUNE_KNIGHT_T+class_);
  4820. case JOB_ROYAL_GUARD:
  4821. case JOB_SORCERER:
  4822. case JOB_MINSTREL:
  4823. case JOB_WANDERER:
  4824. case JOB_SURA:
  4825. case JOB_GENETIC:
  4826. case JOB_SHADOW_CHASER:
  4827. return msg_txt(631 - JOB_ROYAL_GUARD+class_);
  4828. case JOB_ROYAL_GUARD_T:
  4829. case JOB_SORCERER_T:
  4830. case JOB_MINSTREL_T:
  4831. case JOB_WANDERER_T:
  4832. case JOB_SURA_T:
  4833. case JOB_GENETIC_T:
  4834. case JOB_SHADOW_CHASER_T:
  4835. return msg_txt(687 - JOB_ROYAL_GUARD_T+class_);
  4836. case JOB_RUNE_KNIGHT2:
  4837. case JOB_RUNE_KNIGHT_T2:
  4838. return msg_txt(625);
  4839. case JOB_ROYAL_GUARD2:
  4840. case JOB_ROYAL_GUARD_T2:
  4841. return msg_txt(631);
  4842. case JOB_RANGER2:
  4843. case JOB_RANGER_T2:
  4844. return msg_txt(627);
  4845. case JOB_MECHANIC2:
  4846. case JOB_MECHANIC_T2:
  4847. return msg_txt(629);
  4848. case JOB_BABY_RUNE:
  4849. case JOB_BABY_WARLOCK:
  4850. case JOB_BABY_RANGER:
  4851. case JOB_BABY_BISHOP:
  4852. case JOB_BABY_MECHANIC:
  4853. case JOB_BABY_CROSS:
  4854. case JOB_BABY_GUARD:
  4855. case JOB_BABY_SORCERER:
  4856. case JOB_BABY_MINSTREL:
  4857. case JOB_BABY_WANDERER:
  4858. case JOB_BABY_SURA:
  4859. case JOB_BABY_GENETIC:
  4860. case JOB_BABY_CHASER:
  4861. return msg_txt(638 - JOB_BABY_RUNE+class_);
  4862. case JOB_BABY_RUNE2:
  4863. return msg_txt(638);
  4864. case JOB_BABY_GUARD2:
  4865. return msg_txt(644);
  4866. case JOB_BABY_RANGER2:
  4867. return msg_txt(640);
  4868. case JOB_BABY_MECHANIC2:
  4869. return msg_txt(642);
  4870. case JOB_SUPER_NOVICE_E:
  4871. case JOB_SUPER_BABY_E:
  4872. return msg_txt(651 - JOB_SUPER_NOVICE_E+class_);
  4873. case JOB_KAGEROU:
  4874. case JOB_OBORO:
  4875. return msg_txt(653 - JOB_KAGEROU+class_);
  4876. default:
  4877. return msg_txt(655);
  4878. }
  4879. }
  4880. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  4881. {
  4882. struct map_session_data *sd;
  4883. struct block_list *tbl;
  4884. sd = map_id2sd(id);
  4885. nullpo_ret(sd);
  4886. if (sd->followtimer != tid){
  4887. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  4888. sd->followtimer = INVALID_TIMER;
  4889. return 0;
  4890. }
  4891. sd->followtimer = INVALID_TIMER;
  4892. tbl = map_id2bl(sd->followtarget);
  4893. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  4894. {
  4895. pc_stop_following(sd);
  4896. return 0;
  4897. }
  4898. // either player or target is currently detached from map blocks (could be teleporting),
  4899. // but still connected to this map, so we'll just increment the timer and check back later
  4900. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  4901. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  4902. {
  4903. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  4904. if (!check_distance_bl(&sd->bl, tbl, 5))
  4905. unit_walktobl(&sd->bl, tbl, 5, 0);
  4906. } else
  4907. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  4908. }
  4909. sd->followtimer = add_timer(
  4910. tick + 1000, // increase time a bit to loosen up map's load
  4911. pc_follow_timer, sd->bl.id, 0);
  4912. return 0;
  4913. }
  4914. int pc_stop_following (struct map_session_data *sd)
  4915. {
  4916. nullpo_ret(sd);
  4917. if (sd->followtimer != INVALID_TIMER) {
  4918. delete_timer(sd->followtimer,pc_follow_timer);
  4919. sd->followtimer = INVALID_TIMER;
  4920. }
  4921. sd->followtarget = -1;
  4922. sd->ud.target_to = 0;
  4923. return 0;
  4924. }
  4925. int pc_follow(struct map_session_data *sd,int target_id)
  4926. {
  4927. struct block_list *bl = map_id2bl(target_id);
  4928. if (bl == NULL /*|| bl->type != BL_PC*/)
  4929. return 1;
  4930. if (sd->followtimer != INVALID_TIMER)
  4931. pc_stop_following(sd);
  4932. sd->followtarget = target_id;
  4933. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  4934. return 0;
  4935. }
  4936. int pc_checkbaselevelup(struct map_session_data *sd) {
  4937. unsigned int next = pc_nextbaseexp(sd);
  4938. if (!next || sd->status.base_exp < next)
  4939. return 0;
  4940. do {
  4941. sd->status.base_exp -= next;
  4942. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  4943. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  4944. sd->status.base_exp = next-1;
  4945. next = pc_gets_status_point(sd->status.base_level);
  4946. sd->status.base_level ++;
  4947. sd->status.status_point += next;
  4948. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  4949. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  4950. status_calc_pet(sd->pd,0);
  4951. clif_updatestatus(sd,SP_STATUSPOINT);
  4952. clif_updatestatus(sd,SP_BASELEVEL);
  4953. clif_updatestatus(sd,SP_BASEEXP);
  4954. clif_updatestatus(sd,SP_NEXTBASEEXP);
  4955. status_calc_pc(sd,0);
  4956. status_percent_heal(&sd->bl,100,100);
  4957. if((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  4958. sc_start(&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  4959. sc_start(&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  4960. sc_start(&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  4961. sc_start(&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  4962. sc_start(&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  4963. if (sd->state.snovice_dead_flag)
  4964. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  4965. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  4966. sc_start(&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  4967. sc_start(&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  4968. }
  4969. clif_misceffect(&sd->bl,0);
  4970. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  4971. if(sd->status.party_id)
  4972. party_send_levelup(sd);
  4973. pc_baselevelchanged(sd);
  4974. return 1;
  4975. }
  4976. void pc_baselevelchanged(struct map_session_data *sd) {
  4977. #ifdef RENEWAL
  4978. int i;
  4979. for( i = 0; i < EQI_MAX; i++ ) {
  4980. if( sd->equip_index[i] >= 0 ) {
  4981. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  4982. pc_unequipitem(sd, sd->equip_index[i], 3);
  4983. }
  4984. }
  4985. #endif
  4986. }
  4987. int pc_checkjoblevelup(struct map_session_data *sd)
  4988. {
  4989. unsigned int next = pc_nextjobexp(sd);
  4990. nullpo_ret(sd);
  4991. if(!next || sd->status.job_exp < next)
  4992. return 0;
  4993. do {
  4994. sd->status.job_exp -= next;
  4995. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  4996. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  4997. sd->status.job_exp = next-1;
  4998. sd->status.job_level ++;
  4999. sd->status.skill_point ++;
  5000. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5001. clif_updatestatus(sd,SP_JOBLEVEL);
  5002. clif_updatestatus(sd,SP_JOBEXP);
  5003. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5004. clif_updatestatus(sd,SP_SKILLPOINT);
  5005. status_calc_pc(sd,0);
  5006. clif_misceffect(&sd->bl,1);
  5007. if (pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd))
  5008. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5009. npc_script_event(sd, NPCE_JOBLVUP);
  5010. return 1;
  5011. }
  5012. /*==========================================
  5013. * Alters experienced based on self bonuses that do not get even shared to the party.
  5014. *------------------------------------------*/
  5015. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5016. {
  5017. int bonus = 0;
  5018. struct status_data *status = status_get_status_data(src);
  5019. if (sd->expaddrace[status->race])
  5020. bonus += sd->expaddrace[status->race];
  5021. bonus += sd->expaddrace[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  5022. if (battle_config.pk_mode &&
  5023. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5024. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5025. if (sd->sc.data[SC_EXPBOOST])
  5026. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5027. *base_exp = (unsigned int) cap_value(*base_exp + (double)*base_exp * bonus/100., 1, UINT_MAX);
  5028. if (sd->sc.data[SC_JEXPBOOST])
  5029. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5030. *job_exp = (unsigned int) cap_value(*job_exp + (double)*job_exp * bonus/100., 1, UINT_MAX);
  5031. return;
  5032. }
  5033. /*==========================================
  5034. * Give x exp at sd player and calculate remaining exp for next lvl
  5035. *------------------------------------------*/
  5036. int pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp,unsigned int job_exp,bool quest)
  5037. {
  5038. float nextbp=0, nextjp=0;
  5039. unsigned int nextb=0, nextj=0;
  5040. nullpo_ret(sd);
  5041. if(sd->bl.prev == NULL || pc_isdead(sd))
  5042. return 0;
  5043. if(!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5044. return 0; // no exp on pvp maps
  5045. if(sd->status.guild_id>0)
  5046. base_exp-=guild_payexp(sd,base_exp);
  5047. if(src) pc_calcexp(sd, &base_exp, &job_exp, src);
  5048. nextb = pc_nextbaseexp(sd);
  5049. nextj = pc_nextjobexp(sd);
  5050. if(sd->state.showexp || battle_config.max_exp_gain_rate){
  5051. if (nextb > 0)
  5052. nextbp = (float) base_exp / (float) nextb;
  5053. if (nextj > 0)
  5054. nextjp = (float) job_exp / (float) nextj;
  5055. if(battle_config.max_exp_gain_rate) {
  5056. if (nextbp > battle_config.max_exp_gain_rate/1000.) {
  5057. //Note that this value should never be greater than the original
  5058. //base_exp, therefore no overflow checks are needed. [Skotlex]
  5059. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5060. if (sd->state.showexp)
  5061. nextbp = (float) base_exp / (float) nextb;
  5062. }
  5063. if (nextjp > battle_config.max_exp_gain_rate/1000.) {
  5064. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5065. if (sd->state.showexp)
  5066. nextjp = (float) job_exp / (float) nextj;
  5067. }
  5068. }
  5069. }
  5070. //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]
  5071. if (base_exp) {
  5072. nextb = nextb?UINT_MAX:pc_thisbaseexp(sd);
  5073. if(sd->status.base_exp > nextb - base_exp)
  5074. sd->status.base_exp = nextb;
  5075. else
  5076. sd->status.base_exp += base_exp;
  5077. pc_checkbaselevelup(sd);
  5078. clif_updatestatus(sd,SP_BASEEXP);
  5079. }
  5080. if (job_exp) {
  5081. nextj = nextj?UINT_MAX:pc_thisjobexp(sd);
  5082. if(sd->status.job_exp > nextj - job_exp)
  5083. sd->status.job_exp = nextj;
  5084. else
  5085. sd->status.job_exp += job_exp;
  5086. pc_checkjoblevelup(sd);
  5087. clif_updatestatus(sd,SP_JOBEXP);
  5088. }
  5089. if(base_exp)
  5090. clif_displayexp(sd, base_exp, SP_BASEEXP, quest);
  5091. if(job_exp)
  5092. clif_displayexp(sd, job_exp, SP_JOBEXP, quest);
  5093. if(sd->state.showexp) {
  5094. char output[256];
  5095. sprintf(output,
  5096. "Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
  5097. clif_disp_onlyself(sd,output,strlen(output));
  5098. }
  5099. return 1;
  5100. }
  5101. /*==========================================
  5102. * Returns max level for this character.
  5103. *------------------------------------------*/
  5104. unsigned int pc_maxbaselv(struct map_session_data *sd)
  5105. {
  5106. return max_level[pc_class2idx(sd->status.class_)][0];
  5107. }
  5108. unsigned int pc_maxjoblv(struct map_session_data *sd)
  5109. {
  5110. return max_level[pc_class2idx(sd->status.class_)][1];
  5111. }
  5112. /*==========================================
  5113. * base level exp lookup.
  5114. *------------------------------------------*/
  5115. //Base exp needed for next level.
  5116. unsigned int pc_nextbaseexp(struct map_session_data *sd)
  5117. {
  5118. nullpo_ret(sd);
  5119. if(sd->status.base_level>=pc_maxbaselv(sd) || sd->status.base_level<=0)
  5120. return 0;
  5121. return exp_table[pc_class2idx(sd->status.class_)][0][sd->status.base_level-1];
  5122. }
  5123. //Base exp needed for this level.
  5124. unsigned int pc_thisbaseexp(struct map_session_data *sd)
  5125. {
  5126. if(sd->status.base_level>pc_maxbaselv(sd) || sd->status.base_level<=1)
  5127. return 0;
  5128. return exp_table[pc_class2idx(sd->status.class_)][0][sd->status.base_level-2];
  5129. }
  5130. /*==========================================
  5131. * job level exp lookup
  5132. * Return:
  5133. * 0 = not found
  5134. * x = exp for level
  5135. *------------------------------------------*/
  5136. //Job exp needed for next level.
  5137. unsigned int pc_nextjobexp(struct map_session_data *sd)
  5138. {
  5139. nullpo_ret(sd);
  5140. if(sd->status.job_level>=pc_maxjoblv(sd) || sd->status.job_level<=0)
  5141. return 0;
  5142. return exp_table[pc_class2idx(sd->status.class_)][1][sd->status.job_level-1];
  5143. }
  5144. //Job exp needed for this level.
  5145. unsigned int pc_thisjobexp(struct map_session_data *sd)
  5146. {
  5147. if(sd->status.job_level>pc_maxjoblv(sd) || sd->status.job_level<=1)
  5148. return 0;
  5149. return exp_table[pc_class2idx(sd->status.class_)][1][sd->status.job_level-2];
  5150. }
  5151. /// Returns the value of the specified stat.
  5152. static int pc_getstat(struct map_session_data* sd, int type)
  5153. {
  5154. nullpo_retr(-1, sd);
  5155. switch( type ) {
  5156. case SP_STR: return sd->status.str;
  5157. case SP_AGI: return sd->status.agi;
  5158. case SP_VIT: return sd->status.vit;
  5159. case SP_INT: return sd->status.int_;
  5160. case SP_DEX: return sd->status.dex;
  5161. case SP_LUK: return sd->status.luk;
  5162. default:
  5163. return -1;
  5164. }
  5165. }
  5166. /// Sets the specified stat to the specified value.
  5167. /// Returns the new value.
  5168. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5169. {
  5170. nullpo_retr(-1, sd);
  5171. switch( type ) {
  5172. case SP_STR: sd->status.str = val; break;
  5173. case SP_AGI: sd->status.agi = val; break;
  5174. case SP_VIT: sd->status.vit = val; break;
  5175. case SP_INT: sd->status.int_ = val; break;
  5176. case SP_DEX: sd->status.dex = val; break;
  5177. case SP_LUK: sd->status.luk = val; break;
  5178. default:
  5179. return -1;
  5180. }
  5181. return val;
  5182. }
  5183. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5184. int pc_gets_status_point(int level)
  5185. {
  5186. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5187. return (statp[level+1] - statp[level]);
  5188. else //Default increase
  5189. return ((level+15) / 5);
  5190. }
  5191. /// Returns the number of stat points needed to change the specified stat by val.
  5192. /// If val is negative, returns the number of stat points that would be needed to
  5193. /// raise the specified stat from (current value - val) to current value.
  5194. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5195. {
  5196. int low, high, sp = 0;
  5197. if ( val == 0 )
  5198. return 0;
  5199. low = pc_getstat(sd,type);
  5200. if ( low >= pc_maxparameter(sd) && val > 0 )
  5201. return 0; // Official servers show '0' when max is reached
  5202. high = low + val;
  5203. if ( val < 0 )
  5204. swap(low, high);
  5205. for ( ; low < high; low++ )
  5206. #ifdef RENEWAL // renewal status point cost formula
  5207. sp += (low < 100) ? (2 + (low - 1) / 10) : (16 + 4 * ((low - 100) / 5));
  5208. #else
  5209. sp += ( 1 + (low + 9) / 10 );
  5210. #endif
  5211. return sp;
  5212. }
  5213. /// Raises a stat by 1.
  5214. /// Obeys max_parameter limits.
  5215. /// Subtracts stat points.
  5216. ///
  5217. /// @param type The stat to change (see enum _sp)
  5218. int pc_statusup(struct map_session_data* sd, int type)
  5219. {
  5220. int max, need, val;
  5221. nullpo_ret(sd);
  5222. // check conditions
  5223. need = pc_need_status_point(sd,type,1);
  5224. if( type < SP_STR || type > SP_LUK || need < 0 || need > sd->status.status_point )
  5225. {
  5226. clif_statusupack(sd,type,0,0);
  5227. return 1;
  5228. }
  5229. // check limits
  5230. max = pc_maxparameter(sd);
  5231. if( pc_getstat(sd,type) >= max )
  5232. {
  5233. clif_statusupack(sd,type,0,0);
  5234. return 1;
  5235. }
  5236. // set new values
  5237. val = pc_setstat(sd, type, pc_getstat(sd,type) + 1);
  5238. sd->status.status_point -= need;
  5239. status_calc_pc(sd,0);
  5240. // update increase cost indicator
  5241. if( need != pc_need_status_point(sd,type,1) )
  5242. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5243. // update statpoint count
  5244. clif_updatestatus(sd,SP_STATUSPOINT);
  5245. // update stat value
  5246. clif_statusupack(sd,type,1,val); // required
  5247. if( val > 255 )
  5248. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5249. return 0;
  5250. }
  5251. /// Raises a stat by the specified amount.
  5252. /// Obeys max_parameter limits.
  5253. /// Does not subtract stat points.
  5254. ///
  5255. /// @param type The stat to change (see enum _sp)
  5256. /// @param val The stat increase amount.
  5257. int pc_statusup2(struct map_session_data* sd, int type, int val)
  5258. {
  5259. int max, need;
  5260. nullpo_ret(sd);
  5261. if( type < SP_STR || type > SP_LUK )
  5262. {
  5263. clif_statusupack(sd,type,0,0);
  5264. return 1;
  5265. }
  5266. need = pc_need_status_point(sd,type,1);
  5267. // set new value
  5268. max = pc_maxparameter(sd);
  5269. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  5270. status_calc_pc(sd,0);
  5271. // update increase cost indicator
  5272. if( need != pc_need_status_point(sd,type,1) )
  5273. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5274. // update stat value
  5275. clif_statusupack(sd,type,1,val); // required
  5276. if( val > 255 )
  5277. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5278. return 0;
  5279. }
  5280. /*==========================================
  5281. * Update skill_lv for player sd
  5282. * Skill point allocation
  5283. *------------------------------------------*/
  5284. int pc_skillup(struct map_session_data *sd,uint16 skill_id)
  5285. {
  5286. nullpo_ret(sd);
  5287. if( skill_id >= GD_SKILLBASE && skill_id < GD_SKILLBASE+MAX_GUILDSKILL )
  5288. {
  5289. guild_skillup(sd, skill_id);
  5290. return 0;
  5291. }
  5292. if( skill_id >= HM_SKILLBASE && skill_id < HM_SKILLBASE+MAX_HOMUNSKILL && sd->hd )
  5293. {
  5294. merc_hom_skillup(sd->hd, skill_id);
  5295. return 0;
  5296. }
  5297. if(skill_id >= MAX_SKILL )
  5298. return 0;
  5299. if( sd->status.skill_point > 0 &&
  5300. sd->status.skill[skill_id].id &&
  5301. sd->status.skill[skill_id].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  5302. sd->status.skill[skill_id].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  5303. {
  5304. sd->status.skill[skill_id].lv++;
  5305. sd->status.skill_point--;
  5306. if( !skill_get_inf(skill_id) )
  5307. status_calc_pc(sd,0); // Only recalculate for passive skills.
  5308. 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) )
  5309. pc_calc_skilltree(sd); // Required to grant all TK Ranger skills.
  5310. else
  5311. pc_check_skilltree(sd, skill_id); // Check if a new skill can Lvlup
  5312. clif_skillup(sd,skill_id);
  5313. clif_updatestatus(sd,SP_SKILLPOINT);
  5314. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  5315. clif_updatestatus(sd,SP_CARTINFO);
  5316. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  5317. clif_skillinfoblock(sd);
  5318. }
  5319. return 0;
  5320. }
  5321. /*==========================================
  5322. * /allskill
  5323. *------------------------------------------*/
  5324. int pc_allskillup(struct map_session_data *sd)
  5325. {
  5326. int i,id;
  5327. nullpo_ret(sd);
  5328. for(i=0;i<MAX_SKILL;i++){
  5329. if (sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED) {
  5330. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  5331. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5332. if (sd->status.skill[i].lv == 0)
  5333. sd->status.skill[i].id = 0;
  5334. }
  5335. }
  5336. if (pc_has_permission(sd, PC_PERM_ALL_SKILL))
  5337. { //Get ALL skills except npc/guild ones. [Skotlex]
  5338. //and except SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  5339. for(i=0;i<MAX_SKILL;i++){
  5340. switch( i ) {
  5341. case SG_DEVIL:
  5342. case MO_TRIPLEATTACK:
  5343. case RG_SNATCHER:
  5344. continue;
  5345. default:
  5346. if( !(skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)) )
  5347. if ( ( sd->status.skill[i].lv = skill_get_max(i) ) )//Nonexistant skills should return a max of 0 anyway.
  5348. sd->status.skill[i].id = i;
  5349. }
  5350. }
  5351. } else {
  5352. int inf2;
  5353. for(i=0;i < MAX_SKILL_TREE && (id=skill_tree[pc_class2idx(sd->status.class_)][i].id)>0;i++){
  5354. inf2 = skill_get_inf2(id);
  5355. if (
  5356. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  5357. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  5358. id==SG_DEVIL
  5359. )
  5360. continue; //Cannot be learned normally.
  5361. sd->status.skill[id].id = id;
  5362. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_); // celest
  5363. }
  5364. }
  5365. status_calc_pc(sd,0);
  5366. //Required because if you could level up all skills previously,
  5367. //the update will not be sent as only the lv variable changes.
  5368. clif_skillinfoblock(sd);
  5369. return 0;
  5370. }
  5371. /*==========================================
  5372. * /resetlvl
  5373. *------------------------------------------*/
  5374. int pc_resetlvl(struct map_session_data* sd,int type)
  5375. {
  5376. int i;
  5377. nullpo_ret(sd);
  5378. if (type != 3) //Also reset skills
  5379. pc_resetskill(sd, 0);
  5380. if(type == 1){
  5381. sd->status.skill_point=0;
  5382. sd->status.base_level=1;
  5383. sd->status.job_level=1;
  5384. sd->status.base_exp=0;
  5385. sd->status.job_exp=0;
  5386. if(sd->sc.option !=0)
  5387. sd->sc.option = 0;
  5388. sd->status.str=1;
  5389. sd->status.agi=1;
  5390. sd->status.vit=1;
  5391. sd->status.int_=1;
  5392. sd->status.dex=1;
  5393. sd->status.luk=1;
  5394. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  5395. sd->status.status_point=100; // not 88 [celest]
  5396. // give platinum skills upon changing
  5397. pc_skill(sd,142,1,0);
  5398. pc_skill(sd,143,1,0);
  5399. }
  5400. }
  5401. if(type == 2){
  5402. sd->status.skill_point=0;
  5403. sd->status.base_level=1;
  5404. sd->status.job_level=1;
  5405. sd->status.base_exp=0;
  5406. sd->status.job_exp=0;
  5407. }
  5408. if(type == 3){
  5409. sd->status.base_level=1;
  5410. sd->status.base_exp=0;
  5411. }
  5412. if(type == 4){
  5413. sd->status.job_level=1;
  5414. sd->status.job_exp=0;
  5415. }
  5416. clif_updatestatus(sd,SP_STATUSPOINT);
  5417. clif_updatestatus(sd,SP_STR);
  5418. clif_updatestatus(sd,SP_AGI);
  5419. clif_updatestatus(sd,SP_VIT);
  5420. clif_updatestatus(sd,SP_INT);
  5421. clif_updatestatus(sd,SP_DEX);
  5422. clif_updatestatus(sd,SP_LUK);
  5423. clif_updatestatus(sd,SP_BASELEVEL);
  5424. clif_updatestatus(sd,SP_JOBLEVEL);
  5425. clif_updatestatus(sd,SP_STATUSPOINT);
  5426. clif_updatestatus(sd,SP_BASEEXP);
  5427. clif_updatestatus(sd,SP_JOBEXP);
  5428. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5429. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5430. clif_updatestatus(sd,SP_SKILLPOINT);
  5431. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5432. clif_updatestatus(sd,SP_UAGI);
  5433. clif_updatestatus(sd,SP_UVIT);
  5434. clif_updatestatus(sd,SP_UINT);
  5435. clif_updatestatus(sd,SP_UDEX);
  5436. clif_updatestatus(sd,SP_ULUK); // End Addition
  5437. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  5438. if(sd->equip_index[i] >= 0)
  5439. if(!pc_isequip(sd,sd->equip_index[i]))
  5440. pc_unequipitem(sd,sd->equip_index[i],2);
  5441. }
  5442. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  5443. party_send_levelup(sd);
  5444. status_calc_pc(sd,0);
  5445. clif_skillinfoblock(sd);
  5446. return 0;
  5447. }
  5448. /*==========================================
  5449. * /resetstate
  5450. *------------------------------------------*/
  5451. int pc_resetstate(struct map_session_data* sd)
  5452. {
  5453. nullpo_ret(sd);
  5454. if (battle_config.use_statpoint_table)
  5455. { // New statpoint table used here - Dexity
  5456. if (sd->status.base_level > MAX_LEVEL)
  5457. { //statp[] goes out of bounds, can't reset!
  5458. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  5459. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  5460. return 0;
  5461. }
  5462. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  5463. }
  5464. else
  5465. {
  5466. int add=0;
  5467. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  5468. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  5469. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  5470. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  5471. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  5472. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  5473. sd->status.status_point+=add;
  5474. }
  5475. pc_setstat(sd, SP_STR, 1);
  5476. pc_setstat(sd, SP_AGI, 1);
  5477. pc_setstat(sd, SP_VIT, 1);
  5478. pc_setstat(sd, SP_INT, 1);
  5479. pc_setstat(sd, SP_DEX, 1);
  5480. pc_setstat(sd, SP_LUK, 1);
  5481. clif_updatestatus(sd,SP_STR);
  5482. clif_updatestatus(sd,SP_AGI);
  5483. clif_updatestatus(sd,SP_VIT);
  5484. clif_updatestatus(sd,SP_INT);
  5485. clif_updatestatus(sd,SP_DEX);
  5486. clif_updatestatus(sd,SP_LUK);
  5487. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5488. clif_updatestatus(sd,SP_UAGI);
  5489. clif_updatestatus(sd,SP_UVIT);
  5490. clif_updatestatus(sd,SP_UINT);
  5491. clif_updatestatus(sd,SP_UDEX);
  5492. clif_updatestatus(sd,SP_ULUK); // End Addition
  5493. clif_updatestatus(sd,SP_STATUSPOINT);
  5494. if( sd->mission_mobid ) { //bugreport:2200
  5495. sd->mission_mobid = 0;
  5496. sd->mission_count = 0;
  5497. pc_setglobalreg(sd,"TK_MISSION_ID", 0);
  5498. }
  5499. status_calc_pc(sd,0);
  5500. return 1;
  5501. }
  5502. /*==========================================
  5503. * /resetskill
  5504. * if flag&1, perform block resync and status_calc call.
  5505. * if flag&2, just count total amount of skill points used by player, do not really reset.
  5506. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  5507. *------------------------------------------*/
  5508. int pc_resetskill(struct map_session_data* sd, int flag)
  5509. {
  5510. int i, lv, inf2, skill_point=0;
  5511. nullpo_ret(sd);
  5512. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  5513. return 0;
  5514. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  5515. /**
  5516. * 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)
  5517. **/
  5518. if( (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5519. return 0;
  5520. if( pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd) )
  5521. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  5522. i = sd->sc.option;
  5523. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  5524. i &= ~OPTION_RIDING;
  5525. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  5526. i &= ~OPTION_FALCON;
  5527. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  5528. i &= ~OPTION_DRAGON;
  5529. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  5530. i &= ~OPTION_WUG;
  5531. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  5532. i &= ~OPTION_WUGRIDER;
  5533. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  5534. i &= ~OPTION_MADOGEAR;
  5535. if( i&OPTION_MOUNTING )
  5536. i &= ~OPTION_MOUNTING;
  5537. #ifndef NEW_CARTS
  5538. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  5539. i &= ~OPTION_CART;
  5540. #else
  5541. if( sd->sc.data[SC_PUSH_CART] )
  5542. pc_setcart(sd, 0);
  5543. #endif
  5544. if( i != sd->sc.option )
  5545. pc_setoption(sd, i);
  5546. if( merc_is_hom_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  5547. merc_hom_vaporize(sd, 0);
  5548. }
  5549. for( i = 1; i < MAX_SKILL; i++ )
  5550. {
  5551. lv = sd->status.skill[i].lv;
  5552. if (lv < 1) continue;
  5553. inf2 = skill_get_inf2(i);
  5554. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  5555. continue;
  5556. // Don't reset trick dead if not a novice/baby
  5557. if( i == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5558. {
  5559. sd->status.skill[i].lv = 0;
  5560. sd->status.skill[i].flag = 0;
  5561. continue;
  5562. }
  5563. // do not reset basic skill
  5564. if( i == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5565. continue;
  5566. if( flag&4 && !skill_ischangesex(i) )
  5567. continue;
  5568. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  5569. { //Only handle quest skills in a special way when you can't learn them manually
  5570. if( battle_config.quest_skill_reset && !(flag&2) )
  5571. { //Wipe them
  5572. sd->status.skill[i].lv = 0;
  5573. sd->status.skill[i].flag = 0;
  5574. }
  5575. continue;
  5576. }
  5577. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  5578. skill_point += lv;
  5579. else
  5580. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  5581. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  5582. if( !(flag&2) )
  5583. {// reset
  5584. sd->status.skill[i].lv = 0;
  5585. sd->status.skill[i].flag = 0;
  5586. }
  5587. }
  5588. if( flag&2 || !skill_point ) return skill_point;
  5589. sd->status.skill_point += skill_point;
  5590. if( flag&1 )
  5591. {
  5592. clif_updatestatus(sd,SP_SKILLPOINT);
  5593. clif_skillinfoblock(sd);
  5594. status_calc_pc(sd,0);
  5595. }
  5596. return skill_point;
  5597. }
  5598. /*==========================================
  5599. * /resetfeel [Komurka]
  5600. *------------------------------------------*/
  5601. int pc_resetfeel(struct map_session_data* sd)
  5602. {
  5603. int i;
  5604. nullpo_ret(sd);
  5605. for (i=0; i<MAX_PC_FEELHATE; i++)
  5606. {
  5607. sd->feel_map[i].m = -1;
  5608. sd->feel_map[i].index = 0;
  5609. pc_setglobalreg(sd,sg_info[i].feel_var,0);
  5610. }
  5611. return 0;
  5612. }
  5613. int pc_resethate(struct map_session_data* sd)
  5614. {
  5615. int i;
  5616. nullpo_ret(sd);
  5617. for (i=0; i<3; i++)
  5618. {
  5619. sd->hate_mob[i] = -1;
  5620. pc_setglobalreg(sd,sg_info[i].hate_var,0);
  5621. }
  5622. return 0;
  5623. }
  5624. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  5625. {
  5626. int i, bonus = 0;
  5627. nullpo_ret(sd);
  5628. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  5629. if( i < ARRAYLENGTH(sd->skillatk) ) bonus = sd->skillatk[i].val;
  5630. if(sd->sc.data[SC_PYROTECHNIC_OPTION] || sd->sc.data[SC_AQUAPLAY_OPTION])
  5631. bonus += 10;
  5632. return bonus;
  5633. }
  5634. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  5635. int i, bonus = sd->bonus.add_heal_rate;
  5636. if( bonus ) {
  5637. switch( skill_id ) {
  5638. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  5639. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  5640. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  5641. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  5642. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  5643. }
  5644. }
  5645. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  5646. if( i < ARRAYLENGTH(sd->skillheal) )
  5647. bonus += sd->skillheal[i].val;
  5648. return bonus;
  5649. }
  5650. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  5651. int i, bonus = sd->bonus.add_heal2_rate;
  5652. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  5653. if( i < ARRAYLENGTH(sd->skillheal2) )
  5654. bonus += sd->skillheal2[i].val;
  5655. return bonus;
  5656. }
  5657. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  5658. {
  5659. if( !pc_isdead(sd) )
  5660. return; // not applicable
  5661. if( sd->bg_id && bg_member_respawn(sd) )
  5662. return; // member revived by battleground
  5663. pc_setstand(sd);
  5664. pc_setrestartvalue(sd,3);
  5665. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
  5666. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  5667. }
  5668. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  5669. {
  5670. struct map_session_data *sd = map_id2sd(id);
  5671. if( sd != NULL )
  5672. {
  5673. sd->pvp_point=0;
  5674. pc_respawn(sd,CLR_OUTSIGHT);
  5675. }
  5676. return 0;
  5677. }
  5678. /*==========================================
  5679. * Invoked when a player has received damage
  5680. *------------------------------------------*/
  5681. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  5682. {
  5683. if (sp) clif_updatestatus(sd,SP_SP);
  5684. if (hp) clif_updatestatus(sd,SP_HP);
  5685. else return;
  5686. if( !src || src == &sd->bl )
  5687. return;
  5688. if( pc_issit(sd) )
  5689. {
  5690. pc_setstand(sd);
  5691. skill_sit(sd,0);
  5692. }
  5693. if( sd->progressbar.npc_id )
  5694. clif_progressbar_abort(sd);
  5695. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  5696. pet_target_check(sd,src,1);
  5697. if( sd->status.ele_id > 0 )
  5698. elemental_set_target(sd,src);
  5699. sd->canlog_tick = gettick();
  5700. }
  5701. /*==========================================
  5702. * Invoked when a player has negative current hp
  5703. *------------------------------------------*/
  5704. int pc_dead(struct map_session_data *sd,struct block_list *src)
  5705. {
  5706. int i=0,j=0,k=0;
  5707. unsigned int tick = gettick();
  5708. for(k = 0; k < 5; k++)
  5709. if (sd->devotion[k]){
  5710. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  5711. if (devsd)
  5712. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  5713. sd->devotion[k] = 0;
  5714. }
  5715. if(sd->status.pet_id > 0 && sd->pd) {
  5716. struct pet_data *pd = sd->pd;
  5717. if( !map[sd->bl.m].flag.noexppenalty ) {
  5718. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  5719. if( pd->pet.intimate < 0 )
  5720. pd->pet.intimate = 0;
  5721. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  5722. }
  5723. if( sd->pd->target_id ) // Unlock all targets...
  5724. pet_unlocktarget(sd->pd);
  5725. }
  5726. if (sd->status.hom_id > 0){
  5727. if(battle_config.homunculus_auto_vapor && sd->hd && !sd->hd->sc.data[SC_LIGHT_OF_REGENE])
  5728. merc_hom_vaporize(sd, 0);
  5729. }
  5730. if( sd->md )
  5731. merc_delete(sd->md, 3); // Your mercenary soldier has ran away.
  5732. if( sd->ed )
  5733. elemental_delete(sd->ed, 0);
  5734. // Leave duel if you die [LuzZza]
  5735. if(battle_config.duel_autoleave_when_die) {
  5736. if(sd->duel_group > 0)
  5737. duel_leave(sd->duel_group, sd);
  5738. if(sd->duel_invite > 0)
  5739. duel_reject(sd->duel_invite, sd);
  5740. }
  5741. pc_setglobalreg(sd,"PC_DIE_COUNTER",sd->die_counter+1);
  5742. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  5743. if( sd->bg_id ) {
  5744. struct battleground_data *bg;
  5745. if( (bg = bg_team_search(sd->bg_id)) != NULL && bg->die_event[0] )
  5746. npc_event(sd, bg->die_event, 0);
  5747. }
  5748. // Clear anything NPC-related when you die and was interacting with one.
  5749. if (sd->npc_id)
  5750. {
  5751. if (sd->state.using_fake_npc) {
  5752. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  5753. sd->state.using_fake_npc = 0;
  5754. }
  5755. if (sd->state.menu_or_input)
  5756. sd->state.menu_or_input = 0;
  5757. if (sd->npc_menu)
  5758. sd->npc_menu = 0;
  5759. sd->npc_id = 0;
  5760. if (sd->st && sd->st->state != END)
  5761. sd->st->state = END;
  5762. }
  5763. npc_script_event(sd,NPCE_DIE);
  5764. /* e.g. not killed thru pc_damage */
  5765. if( pc_issit(sd) ) {
  5766. clif_status_load(&sd->bl,SI_SIT,0);
  5767. }
  5768. pc_setdead(sd);
  5769. //Reset menu skills/item skills
  5770. if (sd->skillitem)
  5771. sd->skillitem = sd->skillitemlv = 0;
  5772. if (sd->menuskill_id)
  5773. sd->menuskill_id = sd->menuskill_val = 0;
  5774. //Reset ticks.
  5775. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  5776. if ( sd && sd->spiritball )
  5777. pc_delspiritball(sd,sd->spiritball,0);
  5778. for(i = 1; i < 5; i++)
  5779. pc_del_talisman(sd, sd->talisman[i], i);
  5780. if (src)
  5781. switch (src->type) {
  5782. case BL_MOB:
  5783. {
  5784. struct mob_data *md=(struct mob_data *)src;
  5785. if(md->target_id==sd->bl.id)
  5786. mob_unlocktarget(md,tick);
  5787. if(battle_config.mobs_level_up && md->status.hp &&
  5788. (unsigned int)md->level < pc_maxbaselv(sd) &&
  5789. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  5790. ) { // monster level up [Valaris]
  5791. clif_misceffect(&md->bl,0);
  5792. md->level++;
  5793. status_calc_mob(md, 0);
  5794. status_percent_heal(src,10,0);
  5795. if( battle_config.show_mob_info&4 )
  5796. {// update name with new level
  5797. clif_charnameack(0, &md->bl);
  5798. }
  5799. }
  5800. src = battle_get_master(src); // Maybe Player Summon
  5801. }
  5802. break;
  5803. case BL_PET: //Pass on to master...
  5804. src = &((TBL_PET*)src)->msd->bl;
  5805. break;
  5806. case BL_HOM:
  5807. src = &((TBL_HOM*)src)->master->bl;
  5808. break;
  5809. case BL_MER:
  5810. src = &((TBL_MER*)src)->master->bl;
  5811. break;
  5812. }
  5813. if (src && src->type == BL_PC)
  5814. {
  5815. struct map_session_data *ssd = (struct map_session_data *)src;
  5816. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  5817. npc_script_event(ssd, NPCE_KILLPC);
  5818. if (battle_config.pk_mode&2) {
  5819. ssd->status.manner -= 5;
  5820. if(ssd->status.manner < 0)
  5821. sc_start(src,SC_NOCHAT,100,0,0);
  5822. #if 0
  5823. // PK/Karma system code (not enabled yet) [celest]
  5824. // originally from Kade Online, so i don't know if any of these is correct ^^;
  5825. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  5826. // karma going down = more 'good' / more honourable.
  5827. // The Karma System way...
  5828. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  5829. sd->status.karma--;
  5830. ssd->status.karma--;
  5831. }
  5832. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  5833. ssd->status.karma++;
  5834. // or the PK System way...
  5835. if (sd->status.karma > 0) // player killed is dishonourable?
  5836. ssd->status.karma--; // honour points earned
  5837. sd->status.karma++; // honour points lost
  5838. // To-do: Receive exp on certain occasions
  5839. #endif
  5840. }
  5841. }
  5842. if(battle_config.bone_drop==2
  5843. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  5844. {
  5845. struct item item_tmp;
  5846. memset(&item_tmp,0,sizeof(item_tmp));
  5847. item_tmp.nameid=ITEMID_SKULL_;
  5848. item_tmp.identify=1;
  5849. item_tmp.card[0]=CARD0_CREATE;
  5850. item_tmp.card[1]=0;
  5851. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  5852. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  5853. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5854. }
  5855. // activate Steel body if a super novice dies at 99+% exp [celest]
  5856. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && !sd->state.snovice_dead_flag)
  5857. {
  5858. unsigned int next = pc_nextbaseexp(sd);
  5859. if( next == 0 ) next = pc_thisbaseexp(sd);
  5860. if( get_percentage(sd->status.base_exp,next) >= 99 ) {
  5861. sd->state.snovice_dead_flag = 1;
  5862. pc_setstand(sd);
  5863. status_percent_heal(&sd->bl, 100, 100);
  5864. clif_resurrection(&sd->bl, 1);
  5865. if(battle_config.pc_invincible_time)
  5866. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  5867. sc_start(&sd->bl,status_skill2sc(MO_STEELBODY),100,1,skill_get_time(MO_STEELBODY,1));
  5868. if(map_flag_gvg(sd->bl.m))
  5869. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5870. return 0;
  5871. }
  5872. }
  5873. // changed penalty options, added death by player if pk_mode [Valaris]
  5874. if(battle_config.death_penalty_type
  5875. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  5876. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  5877. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  5878. {
  5879. unsigned int base_penalty =0;
  5880. if (battle_config.death_penalty_base > 0) {
  5881. switch (battle_config.death_penalty_type) {
  5882. case 1:
  5883. base_penalty = (unsigned int) ((double)pc_nextbaseexp(sd) * (double)battle_config.death_penalty_base/10000);
  5884. break;
  5885. case 2:
  5886. base_penalty = (unsigned int) ((double)sd->status.base_exp * (double)battle_config.death_penalty_base/10000);
  5887. break;
  5888. }
  5889. if(base_penalty) {
  5890. if (battle_config.pk_mode && src && src->type==BL_PC)
  5891. base_penalty*=2;
  5892. sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
  5893. clif_updatestatus(sd,SP_BASEEXP);
  5894. }
  5895. }
  5896. if(battle_config.death_penalty_job > 0)
  5897. {
  5898. base_penalty = 0;
  5899. switch (battle_config.death_penalty_type) {
  5900. case 1:
  5901. base_penalty = (unsigned int) ((double)pc_nextjobexp(sd) * (double)battle_config.death_penalty_job/10000);
  5902. break;
  5903. case 2:
  5904. base_penalty = (unsigned int) ((double)sd->status.job_exp * (double)battle_config.death_penalty_job/10000);
  5905. break;
  5906. }
  5907. if(base_penalty) {
  5908. if (battle_config.pk_mode && src && src->type==BL_PC)
  5909. base_penalty*=2;
  5910. sd->status.job_exp -= min(sd->status.job_exp, base_penalty);
  5911. clif_updatestatus(sd,SP_JOBEXP);
  5912. }
  5913. }
  5914. if(battle_config.zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty)
  5915. {
  5916. base_penalty = (unsigned int)((double)sd->status.zeny * (double)battle_config.zeny_penalty / 10000.);
  5917. if(base_penalty)
  5918. pc_payzeny(sd, base_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  5919. }
  5920. }
  5921. if(map[sd->bl.m].flag.pvp_nightmaredrop)
  5922. { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  5923. for(j=0;j<MAX_DROP_PER_MAP;j++){
  5924. int id = map[sd->bl.m].drop_list[j].drop_id;
  5925. int type = map[sd->bl.m].drop_list[j].drop_type;
  5926. int per = map[sd->bl.m].drop_list[j].drop_per;
  5927. if(id == 0)
  5928. continue;
  5929. if(id == -1){
  5930. int eq_num=0,eq_n[MAX_INVENTORY];
  5931. memset(eq_n,0,sizeof(eq_n));
  5932. for(i=0;i<MAX_INVENTORY;i++){
  5933. int k;
  5934. if( (type == 1 && !sd->status.inventory[i].equip)
  5935. || (type == 2 && sd->status.inventory[i].equip)
  5936. || type == 3)
  5937. {
  5938. ARR_FIND( 0, MAX_INVENTORY, k, eq_n[k] <= 0 );
  5939. if( k < MAX_INVENTORY )
  5940. eq_n[k] = i;
  5941. eq_num++;
  5942. }
  5943. }
  5944. if(eq_num > 0){
  5945. int n = eq_n[rnd()%eq_num];
  5946. if(rnd()%10000 < per){
  5947. if(sd->status.inventory[n].equip)
  5948. pc_unequipitem(sd,n,3);
  5949. pc_dropitem(sd,n,1);
  5950. }
  5951. }
  5952. }
  5953. else if(id > 0){
  5954. for(i=0;i<MAX_INVENTORY;i++){
  5955. if(sd->status.inventory[i].nameid == id
  5956. && rnd()%10000 < per
  5957. && ((type == 1 && !sd->status.inventory[i].equip)
  5958. || (type == 2 && sd->status.inventory[i].equip)
  5959. || type == 3) ){
  5960. if(sd->status.inventory[i].equip)
  5961. pc_unequipitem(sd,i,3);
  5962. pc_dropitem(sd,i,1);
  5963. break;
  5964. }
  5965. }
  5966. }
  5967. }
  5968. }
  5969. // pvp
  5970. // disable certain pvp functions on pk_mode [Valaris]
  5971. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank )
  5972. {
  5973. sd->pvp_point -= 5;
  5974. sd->pvp_lost++;
  5975. if( src && src->type == BL_PC )
  5976. {
  5977. struct map_session_data *ssd = (struct map_session_data *)src;
  5978. ssd->pvp_point++;
  5979. ssd->pvp_won++;
  5980. }
  5981. if( sd->pvp_point < 0 )
  5982. {
  5983. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  5984. return 1|8;
  5985. }
  5986. }
  5987. //GvG
  5988. if( map_flag_gvg(sd->bl.m) )
  5989. {
  5990. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  5991. return 1|8;
  5992. }
  5993. else if( sd->bg_id )
  5994. {
  5995. struct battleground_data *bg = bg_team_search(sd->bg_id);
  5996. if( bg && bg->mapindex > 0 )
  5997. { // Respawn by BG
  5998. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  5999. return 1|8;
  6000. }
  6001. }
  6002. //Reset "can log out" tick.
  6003. if( battle_config.prevent_logout )
  6004. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6005. return 1;
  6006. }
  6007. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6008. if(hp) clif_updatestatus(sd,SP_HP);
  6009. if(sp) clif_updatestatus(sd,SP_SP);
  6010. pc_setstand(sd);
  6011. if(battle_config.pc_invincible_time > 0)
  6012. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6013. if( sd->state.gmaster_flag ) {
  6014. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->state.gmaster_flag,GD_LEADERSHIP));
  6015. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->state.gmaster_flag,GD_GLORYWOUNDS));
  6016. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->state.gmaster_flag,GD_SOULCOLD));
  6017. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->state.gmaster_flag,GD_HAWKEYES));
  6018. }
  6019. }
  6020. // script
  6021. //
  6022. /*==========================================
  6023. * script reading pc status registry
  6024. *------------------------------------------*/
  6025. int pc_readparam(struct map_session_data* sd,int type)
  6026. {
  6027. int val = 0;
  6028. nullpo_ret(sd);
  6029. switch(type) {
  6030. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6031. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6032. case SP_ZENY: val = sd->status.zeny; break;
  6033. case SP_BASELEVEL: val = sd->status.base_level; break;
  6034. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6035. case SP_CLASS: val = sd->status.class_; break;
  6036. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6037. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6038. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6039. case SP_SEX: val = sd->status.sex; break;
  6040. case SP_WEIGHT: val = sd->weight; break;
  6041. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6042. case SP_BASEEXP: val = sd->status.base_exp; break;
  6043. case SP_JOBEXP: val = sd->status.job_exp; break;
  6044. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6045. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6046. case SP_HP: val = sd->battle_status.hp; break;
  6047. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6048. case SP_SP: val = sd->battle_status.sp; break;
  6049. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6050. case SP_STR: val = sd->status.str; break;
  6051. case SP_AGI: val = sd->status.agi; break;
  6052. case SP_VIT: val = sd->status.vit; break;
  6053. case SP_INT: val = sd->status.int_; break;
  6054. case SP_DEX: val = sd->status.dex; break;
  6055. case SP_LUK: val = sd->status.luk; break;
  6056. case SP_KARMA: val = sd->status.karma; break;
  6057. case SP_MANNER: val = sd->status.manner; break;
  6058. case SP_FAME: val = sd->status.fame; break;
  6059. case SP_KILLERRID: val = sd->killerrid; break;
  6060. case SP_KILLEDRID: val = sd->killedrid; break;
  6061. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6062. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6063. }
  6064. return val;
  6065. }
  6066. /*==========================================
  6067. * script set pc status registry
  6068. *------------------------------------------*/
  6069. int pc_setparam(struct map_session_data *sd,int type,int val)
  6070. {
  6071. int i = 0;
  6072. nullpo_ret(sd);
  6073. switch(type){
  6074. case SP_BASELEVEL:
  6075. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  6076. val = pc_maxbaselv(sd);
  6077. if ((unsigned int)val > sd->status.base_level) {
  6078. int stat=0;
  6079. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  6080. stat += pc_gets_status_point(sd->status.base_level + i);
  6081. sd->status.status_point += stat;
  6082. }
  6083. sd->status.base_level = (unsigned int)val;
  6084. sd->status.base_exp = 0;
  6085. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  6086. clif_updatestatus(sd, SP_NEXTBASEEXP);
  6087. clif_updatestatus(sd, SP_STATUSPOINT);
  6088. clif_updatestatus(sd, SP_BASEEXP);
  6089. status_calc_pc(sd, 0);
  6090. if(sd->status.party_id)
  6091. {
  6092. party_send_levelup(sd);
  6093. }
  6094. break;
  6095. case SP_JOBLEVEL:
  6096. if ((unsigned int)val >= sd->status.job_level) {
  6097. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  6098. sd->status.skill_point += val - sd->status.job_level;
  6099. clif_updatestatus(sd, SP_SKILLPOINT);
  6100. }
  6101. sd->status.job_level = (unsigned int)val;
  6102. sd->status.job_exp = 0;
  6103. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  6104. clif_updatestatus(sd, SP_NEXTJOBEXP);
  6105. clif_updatestatus(sd, SP_JOBEXP);
  6106. status_calc_pc(sd, 0);
  6107. break;
  6108. case SP_SKILLPOINT:
  6109. sd->status.skill_point = val;
  6110. break;
  6111. case SP_STATUSPOINT:
  6112. sd->status.status_point = val;
  6113. break;
  6114. case SP_ZENY:
  6115. if( val < 0 )
  6116. return 0;// can't set negative zeny
  6117. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  6118. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  6119. break;
  6120. case SP_BASEEXP:
  6121. if(pc_nextbaseexp(sd) > 0) {
  6122. sd->status.base_exp = val;
  6123. pc_checkbaselevelup(sd);
  6124. }
  6125. break;
  6126. case SP_JOBEXP:
  6127. if(pc_nextjobexp(sd) > 0) {
  6128. sd->status.job_exp = val;
  6129. pc_checkjoblevelup(sd);
  6130. }
  6131. break;
  6132. case SP_SEX:
  6133. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  6134. break;
  6135. case SP_WEIGHT:
  6136. sd->weight = val;
  6137. break;
  6138. case SP_MAXWEIGHT:
  6139. sd->max_weight = val;
  6140. break;
  6141. case SP_HP:
  6142. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  6143. break;
  6144. case SP_MAXHP:
  6145. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  6146. if( sd->battle_status.max_hp < sd->battle_status.hp )
  6147. {
  6148. sd->battle_status.hp = sd->battle_status.max_hp;
  6149. clif_updatestatus(sd, SP_HP);
  6150. }
  6151. break;
  6152. case SP_SP:
  6153. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  6154. break;
  6155. case SP_MAXSP:
  6156. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  6157. if( sd->battle_status.max_sp < sd->battle_status.sp )
  6158. {
  6159. sd->battle_status.sp = sd->battle_status.max_sp;
  6160. clif_updatestatus(sd, SP_SP);
  6161. }
  6162. break;
  6163. case SP_STR:
  6164. sd->status.str = cap_value(val, 1, pc_maxparameter(sd));
  6165. break;
  6166. case SP_AGI:
  6167. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd));
  6168. break;
  6169. case SP_VIT:
  6170. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd));
  6171. break;
  6172. case SP_INT:
  6173. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd));
  6174. break;
  6175. case SP_DEX:
  6176. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd));
  6177. break;
  6178. case SP_LUK:
  6179. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd));
  6180. break;
  6181. case SP_KARMA:
  6182. sd->status.karma = val;
  6183. break;
  6184. case SP_MANNER:
  6185. sd->status.manner = val;
  6186. break;
  6187. case SP_FAME:
  6188. sd->status.fame = val;
  6189. break;
  6190. case SP_KILLERRID:
  6191. sd->killerrid = val;
  6192. return 1;
  6193. case SP_KILLEDRID:
  6194. sd->killedrid = val;
  6195. return 1;
  6196. default:
  6197. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  6198. return 0;
  6199. }
  6200. clif_updatestatus(sd,type);
  6201. return 1;
  6202. }
  6203. /*==========================================
  6204. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  6205. *------------------------------------------*/
  6206. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  6207. {
  6208. if (type) {
  6209. if (hp)
  6210. clif_heal(sd->fd,SP_HP,hp);
  6211. if (sp)
  6212. clif_heal(sd->fd,SP_SP,sp);
  6213. } else {
  6214. if(hp)
  6215. clif_updatestatus(sd,SP_HP);
  6216. if(sp)
  6217. clif_updatestatus(sd,SP_SP);
  6218. }
  6219. return;
  6220. }
  6221. /*==========================================
  6222. * HP/SP Recovery
  6223. * Heal player hp and/or sp linearly.
  6224. * Calculate bonus by status.
  6225. *------------------------------------------*/
  6226. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  6227. {
  6228. int i, bonus;
  6229. if(hp) {
  6230. bonus = 100 + (sd->battle_status.vit<<1)
  6231. + pc_checkskill(sd,SM_RECOVERY)*10
  6232. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6233. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  6234. if (potion_flag > 1)
  6235. bonus += bonus*(potion_flag-1)*50/100;
  6236. //All item bonuses.
  6237. bonus += sd->bonus.itemhealrate2;
  6238. //Item Group bonuses
  6239. bonus += bonus*itemdb_group_bonus(sd, itemid)/100;
  6240. //Individual item bonuses.
  6241. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  6242. {
  6243. if (sd->itemhealrate[i].nameid == itemid) {
  6244. bonus += bonus*sd->itemhealrate[i].rate/100;
  6245. break;
  6246. }
  6247. }
  6248. if(bonus!=100)
  6249. hp = hp * bonus / 100;
  6250. // Recovery Potion
  6251. if( sd->sc.data[SC_INCHEALRATE] )
  6252. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  6253. }
  6254. if(sp) {
  6255. bonus = 100 + (sd->battle_status.int_<<1)
  6256. + pc_checkskill(sd,MG_SRECOVERY)*10
  6257. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6258. if (potion_flag > 1)
  6259. bonus += bonus*(potion_flag-1)*50/100;
  6260. if(bonus != 100)
  6261. sp = sp * bonus / 100;
  6262. }
  6263. if( sd->sc.count ) {
  6264. if ( sd->sc.data[SC_CRITICALWOUND] ) {
  6265. hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6266. sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6267. }
  6268. if ( sd->sc.data[SC_DEATHHURT] ) {
  6269. hp -= hp * 20 / 100;
  6270. sp -= sp * 20 / 100;
  6271. }
  6272. if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
  6273. hp += hp / 10;
  6274. sp += sp / 10;
  6275. }
  6276. #ifdef RENEWAL
  6277. if( sd->sc.data[SC_EXTREMITYFIST2] )
  6278. sp = 0;
  6279. #endif
  6280. }
  6281. return status_heal(&sd->bl, hp, sp, 1);
  6282. }
  6283. /*==========================================
  6284. * HP/SP Recovery
  6285. * Heal player hp nad/or sp by rate
  6286. *------------------------------------------*/
  6287. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  6288. {
  6289. nullpo_ret(sd);
  6290. if(hp > 100) hp = 100;
  6291. else
  6292. if(hp <-100) hp =-100;
  6293. if(sp > 100) sp = 100;
  6294. else
  6295. if(sp <-100) sp =-100;
  6296. if(hp >= 0 && sp >= 0) //Heal
  6297. return status_percent_heal(&sd->bl, hp, sp);
  6298. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  6299. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  6300. //Crossed signs
  6301. if(hp) {
  6302. if(hp > 0)
  6303. status_percent_heal(&sd->bl, hp, 0);
  6304. else
  6305. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  6306. }
  6307. if(sp) {
  6308. if(sp > 0)
  6309. status_percent_heal(&sd->bl, 0, sp);
  6310. else
  6311. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  6312. }
  6313. return 0;
  6314. }
  6315. static int jobchange_killclone(struct block_list *bl, va_list ap)
  6316. {
  6317. struct mob_data *md;
  6318. int flag;
  6319. md = (struct mob_data *)bl;
  6320. nullpo_ret(md);
  6321. flag = va_arg(ap, int);
  6322. if (md->master_id && md->special_state.clone && md->master_id == flag)
  6323. status_kill(&md->bl);
  6324. return 1;
  6325. }
  6326. /*==========================================
  6327. * Called when player changes job
  6328. * Rewrote to make it tidider [Celest]
  6329. *------------------------------------------*/
  6330. int pc_jobchange(struct map_session_data *sd,int job, int upper)
  6331. {
  6332. int i, fame_flag=0;
  6333. int b_class;
  6334. nullpo_ret(sd);
  6335. if (job < 0)
  6336. return 1;
  6337. //Normalize job.
  6338. b_class = pc_jobid2mapid(job);
  6339. if (b_class == -1)
  6340. return 1;
  6341. switch (upper) {
  6342. case 1:
  6343. b_class|= JOBL_UPPER;
  6344. break;
  6345. case 2:
  6346. b_class|= JOBL_BABY;
  6347. break;
  6348. }
  6349. //This will automatically adjust bard/dancer classes to the correct gender
  6350. //That is, if you try to jobchange into dancer, it will turn you to bard.
  6351. job = pc_mapid2jobid(b_class, sd->status.sex);
  6352. if (job == -1)
  6353. return 1;
  6354. if ((unsigned short)b_class == sd->class_)
  6355. return 1; //Nothing to change.
  6356. // changing from 1st to 2nd job
  6357. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (b_class&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  6358. sd->change_level_2nd = sd->status.job_level;
  6359. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  6360. }
  6361. // changing from 2nd to 3rd job
  6362. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  6363. sd->change_level_3rd = sd->status.job_level;
  6364. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  6365. }
  6366. if(sd->cloneskill_id) {
  6367. if( sd->status.skill[sd->cloneskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6368. sd->status.skill[sd->cloneskill_id].id = 0;
  6369. sd->status.skill[sd->cloneskill_id].lv = 0;
  6370. sd->status.skill[sd->cloneskill_id].flag = 0;
  6371. clif_deleteskill(sd,sd->cloneskill_id);
  6372. }
  6373. sd->cloneskill_id = 0;
  6374. pc_setglobalreg(sd, "CLONE_SKILL", 0);
  6375. pc_setglobalreg(sd, "CLONE_SKILL_LV", 0);
  6376. }
  6377. if(sd->reproduceskill_id) {
  6378. if( sd->status.skill[sd->reproduceskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6379. sd->status.skill[sd->reproduceskill_id].id = 0;
  6380. sd->status.skill[sd->reproduceskill_id].lv = 0;
  6381. sd->status.skill[sd->reproduceskill_id].flag = 0;
  6382. clif_deleteskill(sd,sd->reproduceskill_id);
  6383. }
  6384. sd->reproduceskill_id = 0;
  6385. pc_setglobalreg(sd, "REPRODUCE_SKILL",0);
  6386. pc_setglobalreg(sd, "REPRODUCE_SKILL_LV",0);
  6387. }
  6388. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  6389. const int class_ = pc_class2idx(sd->status.class_);
  6390. short id;
  6391. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  6392. //Remove status specific to your current tree skills.
  6393. enum sc_type sc = status_skill2sc(id);
  6394. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  6395. status_change_end(&sd->bl, sc, INVALID_TIMER);
  6396. }
  6397. }
  6398. sd->status.class_ = job;
  6399. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  6400. sd->class_ = (unsigned short)b_class;
  6401. sd->status.job_level=1;
  6402. sd->status.job_exp=0;
  6403. if (sd->status.base_level > pc_maxbaselv(sd)) {
  6404. sd->status.base_level = pc_maxbaselv(sd);
  6405. sd->status.base_exp=0;
  6406. pc_resetstate(sd);
  6407. clif_updatestatus(sd,SP_STATUSPOINT);
  6408. clif_updatestatus(sd,SP_BASELEVEL);
  6409. clif_updatestatus(sd,SP_BASEEXP);
  6410. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6411. }
  6412. clif_updatestatus(sd,SP_JOBLEVEL);
  6413. clif_updatestatus(sd,SP_JOBEXP);
  6414. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6415. for(i=0;i<EQI_MAX;i++) {
  6416. if(sd->equip_index[i] >= 0)
  6417. if(!pc_isequip(sd,sd->equip_index[i]))
  6418. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  6419. }
  6420. //Change look, if disguised, you need to undisguise
  6421. //to correctly calculate new job sprite without
  6422. if (sd->disguise)
  6423. pc_disguise(sd, 0);
  6424. status_set_viewdata(&sd->bl, job);
  6425. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  6426. if(sd->vd.cloth_color)
  6427. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6428. //Update skill tree.
  6429. pc_calc_skilltree(sd);
  6430. clif_skillinfoblock(sd);
  6431. if (sd->ed)
  6432. elemental_delete(sd->ed, 0);
  6433. if (sd->state.vending)
  6434. vending_closevending(sd);
  6435. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  6436. //Remove peco/cart/falcon
  6437. i = sd->sc.option;
  6438. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  6439. i&=~OPTION_RIDING;
  6440. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  6441. i&=~OPTION_FALCON;
  6442. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  6443. i&=~OPTION_DRAGON;
  6444. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  6445. i&=~OPTION_WUGRIDER;
  6446. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  6447. i&=~OPTION_WUG;
  6448. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  6449. i&=~OPTION_MADOGEAR;
  6450. #ifndef NEW_CARTS
  6451. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  6452. i&=~OPTION_CART;
  6453. #else
  6454. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  6455. pc_setcart(sd, 0);
  6456. #endif
  6457. if(i != sd->sc.option)
  6458. pc_setoption(sd, i);
  6459. if(merc_is_hom_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  6460. merc_hom_vaporize(sd, 0);
  6461. if(sd->status.manner < 0)
  6462. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  6463. status_calc_pc(sd,0);
  6464. pc_checkallowskill(sd);
  6465. pc_equiplookall(sd);
  6466. //if you were previously famous, not anymore.
  6467. if (fame_flag) {
  6468. chrif_save(sd,0);
  6469. chrif_buildfamelist();
  6470. } else if (sd->status.fame > 0) {
  6471. //It may be that now they are famous?
  6472. switch (sd->class_&MAPID_UPPERMASK) {
  6473. case MAPID_BLACKSMITH:
  6474. case MAPID_ALCHEMIST:
  6475. case MAPID_TAEKWON:
  6476. chrif_save(sd,0);
  6477. chrif_buildfamelist();
  6478. break;
  6479. }
  6480. }
  6481. return 0;
  6482. }
  6483. /*==========================================
  6484. * Tell client player sd has change equipement
  6485. *------------------------------------------*/
  6486. int pc_equiplookall(struct map_session_data *sd)
  6487. {
  6488. nullpo_ret(sd);
  6489. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  6490. clif_changelook(&sd->bl,LOOK_SHOES,0);
  6491. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  6492. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  6493. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  6494. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  6495. return 0;
  6496. }
  6497. /*==========================================
  6498. * Tell client player sd has change look (hair,equip...)
  6499. *------------------------------------------*/
  6500. int pc_changelook(struct map_session_data *sd,int type,int val)
  6501. {
  6502. nullpo_ret(sd);
  6503. switch(type){
  6504. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  6505. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6506. if (sd->status.hair != val)
  6507. {
  6508. sd->status.hair=val;
  6509. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6510. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6511. GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
  6512. }
  6513. break;
  6514. case LOOK_WEAPON:
  6515. sd->status.weapon=val;
  6516. break;
  6517. case LOOK_HEAD_BOTTOM:
  6518. sd->status.head_bottom=val;
  6519. break;
  6520. case LOOK_HEAD_TOP:
  6521. sd->status.head_top=val;
  6522. break;
  6523. case LOOK_HEAD_MID:
  6524. sd->status.head_mid=val;
  6525. break;
  6526. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  6527. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6528. if (sd->status.hair_color != val)
  6529. {
  6530. sd->status.hair_color=val;
  6531. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6532. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6533. GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
  6534. }
  6535. break;
  6536. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  6537. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6538. sd->status.clothes_color=val;
  6539. break;
  6540. case LOOK_SHIELD:
  6541. sd->status.shield=val;
  6542. break;
  6543. case LOOK_SHOES:
  6544. break;
  6545. case LOOK_ROBE:
  6546. sd->status.robe = val;
  6547. break;
  6548. }
  6549. clif_changelook(&sd->bl,type,val);
  6550. return 0;
  6551. }
  6552. /*==========================================
  6553. * Give an option (type) to player (sd) and display it to client
  6554. *------------------------------------------*/
  6555. int pc_setoption(struct map_session_data *sd,int type)
  6556. {
  6557. int p_type, new_look=0;
  6558. nullpo_ret(sd);
  6559. p_type = sd->sc.option;
  6560. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  6561. sd->sc.option=type;
  6562. clif_changeoption(&sd->bl);
  6563. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6564. { // Mounting
  6565. clif_status_load(&sd->bl,SI_RIDING,1);
  6566. status_calc_pc(sd,0);
  6567. }
  6568. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6569. { // Dismount
  6570. clif_status_load(&sd->bl,SI_RIDING,0);
  6571. status_calc_pc(sd,0);
  6572. }
  6573. #ifndef NEW_CARTS
  6574. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  6575. clif_cartlist(sd);
  6576. clif_updatestatus(sd, SP_CARTINFO);
  6577. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6578. status_calc_pc(sd,0); //Apply speed penalty.
  6579. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  6580. clif_clearcart(sd->fd);
  6581. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6582. status_calc_pc(sd,0); //Remove speed penalty.
  6583. }
  6584. #endif
  6585. if (type&OPTION_MOUNTING && !(p_type&OPTION_MOUNTING) ) {
  6586. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,2,1,0,0);
  6587. status_calc_pc(sd,0);
  6588. } else if (!(type&OPTION_MOUNTING) && p_type&OPTION_MOUNTING) {
  6589. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,0,0,0,0);
  6590. status_calc_pc(sd,0);
  6591. }
  6592. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  6593. clif_status_load(&sd->bl,SI_FALCON,1);
  6594. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  6595. clif_status_load(&sd->bl,SI_FALCON,0);
  6596. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  6597. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  6598. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  6599. status_calc_pc(sd,0);
  6600. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  6601. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  6602. status_calc_pc(sd,0);
  6603. }
  6604. }
  6605. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  6606. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  6607. status_calc_pc(sd, 0);
  6608. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  6609. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  6610. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  6611. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  6612. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  6613. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  6614. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  6615. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  6616. status_calc_pc(sd, 0);
  6617. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  6618. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  6619. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  6620. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  6621. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  6622. }
  6623. }
  6624. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  6625. new_look = JOB_STAR_GLADIATOR2;
  6626. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  6627. new_look = -1;
  6628. if (type&OPTION_WEDDING && !(p_type&OPTION_WEDDING))
  6629. new_look = JOB_WEDDING;
  6630. else if (!(type&OPTION_WEDDING) && p_type&OPTION_WEDDING)
  6631. new_look = -1;
  6632. if (type&OPTION_XMAS && !(p_type&OPTION_XMAS))
  6633. new_look = JOB_XMAS;
  6634. else if (!(type&OPTION_XMAS) && p_type&OPTION_XMAS)
  6635. new_look = -1;
  6636. if (type&OPTION_SUMMER && !(p_type&OPTION_SUMMER))
  6637. new_look = JOB_SUMMER;
  6638. else if (!(type&OPTION_SUMMER) && p_type&OPTION_SUMMER)
  6639. new_look = -1;
  6640. if (sd->disguise || !new_look)
  6641. return 0; //Disguises break sprite changes
  6642. if (new_look < 0) { //Restore normal look.
  6643. status_set_viewdata(&sd->bl, sd->status.class_);
  6644. new_look = sd->vd.class_;
  6645. }
  6646. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  6647. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  6648. if (sd->vd.cloth_color)
  6649. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6650. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  6651. return 0;
  6652. }
  6653. /*==========================================
  6654. * Give player a cart
  6655. *------------------------------------------*/
  6656. int pc_setcart(struct map_session_data *sd,int type) {
  6657. #ifndef NEW_CARTS
  6658. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  6659. int option;
  6660. #endif
  6661. nullpo_ret(sd);
  6662. if( type < 0 || type > MAX_CARTS )
  6663. return 1;// Never trust the values sent by the client! [Skotlex]
  6664. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  6665. return 1;// Push cart is required
  6666. if( type == 0 && pc_iscarton(sd) )
  6667. status_change_end(&sd->bl,SC_GN_CARTBOOST,INVALID_TIMER);
  6668. #ifdef NEW_CARTS
  6669. switch( type ) {
  6670. case 0:
  6671. if( !sd->sc.data[SC_PUSH_CART] )
  6672. return 0;
  6673. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  6674. clif_clearcart(sd->fd);
  6675. break;
  6676. default:/* everything else is an allowed ID so we can move on */
  6677. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  6678. clif_cartlist(sd);
  6679. clif_updatestatus(sd, SP_CARTINFO);
  6680. sc_start(&sd->bl, SC_PUSH_CART, 100, type, 0);
  6681. clif_status_load_notick(&sd->bl, SI_ON_PUSH_CART, 2 , type, 0, 0);
  6682. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  6683. sd->sc.data[SC_PUSH_CART]->val1 = type;
  6684. break;
  6685. }
  6686. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6687. status_calc_pc(sd,0); //Recalc speed penalty.
  6688. #else
  6689. // Update option
  6690. option = sd->sc.option;
  6691. option &= ~OPTION_CART;// clear cart bits
  6692. option |= cart[type]; // set cart
  6693. pc_setoption(sd, option);
  6694. #endif
  6695. return 0;
  6696. }
  6697. /*==========================================
  6698. * Give player a falcon
  6699. *------------------------------------------*/
  6700. int pc_setfalcon(TBL_PC* sd, int flag)
  6701. {
  6702. if( flag ){
  6703. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  6704. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  6705. } else if( pc_isfalcon(sd) ){
  6706. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  6707. }
  6708. return 0;
  6709. }
  6710. /*==========================================
  6711. * Set player riding
  6712. *------------------------------------------*/
  6713. int pc_setriding(TBL_PC* sd, int flag)
  6714. {
  6715. if( flag ){
  6716. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  6717. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  6718. } else if( pc_isriding(sd) ){
  6719. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  6720. }
  6721. return 0;
  6722. }
  6723. /*==========================================
  6724. * Give player a mado
  6725. *------------------------------------------*/
  6726. int pc_setmadogear(TBL_PC* sd, int flag)
  6727. {
  6728. if( flag ){
  6729. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  6730. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  6731. } else if( pc_ismadogear(sd) ){
  6732. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  6733. }
  6734. return 0;
  6735. }
  6736. /*==========================================
  6737. * Check if player can drop an item
  6738. *------------------------------------------*/
  6739. int pc_candrop(struct map_session_data *sd, struct item *item)
  6740. {
  6741. if( item && item->expire_time )
  6742. return 0;
  6743. if( !pc_can_give_items(sd) ) //check if this GM level can drop items
  6744. return 0;
  6745. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  6746. }
  6747. /*==========================================
  6748. * Read ram register for player sd
  6749. * get val (int) from reg for player sd
  6750. *------------------------------------------*/
  6751. int pc_readreg(struct map_session_data* sd, int reg)
  6752. {
  6753. int i;
  6754. nullpo_ret(sd);
  6755. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  6756. return ( i < sd->reg_num ) ? sd->reg[i].data : 0;
  6757. }
  6758. /*==========================================
  6759. * Set ram register for player sd
  6760. * memo val(int) at reg for player sd
  6761. *------------------------------------------*/
  6762. int pc_setreg(struct map_session_data* sd, int reg, int val)
  6763. {
  6764. int i;
  6765. nullpo_ret(sd);
  6766. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  6767. if( i < sd->reg_num )
  6768. {// overwrite existing entry
  6769. sd->reg[i].data = val;
  6770. return 1;
  6771. }
  6772. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].data == 0 );
  6773. if( i == sd->reg_num )
  6774. {// nothing free, increase size
  6775. sd->reg_num++;
  6776. RECREATE(sd->reg, struct script_reg, sd->reg_num);
  6777. }
  6778. sd->reg[i].index = reg;
  6779. sd->reg[i].data = val;
  6780. return 1;
  6781. }
  6782. /*==========================================
  6783. * Read ram register for player sd
  6784. * get val (str) from reg for player sd
  6785. *------------------------------------------*/
  6786. char* pc_readregstr(struct map_session_data* sd, int reg)
  6787. {
  6788. int i;
  6789. nullpo_ret(sd);
  6790. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  6791. return ( i < sd->regstr_num ) ? sd->regstr[i].data : NULL;
  6792. }
  6793. /*==========================================
  6794. * Set ram register for player sd
  6795. * memo val(str) at reg for player sd
  6796. *------------------------------------------*/
  6797. int pc_setregstr(struct map_session_data* sd, int reg, const char* str)
  6798. {
  6799. int i;
  6800. nullpo_ret(sd);
  6801. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  6802. if( i < sd->regstr_num )
  6803. {// found entry, update
  6804. if( str == NULL || *str == '\0' )
  6805. {// empty string
  6806. if( sd->regstr[i].data != NULL )
  6807. aFree(sd->regstr[i].data);
  6808. sd->regstr[i].data = NULL;
  6809. }
  6810. else if( sd->regstr[i].data )
  6811. {// recreate
  6812. size_t len = strlen(str)+1;
  6813. RECREATE(sd->regstr[i].data, char, len);
  6814. memcpy(sd->regstr[i].data, str, len*sizeof(char));
  6815. }
  6816. else
  6817. {// create
  6818. sd->regstr[i].data = aStrdup(str);
  6819. }
  6820. return 1;
  6821. }
  6822. if( str == NULL || *str == '\0' )
  6823. return 1;// nothing to add, empty string
  6824. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].data == NULL );
  6825. if( i == sd->regstr_num )
  6826. {// nothing free, increase size
  6827. sd->regstr_num++;
  6828. RECREATE(sd->regstr, struct script_regstr, sd->regstr_num);
  6829. }
  6830. sd->regstr[i].index = reg;
  6831. sd->regstr[i].data = aStrdup(str);
  6832. return 1;
  6833. }
  6834. int pc_readregistry(struct map_session_data *sd,const char *reg,int type)
  6835. {
  6836. struct global_reg *sd_reg;
  6837. int i,max;
  6838. nullpo_ret(sd);
  6839. switch (type) {
  6840. case 3: //Char reg
  6841. sd_reg = sd->save_reg.global;
  6842. max = sd->save_reg.global_num;
  6843. break;
  6844. case 2: //Account reg
  6845. sd_reg = sd->save_reg.account;
  6846. max = sd->save_reg.account_num;
  6847. break;
  6848. case 1: //Account2 reg
  6849. sd_reg = sd->save_reg.account2;
  6850. max = sd->save_reg.account2_num;
  6851. break;
  6852. default:
  6853. return 0;
  6854. }
  6855. if (max == -1) {
  6856. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  6857. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  6858. intif_request_registry(sd,type==3?4:type);
  6859. return 0;
  6860. }
  6861. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  6862. return ( i < max ) ? atoi(sd_reg[i].value) : 0;
  6863. }
  6864. char* pc_readregistry_str(struct map_session_data *sd,const char *reg,int type)
  6865. {
  6866. struct global_reg *sd_reg;
  6867. int i,max;
  6868. nullpo_ret(sd);
  6869. switch (type) {
  6870. case 3: //Char reg
  6871. sd_reg = sd->save_reg.global;
  6872. max = sd->save_reg.global_num;
  6873. break;
  6874. case 2: //Account reg
  6875. sd_reg = sd->save_reg.account;
  6876. max = sd->save_reg.account_num;
  6877. break;
  6878. case 1: //Account2 reg
  6879. sd_reg = sd->save_reg.account2;
  6880. max = sd->save_reg.account2_num;
  6881. break;
  6882. default:
  6883. return NULL;
  6884. }
  6885. if (max == -1) {
  6886. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  6887. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  6888. intif_request_registry(sd,type==3?4:type);
  6889. return NULL;
  6890. }
  6891. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  6892. return ( i < max ) ? sd_reg[i].value : NULL;
  6893. }
  6894. int pc_setregistry(struct map_session_data *sd,const char *reg,int val,int type)
  6895. {
  6896. struct global_reg *sd_reg;
  6897. int i,*max, regmax;
  6898. nullpo_ret(sd);
  6899. switch( type )
  6900. {
  6901. case 3: //Char reg
  6902. if( !strcmp(reg,"PC_DIE_COUNTER") && sd->die_counter != val )
  6903. {
  6904. i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  6905. sd->die_counter = val;
  6906. if( i )
  6907. status_calc_pc(sd,0); // Lost the bonus.
  6908. }
  6909. else if( !strcmp(reg,"COOK_MASTERY") && sd->cook_mastery != val )
  6910. {
  6911. val = cap_value(val, 0, 1999);
  6912. sd->cook_mastery = val;
  6913. }
  6914. sd_reg = sd->save_reg.global;
  6915. max = &sd->save_reg.global_num;
  6916. regmax = GLOBAL_REG_NUM;
  6917. break;
  6918. case 2: //Account reg
  6919. if( !strcmp(reg,"#CASHPOINTS") && sd->cashPoints != val )
  6920. {
  6921. val = cap_value(val, 0, MAX_ZENY);
  6922. sd->cashPoints = val;
  6923. }
  6924. else if( !strcmp(reg,"#KAFRAPOINTS") && sd->kafraPoints != val )
  6925. {
  6926. val = cap_value(val, 0, MAX_ZENY);
  6927. sd->kafraPoints = val;
  6928. }
  6929. sd_reg = sd->save_reg.account;
  6930. max = &sd->save_reg.account_num;
  6931. regmax = ACCOUNT_REG_NUM;
  6932. break;
  6933. case 1: //Account2 reg
  6934. sd_reg = sd->save_reg.account2;
  6935. max = &sd->save_reg.account2_num;
  6936. regmax = ACCOUNT_REG2_NUM;
  6937. break;
  6938. default:
  6939. return 0;
  6940. }
  6941. if (*max == -1) {
  6942. ShowError("pc_setregistry : refusing to set %s (type %d) until vars are received.\n", reg, type);
  6943. return 1;
  6944. }
  6945. // delete reg
  6946. if (val == 0) {
  6947. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  6948. if( i < *max )
  6949. {
  6950. if (i != *max - 1)
  6951. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  6952. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  6953. (*max)--;
  6954. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  6955. }
  6956. return 1;
  6957. }
  6958. // change value if found
  6959. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  6960. if( i < *max )
  6961. {
  6962. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  6963. sd->state.reg_dirty |= 1<<(type-1);
  6964. return 1;
  6965. }
  6966. // add value if not found
  6967. if (i < regmax) {
  6968. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  6969. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  6970. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  6971. (*max)++;
  6972. sd->state.reg_dirty |= 1<<(type-1);
  6973. return 1;
  6974. }
  6975. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  6976. return 0;
  6977. }
  6978. int pc_setregistry_str(struct map_session_data *sd,const char *reg,const char *val,int type)
  6979. {
  6980. struct global_reg *sd_reg;
  6981. int i,*max, regmax;
  6982. nullpo_ret(sd);
  6983. if (reg[strlen(reg)-1] != '$') {
  6984. ShowError("pc_setregistry_str : reg %s must be string (end in '$') to use this!\n", reg);
  6985. return 0;
  6986. }
  6987. switch (type) {
  6988. case 3: //Char reg
  6989. sd_reg = sd->save_reg.global;
  6990. max = &sd->save_reg.global_num;
  6991. regmax = GLOBAL_REG_NUM;
  6992. break;
  6993. case 2: //Account reg
  6994. sd_reg = sd->save_reg.account;
  6995. max = &sd->save_reg.account_num;
  6996. regmax = ACCOUNT_REG_NUM;
  6997. break;
  6998. case 1: //Account2 reg
  6999. sd_reg = sd->save_reg.account2;
  7000. max = &sd->save_reg.account2_num;
  7001. regmax = ACCOUNT_REG2_NUM;
  7002. break;
  7003. default:
  7004. return 0;
  7005. }
  7006. if (*max == -1) {
  7007. ShowError("pc_setregistry_str : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7008. return 0;
  7009. }
  7010. // delete reg
  7011. if (!val || strcmp(val,"")==0)
  7012. {
  7013. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7014. if( i < *max )
  7015. {
  7016. if (i != *max - 1)
  7017. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7018. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7019. (*max)--;
  7020. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7021. if (type!=3) intif_saveregistry(sd,type);
  7022. }
  7023. return 1;
  7024. }
  7025. // change value if found
  7026. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7027. if( i < *max )
  7028. {
  7029. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7030. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7031. if (type!=3) intif_saveregistry(sd,type);
  7032. return 1;
  7033. }
  7034. // add value if not found
  7035. if (i < regmax) {
  7036. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7037. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7038. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7039. (*max)++;
  7040. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7041. if (type!=3) intif_saveregistry(sd,type);
  7042. return 1;
  7043. }
  7044. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7045. return 0;
  7046. }
  7047. /*==========================================
  7048. * Exec eventtimer for player sd (retrieved from map_session (id))
  7049. *------------------------------------------*/
  7050. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  7051. {
  7052. struct map_session_data *sd=map_id2sd(id);
  7053. char *p = (char *)data;
  7054. int i;
  7055. if(sd==NULL)
  7056. return 0;
  7057. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  7058. if( i < MAX_EVENTTIMER )
  7059. {
  7060. sd->eventtimer[i] = INVALID_TIMER;
  7061. sd->eventcount--;
  7062. npc_event(sd,p,0);
  7063. }
  7064. else
  7065. ShowError("pc_eventtimer: no such event timer\n");
  7066. if (p) aFree(p);
  7067. return 0;
  7068. }
  7069. /*==========================================
  7070. * Add eventtimer for player sd ?
  7071. *------------------------------------------*/
  7072. int pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  7073. {
  7074. int i;
  7075. nullpo_ret(sd);
  7076. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  7077. if( i == MAX_EVENTTIMER )
  7078. return 0;
  7079. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  7080. sd->eventcount++;
  7081. return 1;
  7082. }
  7083. /*==========================================
  7084. * Del eventtimer for player sd ?
  7085. *------------------------------------------*/
  7086. int pc_deleventtimer(struct map_session_data *sd,const char *name)
  7087. {
  7088. char* p = NULL;
  7089. int i;
  7090. nullpo_ret(sd);
  7091. if (sd->eventcount <= 0)
  7092. return 0;
  7093. // find the named event timer
  7094. ARR_FIND( 0, MAX_EVENTTIMER, i,
  7095. sd->eventtimer[i] != INVALID_TIMER &&
  7096. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  7097. strcmp(p, name) == 0
  7098. );
  7099. if( i == MAX_EVENTTIMER )
  7100. return 0; // not found
  7101. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7102. sd->eventtimer[i] = INVALID_TIMER;
  7103. sd->eventcount--;
  7104. aFree(p);
  7105. return 1;
  7106. }
  7107. /*==========================================
  7108. * Update eventtimer count for player sd
  7109. *------------------------------------------*/
  7110. int pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  7111. {
  7112. int i;
  7113. nullpo_ret(sd);
  7114. for(i=0;i<MAX_EVENTTIMER;i++)
  7115. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  7116. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  7117. addtick_timer(sd->eventtimer[i],tick);
  7118. break;
  7119. }
  7120. return 0;
  7121. }
  7122. /*==========================================
  7123. * Remove all eventtimer for player sd
  7124. *------------------------------------------*/
  7125. int pc_cleareventtimer(struct map_session_data *sd)
  7126. {
  7127. int i;
  7128. nullpo_ret(sd);
  7129. if (sd->eventcount <= 0)
  7130. return 0;
  7131. for(i=0;i<MAX_EVENTTIMER;i++)
  7132. if( sd->eventtimer[i] != INVALID_TIMER ){
  7133. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  7134. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7135. sd->eventtimer[i] = INVALID_TIMER;
  7136. sd->eventcount--;
  7137. if (p) aFree(p);
  7138. }
  7139. return 0;
  7140. }
  7141. /* called when a item with combo is worn */
  7142. int pc_checkcombo(struct map_session_data *sd, struct item_data *data ) {
  7143. int i, j, k, z;
  7144. int index, idx, success = 0;
  7145. for( i = 0; i < data->combos_count; i++ ) {
  7146. /* ensure this isn't a duplicate combo */
  7147. if( sd->combos.bonus != NULL ) {
  7148. int x;
  7149. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7150. /* found a match, skip this combo */
  7151. if( x < sd->combos.count )
  7152. continue;
  7153. }
  7154. for( j = 0; j < data->combos[i]->count; j++ ) {
  7155. int id = data->combos[i]->nameid[j];
  7156. bool found = false;
  7157. for( k = 0; k < EQI_MAX; k++ ) {
  7158. index = sd->equip_index[k];
  7159. if( index < 0 ) continue;
  7160. if( k == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  7161. if( k == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  7162. if( k == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  7163. if(!sd->inventory_data[index])
  7164. continue;
  7165. if ( itemdb_type(id) != IT_CARD ) {
  7166. if ( sd->inventory_data[index]->nameid != id )
  7167. continue;
  7168. found = true;
  7169. break;
  7170. } else { //Cards
  7171. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  7172. continue;
  7173. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  7174. if (sd->status.inventory[index].card[z] != id)
  7175. continue;
  7176. // We have found a match
  7177. found = true;
  7178. break;
  7179. }
  7180. }
  7181. }
  7182. if( !found )
  7183. break;/* we haven't found all the ids for this combo, so we can return */
  7184. }
  7185. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  7186. if( j < data->combos[i]->count )
  7187. continue;
  7188. /* we got here, means all items in the combo are matching */
  7189. idx = sd->combos.count;
  7190. if( sd->combos.bonus == NULL ) {
  7191. CREATE(sd->combos.bonus, struct script_code *, 1);
  7192. CREATE(sd->combos.id, unsigned short, 1);
  7193. sd->combos.count = 1;
  7194. } else {
  7195. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  7196. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  7197. }
  7198. /* we simply copy the pointer */
  7199. sd->combos.bonus[idx] = data->combos[i]->script;
  7200. /* save this combo's id */
  7201. sd->combos.id[idx] = data->combos[i]->id;
  7202. success++;
  7203. }
  7204. return success;
  7205. }
  7206. /* called when a item with combo is removed */
  7207. int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  7208. int i, retval = 0;
  7209. if( sd->combos.bonus == NULL )
  7210. return 0;/* nothing to do here, player has no combos */
  7211. for( i = 0; i < data->combos_count; i++ ) {
  7212. /* check if this combo exists in this user */
  7213. int x = 0, cursor = 0, j;
  7214. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7215. /* no match, skip this combo */
  7216. if( !(x < sd->combos.count) )
  7217. continue;
  7218. sd->combos.bonus[x] = NULL;
  7219. sd->combos.id[x] = 0;
  7220. retval++;
  7221. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  7222. if( sd->combos.bonus[j] == NULL )
  7223. continue;
  7224. if( cursor != j ) {
  7225. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  7226. sd->combos.id[cursor] = sd->combos.id[j];
  7227. }
  7228. cursor++;
  7229. }
  7230. /* it's empty, we can clear all the memory */
  7231. if( (sd->combos.count = cursor) == 0 ) {
  7232. aFree(sd->combos.bonus);
  7233. aFree(sd->combos.id);
  7234. sd->combos.bonus = NULL;
  7235. sd->combos.id = NULL;
  7236. return retval; /* we also can return at this point for we have no more combos to check */
  7237. }
  7238. }
  7239. return retval;
  7240. }
  7241. int pc_load_combo(struct map_session_data *sd) {
  7242. int i, ret = 0;
  7243. for( i = 0; i < EQI_MAX; i++ ) {
  7244. struct item_data *id = NULL;
  7245. int idx = sd->equip_index[i];
  7246. if( sd->equip_index[i] < 0 || !(id = sd->inventory_data[idx] ) )
  7247. continue;
  7248. if( id->combos_count )
  7249. ret += pc_checkcombo(sd,id);
  7250. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  7251. struct item_data *data;
  7252. int j;
  7253. for( j = 0; j < id->slot; j++ ) {
  7254. if (!sd->status.inventory[idx].card[j])
  7255. continue;
  7256. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  7257. if( data->combos_count )
  7258. ret += pc_checkcombo(sd,data);
  7259. }
  7260. }
  7261. }
  7262. }
  7263. return ret;
  7264. }
  7265. /*==========================================
  7266. * Equip item on player sd at req_pos from inventory index n
  7267. *------------------------------------------*/
  7268. int pc_equipitem(struct map_session_data *sd,int n,int req_pos)
  7269. {
  7270. int i,pos,flag=0;
  7271. struct item_data *id;
  7272. nullpo_ret(sd);
  7273. if( n < 0 || n >= MAX_INVENTORY ) {
  7274. clif_equipitemack(sd,0,0,0);
  7275. return 0;
  7276. }
  7277. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 )
  7278. {
  7279. clif_equipitemack(sd,n,0,0);
  7280. return 0;
  7281. }
  7282. id = sd->inventory_data[n];
  7283. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  7284. if(battle_config.battle_log)
  7285. ShowInfo("equip %d(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  7286. if(!pc_isequip(sd,n) || !(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  7287. // FIXME: pc_isequip: equip level failure uses 2 instead of 0
  7288. clif_equipitemack(sd,n,0,0); // fail
  7289. return 0;
  7290. }
  7291. if (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] || sd->sc.data[SC__BLOODYLUST])
  7292. {
  7293. clif_equipitemack(sd,n,0,0); // fail
  7294. return 0;
  7295. }
  7296. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  7297. pos = req_pos&EQP_ACC;
  7298. if (pos == EQP_ACC) //User specified both slots..
  7299. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  7300. }
  7301. if(pos == EQP_ARMS && id->equip == EQP_HAND_R)
  7302. { //Dual wield capable weapon.
  7303. pos = (req_pos&EQP_ARMS);
  7304. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  7305. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  7306. }
  7307. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC)
  7308. { //Update skill-block range database when weapon range changes. [Skotlex]
  7309. i = sd->equip_index[EQI_HAND_R];
  7310. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  7311. flag = 1;
  7312. else
  7313. flag = id->range != sd->inventory_data[i]->range;
  7314. }
  7315. for(i=0;i<EQI_MAX;i++) {
  7316. if(pos & equip_pos[i]) {
  7317. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  7318. pc_unequipitem(sd,sd->equip_index[i],2);
  7319. sd->equip_index[i] = n;
  7320. }
  7321. }
  7322. if(pos==EQP_AMMO){
  7323. clif_arrowequip(sd,n);
  7324. clif_arrow_fail(sd,3);
  7325. }
  7326. else
  7327. clif_equipitemack(sd,n,pos,1);
  7328. sd->status.inventory[n].equip=pos;
  7329. if(pos & EQP_HAND_R) {
  7330. if(id)
  7331. sd->weapontype1 = id->look;
  7332. else
  7333. sd->weapontype1 = 0;
  7334. pc_calcweapontype(sd);
  7335. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7336. }
  7337. if(pos & EQP_HAND_L) {
  7338. if(id) {
  7339. if(id->type == IT_WEAPON) {
  7340. sd->status.shield = 0;
  7341. sd->weapontype2 = id->look;
  7342. }
  7343. else
  7344. if(id->type == IT_ARMOR) {
  7345. sd->status.shield = id->look;
  7346. sd->weapontype2 = 0;
  7347. }
  7348. }
  7349. else
  7350. sd->status.shield = sd->weapontype2 = 0;
  7351. pc_calcweapontype(sd);
  7352. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7353. }
  7354. //Added check to prevent sending the same look on multiple slots ->
  7355. //causes client to redraw item on top of itself. (suggested by Lupus)
  7356. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  7357. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  7358. sd->status.head_bottom = id->look;
  7359. else
  7360. sd->status.head_bottom = 0;
  7361. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7362. }
  7363. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  7364. if(id)
  7365. sd->status.head_top = id->look;
  7366. else
  7367. sd->status.head_top = 0;
  7368. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7369. }
  7370. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  7371. if(id && !(pos&EQP_HEAD_TOP))
  7372. sd->status.head_mid = id->look;
  7373. else
  7374. sd->status.head_mid = 0;
  7375. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7376. }
  7377. if(pos & EQP_COSTUME_HEAD_TOP) {
  7378. if(id){
  7379. sd->status.head_top = id->look;
  7380. } else
  7381. sd->status.head_top = 0;
  7382. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7383. }
  7384. if(pos & EQP_COSTUME_HEAD_MID) {
  7385. if(id && !(pos&EQP_HEAD_TOP)){
  7386. sd->status.head_mid = id->look;
  7387. } else
  7388. sd->status.head_mid = 0;
  7389. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7390. }
  7391. if(pos & EQP_COSTUME_HEAD_LOW) {
  7392. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  7393. sd->status.head_bottom = id->look;
  7394. } else
  7395. sd->status.head_bottom = 0;
  7396. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7397. }
  7398. if(pos & EQP_SHOES)
  7399. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7400. if( pos&EQP_GARMENT )
  7401. {
  7402. sd->status.robe = id ? id->look : 0;
  7403. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  7404. }
  7405. pc_checkallowskill(sd); //Check if status changes should be halted.
  7406. /* check for combos (MUST be before status_calc_pc) */
  7407. if ( id ) {
  7408. struct item_data *data;
  7409. if( id->combos_count )
  7410. pc_checkcombo(sd,id);
  7411. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7412. ; //No cards
  7413. else {
  7414. for( i = 0; i < id->slot; i++ ) {
  7415. if (!sd->status.inventory[n].card[i])
  7416. continue;
  7417. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7418. if( data->combos_count )
  7419. pc_checkcombo(sd,data);
  7420. }
  7421. }
  7422. }
  7423. }
  7424. status_calc_pc(sd,0);
  7425. if (flag) //Update skill data
  7426. clif_skillinfoblock(sd);
  7427. //OnEquip script [Skotlex]
  7428. if (id) {
  7429. struct item_data *data;
  7430. if (id->equip_script)
  7431. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7432. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7433. ; //No cards
  7434. else {
  7435. for( i = 0; i < id->slot; i++ ) {
  7436. if (!sd->status.inventory[n].card[i])
  7437. continue;
  7438. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7439. if( data->equip_script )
  7440. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7441. }
  7442. }
  7443. }
  7444. }
  7445. return 0;
  7446. }
  7447. /*==========================================
  7448. * Called when attemting to unequip an item from player
  7449. * type:
  7450. * 0 - only unequip
  7451. * 1 - calculate status after unequipping
  7452. * 2 - force unequip
  7453. *------------------------------------------*/
  7454. int pc_unequipitem(struct map_session_data *sd,int n,int flag) {
  7455. int i;
  7456. bool status_cacl = false;
  7457. nullpo_ret(sd);
  7458. if( n < 0 || n >= MAX_INVENTORY ) {
  7459. clif_unequipitemack(sd,0,0,0);
  7460. return 0;
  7461. }
  7462. // if player is berserk then cannot unequip
  7463. if (!(flag & 2) && sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] || sd->sc.data[SC__BLOODYLUST]))
  7464. {
  7465. clif_unequipitemack(sd,n,0,0);
  7466. return 0;
  7467. }
  7468. if( !(flag&2) && sd->sc.count && sd->sc.data[SC_KYOUGAKU] )
  7469. {
  7470. clif_unequipitemack(sd,n,0,0);
  7471. return 0;
  7472. }
  7473. if(battle_config.battle_log)
  7474. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  7475. if(!sd->status.inventory[n].equip){ //Nothing to unequip
  7476. clif_unequipitemack(sd,n,0,0);
  7477. return 0;
  7478. }
  7479. for(i=0;i<EQI_MAX;i++) {
  7480. if(sd->status.inventory[n].equip & equip_pos[i])
  7481. sd->equip_index[i] = -1;
  7482. }
  7483. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  7484. sd->weapontype1 = 0;
  7485. sd->status.weapon = sd->weapontype2;
  7486. pc_calcweapontype(sd);
  7487. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7488. if( !battle_config.dancing_weaponswitch_fix )
  7489. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  7490. }
  7491. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  7492. sd->status.shield = sd->weapontype2 = 0;
  7493. pc_calcweapontype(sd);
  7494. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7495. }
  7496. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  7497. sd->status.head_bottom = 0;
  7498. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7499. }
  7500. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  7501. sd->status.head_top = 0;
  7502. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7503. }
  7504. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  7505. sd->status.head_mid = 0;
  7506. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7507. }
  7508. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  7509. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  7510. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7511. }
  7512. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  7513. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  7514. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7515. }
  7516. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  7517. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  7518. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7519. }
  7520. if(sd->status.inventory[n].equip & EQP_SHOES)
  7521. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7522. if( sd->status.inventory[n].equip&EQP_GARMENT )
  7523. {
  7524. sd->status.robe = 0;
  7525. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  7526. }
  7527. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  7528. if((sd->status.inventory[n].equip & EQP_ARMS) &&
  7529. sd->weapontype1 == 0 && sd->weapontype2 == 0 && (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
  7530. skill_enchant_elemental_end(&sd->bl,-1);
  7531. if(sd->status.inventory[n].equip & EQP_ARMOR) {
  7532. // On Armor Change...
  7533. status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
  7534. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  7535. }
  7536. if( sd->state.autobonus&sd->status.inventory[n].equip )
  7537. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  7538. sd->status.inventory[n].equip=0;
  7539. /* check for combos (MUST be before status_calc_pc) */
  7540. if ( sd->inventory_data[n] ) {
  7541. struct item_data *data;
  7542. if( sd->inventory_data[n]->combos_count ) {
  7543. if( pc_removecombo(sd,sd->inventory_data[n]) )
  7544. status_cacl = true;
  7545. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7546. ; //No cards
  7547. else {
  7548. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7549. if (!sd->status.inventory[n].card[i])
  7550. continue;
  7551. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7552. if( data->combos_count ) {
  7553. if( pc_removecombo(sd,data) )
  7554. status_cacl = true;
  7555. }
  7556. }
  7557. }
  7558. }
  7559. }
  7560. if(flag&1 || status_cacl) {
  7561. pc_checkallowskill(sd);
  7562. status_calc_pc(sd,0);
  7563. }
  7564. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  7565. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  7566. //OnUnEquip script [Skotlex]
  7567. if (sd->inventory_data[n]) {
  7568. struct item_data *data;
  7569. if (sd->inventory_data[n]->unequip_script)
  7570. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7571. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7572. ; //No cards
  7573. else {
  7574. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7575. if (!sd->status.inventory[n].card[i])
  7576. continue;
  7577. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7578. if( data->unequip_script )
  7579. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7580. }
  7581. }
  7582. }
  7583. }
  7584. return 0;
  7585. }
  7586. /*==========================================
  7587. * Checking if player (sd) have unauthorize, invalide item
  7588. * on inventory, cart, equiped for the map (item_noequip)
  7589. *------------------------------------------*/
  7590. int pc_checkitem(struct map_session_data *sd)
  7591. {
  7592. int i,id,calc_flag = 0;
  7593. struct item_data *it=NULL;
  7594. nullpo_ret(sd);
  7595. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  7596. return 0;
  7597. if( battle_config.item_check )
  7598. {// check for invalid(ated) items
  7599. for( i = 0; i < MAX_INVENTORY; i++ )
  7600. {
  7601. id = sd->status.inventory[i].nameid;
  7602. if( id && !itemdb_available(id) )
  7603. {
  7604. 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);
  7605. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  7606. }
  7607. }
  7608. for( i = 0; i < MAX_CART; i++ )
  7609. {
  7610. id = sd->status.cart[i].nameid;
  7611. if( id && !itemdb_available(id) )
  7612. {
  7613. 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);
  7614. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  7615. }
  7616. }
  7617. }
  7618. for( i = 0; i < MAX_INVENTORY; i++)
  7619. {
  7620. it = sd->inventory_data[i];
  7621. if( sd->status.inventory[i].nameid == 0 )
  7622. continue;
  7623. if( !sd->status.inventory[i].equip )
  7624. continue;
  7625. if( sd->status.inventory[i].equip&~pc_equippoint(sd,i) )
  7626. {
  7627. pc_unequipitem(sd, i, 2);
  7628. calc_flag = 1;
  7629. continue;
  7630. }
  7631. if( it )
  7632. { // check for forbiden items.
  7633. int flag =
  7634. (map[sd->bl.m].flag.restricted?(8*map[sd->bl.m].zone):0)
  7635. | (!map_flag_vs(sd->bl.m)?1:0)
  7636. | (map[sd->bl.m].flag.pvp?2:0)
  7637. | (map_flag_gvg(sd->bl.m)?4:0)
  7638. | (map[sd->bl.m].flag.battleground?8:0);
  7639. if( flag && (it->flag.no_equip&flag || !pc_isAllowedCardOn(sd,it->slot,i,flag)) )
  7640. {
  7641. pc_unequipitem(sd, i, 2);
  7642. calc_flag = 1;
  7643. }
  7644. }
  7645. }
  7646. if( calc_flag && sd->state.active )
  7647. {
  7648. pc_checkallowskill(sd);
  7649. status_calc_pc(sd,0);
  7650. }
  7651. return 0;
  7652. }
  7653. /*==========================================
  7654. * Update PVP rank for sd1 in cmp to sd2
  7655. *------------------------------------------*/
  7656. int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  7657. {
  7658. struct map_session_data *sd1,*sd2=NULL;
  7659. sd1=(struct map_session_data *)bl;
  7660. sd2=va_arg(ap,struct map_session_data *);
  7661. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  7662. {// cannot register pvp rank for hidden GMs
  7663. return 0;
  7664. }
  7665. if( sd1->pvp_point > sd2->pvp_point )
  7666. sd2->pvp_rank++;
  7667. return 0;
  7668. }
  7669. /*==========================================
  7670. * Calculate new rank beetween all present players (map_foreachinarea)
  7671. * and display result
  7672. *------------------------------------------*/
  7673. int pc_calc_pvprank(struct map_session_data *sd)
  7674. {
  7675. int old;
  7676. struct map_data *m;
  7677. m=&map[sd->bl.m];
  7678. old=sd->pvp_rank;
  7679. sd->pvp_rank=1;
  7680. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  7681. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  7682. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  7683. return sd->pvp_rank;
  7684. }
  7685. /*==========================================
  7686. * Calculate next sd ranking calculation from config
  7687. *------------------------------------------*/
  7688. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  7689. {
  7690. struct map_session_data *sd=NULL;
  7691. sd=map_id2sd(id);
  7692. if(sd==NULL)
  7693. return 0;
  7694. sd->pvp_timer = INVALID_TIMER;
  7695. if( sd->sc.option&OPTION_INVISIBLE )
  7696. {// do not calculate the pvp rank for a hidden GM
  7697. return 0;
  7698. }
  7699. if( pc_calc_pvprank(sd) > 0 )
  7700. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  7701. return 0;
  7702. }
  7703. /*==========================================
  7704. * Checking if sd is married
  7705. * Return:
  7706. * partner_id = yes
  7707. * 0 = no
  7708. *------------------------------------------*/
  7709. int pc_ismarried(struct map_session_data *sd)
  7710. {
  7711. if(sd == NULL)
  7712. return -1;
  7713. if(sd->status.partner_id > 0)
  7714. return sd->status.partner_id;
  7715. else
  7716. return 0;
  7717. }
  7718. /*==========================================
  7719. * Marry player sd to player dstsd
  7720. * Return:
  7721. * -1 = fail
  7722. * 0 = success
  7723. *------------------------------------------*/
  7724. int pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  7725. {
  7726. if(sd == NULL || dstsd == NULL ||
  7727. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  7728. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  7729. return -1;
  7730. sd->status.partner_id = dstsd->status.char_id;
  7731. dstsd->status.partner_id = sd->status.char_id;
  7732. return 0;
  7733. }
  7734. /*==========================================
  7735. * Divorce sd from its partner
  7736. * Return:
  7737. * -1 = fail
  7738. * 0 = success
  7739. *------------------------------------------*/
  7740. int pc_divorce(struct map_session_data *sd)
  7741. {
  7742. struct map_session_data *p_sd;
  7743. int i;
  7744. if( sd == NULL || !pc_ismarried(sd) )
  7745. return -1;
  7746. if( !sd->status.partner_id )
  7747. return -1; // Char is not married
  7748. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  7749. { // Lets char server do the divorce
  7750. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  7751. return -1; // No char server connected
  7752. return 0;
  7753. }
  7754. // Both players online, lets do the divorce manually
  7755. sd->status.partner_id = 0;
  7756. p_sd->status.partner_id = 0;
  7757. for( i = 0; i < MAX_INVENTORY; i++ )
  7758. {
  7759. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  7760. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  7761. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  7762. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  7763. }
  7764. clif_divorced(sd, p_sd->status.name);
  7765. clif_divorced(p_sd, sd->status.name);
  7766. return 0;
  7767. }
  7768. /*==========================================
  7769. * Get sd partner charid. (Married partner)
  7770. *------------------------------------------*/
  7771. struct map_session_data *pc_get_partner(struct map_session_data *sd)
  7772. {
  7773. if (sd && pc_ismarried(sd))
  7774. // charid2sd returns NULL if not found
  7775. return map_charid2sd(sd->status.partner_id);
  7776. return NULL;
  7777. }
  7778. /*==========================================
  7779. * Get sd father charid. (Need to be baby)
  7780. *------------------------------------------*/
  7781. struct map_session_data *pc_get_father (struct map_session_data *sd)
  7782. {
  7783. if (sd && sd->class_&JOBL_BABY && sd->status.father > 0)
  7784. // charid2sd returns NULL if not found
  7785. return map_charid2sd(sd->status.father);
  7786. return NULL;
  7787. }
  7788. /*==========================================
  7789. * Get sd mother charid. (Need to be baby)
  7790. *------------------------------------------*/
  7791. struct map_session_data *pc_get_mother (struct map_session_data *sd)
  7792. {
  7793. if (sd && sd->class_&JOBL_BABY && sd->status.mother > 0)
  7794. // charid2sd returns NULL if not found
  7795. return map_charid2sd(sd->status.mother);
  7796. return NULL;
  7797. }
  7798. /*==========================================
  7799. * Get sd children charid. (Need to be married)
  7800. *------------------------------------------*/
  7801. struct map_session_data *pc_get_child (struct map_session_data *sd)
  7802. {
  7803. if (sd && pc_ismarried(sd) && sd->status.child > 0)
  7804. // charid2sd returns NULL if not found
  7805. return map_charid2sd(sd->status.child);
  7806. return NULL;
  7807. }
  7808. /*==========================================
  7809. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  7810. *------------------------------------------*/
  7811. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  7812. {
  7813. int hp = 0, sp = 0;
  7814. if( pc_isdead(sd) )
  7815. return;
  7816. if (sd->hp_loss.value) {
  7817. sd->hp_loss.tick += diff_tick;
  7818. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  7819. hp += sd->hp_loss.value;
  7820. sd->hp_loss.tick -= sd->hp_loss.rate;
  7821. }
  7822. if(hp >= sd->battle_status.hp)
  7823. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  7824. }
  7825. if (sd->sp_loss.value) {
  7826. sd->sp_loss.tick += diff_tick;
  7827. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  7828. sp += sd->sp_loss.value;
  7829. sd->sp_loss.tick -= sd->sp_loss.rate;
  7830. }
  7831. }
  7832. if (hp > 0 || sp > 0)
  7833. status_zap(&sd->bl, hp, sp);
  7834. return;
  7835. }
  7836. //Character regen. Flag is used to know which types of regen can take place.
  7837. //&1: HP regen
  7838. //&2: SP regen
  7839. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  7840. {
  7841. int hp = 0, sp = 0;
  7842. if (sd->hp_regen.value) {
  7843. sd->hp_regen.tick += diff_tick;
  7844. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  7845. hp += sd->hp_regen.value;
  7846. sd->hp_regen.tick -= sd->hp_regen.rate;
  7847. }
  7848. }
  7849. if (sd->sp_regen.value) {
  7850. sd->sp_regen.tick += diff_tick;
  7851. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  7852. sp += sd->sp_regen.value;
  7853. sd->sp_regen.tick -= sd->sp_regen.rate;
  7854. }
  7855. }
  7856. if (hp > 0 || sp > 0)
  7857. status_heal(&sd->bl, hp, sp, 0);
  7858. return;
  7859. }
  7860. /*==========================================
  7861. * Memo player sd savepoint. (map,x,y)
  7862. *------------------------------------------*/
  7863. int pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  7864. {
  7865. nullpo_ret(sd);
  7866. sd->status.save_point.map = mapindex;
  7867. sd->status.save_point.x = x;
  7868. sd->status.save_point.y = y;
  7869. return 0;
  7870. }
  7871. /*==========================================
  7872. * Save 1 player data at autosave intervalle
  7873. *------------------------------------------*/
  7874. int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  7875. {
  7876. int interval;
  7877. struct s_mapiterator* iter;
  7878. struct map_session_data* sd;
  7879. static int last_save_id = 0, save_flag = 0;
  7880. if(save_flag == 2) //Someone was saved on last call, normal cycle
  7881. save_flag = 0;
  7882. else
  7883. save_flag = 1; //Noone was saved, so save first found char.
  7884. iter = mapit_getallusers();
  7885. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  7886. {
  7887. if(sd->bl.id == last_save_id && save_flag != 1) {
  7888. save_flag = 1;
  7889. continue;
  7890. }
  7891. if(save_flag != 1) //Not our turn to save yet.
  7892. continue;
  7893. //Save char.
  7894. last_save_id = sd->bl.id;
  7895. save_flag = 2;
  7896. chrif_save(sd,0);
  7897. break;
  7898. }
  7899. mapit_free(iter);
  7900. interval = autosave_interval/(map_usercount()+1);
  7901. if(interval < minsave_interval)
  7902. interval = minsave_interval;
  7903. add_timer(gettick()+interval,pc_autosave,0,0);
  7904. return 0;
  7905. }
  7906. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  7907. {
  7908. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  7909. { //Night/day state does not match.
  7910. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  7911. sd->state.night = night_flag;
  7912. return 1;
  7913. }
  7914. return 0;
  7915. }
  7916. /*================================================
  7917. * timer to do the day [Yor]
  7918. * data: 0 = called by timer, 1 = gmcommand/script
  7919. *------------------------------------------------*/
  7920. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  7921. {
  7922. char tmp_soutput[1024];
  7923. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  7924. return 0;
  7925. if (!night_flag)
  7926. return 0; //Already day.
  7927. night_flag = 0; // 0=day, 1=night [Yor]
  7928. map_foreachpc(pc_daynight_timer_sub);
  7929. strcpy(tmp_soutput, (data == 0) ? msg_txt(502) : msg_txt(60)); // The day has arrived!
  7930. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  7931. return 0;
  7932. }
  7933. /*================================================
  7934. * timer to do the night [Yor]
  7935. * data: 0 = called by timer, 1 = gmcommand/script
  7936. *------------------------------------------------*/
  7937. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  7938. {
  7939. char tmp_soutput[1024];
  7940. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  7941. return 0;
  7942. if (night_flag)
  7943. return 0; //Already nigth.
  7944. night_flag = 1; // 0=day, 1=night [Yor]
  7945. map_foreachpc(pc_daynight_timer_sub);
  7946. strcpy(tmp_soutput, (data == 0) ? msg_txt(503) : msg_txt(59)); // The night has fallen...
  7947. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  7948. return 0;
  7949. }
  7950. void pc_setstand(struct map_session_data *sd){
  7951. nullpo_retv(sd);
  7952. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  7953. clif_status_load(&sd->bl,SI_SIT,0);
  7954. //Reset sitting tick.
  7955. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  7956. sd->state.dead_sit = sd->vd.dead_sit = 0;
  7957. }
  7958. /**
  7959. * Mechanic (MADO GEAR)
  7960. **/
  7961. void pc_overheat(struct map_session_data *sd, int val) {
  7962. int heat = val, skill,
  7963. limit[] = { 10, 20, 28, 46, 66 };
  7964. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  7965. return; // already burning
  7966. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  7967. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  7968. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  7969. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7970. }
  7971. heat = max(0,heat); // Avoid negative HEAT
  7972. if( heat >= limit[skill] )
  7973. sc_start(&sd->bl,SC_OVERHEAT,100,0,1000);
  7974. else
  7975. sc_start(&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  7976. return;
  7977. }
  7978. /**
  7979. * Check if player is autolooting given itemID.
  7980. */
  7981. bool pc_isautolooting(struct map_session_data *sd, int nameid)
  7982. {
  7983. int i;
  7984. if( !sd->state.autolooting )
  7985. return false;
  7986. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  7987. return (i != AUTOLOOTITEM_SIZE);
  7988. }
  7989. /**
  7990. * Checks if player can use @/#command
  7991. * @param sd Player map session data
  7992. * @param command Command name without @/# and params
  7993. * @param type is it atcommand or charcommand
  7994. */
  7995. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  7996. {
  7997. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  7998. }
  7999. /**
  8000. * Checks if commands used by a player should be logged
  8001. * according to their group setting.
  8002. * @param sd Player map session data
  8003. */
  8004. bool pc_should_log_commands(struct map_session_data *sd)
  8005. {
  8006. return pc_group_should_log_commands(pc_get_group_id(sd));
  8007. }
  8008. static int pc_talisman_timer(int tid, unsigned int tick, int id, intptr_t data)
  8009. {
  8010. struct map_session_data *sd;
  8011. int i, type;
  8012. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  8013. return 1;
  8014. ARR_FIND(1, 5, type, sd->talisman[type] > 0);
  8015. if( sd->talisman[type] <= 0 )
  8016. {
  8017. 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);
  8018. sd->talisman[type] = 0;
  8019. return 0;
  8020. }
  8021. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == tid);
  8022. if( i == sd->talisman[type] )
  8023. {
  8024. ShowError("pc_talisman_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  8025. return 0;
  8026. }
  8027. sd->talisman[type]--;
  8028. if( i != sd->talisman[type] )
  8029. memmove(sd->talisman_timer[type]+i, sd->talisman_timer[type]+i+1, (sd->talisman[type]-i)*sizeof(int));
  8030. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8031. clif_talisman(sd, type);
  8032. return 0;
  8033. }
  8034. int pc_add_talisman(struct map_session_data *sd,int interval,int max,int type)
  8035. {
  8036. int tid, i;
  8037. nullpo_ret(sd);
  8038. if(max > 10)
  8039. max = 10;
  8040. if(sd->talisman[type] < 0)
  8041. sd->talisman[type] = 0;
  8042. if( sd->talisman[type] && sd->talisman[type] >= max )
  8043. {
  8044. if(sd->talisman_timer[type][0] != INVALID_TIMER)
  8045. delete_timer(sd->talisman_timer[type][0],pc_talisman_timer);
  8046. sd->talisman[type]--;
  8047. if( sd->talisman[type] != 0 )
  8048. memmove(sd->talisman_timer[type]+0, sd->talisman_timer[type]+1, (sd->talisman[type])*sizeof(int));
  8049. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8050. }
  8051. tid = add_timer(gettick()+interval, pc_talisman_timer, sd->bl.id, 0);
  8052. 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);
  8053. if( i != sd->talisman[type] )
  8054. memmove(sd->talisman_timer[type]+i+1, sd->talisman_timer[type]+i, (sd->talisman[type]-i)*sizeof(int));
  8055. sd->talisman_timer[type][i] = tid;
  8056. sd->talisman[type]++;
  8057. clif_talisman(sd, type);
  8058. return 0;
  8059. }
  8060. int pc_del_talisman(struct map_session_data *sd,int count,int type)
  8061. {
  8062. int i;
  8063. nullpo_ret(sd);
  8064. if( sd->talisman[type] <= 0 ) {
  8065. sd->talisman[type] = 0;
  8066. return 0;
  8067. }
  8068. if( count <= 0 )
  8069. return 0;
  8070. if( count > sd->talisman[type] )
  8071. count = sd->talisman[type];
  8072. sd->talisman[type] -= count;
  8073. if( count > 10 )
  8074. count = 10;
  8075. for(i = 0; i < count; i++) {
  8076. if(sd->talisman_timer[type][i] != INVALID_TIMER) {
  8077. delete_timer(sd->talisman_timer[type][i],pc_talisman_timer);
  8078. sd->talisman_timer[type][i] = INVALID_TIMER;
  8079. }
  8080. }
  8081. for(i = count; i < 10; i++) {
  8082. sd->talisman_timer[type][i-count] = sd->talisman_timer[type][i];
  8083. sd->talisman_timer[type][i] = INVALID_TIMER;
  8084. }
  8085. clif_talisman(sd, type);
  8086. return 0;
  8087. }
  8088. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8089. /*==========================================
  8090. * Renewal EXP/Itemdrop rate modifier base on level penalty
  8091. * 1=exp 2=itemdrop
  8092. *------------------------------------------*/
  8093. int pc_level_penalty_mod(struct map_session_data *sd, struct mob_data *md, int type)
  8094. {
  8095. int diff, rate = 100, i;
  8096. nullpo_ret(sd);
  8097. nullpo_ret(md);
  8098. diff = md->level - sd->status.base_level;
  8099. if( diff < 0 )
  8100. diff = MAX_LEVEL + ( ~diff + 1 );
  8101. for(i=0; i<RC_MAX; i++){
  8102. int tmp;
  8103. if( md->status.race != i ){
  8104. if( md->status.mode&MD_BOSS && i < RC_BOSS )
  8105. i = RC_BOSS;
  8106. else if( i <= RC_BOSS )
  8107. continue;
  8108. }
  8109. if( (tmp=level_penalty[type][i][diff]) > 0 ){
  8110. rate = tmp;
  8111. break;
  8112. }
  8113. }
  8114. return rate;
  8115. }
  8116. #endif
  8117. int pc_split_str(char *str,char **val,int num)
  8118. {
  8119. int i;
  8120. for (i=0; i<num && str; i++){
  8121. val[i] = str;
  8122. str = strchr(str,',');
  8123. if (str && i<num-1) //Do not remove a trailing comma.
  8124. *str++=0;
  8125. }
  8126. return i;
  8127. }
  8128. int pc_split_atoi(char* str, int* val, char sep, int max)
  8129. {
  8130. int i,j;
  8131. for (i=0; i<max; i++) {
  8132. if (!str) break;
  8133. val[i] = atoi(str);
  8134. str = strchr(str,sep);
  8135. if (str)
  8136. *str++=0;
  8137. }
  8138. //Zero up the remaining.
  8139. for(j=i; j < max; j++)
  8140. val[j] = 0;
  8141. return i;
  8142. }
  8143. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  8144. {
  8145. static int warning=0;
  8146. int i,j;
  8147. double f;
  8148. for (i=0; i<max; i++) {
  8149. if (!str) break;
  8150. f = atof(str);
  8151. if (f < 0)
  8152. val[i] = 0;
  8153. else if (f > UINT_MAX) {
  8154. val[i] = UINT_MAX;
  8155. if (!warning) {
  8156. warning = 1;
  8157. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  8158. }
  8159. } else
  8160. val[i] = (unsigned int)f;
  8161. str = strchr(str,sep);
  8162. if (str)
  8163. *str++=0;
  8164. }
  8165. //Zero up the remaining.
  8166. for(j=i; j < max; j++)
  8167. val[j] = 0;
  8168. return i;
  8169. }
  8170. /*==========================================
  8171. * sub DB reading.
  8172. * Function used to read skill_tree.txt
  8173. *------------------------------------------*/
  8174. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  8175. {
  8176. unsigned char joblv = 0, skill_lv;
  8177. uint16 skill_id;
  8178. int idx, class_;
  8179. unsigned int i, offset = 3, skill_idx;
  8180. class_ = atoi(fields[0]);
  8181. skill_id = (uint16)atoi(fields[1]);
  8182. skill_lv = (unsigned char)atoi(fields[2]);
  8183. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  8184. {// job level requirement extra column
  8185. joblv = (unsigned char)atoi(fields[3]);
  8186. offset++;
  8187. }
  8188. if(!pcdb_checkid(class_))
  8189. {
  8190. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  8191. return false;
  8192. }
  8193. idx = pc_class2idx(class_);
  8194. //This is to avoid adding two lines for the same skill. [Skotlex]
  8195. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  8196. if( skill_idx == MAX_SKILL_TREE )
  8197. {
  8198. 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_);
  8199. return false;
  8200. }
  8201. else if(skill_tree[idx][skill_idx].id)
  8202. {
  8203. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  8204. }
  8205. skill_tree[idx][skill_idx].id = skill_id;
  8206. skill_tree[idx][skill_idx].max = skill_lv;
  8207. skill_tree[idx][skill_idx].joblv = joblv;
  8208. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  8209. {
  8210. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  8211. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  8212. }
  8213. return true;
  8214. }
  8215. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8216. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  8217. {
  8218. int type, race, diff;
  8219. type = atoi(fields[0]);
  8220. race = atoi(fields[1]);
  8221. diff = atoi(fields[2]);
  8222. if( type != 1 && type != 2 ){
  8223. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  8224. return false;
  8225. }
  8226. if( race < 0 && race > RC_MAX ){
  8227. ShowWarning("pc_readdb_levelpenalty: Invalid race %d specified.\n", race);
  8228. return false;
  8229. }
  8230. diff = min(diff, MAX_LEVEL);
  8231. if( diff < 0 )
  8232. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  8233. level_penalty[type][race][diff] = atoi(fields[3]);
  8234. return true;
  8235. }
  8236. #endif
  8237. /*==========================================
  8238. * pc DB reading.
  8239. * exp.txt - required experience values
  8240. * skill_tree.txt - skill tree for every class
  8241. * attr_fix.txt - elemental adjustment table
  8242. *------------------------------------------*/
  8243. int pc_readdb(void)
  8244. {
  8245. int i,j,k,tmp=0;
  8246. FILE *fp;
  8247. char line[24000],*p;
  8248. //reset
  8249. memset(exp_table,0,sizeof(exp_table));
  8250. memset(max_level,0,sizeof(max_level));
  8251. sprintf(line, "%s/"DBPATH"exp.txt", db_path);
  8252. fp=fopen(line, "r");
  8253. if(fp==NULL){
  8254. ShowError("can't read %s\n", line);
  8255. return 1;
  8256. }
  8257. while(fgets(line, sizeof(line), fp))
  8258. {
  8259. int jobs[CLASS_COUNT], job_count, job, job_id;
  8260. int type;
  8261. unsigned int ui,maxlv;
  8262. char *split[4];
  8263. if(line[0]=='/' && line[1]=='/')
  8264. continue;
  8265. if (pc_split_str(line,split,4) < 4)
  8266. continue;
  8267. job_count = pc_split_atoi(split[1],jobs,':',CLASS_COUNT);
  8268. if (job_count < 1)
  8269. continue;
  8270. job_id = jobs[0];
  8271. if (!pcdb_checkid(job_id)) {
  8272. ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
  8273. continue;
  8274. }
  8275. type = atoi(split[2]);
  8276. if (type < 0 || type > 1) {
  8277. ShowError("pc_readdb: Invalid type %d (must be 0 for base levels, 1 for job levels).\n", type);
  8278. continue;
  8279. }
  8280. maxlv = atoi(split[0]);
  8281. if (maxlv > MAX_LEVEL) {
  8282. ShowWarning("pc_readdb: Specified max level %u for job %d is beyond server's limit (%u).\n ", maxlv, job_id, MAX_LEVEL);
  8283. maxlv = MAX_LEVEL;
  8284. }
  8285. job = jobs[0] = pc_class2idx(job_id);
  8286. //We send one less and then one more because the last entry in the exp array should hold 0.
  8287. max_level[job][type] = pc_split_atoui(split[3], exp_table[job][type],',',maxlv-1)+1;
  8288. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  8289. //The reasoning behind the -2 is this... if the max level is 5, then the array
  8290. //should look like this:
  8291. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  8292. while ((ui = max_level[job][type]) >= 2 && exp_table[job][type][ui-2] <= 0)
  8293. max_level[job][type]--;
  8294. if (max_level[job][type] < maxlv) {
  8295. ShowWarning("pc_readdb: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlv, job_id, max_level[job][type]);
  8296. ShowInfo("Filling the missing values with the last exp entry.\n");
  8297. //Fill the requested values with the last entry.
  8298. ui = (max_level[job][type] <= 2? 0: max_level[job][type]-2);
  8299. for (; ui+2 < maxlv; ui++)
  8300. exp_table[job][type][ui] = exp_table[job][type][ui-1];
  8301. max_level[job][type] = maxlv;
  8302. }
  8303. // ShowDebug("%s - Class %d: %d\n", type?"Job":"Base", job_id, max_level[job][type]);
  8304. for (i = 1; i < job_count; i++) {
  8305. job_id = jobs[i];
  8306. if (!pcdb_checkid(job_id)) {
  8307. ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
  8308. continue;
  8309. }
  8310. job = pc_class2idx(job_id);
  8311. memcpy(exp_table[job][type], exp_table[jobs[0]][type], sizeof(exp_table[0][0]));
  8312. max_level[job][type] = maxlv;
  8313. // ShowDebug("%s - Class %d: %u\n", type?"Job":"Base", job_id, max_level[job][type]);
  8314. }
  8315. }
  8316. fclose(fp);
  8317. for (i = 0; i < JOB_MAX; i++) {
  8318. if (!pcdb_checkid(i)) continue;
  8319. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER)
  8320. continue; //Classes that do not need exp tables.
  8321. j = pc_class2idx(i);
  8322. if (!max_level[j][0])
  8323. ShowWarning("Class %s (%d) does not has a base exp table.\n", job_name(i), i);
  8324. if (!max_level[j][1])
  8325. ShowWarning("Class %s (%d) does not has a job exp table.\n", job_name(i), i);
  8326. }
  8327. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","exp.txt");
  8328. // Reset and read skilltree
  8329. memset(skill_tree,0,sizeof(skill_tree));
  8330. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree);
  8331. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8332. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty);
  8333. for( k=1; k < 3; k++ ){ // fill in the blanks
  8334. for( j = 0; j < RC_MAX; j++ ){
  8335. tmp = 0;
  8336. for( i = 0; i < MAX_LEVEL*2; i++ ){
  8337. if( i == MAX_LEVEL+1 )
  8338. tmp = level_penalty[k][j][0];// reset
  8339. if( level_penalty[k][j][i] > 0 )
  8340. tmp = level_penalty[k][j][i];
  8341. else
  8342. level_penalty[k][j][i] = tmp;
  8343. }
  8344. }
  8345. }
  8346. #endif
  8347. // Reset then read attr_fix
  8348. for(i=0;i<4;i++)
  8349. for(j=0;j<ELE_MAX;j++)
  8350. for(k=0;k<ELE_MAX;k++)
  8351. attr_fix_table[i][j][k]=100;
  8352. sprintf(line, "%s/"DBPATH"attr_fix.txt", db_path);
  8353. fp=fopen(line,"r");
  8354. if(fp==NULL){
  8355. ShowError("can't read %s\n", line);
  8356. return 1;
  8357. }
  8358. while(fgets(line, sizeof(line), fp))
  8359. {
  8360. char *split[10];
  8361. int lv,n;
  8362. if(line[0]=='/' && line[1]=='/')
  8363. continue;
  8364. for(j=0,p=line;j<3 && p;j++){
  8365. split[j]=p;
  8366. p=strchr(p,',');
  8367. if(p) *p++=0;
  8368. }
  8369. if( j < 2 )
  8370. continue;
  8371. lv=atoi(split[0]);
  8372. n=atoi(split[1]);
  8373. for(i=0;i<n && i<ELE_MAX;){
  8374. if( !fgets(line, sizeof(line), fp) )
  8375. break;
  8376. if(line[0]=='/' && line[1]=='/')
  8377. continue;
  8378. for(j=0,p=line;j<n && j<ELE_MAX && p;j++){
  8379. while(*p==32 && *p>0)
  8380. p++;
  8381. attr_fix_table[lv-1][i][j]=atoi(p);
  8382. if(battle_config.attr_recover == 0 && attr_fix_table[lv-1][i][j] < 0)
  8383. attr_fix_table[lv-1][i][j] = 0;
  8384. p=strchr(p,',');
  8385. if(p) *p++=0;
  8386. }
  8387. i++;
  8388. }
  8389. }
  8390. fclose(fp);
  8391. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","attr_fix.txt");
  8392. // reset then read statspoint
  8393. memset(statp,0,sizeof(statp));
  8394. i=1;
  8395. sprintf(line, "%s/"DBPATH"statpoint.txt", db_path);
  8396. fp=fopen(line,"r");
  8397. if(fp == NULL){
  8398. ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  8399. //return 1;
  8400. } else {
  8401. while(fgets(line, sizeof(line), fp))
  8402. {
  8403. int stat;
  8404. if(line[0]=='/' && line[1]=='/')
  8405. continue;
  8406. if ((stat=strtoul(line,NULL,10))<0)
  8407. stat=0;
  8408. if (i > MAX_LEVEL)
  8409. break;
  8410. statp[i]=stat;
  8411. i++;
  8412. }
  8413. fclose(fp);
  8414. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","statpoint.txt");
  8415. }
  8416. // generate the remaining parts of the db if necessary
  8417. k = battle_config.use_statpoint_table; //save setting
  8418. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  8419. statp[0] = 45; // seed value
  8420. for (; i <= MAX_LEVEL; i++)
  8421. statp[i] = statp[i-1] + pc_gets_status_point(i-1);
  8422. battle_config.use_statpoint_table = k; //restore setting
  8423. return 0;
  8424. }
  8425. // Read MOTD on startup. [Valaris]
  8426. int pc_read_motd(void)
  8427. {
  8428. FILE* fp;
  8429. // clear old MOTD
  8430. memset(motd_text, 0, sizeof(motd_text));
  8431. // read current MOTD
  8432. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  8433. {
  8434. char* buf, * ptr;
  8435. unsigned int lines = 0, entries = 0;
  8436. size_t len;
  8437. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  8438. {
  8439. lines++;
  8440. buf = motd_text[entries];
  8441. if( buf[0] == '/' && buf[1] == '/' )
  8442. {
  8443. continue;
  8444. }
  8445. len = strlen(buf);
  8446. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) )
  8447. {// strip trailing EOL characters
  8448. len--;
  8449. }
  8450. if( len )
  8451. {
  8452. buf[len] = 0;
  8453. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH )
  8454. {// crashes newer clients
  8455. 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);
  8456. }
  8457. }
  8458. else
  8459. {// empty line
  8460. buf[0] = ' ';
  8461. buf[1] = 0;
  8462. }
  8463. entries++;
  8464. }
  8465. fclose(fp);
  8466. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  8467. }
  8468. else
  8469. {
  8470. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  8471. }
  8472. return 0;
  8473. }
  8474. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  8475. int i,cursor = 0;
  8476. struct item_cd* cd = NULL;
  8477. if( load ) {
  8478. if( !(cd = idb_get(itemcd_db, sd->status.char_id)) ) {
  8479. // no skill cooldown is associated with this character
  8480. return;
  8481. }
  8482. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8483. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  8484. sd->item_delay[cursor].tick = cd->tick[i];
  8485. sd->item_delay[cursor].nameid = cd->nameid[i];
  8486. cursor++;
  8487. }
  8488. }
  8489. idb_remove(itemcd_db,sd->status.char_id);
  8490. } else {
  8491. if( !(cd = idb_get(itemcd_db,sd->status.char_id)) ) {
  8492. // create a new skill cooldown object for map storage
  8493. CREATE( cd, struct item_cd, 1 );
  8494. idb_put( itemcd_db, sd->status.char_id, cd );
  8495. }
  8496. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8497. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  8498. cd->tick[cursor] = sd->item_delay[i].tick;
  8499. cd->nameid[cursor] = sd->item_delay[i].nameid;
  8500. cursor++;
  8501. }
  8502. }
  8503. }
  8504. return;
  8505. }
  8506. /*==========================================
  8507. * pc Init/Terminate
  8508. *------------------------------------------*/
  8509. void do_final_pc(void) {
  8510. db_destroy(itemcd_db);
  8511. do_final_pc_groups();
  8512. return;
  8513. }
  8514. int do_init_pc(void) {
  8515. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  8516. pc_readdb();
  8517. pc_read_motd(); // Read MOTD [Valaris]
  8518. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  8519. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  8520. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  8521. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  8522. add_timer_func_list(pc_autosave, "pc_autosave");
  8523. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  8524. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  8525. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  8526. add_timer_func_list(pc_talisman_timer, "pc_talisman_timer");
  8527. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  8528. // 0=day, 1=night [Yor]
  8529. night_flag = battle_config.night_at_start ? 1 : 0;
  8530. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  8531. int day_duration = battle_config.day_duration;
  8532. int night_duration = battle_config.night_duration;
  8533. // add night/day timer [Yor]
  8534. add_timer_func_list(map_day_timer, "map_day_timer");
  8535. add_timer_func_list(map_night_timer, "map_night_timer");
  8536. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  8537. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  8538. }
  8539. do_init_pc_groups();
  8540. return 0;
  8541. }