pc.c 371 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120
  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 "map.h"
  16. #include "battle.h" // battle_config
  17. #include "battleground.h"
  18. #include "channel.h"
  19. #include "chat.h"
  20. #include "chrif.h"
  21. #include "date.h" // is_day_of_*()
  22. #include "duel.h"
  23. #include "intif.h"
  24. #include "homunculus.h"
  25. #include "instance.h"
  26. #include "mercenary.h"
  27. #include "elemental.h"
  28. #include "pet.h" // pet_unlocktarget()
  29. #include "party.h" // party_search()
  30. #include "storage.h"
  31. #include "quest.h"
  32. #include <stdlib.h>
  33. #include <math.h>
  34. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  35. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  36. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  37. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  38. static unsigned int statp[MAX_LEVEL+1];
  39. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  40. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  41. #endif
  42. // h-files are for declarations, not for implementations... [Shinomori]
  43. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  44. // timer for night.day implementation
  45. int day_timer_tid = INVALID_TIMER;
  46. int night_timer_tid = INVALID_TIMER;
  47. struct eri *pc_sc_display_ers = NULL;
  48. struct eri *pc_itemgrouphealrate_ers = NULL;
  49. int pc_expiration_tid = INVALID_TIMER;
  50. struct fame_list smith_fame_list[MAX_FAME_LIST];
  51. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  52. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  53. #define MOTD_LINE_SIZE 128
  54. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  55. /**
  56. * Translation table from athena equip index to aegis bitmask
  57. */
  58. unsigned int equip_bitmask[EQI_MAX] = {
  59. EQP_ACC_L, // EQI_ACC_L
  60. EQP_ACC_R, // EQI_ACC_R
  61. EQP_SHOES, // EQI_SHOES
  62. EQP_GARMENT, // EQI_GARMENT
  63. EQP_HEAD_LOW, // EQI_HEAD_LOW
  64. EQP_HEAD_MID, // EQI_HEAD_MID
  65. EQP_HEAD_TOP, // EQI_HEAD_TOP
  66. EQP_ARMOR, // EQI_ARMOR
  67. EQP_HAND_L, // EQI_HAND_L
  68. EQP_HAND_R, // EQI_HAND_R
  69. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  70. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  71. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  72. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  73. EQP_AMMO, // EQI_AMMO
  74. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  75. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  76. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  77. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  78. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  79. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  80. };
  81. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  82. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  83. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  84. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  85. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  86. };
  87. /**
  88. * Item Cool Down Delay Saving
  89. * Struct item_cd is not a member of struct map_session_data
  90. * to keep cooldowns in memory between player log-ins.
  91. * All cooldowns are reset when server is restarted.
  92. **/
  93. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  94. struct item_cd {
  95. unsigned int tick[MAX_ITEMDELAYS]; //tick
  96. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  97. };
  98. /**
  99. * Converts a class to its array index for CLASS_COUNT defined arrays.
  100. * Note that it does not do a validity check for speed purposes, where parsing
  101. * player input make sure to use a pcdb_checkid first!
  102. * @param class_
  103. * @return Class Index
  104. */
  105. int pc_class2idx(int class_) {
  106. if (class_ >= JOB_NOVICE_HIGH)
  107. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  108. return class_;
  109. }
  110. /**
  111. * Get player's group ID
  112. * @param sd
  113. * @return Group ID
  114. */
  115. inline int pc_get_group_id(struct map_session_data *sd) {
  116. return sd->group_id;
  117. }
  118. /** Get player's group Level
  119. * @param sd
  120. * @return Group Level
  121. */
  122. inline int pc_get_group_level(struct map_session_data *sd) {
  123. return sd->group_level;
  124. }
  125. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  126. {
  127. struct map_session_data *sd;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if(sd->invincible_timer != tid){
  131. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  132. return 0;
  133. }
  134. sd->invincible_timer = INVALID_TIMER;
  135. skill_unit_move(&sd->bl,tick,1);
  136. return 0;
  137. }
  138. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  139. nullpo_retv(sd);
  140. if( sd->invincible_timer != INVALID_TIMER )
  141. delete_timer(sd->invincible_timer,pc_invincible_timer);
  142. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  143. }
  144. void pc_delinvincibletimer(struct map_session_data* sd)
  145. {
  146. nullpo_retv(sd);
  147. if( sd->invincible_timer != INVALID_TIMER )
  148. {
  149. delete_timer(sd->invincible_timer,pc_invincible_timer);
  150. sd->invincible_timer = INVALID_TIMER;
  151. skill_unit_move(&sd->bl,gettick(),1);
  152. }
  153. }
  154. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  155. {
  156. struct map_session_data *sd;
  157. int i;
  158. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  159. return 1;
  160. if( sd->spiritball <= 0 )
  161. {
  162. 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);
  163. sd->spiritball = 0;
  164. return 0;
  165. }
  166. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  167. if( i == sd->spiritball )
  168. {
  169. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  170. return 0;
  171. }
  172. sd->spiritball--;
  173. if( i != sd->spiritball )
  174. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  175. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  176. clif_spiritball(&sd->bl);
  177. return 0;
  178. }
  179. /**
  180. * Adds a spiritball to player for 'interval' ms
  181. * @param sd
  182. * @param interval
  183. * @param max
  184. */
  185. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  186. {
  187. int tid;
  188. uint8 i;
  189. nullpo_retv(sd);
  190. if(max > MAX_SPIRITBALL)
  191. max = MAX_SPIRITBALL;
  192. if(sd->spiritball < 0)
  193. sd->spiritball = 0;
  194. if( sd->spiritball && sd->spiritball >= max )
  195. {
  196. if(sd->spirit_timer[0] != INVALID_TIMER)
  197. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  198. sd->spiritball--;
  199. if( sd->spiritball != 0 )
  200. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  201. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  202. }
  203. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  204. 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);
  205. if( i != sd->spiritball )
  206. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  207. sd->spirit_timer[i] = tid;
  208. sd->spiritball++;
  209. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  210. clif_millenniumshield(&sd->bl,sd->spiritball);
  211. else
  212. clif_spiritball(&sd->bl);
  213. }
  214. /**
  215. * Removes number of spiritball from player
  216. * @param sd
  217. * @param count
  218. * @param type 1 = doesn't give client effect
  219. */
  220. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  221. {
  222. uint8 i;
  223. nullpo_retv(sd);
  224. if(sd->spiritball <= 0) {
  225. sd->spiritball = 0;
  226. return;
  227. }
  228. if(count == 0)
  229. return;
  230. if(count > sd->spiritball)
  231. count = sd->spiritball;
  232. sd->spiritball -= count;
  233. if(count > MAX_SPIRITBALL)
  234. count = MAX_SPIRITBALL;
  235. for(i=0;i<count;i++) {
  236. if(sd->spirit_timer[i] != INVALID_TIMER) {
  237. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  238. sd->spirit_timer[i] = INVALID_TIMER;
  239. }
  240. }
  241. for(i=count;i<MAX_SPIRITBALL;i++) {
  242. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  243. sd->spirit_timer[i] = INVALID_TIMER;
  244. }
  245. if(!type) {
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. }
  252. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  253. int *c, *b_sd;
  254. struct block_list *src;
  255. struct map_session_data *tsd;
  256. struct status_change *sc;
  257. nullpo_ret(bl);
  258. nullpo_ret(tsd = (struct map_session_data*)bl);
  259. nullpo_ret(src = va_arg(ap,struct block_list *));
  260. c = va_arg(ap,int *);
  261. b_sd = va_arg(ap, int *);
  262. if(pc_isdead(tsd))
  263. return 0;
  264. sc = status_get_sc(bl);
  265. if( sc && sc->data[SC_BANDING] )
  266. {
  267. b_sd[(*c)++] = tsd->bl.id;
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  273. int c;
  274. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  275. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  276. int range = skill_get_splash(LG_BANDING,skill_lv);
  277. nullpo_ret(sd);
  278. c = 0;
  279. memset(b_sd, 0, sizeof(b_sd));
  280. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  281. if( c < 1 ) //just recalc status no need to recalc hp
  282. { // No more Royal Guards in Banding found.
  283. struct status_change *sc;
  284. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  285. {
  286. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  287. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  288. }
  289. return 0;
  290. }
  291. //Add yourself
  292. hp = status_get_hp(&sd->bl);
  293. i++;
  294. // Get total HP of all Royal Guards in party.
  295. for( j = 0; j < i; j++ ){
  296. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  297. if( bsd != NULL )
  298. hp += status_get_hp(&bsd->bl);
  299. }
  300. // Set average HP.
  301. hp = hp / i;
  302. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  303. for( j = 0; j < i; j++ )
  304. {
  305. int tmp_hp=0;
  306. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  307. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  308. extra_hp += tmp_hp;
  309. tmp_qty++;
  310. }
  311. }
  312. if( extra_hp > 0 && tmp_qty > 0 )
  313. hp += extra_hp / tmp_qty;
  314. for( j = 0; j < i; j++ ){
  315. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  316. if( bsd != NULL )
  317. {
  318. struct status_change *sc;
  319. status_set_hp(&bsd->bl,hp,0); // Set hp
  320. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  321. {
  322. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  323. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  324. }
  325. }
  326. }
  327. return c;
  328. }
  329. /**
  330. * Increases a player's fame points and displays a notice to him
  331. * @param sd Player
  332. * @param count Fame point
  333. */
  334. void pc_addfame(struct map_session_data *sd,int count)
  335. {
  336. int ranktype=-1;
  337. nullpo_retv(sd);
  338. sd->status.fame += count;
  339. if(sd->status.fame > MAX_FAME)
  340. sd->status.fame = MAX_FAME;
  341. switch(sd->class_&MAPID_UPPERMASK){
  342. case MAPID_BLACKSMITH: ranktype = 0; break;
  343. case MAPID_ALCHEMIST: ranktype = 1; break;
  344. case MAPID_TAEKWON: ranktype = 2; break;
  345. }
  346. clif_update_rankingpoint(sd,ranktype,count);
  347. chrif_updatefamelist(sd);
  348. }
  349. /**
  350. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  351. * @param sd
  352. * @param job Job use enum e_mapid
  353. * @return Rank
  354. */
  355. unsigned char pc_famerank(uint32 char_id, int job)
  356. {
  357. uint8 i;
  358. switch(job){
  359. case MAPID_BLACKSMITH: // Blacksmith
  360. for(i = 0; i < MAX_FAME_LIST; i++){
  361. if(smith_fame_list[i].id == char_id)
  362. return i + 1;
  363. }
  364. break;
  365. case MAPID_ALCHEMIST: // Alchemist
  366. for(i = 0; i < MAX_FAME_LIST; i++){
  367. if(chemist_fame_list[i].id == char_id)
  368. return i + 1;
  369. }
  370. break;
  371. case MAPID_TAEKWON: // Taekwon
  372. for(i = 0; i < MAX_FAME_LIST; i++){
  373. if(taekwon_fame_list[i].id == char_id)
  374. return i + 1;
  375. }
  376. break;
  377. }
  378. return 0;
  379. }
  380. /**
  381. * Restart player's HP & SP value
  382. * @param sd
  383. * @param type Restart type: 1 - Normal Resurection
  384. */
  385. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  386. struct status_data *status, *b_status;
  387. nullpo_retv(sd);
  388. b_status = &sd->base_status;
  389. status = &sd->battle_status;
  390. if (type&1) { //Normal resurrection
  391. status->hp = 1; //Otherwise status_heal may fail if dead.
  392. status_heal(&sd->bl, b_status->hp, 0, 1);
  393. if( status->sp < b_status->sp )
  394. status_set_sp(&sd->bl, b_status->sp, 1);
  395. } else { //Just for saving on the char-server (with values as if respawned)
  396. sd->status.hp = b_status->hp;
  397. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  398. }
  399. }
  400. /*==========================================
  401. Rental System
  402. *------------------------------------------*/
  403. /**
  404. * Ends a rental and removes the item/effect
  405. * @param tid: Tick ID
  406. * @param tick: Timer
  407. * @param id: Timer ID
  408. * @param data: Data
  409. * @return false - failure, true - success
  410. */
  411. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  412. {
  413. struct map_session_data *sd = map_id2sd(id);
  414. if( sd == NULL )
  415. return 0;
  416. if( tid != sd->rental_timer ) {
  417. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  418. return 0;
  419. }
  420. pc_inventory_rentals(sd);
  421. return 1;
  422. }
  423. /**
  424. * Removes the rental timer from the player
  425. * @param sd: Player data
  426. */
  427. void pc_inventory_rental_clear(struct map_session_data *sd)
  428. {
  429. if( sd->rental_timer != INVALID_TIMER ) {
  430. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  431. sd->rental_timer = INVALID_TIMER;
  432. }
  433. }
  434. /**
  435. * Check for items in the player's inventory that are rental type
  436. * @param sd: Player data
  437. */
  438. void pc_inventory_rentals(struct map_session_data *sd)
  439. {
  440. int i, c = 0;
  441. unsigned int expire_tick, next_tick = UINT_MAX;
  442. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  443. if( sd->status.inventory[i].nameid == 0 )
  444. continue; // Nothing here
  445. if( sd->status.inventory[i].expire_time == 0 )
  446. continue;
  447. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  448. if (sd->inventory_data[i]->unequip_script)
  449. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  450. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  451. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  452. } else {
  453. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  454. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  455. next_tick = umin(expire_tick, next_tick);
  456. c++;
  457. }
  458. }
  459. 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
  460. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  461. else
  462. sd->rental_timer = INVALID_TIMER;
  463. }
  464. /**
  465. * Add a rental item to the player and adjusts the rental timer appropriately
  466. * @param sd: Player data
  467. * @param seconds: Rental time
  468. */
  469. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  470. {
  471. unsigned int tick = seconds * 1000;
  472. if( sd == NULL )
  473. return;
  474. if( sd->rental_timer != INVALID_TIMER ) {
  475. const struct TimerData * td;
  476. td = get_timer(sd->rental_timer);
  477. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  478. pc_inventory_rental_clear(sd);
  479. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  480. }
  481. } else
  482. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  483. }
  484. /**
  485. * Determines if player can give / drop / trade / vend items
  486. */
  487. bool pc_can_give_items(struct map_session_data *sd)
  488. {
  489. return pc_has_permission(sd, PC_PERM_TRADE);
  490. }
  491. /**
  492. * Determines if player can give / drop / trade / vend bounded items
  493. */
  494. bool pc_can_give_bounded_items(struct map_session_data *sd)
  495. {
  496. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  497. }
  498. /*==========================================
  499. * Prepares character for saving.
  500. * @param sd
  501. *------------------------------------------*/
  502. void pc_makesavestatus(struct map_session_data *sd) {
  503. nullpo_retv(sd);
  504. if(!battle_config.save_clothcolor)
  505. sd->status.clothes_color = 0;
  506. // Since this is currently not officially released,
  507. // its best to have a forced option to not save body styles.
  508. if(!battle_config.save_body_style)
  509. sd->status.body = 0;
  510. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  511. //status change load/saving. [Skotlex]
  512. #ifdef NEW_CARTS
  513. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  514. #else
  515. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  516. #endif
  517. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  518. if(pc_isdead(sd)){
  519. pc_setrestartvalue(sd, 0);
  520. } else {
  521. sd->status.hp = sd->battle_status.hp;
  522. sd->status.sp = sd->battle_status.sp;
  523. }
  524. sd->status.last_point.map = sd->mapindex;
  525. sd->status.last_point.x = sd->bl.x;
  526. sd->status.last_point.y = sd->bl.y;
  527. return;
  528. }
  529. if(pc_isdead(sd)) {
  530. pc_setrestartvalue(sd, 0);
  531. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  532. } else {
  533. sd->status.hp = sd->battle_status.hp;
  534. sd->status.sp = sd->battle_status.sp;
  535. sd->status.last_point.map = sd->mapindex;
  536. sd->status.last_point.x = sd->bl.x;
  537. sd->status.last_point.y = sd->bl.y;
  538. }
  539. if(map[sd->bl.m].flag.nosave) {
  540. struct map_data *m=&map[sd->bl.m];
  541. if(m->save.map)
  542. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  543. else
  544. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  545. }
  546. }
  547. /*==========================================
  548. * Off init ? Connection?
  549. *------------------------------------------*/
  550. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
  551. nullpo_retv(sd);
  552. sd->bl.id = account_id;
  553. sd->status.account_id = account_id;
  554. sd->status.char_id = char_id;
  555. sd->status.sex = sex;
  556. sd->login_id1 = login_id1;
  557. sd->login_id2 = 0; // at this point, we can not know the value :(
  558. sd->client_tick = client_tick;
  559. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  560. sd->bl.type = BL_PC;
  561. sd->canlog_tick = gettick();
  562. //Required to prevent homunculus copuing a base speed of 0.
  563. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  564. }
  565. /**
  566. * Get equip point for an equip
  567. * @param sd
  568. * @param n Equip index in inventory
  569. */
  570. int pc_equippoint(struct map_session_data *sd,int n){
  571. int ep = 0;
  572. nullpo_ret(sd);
  573. if(!sd->inventory_data[n])
  574. return 0;
  575. if (!itemdb_isequip2(sd->inventory_data[n]))
  576. return 0; //Not equippable by players.
  577. ep = sd->inventory_data[n]->equip;
  578. if(sd->inventory_data[n]->look == W_DAGGER ||
  579. sd->inventory_data[n]->look == W_1HSWORD ||
  580. sd->inventory_data[n]->look == W_1HAXE) {
  581. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  582. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  583. return EQP_ARMS;
  584. }
  585. return ep;
  586. }
  587. /**
  588. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  589. * @param sd : player session
  590. * @return 0 sucess, 1:invalid sd
  591. */
  592. void pc_setinventorydata(struct map_session_data *sd)
  593. {
  594. uint8 i;
  595. nullpo_retv(sd);
  596. for(i = 0; i < MAX_INVENTORY; i++) {
  597. unsigned short id = sd->status.inventory[i].nameid;
  598. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  599. }
  600. }
  601. /**
  602. * 'Calculates' weapon type
  603. * @param sd : player
  604. */
  605. void pc_calcweapontype(struct map_session_data *sd)
  606. {
  607. nullpo_retv(sd);
  608. // single-hand
  609. if(sd->weapontype2 == W_FIST) {
  610. sd->status.weapon = sd->weapontype1;
  611. return;
  612. }
  613. if(sd->weapontype1 == W_FIST) {
  614. sd->status.weapon = sd->weapontype2;
  615. return;
  616. }
  617. // dual-wield
  618. sd->status.weapon = 0;
  619. switch (sd->weapontype1){
  620. case W_DAGGER:
  621. switch (sd->weapontype2) {
  622. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  623. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  624. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  625. }
  626. break;
  627. case W_1HSWORD:
  628. switch (sd->weapontype2) {
  629. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  630. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  631. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  632. }
  633. break;
  634. case W_1HAXE:
  635. switch (sd->weapontype2) {
  636. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  637. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  638. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  639. }
  640. }
  641. // unknown, default to right hand type
  642. if (!sd->status.weapon)
  643. sd->status.weapon = sd->weapontype1;
  644. }
  645. /**
  646. * Set equip index
  647. * @param sd : Player
  648. */
  649. void pc_setequipindex(struct map_session_data *sd)
  650. {
  651. uint16 i;
  652. nullpo_retv(sd);
  653. for (i = 0; i < EQI_MAX; i++)
  654. sd->equip_index[i] = -1;
  655. for (i = 0; i < MAX_INVENTORY; i++) {
  656. if (sd->status.inventory[i].nameid <= 0)
  657. continue;
  658. if (sd->status.inventory[i].equip) {
  659. uint8 j;
  660. for (j = 0; j < EQI_MAX; j++)
  661. if (sd->status.inventory[i].equip & equip_bitmask[j])
  662. sd->equip_index[j] = i;
  663. if (sd->status.inventory[i].equip & EQP_HAND_R) {
  664. if (sd->inventory_data[i])
  665. sd->weapontype1 = sd->inventory_data[i]->look;
  666. else
  667. sd->weapontype1 = 0;
  668. }
  669. if( sd->status.inventory[i].equip & EQP_HAND_L ) {
  670. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  671. sd->weapontype2 = sd->inventory_data[i]->look;
  672. else
  673. sd->weapontype2 = 0;
  674. }
  675. }
  676. }
  677. pc_calcweapontype(sd);
  678. }
  679. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  680. //{
  681. // int i;
  682. // struct item *item = &sd->status.inventory[eqindex];
  683. // struct item_data *data;
  684. //
  685. // //Crafted/made/hatched items.
  686. // if (itemdb_isspecial(item->card[0]))
  687. // return 1;
  688. //
  689. // /* scan for enchant armor gems */
  690. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  691. // s = MAX_SLOTS - 1;
  692. //
  693. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  694. // return( i < s ) ? 0 : 1;
  695. //}
  696. /**
  697. * Check if an item is equiped by player
  698. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  699. * @param sd : player session
  700. * @param nameid : itemid
  701. * @return 1:yes, 0:no
  702. */
  703. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  704. {
  705. uint8 i;
  706. for( i = 0; i < EQI_MAX; i++ )
  707. {
  708. short index = sd->equip_index[i], j;
  709. if( index < 0 )
  710. continue;
  711. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  712. continue;
  713. if( !sd->inventory_data[index] )
  714. continue;
  715. if( sd->inventory_data[index]->nameid == nameid )
  716. return true;
  717. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  718. if( sd->status.inventory[index].card[j] == nameid )
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. /**
  725. * Check adoption rules
  726. * @param p1_sd: Player 1
  727. * @param p2_sd: Player 2
  728. * @param b_sd: Player that will be adopted
  729. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  730. * ADOPT_ALREADY_ADOPTED - Already adopted
  731. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  732. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  733. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  734. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  735. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  736. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  737. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  738. */
  739. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  740. {
  741. if( !p1_sd || !p2_sd || !b_sd )
  742. return ADOPT_CHARACTER_NOT_FOUND;
  743. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  744. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  745. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  746. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  747. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  748. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  749. if( p2_sd->status.party_id != p1_sd->status.party_id )
  750. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  751. // Parents need to have their ring equipped
  752. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  753. return ADOPT_EQUIP_RINGS;
  754. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  755. return ADOPT_EQUIP_RINGS;
  756. // Already adopted a baby
  757. if( p1_sd->status.child || p2_sd->status.child ) {
  758. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  759. return ADOPT_MORE_CHILDREN;
  760. }
  761. // Parents need at least lvl 70 to adopt
  762. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  763. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  764. return ADOPT_LEVEL_70;
  765. }
  766. if( b_sd->status.partner_id ) {
  767. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  768. return ADOPT_MARRIED;
  769. }
  770. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
  771. return ADOPT_NOT_NOVICE;
  772. return ADOPT_ALLOWED;
  773. }
  774. /*==========================================
  775. * Adoption Process
  776. *------------------------------------------*/
  777. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  778. {
  779. int job, joblevel;
  780. unsigned int jobexp;
  781. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  782. return false;
  783. // Preserve current job levels and progress
  784. joblevel = b_sd->status.job_level;
  785. jobexp = b_sd->status.job_exp;
  786. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  787. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  788. { // Success, proceed to configure parents and baby skills
  789. p1_sd->status.child = b_sd->status.char_id;
  790. p2_sd->status.child = b_sd->status.char_id;
  791. b_sd->status.father = p1_sd->status.char_id;
  792. b_sd->status.mother = p2_sd->status.char_id;
  793. // Restore progress
  794. b_sd->status.job_level = joblevel;
  795. clif_updatestatus(b_sd, SP_JOBLEVEL);
  796. b_sd->status.job_exp = jobexp;
  797. clif_updatestatus(b_sd, SP_JOBEXP);
  798. // Baby Skills
  799. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  800. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  801. // Parents Skills
  802. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  803. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  804. return true;
  805. }
  806. return false; // Job Change Fail
  807. }
  808. /*==========================================
  809. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  810. Returns:
  811. false : Cannot use/equip
  812. true : Can use/equip
  813. * Credits:
  814. [Inkfish] for first idea
  815. [Haru] for third-classes extension
  816. [Cydh] finishing :D
  817. *------------------------------------------*/
  818. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  819. while (1) {
  820. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  821. break;
  822. #ifndef RENEWAL
  823. //allow third classes to use trans. class items
  824. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  825. break;
  826. //third-baby classes can use same item too
  827. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  828. break;
  829. //don't need to decide specific rules for third-classes?
  830. //items for third classes can be used for all third classes
  831. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  832. break;
  833. #else
  834. //trans. classes (exl. third-trans.)
  835. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  836. break;
  837. //baby classes (exl. third-baby)
  838. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  839. break;
  840. //third classes (exl. third-trans. and baby-third)
  841. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  842. break;
  843. //trans-third classes
  844. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  845. break;
  846. //third-baby classes
  847. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  848. break;
  849. #endif
  850. return false;
  851. }
  852. return true;
  853. }
  854. /*=================================================
  855. * Checks if the player can equip the item at index n in inventory.
  856. * @param sd
  857. * @param n Item index in inventory
  858. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  859. *------------------------------------------------*/
  860. uint8 pc_isequip(struct map_session_data *sd,int n)
  861. {
  862. struct item_data *item;
  863. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  864. item = sd->inventory_data[n];
  865. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  866. return ITEM_EQUIP_ACK_OK;
  867. if(item == NULL)
  868. return ITEM_EQUIP_ACK_FAIL;
  869. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  870. return ITEM_EQUIP_ACK_FAILLEVEL;
  871. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  872. return ITEM_EQUIP_ACK_FAILLEVEL;
  873. if(item->sex != 2 && sd->status.sex != item->sex)
  874. return ITEM_EQUIP_ACK_FAIL;
  875. if (sd->sc.count) {
  876. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  877. return ITEM_EQUIP_ACK_FAIL;
  878. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  879. return ITEM_EQUIP_ACK_FAIL;
  880. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  881. return ITEM_EQUIP_ACK_FAIL;
  882. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  883. return ITEM_EQUIP_ACK_FAIL;
  884. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  885. return ITEM_EQUIP_ACK_FAIL;
  886. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  887. return ITEM_EQUIP_ACK_FAIL;
  888. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  889. //Spirit of Super Novice equip bonuses. [Skotlex]
  890. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  891. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  892. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  893. switch(item->look) { //In weapons, the look determines type of weapon.
  894. case W_DAGGER: //All level 4 - Daggers
  895. case W_1HSWORD: //All level 4 - 1H Swords
  896. case W_1HAXE: //All level 4 - 1H Axes
  897. case W_MACE: //All level 4 - 1H Maces
  898. case W_STAFF: //All level 4 - 1H Staves
  899. case W_2HSTAFF: //All level 4 - 2H Staves
  900. return ITEM_EQUIP_ACK_OK;
  901. }
  902. }
  903. }
  904. //fail to equip if item is restricted
  905. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  906. return ITEM_EQUIP_ACK_FAIL;
  907. //Not equipable by class. [Skotlex]
  908. if (!(1ULL<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  909. return ITEM_EQUIP_ACK_FAIL;
  910. if (!pc_isItemClass(sd,item))
  911. return ITEM_EQUIP_ACK_FAIL;
  912. return ITEM_EQUIP_ACK_OK;
  913. }
  914. /*==========================================
  915. * No problem with the session id
  916. * set the status that has been sent from char server
  917. *------------------------------------------*/
  918. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  919. {
  920. int i;
  921. #ifdef BOUND_ITEMS
  922. int j;
  923. int idxlist[MAX_INVENTORY];
  924. #endif
  925. unsigned long tick = gettick();
  926. uint32 ip = session[sd->fd]->client_addr;
  927. sd->login_id2 = login_id2;
  928. sd->group_id = group_id;
  929. /* load user permissions */
  930. pc_group_pc_load(sd);
  931. memcpy(&sd->status, st, sizeof(*st));
  932. if (st->sex != sd->status.sex) {
  933. clif_authfail_fd(sd->fd, 0);
  934. return false;
  935. }
  936. //Set the map-server used job id. [Skotlex]
  937. i = pc_jobid2mapid(sd->status.class_);
  938. if (i == -1) { //Invalid class?
  939. 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);
  940. sd->status.class_ = JOB_NOVICE;
  941. sd->class_ = MAPID_NOVICE;
  942. } else
  943. sd->class_ = i;
  944. // Checks and fixes to character status data, that are required
  945. // in case of configuration change or stuff, which cannot be
  946. // checked on char-server.
  947. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  948. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  949. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  950. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  951. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  952. if(!sd->status.hp) pc_setdead(sd);
  953. sd->state.connect_new = 1;
  954. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  955. sd->invincible_timer = INVALID_TIMER;
  956. sd->npc_timer_id = INVALID_TIMER;
  957. sd->pvp_timer = INVALID_TIMER;
  958. sd->expiration_tid = INVALID_TIMER;
  959. sd->autotrade_tid = INVALID_TIMER;
  960. #ifdef SECURE_NPCTIMEOUT
  961. // Initialize to defaults/expected
  962. sd->npc_idle_timer = INVALID_TIMER;
  963. sd->npc_idle_tick = tick;
  964. sd->npc_idle_type = NPCT_INPUT;
  965. sd->state.ignoretimeout = false;
  966. #endif
  967. sd->canuseitem_tick = tick;
  968. sd->canusecashfood_tick = tick;
  969. sd->canequip_tick = tick;
  970. sd->cantalk_tick = tick;
  971. sd->canskill_tick = tick;
  972. sd->cansendmail_tick = tick;
  973. sd->idletime = last_tick;
  974. for(i = 0; i < MAX_SPIRITBALL; i++)
  975. sd->spirit_timer[i] = INVALID_TIMER;
  976. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  977. sd->autobonus[i].active = INVALID_TIMER;
  978. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  979. sd->autobonus2[i].active = INVALID_TIMER;
  980. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  981. sd->autobonus3[i].active = INVALID_TIMER;
  982. if (battle_config.item_auto_get)
  983. sd->state.autoloot = 10000;
  984. if (battle_config.disp_experience)
  985. sd->state.showexp = 1;
  986. if (battle_config.disp_zeny)
  987. sd->state.showzeny = 1;
  988. #ifdef VIP_ENABLE
  989. if (!battle_config.vip_disp_rate)
  990. sd->disableshowrate = 1;
  991. #endif
  992. if (!(battle_config.display_skill_fail&2))
  993. sd->state.showdelay = 1;
  994. pc_setinventorydata(sd);
  995. pc_setequipindex(sd);
  996. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  997. sd->status.option &= ~OPTION_INVISIBLE;
  998. }
  999. status_change_init(&sd->bl);
  1000. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1001. //Set here because we need the inventory data for weapon sprite parsing.
  1002. status_set_viewdata(&sd->bl, sd->status.class_);
  1003. unit_dataset(&sd->bl);
  1004. sd->guild_x = -1;
  1005. sd->guild_y = -1;
  1006. sd->delayed_damage = 0;
  1007. // Event Timers
  1008. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1009. sd->eventtimer[i] = INVALID_TIMER;
  1010. // Rental Timer
  1011. sd->rental_timer = INVALID_TIMER;
  1012. for( i = 0; i < 3; i++ )
  1013. sd->hate_mob[i] = -1;
  1014. sd->quest_log = NULL;
  1015. sd->num_quests = 0;
  1016. sd->avail_quests = 0;
  1017. sd->save_quest = false;
  1018. sd->count_rewarp = 0;
  1019. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1020. sd->regs.arrays = NULL;
  1021. sd->vars_dirty = false;
  1022. sd->vars_ok = false;
  1023. sd->vars_received = 0x0;
  1024. sd->qi_display = NULL;
  1025. sd->qi_count = 0;
  1026. //warp player
  1027. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1028. 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);
  1029. // try warping to a default map instead (church graveyard)
  1030. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1031. // if we fail again
  1032. clif_authfail_fd(sd->fd, 0);
  1033. return false;
  1034. }
  1035. }
  1036. clif_authok(sd);
  1037. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1038. sd->die_counter=-1;
  1039. //display login notice
  1040. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1041. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1042. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1043. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1044. sd->status.name, sd->status.account_id, sd->status.char_id,
  1045. sd->packet_ver, CONVIP(ip), sd->group_id);
  1046. // Send friends list
  1047. clif_friendslist_send(sd);
  1048. if( !changing_mapservers ) {
  1049. if (battle_config.display_version == 1)
  1050. pc_show_version(sd);
  1051. // Message of the Day [Valaris]
  1052. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1053. if (battle_config.motd_type)
  1054. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  1055. else
  1056. clif_displaymessage(sd->fd, motd_text[i]);
  1057. }
  1058. if (expiration_time != 0)
  1059. sd->expiration_time = expiration_time;
  1060. /**
  1061. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1062. **/
  1063. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1064. }
  1065. /**
  1066. * Check if player have any item cooldowns on
  1067. **/
  1068. pc_itemcd_do(sd,true);
  1069. pc_validate_skill(sd);
  1070. #ifdef BOUND_ITEMS
  1071. // Party bound item check
  1072. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,BOUND_PARTY,idxlist))) { // Party was deleted while character offline
  1073. for(i=0;i<j;i++)
  1074. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  1075. }
  1076. #endif
  1077. /* [Ind] */
  1078. sd->sc_display = NULL;
  1079. sd->sc_display_count = 0;
  1080. // Player has not yet received the CashShop list
  1081. sd->status.cashshop_sent = false;
  1082. sd->last_addeditem_index = -1;
  1083. sd->bonus_script.head = NULL;
  1084. sd->bonus_script.count = 0;
  1085. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1086. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1087. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1088. clif_updatestatus(sd, SP_BASEEXP);
  1089. }
  1090. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1091. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1092. clif_updatestatus(sd, SP_JOBEXP);
  1093. }
  1094. // Request all registries (auth is considered completed whence they arrive)
  1095. intif_request_registry(sd,7);
  1096. return true;
  1097. }
  1098. /*==========================================
  1099. * Closes a connection because it failed to be authenticated from the char server.
  1100. *------------------------------------------*/
  1101. void pc_authfail(struct map_session_data *sd)
  1102. {
  1103. clif_authfail_fd(sd->fd, 0);
  1104. return;
  1105. }
  1106. /**
  1107. * Player register a bl as hatred
  1108. * @param sd : player session
  1109. * @param pos : hate position [0;2]
  1110. * @param bl : target bl
  1111. * @return false:failed, true:success
  1112. */
  1113. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1114. {
  1115. int class_;
  1116. if (!sd || !bl || pos < 0 || pos > 2)
  1117. return false;
  1118. if (sd->hate_mob[pos] != -1)
  1119. { //Can't change hate targets.
  1120. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1121. return false;
  1122. }
  1123. class_ = status_get_class(bl);
  1124. if (!pcdb_checkid(class_)) {
  1125. unsigned int max_hp = status_get_max_hp(bl);
  1126. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1127. return false;
  1128. if (pos != status_get_size(bl))
  1129. return false; //Wrong size
  1130. }
  1131. sd->hate_mob[pos] = class_;
  1132. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1133. clif_hate_info(sd, pos, class_, 1);
  1134. return true;
  1135. }
  1136. /*==========================================
  1137. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1138. *------------------------------------------*/
  1139. void pc_reg_received(struct map_session_data *sd)
  1140. {
  1141. uint8 i;
  1142. sd->vars_ok = true;
  1143. sd->change_level_2nd = pc_readglobalreg(sd, add_str("jobchange_level"));
  1144. sd->change_level_3rd = pc_readglobalreg(sd, add_str("jobchange_level_3rd"));
  1145. sd->die_counter = pc_readglobalreg(sd, add_str("PC_DIE_COUNTER"));
  1146. sd->langtype = pc_readaccountreg(sd, add_str("#langtype"));
  1147. if (msg_checklangtype(sd->langtype,true) < 0)
  1148. sd->langtype = 0; //invalid langtype reset to default
  1149. // Cash shop
  1150. sd->cashPoints = pc_readaccountreg(sd, add_str("#CASHPOINTS"));
  1151. sd->kafraPoints = pc_readaccountreg(sd, add_str("#KAFRAPOINTS"));
  1152. // Cooking Exp
  1153. sd->cook_mastery = pc_readglobalreg(sd, add_str("COOK_MASTERY"));
  1154. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1155. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1156. sd->mission_mobid = pc_readglobalreg(sd, add_str("TK_MISSION_ID"));
  1157. sd->mission_count = pc_readglobalreg(sd, add_str("TK_MISSION_COUNT"));
  1158. }
  1159. if (battle_config.feature_banking)
  1160. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1161. if (battle_config.feature_roulette) {
  1162. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1163. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1164. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1165. }
  1166. sd->roulette.prizeIdx = -1;
  1167. //SG map and mob read [Komurka]
  1168. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1169. uint16 j;
  1170. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1171. sd->feel_map[i].index = j;
  1172. sd->feel_map[i].m = map_mapindex2mapid(j);
  1173. } else {
  1174. sd->feel_map[i].index = 0;
  1175. sd->feel_map[i].m = -1;
  1176. }
  1177. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1178. }
  1179. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1180. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1181. sd->cloneskill_idx = skill_get_index(skid);
  1182. if (sd->cloneskill_idx > 0) {
  1183. sd->status.skill[sd->cloneskill_idx].id = skid;
  1184. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1185. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1186. sd->status.skill[sd->cloneskill_idx].lv = i;
  1187. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1188. }
  1189. }
  1190. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1191. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1192. sd->reproduceskill_idx = skill_get_index(skid);
  1193. if (sd->reproduceskill_idx > 0) {
  1194. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1195. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1196. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1197. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1198. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1199. }
  1200. }
  1201. //Weird... maybe registries were reloaded?
  1202. if (sd->state.active)
  1203. return;
  1204. sd->state.active = 1;
  1205. if (sd->status.party_id)
  1206. party_member_joined(sd);
  1207. if (sd->status.guild_id)
  1208. guild_member_joined(sd);
  1209. // pet
  1210. if (sd->status.pet_id > 0)
  1211. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1212. // Homunculus [albator]
  1213. if( sd->status.hom_id > 0 )
  1214. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1215. if( sd->status.mer_id > 0 )
  1216. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1217. if( sd->status.ele_id > 0 )
  1218. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1219. map_addiddb(&sd->bl);
  1220. map_delnickdb(sd->status.char_id, sd->status.name);
  1221. if (!chrif_auth_finished(sd))
  1222. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1223. pc_load_combo(sd);
  1224. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  1225. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1226. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1227. chrif_bsdata_request(sd->status.char_id);
  1228. sd->storage_size = MIN_STORAGE; //default to min
  1229. #ifdef VIP_ENABLE
  1230. sd->vip.time = 0;
  1231. sd->vip.enabled = 0;
  1232. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0); // request VIP informations
  1233. #endif
  1234. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1235. intif_request_questlog(sd);
  1236. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1237. sd->state.connect_new = 1;
  1238. clif_parse_LoadEndAck(sd->fd, sd);
  1239. }
  1240. if( sd->sc.option&OPTION_INVISIBLE ) {
  1241. sd->vd.class_ = INVISIBLE_CLASS;
  1242. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1243. // decrement the number of pvp players on the map
  1244. map[sd->bl.m].users_pvp--;
  1245. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1246. // unregister the player for ranking
  1247. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1248. sd->pvp_timer = INVALID_TIMER;
  1249. }
  1250. clif_changeoption( &sd->bl );
  1251. }
  1252. pc_check_expiration(sd);
  1253. if( sd->state.autotrade ) {
  1254. clif_parse_LoadEndAck(sd->fd, sd);
  1255. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  1256. }
  1257. }
  1258. static int pc_calc_skillpoint(struct map_session_data* sd)
  1259. {
  1260. uint16 i, skill_point = 0;
  1261. nullpo_ret(sd);
  1262. for(i = 1; i < MAX_SKILL; i++) {
  1263. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1264. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1265. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1266. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1267. )
  1268. {
  1269. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1270. skill_point += sd->status.skill[i].lv;
  1271. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1272. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1273. }
  1274. }
  1275. }
  1276. return skill_point;
  1277. }
  1278. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1279. uint16 i = 0;
  1280. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1281. return false;
  1282. /**
  1283. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1284. * skill tree and since they have no icons they'll give resource errors
  1285. * Get ALL skills except npc/guild ones. [Skotlex]
  1286. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1287. **/
  1288. for( i = 0; i < MAX_SKILL; i++ ) {
  1289. uint16 skill_id = skill_idx2id(i);
  1290. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1291. continue;
  1292. switch (skill_id) {
  1293. case SM_SELFPROVOKE:
  1294. case AB_DUPLELIGHT_MELEE:
  1295. case AB_DUPLELIGHT_MAGIC:
  1296. case WL_CHAINLIGHTNING_ATK:
  1297. case WL_TETRAVORTEX_FIRE:
  1298. case WL_TETRAVORTEX_WATER:
  1299. case WL_TETRAVORTEX_WIND:
  1300. case WL_TETRAVORTEX_GROUND:
  1301. case WL_SUMMON_ATK_FIRE:
  1302. case WL_SUMMON_ATK_WIND:
  1303. case WL_SUMMON_ATK_WATER:
  1304. case WL_SUMMON_ATK_GROUND:
  1305. case LG_OVERBRAND_BRANDISH:
  1306. case LG_OVERBRAND_PLUSATK:
  1307. case WM_SEVERE_RAINSTORM_MELEE:
  1308. case RL_R_TRIP_PLUSATK:
  1309. case SG_DEVIL:
  1310. case MO_TRIPLEATTACK:
  1311. case RG_SNATCHER:
  1312. continue;
  1313. default:
  1314. {
  1315. uint8 lv = (uint8)skill_get_max(skill_id);
  1316. if (lv > 0) {
  1317. sd->status.skill[i].id = skill_id;
  1318. if (addlv)
  1319. sd->status.skill[i].lv = lv;
  1320. }
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. return true;
  1326. }
  1327. /*==========================================
  1328. * Calculation of skill level.
  1329. * @param sd
  1330. *------------------------------------------*/
  1331. void pc_calc_skilltree(struct map_session_data *sd)
  1332. {
  1333. int i, flag;
  1334. int c = 0;
  1335. nullpo_retv(sd);
  1336. i = pc_calc_skilltree_normalize_job(sd);
  1337. c = pc_mapid2jobid(i, sd->status.sex);
  1338. if( c == -1 )
  1339. { //Unable to normalize job??
  1340. 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);
  1341. return;
  1342. }
  1343. c = pc_class2idx(c);
  1344. for( i = 0; i < MAX_SKILL; i++ ) {
  1345. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1346. sd->status.skill[i].id = 0; //First clear skills.
  1347. /* permanent skills that must be re-checked */
  1348. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1349. uint16 sk_id = skill_idx2id(i);
  1350. if (!sk_id) {
  1351. sd->status.skill[i].id = 0;
  1352. sd->status.skill[i].lv = 0;
  1353. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1354. continue;
  1355. }
  1356. switch (sk_id) {
  1357. case NV_TRICKDEAD:
  1358. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1359. sd->status.skill[i].id = 0;
  1360. sd->status.skill[i].lv = 0;
  1361. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1362. }
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. for( i = 0; i < MAX_SKILL; i++ ) {
  1368. uint16 skill_id = 0;
  1369. // Restore original level of skills after deleting earned skills.
  1370. if( sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED ) {
  1371. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1372. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1373. }
  1374. //Enable Bard/Dancer spirit linked skills.
  1375. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1376. continue;
  1377. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1378. //Link Dancer skills to bard.
  1379. if( sd->status.sex ) {
  1380. if( sd->status.skill[i-8].lv < 10 )
  1381. continue;
  1382. sd->status.skill[i].id = skill_id;
  1383. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1384. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1385. }
  1386. //Link Bard skills to dancer.
  1387. else {
  1388. if( sd->status.skill[i].lv < 10 )
  1389. continue;
  1390. sd->status.skill[i-8].id = skill_id - 8;
  1391. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1392. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1393. }
  1394. }
  1395. }
  1396. // Removes Taekwon Ranker skill bonus
  1397. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1398. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1399. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1400. uint16 sk_id = skill_tree[c_][i].id;
  1401. uint16 sk_idx = 0;
  1402. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].id)))
  1403. continue;
  1404. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1405. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1406. continue;
  1407. sd->status.skill[sk_idx].id = 0;
  1408. }
  1409. }
  1410. }
  1411. // Grant all skills
  1412. pc_grant_allskills(sd, false);
  1413. do {
  1414. uint16 skid = 0;
  1415. flag = 0;
  1416. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++) {
  1417. bool fail = false;
  1418. uint16 sk_idx = skill_get_index(skid);
  1419. if (sd->status.skill[sk_idx].id)
  1420. continue; //Skill already known.
  1421. if (!battle_config.skillfree) {
  1422. uint8 j;
  1423. // Checking required skills
  1424. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1425. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1426. uint16 sk_need_idx = 0;
  1427. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1428. short sk_need = sk_need_id;
  1429. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  1430. sk_need = 0; //Not learned.
  1431. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1432. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1433. else
  1434. sk_need = pc_checkskill(sd,sk_need_id);
  1435. if (sk_need < skill_tree[c][i].need[j].lv) {
  1436. fail = true;
  1437. break;
  1438. }
  1439. }
  1440. }
  1441. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1442. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1443. class_ = pc_class2idx(class_);
  1444. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1445. fail = true; // job level requirement wasn't satisfied
  1446. }
  1447. }
  1448. if (!fail) {
  1449. int inf2 = skill_get_inf2(skid);
  1450. if (!sd->status.skill[sk_idx].lv && (
  1451. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1452. inf2&INF2_WEDDING_SKILL ||
  1453. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1454. ))
  1455. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1456. sd->status.skill[sk_idx].id = skid;
  1457. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1458. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1459. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1460. }
  1461. flag = 1; // skill list has changed, perform another pass
  1462. }
  1463. }
  1464. } while(flag);
  1465. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1466. unsigned short skid = 0;
  1467. /* Taekwon Ranker Bonus Skill Tree
  1468. ============================================
  1469. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1470. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1471. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1472. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1473. uint16 sk_idx = 0;
  1474. if (!(sk_idx = skill_get_index(skid)))
  1475. continue;
  1476. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1477. continue; //Do not include Quest/Wedding skills.
  1478. if( sd->status.skill[sk_idx].id == 0 ) {
  1479. sd->status.skill[sk_idx].id = skid;
  1480. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1481. } else if( skid != NV_BASIC )
  1482. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1483. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1484. }
  1485. }
  1486. }
  1487. //Checks if you can learn a new skill after having leveled up a skill.
  1488. static void pc_check_skilltree(struct map_session_data *sd)
  1489. {
  1490. int i, flag = 0;
  1491. int c = 0;
  1492. if (battle_config.skillfree)
  1493. return; //Function serves no purpose if this is set
  1494. i = pc_calc_skilltree_normalize_job(sd);
  1495. c = pc_mapid2jobid(i, sd->status.sex);
  1496. if (c == -1) { //Unable to normalize job??
  1497. 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);
  1498. return;
  1499. }
  1500. c = pc_class2idx(c);
  1501. do {
  1502. uint16 skid = 0;
  1503. flag = 0;
  1504. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].id) > 0; i++ ) {
  1505. uint16 sk_idx = skill_get_index(skid);
  1506. bool fail = false;
  1507. uint8 j = 0;
  1508. if (sd->status.skill[sk_idx].id) //Already learned
  1509. continue;
  1510. // Checking required skills
  1511. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1512. uint16 sk_need_id = skill_tree[c][i].need[j].id;
  1513. uint16 sk_need_idx = 0;
  1514. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1515. short sk_need = sk_need_id;
  1516. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  1517. sk_need = 0; //Not learned.
  1518. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1519. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1520. else
  1521. sk_need = pc_checkskill(sd,sk_need_id);
  1522. if (sk_need < skill_tree[c][i].need[j].lv) {
  1523. fail = true;
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. if( fail )
  1529. continue;
  1530. if( sd->status.job_level < skill_tree[c][i].joblv )
  1531. continue;
  1532. j = skill_get_inf2(skid);
  1533. if( !sd->status.skill[sk_idx].lv && (
  1534. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1535. j&INF2_WEDDING_SKILL ||
  1536. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1537. ) )
  1538. continue; //Cannot be learned via normal means.
  1539. sd->status.skill[sk_idx].id = skid;
  1540. flag = 1;
  1541. }
  1542. } while(flag);
  1543. }
  1544. // Make sure all the skills are in the correct condition
  1545. // before persisting to the backend.. [MouseJstr]
  1546. void pc_clean_skilltree(struct map_session_data *sd)
  1547. {
  1548. uint16 i;
  1549. for (i = 0; i < MAX_SKILL; i++){
  1550. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1551. sd->status.skill[i].id = 0;
  1552. sd->status.skill[i].lv = 0;
  1553. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1554. }
  1555. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1556. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1557. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1558. }
  1559. }
  1560. }
  1561. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1562. {
  1563. int skill_point, novice_skills;
  1564. int c = sd->class_;
  1565. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1566. return c;
  1567. skill_point = pc_calc_skillpoint(sd);
  1568. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1569. // limit 1st class and above to novice job levels
  1570. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1571. {
  1572. c = MAPID_NOVICE;
  1573. }
  1574. // limit 2nd class and above to first class job levels (super novices are exempt)
  1575. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1576. {
  1577. // regenerate change_level_2nd
  1578. if (!sd->change_level_2nd)
  1579. {
  1580. if (sd->class_&JOBL_THIRD)
  1581. {
  1582. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1583. if (!sd->change_level_3rd)
  1584. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1585. else
  1586. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1587. - (sd->status.job_level - 1)
  1588. - (sd->change_level_3rd - 1)
  1589. - novice_skills;
  1590. }
  1591. else
  1592. {
  1593. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1594. - (sd->status.job_level - 1)
  1595. - novice_skills;
  1596. }
  1597. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  1598. }
  1599. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1600. {
  1601. c &= MAPID_BASEMASK;
  1602. }
  1603. // limit 3rd class to 2nd class/trans job levels
  1604. else if(sd->class_&JOBL_THIRD)
  1605. {
  1606. // regenerate change_level_3rd
  1607. if (!sd->change_level_3rd)
  1608. {
  1609. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1610. - (sd->status.job_level - 1)
  1611. - (sd->change_level_2nd - 1)
  1612. - novice_skills;
  1613. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  1614. }
  1615. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1616. c &= MAPID_UPPERMASK;
  1617. }
  1618. }
  1619. // restore non-limiting flags
  1620. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1621. return c;
  1622. }
  1623. /*==========================================
  1624. * Updates the weight status
  1625. *------------------------------------------
  1626. * 1: overweight 50%
  1627. * 2: overweight 90%
  1628. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1629. */
  1630. void pc_updateweightstatus(struct map_session_data *sd)
  1631. {
  1632. int old_overweight;
  1633. int new_overweight;
  1634. nullpo_retv(sd);
  1635. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1636. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1637. if( old_overweight == new_overweight )
  1638. return; // no change
  1639. // stop old status change
  1640. if( old_overweight == 1 )
  1641. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1642. else if( old_overweight == 2 )
  1643. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1644. // start new status change
  1645. if( new_overweight == 1 )
  1646. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1647. else if( new_overweight == 2 )
  1648. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1649. // update overweight status
  1650. sd->regen.state.overweight = new_overweight;
  1651. }
  1652. int pc_disguise(struct map_session_data *sd, int class_)
  1653. {
  1654. if (!class_ && !sd->disguise)
  1655. return 0;
  1656. if (class_ && sd->disguise == class_)
  1657. return 0;
  1658. if(sd->sc.option&OPTION_INVISIBLE)
  1659. { //Character is invisible. Stealth class-change. [Skotlex]
  1660. sd->disguise = class_; //viewdata is set on uncloaking.
  1661. return 2;
  1662. }
  1663. if (sd->bl.prev != NULL) {
  1664. pc_stop_walking(sd, 0);
  1665. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1666. }
  1667. if (!class_) {
  1668. sd->disguise = 0;
  1669. class_ = sd->status.class_;
  1670. } else
  1671. sd->disguise=class_;
  1672. status_set_viewdata(&sd->bl, class_);
  1673. clif_changeoption(&sd->bl);
  1674. if (sd->bl.prev != NULL) {
  1675. clif_spawn(&sd->bl);
  1676. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1677. { //It seems the cart info is lost on undisguise.
  1678. clif_cartlist(sd);
  1679. clif_updatestatus(sd,SP_CARTINFO);
  1680. }
  1681. if (sd->chatID) {
  1682. struct chat_data* cd;
  1683. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1684. clif_dispchat(cd,0);
  1685. }
  1686. }
  1687. return 1;
  1688. }
  1689. /// Show error message
  1690. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1691. /// Check for valid Element, break & show error message if invalid Element
  1692. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1693. /// Check for valid Race, break & show error message if invalid Race
  1694. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1695. /// Check for valid Race2, break & show error message if invalid Race2
  1696. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1697. /// Check for valid Class, break & show error message if invalid Class
  1698. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1699. /// Check for valid Size, break & show error message if invalid Size
  1700. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1701. /// Check for valid SC, break & show error message if invalid SC
  1702. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1703. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1704. {
  1705. uint8 i;
  1706. if( !rate )
  1707. return;
  1708. for( i = 0; i < max && spell[i].id; i++ )
  1709. {
  1710. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1711. {
  1712. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1713. return;
  1714. rate += spell[i].rate;
  1715. break;
  1716. }
  1717. }
  1718. if (i == max) {
  1719. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1720. return;
  1721. }
  1722. spell[i].id = id;
  1723. spell[i].lv = lv;
  1724. spell[i].rate = rate;
  1725. //Auto-update flag value.
  1726. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1727. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1728. if (!(flag&BF_SKILLMASK)) {
  1729. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1730. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1731. }
  1732. spell[i].flag|= flag;
  1733. spell[i].card_id = card_id;
  1734. }
  1735. static void pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1736. {
  1737. uint8 i;
  1738. if( !rate )
  1739. return;
  1740. for( i = 0; i < max && spell[i].id; i++ )
  1741. {
  1742. ; // each autospell works independently
  1743. }
  1744. if( i == max )
  1745. {
  1746. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1747. return;
  1748. }
  1749. spell[i].flag = src_skill;
  1750. spell[i].id = id;
  1751. spell[i].lv = lv;
  1752. spell[i].rate = rate;
  1753. spell[i].card_id = card_id;
  1754. return;
  1755. }
  1756. /**
  1757. * Add inflict effect bonus for player while attacking/atatcked
  1758. * @param effect Effect array
  1759. * @param pmax Max array
  1760. * @param sc SC/Effect type
  1761. * @param rate Success chance
  1762. * @param arrow_rate success chance if bonus comes from arrow-type item
  1763. * @param flag Target flag
  1764. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1765. **/
  1766. static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  1767. {
  1768. uint16 i;
  1769. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1770. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1771. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1772. flag |= ATF_TARGET; //Default target: enemy.
  1773. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1774. flag |= ATF_WEAPON; //Default type: weapon.
  1775. if (!duration)
  1776. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1777. for (i = 0; i < pmax && effect[i].flag; i++) {
  1778. if (effect[i].sc == sc && effect[i].flag == flag) {
  1779. effect[i].rate += rate;
  1780. effect[i].arrow_rate += arrow_rate;
  1781. effect[i].duration = umax(effect[i].duration, duration);
  1782. return;
  1783. }
  1784. }
  1785. if (i == pmax) {
  1786. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1787. return;
  1788. }
  1789. effect[i].sc = sc;
  1790. effect[i].rate = rate;
  1791. effect[i].arrow_rate = arrow_rate;
  1792. effect[i].flag = flag;
  1793. effect[i].duration = duration;
  1794. }
  1795. /**
  1796. * Add inflict effect bonus for player while attacking using skill
  1797. * @param effect Effect array
  1798. * @param pmax Max array
  1799. * @param sc SC/Effect type
  1800. * @param rate Success chance
  1801. * @param flag Target flag
  1802. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1803. **/
  1804. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  1805. {
  1806. uint8 i;
  1807. if (!duration)
  1808. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1809. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1810. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1811. effect[i].rate += rate;
  1812. effect[i].duration = umax(effect[i].duration, duration);
  1813. return;
  1814. }
  1815. }
  1816. if( i == pmax ) {
  1817. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1818. return;
  1819. }
  1820. effect[i].sc = sc;
  1821. effect[i].rate = rate;
  1822. effect[i].skill_id = skill_id;
  1823. effect[i].target = target;
  1824. effect[i].duration = duration;
  1825. }
  1826. /** Adjust/add drop rate modifier for player
  1827. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1828. * @param max: Max bonus can be received
  1829. * @param nameid: item id that will be dropped
  1830. * @param group: group id
  1831. * @param class_: target class
  1832. * @param race: target race. if < 0, means monster_id
  1833. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1834. */
  1835. static void pc_bonus_item_drop(struct s_add_drop *drop, const short max, unsigned short nameid, uint16 group, int class_, short race, int rate)
  1836. {
  1837. uint8 i;
  1838. if (!nameid && !group) {
  1839. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1840. return;
  1841. }
  1842. if (nameid && !itemdb_exists(nameid)) {
  1843. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1844. return;
  1845. }
  1846. if (group && !itemdb_group_exists(group)) {
  1847. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1848. return;
  1849. }
  1850. //Apply config rate adjustment settings.
  1851. if (rate >= 0) { //Absolute drop.
  1852. if (battle_config.item_rate_adddrop != 100)
  1853. rate = rate*battle_config.item_rate_adddrop/100;
  1854. if (rate < battle_config.item_drop_adddrop_min)
  1855. rate = battle_config.item_drop_adddrop_min;
  1856. else if (rate > battle_config.item_drop_adddrop_max)
  1857. rate = battle_config.item_drop_adddrop_max;
  1858. } else { //Relative drop, max/min limits are applied at drop time.
  1859. if (battle_config.item_rate_adddrop != 100)
  1860. rate = rate*battle_config.item_rate_adddrop/100;
  1861. if (rate > -1)
  1862. rate = -1;
  1863. }
  1864. //Find match entry, and adjust the rate only
  1865. for (i = 0; i < max; i++) {
  1866. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1867. continue;
  1868. if (drop[i].nameid == nameid &&
  1869. drop[i].group == group &&
  1870. drop[i].race == race &&
  1871. drop[i].class_ == class_
  1872. )
  1873. {
  1874. //Adjust the rate if it has same classification
  1875. if ((rate < 0 && drop[i].rate < 0) ||
  1876. (rate > 0 && drop[i].rate > 0))
  1877. {
  1878. drop[i].rate += rate;
  1879. return;
  1880. }
  1881. }
  1882. }
  1883. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1884. if (i >= max) {
  1885. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid:%hu group:%d class_:%d race:%d rate:%d)\n",max,nameid,group,class_,race,rate);
  1886. return;
  1887. }
  1888. drop[i].nameid = nameid;
  1889. drop[i].group = group;
  1890. drop[i].race = race;
  1891. drop[i].class_ = class_;
  1892. drop[i].rate = rate;
  1893. }
  1894. bool pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned int pos,bool onskill)
  1895. {
  1896. int i;
  1897. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1898. if( i == max )
  1899. {
  1900. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1901. return false;
  1902. }
  1903. if( !onskill )
  1904. {
  1905. if( !(flag&BF_RANGEMASK) )
  1906. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1907. if( !(flag&BF_WEAPONMASK) )
  1908. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1909. if( !(flag&BF_SKILLMASK) )
  1910. {
  1911. if( flag&(BF_MAGIC|BF_MISC) )
  1912. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1913. if( flag&BF_WEAPON )
  1914. flag|=BF_NORMAL|BF_SKILL;
  1915. }
  1916. }
  1917. bonus[i].rate = rate;
  1918. bonus[i].duration = dur;
  1919. bonus[i].active = INVALID_TIMER;
  1920. bonus[i].atk_type = flag;
  1921. bonus[i].pos = pos;
  1922. bonus[i].bonus_script = aStrdup(script);
  1923. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1924. return true;
  1925. }
  1926. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1927. {
  1928. int i;
  1929. if (!sd)
  1930. return;
  1931. for( i = 0; i < max; i++ )
  1932. {
  1933. if( autobonus[i].active != INVALID_TIMER )
  1934. {
  1935. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1936. {
  1937. if( autobonus[i].bonus_script )
  1938. {
  1939. int j;
  1940. unsigned int equip_pos_idx = 0;
  1941. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1942. for(j = 0; j < EQI_MAX; j++) {
  1943. if(sd->equip_index[j] >= 0)
  1944. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1945. }
  1946. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  1947. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  1948. }
  1949. continue;
  1950. }
  1951. else
  1952. { // Logout / Unequipped an item with an activated bonus
  1953. delete_timer(autobonus[i].active,pc_endautobonus);
  1954. autobonus[i].active = INVALID_TIMER;
  1955. }
  1956. }
  1957. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1958. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1959. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1960. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1961. autobonus[i].active = INVALID_TIMER;
  1962. }
  1963. }
  1964. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1965. {
  1966. if (!sd || !autobonus)
  1967. return;
  1968. if( autobonus->other_script )
  1969. {
  1970. int j;
  1971. unsigned int equip_pos_idx = 0;
  1972. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1973. for(j = 0; j < EQI_MAX; j++) {
  1974. if(sd->equip_index[j] >= 0)
  1975. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1976. }
  1977. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  1978. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  1979. }
  1980. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1981. sd->state.autobonus |= autobonus->pos;
  1982. status_calc_pc(sd,SCO_FORCE);
  1983. }
  1984. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1985. {
  1986. struct map_session_data *sd = map_id2sd(id);
  1987. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1988. nullpo_ret(sd);
  1989. nullpo_ret(autobonus);
  1990. autobonus->active = INVALID_TIMER;
  1991. sd->state.autobonus &= ~autobonus->pos;
  1992. status_calc_pc(sd,SCO_FORCE);
  1993. return 0;
  1994. }
  1995. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1996. {
  1997. uint8 i;
  1998. struct weapon_data* wd;
  1999. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2000. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2001. if (i == MAX_PC_BONUS)
  2002. {
  2003. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2004. return;
  2005. }
  2006. if (!(flag&BF_RANGEMASK))
  2007. flag |= BF_SHORT|BF_LONG;
  2008. if (!(flag&BF_WEAPONMASK))
  2009. flag |= BF_WEAPON;
  2010. if (!(flag&BF_SKILLMASK))
  2011. {
  2012. if (flag&(BF_MAGIC|BF_MISC))
  2013. flag |= BF_SKILL;
  2014. if (flag&BF_WEAPON)
  2015. flag |= BF_NORMAL|BF_SKILL;
  2016. }
  2017. wd->addele2[i].ele = ele;
  2018. wd->addele2[i].rate = rate;
  2019. wd->addele2[i].flag = flag;
  2020. }
  2021. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2022. {
  2023. uint8 i;
  2024. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2025. if (i == MAX_PC_BONUS)
  2026. {
  2027. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2028. return;
  2029. }
  2030. if (!(flag&BF_RANGEMASK))
  2031. flag |= BF_SHORT|BF_LONG;
  2032. if (!(flag&BF_WEAPONMASK))
  2033. flag |= BF_WEAPON;
  2034. if (!(flag&BF_SKILLMASK))
  2035. {
  2036. if (flag&(BF_MAGIC|BF_MISC))
  2037. flag |= BF_SKILL;
  2038. if (flag&BF_WEAPON)
  2039. flag |= BF_NORMAL|BF_SKILL;
  2040. }
  2041. sd->subele2[i].ele = ele;
  2042. sd->subele2[i].rate = rate;
  2043. sd->subele2[i].flag = flag;
  2044. }
  2045. /** Add item group heal rate bonus to player
  2046. * @param sd Player
  2047. * @param group_id Item Group ID
  2048. * @param rate
  2049. * @author Cydh
  2050. */
  2051. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2052. struct s_pc_itemgrouphealrate *entry;
  2053. uint8 i;
  2054. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2055. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2056. break;
  2057. }
  2058. if (i != sd->itemgrouphealrate_count) {
  2059. sd->itemgrouphealrate[i]->rate += rate;
  2060. return;
  2061. }
  2062. if (i >= UINT8_MAX) {
  2063. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2064. return;
  2065. }
  2066. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2067. entry->group_id = group_id;
  2068. entry->rate = rate;
  2069. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2070. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2071. }
  2072. /** Clear item group heal rate from player
  2073. * @param sd Player
  2074. * @author Cydh
  2075. */
  2076. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2077. if (!sd || !sd->itemgrouphealrate_count)
  2078. return;
  2079. else {
  2080. uint8 i;
  2081. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2082. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2083. sd->itemgrouphealrate_count = 0;
  2084. aFree(sd->itemgrouphealrate);
  2085. sd->itemgrouphealrate = NULL;
  2086. }
  2087. }
  2088. /*==========================================
  2089. * Add a bonus(type) to player sd
  2090. * format: bonus bBonusName,val;
  2091. * @param sd
  2092. * @param type Bonus type used by bBonusName
  2093. * @param val Value that usually for rate or fixed value
  2094. *------------------------------------------*/
  2095. void pc_bonus(struct map_session_data *sd,int type,int val)
  2096. {
  2097. struct status_data *status;
  2098. int bonus;
  2099. nullpo_retv(sd);
  2100. status = &sd->base_status;
  2101. switch(type){
  2102. case SP_STR:
  2103. case SP_AGI:
  2104. case SP_VIT:
  2105. case SP_INT:
  2106. case SP_DEX:
  2107. case SP_LUK:
  2108. if(sd->state.lr_flag != 2)
  2109. sd->param_bonus[type-SP_STR]+=val;
  2110. break;
  2111. case SP_ATK1:
  2112. if(!sd->state.lr_flag) {
  2113. bonus = status->rhw.atk + val;
  2114. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2115. }
  2116. else if(sd->state.lr_flag == 1) {
  2117. bonus = status->lhw.atk + val;
  2118. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2119. }
  2120. break;
  2121. case SP_ATK2:
  2122. if(!sd->state.lr_flag) {
  2123. bonus = status->rhw.atk2 + val;
  2124. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2125. }
  2126. else if(sd->state.lr_flag == 1) {
  2127. bonus = status->lhw.atk2 + val;
  2128. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2129. }
  2130. break;
  2131. case SP_BASE_ATK:
  2132. if(sd->state.lr_flag != 2) {
  2133. #ifdef RENEWAL
  2134. sd->bonus.eatk += val;
  2135. #else
  2136. bonus = status->batk + val;
  2137. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2138. #endif
  2139. }
  2140. break;
  2141. case SP_DEF1:
  2142. if(sd->state.lr_flag != 2) {
  2143. bonus = status->def + val;
  2144. #ifdef RENEWAL
  2145. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2146. #else
  2147. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2148. #endif
  2149. }
  2150. break;
  2151. case SP_DEF2:
  2152. if(sd->state.lr_flag != 2) {
  2153. bonus = status->def2 + val;
  2154. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2155. }
  2156. break;
  2157. case SP_MDEF1:
  2158. if(sd->state.lr_flag != 2) {
  2159. bonus = status->mdef + val;
  2160. #ifdef RENEWAL
  2161. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2162. #else
  2163. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2164. #endif
  2165. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2166. sd->bonus.shieldmdef += bonus;
  2167. }
  2168. }
  2169. break;
  2170. case SP_MDEF2:
  2171. if(sd->state.lr_flag != 2) {
  2172. bonus = status->mdef2 + val;
  2173. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2174. }
  2175. break;
  2176. case SP_HIT:
  2177. if(sd->state.lr_flag != 2) {
  2178. bonus = status->hit + val;
  2179. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2180. } else
  2181. sd->bonus.arrow_hit+=val;
  2182. break;
  2183. case SP_FLEE1:
  2184. if(sd->state.lr_flag != 2) {
  2185. bonus = status->flee + val;
  2186. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2187. }
  2188. break;
  2189. case SP_FLEE2:
  2190. if(sd->state.lr_flag != 2) {
  2191. bonus = status->flee2 + val*10;
  2192. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2193. }
  2194. break;
  2195. case SP_CRITICAL:
  2196. if(sd->state.lr_flag != 2) {
  2197. bonus = status->cri + val*10;
  2198. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2199. } else
  2200. sd->bonus.arrow_cri += val*10;
  2201. break;
  2202. case SP_ATKELE:
  2203. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2204. switch (sd->state.lr_flag)
  2205. {
  2206. case 2:
  2207. switch (sd->status.weapon) {
  2208. case W_BOW:
  2209. case W_REVOLVER:
  2210. case W_RIFLE:
  2211. case W_GATLING:
  2212. case W_SHOTGUN:
  2213. case W_GRENADE:
  2214. //Become weapon element.
  2215. status->rhw.ele=val;
  2216. break;
  2217. default: //Become arrow element.
  2218. sd->bonus.arrow_ele=val;
  2219. break;
  2220. }
  2221. break;
  2222. case 1:
  2223. status->lhw.ele=val;
  2224. break;
  2225. default:
  2226. status->rhw.ele=val;
  2227. break;
  2228. }
  2229. break;
  2230. case SP_DEFELE:
  2231. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2232. if(sd->state.lr_flag != 2)
  2233. status->def_ele=val;
  2234. break;
  2235. case SP_MAXHP:
  2236. if(sd->state.lr_flag == 2)
  2237. break;
  2238. sd->bonus.hp += val;
  2239. break;
  2240. case SP_MAXSP:
  2241. if(sd->state.lr_flag == 2)
  2242. break;
  2243. sd->bonus.sp += val;
  2244. break;
  2245. case SP_MAXHPRATE:
  2246. if(sd->state.lr_flag != 2)
  2247. sd->hprate+=val;
  2248. break;
  2249. case SP_MAXSPRATE:
  2250. if(sd->state.lr_flag != 2)
  2251. sd->sprate+=val;
  2252. break;
  2253. case SP_SPRATE:
  2254. if(sd->state.lr_flag != 2)
  2255. sd->dsprate+=val;
  2256. break;
  2257. case SP_ATTACKRANGE:
  2258. switch (sd->state.lr_flag) {
  2259. case 2:
  2260. switch (sd->status.weapon) {
  2261. case W_BOW:
  2262. case W_REVOLVER:
  2263. case W_RIFLE:
  2264. case W_GATLING:
  2265. case W_SHOTGUN:
  2266. case W_GRENADE:
  2267. status->rhw.range += val;
  2268. }
  2269. break;
  2270. case 1:
  2271. status->lhw.range += val;
  2272. break;
  2273. default:
  2274. status->rhw.range += val;
  2275. break;
  2276. }
  2277. break;
  2278. case SP_SPEED_RATE: //Non stackable increase
  2279. if(sd->state.lr_flag != 2)
  2280. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2281. break;
  2282. case SP_SPEED_ADDRATE: //Stackable increase
  2283. if(sd->state.lr_flag != 2)
  2284. sd->bonus.speed_add_rate -= val;
  2285. break;
  2286. case SP_ASPD: //Raw increase
  2287. if(sd->state.lr_flag != 2)
  2288. sd->bonus.aspd_add -= 10*val;
  2289. break;
  2290. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2291. if(sd->state.lr_flag != 2)
  2292. #ifndef RENEWAL_ASPD
  2293. status->aspd_rate -= 10*val;
  2294. #else
  2295. status->aspd_rate2 += val;
  2296. #endif
  2297. break;
  2298. case SP_HP_RECOV_RATE:
  2299. if(sd->state.lr_flag != 2)
  2300. sd->hprecov_rate += val;
  2301. break;
  2302. case SP_SP_RECOV_RATE:
  2303. if(sd->state.lr_flag != 2)
  2304. sd->sprecov_rate += val;
  2305. break;
  2306. case SP_CRITICAL_DEF:
  2307. if(sd->state.lr_flag != 2)
  2308. sd->bonus.critical_def += val;
  2309. break;
  2310. case SP_NEAR_ATK_DEF:
  2311. if(sd->state.lr_flag != 2)
  2312. sd->bonus.near_attack_def_rate += val;
  2313. break;
  2314. case SP_LONG_ATK_DEF:
  2315. if(sd->state.lr_flag != 2)
  2316. sd->bonus.long_attack_def_rate += val;
  2317. break;
  2318. case SP_DOUBLE_RATE:
  2319. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2320. sd->bonus.double_rate = val;
  2321. break;
  2322. case SP_DOUBLE_ADD_RATE:
  2323. if(sd->state.lr_flag == 0)
  2324. sd->bonus.double_add_rate += val;
  2325. break;
  2326. case SP_MATK_RATE:
  2327. if(sd->state.lr_flag != 2)
  2328. sd->matk_rate += val;
  2329. break;
  2330. case SP_IGNORE_DEF_ELE:
  2331. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2332. if(!sd->state.lr_flag)
  2333. sd->right_weapon.ignore_def_ele |= 1<<val;
  2334. else if(sd->state.lr_flag == 1)
  2335. sd->left_weapon.ignore_def_ele |= 1<<val;
  2336. break;
  2337. case SP_IGNORE_DEF_RACE:
  2338. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2339. if(!sd->state.lr_flag)
  2340. sd->right_weapon.ignore_def_race |= 1<<val;
  2341. else if(sd->state.lr_flag == 1)
  2342. sd->left_weapon.ignore_def_race |= 1<<val;
  2343. break;
  2344. case SP_IGNORE_DEF_CLASS:
  2345. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2346. if(!sd->state.lr_flag)
  2347. sd->right_weapon.ignore_def_class |= 1<<val;
  2348. else if(sd->state.lr_flag == 1)
  2349. sd->left_weapon.ignore_def_class |= 1<<val;
  2350. break;
  2351. case SP_ATK_RATE:
  2352. if(sd->state.lr_flag != 2)
  2353. sd->bonus.atk_rate += val;
  2354. break;
  2355. case SP_MAGIC_ATK_DEF:
  2356. if(sd->state.lr_flag != 2)
  2357. sd->bonus.magic_def_rate += val;
  2358. break;
  2359. case SP_MISC_ATK_DEF:
  2360. if(sd->state.lr_flag != 2)
  2361. sd->bonus.misc_def_rate += val;
  2362. break;
  2363. case SP_IGNORE_MDEF_ELE:
  2364. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2365. if(sd->state.lr_flag != 2)
  2366. sd->bonus.ignore_mdef_ele |= 1<<val;
  2367. break;
  2368. case SP_IGNORE_MDEF_RACE:
  2369. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2370. if(sd->state.lr_flag != 2)
  2371. sd->bonus.ignore_mdef_race |= 1<<val;
  2372. break;
  2373. case SP_PERFECT_HIT_RATE:
  2374. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2375. sd->bonus.perfect_hit = val;
  2376. break;
  2377. case SP_PERFECT_HIT_ADD_RATE:
  2378. if(sd->state.lr_flag != 2)
  2379. sd->bonus.perfect_hit_add += val;
  2380. break;
  2381. case SP_CRITICAL_RATE:
  2382. if(sd->state.lr_flag != 2)
  2383. sd->critical_rate+=val;
  2384. break;
  2385. case SP_DEF_RATIO_ATK_ELE:
  2386. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2387. if(!sd->state.lr_flag)
  2388. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2389. else if(sd->state.lr_flag == 1)
  2390. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2391. break;
  2392. case SP_DEF_RATIO_ATK_RACE:
  2393. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2394. if(!sd->state.lr_flag)
  2395. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2396. else if(sd->state.lr_flag == 1)
  2397. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2398. break;
  2399. case SP_DEF_RATIO_ATK_CLASS:
  2400. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2401. if(!sd->state.lr_flag)
  2402. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2403. else if(sd->state.lr_flag == 1)
  2404. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2405. break;
  2406. case SP_HIT_RATE:
  2407. if(sd->state.lr_flag != 2)
  2408. sd->hit_rate += val;
  2409. break;
  2410. case SP_FLEE_RATE:
  2411. if(sd->state.lr_flag != 2)
  2412. sd->flee_rate += val;
  2413. break;
  2414. case SP_FLEE2_RATE:
  2415. if(sd->state.lr_flag != 2)
  2416. sd->flee2_rate += val;
  2417. break;
  2418. case SP_DEF_RATE:
  2419. if(sd->state.lr_flag != 2)
  2420. sd->def_rate += val;
  2421. break;
  2422. case SP_DEF2_RATE:
  2423. if(sd->state.lr_flag != 2)
  2424. sd->def2_rate += val;
  2425. break;
  2426. case SP_MDEF_RATE:
  2427. if(sd->state.lr_flag != 2)
  2428. sd->mdef_rate += val;
  2429. break;
  2430. case SP_MDEF2_RATE:
  2431. if(sd->state.lr_flag != 2)
  2432. sd->mdef2_rate += val;
  2433. break;
  2434. case SP_RESTART_FULL_RECOVER:
  2435. if(sd->state.lr_flag != 2)
  2436. sd->special_state.restart_full_recover = 1;
  2437. break;
  2438. case SP_NO_CASTCANCEL:
  2439. if(sd->state.lr_flag != 2)
  2440. sd->special_state.no_castcancel = 1;
  2441. break;
  2442. case SP_NO_CASTCANCEL2:
  2443. if(sd->state.lr_flag != 2)
  2444. sd->special_state.no_castcancel2 = 1;
  2445. break;
  2446. case SP_NO_SIZEFIX:
  2447. if(sd->state.lr_flag != 2)
  2448. sd->special_state.no_sizefix = 1;
  2449. break;
  2450. case SP_NO_MAGIC_DAMAGE:
  2451. if(sd->state.lr_flag == 2)
  2452. break;
  2453. val+= sd->special_state.no_magic_damage;
  2454. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2455. break;
  2456. case SP_NO_WEAPON_DAMAGE:
  2457. if(sd->state.lr_flag == 2)
  2458. break;
  2459. val+= sd->special_state.no_weapon_damage;
  2460. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2461. break;
  2462. case SP_NO_MISC_DAMAGE:
  2463. if(sd->state.lr_flag == 2)
  2464. break;
  2465. val+= sd->special_state.no_misc_damage;
  2466. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2467. break;
  2468. case SP_NO_GEMSTONE:
  2469. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2470. sd->special_state.no_gemstone = 1;
  2471. break;
  2472. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2473. if(sd->state.lr_flag != 2) {
  2474. sd->special_state.intravision = 1;
  2475. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2476. }
  2477. break;
  2478. case SP_NO_KNOCKBACK:
  2479. if(sd->state.lr_flag != 2)
  2480. sd->special_state.no_knockback = 1;
  2481. break;
  2482. case SP_SPLASH_RANGE:
  2483. if(sd->bonus.splash_range < val)
  2484. sd->bonus.splash_range = val;
  2485. break;
  2486. case SP_SPLASH_ADD_RANGE:
  2487. sd->bonus.splash_add_range += val;
  2488. break;
  2489. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2490. if(sd->state.lr_flag != 2)
  2491. sd->bonus.short_weapon_damage_return += val;
  2492. break;
  2493. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2494. if(sd->state.lr_flag != 2)
  2495. sd->bonus.long_weapon_damage_return += val;
  2496. break;
  2497. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2498. if(sd->state.lr_flag != 2)
  2499. sd->bonus.magic_damage_return += val;
  2500. break;
  2501. case SP_ALL_STATS: // [Valaris]
  2502. if(sd->state.lr_flag!=2) {
  2503. sd->param_bonus[SP_STR-SP_STR]+=val;
  2504. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2505. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2506. sd->param_bonus[SP_INT-SP_STR]+=val;
  2507. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2508. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2509. }
  2510. break;
  2511. case SP_AGI_VIT: // [Valaris]
  2512. if(sd->state.lr_flag!=2) {
  2513. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2514. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2515. }
  2516. break;
  2517. case SP_AGI_DEX_STR: // [Valaris]
  2518. if(sd->state.lr_flag!=2) {
  2519. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2520. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2521. sd->param_bonus[SP_STR-SP_STR]+=val;
  2522. }
  2523. break;
  2524. case SP_PERFECT_HIDE: // [Valaris]
  2525. if(sd->state.lr_flag!=2)
  2526. sd->special_state.perfect_hiding=1;
  2527. break;
  2528. case SP_UNBREAKABLE:
  2529. if(sd->state.lr_flag!=2)
  2530. sd->bonus.unbreakable += val;
  2531. break;
  2532. case SP_UNBREAKABLE_WEAPON:
  2533. if(sd->state.lr_flag != 2)
  2534. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2535. break;
  2536. case SP_UNBREAKABLE_ARMOR:
  2537. if(sd->state.lr_flag != 2)
  2538. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2539. break;
  2540. case SP_UNBREAKABLE_HELM:
  2541. if(sd->state.lr_flag != 2)
  2542. sd->bonus.unbreakable_equip |= EQP_HELM;
  2543. break;
  2544. case SP_UNBREAKABLE_SHIELD:
  2545. if(sd->state.lr_flag != 2)
  2546. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2547. break;
  2548. case SP_UNBREAKABLE_GARMENT:
  2549. if(sd->state.lr_flag != 2)
  2550. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2551. break;
  2552. case SP_UNBREAKABLE_SHOES:
  2553. if(sd->state.lr_flag != 2)
  2554. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2555. break;
  2556. case SP_CLASSCHANGE: // [Valaris]
  2557. if(sd->state.lr_flag !=2)
  2558. sd->bonus.classchange=val;
  2559. break;
  2560. case SP_LONG_ATK_RATE:
  2561. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2562. sd->bonus.long_attack_atk_rate+=val;
  2563. break;
  2564. case SP_BREAK_WEAPON_RATE:
  2565. if(sd->state.lr_flag != 2)
  2566. sd->bonus.break_weapon_rate+=val;
  2567. break;
  2568. case SP_BREAK_ARMOR_RATE:
  2569. if(sd->state.lr_flag != 2)
  2570. sd->bonus.break_armor_rate+=val;
  2571. break;
  2572. case SP_ADD_STEAL_RATE:
  2573. if(sd->state.lr_flag != 2)
  2574. sd->bonus.add_steal_rate+=val;
  2575. break;
  2576. case SP_DELAYRATE:
  2577. if(sd->state.lr_flag != 2)
  2578. sd->delayrate+=val;
  2579. break;
  2580. case SP_CRIT_ATK_RATE:
  2581. if(sd->state.lr_flag != 2)
  2582. sd->bonus.crit_atk_rate += val;
  2583. break;
  2584. case SP_NO_REGEN:
  2585. if(sd->state.lr_flag != 2)
  2586. sd->regen.state.block|=val;
  2587. break;
  2588. case SP_UNSTRIPABLE_WEAPON:
  2589. if(sd->state.lr_flag != 2)
  2590. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2591. break;
  2592. case SP_UNSTRIPABLE:
  2593. case SP_UNSTRIPABLE_ARMOR:
  2594. if(sd->state.lr_flag != 2)
  2595. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2596. break;
  2597. case SP_UNSTRIPABLE_HELM:
  2598. if(sd->state.lr_flag != 2)
  2599. sd->bonus.unstripable_equip |= EQP_HELM;
  2600. break;
  2601. case SP_UNSTRIPABLE_SHIELD:
  2602. if(sd->state.lr_flag != 2)
  2603. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2604. break;
  2605. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2606. if(!sd->state.lr_flag) {
  2607. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2608. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2609. } else if(sd->state.lr_flag == 1) {
  2610. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2611. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2612. }
  2613. break;
  2614. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2615. if(!sd->state.lr_flag) {
  2616. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2617. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2618. } else if(sd->state.lr_flag == 1) {
  2619. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2620. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2621. }
  2622. break;
  2623. case SP_SP_GAIN_VALUE:
  2624. if(!sd->state.lr_flag)
  2625. sd->bonus.sp_gain_value += val;
  2626. break;
  2627. case SP_HP_GAIN_VALUE:
  2628. if(!sd->state.lr_flag)
  2629. sd->bonus.hp_gain_value += val;
  2630. break;
  2631. case SP_MAGIC_SP_GAIN_VALUE:
  2632. if(!sd->state.lr_flag)
  2633. sd->bonus.magic_sp_gain_value += val;
  2634. break;
  2635. case SP_MAGIC_HP_GAIN_VALUE:
  2636. if(!sd->state.lr_flag)
  2637. sd->bonus.magic_hp_gain_value += val;
  2638. break;
  2639. case SP_ADD_HEAL_RATE:
  2640. if(sd->state.lr_flag != 2)
  2641. sd->bonus.add_heal_rate += val;
  2642. break;
  2643. case SP_ADD_HEAL2_RATE:
  2644. if(sd->state.lr_flag != 2)
  2645. sd->bonus.add_heal2_rate += val;
  2646. break;
  2647. case SP_ADD_ITEM_HEAL_RATE:
  2648. if(sd->state.lr_flag != 2)
  2649. sd->bonus.itemhealrate2 += val;
  2650. break;
  2651. case SP_EMATK:
  2652. if(sd->state.lr_flag != 2)
  2653. sd->bonus.ematk += val;
  2654. break;
  2655. #ifdef RENEWAL_CAST
  2656. case SP_FIXCASTRATE:
  2657. if(sd->state.lr_flag != 2)
  2658. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2659. break;
  2660. case SP_ADD_FIXEDCAST:
  2661. if(sd->state.lr_flag != 2)
  2662. sd->bonus.add_fixcast += val;
  2663. break;
  2664. case SP_CASTRATE:
  2665. case SP_VARCASTRATE:
  2666. if(sd->state.lr_flag != 2)
  2667. sd->bonus.varcastrate -= val;
  2668. break;
  2669. case SP_ADD_VARIABLECAST:
  2670. if(sd->state.lr_flag != 2)
  2671. sd->bonus.add_varcast += val;
  2672. break;
  2673. #else
  2674. case SP_ADD_FIXEDCAST:
  2675. case SP_FIXCASTRATE:
  2676. case SP_ADD_VARIABLECAST:
  2677. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2678. break;
  2679. case SP_VARCASTRATE:
  2680. case SP_CASTRATE:
  2681. if(sd->state.lr_flag != 2)
  2682. sd->castrate += val;
  2683. break;
  2684. #endif
  2685. case SP_ADDMAXWEIGHT:
  2686. if (sd->state.lr_flag != 2)
  2687. sd->max_weight += val;
  2688. break;
  2689. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2690. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2691. break;
  2692. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2693. if (sd->state.lr_flag != 2)
  2694. sd->bonus.critical_rangeatk += val*10;
  2695. else
  2696. sd->bonus.arrow_cri += val*10;
  2697. break;
  2698. case SP_WEAPON_ATK_RATE:
  2699. if (sd->state.lr_flag != 2)
  2700. sd->bonus.weapon_atk_rate += val;
  2701. break;
  2702. case SP_WEAPON_MATK_RATE:
  2703. if (sd->state.lr_flag != 2)
  2704. sd->bonus.weapon_matk_rate += val;
  2705. break;
  2706. default:
  2707. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2708. break;
  2709. }
  2710. }
  2711. /*==========================================
  2712. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2713. * format: bonus2 bBonusName,type2,val;
  2714. * @param sd
  2715. * @param type Bonus type used by bBonusName
  2716. * @param type2
  2717. * @param val Value that usually for rate or fixed value
  2718. *------------------------------------------*/
  2719. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2720. {
  2721. int i;
  2722. nullpo_retv(sd);
  2723. switch(type){
  2724. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2725. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2726. if(!sd->state.lr_flag)
  2727. sd->right_weapon.addele[type2]+=val;
  2728. else if(sd->state.lr_flag == 1)
  2729. sd->left_weapon.addele[type2]+=val;
  2730. else if(sd->state.lr_flag == 2)
  2731. sd->arrow_addele[type2]+=val;
  2732. break;
  2733. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2734. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2735. if(!sd->state.lr_flag)
  2736. sd->right_weapon.addrace[type2]+=val;
  2737. else if(sd->state.lr_flag == 1)
  2738. sd->left_weapon.addrace[type2]+=val;
  2739. else if(sd->state.lr_flag == 2)
  2740. sd->arrow_addrace[type2]+=val;
  2741. break;
  2742. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2743. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2744. if(!sd->state.lr_flag)
  2745. sd->right_weapon.addclass[type2]+=val;
  2746. else if(sd->state.lr_flag == 1)
  2747. sd->left_weapon.addclass[type2]+=val;
  2748. else if(sd->state.lr_flag == 2)
  2749. sd->arrow_addclass[type2]+=val;
  2750. break;
  2751. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2752. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2753. if(!sd->state.lr_flag)
  2754. sd->right_weapon.addsize[type2]+=val;
  2755. else if(sd->state.lr_flag == 1)
  2756. sd->left_weapon.addsize[type2]+=val;
  2757. else if(sd->state.lr_flag == 2)
  2758. sd->arrow_addsize[type2]+=val;
  2759. break;
  2760. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2761. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2762. if(sd->state.lr_flag != 2)
  2763. sd->subele_script[type2] += val;
  2764. break;
  2765. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2766. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2767. if(sd->state.lr_flag != 2)
  2768. sd->subrace[type2]+=val;
  2769. break;
  2770. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2771. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2772. if(sd->state.lr_flag != 2)
  2773. sd->subclass[type2]+=val;
  2774. break;
  2775. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2776. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2777. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2778. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2779. break;
  2780. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2781. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2782. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2783. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2784. break;
  2785. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2786. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2787. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2788. break;
  2789. }
  2790. if(sd->state.lr_flag == 2)
  2791. break;
  2792. i = sd->reseff[type2]+val;
  2793. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2794. break;
  2795. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2796. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2797. if(sd->state.lr_flag != 2)
  2798. sd->magic_addele_script[type2] += val;
  2799. break;
  2800. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2801. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2802. if(sd->state.lr_flag != 2)
  2803. sd->magic_addrace[type2]+=val;
  2804. break;
  2805. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2806. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2807. if(sd->state.lr_flag != 2)
  2808. sd->magic_addclass[type2]+=val;
  2809. break;
  2810. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2811. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2812. if(sd->state.lr_flag != 2)
  2813. sd->magic_addsize[type2]+=val;
  2814. break;
  2815. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2816. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2817. if(sd->state.lr_flag != 2)
  2818. sd->magic_atk_ele[type2]+=val;
  2819. break;
  2820. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2821. switch (sd->state.lr_flag) {
  2822. case 0: //Right hand
  2823. 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);
  2824. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2825. {
  2826. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2827. break;
  2828. }
  2829. sd->right_weapon.add_dmg[i].class_ = type2;
  2830. sd->right_weapon.add_dmg[i].rate += val;
  2831. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2832. 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]));
  2833. break;
  2834. case 1: //Left hand
  2835. 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);
  2836. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2837. {
  2838. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2839. break;
  2840. }
  2841. sd->left_weapon.add_dmg[i].class_ = type2;
  2842. sd->left_weapon.add_dmg[i].rate += val;
  2843. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2844. 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]));
  2845. break;
  2846. }
  2847. break;
  2848. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2849. if(sd->state.lr_flag == 2)
  2850. break;
  2851. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2852. if (i == ARRAYLENGTH(sd->add_mdmg))
  2853. {
  2854. ShowError("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2855. break;
  2856. }
  2857. sd->add_mdmg[i].class_ = type2;
  2858. sd->add_mdmg[i].rate += val;
  2859. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2860. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2861. break;
  2862. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2863. if(sd->state.lr_flag == 2)
  2864. break;
  2865. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2866. if (i == ARRAYLENGTH(sd->add_def))
  2867. {
  2868. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2869. break;
  2870. }
  2871. sd->add_def[i].class_ = type2;
  2872. sd->add_def[i].rate += val;
  2873. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2874. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2875. break;
  2876. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2877. if(sd->state.lr_flag == 2)
  2878. break;
  2879. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2880. if (i == ARRAYLENGTH(sd->add_mdef))
  2881. {
  2882. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2883. break;
  2884. }
  2885. sd->add_mdef[i].class_ = type2;
  2886. sd->add_mdef[i].rate += val;
  2887. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2888. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2889. break;
  2890. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2891. if(!sd->state.lr_flag) {
  2892. sd->right_weapon.hp_drain_rate.rate += type2;
  2893. sd->right_weapon.hp_drain_rate.per += val;
  2894. }
  2895. else if(sd->state.lr_flag == 1) {
  2896. sd->left_weapon.hp_drain_rate.rate += type2;
  2897. sd->left_weapon.hp_drain_rate.per += val;
  2898. }
  2899. break;
  2900. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2901. if(!sd->state.lr_flag) {
  2902. sd->right_weapon.sp_drain_rate.rate += type2;
  2903. sd->right_weapon.sp_drain_rate.per += val;
  2904. }
  2905. else if(sd->state.lr_flag == 1) {
  2906. sd->left_weapon.sp_drain_rate.rate += type2;
  2907. sd->left_weapon.sp_drain_rate.per += val;
  2908. }
  2909. break;
  2910. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2911. if(sd->state.lr_flag != 2) {
  2912. sd->bonus.sp_vanish_rate += type2;
  2913. sd->bonus.sp_vanish_per += val;
  2914. }
  2915. break;
  2916. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2917. if(sd->state.lr_flag != 2) {
  2918. sd->bonus.hp_vanish_rate += type2;
  2919. sd->bonus.hp_vanish_per += val;
  2920. }
  2921. break;
  2922. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2923. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2924. sd->bonus.get_zeny_rate = val;
  2925. sd->bonus.get_zeny_num = type2;
  2926. }
  2927. break;
  2928. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  2929. if(sd->state.lr_flag != 2) {
  2930. sd->bonus.get_zeny_rate += val;
  2931. sd->bonus.get_zeny_num += type2;
  2932. }
  2933. break;
  2934. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  2935. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  2936. if(sd->state.lr_flag == 2)
  2937. break;
  2938. sd->weapon_coma_ele[type2] += val;
  2939. sd->special_state.bonus_coma = 1;
  2940. break;
  2941. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  2942. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  2943. if(sd->state.lr_flag == 2)
  2944. break;
  2945. sd->weapon_coma_race[type2] += val;
  2946. sd->special_state.bonus_coma = 1;
  2947. break;
  2948. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  2949. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  2950. if(sd->state.lr_flag == 2)
  2951. break;
  2952. sd->weapon_coma_class[type2] += val;
  2953. sd->special_state.bonus_coma = 1;
  2954. break;
  2955. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  2956. if(sd->state.lr_flag != 2)
  2957. sd->weapon_atk[type2]+=val;
  2958. break;
  2959. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  2960. if(sd->state.lr_flag != 2)
  2961. sd->weapon_damage_rate[type2]+=val;
  2962. break;
  2963. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  2964. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  2965. if(sd->state.lr_flag != 2)
  2966. sd->critaddrace[type2] += val*10;
  2967. break;
  2968. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  2969. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  2970. if(sd->state.lr_flag != 2)
  2971. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  2972. break;
  2973. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  2974. if(sd->state.lr_flag == 2)
  2975. break;
  2976. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2977. if (i == ARRAYLENGTH(sd->skillatk))
  2978. { //Better mention this so the array length can be updated. [Skotlex]
  2979. ShowError("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  2980. break;
  2981. }
  2982. if (sd->skillatk[i].id == type2)
  2983. sd->skillatk[i].val += val;
  2984. else {
  2985. sd->skillatk[i].id = type2;
  2986. sd->skillatk[i].val = val;
  2987. }
  2988. break;
  2989. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  2990. if(sd->state.lr_flag == 2)
  2991. break;
  2992. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2993. if (i == ARRAYLENGTH(sd->skillheal))
  2994. { // Better mention this so the array length can be updated. [Skotlex]
  2995. ShowError("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  2996. break;
  2997. }
  2998. if (sd->skillheal[i].id == type2)
  2999. sd->skillheal[i].val += val;
  3000. else {
  3001. sd->skillheal[i].id = type2;
  3002. sd->skillheal[i].val = val;
  3003. }
  3004. break;
  3005. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3006. if(sd->state.lr_flag == 2)
  3007. break;
  3008. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3009. if (i == ARRAYLENGTH(sd->skillheal2))
  3010. { // Better mention this so the array length can be updated. [Skotlex]
  3011. ShowError("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  3012. break;
  3013. }
  3014. if (sd->skillheal2[i].id == type2)
  3015. sd->skillheal2[i].val += val;
  3016. else {
  3017. sd->skillheal2[i].id = type2;
  3018. sd->skillheal2[i].val = val;
  3019. }
  3020. break;
  3021. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3022. if(sd->state.lr_flag == 2)
  3023. break;
  3024. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3025. if (i == ARRAYLENGTH(sd->skillblown))
  3026. { //Better mention this so the array length can be updated. [Skotlex]
  3027. ShowError("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  3028. break;
  3029. }
  3030. if(sd->skillblown[i].id == type2)
  3031. sd->skillblown[i].val += val;
  3032. else {
  3033. sd->skillblown[i].id = type2;
  3034. sd->skillblown[i].val = val;
  3035. }
  3036. break;
  3037. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3038. if(sd->state.lr_flag != 2) {
  3039. sd->hp_loss.value = type2;
  3040. sd->hp_loss.rate = val;
  3041. }
  3042. break;
  3043. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3044. if(sd->state.lr_flag != 2) {
  3045. sd->hp_regen.value = type2;
  3046. sd->hp_regen.rate = val;
  3047. }
  3048. break;
  3049. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3050. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3051. if(sd->state.lr_flag != 2)
  3052. sd->right_weapon.addrace2[type2] += val;
  3053. else
  3054. sd->left_weapon.addrace2[type2] += val;
  3055. break;
  3056. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3057. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3058. if(sd->state.lr_flag != 2)
  3059. sd->subsize[type2]+=val;
  3060. break;
  3061. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3062. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3063. if(sd->state.lr_flag != 2)
  3064. sd->subrace2[type2]+=val;
  3065. break;
  3066. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3067. if(sd->state.lr_flag == 2)
  3068. break;
  3069. if (!itemdb_exists(type2)) {
  3070. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3071. break;
  3072. }
  3073. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3074. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3075. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3076. break;
  3077. }
  3078. sd->itemhealrate[i].nameid = type2;
  3079. sd->itemhealrate[i].rate += val;
  3080. break;
  3081. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3082. if (sd->state.lr_flag == 2)
  3083. break;
  3084. if (!type2 || !itemdb_group_exists(type2)) {
  3085. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3086. break;
  3087. }
  3088. pc_itemgrouphealrate(sd, type2, val);
  3089. break;
  3090. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3091. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3092. if(sd->state.lr_flag != 2)
  3093. sd->expaddrace[type2]+=val;
  3094. break;
  3095. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3096. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3097. if(sd->state.lr_flag != 2)
  3098. sd->expaddclass[type2]+=val;
  3099. break;
  3100. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3101. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3102. if(sd->state.lr_flag != 2)
  3103. sd->sp_gain_race[type2]+=val;
  3104. break;
  3105. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3106. if (sd->state.lr_flag != 2)
  3107. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3108. break;
  3109. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3110. if (sd->state.lr_flag != 2)
  3111. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3112. break;
  3113. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3114. if(sd->state.lr_flag != 2) {
  3115. sd->sp_loss.value = type2;
  3116. sd->sp_loss.rate = val;
  3117. }
  3118. break;
  3119. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3120. if(sd->state.lr_flag != 2) {
  3121. sd->sp_regen.value = type2;
  3122. sd->sp_regen.rate = val;
  3123. }
  3124. break;
  3125. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3126. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3127. if(!sd->state.lr_flag) {
  3128. sd->right_weapon.hp_drain_race[type2] += val;
  3129. }
  3130. else if(sd->state.lr_flag == 1) {
  3131. sd->left_weapon.hp_drain_race[type2] += val;
  3132. }
  3133. break;
  3134. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3135. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3136. if(!sd->state.lr_flag) {
  3137. sd->right_weapon.sp_drain_race[type2] += val;
  3138. }
  3139. else if(sd->state.lr_flag == 1) {
  3140. sd->left_weapon.sp_drain_race[type2] += val;
  3141. }
  3142. break;
  3143. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3144. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3145. if(!sd->state.lr_flag) {
  3146. sd->right_weapon.hp_drain_class[type2] += val;
  3147. }
  3148. else if(sd->state.lr_flag == 1) {
  3149. sd->left_weapon.hp_drain_class[type2] += val;
  3150. }
  3151. break;
  3152. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3153. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3154. if(!sd->state.lr_flag) {
  3155. sd->right_weapon.sp_drain_class[type2] += val;
  3156. }
  3157. else if(sd->state.lr_flag == 1) {
  3158. sd->left_weapon.sp_drain_class[type2] += val;
  3159. }
  3160. break;
  3161. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3162. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3163. if(sd->state.lr_flag != 2)
  3164. sd->ignore_mdef_by_race[type2] += val;
  3165. break;
  3166. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3167. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3168. if(sd->state.lr_flag != 2)
  3169. sd->ignore_mdef_by_class[type2] += val;
  3170. break;
  3171. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3172. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3173. if(sd->state.lr_flag != 2)
  3174. sd->ignore_def_by_race[type2] += val;
  3175. break;
  3176. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3177. if(sd->state.lr_flag == 2)
  3178. break;
  3179. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3180. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3181. ShowError("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  3182. break;
  3183. }
  3184. if (sd->skillusesprate[i].id == type2)
  3185. sd->skillusesprate[i].val += val;
  3186. else {
  3187. sd->skillusesprate[i].id = type2;
  3188. sd->skillusesprate[i].val = val;
  3189. }
  3190. break;
  3191. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3192. if(sd->state.lr_flag == 2)
  3193. break;
  3194. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3195. if (i == ARRAYLENGTH(sd->skillcooldown))
  3196. {
  3197. ShowError("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  3198. break;
  3199. }
  3200. if (sd->skillcooldown[i].id == type2)
  3201. sd->skillcooldown[i].val += val;
  3202. else {
  3203. sd->skillcooldown[i].id = type2;
  3204. sd->skillcooldown[i].val = val;
  3205. }
  3206. break;
  3207. #ifdef RENEWAL_CAST
  3208. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3209. if(sd->state.lr_flag == 2)
  3210. break;
  3211. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3212. if (i == ARRAYLENGTH(sd->skillfixcast))
  3213. {
  3214. ShowError("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  3215. break;
  3216. }
  3217. if (sd->skillfixcast[i].id == type2)
  3218. sd->skillfixcast[i].val += val;
  3219. else {
  3220. sd->skillfixcast[i].id = type2;
  3221. sd->skillfixcast[i].val = val;
  3222. }
  3223. break;
  3224. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3225. if(sd->state.lr_flag == 2)
  3226. break;
  3227. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3228. if (i == ARRAYLENGTH(sd->skillvarcast))
  3229. {
  3230. ShowError("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  3231. break;
  3232. }
  3233. if (sd->skillvarcast[i].id == type2)
  3234. sd->skillvarcast[i].val += val;
  3235. else {
  3236. sd->skillvarcast[i].id = type2;
  3237. sd->skillvarcast[i].val = val;
  3238. }
  3239. break;
  3240. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3241. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3242. if(sd->state.lr_flag == 2)
  3243. break;
  3244. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3245. if (i == ARRAYLENGTH(sd->skillcastrate))
  3246. {
  3247. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3248. break;
  3249. }
  3250. if(sd->skillcastrate[i].id == type2)
  3251. sd->skillcastrate[i].val -= val;
  3252. else {
  3253. sd->skillcastrate[i].id = type2;
  3254. sd->skillcastrate[i].val -= val;
  3255. }
  3256. break;
  3257. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3258. if(sd->state.lr_flag == 2)
  3259. break;
  3260. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3261. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3262. {
  3263. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3264. break;
  3265. }
  3266. if(sd->skillfixcastrate[i].id == type2)
  3267. sd->skillfixcastrate[i].val -= val;
  3268. else {
  3269. sd->skillfixcastrate[i].id = type2;
  3270. sd->skillfixcastrate[i].val -= val;
  3271. }
  3272. break;
  3273. #else
  3274. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3275. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3276. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3277. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3278. break;
  3279. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3280. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3281. if(sd->state.lr_flag == 2)
  3282. break;
  3283. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3284. if (i == ARRAYLENGTH(sd->skillcastrate))
  3285. { //Better mention this so the array length can be updated. [Skotlex]
  3286. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3287. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3288. break;
  3289. }
  3290. if(sd->skillcastrate[i].id == type2)
  3291. sd->skillcastrate[i].val += val;
  3292. else {
  3293. sd->skillcastrate[i].id = type2;
  3294. sd->skillcastrate[i].val = val;
  3295. }
  3296. break;
  3297. #endif
  3298. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3299. if(sd->state.lr_flag == 2)
  3300. break;
  3301. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3302. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3303. ShowError("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  3304. break;
  3305. }
  3306. if (sd->skillusesp[i].id == type2)
  3307. sd->skillusesp[i].val += val;
  3308. else {
  3309. sd->skillusesp[i].id = type2;
  3310. sd->skillusesp[i].val = val;
  3311. }
  3312. break;
  3313. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3314. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3315. if (i == ARRAYLENGTH(sd->subskill)) {
  3316. ShowError("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->subskill), type2, val);
  3317. break;
  3318. }
  3319. if (sd->subskill[i].id == type2)
  3320. sd->subskill[i].val += val;
  3321. else {
  3322. sd->subskill[i].id = type2;
  3323. sd->subskill[i].val = val;
  3324. }
  3325. break;
  3326. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3327. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3328. sd->subdefele[type2] += val;
  3329. break;
  3330. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3331. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3332. sd->coma_class[type2] += val;
  3333. sd->special_state.bonus_coma = 1;
  3334. break;
  3335. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3336. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3337. sd->coma_race[type2] += val;
  3338. sd->special_state.bonus_coma = 1;
  3339. break;
  3340. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3341. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3342. if(sd->state.lr_flag != 2)
  3343. sd->magic_addrace2[type2] += val;
  3344. break;
  3345. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3346. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3347. if (sd->state.lr_flag != 2)
  3348. sd->ignore_mdef_by_race2[type2] += val;
  3349. break;
  3350. default:
  3351. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  3352. break;
  3353. }
  3354. }
  3355. /**
  3356. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3357. * @param sd
  3358. * @param type Bonus type used by bBonusName
  3359. * @param type2
  3360. * @param val Value that usually for rate or fixed value
  3361. */
  3362. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3363. {
  3364. nullpo_retv(sd);
  3365. switch(type){
  3366. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3367. if(sd->state.lr_flag != 2)
  3368. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3369. break;
  3370. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3371. if(sd->state.lr_flag != 2)
  3372. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3373. break;
  3374. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3375. if(sd->state.lr_flag != 2)
  3376. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3377. break;
  3378. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3379. if(sd->state.lr_flag != 2)
  3380. {
  3381. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3382. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3383. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3384. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3385. }
  3386. break;
  3387. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3388. if(sd->state.lr_flag != 2)
  3389. {
  3390. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3391. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3392. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3393. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3394. }
  3395. break;
  3396. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3397. if (sd->state.lr_flag != 2)
  3398. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3399. break;
  3400. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3401. if (sd->state.lr_flag != 2)
  3402. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3403. break;
  3404. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3405. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3406. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3407. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3408. break;
  3409. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3410. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3411. if(sd->state.lr_flag != 2)
  3412. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3413. break;
  3414. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3415. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3416. if( sd->state.lr_flag != 2 )
  3417. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3418. break;
  3419. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3420. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3421. if (sd->state.lr_flag != 2)
  3422. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3423. break;
  3424. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3425. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3426. if (sd->state.lr_flag != 2)
  3427. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3428. break;
  3429. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3430. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3431. if(sd->state.lr_flag != 2) {
  3432. sd->sp_vanish_race[type2].rate += type3;
  3433. sd->sp_vanish_race[type2].per += val;
  3434. }
  3435. break;
  3436. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3437. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3438. if(sd->state.lr_flag != 2) {
  3439. sd->hp_vanish_race[type2].rate += type3;
  3440. sd->hp_vanish_race[type2].per += val;
  3441. }
  3442. break;
  3443. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3444. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3445. if (sd->state.lr_flag == 2)
  3446. break;
  3447. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3448. //if (type3 > sd->norecover_state_race[type2].rate) {
  3449. // sd->norecover_state_race[type2].rate = type3;
  3450. // sd->norecover_state_race[type2].tick = val;
  3451. // break;
  3452. //}
  3453. sd->norecover_state_race[type2].rate = type3;
  3454. sd->norecover_state_race[type2].tick = val;
  3455. break;
  3456. default:
  3457. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3458. break;
  3459. }
  3460. }
  3461. /**
  3462. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3463. * @param sd
  3464. * @param type Bonus type used by bBonusName
  3465. * @param type2
  3466. * @param type3
  3467. * @param val Value that usually for rate or fixed value
  3468. */
  3469. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3470. {
  3471. nullpo_retv(sd);
  3472. switch(type){
  3473. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3474. if(sd->state.lr_flag != 2)
  3475. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3476. break;
  3477. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3478. if(sd->state.lr_flag != 2)
  3479. 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);
  3480. break;
  3481. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3482. if(sd->state.lr_flag != 2)
  3483. {
  3484. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3485. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3486. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3487. }
  3488. break;
  3489. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3490. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3491. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3492. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3493. break;
  3494. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3495. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3496. if (sd->state.lr_flag != 2)
  3497. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3498. break;
  3499. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3500. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3501. if( sd->state.lr_flag != 2 )
  3502. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3503. break;
  3504. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3505. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3506. if(sd->state.lr_flag == 2)
  3507. break;
  3508. sd->def_set_race[type2].rate = type3;
  3509. sd->def_set_race[type2].tick = type4;
  3510. sd->def_set_race[type2].value = val;
  3511. break;
  3512. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3513. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3514. if(sd->state.lr_flag == 2)
  3515. break;
  3516. sd->mdef_set_race[type2].rate = type3;
  3517. sd->mdef_set_race[type2].tick = type4;
  3518. sd->mdef_set_race[type2].value = val;
  3519. break;
  3520. default:
  3521. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3522. break;
  3523. }
  3524. }
  3525. /**
  3526. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3527. * @param sd
  3528. * @param type Bonus type used by bBonusName
  3529. * @param type2
  3530. * @param type3
  3531. * @param type4
  3532. * @param val Value that usually for rate or fixed value
  3533. */
  3534. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3535. {
  3536. nullpo_retv(sd);
  3537. switch(type){
  3538. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3539. if(sd->state.lr_flag != 2)
  3540. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3541. break;
  3542. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3543. if(sd->state.lr_flag != 2)
  3544. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3545. break;
  3546. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3547. if(sd->state.lr_flag != 2)
  3548. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3549. break;
  3550. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3551. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3552. if( sd->state.lr_flag != 2 )
  3553. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3554. break;
  3555. default:
  3556. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3557. break;
  3558. }
  3559. }
  3560. /*==========================================
  3561. * Grants a player a given skill. Flag values are:
  3562. * 0 - Grant permanent skill to be bound to skill tree
  3563. * 1 - Grant an item skill (temporary)
  3564. * 2 - Like 1, except the level granted can stack with previously learned level.
  3565. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3566. *------------------------------------------*/
  3567. bool pc_skill(TBL_PC* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3568. uint16 idx = 0;
  3569. nullpo_ret(sd);
  3570. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3571. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3572. return false;
  3573. }
  3574. if (level > MAX_SKILL_LEVEL) {
  3575. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3576. return false;
  3577. }
  3578. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3579. ShowWarning("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d. Set to max level.\n", level, MAX_SKILL_LEVEL, sd->status.skill[idx].lv);
  3580. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3581. }
  3582. switch (type) {
  3583. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3584. sd->status.skill[idx].id = skill_id;
  3585. sd->status.skill[idx].lv = level;
  3586. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3587. if (level == 0) { //Remove skill.
  3588. sd->status.skill[idx].id = 0;
  3589. clif_deleteskill(sd,skill_id);
  3590. } else
  3591. clif_addskill(sd,skill_id);
  3592. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3593. status_calc_pc(sd, SCO_NONE);
  3594. break;
  3595. case ADDSKILL_TEMP: //Item bonus skill.
  3596. if (sd->status.skill[idx].id != 0) {
  3597. if (sd->status.skill[idx].lv >= level)
  3598. return true;
  3599. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3600. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3601. } else {
  3602. sd->status.skill[idx].id = skill_id;
  3603. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3604. }
  3605. sd->status.skill[idx].lv = level;
  3606. break;
  3607. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3608. if (sd->status.skill[idx].id != 0) {
  3609. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3610. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3611. } else {
  3612. sd->status.skill[idx].id = skill_id;
  3613. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3614. }
  3615. sd->status.skill[idx].lv += level;
  3616. break;
  3617. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3618. sd->status.skill[idx].id = skill_id;
  3619. sd->status.skill[idx].lv = level;
  3620. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3621. if (level == 0) { //Remove skill.
  3622. sd->status.skill[idx].id = 0;
  3623. clif_deleteskill(sd,skill_id);
  3624. } else
  3625. clif_addskill(sd,skill_id);
  3626. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3627. status_calc_pc(sd, SCO_NONE);
  3628. break;
  3629. default:
  3630. return false;
  3631. }
  3632. return true;
  3633. }
  3634. /*==========================================
  3635. * Append a card to an item ?
  3636. *------------------------------------------*/
  3637. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3638. {
  3639. int i;
  3640. unsigned short nameid;
  3641. nullpo_ret(sd);
  3642. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3643. return 0; //Invalid item index.
  3644. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3645. return 0; //Invalid card index.
  3646. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3647. return 0; // target item missing
  3648. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3649. return 0; // target card missing
  3650. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3651. return 0; // only weapons and armor are allowed
  3652. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3653. return 0; // must be a card
  3654. if( sd->status.inventory[idx_equip].identify == 0 )
  3655. return 0; // target must be identified
  3656. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3657. return 0; // card slots reserved for other purposes
  3658. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3659. return 0; // card cannot be compounded on this item type
  3660. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3661. return 0; // attempted to place shield card on left-hand weapon.
  3662. if( sd->status.inventory[idx_equip].equip != 0 )
  3663. return 0; // item must be unequipped
  3664. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3665. if( i == sd->inventory_data[idx_equip]->slot )
  3666. return 0; // no free slots
  3667. // remember the card id to insert
  3668. nameid = sd->status.inventory[idx_card].nameid;
  3669. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3670. {// failed
  3671. clif_insert_card(sd,idx_equip,idx_card,1);
  3672. }
  3673. else
  3674. {// success
  3675. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3676. sd->status.inventory[idx_equip].card[i] = nameid;
  3677. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3678. clif_insert_card(sd,idx_equip,idx_card,0);
  3679. }
  3680. return 0;
  3681. }
  3682. //
  3683. // Items
  3684. //
  3685. /*==========================================
  3686. * Update buying value by skills
  3687. *------------------------------------------*/
  3688. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3689. {
  3690. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3691. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3692. rate1 = 5+skill*2-((skill==10)? 1:0);
  3693. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3694. rate2 = 5+skill*4;
  3695. if(rate1 < rate2) rate1 = rate2;
  3696. if(rate1)
  3697. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3698. if(val < 0) val = 0;
  3699. if(orig_value > 0 && val < 1) val = 1;
  3700. return val;
  3701. }
  3702. /*==========================================
  3703. * Update selling value by skills
  3704. *------------------------------------------*/
  3705. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3706. {
  3707. int skill,val = orig_value,rate = 0;
  3708. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3709. rate = 5+skill*2-((skill==10)? 1:0);
  3710. if(rate)
  3711. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3712. if(val < 0) val = 0;
  3713. if(orig_value > 0 && val < 1) val = 1;
  3714. return val;
  3715. }
  3716. /*==========================================
  3717. * Checking if we have enough place on inventory for new item
  3718. * Make sure to take 30k as limit (for client I guess)
  3719. * @param sd
  3720. * @param nameid
  3721. * @param amount
  3722. * @return e_chkitem_result
  3723. *------------------------------------------*/
  3724. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3725. {
  3726. int i;
  3727. struct item_data* data;
  3728. nullpo_ret(sd);
  3729. if(amount > MAX_AMOUNT)
  3730. return CHKADDITEM_OVERAMOUNT;
  3731. data = itemdb_search(nameid);
  3732. if(!itemdb_isstackable2(data))
  3733. return CHKADDITEM_NEW;
  3734. if( data->stack.inventory && amount > data->stack.amount )
  3735. return CHKADDITEM_OVERAMOUNT;
  3736. for(i=0;i<MAX_INVENTORY;i++){
  3737. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3738. if(sd->status.inventory[i].nameid==nameid){
  3739. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3740. return CHKADDITEM_OVERAMOUNT;
  3741. return CHKADDITEM_EXIST;
  3742. }
  3743. }
  3744. return CHKADDITEM_NEW;
  3745. }
  3746. /*==========================================
  3747. * Return number of available place in inventory
  3748. * Each non stackable item will reduce place by 1
  3749. * @param sd
  3750. * @return Number of empty slots
  3751. *------------------------------------------*/
  3752. uint8 pc_inventoryblank(struct map_session_data *sd)
  3753. {
  3754. uint8 i, b;
  3755. nullpo_ret(sd);
  3756. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3757. if(sd->status.inventory[i].nameid==0)
  3758. b++;
  3759. }
  3760. return b;
  3761. }
  3762. /**
  3763. * Attempts to remove zeny from player
  3764. * @param sd: Player
  3765. * @param zeny: Zeny removed
  3766. * @param type: Log type
  3767. * @param tsd: (optional) From who to log (if null take sd)
  3768. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3769. */
  3770. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3771. {
  3772. nullpo_retr(2,sd);
  3773. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3774. if( zeny < 0 )
  3775. {
  3776. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3777. return 1;
  3778. }
  3779. if( sd->status.zeny < zeny )
  3780. return 1; //Not enough.
  3781. sd->status.zeny -= zeny;
  3782. clif_updatestatus(sd,SP_ZENY);
  3783. if(!tsd) tsd = sd;
  3784. log_zeny(sd, type, tsd, -zeny);
  3785. if( zeny > 0 && sd->state.showzeny ) {
  3786. char output[255];
  3787. sprintf(output, "Removed %dz.", zeny);
  3788. clif_disp_onlyself(sd,output,strlen(output));
  3789. }
  3790. return 0;
  3791. }
  3792. /**
  3793. * Attempts to give zeny to player
  3794. * @param sd: Player
  3795. * @param type: Log type
  3796. * @param tsd: (optional) From who to log (if null take sd)
  3797. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3798. */
  3799. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3800. {
  3801. nullpo_retr(-1,sd);
  3802. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3803. if( zeny < 0 )
  3804. {
  3805. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3806. return 1;
  3807. }
  3808. if( zeny > MAX_ZENY - sd->status.zeny )
  3809. zeny = MAX_ZENY - sd->status.zeny;
  3810. sd->status.zeny += zeny;
  3811. clif_updatestatus(sd,SP_ZENY);
  3812. if(!tsd) tsd = sd;
  3813. log_zeny(sd, type, tsd, zeny);
  3814. if( zeny > 0 && sd->state.showzeny ) {
  3815. char output[255];
  3816. sprintf(output, "Gained %dz.", zeny);
  3817. clif_disp_onlyself(sd,output,strlen(output));
  3818. }
  3819. return 0;
  3820. }
  3821. /**
  3822. * Attempts to remove Cash Points from player
  3823. * @param sd: Player
  3824. * @param price: Points player has to pay
  3825. * @param points: Points player has
  3826. * @param type: Log type
  3827. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3828. */
  3829. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3830. {
  3831. int cash;
  3832. nullpo_retr(-1,sd);
  3833. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3834. if( price < 0 || points < 0 )
  3835. {
  3836. 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);
  3837. return -2;
  3838. }
  3839. if( points > price )
  3840. {
  3841. 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);
  3842. points = price;
  3843. }
  3844. cash = price-points;
  3845. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3846. {
  3847. 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);
  3848. return -1;
  3849. }
  3850. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints-cash);
  3851. if( cash ){
  3852. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3853. }
  3854. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints-points);
  3855. if( points ){
  3856. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3857. }
  3858. if( battle_config.cashshop_show_points )
  3859. {
  3860. char output[CHAT_SIZE_MAX];
  3861. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3862. clif_disp_onlyself(sd, output, strlen(output));
  3863. }
  3864. return cash+points;
  3865. }
  3866. /**
  3867. * Attempts to give Cash Points to player
  3868. * @param sd: Player
  3869. * @param cash: Cash player gets
  3870. * @param points: Points player has
  3871. * @param type: Log type
  3872. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  3873. */
  3874. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  3875. {
  3876. char output[CHAT_SIZE_MAX];
  3877. nullpo_retr(-1,sd);
  3878. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3879. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3880. if( cash > 0 )
  3881. {
  3882. if( cash > MAX_ZENY-sd->cashPoints )
  3883. {
  3884. 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);
  3885. cash = MAX_ZENY-sd->cashPoints;
  3886. }
  3887. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints+cash);
  3888. if( cash ){
  3889. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3890. }
  3891. if( battle_config.cashshop_show_points )
  3892. {
  3893. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3894. clif_disp_onlyself(sd, output, strlen(output));
  3895. }
  3896. return cash;
  3897. }
  3898. else if( cash < 0 )
  3899. {
  3900. 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);
  3901. return -1;
  3902. }
  3903. if( points > 0 )
  3904. {
  3905. if( points > MAX_ZENY-sd->kafraPoints )
  3906. {
  3907. 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);
  3908. points = MAX_ZENY-sd->kafraPoints;
  3909. }
  3910. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints+points);
  3911. if( points ){
  3912. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3913. }
  3914. if( battle_config.cashshop_show_points )
  3915. {
  3916. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3917. clif_disp_onlyself(sd, output, strlen(output));
  3918. }
  3919. return points;
  3920. }
  3921. else if( points < 0 )
  3922. {
  3923. 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);
  3924. return -1;
  3925. }
  3926. return -2; //shouldn't happen but just in case
  3927. }
  3928. /**
  3929. * Searching a specified itemid in inventory and return his stored index
  3930. * @param sd Player
  3931. * @param nameid Find this Item!
  3932. * @return Stored index in inventory, or -1 if not found.
  3933. **/
  3934. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  3935. short i;
  3936. nullpo_retr(-1, sd);
  3937. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && (sd->status.inventory[i].amount > 0 || nameid == 0) );
  3938. return ( i < MAX_INVENTORY ) ? i : -1;
  3939. }
  3940. /** Attempt to add a new item to player inventory
  3941. * @param sd
  3942. * @param item_data
  3943. * @param amount
  3944. * @param log_type
  3945. * @return
  3946. * 0 = success
  3947. * 1 = invalid itemid not found or negative amount
  3948. * 2 = overweight
  3949. * 3 = ?
  3950. * 4 = no free place found
  3951. * 5 = max amount reached
  3952. * 6 = ?
  3953. * 7 = stack limitation
  3954. */
  3955. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  3956. struct item_data *id;
  3957. int16 i;
  3958. unsigned int w;
  3959. nullpo_retr(1, sd);
  3960. nullpo_retr(1, item);
  3961. if( item->nameid == 0 || amount <= 0 )
  3962. return ADDITEM_INVALID;
  3963. if( amount > MAX_AMOUNT )
  3964. return ADDITEM_OVERAMOUNT;
  3965. id = itemdb_search(item->nameid);
  3966. if( id->stack.inventory && amount > id->stack.amount )
  3967. {// item stack limitation
  3968. return ADDITEM_STACKLIMIT;
  3969. }
  3970. w = id->weight*amount;
  3971. if(sd->weight + w > sd->max_weight)
  3972. return ADDITEM_OVERWEIGHT;
  3973. i = MAX_INVENTORY;
  3974. if (id->flag.guid && !item->unique_id)
  3975. item->unique_id = pc_generate_unique_id(sd);
  3976. // Stackable | Non Rental
  3977. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  3978. for( i = 0; i < MAX_INVENTORY; i++ ) {
  3979. if( sd->status.inventory[i].nameid == item->nameid &&
  3980. sd->status.inventory[i].bound == item->bound &&
  3981. sd->status.inventory[i].expire_time == 0 &&
  3982. sd->status.inventory[i].unique_id == item->unique_id &&
  3983. memcmp(&sd->status.inventory[i].card, &item->card, sizeof(item->card)) == 0
  3984. )
  3985. {
  3986. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->status.inventory[i].amount ) )
  3987. return ADDITEM_OVERAMOUNT;
  3988. sd->status.inventory[i].amount += amount;
  3989. clif_additem(sd,i,amount,0);
  3990. break;
  3991. }
  3992. }
  3993. }
  3994. if (i >= MAX_INVENTORY) {
  3995. i = pc_search_inventory(sd,0);
  3996. if( i < 0 )
  3997. return ADDITEM_OVERITEM;
  3998. memcpy(&sd->status.inventory[i], item, sizeof(sd->status.inventory[0]));
  3999. // clear equip and favorite fields first, just in case
  4000. if( item->equip )
  4001. sd->status.inventory[i].equip = 0;
  4002. if( item->favorite )
  4003. sd->status.inventory[i].favorite = 0;
  4004. sd->status.inventory[i].amount = amount;
  4005. sd->inventory_data[i] = id;
  4006. sd->last_addeditem_index = i;
  4007. if (!itemdb_isstackable2(id) || id->flag.guid)
  4008. sd->status.inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4009. clif_additem(sd,i,amount,0);
  4010. }
  4011. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  4012. sd->weight += w;
  4013. clif_updatestatus(sd,SP_WEIGHT);
  4014. //Auto-equip
  4015. if(id->flag.autoequip)
  4016. pc_equipitem(sd, i, id->equip);
  4017. /* rental item check */
  4018. if( item->expire_time ) {
  4019. if( time(NULL) > item->expire_time ) {
  4020. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  4021. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4022. } else {
  4023. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4024. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  4025. pc_inventory_rental_add(sd, seconds);
  4026. }
  4027. }
  4028. return ADDITEM_SUCCESS;
  4029. }
  4030. /*==========================================
  4031. * Remove an item at index n from inventory by amount.
  4032. * @param sd
  4033. * @param n Item index in inventory
  4034. * @param amount
  4035. * @param type &1: Don't notify deletion; &2 Don't notify weight change
  4036. * @param reason Delete reason
  4037. * @param log_type e_log_pick_type
  4038. * @return 1 - invalid itemid or negative amount; 0 - Success
  4039. *------------------------------------------*/
  4040. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4041. {
  4042. nullpo_retr(1, sd);
  4043. if(n < 0 || sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4044. return 1;
  4045. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  4046. sd->status.inventory[n].amount -= amount;
  4047. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4048. if( sd->status.inventory[n].amount <= 0 ){
  4049. if(sd->status.inventory[n].equip)
  4050. pc_unequipitem(sd,n,3);
  4051. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  4052. sd->inventory_data[n] = NULL;
  4053. }
  4054. if(!(type&1))
  4055. clif_delitem(sd,n,amount,reason);
  4056. if(!(type&2))
  4057. clif_updatestatus(sd,SP_WEIGHT);
  4058. return 0;
  4059. }
  4060. /*==========================================
  4061. * Attempt to drop an item.
  4062. * @param sd
  4063. * @param n Item index in inventory
  4064. * @param amount Amount of item
  4065. * @return False = fail; True = success
  4066. *------------------------------------------*/
  4067. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4068. {
  4069. nullpo_retr(1, sd);
  4070. if(n < 0 || n >= MAX_INVENTORY)
  4071. return false;
  4072. if(amount <= 0)
  4073. return false;
  4074. if(sd->status.inventory[n].nameid <= 0 ||
  4075. sd->status.inventory[n].amount <= 0 ||
  4076. sd->status.inventory[n].amount < amount ||
  4077. sd->state.trading || sd->state.vending ||
  4078. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4079. )
  4080. return false;
  4081. if( map[sd->bl.m].flag.nodrop )
  4082. {
  4083. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4084. return false; //Can't drop items in nodrop mapflag maps.
  4085. }
  4086. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  4087. {
  4088. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4089. return false;
  4090. }
  4091. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4092. return false;
  4093. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4094. clif_dropitem(sd, n, amount);
  4095. return true;
  4096. }
  4097. /*==========================================
  4098. * Attempt to pick up an item.
  4099. * @param sd
  4100. * @param fitem Item that will be picked
  4101. * @return False = fail; True = success
  4102. *------------------------------------------*/
  4103. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4104. {
  4105. int flag = 0;
  4106. unsigned int tick = gettick();
  4107. struct party_data *p = NULL;
  4108. nullpo_ret(sd);
  4109. nullpo_ret(fitem);
  4110. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4111. return false; // Distance is too far
  4112. if (sd->sc.cant.pickup)
  4113. return false;
  4114. if (sd->status.party_id)
  4115. p = party_search(sd->status.party_id);
  4116. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4117. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4118. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4119. if (!(p && p->party.item&1 &&
  4120. first_sd && first_sd->status.party_id == sd->status.party_id
  4121. ))
  4122. return false;
  4123. }
  4124. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4125. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4126. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4127. if (!(p && p->party.item&1 &&
  4128. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4129. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4130. ))
  4131. return false;
  4132. }
  4133. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4134. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4135. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4136. if(!(p && p->party.item&1 &&
  4137. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4138. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4139. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4140. ))
  4141. return false;
  4142. }
  4143. }
  4144. }
  4145. }
  4146. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4147. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4148. clif_additem(sd,0,0,flag);
  4149. return true;
  4150. }
  4151. //Display pickup animation.
  4152. pc_stop_attack(sd);
  4153. clif_takeitem(&sd->bl,&fitem->bl);
  4154. if (fitem->mob_id &&
  4155. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4156. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4157. )
  4158. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4159. map_clearflooritem(&fitem->bl);
  4160. return true;
  4161. }
  4162. /*==========================================
  4163. * Check if item is usable.
  4164. * Return:
  4165. * 0 = no
  4166. * 1 = yes
  4167. *------------------------------------------*/
  4168. bool pc_isUseitem(struct map_session_data *sd,int n)
  4169. {
  4170. struct item_data *item;
  4171. unsigned short nameid;
  4172. nullpo_ret(sd);
  4173. item = sd->inventory_data[n];
  4174. nameid = sd->status.inventory[n].nameid;
  4175. if( item == NULL )
  4176. return false;
  4177. //Not consumable item
  4178. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4179. return false;
  4180. if( !item->script ) //if it has no script, you can't really consume it!
  4181. return false;
  4182. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4183. return true;
  4184. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4185. return false;
  4186. //Prevent mass item usage. [Skotlex]
  4187. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4188. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4189. )
  4190. return false;
  4191. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4192. clif_msg(sd,ITEM_NOUSE_SITTING);
  4193. return false; // You cannot use this item while sitting.
  4194. }
  4195. if (sd->state.storage_flag && item->type != IT_CASH) {
  4196. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd,388));
  4197. return false; // You cannot use this item while storage is open.
  4198. }
  4199. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m)))
  4200. return false;
  4201. switch( nameid ) {
  4202. case ITEMID_ANODYNE:
  4203. if( map_flag_gvg(sd->bl.m) )
  4204. return false;
  4205. case ITEMID_ALOEBERA:
  4206. if( pc_issit(sd) )
  4207. return false;
  4208. break;
  4209. case ITEMID_WING_OF_FLY:
  4210. case ITEMID_GIANT_FLY_WING:
  4211. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) ) {
  4212. clif_skill_teleportmessage(sd,0);
  4213. return false;
  4214. }
  4215. case ITEMID_WING_OF_BUTTERFLY:
  4216. case ITEMID_DUN_TELE_SCROLL1:
  4217. case ITEMID_DUN_TELE_SCROLL2:
  4218. case ITEMID_DUN_TELE_SCROLL3:
  4219. case ITEMID_WOB_RUNE:
  4220. case ITEMID_WOB_SCHWALTZ:
  4221. case ITEMID_WOB_RACHEL:
  4222. case ITEMID_WOB_LOCAL:
  4223. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4224. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4225. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4226. return false;
  4227. }
  4228. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  4229. return false;
  4230. break;
  4231. case ITEMID_BUBBLE_GUM:
  4232. case ITEMID_COMP_BUBBLE_GUM:
  4233. if( sd->sc.data[SC_ITEMBOOST] )
  4234. return false;
  4235. break;
  4236. case ITEMID_BATTLE_MANUAL:
  4237. case ITEMID_COMP_BATTLE_MANUAL:
  4238. case ITEMID_THICK_BATTLE_MANUAL:
  4239. case ITEMID_NOBLE_NAMEPLATE:
  4240. case ITEMID_BATTLE_MANUAL25:
  4241. case ITEMID_BATTLE_MANUAL100:
  4242. case ITEMID_BATTLE_MANUAL300:
  4243. if( sd->sc.data[SC_EXPBOOST] )
  4244. return false;
  4245. break;
  4246. case ITEMID_JOB_MANUAL50:
  4247. if( sd->sc.data[SC_JEXPBOOST] )
  4248. return false;
  4249. break;
  4250. case ITEMID_MERCENARY_RED_POTION:
  4251. case ITEMID_MERCENARY_BLUE_POTION:
  4252. case ITEMID_M_CENTER_POTION:
  4253. case ITEMID_M_AWAKENING_POTION:
  4254. case ITEMID_M_BERSERK_POTION:
  4255. if( sd->md == NULL || sd->md->db == NULL )
  4256. return false;
  4257. if( sd->md->sc.data[SC_BERSERK] )
  4258. return false;
  4259. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4260. return false;
  4261. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4262. return false;
  4263. break;
  4264. case ITEMID_NEURALIZER:
  4265. if( !map[sd->bl.m].flag.reset )
  4266. return false;
  4267. break;
  4268. }
  4269. if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL )
  4270. return false; // Mercenary Scrolls
  4271. /**
  4272. * Only Rune Knights may use runes
  4273. **/
  4274. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  4275. return false;
  4276. /**
  4277. * Only GCross may use poisons
  4278. **/
  4279. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  4280. return false;
  4281. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4282. if( pc_is90overweight(sd) ) {
  4283. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4284. return false;
  4285. }
  4286. if( !pc_inventoryblank(sd) ) {
  4287. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 732)); //Item cannot be open when inventory is full
  4288. return false;
  4289. }
  4290. }
  4291. //Gender check
  4292. if(item->sex != 2 && sd->status.sex != item->sex)
  4293. return false;
  4294. //Required level check
  4295. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4296. return false;
  4297. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4298. return false;
  4299. //Not equipable by class. [Skotlex]
  4300. if (!(
  4301. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4302. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4303. ))
  4304. return false;
  4305. if (sd->sc.count && (
  4306. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4307. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4308. sd->sc.data[SC_TRICKDEAD] ||
  4309. sd->sc.data[SC_HIDING] ||
  4310. sd->sc.data[SC__SHADOWFORM] ||
  4311. sd->sc.data[SC__INVISIBILITY] ||
  4312. sd->sc.data[SC__MANHOLE] ||
  4313. sd->sc.data[SC_KAGEHUMI] ||
  4314. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4315. sd->sc.data[SC_HEAT_BARREL_AFTER] ||
  4316. sd->sc.data[SC_KINGS_GRACE] ||
  4317. sd->sc.data[SC_SUHIDE]))
  4318. return false;
  4319. if (!pc_isItemClass(sd,item))
  4320. return false;
  4321. //Dead Branch items
  4322. if( item->flag.dead_branch )
  4323. log_branch(sd);
  4324. return true;
  4325. }
  4326. /*==========================================
  4327. * Last checks to use an item.
  4328. * Return:
  4329. * 0 = fail
  4330. * 1 = success
  4331. *------------------------------------------*/
  4332. int pc_useitem(struct map_session_data *sd,int n)
  4333. {
  4334. unsigned int tick = gettick();
  4335. int amount;
  4336. unsigned short nameid;
  4337. struct script_code *script;
  4338. struct item item;
  4339. struct item_data *id;
  4340. nullpo_ret(sd);
  4341. if (sd->npc_id) {
  4342. #ifdef RENEWAL
  4343. clif_msg(sd, USAGE_FAIL); // TODO look for the client date that has this message.
  4344. return 0;
  4345. #else
  4346. if( !sd->npc_item_flag )
  4347. return 0;
  4348. #endif
  4349. }
  4350. item = sd->status.inventory[n];
  4351. id = sd->inventory_data[n];
  4352. if (item.nameid == 0 || item.amount <= 0)
  4353. return 0;
  4354. if( !pc_isUseitem(sd,n) )
  4355. return 0;
  4356. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4357. nameid = id->nameid;
  4358. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4359. return 0;
  4360. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4361. if( id->flag.delay_consume ) {
  4362. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4363. return 0;
  4364. else if( pc_issit(sd) )
  4365. return 0;
  4366. }
  4367. //Since most delay-consume items involve using a "skill-type" target cursor,
  4368. //perform a skill-use check before going through. [Skotlex]
  4369. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4370. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4371. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4372. return 0;
  4373. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4374. return 0;
  4375. /* on restricted maps the item is consumed but the effect is not used */
  4376. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4377. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4378. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4379. clif_useitemack(sd,n,item.amount-1,true);
  4380. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4381. }
  4382. return 0;/* regardless, effect is not run */
  4383. }
  4384. sd->itemid = item.nameid;
  4385. sd->itemindex = n;
  4386. if(sd->catch_target_class != -1) //Abort pet catching.
  4387. sd->catch_target_class = -1;
  4388. amount = item.amount;
  4389. script = id->script;
  4390. //Check if the item is to be consumed immediately [Skotlex]
  4391. if (id->flag.delay_consume)
  4392. clif_useitemack(sd, n, amount, true);
  4393. else
  4394. {
  4395. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4396. {
  4397. clif_useitemack(sd, n, amount - 1, true);
  4398. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4399. }
  4400. else
  4401. clif_useitemack(sd, n, 0, false);
  4402. }
  4403. if(item.card[0]==CARD0_CREATE &&
  4404. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4405. {
  4406. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4407. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4408. potion_flag = 3; //Even more effective potions.
  4409. }
  4410. //Update item use time.
  4411. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4412. if( itemdb_iscashfood(nameid) )
  4413. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4414. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4415. potion_flag = 0;
  4416. return 1;
  4417. }
  4418. /**
  4419. * Add item on cart for given index.
  4420. * @param sd
  4421. * @param item
  4422. * @param amount
  4423. * @param log_type
  4424. * @return 0 = success; 1 = fail; 2 = no slot
  4425. */
  4426. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4427. {
  4428. struct item_data *data;
  4429. int i,w;
  4430. nullpo_retr(1, sd);
  4431. nullpo_retr(1, item);
  4432. if(item->nameid == 0 || amount <= 0)
  4433. return 1;
  4434. data = itemdb_search(item->nameid);
  4435. if( data->stack.cart && amount > data->stack.amount )
  4436. {// item stack limitation
  4437. return 1;
  4438. }
  4439. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4440. { // Check item trade restrictions [Skotlex]
  4441. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4442. return 1;
  4443. }
  4444. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4445. return 1;
  4446. // ID no longer points to inventory/kafra ID. While we get a new one we don't want to mess up vending creation.
  4447. item->id = 0;
  4448. i = MAX_CART;
  4449. if( itemdb_isstackable2(data) && !item->expire_time )
  4450. {
  4451. for (i = 0; i < MAX_CART; i++) {
  4452. if (sd->status.cart[i].nameid == item->nameid
  4453. && sd->status.cart[i].bound == item->bound
  4454. && sd->status.cart[i].unique_id == item->unique_id
  4455. && memcmp(sd->status.cart[i].card, item->card, sizeof(item->card)) == 0
  4456. )
  4457. break;
  4458. }
  4459. }
  4460. if( i < MAX_CART )
  4461. {// item already in cart, stack it
  4462. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4463. return 2; // no slot
  4464. sd->status.cart[i].amount+=amount;
  4465. clif_cart_additem(sd,i,amount,0);
  4466. }
  4467. else
  4468. {// item not stackable or not present, add it
  4469. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4470. if( i == MAX_CART )
  4471. return 2; // no slot
  4472. memcpy(&sd->status.cart[i],item,sizeof(sd->status.cart[0]));
  4473. sd->status.cart[i].amount=amount;
  4474. sd->cart_num++;
  4475. clif_cart_additem(sd,i,amount,0);
  4476. }
  4477. sd->status.cart[i].favorite = 0;/* clear */
  4478. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4479. sd->cart_weight += w;
  4480. clif_updatestatus(sd,SP_CARTINFO);
  4481. return 0;
  4482. }
  4483. /*==========================================
  4484. * Delete item on cart for given index.
  4485. *------------------------------------------*/
  4486. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4487. {
  4488. nullpo_retv(sd);
  4489. if(sd->status.cart[n].nameid == 0 ||
  4490. sd->status.cart[n].amount<amount)
  4491. return;
  4492. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4493. sd->status.cart[n].amount -= amount;
  4494. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4495. if(sd->status.cart[n].amount <= 0){
  4496. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4497. sd->cart_num--;
  4498. }
  4499. if(!type) {
  4500. clif_cart_delitem(sd,n,amount);
  4501. clif_updatestatus(sd,SP_CARTINFO);
  4502. }
  4503. }
  4504. /*==========================================
  4505. * Transfer item from inventory to cart.
  4506. *------------------------------------------*/
  4507. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4508. {
  4509. struct item *item_data;
  4510. char flag;
  4511. nullpo_retv(sd);
  4512. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4513. return;
  4514. item_data = &sd->status.inventory[idx];
  4515. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4516. return;
  4517. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4518. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4519. else {
  4520. clif_dropitem(sd,idx,0);
  4521. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4522. }
  4523. }
  4524. /*==========================================
  4525. * Get number of item in cart.
  4526. * Return:
  4527. -1 = itemid not found or no amount found
  4528. x = remaining itemid on cart after get
  4529. *------------------------------------------*/
  4530. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4531. {
  4532. struct item* item_data;
  4533. nullpo_retr(-1, sd);
  4534. item_data = &sd->status.cart[idx];
  4535. if( item_data->nameid == 0 || item_data->amount == 0 )
  4536. return -1;
  4537. return item_data->amount - amount;
  4538. }
  4539. /*==========================================
  4540. * Retrieve an item at index idx from cart.
  4541. *------------------------------------------*/
  4542. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4543. {
  4544. struct item *item_data;
  4545. unsigned char flag = 0;
  4546. nullpo_retv(sd);
  4547. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4548. return;
  4549. item_data=&sd->status.cart[idx];
  4550. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4551. return;
  4552. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4553. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4554. else {
  4555. clif_dropitem(sd,idx,0);
  4556. clif_additem(sd,0,0,flag);
  4557. }
  4558. }
  4559. /*==========================================
  4560. * Bound Item Check
  4561. * Type:
  4562. * 1 Account Bound
  4563. * 2 Guild Bound
  4564. * 3 Party Bound
  4565. * 4 Character Bound
  4566. *------------------------------------------*/
  4567. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4568. {
  4569. int i=0, j=0;
  4570. for(i=0;i<MAX_INVENTORY;i++){
  4571. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4572. idxlist[j] = i;
  4573. j++;
  4574. }
  4575. }
  4576. return j;
  4577. }
  4578. /*==========================================
  4579. * Display item stolen msg to player sd
  4580. *------------------------------------------*/
  4581. int pc_show_steal(struct block_list *bl,va_list ap)
  4582. {
  4583. struct map_session_data *sd;
  4584. int itemid;
  4585. struct item_data *item=NULL;
  4586. char output[100];
  4587. sd=va_arg(ap,struct map_session_data *);
  4588. itemid=va_arg(ap,int);
  4589. if((item=itemdb_exists(itemid))==NULL)
  4590. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4591. else
  4592. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4593. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4594. return 0;
  4595. }
  4596. /*==========================================
  4597. * Steal an item from bl (mob).
  4598. * Return:
  4599. * 0 = fail
  4600. * 1 = succes
  4601. *------------------------------------------*/
  4602. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4603. {
  4604. int i,itemid;
  4605. double rate;
  4606. unsigned char flag = 0;
  4607. struct status_data *sd_status, *md_status;
  4608. struct mob_data *md;
  4609. struct item tmp_item;
  4610. if(!sd || !bl || bl->type!=BL_MOB)
  4611. return 0;
  4612. md = (TBL_MOB *)bl;
  4613. 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
  4614. return 0;
  4615. sd_status= status_get_status_data(&sd->bl);
  4616. md_status= status_get_status_data(bl);
  4617. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4618. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4619. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4620. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4621. ) { //Can't steal from
  4622. md->state.steal_flag = UCHAR_MAX;
  4623. return 0;
  4624. }
  4625. // base skill success chance (percentual)
  4626. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4627. rate += sd->bonus.add_steal_rate;
  4628. if( rate < 1 )
  4629. return 0;
  4630. // Try dropping one item, in the order from first to last possible slot.
  4631. // Droprate is affected by the skill success rate.
  4632. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4633. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4634. break;
  4635. if( i == MAX_STEAL_DROP )
  4636. return 0;
  4637. itemid = md->db->dropitem[i].nameid;
  4638. memset(&tmp_item,0,sizeof(tmp_item));
  4639. tmp_item.nameid = itemid;
  4640. tmp_item.amount = 1;
  4641. tmp_item.identify = itemdb_isidentified(itemid);
  4642. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4643. //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?
  4644. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4645. if(flag) { //Failed to steal due to overweight
  4646. clif_additem(sd,0,0,flag);
  4647. return 0;
  4648. }
  4649. if(battle_config.show_steal_in_same_party)
  4650. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4651. //Logs items, Stolen from mobs [Lupus]
  4652. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4653. //A Rare Steal Global Announce by Lupus
  4654. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4655. struct item_data *i_data;
  4656. char message[128];
  4657. i_data = itemdb_search(itemid);
  4658. sprintf (message, msg_txt(sd,542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4659. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4660. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4661. }
  4662. return 1;
  4663. }
  4664. /*==========================================
  4665. * Stole zeny from bl (mob)
  4666. * return
  4667. * 0 = fail
  4668. * 1 = success
  4669. *------------------------------------------*/
  4670. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4671. {
  4672. int rate,skill;
  4673. struct mob_data *md;
  4674. if(!sd || !target || target->type != BL_MOB)
  4675. return 0;
  4676. md = (TBL_MOB*)target;
  4677. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  4678. return 0;
  4679. // FIXME: This formula is either custom or outdated.
  4680. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4681. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4682. if(rnd()%1000 < rate)
  4683. {
  4684. int amount = md->level*10 + rnd()%100;
  4685. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4686. md->state.steal_coin_flag = 1;
  4687. return 1;
  4688. }
  4689. return 0;
  4690. }
  4691. /*==========================================
  4692. * Set's a player position.
  4693. * @param sd
  4694. * @param mapindex
  4695. * @param x
  4696. * @param y
  4697. * @param clrtype
  4698. * @return SETPOS_OK Success
  4699. * SETPOS_MAPINDEX Invalid map index
  4700. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4701. * SETPOS_AUTOTRADE Player is in autotrade state
  4702. *------------------------------------------*/
  4703. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4704. {
  4705. int16 m;
  4706. nullpo_retr(SETPOS_OK,sd);
  4707. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4708. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4709. return SETPOS_MAPINDEX;
  4710. }
  4711. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4712. return SETPOS_AUTOTRADE;
  4713. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4714. pc_setstand(sd, true);
  4715. pc_setrestartvalue(sd,1);
  4716. }
  4717. m = map_mapindex2mapid(mapindex);
  4718. sd->state.changemap = (sd->mapindex != mapindex);
  4719. sd->state.warping = 1;
  4720. if(map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4721. bool instance_found = false;
  4722. struct party_data *p = NULL;
  4723. struct guild *g = NULL;
  4724. if (sd->instance_id) {
  4725. instance_delusers(sd->instance_id);
  4726. instance_found = true;
  4727. }
  4728. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4729. instance_delusers(p->instance_id);
  4730. instance_found = true;
  4731. }
  4732. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4733. instance_delusers(g->instance_id);
  4734. }
  4735. if( sd->state.changemap ) { // Misc map-changing settings
  4736. int i;
  4737. sd->state.pmap = sd->bl.m;
  4738. if (sd->sc.count) { // Cancel some map related stuff.
  4739. if (sd->sc.data[SC_JAILED])
  4740. return SETPOS_MAPINDEX; //You may not get out!
  4741. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4742. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4743. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4744. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4745. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4746. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4747. if (sd->sc.data[SC_KNOWLEDGE]) {
  4748. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4749. if (sce->timer != INVALID_TIMER)
  4750. delete_timer(sce->timer, status_change_timer);
  4751. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4752. }
  4753. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4754. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4755. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4756. }
  4757. for( i = 0; i < EQI_MAX; i++ ) {
  4758. if( sd->equip_index[i] >= 0 )
  4759. if( pc_isequip(sd,sd->equip_index[i]) )
  4760. pc_unequipitem(sd,sd->equip_index[i],2);
  4761. }
  4762. if (battle_config.clear_unit_onwarp&BL_PC)
  4763. skill_clear_unitgroup(&sd->bl);
  4764. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4765. guild_send_dot_remove(sd);
  4766. bg_send_dot_remove(sd);
  4767. if (sd->regen.state.gc)
  4768. sd->regen.state.gc = 0;
  4769. // make sure vending is allowed here
  4770. if (sd->state.vending && map[m].flag.novending) {
  4771. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4772. vending_closevending(sd);
  4773. }
  4774. channel_pcquit(sd,4); //quit map chan
  4775. }
  4776. if( m < 0 )
  4777. {
  4778. uint32 ip;
  4779. uint16 port;
  4780. //if can't find any map-servers, just abort setting position.
  4781. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4782. return SETPOS_NO_MAPSERVER;
  4783. if (sd->npc_id)
  4784. npc_event_dequeue(sd);
  4785. npc_script_event(sd, NPCE_LOGOUT);
  4786. //remove from map, THEN change x/y coordinates
  4787. unit_remove_map_pc(sd,clrtype);
  4788. sd->mapindex = mapindex;
  4789. sd->bl.x=x;
  4790. sd->bl.y=y;
  4791. pc_clean_skilltree(sd);
  4792. chrif_save(sd,2);
  4793. chrif_changemapserver(sd, ip, (short)port);
  4794. //Free session data from this map server [Kevin]
  4795. unit_free_pc(sd);
  4796. return SETPOS_OK;
  4797. }
  4798. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4799. {
  4800. 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);
  4801. x = y = 0; // make it random
  4802. }
  4803. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4804. int c=0;
  4805. do {
  4806. x = rnd()%(map[m].xs-2)+1;
  4807. y = rnd()%(map[m].ys-2)+1;
  4808. c++;
  4809. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4810. ShowError("pc_setpos: couldn't found a valid coordinates for player '%s' (%d:%d) on (%s), preventing warp\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex));
  4811. return SETPOS_OK; //preventing warp
  4812. //break; //allow warp anyway
  4813. }
  4814. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4815. }
  4816. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4817. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4818. vending_closevending(sd);
  4819. }
  4820. if(sd->bl.prev != NULL){
  4821. unit_remove_map_pc(sd,clrtype);
  4822. clif_changemap(sd,m,x,y); // [MouseJstr]
  4823. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4824. sd->state.rewarp = 1;
  4825. sd->mapindex = mapindex;
  4826. sd->bl.m = m;
  4827. sd->bl.x = sd->ud.to_x = x;
  4828. sd->bl.y = sd->ud.to_y = y;
  4829. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4830. { // Increased guild castle regen [Valaris]
  4831. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4832. if(gc && gc->guild_id == sd->status.guild_id)
  4833. sd->regen.state.gc = 1;
  4834. }
  4835. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4836. {
  4837. sd->pd->bl.m = m;
  4838. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4839. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4840. sd->pd->ud.dir = sd->ud.dir;
  4841. }
  4842. if( hom_is_active(sd->hd) )
  4843. {
  4844. sd->hd->bl.m = m;
  4845. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4846. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4847. sd->hd->ud.dir = sd->ud.dir;
  4848. }
  4849. if( sd->md )
  4850. {
  4851. sd->md->bl.m = m;
  4852. sd->md->bl.x = sd->md->ud.to_x = x;
  4853. sd->md->bl.y = sd->md->ud.to_y = y;
  4854. sd->md->ud.dir = sd->ud.dir;
  4855. }
  4856. if( sd->ed ) {
  4857. sd->ed->bl.m = m;
  4858. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4859. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4860. sd->ed->ud.dir = sd->ud.dir;
  4861. }
  4862. pc_cell_basilica(sd);
  4863. //check if we gonna be rewarped [lighta]
  4864. if(npc_check_areanpc(1,m,x,y,0)){
  4865. sd->count_rewarp++;
  4866. }
  4867. else
  4868. sd->count_rewarp = 0;
  4869. return SETPOS_OK;
  4870. }
  4871. /*==========================================
  4872. * Warp player sd to random location on current map.
  4873. * May fail if no walkable cell found (1000 attempts).
  4874. * Return:
  4875. * 0 = Success
  4876. * 1,2,3 = Fail
  4877. *------------------------------------------*/
  4878. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4879. {
  4880. int x,y,i=0;
  4881. int16 m;
  4882. nullpo_ret(sd);
  4883. m=sd->bl.m;
  4884. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4885. return 3;
  4886. do {
  4887. x = rnd()%(map[m].xs-2)+1;
  4888. y = rnd()%(map[m].ys-2)+1;
  4889. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  4890. if (i < 1000)
  4891. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4892. return 3;
  4893. }
  4894. /*==========================================
  4895. * Records a memo point at sd's current position
  4896. * pos - entry to replace, (-1: shift oldest entry out)
  4897. *------------------------------------------*/
  4898. bool pc_memo(struct map_session_data* sd, int pos)
  4899. {
  4900. int skill;
  4901. nullpo_ret(sd);
  4902. // check mapflags
  4903. 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) ) {
  4904. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4905. return false;
  4906. }
  4907. // check inputs
  4908. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4909. return false; // invalid input
  4910. // check required skill level
  4911. skill = pc_checkskill(sd, AL_WARP);
  4912. if( skill < 1 ) {
  4913. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4914. return false;
  4915. }
  4916. if( skill < 2 || skill - 2 < pos ) {
  4917. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4918. return false;
  4919. }
  4920. if( pos == -1 )
  4921. {
  4922. uint8 i;
  4923. // prevent memo-ing the same map multiple times
  4924. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4925. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4926. pos = 0;
  4927. }
  4928. if( map[sd->bl.m].instance_id ) {
  4929. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  4930. return false;
  4931. }
  4932. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4933. sd->status.memo_point[pos].x = sd->bl.x;
  4934. sd->status.memo_point[pos].y = sd->bl.y;
  4935. clif_skill_memomessage(sd, 0);
  4936. return true;
  4937. }
  4938. //
  4939. // Skills
  4940. //
  4941. /**
  4942. * Get the skill current cooldown for player.
  4943. * (get the db base cooldown for skill + player specific cooldown)
  4944. * @param sd : player pointer
  4945. * @param id : skill id
  4946. * @param lv : skill lv
  4947. * @return player skill cooldown
  4948. */
  4949. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  4950. uint8 i;
  4951. uint16 idx = skill_get_index(skill_id);
  4952. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  4953. if (!idx) return 0;
  4954. if (skill_db[idx]->cooldown[skill_lv - 1])
  4955. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  4956. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  4957. if (i < cooldownlen) {
  4958. cooldown += sd->skillcooldown[i].val;
  4959. cooldown = max(0,cooldown);
  4960. }
  4961. return cooldown;
  4962. }
  4963. /*==========================================
  4964. * Return player sd skill_lv learned for given skill
  4965. *------------------------------------------*/
  4966. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  4967. {
  4968. uint16 idx = 0;
  4969. if (sd == NULL)
  4970. return 0;
  4971. if ((idx = skill_get_index(skill_id)) == 0) {
  4972. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4973. return 0;
  4974. }
  4975. if (SKILL_CHK_GUILD(skill_id) ) {
  4976. struct guild *g;
  4977. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4978. return guild_checkskill(g,skill_id);
  4979. return 0;
  4980. }
  4981. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  4982. }
  4983. /**
  4984. * Check if we still have the correct weapon to continue the skill (actually status)
  4985. * If not ending it
  4986. * @param sd
  4987. * @return 0:error, 1:check done
  4988. */
  4989. static void pc_checkallowskill(struct map_session_data *sd)
  4990. {
  4991. const enum sc_type scw_list[] = {
  4992. SC_TWOHANDQUICKEN,
  4993. SC_ONEHAND,
  4994. SC_AURABLADE,
  4995. SC_PARRYING,
  4996. SC_SPEARQUICKEN,
  4997. SC_ADRENALINE,
  4998. SC_ADRENALINE2,
  4999. SC_DANCING,
  5000. SC_GATLINGFEVER,
  5001. };
  5002. uint8 i;
  5003. nullpo_retv(sd);
  5004. if(!sd->sc.count)
  5005. return;
  5006. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5007. { // Skills requiring specific weapon types
  5008. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5009. continue;
  5010. if(sd->sc.data[scw_list[i]] &&
  5011. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5012. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5013. }
  5014. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5015. // Spurt requires bare hands (feet, in fact xD)
  5016. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5017. if(sd->status.shield <= 0) { // Skills requiring a shield
  5018. const enum sc_type scs_list[] = {
  5019. SC_AUTOGUARD,
  5020. SC_DEFENDER,
  5021. SC_REFLECTSHIELD,
  5022. SC_REFLECTDAMAGE
  5023. };
  5024. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5025. if(sd->sc.data[scs_list[i]])
  5026. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5027. }
  5028. }
  5029. /*==========================================
  5030. * Return equipped index of item on player sd at pos
  5031. * Return
  5032. * -1 : Nothing equipped
  5033. * idx : (this index could be used in inventory to found item_data)
  5034. *------------------------------------------*/
  5035. short pc_checkequip(struct map_session_data *sd,int pos)
  5036. {
  5037. uint8 i;
  5038. nullpo_retr(-1, sd);
  5039. for(i=0;i<EQI_MAX;i++){
  5040. if(pos & equip_bitmask[i])
  5041. return sd->equip_index[i];
  5042. }
  5043. return -1;
  5044. }
  5045. /*==========================================
  5046. * Check if sd has nameid equipped somewhere
  5047. * @sd : the player session
  5048. * @nameid : id of the item to check
  5049. * @min : : see pc.h enum equip_index from ? to @max
  5050. * @max : see pc.h enum equip_index for @min to ?
  5051. * -return true,false
  5052. *------------------------------------------*/
  5053. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5054. {
  5055. int i;
  5056. for(i = min; i < max; i++) {
  5057. if(equip_bitmask[i]) {
  5058. int idx = sd->equip_index[i];
  5059. if (sd->status.inventory[idx].nameid == nameid)
  5060. return true;
  5061. }
  5062. }
  5063. return false;
  5064. }
  5065. /*==========================================
  5066. * Convert's from the client's lame Job ID system
  5067. * to the map server's 'makes sense' system. [Skotlex]
  5068. *------------------------------------------*/
  5069. int pc_jobid2mapid(unsigned short b_class)
  5070. {
  5071. switch(b_class)
  5072. {
  5073. //Novice And 1-1 Jobs
  5074. case JOB_NOVICE: return MAPID_NOVICE;
  5075. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5076. case JOB_MAGE: return MAPID_MAGE;
  5077. case JOB_ARCHER: return MAPID_ARCHER;
  5078. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5079. case JOB_MERCHANT: return MAPID_MERCHANT;
  5080. case JOB_THIEF: return MAPID_THIEF;
  5081. case JOB_TAEKWON: return MAPID_TAEKWON;
  5082. case JOB_WEDDING: return MAPID_WEDDING;
  5083. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5084. case JOB_NINJA: return MAPID_NINJA;
  5085. case JOB_XMAS: return MAPID_XMAS;
  5086. case JOB_SUMMER: return MAPID_SUMMER;
  5087. case JOB_HANBOK: return MAPID_HANBOK;
  5088. case JOB_GANGSI: return MAPID_GANGSI;
  5089. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5090. //2-1 Jobs
  5091. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5092. case JOB_KNIGHT: return MAPID_KNIGHT;
  5093. case JOB_WIZARD: return MAPID_WIZARD;
  5094. case JOB_HUNTER: return MAPID_HUNTER;
  5095. case JOB_PRIEST: return MAPID_PRIEST;
  5096. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5097. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5098. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5099. case JOB_KAGEROU:
  5100. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5101. case JOB_REBELLION: return MAPID_REBELLION;
  5102. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5103. //2-2 Jobs
  5104. case JOB_CRUSADER: return MAPID_CRUSADER;
  5105. case JOB_SAGE: return MAPID_SAGE;
  5106. case JOB_BARD:
  5107. case JOB_DANCER: return MAPID_BARDDANCER;
  5108. case JOB_MONK: return MAPID_MONK;
  5109. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5110. case JOB_ROGUE: return MAPID_ROGUE;
  5111. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5112. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5113. //Trans Novice And Trans 1-1 Jobs
  5114. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5115. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5116. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5117. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5118. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5119. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5120. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5121. //Trans 2-1 Jobs
  5122. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5123. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5124. case JOB_SNIPER: return MAPID_SNIPER;
  5125. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5126. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5127. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5128. //Trans 2-2 Jobs
  5129. case JOB_PALADIN: return MAPID_PALADIN;
  5130. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5131. case JOB_CLOWN:
  5132. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5133. case JOB_CHAMPION: return MAPID_CHAMPION;
  5134. case JOB_CREATOR: return MAPID_CREATOR;
  5135. case JOB_STALKER: return MAPID_STALKER;
  5136. //Baby Novice And Baby 1-1 Jobs
  5137. case JOB_BABY: return MAPID_BABY;
  5138. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5139. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5140. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5141. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5142. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5143. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5144. //Baby 2-1 Jobs
  5145. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5146. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5147. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5148. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5149. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5150. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5151. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5152. //Baby 2-2 Jobs
  5153. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5154. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5155. case JOB_BABY_BARD:
  5156. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5157. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5158. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5159. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5160. //3-1 Jobs
  5161. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5162. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5163. case JOB_WARLOCK: return MAPID_WARLOCK;
  5164. case JOB_RANGER: return MAPID_RANGER;
  5165. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5166. case JOB_MECHANIC: return MAPID_MECHANIC;
  5167. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5168. //3-2 Jobs
  5169. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5170. case JOB_SORCERER: return MAPID_SORCERER;
  5171. case JOB_MINSTREL:
  5172. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5173. case JOB_SURA: return MAPID_SURA;
  5174. case JOB_GENETIC: return MAPID_GENETIC;
  5175. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5176. //Trans 3-1 Jobs
  5177. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5178. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5179. case JOB_RANGER_T: return MAPID_RANGER_T;
  5180. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5181. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5182. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5183. //Trans 3-2 Jobs
  5184. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5185. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5186. case JOB_MINSTREL_T:
  5187. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5188. case JOB_SURA_T: return MAPID_SURA_T;
  5189. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5190. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5191. //Baby 3-1 Jobs
  5192. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5193. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5194. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5195. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5196. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5197. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5198. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5199. //Baby 3-2 Jobs
  5200. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5201. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5202. case JOB_BABY_MINSTREL:
  5203. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5204. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5205. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5206. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5207. //Doram Jobs
  5208. case JOB_SUMMONER: return MAPID_SUMMONER;
  5209. default:
  5210. return -1;
  5211. }
  5212. }
  5213. //Reverts the map-style class id to the client-style one.
  5214. int pc_mapid2jobid(unsigned short class_, int sex)
  5215. {
  5216. switch(class_) {
  5217. //Novice And 1-1 Jobs
  5218. case MAPID_NOVICE: return JOB_NOVICE;
  5219. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5220. case MAPID_MAGE: return JOB_MAGE;
  5221. case MAPID_ARCHER: return JOB_ARCHER;
  5222. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5223. case MAPID_MERCHANT: return JOB_MERCHANT;
  5224. case MAPID_THIEF: return JOB_THIEF;
  5225. case MAPID_TAEKWON: return JOB_TAEKWON;
  5226. case MAPID_WEDDING: return JOB_WEDDING;
  5227. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5228. case MAPID_NINJA: return JOB_NINJA;
  5229. case MAPID_XMAS: return JOB_XMAS;
  5230. case MAPID_SUMMER: return JOB_SUMMER;
  5231. case MAPID_HANBOK: return JOB_HANBOK;
  5232. case MAPID_GANGSI: return JOB_GANGSI;
  5233. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5234. //2-1 Jobs
  5235. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5236. case MAPID_KNIGHT: return JOB_KNIGHT;
  5237. case MAPID_WIZARD: return JOB_WIZARD;
  5238. case MAPID_HUNTER: return JOB_HUNTER;
  5239. case MAPID_PRIEST: return JOB_PRIEST;
  5240. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5241. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5242. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5243. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5244. case MAPID_REBELLION: return JOB_REBELLION;
  5245. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5246. //2-2 Jobs
  5247. case MAPID_CRUSADER: return JOB_CRUSADER;
  5248. case MAPID_SAGE: return JOB_SAGE;
  5249. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5250. case MAPID_MONK: return JOB_MONK;
  5251. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5252. case MAPID_ROGUE: return JOB_ROGUE;
  5253. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5254. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5255. //Trans Novice And Trans 2-1 Jobs
  5256. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5257. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5258. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5259. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5260. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5261. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5262. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5263. //Trans 2-1 Jobs
  5264. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5265. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5266. case MAPID_SNIPER: return JOB_SNIPER;
  5267. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5268. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5269. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5270. //Trans 2-2 Jobs
  5271. case MAPID_PALADIN: return JOB_PALADIN;
  5272. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5273. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5274. case MAPID_CHAMPION: return JOB_CHAMPION;
  5275. case MAPID_CREATOR: return JOB_CREATOR;
  5276. case MAPID_STALKER: return JOB_STALKER;
  5277. //Baby Novice And Baby 1-1 Jobs
  5278. case MAPID_BABY: return JOB_BABY;
  5279. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5280. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5281. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5282. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5283. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5284. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5285. //Baby 2-1 Jobs
  5286. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5287. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5288. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5289. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5290. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5291. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5292. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5293. //Baby 2-2 Jobs
  5294. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5295. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5296. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5297. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5298. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5299. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5300. //3-1 Jobs
  5301. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5302. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5303. case MAPID_WARLOCK: return JOB_WARLOCK;
  5304. case MAPID_RANGER: return JOB_RANGER;
  5305. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5306. case MAPID_MECHANIC: return JOB_MECHANIC;
  5307. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5308. //3-2 Jobs
  5309. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5310. case MAPID_SORCERER: return JOB_SORCERER;
  5311. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5312. case MAPID_SURA: return JOB_SURA;
  5313. case MAPID_GENETIC: return JOB_GENETIC;
  5314. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5315. //Trans 3-1 Jobs
  5316. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5317. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5318. case MAPID_RANGER_T: return JOB_RANGER_T;
  5319. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5320. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5321. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5322. //Trans 3-2 Jobs
  5323. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5324. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5325. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5326. case MAPID_SURA_T: return JOB_SURA_T;
  5327. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5328. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5329. //Baby 3-1 Jobs
  5330. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5331. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5332. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5333. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5334. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5335. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5336. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5337. //Baby 3-2 Jobs
  5338. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5339. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5340. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5341. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5342. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5343. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5344. //Doram Jobs
  5345. case MAPID_SUMMONER: return JOB_SUMMONER;
  5346. default:
  5347. return -1;
  5348. }
  5349. }
  5350. /*====================================================
  5351. * This function return the name of the job (by [Yor])
  5352. *----------------------------------------------------*/
  5353. const char* job_name(int class_)
  5354. {
  5355. switch (class_) {
  5356. case JOB_NOVICE:
  5357. case JOB_SWORDMAN:
  5358. case JOB_MAGE:
  5359. case JOB_ARCHER:
  5360. case JOB_ACOLYTE:
  5361. case JOB_MERCHANT:
  5362. case JOB_THIEF:
  5363. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5364. case JOB_KNIGHT:
  5365. case JOB_PRIEST:
  5366. case JOB_WIZARD:
  5367. case JOB_BLACKSMITH:
  5368. case JOB_HUNTER:
  5369. case JOB_ASSASSIN:
  5370. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5371. case JOB_KNIGHT2:
  5372. return msg_txt(NULL,557);
  5373. case JOB_CRUSADER:
  5374. case JOB_MONK:
  5375. case JOB_SAGE:
  5376. case JOB_ROGUE:
  5377. case JOB_ALCHEMIST:
  5378. case JOB_BARD:
  5379. case JOB_DANCER:
  5380. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5381. case JOB_CRUSADER2:
  5382. return msg_txt(NULL,563);
  5383. case JOB_WEDDING:
  5384. case JOB_SUPER_NOVICE:
  5385. case JOB_GUNSLINGER:
  5386. case JOB_NINJA:
  5387. case JOB_XMAS:
  5388. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5389. case JOB_SUMMER:
  5390. return msg_txt(NULL,621);
  5391. case JOB_HANBOK:
  5392. return msg_txt(NULL,694);
  5393. case JOB_OKTOBERFEST:
  5394. return msg_txt(NULL,696);
  5395. case JOB_NOVICE_HIGH:
  5396. case JOB_SWORDMAN_HIGH:
  5397. case JOB_MAGE_HIGH:
  5398. case JOB_ARCHER_HIGH:
  5399. case JOB_ACOLYTE_HIGH:
  5400. case JOB_MERCHANT_HIGH:
  5401. case JOB_THIEF_HIGH:
  5402. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5403. case JOB_LORD_KNIGHT:
  5404. case JOB_HIGH_PRIEST:
  5405. case JOB_HIGH_WIZARD:
  5406. case JOB_WHITESMITH:
  5407. case JOB_SNIPER:
  5408. case JOB_ASSASSIN_CROSS:
  5409. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5410. case JOB_LORD_KNIGHT2:
  5411. return msg_txt(NULL,582);
  5412. case JOB_PALADIN:
  5413. case JOB_CHAMPION:
  5414. case JOB_PROFESSOR:
  5415. case JOB_STALKER:
  5416. case JOB_CREATOR:
  5417. case JOB_CLOWN:
  5418. case JOB_GYPSY:
  5419. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5420. case JOB_PALADIN2:
  5421. return msg_txt(NULL,588);
  5422. case JOB_BABY:
  5423. case JOB_BABY_SWORDMAN:
  5424. case JOB_BABY_MAGE:
  5425. case JOB_BABY_ARCHER:
  5426. case JOB_BABY_ACOLYTE:
  5427. case JOB_BABY_MERCHANT:
  5428. case JOB_BABY_THIEF:
  5429. return msg_txt(NULL,595 - JOB_BABY + class_);
  5430. case JOB_BABY_KNIGHT:
  5431. case JOB_BABY_PRIEST:
  5432. case JOB_BABY_WIZARD:
  5433. case JOB_BABY_BLACKSMITH:
  5434. case JOB_BABY_HUNTER:
  5435. case JOB_BABY_ASSASSIN:
  5436. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5437. case JOB_BABY_KNIGHT2:
  5438. return msg_txt(NULL,602);
  5439. case JOB_BABY_CRUSADER:
  5440. case JOB_BABY_MONK:
  5441. case JOB_BABY_SAGE:
  5442. case JOB_BABY_ROGUE:
  5443. case JOB_BABY_ALCHEMIST:
  5444. case JOB_BABY_BARD:
  5445. case JOB_BABY_DANCER:
  5446. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5447. case JOB_BABY_CRUSADER2:
  5448. return msg_txt(NULL,608);
  5449. case JOB_SUPER_BABY:
  5450. return msg_txt(NULL,615);
  5451. case JOB_TAEKWON:
  5452. return msg_txt(NULL,616);
  5453. case JOB_STAR_GLADIATOR:
  5454. case JOB_STAR_GLADIATOR2:
  5455. return msg_txt(NULL,617);
  5456. case JOB_SOUL_LINKER:
  5457. return msg_txt(NULL,618);
  5458. case JOB_GANGSI:
  5459. case JOB_DEATH_KNIGHT:
  5460. case JOB_DARK_COLLECTOR:
  5461. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5462. case JOB_RUNE_KNIGHT:
  5463. case JOB_WARLOCK:
  5464. case JOB_RANGER:
  5465. case JOB_ARCH_BISHOP:
  5466. case JOB_MECHANIC:
  5467. case JOB_GUILLOTINE_CROSS:
  5468. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5469. case JOB_RUNE_KNIGHT_T:
  5470. case JOB_WARLOCK_T:
  5471. case JOB_RANGER_T:
  5472. case JOB_ARCH_BISHOP_T:
  5473. case JOB_MECHANIC_T:
  5474. case JOB_GUILLOTINE_CROSS_T:
  5475. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5476. case JOB_ROYAL_GUARD:
  5477. case JOB_SORCERER:
  5478. case JOB_MINSTREL:
  5479. case JOB_WANDERER:
  5480. case JOB_SURA:
  5481. case JOB_GENETIC:
  5482. case JOB_SHADOW_CHASER:
  5483. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5484. case JOB_ROYAL_GUARD_T:
  5485. case JOB_SORCERER_T:
  5486. case JOB_MINSTREL_T:
  5487. case JOB_WANDERER_T:
  5488. case JOB_SURA_T:
  5489. case JOB_GENETIC_T:
  5490. case JOB_SHADOW_CHASER_T:
  5491. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5492. case JOB_RUNE_KNIGHT2:
  5493. case JOB_RUNE_KNIGHT_T2:
  5494. return msg_txt(NULL,625);
  5495. case JOB_ROYAL_GUARD2:
  5496. case JOB_ROYAL_GUARD_T2:
  5497. return msg_txt(NULL,631);
  5498. case JOB_RANGER2:
  5499. case JOB_RANGER_T2:
  5500. return msg_txt(NULL,627);
  5501. case JOB_MECHANIC2:
  5502. case JOB_MECHANIC_T2:
  5503. return msg_txt(NULL,629);
  5504. case JOB_BABY_RUNE:
  5505. case JOB_BABY_WARLOCK:
  5506. case JOB_BABY_RANGER:
  5507. case JOB_BABY_BISHOP:
  5508. case JOB_BABY_MECHANIC:
  5509. case JOB_BABY_CROSS:
  5510. case JOB_BABY_GUARD:
  5511. case JOB_BABY_SORCERER:
  5512. case JOB_BABY_MINSTREL:
  5513. case JOB_BABY_WANDERER:
  5514. case JOB_BABY_SURA:
  5515. case JOB_BABY_GENETIC:
  5516. case JOB_BABY_CHASER:
  5517. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5518. case JOB_BABY_RUNE2:
  5519. return msg_txt(NULL,638);
  5520. case JOB_BABY_GUARD2:
  5521. return msg_txt(NULL,644);
  5522. case JOB_BABY_RANGER2:
  5523. return msg_txt(NULL,640);
  5524. case JOB_BABY_MECHANIC2:
  5525. return msg_txt(NULL,642);
  5526. case JOB_SUPER_NOVICE_E:
  5527. case JOB_SUPER_BABY_E:
  5528. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5529. case JOB_KAGEROU:
  5530. case JOB_OBORO:
  5531. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5532. case JOB_REBELLION:
  5533. return msg_txt(NULL,695);
  5534. case JOB_SUMMONER:
  5535. return msg_txt(NULL,697);
  5536. default:
  5537. return msg_txt(NULL,655);
  5538. }
  5539. }
  5540. /*====================================================
  5541. * Timered function to make id follow a target.
  5542. * @id = bl.id (player only atm)
  5543. * target is define in sd->followtarget (bl.id)
  5544. * used by pc_follow
  5545. *----------------------------------------------------*/
  5546. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5547. {
  5548. struct map_session_data *sd;
  5549. struct block_list *tbl;
  5550. sd = map_id2sd(id);
  5551. nullpo_ret(sd);
  5552. if (sd->followtimer != tid){
  5553. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5554. sd->followtimer = INVALID_TIMER;
  5555. return 0;
  5556. }
  5557. sd->followtimer = INVALID_TIMER;
  5558. tbl = map_id2bl(sd->followtarget);
  5559. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5560. {
  5561. pc_stop_following(sd);
  5562. return 0;
  5563. }
  5564. // either player or target is currently detached from map blocks (could be teleporting),
  5565. // but still connected to this map, so we'll just increment the timer and check back later
  5566. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5567. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5568. {
  5569. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5570. if (!check_distance_bl(&sd->bl, tbl, 5))
  5571. unit_walktobl(&sd->bl, tbl, 5, 0);
  5572. } else
  5573. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5574. }
  5575. sd->followtimer = add_timer(
  5576. tick + 1000, // increase time a bit to loosen up map's load
  5577. pc_follow_timer, sd->bl.id, 0);
  5578. return 0;
  5579. }
  5580. int pc_stop_following (struct map_session_data *sd)
  5581. {
  5582. nullpo_ret(sd);
  5583. if (sd->followtimer != INVALID_TIMER) {
  5584. delete_timer(sd->followtimer,pc_follow_timer);
  5585. sd->followtimer = INVALID_TIMER;
  5586. }
  5587. sd->followtarget = -1;
  5588. sd->ud.target_to = 0;
  5589. unit_stop_walking(&sd->bl, 1);
  5590. return 0;
  5591. }
  5592. int pc_follow(struct map_session_data *sd,int target_id)
  5593. {
  5594. struct block_list *bl = map_id2bl(target_id);
  5595. if (bl == NULL /*|| bl->type != BL_PC*/)
  5596. return 1;
  5597. if (sd->followtimer != INVALID_TIMER)
  5598. pc_stop_following(sd);
  5599. sd->followtarget = target_id;
  5600. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5601. return 0;
  5602. }
  5603. int pc_checkbaselevelup(struct map_session_data *sd) {
  5604. unsigned int next = pc_nextbaseexp(sd);
  5605. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5606. return 0;
  5607. do {
  5608. sd->status.base_exp -= next;
  5609. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5610. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5611. sd->status.base_exp = next-1;
  5612. next = pc_gets_status_point(sd->status.base_level);
  5613. sd->status.base_level++;
  5614. sd->status.status_point += next;
  5615. if( pc_is_maxbaselv(sd) ){
  5616. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5617. break;
  5618. }
  5619. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5620. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5621. status_calc_pet(sd->pd,SCO_NONE);
  5622. clif_updatestatus(sd,SP_STATUSPOINT);
  5623. clif_updatestatus(sd,SP_BASELEVEL);
  5624. clif_updatestatus(sd,SP_BASEEXP);
  5625. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5626. status_calc_pc(sd,SCO_FORCE);
  5627. status_percent_heal(&sd->bl,100,100);
  5628. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5629. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5630. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5631. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5632. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5633. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5634. if (sd->state.snovice_dead_flag)
  5635. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5636. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5637. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5638. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5639. }
  5640. clif_misceffect(&sd->bl,0);
  5641. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5642. if(sd->status.party_id)
  5643. party_send_levelup(sd);
  5644. pc_baselevelchanged(sd);
  5645. return 1;
  5646. }
  5647. void pc_baselevelchanged(struct map_session_data *sd) {
  5648. uint8 i;
  5649. for( i = 0; i < EQI_MAX; i++ ) {
  5650. if( sd->equip_index[i] >= 0 ) {
  5651. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5652. pc_unequipitem(sd, sd->equip_index[i], 3);
  5653. }
  5654. }
  5655. pc_show_questinfo(sd);
  5656. }
  5657. int pc_checkjoblevelup(struct map_session_data *sd)
  5658. {
  5659. unsigned int next = pc_nextjobexp(sd);
  5660. nullpo_ret(sd);
  5661. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5662. return 0;
  5663. do {
  5664. sd->status.job_exp -= next;
  5665. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5666. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5667. sd->status.job_exp = next-1;
  5668. sd->status.job_level ++;
  5669. sd->status.skill_point ++;
  5670. if( pc_is_maxjoblv(sd) ){
  5671. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5672. break;
  5673. }
  5674. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5675. clif_updatestatus(sd,SP_JOBLEVEL);
  5676. clif_updatestatus(sd,SP_JOBEXP);
  5677. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5678. clif_updatestatus(sd,SP_SKILLPOINT);
  5679. status_calc_pc(sd,SCO_FORCE);
  5680. clif_misceffect(&sd->bl,1);
  5681. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5682. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5683. npc_script_event(sd, NPCE_JOBLVUP);
  5684. return 1;
  5685. }
  5686. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5687. * @param sd Player
  5688. * @param base_exp Base EXP before peronal bonuses
  5689. * @param job_exp Job EXP before peronal bonuses
  5690. * @param src Block list that affecting the exp calculation
  5691. */
  5692. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5693. {
  5694. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5695. if (src) {
  5696. struct status_data *status = status_get_status_data(src);
  5697. if( sd->expaddrace[status->race] )
  5698. bonus += sd->expaddrace[status->race];
  5699. if( sd->expaddrace[RC_ALL] )
  5700. bonus += sd->expaddrace[RC_ALL];
  5701. if( sd->expaddclass[status->class_] )
  5702. bonus += sd->expaddclass[status->class_];
  5703. if( sd->expaddclass[CLASS_ALL] )
  5704. bonus += sd->expaddclass[CLASS_ALL];
  5705. if (battle_config.pk_mode &&
  5706. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5707. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5708. #ifdef VIP_ENABLE
  5709. //EXP bonus for VIP player
  5710. if (src && src->type == BL_MOB && pc_isvip(sd)) {
  5711. vip_bonus_base = battle_config.vip_base_exp_increase;
  5712. vip_bonus_job = battle_config.vip_job_exp_increase;
  5713. }
  5714. #endif
  5715. }
  5716. // Give EXPBOOST for quests even if src is NULL.
  5717. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5718. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5719. if( battle_config.vip_bm_increase && pc_isvip(sd) ) // Increase Battle Manual EXP rate for VIP.
  5720. bonus += ( sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase );
  5721. }
  5722. if (*base_exp) {
  5723. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5724. *base_exp = cap_value(exp, 1, UINT_MAX);
  5725. }
  5726. // Give JEXPBOOST for quests even if src is NULL.
  5727. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5728. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5729. if (*job_exp) {
  5730. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5731. *job_exp = cap_value(exp, 1, UINT_MAX);
  5732. }
  5733. return;
  5734. }
  5735. /**
  5736. * Show EXP gained by player in percentage by @showexp
  5737. * @param sd Player
  5738. * @param base_exp Base EXP gained/loss
  5739. * @param next_base_exp Base EXP needed for next base level
  5740. * @param job_exp Job EXP gained/loss
  5741. * @param next_job_exp Job EXP needed for next job level
  5742. * @param lost True:EXP penalty, lose EXP
  5743. **/
  5744. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  5745. char output[CHAT_SIZE_MAX];
  5746. nullpo_retv(sd);
  5747. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5748. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5749. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5750. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5751. clif_disp_onlyself(sd, output, strlen(output));
  5752. }
  5753. /**
  5754. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5755. * @param sd Player
  5756. * @param src EXP source
  5757. * @param base_exp Base EXP gained
  5758. * @param base_exp Job EXP gained
  5759. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5760. * @return
  5761. **/
  5762. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5763. {
  5764. unsigned int nextb = 0, nextj = 0;
  5765. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5766. nullpo_retv(sd);
  5767. if(sd->bl.prev == NULL || pc_isdead(sd))
  5768. return;
  5769. if (!(exp_flag&2)) {
  5770. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5771. return; // no exp on pvp maps
  5772. if (sd->status.guild_id>0)
  5773. base_exp -= guild_payexp(sd,base_exp);
  5774. }
  5775. flag = ((base_exp) ? 1 : 0) |
  5776. ((job_exp) ? 2 : 0) |
  5777. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5778. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5779. if (!(exp_flag&2))
  5780. pc_calcexp(sd, &base_exp, &job_exp, src);
  5781. nextb = pc_nextbaseexp(sd);
  5782. nextj = pc_nextjobexp(sd);
  5783. if (flag&4){
  5784. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5785. base_exp = 0;
  5786. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5787. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5788. }
  5789. if (flag&8){
  5790. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5791. job_exp = 0;
  5792. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5793. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5794. }
  5795. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5796. //Note that this value should never be greater than the original
  5797. //therefore no overflow checks are needed. [Skotlex]
  5798. if (nextb > 0) {
  5799. float nextbp = (float) base_exp / (float) nextb;
  5800. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5801. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5802. }
  5803. if (nextj > 0) {
  5804. float nextjp = (float) job_exp / (float) nextj;
  5805. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5806. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5807. }
  5808. }
  5809. // Give EXP for Base Level
  5810. if (base_exp) {
  5811. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5812. sd->status.base_exp = UINT32_MAX;
  5813. else
  5814. sd->status.base_exp += base_exp;
  5815. if (!pc_checkbaselevelup(sd))
  5816. clif_updatestatus(sd,SP_BASEEXP);
  5817. }
  5818. // Give EXP for Job Level
  5819. if (job_exp) {
  5820. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5821. sd->status.job_exp = UINT32_MAX;
  5822. else
  5823. sd->status.job_exp += job_exp;
  5824. if (!pc_checkjoblevelup(sd))
  5825. clif_updatestatus(sd,SP_JOBEXP);
  5826. }
  5827. if (flag&1)
  5828. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5829. if (flag&2)
  5830. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5831. if (sd->state.showexp)
  5832. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5833. }
  5834. /**
  5835. * Lost Base/Job EXP from a player
  5836. * @param sd Player
  5837. * @param base_exp Base EXP lost
  5838. * @param job_exp Job EXP lost
  5839. **/
  5840. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  5841. nullpo_retv(sd);
  5842. if (base_exp) {
  5843. base_exp = u32min(sd->status.base_exp, base_exp);
  5844. sd->status.base_exp -= base_exp;
  5845. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  5846. clif_updatestatus(sd, SP_BASEEXP);
  5847. }
  5848. if (job_exp) {
  5849. job_exp = u32min(sd->status.job_exp, job_exp);
  5850. sd->status.job_exp -= job_exp;
  5851. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  5852. clif_updatestatus(sd, SP_JOBEXP);
  5853. }
  5854. if (sd->state.showexp)
  5855. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  5856. }
  5857. /**
  5858. * Returns max base level for this character.
  5859. * @param sd Player
  5860. * @return Max Base Level
  5861. **/
  5862. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5863. return job_info[pc_class2idx(sd->status.class_)].max_level[0];
  5864. }
  5865. /**
  5866. * Returns max job level for this character.
  5867. * @param sd Player
  5868. * @return Max Job Level
  5869. **/
  5870. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5871. return job_info[pc_class2idx(sd->status.class_)].max_level[1];
  5872. }
  5873. /**
  5874. * Check if player is reached max base level
  5875. * @param sd
  5876. * @return True if reached max level
  5877. **/
  5878. bool pc_is_maxbaselv(struct map_session_data *sd) {
  5879. nullpo_retr(false, sd);
  5880. return (sd->status.base_level >= pc_maxbaselv(sd));
  5881. }
  5882. /**
  5883. * Check if player is reached max base level
  5884. * @param sd
  5885. * @return True if reached max level
  5886. **/
  5887. bool pc_is_maxjoblv(struct map_session_data *sd) {
  5888. nullpo_retr(false, sd);
  5889. return (sd->status.job_level >= pc_maxjoblv(sd));
  5890. }
  5891. /**
  5892. * Base exp needed for player to level up.
  5893. * @param sd
  5894. * @return Base EXP needed for next base level
  5895. **/
  5896. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  5897. nullpo_ret(sd);
  5898. if (sd->status.base_level == 0) // Is this something that possible?
  5899. return 0;
  5900. if (pc_is_maxbaselv(sd))
  5901. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  5902. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  5903. }
  5904. /**
  5905. * Job exp needed for player to level up.
  5906. * @param sd
  5907. * @return Job EXP needed for next job level
  5908. **/
  5909. unsigned int pc_nextjobexp(struct map_session_data *sd){
  5910. nullpo_ret(sd);
  5911. if (sd->status.job_level == 0) // Is this something that possible?
  5912. return 0;
  5913. if (pc_is_maxjoblv(sd))
  5914. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  5915. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  5916. }
  5917. /// Returns the value of the specified stat.
  5918. static int pc_getstat(struct map_session_data* sd, int type)
  5919. {
  5920. nullpo_retr(-1, sd);
  5921. switch( type ) {
  5922. case SP_STR: return sd->status.str;
  5923. case SP_AGI: return sd->status.agi;
  5924. case SP_VIT: return sd->status.vit;
  5925. case SP_INT: return sd->status.int_;
  5926. case SP_DEX: return sd->status.dex;
  5927. case SP_LUK: return sd->status.luk;
  5928. default:
  5929. return -1;
  5930. }
  5931. }
  5932. /// Sets the specified stat to the specified value.
  5933. /// Returns the new value.
  5934. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5935. {
  5936. nullpo_retr(-1, sd);
  5937. switch( type ) {
  5938. case SP_STR: sd->status.str = val; break;
  5939. case SP_AGI: sd->status.agi = val; break;
  5940. case SP_VIT: sd->status.vit = val; break;
  5941. case SP_INT: sd->status.int_ = val; break;
  5942. case SP_DEX: sd->status.dex = val; break;
  5943. case SP_LUK: sd->status.luk = val; break;
  5944. default:
  5945. return -1;
  5946. }
  5947. return val;
  5948. }
  5949. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5950. int pc_gets_status_point(int level)
  5951. {
  5952. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5953. return (statp[level+1] - statp[level]);
  5954. else //Default increase
  5955. return ((level+15) / 5);
  5956. }
  5957. #ifdef RENEWAL_STAT
  5958. /// Renewal status point cost formula
  5959. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  5960. #else
  5961. /// Pre-Renewal status point cost formula
  5962. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  5963. #endif
  5964. /// Returns the number of stat points needed to change the specified stat by val.
  5965. /// If val is negative, returns the number of stat points that would be needed to
  5966. /// raise the specified stat from (current value - val) to current value.
  5967. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5968. {
  5969. int low, high, sp = 0, max = 0;
  5970. if ( val == 0 )
  5971. return 0;
  5972. low = pc_getstat(sd,type);
  5973. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  5974. if ( low >= max && val > 0 )
  5975. return 0; // Official servers show '0' when max is reached
  5976. high = low + val;
  5977. if ( val < 0 )
  5978. swap(low, high);
  5979. for ( ; low < high; low++ )
  5980. sp += PC_STATUS_POINT_COST(low);
  5981. return sp;
  5982. }
  5983. /**
  5984. * Returns the value the specified stat can be increased by with the current
  5985. * amount of available status points for the current character's class.
  5986. *
  5987. * @param sd The target character.
  5988. * @param type Stat to verify.
  5989. * @return Maximum value the stat could grow by.
  5990. */
  5991. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  5992. {
  5993. int base, final_val, status_points, max_param;
  5994. nullpo_ret(sd);
  5995. base = final_val = pc_getstat(sd, type);
  5996. status_points = sd->status.status_point;
  5997. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  5998. while (final_val <= max_param && status_points >= 0) {
  5999. status_points -= PC_STATUS_POINT_COST(final_val);
  6000. final_val++;
  6001. }
  6002. final_val--;
  6003. return (final_val > base ? final_val-base : 0);
  6004. }
  6005. /**
  6006. * Raises a stat by the specified amount.
  6007. *
  6008. * Obeys max_parameter limits.
  6009. * Subtracts status points according to the cost of the increased stat points.
  6010. *
  6011. * @param sd The target character.
  6012. * @param type The stat to change (see enum _sp)
  6013. * @param increase The stat increase (strictly positive) amount.
  6014. * @retval true if the stat was increased by any amount.
  6015. * @retval false if there were no changes.
  6016. */
  6017. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6018. {
  6019. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6020. nullpo_ret(sd);
  6021. // check conditions
  6022. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6023. clif_statusupack(sd, type, 0, 0);
  6024. return false;
  6025. }
  6026. // check limits
  6027. current = pc_getstat(sd, type);
  6028. max_increase = pc_maxparameterincrease(sd, type);
  6029. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6030. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6031. clif_statusupack(sd, type, 0, 0);
  6032. return false;
  6033. }
  6034. // check status points
  6035. needed_points = pc_need_status_point(sd, type, increase);
  6036. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6037. clif_statusupack(sd, type, 0, 0);
  6038. return false;
  6039. }
  6040. // set new values
  6041. final_value = pc_setstat(sd, type, current + increase);
  6042. sd->status.status_point -= needed_points;
  6043. status_calc_pc(sd,SCO_NONE);
  6044. // update increase cost indicator
  6045. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6046. // update statpoint count
  6047. clif_updatestatus(sd, SP_STATUSPOINT);
  6048. // update stat value
  6049. clif_statusupack(sd, type, 1, final_value); // required
  6050. if( final_value > 255 )
  6051. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6052. return true;
  6053. }
  6054. /**
  6055. * Raises a stat by the specified amount.
  6056. *
  6057. * Obeys max_parameter limits.
  6058. * Does not subtract status points for the cost of the modified stat points.
  6059. *
  6060. * @param sd The target character.
  6061. * @param type The stat to change (see enum _sp)
  6062. * @param val The stat increase (or decrease) amount.
  6063. * @return the stat increase amount.
  6064. * @retval 0 if no changes were made.
  6065. */
  6066. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6067. {
  6068. int max, need;
  6069. nullpo_ret(sd);
  6070. if( type < SP_STR || type > SP_LUK )
  6071. {
  6072. clif_statusupack(sd,type,0,0);
  6073. return 0;
  6074. }
  6075. need = pc_need_status_point(sd,type,1);
  6076. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6077. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6078. status_calc_pc(sd,SCO_NONE);
  6079. // update increase cost indicator
  6080. if( need != pc_need_status_point(sd,type,1) )
  6081. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6082. // update stat value
  6083. clif_statusupack(sd,type,1,val); // required
  6084. if( val > 255 )
  6085. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6086. return val;
  6087. }
  6088. /*==========================================
  6089. * Update skill_lv for player sd
  6090. * Skill point allocation
  6091. *------------------------------------------*/
  6092. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6093. {
  6094. uint16 idx = skill_get_index(skill_id);
  6095. nullpo_retv(sd);
  6096. if (!idx) {
  6097. if (skill_id)
  6098. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6099. return;
  6100. }
  6101. // Level up guild skill
  6102. if (SKILL_CHK_GUILD(skill_id)) {
  6103. guild_skillup(sd, skill_id);
  6104. return;
  6105. }
  6106. // Level up homunculus skill
  6107. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6108. hom_skillup(sd->hd, skill_id);
  6109. return;
  6110. }
  6111. else {
  6112. if( sd->status.skill_point > 0 &&
  6113. sd->status.skill[idx].id &&
  6114. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6115. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6116. {
  6117. int lv, range, upgradable;
  6118. sd->status.skill[idx].lv++;
  6119. sd->status.skill_point--;
  6120. if( !skill_get_inf(skill_id) )
  6121. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6122. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6123. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6124. else
  6125. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6126. lv = sd->status.skill[idx].lv;
  6127. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6128. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6129. clif_skillup(sd,skill_id,lv,range,upgradable);
  6130. clif_updatestatus(sd,SP_SKILLPOINT);
  6131. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6132. clif_updatestatus(sd,SP_CARTINFO);
  6133. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6134. clif_skillinfoblock(sd);
  6135. }
  6136. //else
  6137. // ShowDebug("Skill Level up failed. ID:%d idx:%d (CID=%d. AID=%d)\n", skill_id, idx, sd->status.char_id, sd->status.account_id);
  6138. }
  6139. }
  6140. /*==========================================
  6141. * /allskill
  6142. *------------------------------------------*/
  6143. int pc_allskillup(struct map_session_data *sd)
  6144. {
  6145. int i;
  6146. nullpo_ret(sd);
  6147. for (i = 0; i < MAX_SKILL; i++) {
  6148. if (sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED) {
  6149. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6150. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6151. if (sd->status.skill[i].lv == 0)
  6152. sd->status.skill[i].id = 0;
  6153. }
  6154. }
  6155. if (!pc_grant_allskills(sd, true)) {
  6156. uint16 sk_id;
  6157. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].id) > 0;i++){
  6158. int inf2 = 0;
  6159. uint16 sk_idx = 0;
  6160. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6161. continue;
  6162. inf2 = skill_get_inf2(sk_id);
  6163. if (
  6164. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6165. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6166. sk_id == SG_DEVIL
  6167. )
  6168. continue; //Cannot be learned normally.
  6169. sd->status.skill[sk_idx].id = sk_id;
  6170. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6171. }
  6172. }
  6173. status_calc_pc(sd,SCO_NONE);
  6174. //Required because if you could level up all skills previously,
  6175. //the update will not be sent as only the lv variable changes.
  6176. clif_skillinfoblock(sd);
  6177. return 0;
  6178. }
  6179. /*==========================================
  6180. * /resetlvl
  6181. *------------------------------------------*/
  6182. int pc_resetlvl(struct map_session_data* sd,int type)
  6183. {
  6184. int i;
  6185. nullpo_ret(sd);
  6186. if (type != 3) //Also reset skills
  6187. pc_resetskill(sd, 0);
  6188. if(type == 1){
  6189. sd->status.skill_point=0;
  6190. sd->status.base_level=1;
  6191. sd->status.job_level=1;
  6192. sd->status.base_exp=0;
  6193. sd->status.job_exp=0;
  6194. if(sd->sc.option !=0)
  6195. sd->sc.option = 0;
  6196. sd->status.str=1;
  6197. sd->status.agi=1;
  6198. sd->status.vit=1;
  6199. sd->status.int_=1;
  6200. sd->status.dex=1;
  6201. sd->status.luk=1;
  6202. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6203. sd->status.status_point=100; // not 88 [celest]
  6204. // give platinum skills upon changing
  6205. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6206. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6207. }
  6208. }
  6209. if(type == 2){
  6210. sd->status.skill_point=0;
  6211. sd->status.base_level=1;
  6212. sd->status.job_level=1;
  6213. sd->status.base_exp=0;
  6214. sd->status.job_exp=0;
  6215. }
  6216. if(type == 3){
  6217. sd->status.base_level=1;
  6218. sd->status.base_exp=0;
  6219. }
  6220. if(type == 4){
  6221. sd->status.job_level=1;
  6222. sd->status.job_exp=0;
  6223. }
  6224. clif_updatestatus(sd,SP_STATUSPOINT);
  6225. clif_updatestatus(sd,SP_STR);
  6226. clif_updatestatus(sd,SP_AGI);
  6227. clif_updatestatus(sd,SP_VIT);
  6228. clif_updatestatus(sd,SP_INT);
  6229. clif_updatestatus(sd,SP_DEX);
  6230. clif_updatestatus(sd,SP_LUK);
  6231. clif_updatestatus(sd,SP_BASELEVEL);
  6232. clif_updatestatus(sd,SP_JOBLEVEL);
  6233. clif_updatestatus(sd,SP_STATUSPOINT);
  6234. clif_updatestatus(sd,SP_BASEEXP);
  6235. clif_updatestatus(sd,SP_JOBEXP);
  6236. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6237. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6238. clif_updatestatus(sd,SP_SKILLPOINT);
  6239. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6240. clif_updatestatus(sd,SP_UAGI);
  6241. clif_updatestatus(sd,SP_UVIT);
  6242. clif_updatestatus(sd,SP_UINT);
  6243. clif_updatestatus(sd,SP_UDEX);
  6244. clif_updatestatus(sd,SP_ULUK); // End Addition
  6245. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6246. if(sd->equip_index[i] >= 0)
  6247. if(pc_isequip(sd,sd->equip_index[i]))
  6248. pc_unequipitem(sd,sd->equip_index[i],2);
  6249. }
  6250. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6251. party_send_levelup(sd);
  6252. status_calc_pc(sd, SCO_FORCE);
  6253. clif_skillinfoblock(sd);
  6254. return 0;
  6255. }
  6256. /*==========================================
  6257. * /resetstate
  6258. *------------------------------------------*/
  6259. int pc_resetstate(struct map_session_data* sd)
  6260. {
  6261. nullpo_ret(sd);
  6262. if (battle_config.use_statpoint_table)
  6263. { // New statpoint table used here - Dexity
  6264. if (sd->status.base_level > MAX_LEVEL)
  6265. { //statp[] goes out of bounds, can't reset!
  6266. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6267. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6268. return 0;
  6269. }
  6270. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6271. }
  6272. else
  6273. {
  6274. int add=0;
  6275. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6276. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6277. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6278. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6279. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6280. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6281. sd->status.status_point+=add;
  6282. }
  6283. pc_setstat(sd, SP_STR, 1);
  6284. pc_setstat(sd, SP_AGI, 1);
  6285. pc_setstat(sd, SP_VIT, 1);
  6286. pc_setstat(sd, SP_INT, 1);
  6287. pc_setstat(sd, SP_DEX, 1);
  6288. pc_setstat(sd, SP_LUK, 1);
  6289. clif_updatestatus(sd,SP_STR);
  6290. clif_updatestatus(sd,SP_AGI);
  6291. clif_updatestatus(sd,SP_VIT);
  6292. clif_updatestatus(sd,SP_INT);
  6293. clif_updatestatus(sd,SP_DEX);
  6294. clif_updatestatus(sd,SP_LUK);
  6295. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6296. clif_updatestatus(sd,SP_UAGI);
  6297. clif_updatestatus(sd,SP_UVIT);
  6298. clif_updatestatus(sd,SP_UINT);
  6299. clif_updatestatus(sd,SP_UDEX);
  6300. clif_updatestatus(sd,SP_ULUK); // End Addition
  6301. clif_updatestatus(sd,SP_STATUSPOINT);
  6302. if( sd->mission_mobid ) { //bugreport:2200
  6303. sd->mission_mobid = 0;
  6304. sd->mission_count = 0;
  6305. pc_setglobalreg(sd, add_str("TK_MISSION_ID"), 0);
  6306. }
  6307. status_calc_pc(sd, SCO_NONE);
  6308. return 1;
  6309. }
  6310. /*==========================================
  6311. * /resetskill
  6312. * if flag&1, perform block resync and status_calc call.
  6313. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6314. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6315. *------------------------------------------*/
  6316. int pc_resetskill(struct map_session_data* sd, int flag)
  6317. {
  6318. int i, skill_point=0;
  6319. nullpo_ret(sd);
  6320. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6321. return 0;
  6322. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6323. /**
  6324. * It has been confirmed on official servers that when you reset skills with a ranked Taekwon your skills are not reset (because you have all of them anyway)
  6325. **/
  6326. if( pc_is_taekwon_ranker(sd) )
  6327. return 0;
  6328. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6329. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6330. i = sd->sc.option;
  6331. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6332. i &= ~OPTION_RIDING;
  6333. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6334. i &= ~OPTION_FALCON;
  6335. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6336. i &= ~OPTION_DRAGON;
  6337. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6338. i &= ~OPTION_WUG;
  6339. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6340. i &= ~OPTION_WUGRIDER;
  6341. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6342. i &= ~OPTION_MADOGEAR;
  6343. #ifndef NEW_CARTS
  6344. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6345. i &= ~OPTION_CART;
  6346. #else
  6347. if( sd->sc.data[SC_PUSH_CART] )
  6348. pc_setcart(sd, 0);
  6349. #endif
  6350. if( i != sd->sc.option )
  6351. pc_setoption(sd, i);
  6352. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6353. hom_vaporize(sd, HOM_ST_ACTIVE);
  6354. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6355. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6356. }
  6357. for( i = 1; i < MAX_SKILL; i++ )
  6358. {
  6359. uint8 lv = sd->status.skill[i].lv;
  6360. int inf2;
  6361. uint16 skill_id = skill_idx2id(i);
  6362. if (lv == 0 || skill_id == 0)
  6363. continue;
  6364. inf2 = skill_get_inf2(skill_id);
  6365. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6366. continue;
  6367. // Don't reset trick dead if not a novice/baby
  6368. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6369. {
  6370. sd->status.skill[i].lv = 0;
  6371. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6372. continue;
  6373. }
  6374. // do not reset basic skill
  6375. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6376. continue;
  6377. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6378. continue;
  6379. if( flag&4 && !skill_ischangesex(skill_id) )
  6380. continue;
  6381. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6382. { //Only handle quest skills in a special way when you can't learn them manually
  6383. if( battle_config.quest_skill_reset && !(flag&2) )
  6384. { //Wipe them
  6385. sd->status.skill[i].lv = 0;
  6386. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6387. }
  6388. continue;
  6389. }
  6390. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6391. skill_point += lv;
  6392. else
  6393. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6394. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6395. if( !(flag&2) )
  6396. {// reset
  6397. sd->status.skill[i].lv = 0;
  6398. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6399. }
  6400. }
  6401. if( flag&2 || !skill_point ) return skill_point;
  6402. sd->status.skill_point += skill_point;
  6403. if (flag&1) {
  6404. clif_updatestatus(sd,SP_SKILLPOINT);
  6405. clif_skillinfoblock(sd);
  6406. status_calc_pc(sd, SCO_FORCE);
  6407. }
  6408. return skill_point;
  6409. }
  6410. /*==========================================
  6411. * /resetfeel [Komurka]
  6412. *------------------------------------------*/
  6413. int pc_resetfeel(struct map_session_data* sd)
  6414. {
  6415. int i;
  6416. nullpo_ret(sd);
  6417. for (i=0; i<MAX_PC_FEELHATE; i++)
  6418. {
  6419. sd->feel_map[i].m = -1;
  6420. sd->feel_map[i].index = 0;
  6421. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6422. }
  6423. return 0;
  6424. }
  6425. int pc_resethate(struct map_session_data* sd)
  6426. {
  6427. int i;
  6428. nullpo_ret(sd);
  6429. for (i=0; i<3; i++)
  6430. {
  6431. sd->hate_mob[i] = -1;
  6432. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6433. }
  6434. return 0;
  6435. }
  6436. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6437. {
  6438. int i, bonus = 0;
  6439. nullpo_ret(sd);
  6440. skill_id = skill_dummy2skill_id(skill_id);
  6441. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6442. if( i < ARRAYLENGTH(sd->skillatk) )
  6443. bonus = sd->skillatk[i].val;
  6444. return bonus;
  6445. }
  6446. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6447. {
  6448. int i, bonus = 0;
  6449. nullpo_ret(sd);
  6450. skill_id = skill_dummy2skill_id(skill_id);
  6451. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6452. if( i < ARRAYLENGTH(sd->subskill) )
  6453. bonus = sd->subskill[i].val;
  6454. return bonus;
  6455. }
  6456. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6457. int i, bonus = sd->bonus.add_heal_rate;
  6458. skill_id = skill_dummy2skill_id(skill_id);
  6459. if( bonus ) {
  6460. switch( skill_id ) {
  6461. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6462. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6463. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6464. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6465. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6466. }
  6467. }
  6468. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6469. if( i < ARRAYLENGTH(sd->skillheal) )
  6470. bonus += sd->skillheal[i].val;
  6471. return bonus;
  6472. }
  6473. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6474. int i, bonus = sd->bonus.add_heal2_rate;
  6475. skill_id = skill_dummy2skill_id(skill_id);
  6476. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6477. if( i < ARRAYLENGTH(sd->skillheal2) )
  6478. bonus += sd->skillheal2[i].val;
  6479. return bonus;
  6480. }
  6481. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6482. {
  6483. if( !pc_isdead(sd) )
  6484. return; // not applicable
  6485. if( sd->bg_id && bg_member_respawn(sd) )
  6486. return; // member revived by battleground
  6487. pc_setstand(sd, true);
  6488. pc_setrestartvalue(sd,3);
  6489. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6490. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6491. }
  6492. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6493. {
  6494. struct map_session_data *sd = map_id2sd(id);
  6495. if( sd != NULL )
  6496. {
  6497. sd->pvp_point=0;
  6498. pc_respawn(sd,CLR_OUTSIGHT);
  6499. }
  6500. return 0;
  6501. }
  6502. /*==========================================
  6503. * Invoked when a player has received damage
  6504. *------------------------------------------*/
  6505. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6506. {
  6507. if (sp) clif_updatestatus(sd,SP_SP);
  6508. if (hp) clif_updatestatus(sd,SP_HP);
  6509. else return;
  6510. if (!src)
  6511. return;
  6512. if( pc_issit(sd) ) {
  6513. pc_setstand(sd, true);
  6514. skill_sit(sd,0);
  6515. }
  6516. if( sd->progressbar.npc_id )
  6517. clif_progressbar_abort(sd);
  6518. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6519. pet_target_check(sd->pd,src,1);
  6520. if( sd->status.ele_id > 0 )
  6521. elemental_set_target(sd,src);
  6522. sd->canlog_tick = gettick();
  6523. }
  6524. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6525. {
  6526. TBL_PC *sd = map_id2sd(id);
  6527. if(sd) pc_close_npc(sd,data);
  6528. return 0;
  6529. }
  6530. /*
  6531. * Method to properly close npc for player and clear anything related
  6532. * @flag == 1 : produce close button
  6533. * @flag == 2 : directly close it
  6534. */
  6535. void pc_close_npc(struct map_session_data *sd,int flag)
  6536. {
  6537. nullpo_retv(sd);
  6538. if (sd->npc_id || sd->npc_shopid) {
  6539. if (sd->state.using_fake_npc) {
  6540. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6541. sd->state.using_fake_npc = 0;
  6542. }
  6543. if (sd->st) {
  6544. if(sd->st->state == RUN){ //wait ending code execution
  6545. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6546. return;
  6547. }
  6548. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6549. sd->st->mes_active = 0;
  6550. }
  6551. sd->state.menu_or_input = 0;
  6552. sd->npc_menu = 0;
  6553. sd->npc_shopid = 0;
  6554. #ifdef SECURE_NPCTIMEOUT
  6555. sd->npc_idle_timer = INVALID_TIMER;
  6556. #endif
  6557. clif_scriptclose(sd,sd->npc_id);
  6558. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6559. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6560. script_free_state(sd->st);
  6561. sd->st = NULL;
  6562. sd->npc_id = 0;
  6563. }
  6564. }
  6565. }
  6566. /*==========================================
  6567. * Invoked when a player has negative current hp
  6568. *------------------------------------------*/
  6569. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6570. {
  6571. int i=0,k=0;
  6572. unsigned int tick = gettick();
  6573. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6574. // Super Novices have no kill or die functions attached when saved by their angel
  6575. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6576. unsigned int exp = pc_nextbaseexp(sd);
  6577. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6578. sd->state.snovice_dead_flag = 1;
  6579. pc_setrestartvalue(sd,1);
  6580. status_percent_heal(&sd->bl, 100, 100);
  6581. clif_resurrection(&sd->bl, 1);
  6582. if(battle_config.pc_invincible_time)
  6583. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6584. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6585. if(map_flag_gvg(sd->bl.m))
  6586. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6587. return 0;
  6588. }
  6589. }
  6590. for(k = 0; k < MAX_DEVOTION; k++) {
  6591. if (sd->devotion[k]){
  6592. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6593. if (devsd)
  6594. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6595. sd->devotion[k] = 0;
  6596. }
  6597. }
  6598. if(sd->shadowform_id) { //if we were target of shadowform
  6599. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6600. sd->shadowform_id = 0; //should be remove on status end anyway
  6601. }
  6602. if(sd->status.pet_id > 0 && sd->pd) {
  6603. struct pet_data *pd = sd->pd;
  6604. if( !map[sd->bl.m].flag.noexppenalty ) {
  6605. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6606. if( pd->pet.intimate < 0 )
  6607. pd->pet.intimate = 0;
  6608. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6609. }
  6610. if( sd->pd->target_id ) // Unlock all targets...
  6611. pet_unlocktarget(sd->pd);
  6612. }
  6613. if (sd->status.hom_id > 0) {
  6614. if(battle_config.homunculus_auto_vapor && sd->hd)
  6615. hom_vaporize(sd, HOM_ST_ACTIVE);
  6616. }
  6617. if( sd->md )
  6618. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6619. if( sd->ed )
  6620. elemental_delete(sd->ed);
  6621. // Leave duel if you die [LuzZza]
  6622. if(battle_config.duel_autoleave_when_die) {
  6623. if(sd->duel_group > 0)
  6624. duel_leave(sd->duel_group, sd);
  6625. if(sd->duel_invite > 0)
  6626. duel_reject(sd->duel_invite, sd);
  6627. }
  6628. pc_close_npc(sd,2); //close npc if we were using one
  6629. /* e.g. not killed thru pc_damage */
  6630. if( pc_issit(sd) ) {
  6631. clif_status_load(&sd->bl,SI_SIT,0);
  6632. }
  6633. pc_setdead(sd);
  6634. pc_setglobalreg(sd, add_str("PC_DIE_COUNTER"), sd->die_counter+1);
  6635. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6636. //Reset menu skills/item skills
  6637. if ((sd->skillitem) != 0)
  6638. sd->skillitem = sd->skillitemlv = 0;
  6639. if ((sd->menuskill_id) != 0)
  6640. sd->menuskill_id = sd->menuskill_val = 0;
  6641. //Reset ticks.
  6642. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6643. if ( sd->spiritball !=0 )
  6644. pc_delspiritball(sd,sd->spiritball,0);
  6645. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6646. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6647. if (src)
  6648. switch (src->type) {
  6649. case BL_MOB:
  6650. {
  6651. struct mob_data *md=(struct mob_data *)src;
  6652. if(md->target_id==sd->bl.id)
  6653. mob_unlocktarget(md,tick);
  6654. if(battle_config.mobs_level_up && md->status.hp &&
  6655. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6656. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6657. ) { // monster level up [Valaris]
  6658. clif_misceffect(&md->bl,0);
  6659. md->level++;
  6660. status_calc_mob(md, SCO_NONE);
  6661. status_percent_heal(src,10,0);
  6662. if( battle_config.show_mob_info&4 )
  6663. {// update name with new level
  6664. clif_charnameack(0, &md->bl);
  6665. }
  6666. }
  6667. src = battle_get_master(src); // Maybe Player Summon
  6668. }
  6669. break;
  6670. case BL_PET: //Pass on to master...
  6671. case BL_HOM:
  6672. case BL_MER:
  6673. src = battle_get_master(src);
  6674. break;
  6675. }
  6676. if (src && src->type == BL_PC) {
  6677. struct map_session_data *ssd = (struct map_session_data *)src;
  6678. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6679. npc_script_event(ssd, NPCE_KILLPC);
  6680. if (battle_config.pk_mode&2) {
  6681. ssd->status.manner -= 5;
  6682. if(ssd->status.manner < 0)
  6683. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6684. #if 0
  6685. // PK/Karma system code (not enabled yet) [celest]
  6686. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6687. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6688. // karma going down = more 'good' / more honourable.
  6689. // The Karma System way...
  6690. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6691. sd->status.karma--;
  6692. ssd->status.karma--;
  6693. }
  6694. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6695. ssd->status.karma++;
  6696. // or the PK System way...
  6697. if (sd->status.karma > 0) // player killed is dishonourable?
  6698. ssd->status.karma--; // honour points earned
  6699. sd->status.karma++; // honour points lost
  6700. // To-do: Receive exp on certain occasions
  6701. #endif
  6702. }
  6703. }
  6704. if(battle_config.bone_drop==2
  6705. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6706. {
  6707. struct item item_tmp;
  6708. memset(&item_tmp,0,sizeof(item_tmp));
  6709. item_tmp.nameid=ITEMID_SKULL_;
  6710. item_tmp.identify=1;
  6711. item_tmp.card[0]=CARD0_CREATE;
  6712. item_tmp.card[1]=0;
  6713. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6714. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6715. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6716. }
  6717. //Remove bonus_script when dead
  6718. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6719. // changed penalty options, added death by player if pk_mode [Valaris]
  6720. if(battle_config.death_penalty_type
  6721. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6722. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6723. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6724. {
  6725. uint32 base_penalty = battle_config.death_penalty_base;
  6726. uint32 job_penalty = battle_config.death_penalty_job;
  6727. uint32 zeny_penalty = battle_config.zeny_penalty;
  6728. #ifdef VIP_ENABLE
  6729. if(pc_isvip(sd)){
  6730. base_penalty *= battle_config.vip_exp_penalty_base;
  6731. job_penalty *= battle_config.vip_exp_penalty_job;
  6732. }
  6733. else {
  6734. base_penalty *= battle_config.vip_exp_penalty_base_normal;
  6735. job_penalty *= battle_config.vip_exp_penalty_job_normal;
  6736. }
  6737. #endif
  6738. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6739. switch (battle_config.death_penalty_type) {
  6740. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6741. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6742. }
  6743. if (base_penalty){ //recheck after altering to speedup
  6744. if (battle_config.pk_mode && src && src->type==BL_PC)
  6745. base_penalty *= 2;
  6746. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6747. }
  6748. }
  6749. else
  6750. base_penalty = 0;
  6751. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6752. switch (battle_config.death_penalty_type) {
  6753. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6754. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6755. }
  6756. if (job_penalty) {
  6757. if (battle_config.pk_mode && src && src->type==BL_PC)
  6758. job_penalty *= 2;
  6759. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6760. }
  6761. }
  6762. else
  6763. job_penalty = 0;
  6764. if (base_penalty || job_penalty)
  6765. pc_lostexp(sd, base_penalty, job_penalty);
  6766. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6767. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6768. if(zeny_penalty)
  6769. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6770. }
  6771. }
  6772. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6773. int j;
  6774. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6775. int id = map[sd->bl.m].drop_list[j].drop_id;
  6776. int type = map[sd->bl.m].drop_list[j].drop_type;
  6777. int per = map[sd->bl.m].drop_list[j].drop_per;
  6778. if(id == 0)
  6779. continue;
  6780. if(id == -1){
  6781. int eq_num=0,eq_n[MAX_INVENTORY];
  6782. memset(eq_n,0,sizeof(eq_n));
  6783. for(i=0;i<MAX_INVENTORY;i++) {
  6784. if( (type == 1 && !sd->status.inventory[i].equip)
  6785. || (type == 2 && sd->status.inventory[i].equip)
  6786. || type == 3)
  6787. {
  6788. int l;
  6789. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6790. if( l < MAX_INVENTORY )
  6791. eq_n[l] = i;
  6792. eq_num++;
  6793. }
  6794. }
  6795. if(eq_num > 0){
  6796. int n = eq_n[rnd()%eq_num];
  6797. if(rnd()%10000 < per) {
  6798. if(sd->status.inventory[n].equip)
  6799. pc_unequipitem(sd,n,3);
  6800. pc_dropitem(sd,n,1);
  6801. }
  6802. }
  6803. }
  6804. else if(id > 0) {
  6805. for(i=0;i<MAX_INVENTORY;i++){
  6806. if(sd->status.inventory[i].nameid == id
  6807. && rnd()%10000 < per
  6808. && ((type == 1 && !sd->status.inventory[i].equip)
  6809. || (type == 2 && sd->status.inventory[i].equip)
  6810. || type == 3) ){
  6811. if(sd->status.inventory[i].equip)
  6812. pc_unequipitem(sd,i,3);
  6813. pc_dropitem(sd,i,1);
  6814. break;
  6815. }
  6816. }
  6817. }
  6818. }
  6819. }
  6820. // pvp
  6821. // disable certain pvp functions on pk_mode [Valaris]
  6822. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6823. sd->pvp_point -= 5;
  6824. sd->pvp_lost++;
  6825. if( src && src->type == BL_PC ) {
  6826. struct map_session_data *ssd = (struct map_session_data *)src;
  6827. ssd->pvp_point++;
  6828. ssd->pvp_won++;
  6829. }
  6830. if( sd->pvp_point < 0 ) {
  6831. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6832. return 1|8;
  6833. }
  6834. }
  6835. //GvG
  6836. if( map_flag_gvg(sd->bl.m) ) {
  6837. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6838. return 1|8;
  6839. }
  6840. else if( sd->bg_id ) {
  6841. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6842. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6843. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6844. return 1|8;
  6845. }
  6846. }
  6847. //Reset "can log out" tick.
  6848. if( battle_config.prevent_logout )
  6849. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6850. return 1;
  6851. }
  6852. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6853. if(hp) clif_updatestatus(sd,SP_HP);
  6854. if(sp) clif_updatestatus(sd,SP_SP);
  6855. pc_setstand(sd, true);
  6856. if(battle_config.pc_invincible_time > 0)
  6857. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6858. if( sd->state.gmaster_flag ) {
  6859. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  6860. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  6861. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  6862. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  6863. }
  6864. }
  6865. // script
  6866. //
  6867. /*==========================================
  6868. * script reading pc status registry
  6869. *------------------------------------------*/
  6870. int pc_readparam(struct map_session_data* sd,int type)
  6871. {
  6872. int val = 0;
  6873. nullpo_ret(sd);
  6874. switch(type) {
  6875. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6876. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6877. case SP_ZENY: val = sd->status.zeny; break;
  6878. case SP_BASELEVEL: val = sd->status.base_level; break;
  6879. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6880. case SP_CLASS: val = sd->status.class_; break;
  6881. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6882. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6883. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6884. case SP_SEX: val = sd->status.sex; break;
  6885. case SP_WEIGHT: val = sd->weight; break;
  6886. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6887. case SP_BASEEXP: val = sd->status.base_exp; break;
  6888. case SP_JOBEXP: val = sd->status.job_exp; break;
  6889. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6890. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6891. case SP_HP: val = sd->battle_status.hp; break;
  6892. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6893. case SP_SP: val = sd->battle_status.sp; break;
  6894. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6895. case SP_STR: val = sd->status.str; break;
  6896. case SP_AGI: val = sd->status.agi; break;
  6897. case SP_VIT: val = sd->status.vit; break;
  6898. case SP_INT: val = sd->status.int_; break;
  6899. case SP_DEX: val = sd->status.dex; break;
  6900. case SP_LUK: val = sd->status.luk; break;
  6901. case SP_KARMA: val = sd->status.karma; break;
  6902. case SP_MANNER: val = sd->status.manner; break;
  6903. case SP_FAME: val = sd->status.fame; break;
  6904. case SP_KILLERRID: val = sd->killerrid; break;
  6905. case SP_KILLEDRID: val = sd->killedrid; break;
  6906. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6907. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6908. case SP_CHARRENAME: val = sd->status.rename; break;
  6909. case SP_CHARFONT: val = sd->status.font; break;
  6910. case SP_BANK_VAULT: val = sd->bank_vault; break;
  6911. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  6912. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  6913. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  6914. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6915. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6916. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6917. case SP_DEF1: val = sd->battle_status.def; break;
  6918. case SP_DEF2: val = sd->battle_status.def2; break;
  6919. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6920. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6921. case SP_HIT: val = sd->battle_status.hit; break;
  6922. case SP_FLEE1: val = sd->battle_status.flee; break;
  6923. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6924. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6925. case SP_MAXHPRATE: val = sd->hprate; break;
  6926. case SP_MAXSPRATE: val = sd->sprate; break;
  6927. case SP_SPRATE: val = sd->dsprate; break;
  6928. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6929. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6930. case SP_ASPD_RATE:
  6931. #ifndef RENEWAL_ASPD
  6932. val = sd->battle_status.aspd_rate;
  6933. #else
  6934. val = sd->battle_status.aspd_rate2;
  6935. #endif
  6936. break;
  6937. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6938. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6939. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6940. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6941. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6942. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6943. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6944. case SP_MATK_RATE: val = sd->matk_rate; break;
  6945. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6946. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6947. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6948. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6949. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6950. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6951. case SP_HIT_RATE: val = sd->hit_rate; break;
  6952. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6953. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6954. case SP_DEF_RATE: val = sd->def_rate; break;
  6955. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6956. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6957. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6958. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6959. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6960. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6961. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6962. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6963. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6964. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6965. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6966. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6967. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6968. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6969. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6970. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6971. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6972. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6973. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6974. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6975. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6976. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6977. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6978. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6979. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6980. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6981. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6982. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6983. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6984. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6985. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6986. case SP_DELAYRATE: val = sd->delayrate; break;
  6987. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6988. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6989. case SP_UNSTRIPABLE:
  6990. case SP_UNSTRIPABLE_ARMOR:
  6991. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6992. break;
  6993. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6994. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6995. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6996. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6997. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6998. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6999. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7000. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7001. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7002. case SP_EMATK: val = sd->bonus.ematk; break;
  7003. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7004. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7005. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7006. case SP_CASTRATE:
  7007. case SP_VARCASTRATE:
  7008. #ifdef RENEWAL_CAST
  7009. val = sd->bonus.varcastrate; break;
  7010. #else
  7011. val = sd->castrate; break;
  7012. #endif
  7013. default:
  7014. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7015. return -1;
  7016. }
  7017. return val;
  7018. }
  7019. /*==========================================
  7020. * script set pc status registry
  7021. *------------------------------------------*/
  7022. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7023. {
  7024. nullpo_retr(false,sd);
  7025. switch(type){
  7026. case SP_BASELEVEL:
  7027. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7028. val = pc_maxbaselv(sd);
  7029. if ((unsigned int)val > sd->status.base_level) {
  7030. int i = 0;
  7031. int stat=0;
  7032. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7033. stat += pc_gets_status_point(sd->status.base_level + i);
  7034. sd->status.status_point += stat;
  7035. }
  7036. sd->status.base_level = (unsigned int)val;
  7037. sd->status.base_exp = 0;
  7038. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7039. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7040. clif_updatestatus(sd, SP_STATUSPOINT);
  7041. clif_updatestatus(sd, SP_BASEEXP);
  7042. status_calc_pc(sd, SCO_FORCE);
  7043. if(sd->status.party_id)
  7044. party_send_levelup(sd);
  7045. break;
  7046. case SP_JOBLEVEL:
  7047. if ((unsigned int)val >= sd->status.job_level) {
  7048. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7049. sd->status.skill_point += val - sd->status.job_level;
  7050. clif_updatestatus(sd, SP_SKILLPOINT);
  7051. }
  7052. sd->status.job_level = (unsigned int)val;
  7053. sd->status.job_exp = 0;
  7054. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7055. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7056. clif_updatestatus(sd, SP_JOBEXP);
  7057. status_calc_pc(sd, SCO_FORCE);
  7058. break;
  7059. case SP_SKILLPOINT:
  7060. sd->status.skill_point = val;
  7061. break;
  7062. case SP_STATUSPOINT:
  7063. sd->status.status_point = val;
  7064. break;
  7065. case SP_ZENY:
  7066. if( val < 0 )
  7067. return false;// can't set negative zeny
  7068. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7069. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7070. break;
  7071. case SP_BASEEXP:
  7072. {
  7073. val = cap_value(val, 0, INT_MAX);
  7074. if ((unsigned int)val < sd->status.base_exp) // Lost
  7075. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7076. else // Gained
  7077. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7078. }
  7079. return true;
  7080. case SP_JOBEXP:
  7081. {
  7082. val = cap_value(val, 0, INT_MAX);
  7083. if ((unsigned int)val < sd->status.job_exp) // Lost
  7084. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7085. else // Gained
  7086. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7087. }
  7088. return true;
  7089. case SP_SEX:
  7090. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7091. break;
  7092. case SP_WEIGHT:
  7093. sd->weight = val;
  7094. break;
  7095. case SP_MAXWEIGHT:
  7096. sd->max_weight = val;
  7097. break;
  7098. case SP_HP:
  7099. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7100. break;
  7101. case SP_MAXHP:
  7102. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7103. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7104. {
  7105. sd->battle_status.hp = sd->battle_status.max_hp;
  7106. clif_updatestatus(sd, SP_HP);
  7107. }
  7108. break;
  7109. case SP_SP:
  7110. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7111. break;
  7112. case SP_MAXSP:
  7113. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7114. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7115. {
  7116. sd->battle_status.sp = sd->battle_status.max_sp;
  7117. clif_updatestatus(sd, SP_SP);
  7118. }
  7119. break;
  7120. case SP_STR:
  7121. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7122. break;
  7123. case SP_AGI:
  7124. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7125. break;
  7126. case SP_VIT:
  7127. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7128. break;
  7129. case SP_INT:
  7130. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7131. break;
  7132. case SP_DEX:
  7133. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7134. break;
  7135. case SP_LUK:
  7136. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7137. break;
  7138. case SP_KARMA:
  7139. sd->status.karma = val;
  7140. break;
  7141. case SP_MANNER:
  7142. sd->status.manner = val;
  7143. if( val < 0 )
  7144. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7145. else {
  7146. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7147. clif_manner_message(sd, 5);
  7148. }
  7149. return true; // status_change_start/status_change_end already sends packets warning the client
  7150. case SP_FAME:
  7151. sd->status.fame = val;
  7152. break;
  7153. case SP_KILLERRID:
  7154. sd->killerrid = val;
  7155. return true;
  7156. case SP_KILLEDRID:
  7157. sd->killedrid = val;
  7158. return true;
  7159. case SP_CHARMOVE:
  7160. sd->status.character_moves = val;
  7161. return true;
  7162. case SP_CHARRENAME:
  7163. sd->status.rename = val;
  7164. return true;
  7165. case SP_CHARFONT:
  7166. sd->status.font = val;
  7167. clif_font(sd);
  7168. return true;
  7169. case SP_BANK_VAULT:
  7170. if (val < 0)
  7171. return false;
  7172. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7173. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7174. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7175. return true;
  7176. case SP_ROULETTE_BRONZE:
  7177. sd->roulette_point.bronze = val;
  7178. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7179. return true;
  7180. case SP_ROULETTE_SILVER:
  7181. sd->roulette_point.silver = val;
  7182. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7183. return true;
  7184. case SP_ROULETTE_GOLD:
  7185. sd->roulette_point.gold = val;
  7186. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7187. return true;
  7188. default:
  7189. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7190. return false;
  7191. }
  7192. clif_updatestatus(sd,type);
  7193. return true;
  7194. }
  7195. /*==========================================
  7196. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7197. *------------------------------------------*/
  7198. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7199. {
  7200. if (type) {
  7201. if (hp)
  7202. clif_heal(sd->fd,SP_HP,hp);
  7203. if (sp)
  7204. clif_heal(sd->fd,SP_SP,sp);
  7205. } else {
  7206. if(hp)
  7207. clif_updatestatus(sd,SP_HP);
  7208. if(sp)
  7209. clif_updatestatus(sd,SP_SP);
  7210. }
  7211. return;
  7212. }
  7213. /*==========================================
  7214. * HP/SP Recovery
  7215. * Heal player hp and/or sp linearly.
  7216. * Calculate bonus by status.
  7217. *------------------------------------------*/
  7218. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  7219. {
  7220. int bonus, tmp, penalty = 0;
  7221. if(hp) {
  7222. int i;
  7223. bonus = 100 + (sd->battle_status.vit<<1)
  7224. + pc_checkskill(sd,SM_RECOVERY)*10
  7225. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7226. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7227. if (potion_flag > 1)
  7228. bonus += bonus*(potion_flag-1)*50/100;
  7229. //All item bonuses.
  7230. bonus += sd->bonus.itemhealrate2;
  7231. //Item Group bonuses
  7232. bonus += bonus*pc_get_itemgroup_bonus(sd, itemid)/100;
  7233. //Individual item bonuses.
  7234. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  7235. {
  7236. if (sd->itemhealrate[i].nameid == itemid) {
  7237. bonus += bonus*sd->itemhealrate[i].rate/100;
  7238. break;
  7239. }
  7240. }
  7241. tmp = hp * bonus / 100; // overflow check
  7242. if(bonus != 100 && tmp > hp)
  7243. hp = tmp;
  7244. // Recovery Potion
  7245. if( sd->sc.data[SC_INCHEALRATE] )
  7246. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  7247. // 2014 Halloween Event : Pumpkin Bonus
  7248. if(sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7249. hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100.);
  7250. }
  7251. if(sp) {
  7252. bonus = 100 + (sd->battle_status.int_<<1)
  7253. + pc_checkskill(sd,MG_SRECOVERY)*10
  7254. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7255. if (potion_flag > 1)
  7256. bonus += bonus*(potion_flag-1)*50/100;
  7257. tmp = sp * bonus / 100;
  7258. if(bonus != 100 && tmp > sp)
  7259. sp = tmp;
  7260. }
  7261. if (sd->sc.count) {
  7262. // Critical Wound and Death Hurt stack
  7263. if (sd->sc.data[SC_CRITICALWOUND])
  7264. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7265. if (sd->sc.data[SC_DEATHHURT])
  7266. penalty += 20;
  7267. if (sd->sc.data[SC_NORECOVER_STATE])
  7268. penalty = 100;
  7269. if (penalty > 0) {
  7270. hp -= hp * penalty / 100;
  7271. sp -= sp * penalty / 100;
  7272. }
  7273. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7274. hp += hp / 2; // 1.5 times
  7275. sp -= sp / 2;
  7276. }
  7277. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7278. hp += hp / 10;
  7279. sp += sp / 10;
  7280. }
  7281. #ifdef RENEWAL
  7282. if (sd->sc.data[SC_EXTREMITYFIST2])
  7283. sp = 0;
  7284. #endif
  7285. if (sd->sc.data[SC_BITESCAR])
  7286. hp = 0;
  7287. }
  7288. return status_heal(&sd->bl, hp, sp, 1);
  7289. }
  7290. /*==========================================
  7291. * HP/SP Recovery
  7292. * Heal player hp nad/or sp by rate
  7293. *------------------------------------------*/
  7294. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7295. {
  7296. nullpo_ret(sd);
  7297. if (hp > 100) hp = 100;
  7298. else if (hp <-100) hp = -100;
  7299. if (sp > 100) sp = 100;
  7300. else if (sp <-100) sp = -100;
  7301. if(hp >= 0 && sp >= 0) //Heal
  7302. return status_percent_heal(&sd->bl, hp, sp);
  7303. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7304. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7305. //Crossed signs
  7306. if(hp) {
  7307. if(hp > 0)
  7308. status_percent_heal(&sd->bl, hp, 0);
  7309. else
  7310. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7311. }
  7312. if(sp) {
  7313. if(sp > 0)
  7314. status_percent_heal(&sd->bl, 0, sp);
  7315. else
  7316. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7317. }
  7318. return 0;
  7319. }
  7320. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7321. {
  7322. struct mob_data *md;
  7323. int flag;
  7324. md = (struct mob_data *)bl;
  7325. nullpo_ret(md);
  7326. flag = va_arg(ap, int);
  7327. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7328. status_kill(&md->bl);
  7329. return 1;
  7330. }
  7331. /**
  7332. * Called when player changes job
  7333. * Rewrote to make it tidider [Celest]
  7334. * @param sd
  7335. * @param job JOB ID. See enum e_job
  7336. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7337. * @return True if success, false if failed
  7338. **/
  7339. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7340. {
  7341. int i, fame_flag = 0;
  7342. int b_class;
  7343. nullpo_retr(false,sd);
  7344. if (job < 0)
  7345. return false;
  7346. //Normalize job.
  7347. b_class = pc_jobid2mapid(job);
  7348. if (b_class == -1)
  7349. return false;
  7350. switch (upper) {
  7351. case 1:
  7352. b_class|= JOBL_UPPER;
  7353. break;
  7354. case 2:
  7355. b_class|= JOBL_BABY;
  7356. break;
  7357. }
  7358. //This will automatically adjust bard/dancer classes to the correct gender
  7359. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7360. job = pc_mapid2jobid(b_class, sd->status.sex);
  7361. if (job == -1)
  7362. return false;
  7363. if ((unsigned short)b_class == sd->class_)
  7364. return false; //Nothing to change.
  7365. // changing from 1st to 2nd job
  7366. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7367. sd->change_level_2nd = sd->status.job_level;
  7368. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  7369. }
  7370. // changing from 2nd to 3rd job
  7371. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7372. sd->change_level_3rd = sd->status.job_level;
  7373. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  7374. }
  7375. if(sd->cloneskill_idx > 0) {
  7376. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7377. sd->status.skill[sd->cloneskill_idx].id = 0;
  7378. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7379. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7380. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7381. }
  7382. sd->cloneskill_idx = 0;
  7383. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7384. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7385. }
  7386. if(sd->reproduceskill_idx > 0) {
  7387. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7388. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7389. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7390. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7391. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7392. }
  7393. sd->reproduceskill_idx = 0;
  7394. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7395. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7396. }
  7397. // Give or reduce transcendent status points
  7398. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7399. sd->status.status_point += battle_config.transcendent_status_points;
  7400. clif_updatestatus(sd,SP_STATUSPOINT);
  7401. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7402. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7403. // The player already used his bonus points, so we have to reset his status points
  7404. pc_resetstate(sd);
  7405. }
  7406. sd->status.status_point -= battle_config.transcendent_status_points;
  7407. clif_updatestatus(sd,SP_STATUSPOINT);
  7408. }
  7409. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7410. const int class_ = pc_class2idx(sd->status.class_);
  7411. short id;
  7412. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  7413. //Remove status specific to your current tree skills.
  7414. enum sc_type sc = status_skill2sc(id);
  7415. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7416. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7417. }
  7418. }
  7419. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7420. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7421. pc_resetfeel(sd);
  7422. }
  7423. // Reset body style to 0 before changing job to avoid
  7424. // errors since not every job has a alternate outfit.
  7425. sd->status.body = 0;
  7426. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7427. sd->status.class_ = job;
  7428. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7429. sd->class_ = (unsigned short)b_class;
  7430. sd->status.job_level=1;
  7431. sd->status.job_exp=0;
  7432. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7433. sd->status.base_level = pc_maxbaselv(sd);
  7434. sd->status.base_exp=0;
  7435. pc_resetstate(sd);
  7436. clif_updatestatus(sd,SP_STATUSPOINT);
  7437. clif_updatestatus(sd,SP_BASELEVEL);
  7438. clif_updatestatus(sd,SP_BASEEXP);
  7439. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7440. }
  7441. clif_updatestatus(sd,SP_JOBLEVEL);
  7442. clif_updatestatus(sd,SP_JOBEXP);
  7443. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7444. for(i=0;i<EQI_MAX;i++) {
  7445. if(sd->equip_index[i] >= 0)
  7446. if(pc_isequip(sd,sd->equip_index[i]))
  7447. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7448. }
  7449. //Change look, if disguised, you need to undisguise
  7450. //to correctly calculate new job sprite without
  7451. if (sd->disguise)
  7452. pc_disguise(sd, 0);
  7453. status_set_viewdata(&sd->bl, job);
  7454. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7455. #if PACKETVER >= 20151001
  7456. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7457. #endif
  7458. if(sd->vd.cloth_color)
  7459. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7460. /*
  7461. if(sd->vd.body_style)
  7462. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7463. */
  7464. //Update skill tree.
  7465. pc_calc_skilltree(sd);
  7466. clif_skillinfoblock(sd);
  7467. if (sd->ed)
  7468. elemental_delete(sd->ed);
  7469. if (sd->state.vending)
  7470. vending_closevending(sd);
  7471. if (sd->state.buyingstore)
  7472. buyingstore_close(sd);
  7473. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7474. //Remove peco/cart/falcon
  7475. i = sd->sc.option;
  7476. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7477. i&=~OPTION_RIDING;
  7478. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7479. i&=~OPTION_FALCON;
  7480. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7481. i&=~OPTION_DRAGON;
  7482. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7483. i&=~OPTION_WUGRIDER;
  7484. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7485. i&=~OPTION_WUG;
  7486. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7487. i&=~OPTION_MADOGEAR;
  7488. #ifndef NEW_CARTS
  7489. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7490. i&=~OPTION_CART;
  7491. #else
  7492. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7493. pc_setcart(sd, 0);
  7494. #endif
  7495. if(i != sd->sc.option)
  7496. pc_setoption(sd, i);
  7497. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7498. hom_vaporize(sd, HOM_ST_ACTIVE);
  7499. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7500. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7501. if(sd->status.manner < 0)
  7502. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7503. status_calc_pc(sd,SCO_FORCE);
  7504. pc_checkallowskill(sd);
  7505. pc_equiplookall(sd);
  7506. pc_show_questinfo(sd);
  7507. //if you were previously famous, not anymore.
  7508. if (fame_flag) {
  7509. chrif_save(sd,0);
  7510. chrif_buildfamelist();
  7511. } else if (sd->status.fame > 0) {
  7512. //It may be that now they are famous?
  7513. switch (sd->class_&MAPID_UPPERMASK) {
  7514. case MAPID_BLACKSMITH:
  7515. case MAPID_ALCHEMIST:
  7516. case MAPID_TAEKWON:
  7517. chrif_save(sd,0);
  7518. chrif_buildfamelist();
  7519. break;
  7520. }
  7521. }
  7522. return true;
  7523. }
  7524. /*==========================================
  7525. * Tell client player sd has change equipement
  7526. *------------------------------------------*/
  7527. void pc_equiplookall(struct map_session_data *sd)
  7528. {
  7529. nullpo_retv(sd);
  7530. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7531. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7532. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7533. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7534. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7535. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7536. }
  7537. /*==========================================
  7538. * Tell client player sd has change look (hair,equip...)
  7539. *------------------------------------------*/
  7540. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7541. nullpo_retv(sd);
  7542. switch(type) {
  7543. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7544. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7545. if (sd->status.hair != val) {
  7546. sd->status.hair = val;
  7547. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7548. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7549. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7550. }
  7551. break;
  7552. case LOOK_WEAPON:
  7553. sd->status.weapon = val;
  7554. break;
  7555. case LOOK_HEAD_BOTTOM:
  7556. sd->status.head_bottom = val;
  7557. break;
  7558. case LOOK_HEAD_TOP:
  7559. sd->status.head_top = val;
  7560. break;
  7561. case LOOK_HEAD_MID:
  7562. sd->status.head_mid = val;
  7563. break;
  7564. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7565. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7566. if (sd->status.hair_color != val) {
  7567. sd->status.hair_color = val;
  7568. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7569. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7570. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7571. }
  7572. break;
  7573. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7574. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7575. sd->status.clothes_color = val;
  7576. break;
  7577. case LOOK_SHIELD:
  7578. sd->status.shield = val;
  7579. break;
  7580. case LOOK_SHOES:
  7581. break;
  7582. case LOOK_ROBE:
  7583. sd->status.robe = val;
  7584. break;
  7585. case LOOK_BODY2:
  7586. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7587. sd->status.body = val;
  7588. break;
  7589. }
  7590. clif_changelook(&sd->bl, type, val);
  7591. }
  7592. /*==========================================
  7593. * Give an option (type) to player (sd) and display it to client
  7594. *------------------------------------------*/
  7595. void pc_setoption(struct map_session_data *sd,int type)
  7596. {
  7597. int p_type, new_look=0;
  7598. nullpo_retv(sd);
  7599. p_type = sd->sc.option;
  7600. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7601. sd->sc.option=type;
  7602. clif_changeoption(&sd->bl);
  7603. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7604. { // Mounting
  7605. clif_status_load(&sd->bl,SI_RIDING,1);
  7606. status_calc_pc(sd,SCO_NONE);
  7607. }
  7608. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7609. { // Dismount
  7610. clif_status_load(&sd->bl,SI_RIDING,0);
  7611. status_calc_pc(sd,SCO_NONE);
  7612. }
  7613. #ifndef NEW_CARTS
  7614. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7615. clif_cartlist(sd);
  7616. clif_updatestatus(sd, SP_CARTINFO);
  7617. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7618. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7619. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7620. clif_clearcart(sd->fd);
  7621. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7622. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7623. }
  7624. #endif
  7625. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7626. clif_status_load(&sd->bl,SI_FALCON,1);
  7627. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7628. clif_status_load(&sd->bl,SI_FALCON,0);
  7629. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7630. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7631. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7632. status_calc_pc(sd,SCO_NONE);
  7633. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7634. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7635. status_calc_pc(sd,SCO_NONE);
  7636. }
  7637. }
  7638. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7639. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7640. status_calc_pc(sd,SCO_NONE);
  7641. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7642. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7643. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7644. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7645. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7646. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7647. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7648. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7649. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7650. status_calc_pc(sd,SCO_NONE);
  7651. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7652. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7653. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7654. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7655. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7656. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7657. }
  7658. }
  7659. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7660. new_look = JOB_STAR_GLADIATOR2;
  7661. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7662. new_look = -1;
  7663. if (sd->disguise || !new_look)
  7664. return; //Disguises break sprite changes
  7665. if (new_look < 0) { //Restore normal look.
  7666. status_set_viewdata(&sd->bl, sd->status.class_);
  7667. new_look = sd->vd.class_;
  7668. }
  7669. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7670. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7671. if (sd->vd.cloth_color)
  7672. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7673. if( sd->vd.body_style )
  7674. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7675. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7676. }
  7677. /*==========================================
  7678. * Give player a cart
  7679. *------------------------------------------*/
  7680. bool pc_setcart(struct map_session_data *sd,int type) {
  7681. #ifndef NEW_CARTS
  7682. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7683. int option;
  7684. #endif
  7685. nullpo_retr(false,sd);
  7686. if( type < 0 || type > MAX_CARTS )
  7687. return false;// Never trust the values sent by the client! [Skotlex]
  7688. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7689. return false;// Push cart is required
  7690. #ifdef NEW_CARTS
  7691. switch( type ) {
  7692. case 0:
  7693. if( !sd->sc.data[SC_PUSH_CART] )
  7694. return 0;
  7695. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7696. clif_clearcart(sd->fd);
  7697. break;
  7698. default:/* everything else is an allowed ID so we can move on */
  7699. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7700. clif_cartlist(sd);
  7701. clif_updatestatus(sd, SP_CARTINFO);
  7702. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  7703. clif_efst_status_change(&sd->bl, sd->bl.id, AREA, SI_ON_PUSH_CART, 9999, type, 0, 0);
  7704. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  7705. sd->sc.data[SC_PUSH_CART]->val1 = type;
  7706. break;
  7707. }
  7708. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7709. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7710. #else
  7711. // Update option
  7712. option = sd->sc.option;
  7713. option &= ~OPTION_CART;// clear cart bits
  7714. option |= cart[type]; // set cart
  7715. pc_setoption(sd, option);
  7716. #endif
  7717. return true;
  7718. }
  7719. /*==========================================
  7720. * Give player a falcon
  7721. *------------------------------------------*/
  7722. void pc_setfalcon(TBL_PC* sd, int flag)
  7723. {
  7724. if( flag ){
  7725. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7726. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7727. } else if( pc_isfalcon(sd) ){
  7728. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7729. }
  7730. }
  7731. /*==========================================
  7732. * Set player riding
  7733. *------------------------------------------*/
  7734. void pc_setriding(TBL_PC* sd, int flag)
  7735. {
  7736. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7737. return;
  7738. if( flag ){
  7739. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7740. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7741. } else if( pc_isriding(sd) ){
  7742. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7743. }
  7744. }
  7745. /*==========================================
  7746. * Give player a mado
  7747. *------------------------------------------*/
  7748. void pc_setmadogear(TBL_PC* sd, int flag)
  7749. {
  7750. if( flag ){
  7751. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7752. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7753. } else if( pc_ismadogear(sd) ){
  7754. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7755. }
  7756. }
  7757. /*==========================================
  7758. * Check if player can drop an item
  7759. *------------------------------------------*/
  7760. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7761. {
  7762. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7763. return false;
  7764. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7765. return false;
  7766. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7767. }
  7768. /**
  7769. * Determines whether a player can attack based on status changes
  7770. * Why not use status_check_skilluse?
  7771. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7772. * Even ground-based attacks should be blocked by these statuses
  7773. * Called from unit_attack and unit_attack_timer_sub
  7774. * @retval true Can attack
  7775. **/
  7776. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7777. nullpo_retr(false, sd);
  7778. if (!&sd->sc)
  7779. return true;
  7780. if( sd->sc.data[SC_BASILICA] ||
  7781. sd->sc.data[SC__SHADOWFORM] ||
  7782. sd->sc.data[SC__MANHOLE] ||
  7783. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7784. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7785. sd->sc.data[SC_CRYSTALIZE] ||
  7786. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7787. sd->sc.data[SC_TRICKDEAD] ||
  7788. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7789. sd->sc.data[SC_BLADESTOP] ||
  7790. sd->sc.data[SC_DEEPSLEEP] ||
  7791. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  7792. sd->sc.data[SC_KINGS_GRACE] )
  7793. return false;
  7794. return true;
  7795. }
  7796. /*==========================================
  7797. * Read '@type' variables (temporary numeric char reg)
  7798. *------------------------------------------*/
  7799. int pc_readreg(struct map_session_data* sd, int64 reg)
  7800. {
  7801. return i64db_iget(sd->regs.vars, reg);
  7802. }
  7803. /*==========================================
  7804. * Set '@type' variables (temporary numeric char reg)
  7805. *------------------------------------------*/
  7806. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  7807. {
  7808. unsigned int index = script_getvaridx(reg);
  7809. nullpo_retr(false, sd);
  7810. if( val ) {
  7811. i64db_iput(sd->regs.vars, reg, val);
  7812. if( index )
  7813. script_array_update(&sd->regs, reg, false);
  7814. } else {
  7815. i64db_remove(sd->regs.vars, reg);
  7816. if( index )
  7817. script_array_update(&sd->regs, reg, true);
  7818. }
  7819. return true;
  7820. }
  7821. /*==========================================
  7822. * Read '@type$' variables (temporary string char reg)
  7823. *------------------------------------------*/
  7824. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  7825. {
  7826. struct script_reg_str *p = NULL;
  7827. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7828. return p ? p->value : NULL;
  7829. }
  7830. /*==========================================
  7831. * Set '@type$' variables (temporary string char reg)
  7832. *------------------------------------------*/
  7833. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  7834. {
  7835. struct script_reg_str *p = NULL;
  7836. unsigned int index = script_getvaridx(reg);
  7837. DBData prev;
  7838. nullpo_retr(false, sd);
  7839. if( str[0] ) {
  7840. p = ers_alloc(str_reg_ers, struct script_reg_str);
  7841. p->value = aStrdup(str);
  7842. p->flag.type = 1;
  7843. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7844. p = (struct script_reg_str *)db_data2ptr(&prev);
  7845. if( p->value )
  7846. aFree(p->value);
  7847. ers_free(str_reg_ers, p);
  7848. } else {
  7849. if( index )
  7850. script_array_update(&sd->regs, reg, false);
  7851. }
  7852. } else {
  7853. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  7854. p = (struct script_reg_str *)db_data2ptr(&prev);
  7855. if( p->value )
  7856. aFree(p->value);
  7857. ers_free(str_reg_ers, p);
  7858. if( index )
  7859. script_array_update(&sd->regs, reg, true);
  7860. }
  7861. }
  7862. return true;
  7863. }
  7864. /**
  7865. * Serves the following variable types:
  7866. * - 'type' (permanent numeric char reg)
  7867. * - '#type' (permanent numeric account reg)
  7868. * - '##type' (permanent numeric account reg2)
  7869. **/
  7870. int pc_readregistry(struct map_session_data *sd, int64 reg)
  7871. {
  7872. struct script_reg_num *p = NULL;
  7873. if (!sd->vars_ok) {
  7874. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7875. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7876. //intif->request_registry(sd,type==3?4:type);
  7877. set_eof(sd->fd);
  7878. return 0;
  7879. }
  7880. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  7881. return p ? p->value : 0;
  7882. }
  7883. /**
  7884. * Serves the following variable types:
  7885. * - 'type$' (permanent str char reg)
  7886. * - '#type$' (permanent str account reg)
  7887. * - '##type$' (permanent str account reg2)
  7888. **/
  7889. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  7890. {
  7891. struct script_reg_str *p = NULL;
  7892. if (!sd->vars_ok) {
  7893. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7894. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7895. //intif->request_registry(sd,type==3?4:type);
  7896. set_eof(sd->fd);
  7897. return NULL;
  7898. }
  7899. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7900. return p ? p->value : NULL;
  7901. }
  7902. /**
  7903. * Serves the following variable types:
  7904. * - 'type' (permanent numeric char reg)
  7905. * - '#type' (permanent numeric account reg)
  7906. * - '##type' (permanent numeric account reg2)
  7907. **/
  7908. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  7909. {
  7910. struct script_reg_num *p = NULL;
  7911. const char *regname = get_str(script_getvarid(reg));
  7912. unsigned int index = script_getvaridx(reg);
  7913. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  7914. switch( regname[0] ) {
  7915. default: //Char reg
  7916. if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  7917. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7918. sd->die_counter = val;
  7919. if( i )
  7920. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  7921. } else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
  7922. val = cap_value(val, 0, 1999);
  7923. sd->cook_mastery = val;
  7924. }
  7925. break;
  7926. case '#':
  7927. if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
  7928. val = cap_value(val, 0, MAX_ZENY);
  7929. sd->cashPoints = val;
  7930. } else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  7931. val = cap_value(val, 0, MAX_ZENY);
  7932. sd->kafraPoints = val;
  7933. }
  7934. break;
  7935. }
  7936. if ( !reg_load && !sd->vars_ok ) {
  7937. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  7938. return 0;
  7939. }
  7940. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  7941. if( val ) {
  7942. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  7943. script_array_update(&sd->regs, reg, false);
  7944. p->value = val;
  7945. } else {
  7946. p->value = 0;
  7947. if( index )
  7948. script_array_update(&sd->regs, reg, true);
  7949. }
  7950. if (!reg_load)
  7951. p->flag.update = 1;/* either way, it will require either delete or replace */
  7952. } else if( val ) {
  7953. DBData prev;
  7954. if( index )
  7955. script_array_update(&sd->regs, reg, false);
  7956. p = ers_alloc(num_reg_ers, struct script_reg_num);
  7957. p->value = val;
  7958. if (!reg_load)
  7959. p->flag.update = 1;
  7960. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7961. p = (struct script_reg_num *)db_data2ptr(&prev);
  7962. ers_free(num_reg_ers, p);
  7963. }
  7964. }
  7965. if (!reg_load && p)
  7966. sd->vars_dirty = true;
  7967. return 1;
  7968. }
  7969. /**
  7970. * Serves the following variable types:
  7971. * - 'type$' (permanent str char reg)
  7972. * - '#type$' (permanent str account reg)
  7973. * - '##type$' (permanent str account reg2)
  7974. **/
  7975. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  7976. {
  7977. struct script_reg_str *p = NULL;
  7978. const char *regname = get_str(script_getvarid(reg));
  7979. unsigned int index = script_getvaridx(reg);
  7980. if (!reg_load && !sd->vars_ok) {
  7981. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  7982. return 0;
  7983. }
  7984. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  7985. if( val[0] ) {
  7986. if( p->value )
  7987. aFree(p->value);
  7988. else if ( index ) // an entry that was deleted, so we reset
  7989. script_array_update(&sd->regs, reg, false);
  7990. p->value = aStrdup(val);
  7991. } else {
  7992. p->value = NULL;
  7993. if( index )
  7994. script_array_update(&sd->regs, reg, true);
  7995. }
  7996. if( !reg_load )
  7997. p->flag.update = 1; // either way, it will require either delete or replace
  7998. } else if( val[0] ) {
  7999. DBData prev;
  8000. if( index )
  8001. script_array_update(&sd->regs, reg, false);
  8002. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8003. p->value = aStrdup(val);
  8004. if( !reg_load )
  8005. p->flag.update = 1;
  8006. p->flag.type = 1;
  8007. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8008. p = (struct script_reg_str *)db_data2ptr(&prev);
  8009. if( p->value )
  8010. aFree(p->value);
  8011. ers_free(str_reg_ers, p);
  8012. }
  8013. }
  8014. if( !reg_load && p )
  8015. sd->vars_dirty = true;
  8016. return 1;
  8017. }
  8018. /**
  8019. * Set value of player variable
  8020. * @param sd Player
  8021. * @param reg Variable name
  8022. * @param value
  8023. * @return True if success, false if failed.
  8024. **/
  8025. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8026. char prefix = reg[0];
  8027. nullpo_retr(false, sd);
  8028. if (reg[strlen(reg)-1] == '$') {
  8029. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8030. return false;
  8031. }
  8032. val = cap_value(val, INT_MIN, INT_MAX);
  8033. switch (prefix) {
  8034. case '.':
  8035. case '\'':
  8036. case '$':
  8037. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8038. return false;
  8039. case '@':
  8040. return pc_setreg(sd, add_str(reg), val);
  8041. case '#':
  8042. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8043. default:
  8044. return pc_setglobalreg(sd, add_str(reg), val);
  8045. }
  8046. return false;
  8047. }
  8048. /**
  8049. * Get value of player variable
  8050. * @param sd Player
  8051. * @param reg Variable name
  8052. * @return Variable value or 0 if failed.
  8053. **/
  8054. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8055. char prefix = reg[0];
  8056. nullpo_ret(sd);
  8057. if (reg[strlen(reg)-1] == '$') {
  8058. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8059. return 0;
  8060. }
  8061. switch (prefix) {
  8062. case '.':
  8063. case '\'':
  8064. case '$':
  8065. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8066. return 0;
  8067. case '@':
  8068. return pc_readreg(sd, add_str(reg));
  8069. case '#':
  8070. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8071. default:
  8072. return pc_readglobalreg(sd, add_str(reg));
  8073. }
  8074. return 0;
  8075. }
  8076. /*==========================================
  8077. * Exec eventtimer for player sd (retrieved from map_session (id))
  8078. *------------------------------------------*/
  8079. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8080. {
  8081. struct map_session_data *sd=map_id2sd(id);
  8082. char *p = (char *)data;
  8083. int i;
  8084. if(sd==NULL)
  8085. return 0;
  8086. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8087. if( i < MAX_EVENTTIMER )
  8088. {
  8089. sd->eventtimer[i] = INVALID_TIMER;
  8090. sd->eventcount--;
  8091. npc_event(sd,p,0);
  8092. }
  8093. else
  8094. ShowError("pc_eventtimer: no such event timer\n");
  8095. if (p) aFree(p);
  8096. return 0;
  8097. }
  8098. /*==========================================
  8099. * Add eventtimer for player sd ?
  8100. *------------------------------------------*/
  8101. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8102. {
  8103. int i;
  8104. nullpo_retr(false,sd);
  8105. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8106. if( i == MAX_EVENTTIMER )
  8107. return false;
  8108. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8109. sd->eventcount++;
  8110. return true;
  8111. }
  8112. /*==========================================
  8113. * Del eventtimer for player sd ?
  8114. *------------------------------------------*/
  8115. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8116. {
  8117. char* p = NULL;
  8118. int i;
  8119. nullpo_retr(false,sd);
  8120. if (sd->eventcount == 0)
  8121. return false;
  8122. // find the named event timer
  8123. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8124. sd->eventtimer[i] != INVALID_TIMER &&
  8125. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8126. strcmp(p, name) == 0
  8127. );
  8128. if( i == MAX_EVENTTIMER )
  8129. return false; // not found
  8130. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8131. sd->eventtimer[i] = INVALID_TIMER;
  8132. if(sd->eventcount > 0)
  8133. sd->eventcount--;
  8134. aFree(p);
  8135. return true;
  8136. }
  8137. /*==========================================
  8138. * Update eventtimer count for player sd
  8139. *------------------------------------------*/
  8140. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8141. {
  8142. int i;
  8143. nullpo_retv(sd);
  8144. for(i=0;i<MAX_EVENTTIMER;i++)
  8145. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8146. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8147. addtick_timer(sd->eventtimer[i],tick);
  8148. break;
  8149. }
  8150. }
  8151. /*==========================================
  8152. * Remove all eventtimer for player sd
  8153. *------------------------------------------*/
  8154. void pc_cleareventtimer(struct map_session_data *sd)
  8155. {
  8156. int i;
  8157. nullpo_retv(sd);
  8158. if (sd->eventcount == 0)
  8159. return;
  8160. for(i=0;i<MAX_EVENTTIMER;i++){
  8161. if( sd->eventtimer[i] != INVALID_TIMER ){
  8162. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8163. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8164. sd->eventtimer[i] = INVALID_TIMER;
  8165. if(sd->eventcount > 0) //avoid looping to max val
  8166. sd->eventcount--;
  8167. if (p) aFree(p);
  8168. }
  8169. }
  8170. }
  8171. /**
  8172. * Called when an item with combo is worn
  8173. * @param *sd
  8174. * @param *data struct item_data
  8175. * @return success numbers of succeed combo
  8176. */
  8177. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8178. uint16 i;
  8179. int success = 0;
  8180. for( i = 0; i < data->combos_count; i++ ) {
  8181. struct itemchk {
  8182. int idx;
  8183. short card[MAX_SLOTS];
  8184. } *combo_idx;
  8185. int idx, j;
  8186. int nb_itemCombo;
  8187. unsigned int pos = 0;
  8188. /* ensure this isn't a duplicate combo */
  8189. if( sd->combos.bonus != NULL ) {
  8190. int x;
  8191. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8192. /* found a match, skip this combo */
  8193. if( x < sd->combos.count )
  8194. continue;
  8195. }
  8196. nb_itemCombo = data->combos[i]->count;
  8197. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8198. continue;
  8199. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8200. for(j=0; j < nb_itemCombo; j++){
  8201. combo_idx[j].idx=-1;
  8202. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8203. }
  8204. for( j = 0; j < nb_itemCombo; j++ ) {
  8205. uint16 id = data->combos[i]->nameid[j], k;
  8206. bool found = false;
  8207. for( k = 0; k < EQI_MAX; k++ ) {
  8208. short index = sd->equip_index[k];
  8209. if( index < 0 )
  8210. continue;
  8211. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8212. continue;
  8213. if (!sd->inventory_data[index] )
  8214. continue;
  8215. if ( itemdb_type(id) != IT_CARD ) {
  8216. if ( sd->inventory_data[index]->nameid != id )
  8217. continue;
  8218. if(j>0){ //check if this item not already used
  8219. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8220. uint8 z;
  8221. for (z = 0; z < nb_itemCombo-1; z++)
  8222. if(combo_idx[z].idx == index) //we already have that index recorded
  8223. do_continue=true;
  8224. if(do_continue)
  8225. continue;
  8226. }
  8227. combo_idx[j].idx = index;
  8228. pos |= sd->status.inventory[index].equip;
  8229. found = true;
  8230. break;
  8231. } else { //Cards
  8232. uint16 z;
  8233. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  8234. continue;
  8235. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  8236. bool do_continue=false;
  8237. if (sd->status.inventory[index].card[z] != id)
  8238. continue;
  8239. if(j>0){
  8240. int c1, c2;
  8241. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8242. if(combo_idx[c1].idx == index){
  8243. for (c2 = 0; c2 < sd->inventory_data[index]->slot; c2++){
  8244. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8245. do_continue = true;
  8246. break;
  8247. }
  8248. }
  8249. }
  8250. }
  8251. }
  8252. if(do_continue)
  8253. continue;
  8254. combo_idx[j].idx = index;
  8255. combo_idx[j].card[z] = id;
  8256. pos |= sd->status.inventory[index].equip;
  8257. found = true;
  8258. break;
  8259. }
  8260. }
  8261. }
  8262. if( !found )
  8263. break;/* we haven't found all the ids for this combo, so we can return */
  8264. }
  8265. aFree(combo_idx);
  8266. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8267. if( j < nb_itemCombo )
  8268. continue;
  8269. /* we got here, means all items in the combo are matching */
  8270. idx = sd->combos.count;
  8271. if( sd->combos.bonus == NULL ) {
  8272. CREATE(sd->combos.bonus, struct script_code *, 1);
  8273. CREATE(sd->combos.id, unsigned short, 1);
  8274. CREATE(sd->combos.pos, unsigned int, 1);
  8275. sd->combos.count = 1;
  8276. } else {
  8277. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8278. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8279. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8280. }
  8281. /* we simply copy the pointer */
  8282. sd->combos.bonus[idx] = data->combos[i]->script;
  8283. /* save this combo's id */
  8284. sd->combos.id[idx] = data->combos[i]->id;
  8285. /* save pos of combo*/
  8286. sd->combos.pos[idx] = pos;
  8287. success++;
  8288. }
  8289. return success;
  8290. }
  8291. /**
  8292. * Called when an item with combo is removed
  8293. * @param *sd
  8294. * @param *data struct item_data
  8295. * @return retval numbers of removed combo
  8296. */
  8297. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8298. int i, retval = 0;
  8299. if( sd->combos.bonus == NULL )
  8300. return 0;/* nothing to do here, player has no combos */
  8301. for( i = 0; i < data->combos_count; i++ ) {
  8302. /* check if this combo exists in this user */
  8303. int x = 0, cursor = 0, j;
  8304. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8305. /* no match, skip this combo */
  8306. if(x >= sd->combos.count)
  8307. continue;
  8308. sd->combos.bonus[x] = NULL;
  8309. sd->combos.id[x] = 0;
  8310. sd->combos.pos[x] = 0;
  8311. retval++;
  8312. /* check if combo requirements still fit */
  8313. if( pc_checkcombo( sd, data ) )
  8314. continue;
  8315. /* move next value to empty slot */
  8316. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8317. if( sd->combos.bonus[j] == NULL )
  8318. continue;
  8319. if( cursor != j ) {
  8320. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8321. sd->combos.id[cursor] = sd->combos.id[j];
  8322. sd->combos.pos[cursor] = sd->combos.pos[j];
  8323. }
  8324. cursor++;
  8325. }
  8326. /* it's empty, we can clear all the memory */
  8327. if( (sd->combos.count = cursor) == 0 ) {
  8328. aFree(sd->combos.bonus);
  8329. aFree(sd->combos.id);
  8330. aFree(sd->combos.pos);
  8331. sd->combos.bonus = NULL;
  8332. sd->combos.id = NULL;
  8333. sd->combos.pos = NULL;
  8334. return retval; /* we also can return at this point for we have no more combos to check */
  8335. }
  8336. }
  8337. return retval;
  8338. }
  8339. /**
  8340. * Load combo data(s) of player
  8341. * @param *sd
  8342. * @return ret numbers of succeed combo
  8343. */
  8344. int pc_load_combo(struct map_session_data *sd) {
  8345. int i, ret = 0;
  8346. for( i = 0; i < EQI_MAX; i++ ) {
  8347. struct item_data *id = NULL;
  8348. short idx = sd->equip_index[i];
  8349. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8350. continue;
  8351. if( id->combos_count )
  8352. ret += pc_checkcombo(sd,id);
  8353. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  8354. struct item_data *data;
  8355. int j;
  8356. for( j = 0; j < id->slot; j++ ) {
  8357. if (!sd->status.inventory[idx].card[j])
  8358. continue;
  8359. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  8360. if( data->combos_count )
  8361. ret += pc_checkcombo(sd,data);
  8362. }
  8363. }
  8364. }
  8365. }
  8366. return ret;
  8367. }
  8368. /*==========================================
  8369. * Equip item on player sd at req_pos from inventory index n
  8370. * return: false - fail; true - success
  8371. *------------------------------------------*/
  8372. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8373. {
  8374. int i, pos, flag = 0, iflag;
  8375. struct item_data *id;
  8376. uint8 res = ITEM_EQUIP_ACK_OK;
  8377. nullpo_retr(false,sd);
  8378. if( n < 0 || n >= MAX_INVENTORY ) {
  8379. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8380. return false;
  8381. }
  8382. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8383. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8384. return false;
  8385. }
  8386. if (!(id = sd->inventory_data[n]))
  8387. return false;
  8388. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8389. if(battle_config.battle_log)
  8390. ShowInfo("equip %hu(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  8391. if((res = pc_isequip(sd,n))) {
  8392. clif_equipitemack(sd,n,0,res); // fail
  8393. return false;
  8394. }
  8395. if (!(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  8396. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8397. return false;
  8398. }
  8399. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8400. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8401. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8402. return false;
  8403. }
  8404. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8405. /** Failing condition:
  8406. * 1. Always failed to equip ammo if no weapon equipped yet
  8407. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8408. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8409. * (4. The rest is relying on item job/class restriction).
  8410. **/
  8411. if (id->type == IT_AMMO) {
  8412. int w_idx = sd->equip_index[EQI_HAND_R];
  8413. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8414. if (w_idx == -1 ||
  8415. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8416. (id->look != A_GRENADE && w_type == W_GRENADE))
  8417. {
  8418. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8419. return false;
  8420. }
  8421. }
  8422. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8423. int a_idx = sd->equip_index[EQI_AMMO];
  8424. if (a_idx != -1) {
  8425. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8426. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8427. (a_type != A_GRENADE && id->look == W_GRENADE))
  8428. {
  8429. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8430. return false;
  8431. }
  8432. }
  8433. }
  8434. }
  8435. if (id->flag.bindOnEquip && !sd->status.inventory[n].bound) {
  8436. sd->status.inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8437. clif_notify_bindOnEquip(sd,n);
  8438. }
  8439. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8440. pos = req_pos&EQP_ACC;
  8441. if (pos == EQP_ACC) //User specified both slots..
  8442. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8443. }
  8444. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8445. pos = req_pos&EQP_SHADOW_ACC;
  8446. if (pos == EQP_SHADOW_ACC)
  8447. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8448. }
  8449. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8450. pos = (req_pos&EQP_ARMS);
  8451. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8452. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8453. }
  8454. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8455. //Update skill-block range database when weapon range changes. [Skotlex]
  8456. i = sd->equip_index[EQI_HAND_R];
  8457. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8458. flag = 1;
  8459. else
  8460. flag = id->range != sd->inventory_data[i]->range;
  8461. }
  8462. for(i=0;i<EQI_MAX;i++) {
  8463. if(pos & equip_bitmask[i]) {
  8464. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8465. pc_unequipitem(sd,sd->equip_index[i],2);
  8466. sd->equip_index[i] = n;
  8467. }
  8468. }
  8469. if(pos==EQP_AMMO) {
  8470. clif_arrowequip(sd,n);
  8471. clif_arrow_fail(sd,3);
  8472. }
  8473. else
  8474. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8475. sd->status.inventory[n].equip=pos;
  8476. if(pos & EQP_HAND_R) {
  8477. if(id)
  8478. sd->weapontype1 = id->look;
  8479. else
  8480. sd->weapontype1 = 0;
  8481. pc_calcweapontype(sd);
  8482. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8483. }
  8484. if(pos & EQP_HAND_L) {
  8485. if(id) {
  8486. if(id->type == IT_WEAPON) {
  8487. sd->status.shield = 0;
  8488. sd->weapontype2 = id->look;
  8489. }
  8490. else
  8491. if(id->type == IT_ARMOR) {
  8492. sd->status.shield = id->look;
  8493. sd->weapontype2 = 0;
  8494. }
  8495. }
  8496. else
  8497. sd->status.shield = sd->weapontype2 = 0;
  8498. pc_calcweapontype(sd);
  8499. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8500. }
  8501. //Added check to prevent sending the same look on multiple slots ->
  8502. //causes client to redraw item on top of itself. (suggested by Lupus)
  8503. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  8504. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  8505. sd->status.head_bottom = id->look;
  8506. else
  8507. sd->status.head_bottom = 0;
  8508. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8509. }
  8510. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  8511. if(id)
  8512. sd->status.head_top = id->look;
  8513. else
  8514. sd->status.head_top = 0;
  8515. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8516. }
  8517. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  8518. if(id && !(pos&EQP_HEAD_TOP))
  8519. sd->status.head_mid = id->look;
  8520. else
  8521. sd->status.head_mid = 0;
  8522. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8523. }
  8524. if(pos & EQP_COSTUME_HEAD_TOP) {
  8525. if(id){
  8526. sd->status.head_top = id->look;
  8527. } else
  8528. sd->status.head_top = 0;
  8529. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8530. }
  8531. if(pos & EQP_COSTUME_HEAD_MID) {
  8532. if(id && !(pos&EQP_HEAD_TOP)){
  8533. sd->status.head_mid = id->look;
  8534. } else
  8535. sd->status.head_mid = 0;
  8536. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8537. }
  8538. if(pos & EQP_COSTUME_HEAD_LOW) {
  8539. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  8540. sd->status.head_bottom = id->look;
  8541. } else
  8542. sd->status.head_bottom = 0;
  8543. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8544. }
  8545. if(pos & EQP_SHOES)
  8546. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8547. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8548. sd->status.robe = id ? id->look : 0;
  8549. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  8550. }
  8551. if(pos & EQP_COSTUME_GARMENT) {
  8552. sd->status.robe = id ? id->look : 0;
  8553. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8554. }
  8555. pc_checkallowskill(sd); //Check if status changes should be halted.
  8556. iflag = sd->npc_item_flag;
  8557. /* check for combos (MUST be before status_calc_pc) */
  8558. if( id->combos_count )
  8559. pc_checkcombo(sd,id);
  8560. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8561. ; //No cards
  8562. else {
  8563. for( i = 0; i < id->slot; i++ ) {
  8564. struct item_data *data;
  8565. if (!sd->status.inventory[n].card[i])
  8566. continue;
  8567. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8568. if( data->combos_count )
  8569. pc_checkcombo(sd,data);
  8570. }
  8571. }
  8572. }
  8573. status_calc_pc(sd,SCO_NONE);
  8574. if (flag) //Update skill data
  8575. clif_skillinfoblock(sd);
  8576. //OnEquip script [Skotlex]
  8577. if (id) {
  8578. //only run the script if item isn't restricted
  8579. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8580. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8581. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8582. ; //No cards
  8583. else {
  8584. for( i = 0; i < id->slot; i++ ) {
  8585. struct item_data *data;
  8586. if (!sd->status.inventory[n].card[i])
  8587. continue;
  8588. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8589. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8590. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8591. }
  8592. }
  8593. }
  8594. }
  8595. sd->npc_item_flag = iflag;
  8596. return true;
  8597. }
  8598. /*==========================================
  8599. * Called when attemting to unequip an item from player
  8600. * type:
  8601. * 0 - only unequip
  8602. * 1 - calculate status after unequipping
  8603. * 2 - force unequip
  8604. * return: false - fail; true - success
  8605. *------------------------------------------*/
  8606. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8607. int i, iflag;
  8608. bool status_cacl = false;
  8609. nullpo_retr(false,sd);
  8610. if (n < 0 || n >= MAX_INVENTORY) {
  8611. clif_unequipitemack(sd,0,0,0);
  8612. return false;
  8613. }
  8614. if (!sd->status.inventory[n].equip) {
  8615. clif_unequipitemack(sd,n,0,0);
  8616. return false; //Nothing to unequip
  8617. }
  8618. // status change that makes player cannot unequip equipment
  8619. if (!(flag&2) && sd->sc.count &&
  8620. (sd->sc.data[SC_BERSERK] ||
  8621. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8622. sd->sc.data[SC__BLOODYLUST] ||
  8623. sd->sc.data[SC_KYOUGAKU] ||
  8624. (sd->sc.data[SC_PYROCLASTIC] &&
  8625. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8626. {
  8627. clif_unequipitemack(sd,n,0,0);
  8628. return false;
  8629. }
  8630. if (battle_config.battle_log)
  8631. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  8632. for(i = 0; i < EQI_MAX; i++) {
  8633. if (sd->status.inventory[n].equip & equip_bitmask[i])
  8634. sd->equip_index[i] = -1;
  8635. }
  8636. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  8637. sd->weapontype1 = 0;
  8638. sd->status.weapon = sd->weapontype2;
  8639. pc_calcweapontype(sd);
  8640. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8641. if( !battle_config.dancing_weaponswitch_fix )
  8642. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8643. }
  8644. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  8645. sd->status.shield = sd->weapontype2 = 0;
  8646. pc_calcweapontype(sd);
  8647. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8648. }
  8649. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  8650. sd->status.head_bottom = 0;
  8651. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8652. }
  8653. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  8654. sd->status.head_top = 0;
  8655. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8656. }
  8657. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  8658. sd->status.head_mid = 0;
  8659. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8660. }
  8661. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  8662. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  8663. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8664. }
  8665. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  8666. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  8667. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8668. }
  8669. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  8670. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  8671. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8672. }
  8673. if(sd->status.inventory[n].equip & EQP_SHOES)
  8674. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8675. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8676. sd->status.robe = 0;
  8677. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  8678. }
  8679. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  8680. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  8681. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8682. }
  8683. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  8684. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8685. // On weapon change (right and left hand)
  8686. if ((sd->status.inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8687. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8688. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8689. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8690. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8691. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8692. }
  8693. // On armor change
  8694. if (sd->status.inventory[n].equip & EQP_ARMOR) {
  8695. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8696. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8697. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8698. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8699. }
  8700. // On ammo change
  8701. if (sd->inventory_data[n]->type == IT_AMMO)
  8702. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8703. if( sd->state.autobonus&sd->status.inventory[n].equip )
  8704. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  8705. sd->status.inventory[n].equip = 0;
  8706. iflag = sd->npc_item_flag;
  8707. /* check for combos (MUST be before status_calc_pc) */
  8708. if ( sd->inventory_data[n] ) {
  8709. if( sd->inventory_data[n]->combos_count ) {
  8710. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8711. status_cacl = true;
  8712. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8713. ; //No cards
  8714. else {
  8715. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8716. struct item_data *data;
  8717. if (!sd->status.inventory[n].card[i])
  8718. continue;
  8719. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8720. if( data->combos_count ) {
  8721. if( pc_removecombo(sd,data) )
  8722. status_cacl = true;
  8723. }
  8724. }
  8725. }
  8726. }
  8727. }
  8728. if(flag&1 || status_cacl) {
  8729. pc_checkallowskill(sd);
  8730. status_calc_pc(sd,SCO_NONE);
  8731. }
  8732. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8733. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8734. //OnUnEquip script [Skotlex]
  8735. if (sd->inventory_data[n]) {
  8736. if (sd->inventory_data[n]->unequip_script)
  8737. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8738. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8739. ; //No cards
  8740. else {
  8741. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8742. struct item_data *data;
  8743. if (!sd->status.inventory[n].card[i])
  8744. continue;
  8745. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8746. if( data->unequip_script )
  8747. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8748. }
  8749. }
  8750. }
  8751. }
  8752. sd->npc_item_flag = iflag;
  8753. return true;
  8754. }
  8755. /*==========================================
  8756. * Checking if player (sd) has an invalid item
  8757. * and is unequiped on map load (item_noequip)
  8758. *------------------------------------------*/
  8759. void pc_checkitem(struct map_session_data *sd) {
  8760. int i, calc_flag = 0;
  8761. struct item it;
  8762. nullpo_retv(sd);
  8763. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8764. return;
  8765. pc_check_available_item(sd); // Check for invalid(ated) items.
  8766. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8767. it = sd->status.inventory[i];
  8768. if( it.nameid == 0 )
  8769. continue;
  8770. if( !it.equip )
  8771. continue;
  8772. if( it.equip&~pc_equippoint(sd,i) ) {
  8773. pc_unequipitem(sd, i, 2);
  8774. calc_flag = 1;
  8775. continue;
  8776. }
  8777. if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) {
  8778. pc_unequipitem(sd, i, 2);
  8779. calc_flag = 1;
  8780. continue;
  8781. }
  8782. }
  8783. if( calc_flag && sd->state.active ) {
  8784. pc_checkallowskill(sd);
  8785. status_calc_pc(sd,SCO_NONE);
  8786. }
  8787. }
  8788. /*==========================================
  8789. * Checks for unavailable items and removes them.
  8790. *------------------------------------------*/
  8791. void pc_check_available_item(struct map_session_data *sd)
  8792. {
  8793. int i;
  8794. unsigned short nameid;
  8795. char output[256];
  8796. nullpo_retv(sd);
  8797. if (battle_config.item_check&0x1) { // Check for invalid(ated) items in inventory.
  8798. for(i = 0; i < MAX_INVENTORY; i++) {
  8799. nameid = sd->status.inventory[i].nameid;
  8800. if (!nameid)
  8801. continue;
  8802. if (!itemdb_available(nameid)) {
  8803. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8804. clif_displaymessage(sd->fd, output);
  8805. ShowWarning("Removed invalid/disabled item id %hu from inventory (amount=%d, char_id=%d).\n", nameid, sd->status.inventory[i].amount, sd->status.char_id);
  8806. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8807. continue;
  8808. }
  8809. if (!sd->status.inventory[i].unique_id && !itemdb_isstackable(nameid))
  8810. sd->status.inventory[i].unique_id = pc_generate_unique_id(sd);
  8811. }
  8812. }
  8813. if (battle_config.item_check&0x2) { // Check for invalid(ated) items in cart.
  8814. for(i = 0; i < MAX_CART; i++) {
  8815. nameid = sd->status.cart[i].nameid;
  8816. if (!nameid)
  8817. continue;
  8818. if (!itemdb_available(nameid)) {
  8819. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  8820. clif_displaymessage(sd->fd, output);
  8821. ShowWarning("Removed invalid/disabled item id %hu from cart (amount=%d, char_id=%d).\n", nameid, sd->status.cart[i].amount, sd->status.char_id);
  8822. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8823. continue;
  8824. }
  8825. if (!sd->status.cart[i].unique_id && !itemdb_isstackable(nameid))
  8826. sd->status.cart[i].unique_id = pc_generate_unique_id(sd);
  8827. }
  8828. }
  8829. if (battle_config.item_check&0x4) { // Check for invalid(ated) items in storage.
  8830. for(i = 0; i < sd->storage_size; i++) {
  8831. nameid = sd->status.storage.items[i].nameid;
  8832. if (!nameid)
  8833. continue;
  8834. if (!itemdb_available(nameid)) {
  8835. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  8836. clif_displaymessage(sd->fd, output);
  8837. ShowWarning("Removed invalid/disabled item id %hu from storage (amount=%d, char_id=%d).\n", nameid, sd->status.storage.items[i].amount, sd->status.char_id);
  8838. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8839. continue;
  8840. }
  8841. if (!sd->status.storage.items[i].unique_id && !itemdb_isstackable(nameid))
  8842. sd->status.storage.items[i].unique_id = pc_generate_unique_id(sd);
  8843. }
  8844. }
  8845. }
  8846. /*==========================================
  8847. * Update PVP rank for sd1 in cmp to sd2
  8848. *------------------------------------------*/
  8849. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8850. {
  8851. struct map_session_data *sd1,*sd2;
  8852. sd1=(struct map_session_data *)bl;
  8853. sd2=va_arg(ap,struct map_session_data *);
  8854. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8855. {// cannot register pvp rank for hidden GMs
  8856. return 0;
  8857. }
  8858. if( sd1->pvp_point > sd2->pvp_point )
  8859. sd2->pvp_rank++;
  8860. return 0;
  8861. }
  8862. /*==========================================
  8863. * Calculate new rank beetween all present players (map_foreachinarea)
  8864. * and display result
  8865. *------------------------------------------*/
  8866. int pc_calc_pvprank(struct map_session_data *sd)
  8867. {
  8868. int old = sd->pvp_rank;
  8869. struct map_data *m = &map[sd->bl.m];
  8870. sd->pvp_rank=1;
  8871. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8872. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8873. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8874. return sd->pvp_rank;
  8875. }
  8876. /*==========================================
  8877. * Calculate next sd ranking calculation from config
  8878. *------------------------------------------*/
  8879. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8880. {
  8881. struct map_session_data *sd;
  8882. sd=map_id2sd(id);
  8883. if(sd==NULL)
  8884. return 0;
  8885. sd->pvp_timer = INVALID_TIMER;
  8886. if( sd->sc.option&OPTION_INVISIBLE )
  8887. {// do not calculate the pvp rank for a hidden GM
  8888. return 0;
  8889. }
  8890. if( pc_calc_pvprank(sd) > 0 )
  8891. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8892. return 0;
  8893. }
  8894. /*==========================================
  8895. * Checking if sd is married
  8896. * Return:
  8897. * partner_id = yes
  8898. * 0 = no
  8899. *------------------------------------------*/
  8900. int pc_ismarried(struct map_session_data *sd)
  8901. {
  8902. if(sd == NULL)
  8903. return -1;
  8904. if(sd->status.partner_id > 0)
  8905. return sd->status.partner_id;
  8906. else
  8907. return 0;
  8908. }
  8909. /*==========================================
  8910. * Marry player sd to player dstsd
  8911. * Return:
  8912. * false = fail
  8913. * true = success
  8914. *------------------------------------------*/
  8915. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  8916. {
  8917. if(sd == NULL || dstsd == NULL ||
  8918. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  8919. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  8920. return false;
  8921. sd->status.partner_id = dstsd->status.char_id;
  8922. dstsd->status.partner_id = sd->status.char_id;
  8923. return true;
  8924. }
  8925. /*==========================================
  8926. * Divorce sd from its partner
  8927. * Return:
  8928. * false = fail
  8929. * true = success
  8930. *------------------------------------------*/
  8931. bool pc_divorce(struct map_session_data *sd)
  8932. {
  8933. struct map_session_data *p_sd;
  8934. int i;
  8935. if( sd == NULL || !pc_ismarried(sd) )
  8936. return false;
  8937. if( !sd->status.partner_id )
  8938. return false; // Char is not married
  8939. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  8940. { // Lets char server do the divorce
  8941. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  8942. return false; // No char server connected
  8943. return true;
  8944. }
  8945. // Both players online, lets do the divorce manually
  8946. sd->status.partner_id = 0;
  8947. p_sd->status.partner_id = 0;
  8948. for( i = 0; i < MAX_INVENTORY; i++ )
  8949. {
  8950. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  8951. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8952. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  8953. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  8954. }
  8955. clif_divorced(sd, p_sd->status.name);
  8956. clif_divorced(p_sd, sd->status.name);
  8957. return true;
  8958. }
  8959. /**
  8960. * Get the partner map_session_data of a player
  8961. * @param sd : the husband|wife session
  8962. * @return partner session or NULL
  8963. */
  8964. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  8965. if (!sd || !pc_ismarried(sd))
  8966. return NULL;
  8967. return map_charid2sd(sd->status.partner_id);
  8968. }
  8969. /**
  8970. * Get the father map_session_data of a player
  8971. * @param sd : the baby session
  8972. * @return father session or NULL
  8973. */
  8974. struct map_session_data *pc_get_father (struct map_session_data *sd){
  8975. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  8976. return NULL;
  8977. return map_charid2sd(sd->status.father);
  8978. }
  8979. /**
  8980. * Get the mother map_session_data of a player
  8981. * @param sd : the baby session
  8982. * @return mother session or NULL
  8983. */
  8984. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  8985. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  8986. return NULL;
  8987. return map_charid2sd(sd->status.mother);
  8988. }
  8989. /*==========================================
  8990. * Get sd children charid. (Need to be married)
  8991. *------------------------------------------*/
  8992. struct map_session_data *pc_get_child (struct map_session_data *sd)
  8993. {
  8994. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  8995. // charid2sd returns NULL if not found
  8996. return NULL;
  8997. return map_charid2sd(sd->status.child);
  8998. }
  8999. /*==========================================
  9000. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9001. *------------------------------------------*/
  9002. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9003. {
  9004. int hp = 0, sp = 0;
  9005. if( pc_isdead(sd) )
  9006. return;
  9007. if (sd->hp_loss.value) {
  9008. sd->hp_loss.tick += diff_tick;
  9009. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9010. hp += sd->hp_loss.value;
  9011. sd->hp_loss.tick -= sd->hp_loss.rate;
  9012. }
  9013. if(hp >= sd->battle_status.hp)
  9014. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9015. }
  9016. if (sd->sp_loss.value) {
  9017. sd->sp_loss.tick += diff_tick;
  9018. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9019. sp += sd->sp_loss.value;
  9020. sd->sp_loss.tick -= sd->sp_loss.rate;
  9021. }
  9022. }
  9023. if (hp > 0 || sp > 0)
  9024. status_zap(&sd->bl, hp, sp);
  9025. }
  9026. //Character regen. Flag is used to know which types of regen can take place.
  9027. //&1: HP regen
  9028. //&2: SP regen
  9029. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9030. {
  9031. int hp = 0, sp = 0;
  9032. if (sd->hp_regen.value) {
  9033. sd->hp_regen.tick += diff_tick;
  9034. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9035. hp += sd->hp_regen.value;
  9036. sd->hp_regen.tick -= sd->hp_regen.rate;
  9037. }
  9038. }
  9039. if (sd->sp_regen.value) {
  9040. sd->sp_regen.tick += diff_tick;
  9041. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9042. sp += sd->sp_regen.value;
  9043. sd->sp_regen.tick -= sd->sp_regen.rate;
  9044. }
  9045. }
  9046. if (hp > 0 || sp > 0)
  9047. status_heal(&sd->bl, hp, sp, 0);
  9048. }
  9049. /*==========================================
  9050. * Memo player sd savepoint. (map,x,y)
  9051. *------------------------------------------*/
  9052. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9053. {
  9054. nullpo_retv(sd);
  9055. sd->status.save_point.map = mapindex;
  9056. sd->status.save_point.x = x;
  9057. sd->status.save_point.y = y;
  9058. }
  9059. /*==========================================
  9060. * Save 1 player data at autosave interval
  9061. *------------------------------------------*/
  9062. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9063. {
  9064. int interval;
  9065. struct s_mapiterator* iter;
  9066. struct map_session_data* sd;
  9067. static int last_save_id = 0, save_flag = 0;
  9068. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9069. save_flag = 0;
  9070. else
  9071. save_flag = 1; //Noone was saved, so save first found char.
  9072. iter = mapit_getallusers();
  9073. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9074. {
  9075. if(sd->bl.id == last_save_id && save_flag != 1) {
  9076. save_flag = 1;
  9077. continue;
  9078. }
  9079. if(save_flag != 1) //Not our turn to save yet.
  9080. continue;
  9081. //Save char.
  9082. last_save_id = sd->bl.id;
  9083. save_flag = 2;
  9084. #ifdef VIP_ENABLE
  9085. if(sd->vip.enabled) //check if we're still vip
  9086. chrif_req_login_operation(1, sd->status.name, 6, 0, 1, 0);
  9087. #endif
  9088. chrif_save(sd,0);
  9089. break;
  9090. }
  9091. mapit_free(iter);
  9092. interval = autosave_interval/(map_usercount()+1);
  9093. if(interval < minsave_interval)
  9094. interval = minsave_interval;
  9095. add_timer(gettick()+interval,pc_autosave,0,0);
  9096. return 0;
  9097. }
  9098. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9099. {
  9100. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9101. { //Night/day state does not match.
  9102. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  9103. sd->state.night = night_flag;
  9104. return 1;
  9105. }
  9106. return 0;
  9107. }
  9108. /*================================================
  9109. * timer to do the day [Yor]
  9110. * data: 0 = called by timer, 1 = gmcommand/script
  9111. *------------------------------------------------*/
  9112. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9113. {
  9114. char tmp_soutput[1024];
  9115. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9116. return 0;
  9117. if (!night_flag)
  9118. return 0; //Already day.
  9119. night_flag = 0; // 0=day, 1=night [Yor]
  9120. map_foreachpc(pc_daynight_timer_sub);
  9121. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9122. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9123. return 0;
  9124. }
  9125. /*================================================
  9126. * timer to do the night [Yor]
  9127. * data: 0 = called by timer, 1 = gmcommand/script
  9128. *------------------------------------------------*/
  9129. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9130. {
  9131. char tmp_soutput[1024];
  9132. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9133. return 0;
  9134. if (night_flag)
  9135. return 0; //Already nigth.
  9136. night_flag = 1; // 0=day, 1=night [Yor]
  9137. map_foreachpc(pc_daynight_timer_sub);
  9138. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9139. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9140. return 0;
  9141. }
  9142. /**
  9143. * Attempt to stand up a player
  9144. * @param sd
  9145. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9146. * @return True if success, Fals if failed
  9147. */
  9148. bool pc_setstand(struct map_session_data *sd, bool force){
  9149. nullpo_ret(sd);
  9150. // Cannot stand yet
  9151. // TODO: Move to SCS_NOSTAND [Cydh]
  9152. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9153. return false;
  9154. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9155. clif_status_load(&sd->bl,SI_SIT,0);
  9156. clif_standing(&sd->bl); //Inform area PC is standing
  9157. //Reset sitting tick.
  9158. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9159. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9160. return true;
  9161. }
  9162. /**
  9163. * Mechanic (MADO GEAR)
  9164. **/
  9165. void pc_overheat(struct map_session_data *sd, int val) {
  9166. int heat = val, skill,
  9167. limit[] = { 10, 20, 28, 46, 66 };
  9168. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9169. return; // already burning
  9170. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9171. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9172. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9173. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9174. }
  9175. heat = max(0,heat); // Avoid negative HEAT
  9176. if( heat >= limit[skill] )
  9177. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9178. else
  9179. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9180. return;
  9181. }
  9182. /**
  9183. * Check if player is autolooting given itemID.
  9184. */
  9185. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9186. {
  9187. uint8 i = 0;
  9188. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9189. return true;
  9190. if (!sd->state.autolooting)
  9191. return false;
  9192. if (sd->state.autolooting)
  9193. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9194. return (i != AUTOLOOTITEM_SIZE);
  9195. }
  9196. /**
  9197. * Checks if player can use @/#command
  9198. * @param sd Player map session data
  9199. * @param command Command name without @/# and params
  9200. * @param type is it atcommand or charcommand
  9201. */
  9202. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9203. {
  9204. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9205. }
  9206. /**
  9207. * Checks if commands used by a player should be logged
  9208. * according to their group setting.
  9209. * @param sd Player map session data
  9210. */
  9211. bool pc_should_log_commands(struct map_session_data *sd)
  9212. {
  9213. return pc_group_should_log_commands(pc_get_group_id(sd));
  9214. }
  9215. /**
  9216. * Spirit Charm expiration timer.
  9217. * @see TimerFunc
  9218. */
  9219. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9220. {
  9221. struct map_session_data *sd;
  9222. int i;
  9223. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9224. return 1;
  9225. if (sd->spiritcharm <= 0) {
  9226. ShowError("pc_spiritcharm_timer: %d spiritcharm's available. (aid=%d cid=%d tid=%d)\n", sd->spiritcharm, sd->status.account_id, sd->status.char_id, tid);
  9227. sd->spiritcharm = 0;
  9228. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9229. return 0;
  9230. }
  9231. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9232. if (i == sd->spiritcharm) {
  9233. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9234. return 0;
  9235. }
  9236. sd->spiritcharm--;
  9237. if (i != sd->spiritcharm)
  9238. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9239. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9240. if (sd->spiritcharm <= 0)
  9241. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9242. clif_spiritcharm(sd);
  9243. return 0;
  9244. }
  9245. /**
  9246. * Adds a spirit charm.
  9247. * @param sd: Target character
  9248. * @param interval: Duration
  9249. * @param max: Maximum amount of charms to add
  9250. * @param type: Charm type (@see spirit_charm_types)
  9251. */
  9252. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9253. {
  9254. int tid, i;
  9255. nullpo_retv(sd);
  9256. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9257. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9258. if (max > MAX_SPIRITCHARM)
  9259. max = MAX_SPIRITCHARM;
  9260. if (sd->spiritcharm < 0)
  9261. sd->spiritcharm = 0;
  9262. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9263. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9264. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9265. sd->spiritcharm--;
  9266. if (sd->spiritcharm != 0)
  9267. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9268. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9269. }
  9270. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9271. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spiritcharm_timer[i])->tick) < 0);
  9272. if (i != sd->spiritcharm)
  9273. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9274. sd->spiritcharm_timer[i] = tid;
  9275. sd->spiritcharm++;
  9276. sd->spiritcharm_type = type;
  9277. clif_spiritcharm(sd);
  9278. }
  9279. /**
  9280. * Removes one or more spirit charms.
  9281. * @param sd: The target character
  9282. * @param count: Amount of charms to remove
  9283. * @param type: Type of charm to remove
  9284. */
  9285. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9286. {
  9287. int i;
  9288. nullpo_retv(sd);
  9289. if (sd->spiritcharm_type != type)
  9290. return;
  9291. if (sd->spiritcharm <= 0) {
  9292. sd->spiritcharm = 0;
  9293. return;
  9294. }
  9295. if (count <= 0)
  9296. return;
  9297. if (count > sd->spiritcharm)
  9298. count = sd->spiritcharm;
  9299. sd->spiritcharm -= count;
  9300. if (count > MAX_SPIRITCHARM)
  9301. count = MAX_SPIRITCHARM;
  9302. for (i = 0; i < count; i++) {
  9303. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9304. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9305. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9306. }
  9307. }
  9308. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9309. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9310. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9311. }
  9312. if (sd->spiritcharm <= 0)
  9313. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9314. clif_spiritcharm(sd);
  9315. }
  9316. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9317. /**
  9318. * Renewal EXP/Item Drop rate modifier based on level penalty
  9319. * @param level_diff: Monster and Player level difference
  9320. * @param mob_class: Monster class
  9321. * @param mode: Monster mode
  9322. * @param type: 1 - EXP, 2 - Item Drop
  9323. * @return Penalty rate
  9324. */
  9325. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9326. {
  9327. int rate = 100;
  9328. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9329. return rate;
  9330. if (level_diff < 0)
  9331. level_diff = MAX_LEVEL + (~level_diff + 1);
  9332. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9333. return rate;
  9334. return 100; // Penalty not found, return default
  9335. }
  9336. #endif
  9337. int pc_split_str(char *str,char **val,int num)
  9338. {
  9339. int i;
  9340. for (i=0; i<num && str; i++){
  9341. val[i] = str;
  9342. str = strchr(str,',');
  9343. if (str && i<num-1) //Do not remove a trailing comma.
  9344. *str++=0;
  9345. }
  9346. return i;
  9347. }
  9348. int pc_split_atoi(char* str, int* val, char sep, int max)
  9349. {
  9350. int i,j;
  9351. for (i=0; i<max; i++) {
  9352. if (!str) break;
  9353. val[i] = atoi(str);
  9354. str = strchr(str,sep);
  9355. if (str)
  9356. *str++=0;
  9357. }
  9358. //Zero up the remaining.
  9359. for(j=i; j < max; j++)
  9360. val[j] = 0;
  9361. return i;
  9362. }
  9363. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9364. {
  9365. static int warning=0;
  9366. int i,j;
  9367. for (i=0; i<max; i++) {
  9368. double f;
  9369. if (!str) break;
  9370. f = atof(str);
  9371. if (f < 0)
  9372. val[i] = 0;
  9373. else if (f > UINT_MAX) {
  9374. val[i] = UINT_MAX;
  9375. if (!warning) {
  9376. warning = 1;
  9377. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9378. }
  9379. } else
  9380. val[i] = (unsigned int)f;
  9381. str = strchr(str,sep);
  9382. if (str)
  9383. *str++=0;
  9384. }
  9385. //Zero up the remaining.
  9386. for(j=i; j < max; j++)
  9387. val[j] = 0;
  9388. return i;
  9389. }
  9390. /*==========================================
  9391. * sub DB reading.
  9392. * Function used to read skill_tree.txt
  9393. *------------------------------------------*/
  9394. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9395. {
  9396. unsigned char joblv = 0, skill_lv;
  9397. uint16 skill_id;
  9398. int idx, class_;
  9399. unsigned int i, offset = 3, skill_idx;
  9400. class_ = atoi(fields[0]);
  9401. skill_id = (uint16)atoi(fields[1]);
  9402. skill_lv = (unsigned char)atoi(fields[2]);
  9403. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  9404. {// job level requirement extra column
  9405. joblv = (unsigned char)atoi(fields[3]);
  9406. offset++;
  9407. }
  9408. if(!pcdb_checkid(class_))
  9409. {
  9410. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9411. return false;
  9412. }
  9413. idx = pc_class2idx(class_);
  9414. //This is to avoid adding two lines for the same skill. [Skotlex]
  9415. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  9416. if( skill_idx == MAX_SKILL_TREE )
  9417. {
  9418. 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_);
  9419. return false;
  9420. }
  9421. else if(skill_tree[idx][skill_idx].id)
  9422. {
  9423. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  9424. }
  9425. skill_tree[idx][skill_idx].id = skill_id;
  9426. skill_tree[idx][skill_idx].max = skill_lv;
  9427. skill_tree[idx][skill_idx].joblv = joblv;
  9428. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9429. {
  9430. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  9431. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  9432. }
  9433. return true;
  9434. }
  9435. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9436. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9437. {
  9438. int type, class_, diff;
  9439. type = atoi(fields[0]); //1=experience, 2=item drop
  9440. class_ = atoi(fields[1]);
  9441. diff = atoi(fields[2]);
  9442. if( type != 1 && type != 2 ){
  9443. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9444. return false;
  9445. }
  9446. if( !CHK_CLASS(class_) ){
  9447. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9448. return false;
  9449. }
  9450. diff = min(diff, MAX_LEVEL);
  9451. if( diff < 0 )
  9452. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9453. level_penalty[type][class_][diff] = atoi(fields[3]);
  9454. return true;
  9455. }
  9456. #endif
  9457. /** [Cydh]
  9458. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9459. * @param level Base level of player
  9460. * @param class_ Job ID @see enum e_job
  9461. * @return base_hp
  9462. */
  9463. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9464. double base_hp;
  9465. uint16 i, idx = pc_class2idx(class_);
  9466. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9467. #ifndef RENEWAL
  9468. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9469. #endif
  9470. for (i = 2; i <= level; i++)
  9471. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9472. if (class_ == JOB_SUMMONER)
  9473. base_hp += floor((base_hp / 2) + 0.5);
  9474. return (unsigned int)base_hp;
  9475. }
  9476. /** [Playtester]
  9477. * Calculates base sp of player.
  9478. * @param level Base level of player
  9479. * @param class_ Job ID @see enum e_job
  9480. * @return base_sp
  9481. */
  9482. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9483. double base_sp;
  9484. uint16 idx = pc_class2idx(class_);
  9485. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9486. switch (class_) {
  9487. case JOB_NINJA:
  9488. if (level >= 10)
  9489. base_sp -= 22;
  9490. else
  9491. base_sp = 11 + 3*level;
  9492. break;
  9493. case JOB_GUNSLINGER:
  9494. if (level > 10)
  9495. base_sp -= 18;
  9496. else
  9497. base_sp = 9 + 3*level;
  9498. break;
  9499. case JOB_SUMMONER:
  9500. base_sp -= floor(base_sp / 2);
  9501. break;
  9502. }
  9503. return (unsigned int)base_sp;
  9504. }
  9505. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9506. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9507. int idx, class_;
  9508. unsigned int i;
  9509. class_ = atoi(fields[0]);
  9510. if (!pcdb_checkid(class_)) {
  9511. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9512. return false;
  9513. }
  9514. idx = pc_class2idx(class_);
  9515. job_info[idx].max_weight_base = atoi(fields[1]);
  9516. job_info[idx].hp_factor = atoi(fields[2]);
  9517. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9518. job_info[idx].sp_factor = atoi(fields[4]);
  9519. #ifdef RENEWAL_ASPD
  9520. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9521. #else
  9522. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9523. #endif
  9524. {
  9525. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9526. }
  9527. return true;
  9528. }
  9529. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9530. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9531. {
  9532. int idx, class_, i;
  9533. class_ = atoi(fields[0]);
  9534. if(!pcdb_checkid(class_))
  9535. {
  9536. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9537. return false;
  9538. }
  9539. idx = pc_class2idx(class_);
  9540. for(i = 1; i < columns; i++)
  9541. {
  9542. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9543. }
  9544. return true;
  9545. }
  9546. //Reading job_exp.txt line
  9547. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9548. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9549. {
  9550. int idx, i, type;
  9551. int job_id,job_count,jobs[CLASS_COUNT];
  9552. unsigned int ui, maxlvl;
  9553. maxlvl = atoi(fields[0]);
  9554. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9555. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9556. return false;
  9557. }
  9558. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9559. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9560. return false;
  9561. }
  9562. type = atoi(fields[2]);
  9563. if(type < 0 || type > 1){
  9564. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9565. return false;
  9566. }
  9567. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9568. if (job_count < 1)
  9569. return false;
  9570. job_id = jobs[0];
  9571. if(!pcdb_checkid(job_id)){
  9572. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9573. return false;
  9574. }
  9575. idx = pc_class2idx(job_id);
  9576. job_info[idx].max_level[type] = maxlvl;
  9577. for(i=0; i<maxlvl; i++)
  9578. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9579. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9580. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9581. //should look like this:
  9582. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9583. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9584. job_info[idx].max_level[type]--;
  9585. if (job_info[idx].max_level[type] < maxlvl) {
  9586. ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
  9587. ShowInfo("Filling the missing values with the last exp entry.\n");
  9588. //Fill the requested values with the last entry.
  9589. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9590. for (; ui+2 < maxlvl; ui++)
  9591. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9592. job_info[idx].max_level[type] = maxlvl;
  9593. }
  9594. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9595. for (i = 1; i < job_count; i++) {
  9596. job_id = jobs[i];
  9597. if (!pcdb_checkid(job_id)) {
  9598. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9599. continue;
  9600. }
  9601. idx = pc_class2idx(job_id);
  9602. memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
  9603. job_info[idx].max_level[type] = maxlvl;
  9604. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9605. }
  9606. return true;
  9607. }
  9608. /**
  9609. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9610. * startlvl,endlvl,class,type,values...
  9611. */
  9612. #ifdef HP_SP_TABLES
  9613. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9614. {
  9615. int i, startlvl, endlvl;
  9616. int job_count,jobs[CLASS_COUNT];
  9617. short type;
  9618. startlvl = atoi(fields[0]);
  9619. if(startlvl<1){
  9620. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9621. return false;
  9622. }
  9623. endlvl = atoi(fields[1]);
  9624. if(endlvl<1 || endlvl<startlvl){
  9625. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9626. return false;
  9627. }
  9628. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9629. ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
  9630. return false;
  9631. }
  9632. type = atoi(fields[3]);
  9633. if(type < 0 || type > 1){
  9634. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9635. return false;
  9636. }
  9637. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9638. if (job_count < 1)
  9639. return false;
  9640. for (i = 0; i < job_count; i++) {
  9641. int idx, job_id = jobs[i], use_endlvl;
  9642. if (!pcdb_checkid(job_id)) {
  9643. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9644. return false;
  9645. }
  9646. idx = pc_class2idx(job_id);
  9647. if (startlvl > job_info[idx].max_level[0]) {
  9648. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9649. return false;
  9650. }
  9651. //Just read until available max level for this job, don't use MAX_LEVEL!
  9652. use_endlvl = endlvl;
  9653. if (use_endlvl > job_info[idx].max_level[0])
  9654. use_endlvl = job_info[idx].max_level[0];
  9655. if(type == 0) { //hp type
  9656. uint16 j;
  9657. for(j = 0; j < use_endlvl; j++) {
  9658. if (atoi(fields[j+4])) {
  9659. uint16 lvl_idx = startlvl-1+j;
  9660. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9661. //Tells if this HP is lower than previous level (but not for 99->100)
  9662. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9663. ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  9664. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9665. }
  9666. }
  9667. }
  9668. else { //sp type
  9669. uint16 j;
  9670. for(j = 0; j < use_endlvl; j++) {
  9671. if (atoi(fields[j+4])) {
  9672. uint16 lvl_idx = startlvl-1+j;
  9673. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9674. //Tells if this SP is lower than previous level (but not for 99->100)
  9675. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9676. ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  9677. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9678. }
  9679. }
  9680. }
  9681. }
  9682. return true;
  9683. }
  9684. #endif
  9685. /** [Cydh]
  9686. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9687. */
  9688. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9689. {
  9690. int idx, class_;
  9691. uint16 str, agi, vit, int_, dex, luk;
  9692. script_get_constant(trim(fields[0]),&class_);
  9693. if ((idx = pc_class2idx(class_)) < 0) {
  9694. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9695. return false;
  9696. }
  9697. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9698. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9699. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9700. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9701. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9702. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9703. job_info[idx].max_param.str = str;
  9704. job_info[idx].max_param.agi = agi;
  9705. job_info[idx].max_param.vit = vit;
  9706. job_info[idx].max_param.int_ = int_;
  9707. job_info[idx].max_param.dex = dex;
  9708. job_info[idx].max_param.luk = luk;
  9709. return true;
  9710. }
  9711. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9712. int i=1;
  9713. char line[24000]; //FIXME this seem too big
  9714. FILE *fp;
  9715. sprintf(line, "%s/statpoint.txt", basedir);
  9716. fp=fopen(line,"r");
  9717. if(fp == NULL){
  9718. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9719. return max(last_s,i);
  9720. } else {
  9721. int entries=0;
  9722. while(fgets(line, sizeof(line), fp))
  9723. {
  9724. int stat;
  9725. trim(line);
  9726. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9727. continue;
  9728. if ((stat=strtoul(line,NULL,10))<0)
  9729. stat=0;
  9730. if (i > MAX_LEVEL)
  9731. break;
  9732. statp[i]=stat;
  9733. i++;
  9734. entries++;
  9735. }
  9736. fclose(fp);
  9737. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9738. }
  9739. return max(last_s,i);
  9740. }
  9741. /*==========================================
  9742. * pc DB reading.
  9743. * job_exp.txt - required experience values
  9744. * skill_tree.txt - skill tree for every class
  9745. * attr_fix.txt - elemental adjustment table
  9746. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9747. * job_db2.txt - job,stats bonuses/lvl
  9748. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9749. *------------------------------------------*/
  9750. void pc_readdb(void) {
  9751. int i, k, s = 1;
  9752. const char* dbsubpath[] = {
  9753. "",
  9754. "/"DBIMPORT,
  9755. //add other path here
  9756. };
  9757. //reset
  9758. memset(job_info,0,sizeof(job_info)); // job_info table
  9759. // Reset and read skilltree
  9760. memset(skill_tree,0,sizeof(skill_tree));
  9761. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree, 0);
  9762. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree, 1);
  9763. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9764. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9765. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9766. for( k=1; k < 3; k++ ){ // fill in the blanks
  9767. int j;
  9768. for( j = 0; j < CLASS_ALL; j++ ){
  9769. int tmp = 0;
  9770. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9771. if( i == MAX_LEVEL+1 )
  9772. tmp = level_penalty[k][j][0];// reset
  9773. if( level_penalty[k][j][i] > 0 )
  9774. tmp = level_penalty[k][j][i];
  9775. else
  9776. level_penalty[k][j][i] = tmp;
  9777. }
  9778. }
  9779. }
  9780. #endif
  9781. // reset then read statspoint
  9782. memset(statp,0,sizeof(statp));
  9783. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9784. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  9785. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  9786. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9787. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9788. if(i==0) {
  9789. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9790. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9791. }
  9792. else {
  9793. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9794. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9795. }
  9796. s = pc_read_statsdb(dbsubpath2,s,i);
  9797. if (i == 0)
  9798. #ifdef RENEWAL_ASPD
  9799. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9800. #else
  9801. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9802. #endif
  9803. else
  9804. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9805. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  9806. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  9807. #ifdef HP_SP_TABLES
  9808. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  9809. #endif
  9810. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  9811. aFree(dbsubpath1);
  9812. aFree(dbsubpath2);
  9813. }
  9814. // generate the remaining parts of the db if necessary
  9815. k = battle_config.use_statpoint_table; //save setting
  9816. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  9817. statp[0] = 45; // seed value
  9818. for (; s <= MAX_LEVEL; s++)
  9819. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  9820. battle_config.use_statpoint_table = k; //restore setting
  9821. //Checking if all class have their data
  9822. for (i = 0; i < JOB_MAX; i++) {
  9823. int idx;
  9824. uint16 j;
  9825. if (!pcdb_checkid(i))
  9826. continue;
  9827. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  9828. continue; //Classes that do not need exp tables.
  9829. idx = pc_class2idx(i);
  9830. if (!job_info[idx].max_level[0])
  9831. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  9832. if (!job_info[idx].max_level[1])
  9833. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  9834. //Init and checking the empty value of Base HP/SP [Cydh]
  9835. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  9836. if (job_info[idx].base_hp[j] == 0)
  9837. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  9838. if (job_info[idx].base_sp[j] == 0)
  9839. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  9840. }
  9841. }
  9842. }
  9843. // Read MOTD on startup. [Valaris]
  9844. int pc_read_motd(void)
  9845. {
  9846. FILE* fp;
  9847. // clear old MOTD
  9848. memset(motd_text, 0, sizeof(motd_text));
  9849. // read current MOTD
  9850. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  9851. {
  9852. unsigned int entries = 0;
  9853. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  9854. {
  9855. char* buf = motd_text[entries];
  9856. unsigned int lines = 0;
  9857. size_t len;
  9858. lines++;
  9859. if( buf[0] == '/' && buf[1] == '/' )
  9860. continue;
  9861. len = strlen(buf);
  9862. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  9863. len--;
  9864. if( len ) {
  9865. char * ptr;
  9866. buf[len] = 0;
  9867. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  9868. 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);
  9869. }
  9870. else {// empty line
  9871. buf[0] = ' ';
  9872. buf[1] = 0;
  9873. }
  9874. entries++;
  9875. }
  9876. fclose(fp);
  9877. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  9878. }
  9879. else
  9880. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  9881. return 0;
  9882. }
  9883. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  9884. int i,cursor = 0;
  9885. struct item_cd* cd = NULL;
  9886. if( load ) {
  9887. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  9888. // no item cooldown is associated with this character
  9889. return;
  9890. }
  9891. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9892. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  9893. sd->item_delay[cursor].tick = cd->tick[i];
  9894. sd->item_delay[cursor].nameid = cd->nameid[i];
  9895. cursor++;
  9896. }
  9897. }
  9898. idb_remove(itemcd_db,sd->status.char_id);
  9899. } else {
  9900. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  9901. // create a new skill cooldown object for map storage
  9902. CREATE( cd, struct item_cd, 1 );
  9903. idb_put( itemcd_db, sd->status.char_id, cd );
  9904. }
  9905. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9906. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  9907. cd->tick[cursor] = sd->item_delay[i].tick;
  9908. cd->nameid[cursor] = sd->item_delay[i].nameid;
  9909. cursor++;
  9910. }
  9911. }
  9912. }
  9913. return;
  9914. }
  9915. /**
  9916. * Add item delay to player's item delay data
  9917. * @param sd Player
  9918. * @param id Item data
  9919. * @param tick Current tick
  9920. * @param n Item index in inventory
  9921. * @return 0: No delay, can consume item.
  9922. * 1: Has delay, cancel consumption.
  9923. **/
  9924. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  9925. int i;
  9926. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  9927. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  9928. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  9929. if( i < MAX_ITEMDELAYS ) {
  9930. if( sd->item_delay[i].nameid ) {// found
  9931. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  9932. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  9933. char e_msg[CHAT_SIZE_MAX];
  9934. if( e_tick > 99 )
  9935. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  9936. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  9937. else
  9938. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  9939. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  9940. clif_colormes(sd->fd,color_table[COLOR_YELLOW],e_msg);
  9941. return 1; // Delay has not expired yet
  9942. }
  9943. } else {// not yet used item (all slots are initially empty)
  9944. sd->item_delay[i].nameid = id->nameid;
  9945. }
  9946. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  9947. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  9948. } else {// should not happen
  9949. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  9950. }
  9951. //clean up used delays so we can give room for more
  9952. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  9953. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  9954. sd->item_delay[i].tick = 0;
  9955. sd->item_delay[i].nameid = 0;
  9956. }
  9957. }
  9958. return 0;
  9959. }
  9960. /**
  9961. * Check if player has delay to reuse item
  9962. * @param sd Player
  9963. * @param id Item data
  9964. * @param tick Current tick
  9965. * @param n Item index in inventory
  9966. * @return 0: No delay, can consume item.
  9967. * 1: Has delay, cancel consumption.
  9968. **/
  9969. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  9970. struct status_change *sc = NULL;
  9971. nullpo_retr(0, sd);
  9972. nullpo_retr(0, id);
  9973. // Do normal delay assignment
  9974. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  9975. return pc_itemcd_add(sd, id, tick, n);
  9976. // Send reply of delay remains
  9977. if (sc->data[id->delay_sc]) {
  9978. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  9979. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  9980. return 1;
  9981. }
  9982. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  9983. return 0;
  9984. }
  9985. /**
  9986. * Clear the dmglog data from player
  9987. * @param sd
  9988. * @param md
  9989. **/
  9990. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  9991. {
  9992. uint8 i;
  9993. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  9994. if (i < DAMAGELOG_SIZE) {
  9995. md->dmglog[i].id = 0;
  9996. md->dmglog[i].dmg = 0;
  9997. md->dmglog[i].flag = 0;
  9998. }
  9999. }
  10000. /**
  10001. * Add log to player's dmglog
  10002. * @param sd
  10003. * @param id Monster's GID
  10004. **/
  10005. void pc_damage_log_add(struct map_session_data *sd, int id)
  10006. {
  10007. uint8 i = 0;
  10008. if (!sd || !id)
  10009. return;
  10010. //Only store new data, don't need to renew the old one with same id
  10011. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10012. if (i < DAMAGELOG_SIZE_PC)
  10013. return;
  10014. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10015. if (sd->dmglog[i] == 0) {
  10016. sd->dmglog[i] = id;
  10017. return;
  10018. }
  10019. }
  10020. }
  10021. /**
  10022. * Clear dmglog data from player
  10023. * @param sd
  10024. * @param id Monster's id
  10025. **/
  10026. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10027. {
  10028. uint8 i;
  10029. struct mob_data *md = NULL;
  10030. if (!sd)
  10031. return;
  10032. if (!id) {
  10033. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10034. if( !sd->dmglog[i] ) //skip the empty value
  10035. continue;
  10036. if ((md = map_id2md(sd->dmglog[i])))
  10037. pc_clear_log_damage_sub(sd->status.char_id,md);
  10038. sd->dmglog[i] = 0;
  10039. }
  10040. }
  10041. else {
  10042. if ((md = map_id2md(id)))
  10043. pc_clear_log_damage_sub(sd->status.char_id,md);
  10044. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10045. if (i < DAMAGELOG_SIZE_PC)
  10046. sd->dmglog[i] = 0;
  10047. }
  10048. }
  10049. /**
  10050. * Status change data arrived from char-server
  10051. * @param sd: Player data
  10052. */
  10053. void pc_scdata_received(struct map_session_data *sd) {
  10054. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10055. }
  10056. /**
  10057. * Check player account expiration time and rental item expirations
  10058. * @param sd: Player data
  10059. */
  10060. void pc_check_expiration(struct map_session_data *sd) {
  10061. #ifndef ENABLE_SC_SAVING
  10062. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10063. #endif
  10064. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10065. time_t exp_time = sd->expiration_time;
  10066. char tmpstr[1024];
  10067. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10068. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  10069. pc_expire_check(sd);
  10070. }
  10071. }
  10072. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10073. struct map_session_data *sd = map_id2sd(id);
  10074. if( !sd ) return 0;
  10075. sd->expiration_tid = INVALID_TIMER;
  10076. if( sd->fd )
  10077. clif_authfail_fd(sd->fd,10);
  10078. map_quit(sd);
  10079. return 0;
  10080. }
  10081. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10082. struct map_session_data *sd = map_id2sd(id);
  10083. if (!sd)
  10084. return 0;
  10085. sd->autotrade_tid = INVALID_TIMER;
  10086. if (sd->state.autotrade&2)
  10087. vending_reopen(sd);
  10088. if (sd->state.autotrade&4)
  10089. buyingstore_reopen(sd);
  10090. if (!sd->vender_id && !sd->buyer_id) {
  10091. sd->state.autotrade = 0;
  10092. map_quit(sd);
  10093. }
  10094. return 0;
  10095. }
  10096. /* this timer exists only when a character with a expire timer > 24h is online */
  10097. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10098. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10099. struct s_mapiterator* iter;
  10100. struct map_session_data* sd;
  10101. iter = mapit_getallusers();
  10102. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10103. if( sd->expiration_time )
  10104. pc_expire_check(sd);
  10105. mapit_free(iter);
  10106. return 0;
  10107. }
  10108. void pc_expire_check(struct map_session_data *sd) {
  10109. /* ongoing timer */
  10110. if( sd->expiration_tid != INVALID_TIMER )
  10111. return;
  10112. /* not within the next 24h, enable the global check */
  10113. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10114. /* global check not running, enable */
  10115. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10116. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10117. return;
  10118. }
  10119. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10120. }
  10121. /**
  10122. * Deposit some money to bank
  10123. * @param sd
  10124. * @param money Amount of money to deposit
  10125. **/
  10126. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10127. unsigned int limit_check = money + sd->bank_vault;
  10128. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10129. return BDA_OVERFLOW;
  10130. } else if ( money > sd->status.zeny ) {
  10131. return BDA_NO_MONEY;
  10132. }
  10133. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10134. return BDA_NO_MONEY;
  10135. sd->bank_vault += money;
  10136. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10137. if( save_settings&CHARSAVE_BANK )
  10138. chrif_save(sd,0);
  10139. return BDA_SUCCESS;
  10140. }
  10141. /**
  10142. * Withdraw money from bank
  10143. * @param sd
  10144. * @param money Amount of money that will be withdrawn
  10145. **/
  10146. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10147. unsigned int limit_check = money + sd->status.zeny;
  10148. if( money <= 0 ) {
  10149. return BWA_UNKNOWN_ERROR;
  10150. } else if ( money > sd->bank_vault ) {
  10151. return BWA_NO_MONEY;
  10152. } else if ( limit_check > MAX_ZENY ) {
  10153. /* no official response for this scenario exists. */
  10154. clif_colormes(sd->fd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  10155. return BWA_UNKNOWN_ERROR;
  10156. }
  10157. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10158. return BWA_NO_MONEY;
  10159. sd->bank_vault -= money;
  10160. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10161. if( save_settings&CHARSAVE_BANK )
  10162. chrif_save(sd,0);
  10163. return BWA_SUCCESS;
  10164. }
  10165. /**
  10166. * Clear Crimson Marker data from caster
  10167. * @param sd: Player
  10168. **/
  10169. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10170. uint8 i;
  10171. if (!sd)
  10172. return;
  10173. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10174. struct block_list *bl = NULL;
  10175. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10176. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10177. sd->c_marker[i] = 0;
  10178. }
  10179. }
  10180. /**
  10181. * Show version to player
  10182. * @param sd: Player
  10183. **/
  10184. void pc_show_version(struct map_session_data *sd) {
  10185. const char* svn = get_svn_revision();
  10186. char buf[CHAT_SIZE_MAX];
  10187. if( svn[0] != UNKNOWN_VERSION )
  10188. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10189. else {
  10190. const char* git = get_git_hash();
  10191. if( git[0] != UNKNOWN_VERSION )
  10192. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10193. else
  10194. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10195. }
  10196. clif_displaymessage(sd->fd,buf);
  10197. }
  10198. /**
  10199. * Run bonus_script on player
  10200. * @param sd
  10201. * @author [Cydh]
  10202. **/
  10203. void pc_bonus_script(struct map_session_data *sd) {
  10204. int now = gettick();
  10205. struct linkdb_node *node = NULL, *next = NULL;
  10206. if (!sd || !(node = sd->bonus_script.head))
  10207. return;
  10208. while (node) {
  10209. struct s_bonus_script_entry *entry = NULL;
  10210. next = node->next;
  10211. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10212. // Only start timer for new bonus_script
  10213. if (entry->tid == INVALID_TIMER) {
  10214. if (entry->icon != SI_BLANK) // Gives status icon if exist
  10215. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10216. entry->tick += now;
  10217. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10218. }
  10219. if (entry->script)
  10220. run_script(entry->script, 0, sd->bl.id, 0);
  10221. else
  10222. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10223. }
  10224. node = next;
  10225. }
  10226. }
  10227. /**
  10228. * Add bonus_script to player
  10229. * @param sd Player
  10230. * @param script_str Script string
  10231. * @param dur Duration in ms
  10232. * @param icon SI
  10233. * @param flag Flags @see enum e_bonus_script_flags
  10234. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10235. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10236. * @author [Cydh]
  10237. **/
  10238. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum si_type icon, uint16 flag, uint8 type) {
  10239. struct script_code *script = NULL;
  10240. struct linkdb_node *node = NULL;
  10241. struct s_bonus_script_entry *entry = NULL;
  10242. if (!sd)
  10243. return NULL;
  10244. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10245. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10246. return NULL;
  10247. }
  10248. // Duplication checks
  10249. if ((node = sd->bonus_script.head)) {
  10250. while (node) {
  10251. entry = (struct s_bonus_script_entry *)node->data;
  10252. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10253. int newdur = gettick() + dur;
  10254. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10255. settick_timer(entry->tid, newdur);
  10256. script_free_code(script);
  10257. return NULL;
  10258. }
  10259. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10260. break;
  10261. else { // No duplicate bonus
  10262. script_free_code(script);
  10263. return NULL;
  10264. }
  10265. }
  10266. node = node->next;
  10267. }
  10268. }
  10269. CREATE(entry, struct s_bonus_script_entry, 1);
  10270. entry->script_buf = StringBuf_Malloc();
  10271. StringBuf_AppendStr(entry->script_buf, script_str);
  10272. entry->tid = INVALID_TIMER;
  10273. entry->flag = flag;
  10274. entry->icon = icon;
  10275. entry->tick = dur; // Use duration first, on run change to expire time
  10276. entry->type = type;
  10277. entry->script = script;
  10278. sd->bonus_script.count++;
  10279. return entry;
  10280. }
  10281. /**
  10282. * Remove bonus_script data from player
  10283. * @param sd: Target player
  10284. * @param list: Bonus script entry from player
  10285. * @author [Cydh]
  10286. **/
  10287. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10288. if (entry->tid != INVALID_TIMER)
  10289. delete_timer(entry->tid, pc_bonus_script_timer);
  10290. if (entry->script)
  10291. script_free_code(entry->script);
  10292. if (entry->script_buf)
  10293. StringBuf_Free(entry->script_buf);
  10294. if (sd) {
  10295. if (entry->icon != SI_BLANK)
  10296. clif_status_load(&sd->bl, entry->icon, 0);
  10297. if (sd->bonus_script.count > 0)
  10298. sd->bonus_script.count--;
  10299. }
  10300. aFree(entry);
  10301. }
  10302. /**
  10303. * Do final process if no entry left
  10304. * @param sd
  10305. **/
  10306. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10307. if (sd->bonus_script.count == 0) {
  10308. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10309. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10310. linkdb_final(&sd->bonus_script.head);
  10311. }
  10312. }
  10313. /**
  10314. * Timer for bonus_script
  10315. * @param tid
  10316. * @param tick
  10317. * @param id
  10318. * @param data
  10319. * @author [Cydh]
  10320. **/
  10321. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10322. struct map_session_data *sd;
  10323. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10324. sd = map_id2sd(id);
  10325. if (!sd) {
  10326. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10327. return 0;
  10328. }
  10329. if (tid == INVALID_TIMER)
  10330. return 0;
  10331. if (!sd->bonus_script.head || entry == NULL) {
  10332. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10333. return 0;
  10334. }
  10335. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10336. pc_bonus_script_free_entry(sd, entry);
  10337. pc_bonus_script_check_final(sd);
  10338. status_calc_pc(sd,SCO_NONE);
  10339. return 0;
  10340. }
  10341. /**
  10342. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10343. * @param sd: Target player
  10344. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10345. * @author [Cydh]
  10346. **/
  10347. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10348. struct linkdb_node *node = NULL;
  10349. uint16 count = 0;
  10350. if (!sd || !(node = sd->bonus_script.head))
  10351. return;
  10352. while (node) {
  10353. struct linkdb_node *next = node->next;
  10354. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10355. if (entry && (
  10356. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10357. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10358. (flag&entry->flag) || // Matched flag
  10359. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10360. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10361. )
  10362. )
  10363. {
  10364. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10365. pc_bonus_script_free_entry(sd, entry);
  10366. count++;
  10367. }
  10368. node = next;
  10369. }
  10370. pc_bonus_script_check_final(sd);
  10371. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10372. status_calc_pc(sd,SCO_NONE);
  10373. }
  10374. /** [Cydh]
  10375. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10376. * @param sd: Target player
  10377. */
  10378. void pc_cell_basilica(struct map_session_data *sd) {
  10379. nullpo_retv(sd);
  10380. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10381. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10382. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10383. }
  10384. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10385. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10386. }
  10387. /** [Cydh]
  10388. * Get maximum specified parameter for specified class
  10389. * @param class_: sd->class
  10390. * @param sex: sd->status.sex
  10391. * @param flag: parameter will be checked
  10392. * @return max_param
  10393. */
  10394. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10395. int idx = -1, class_ = sd->class_;
  10396. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10397. short max_param = 0;
  10398. switch (param) {
  10399. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10400. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10401. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10402. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10403. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10404. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10405. }
  10406. if (max_param > 0)
  10407. return max_param;
  10408. }
  10409. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10410. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10411. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  10412. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10413. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10414. }
  10415. /**
  10416. * Get max ASPD for player based on Class
  10417. * @param sd Player
  10418. * @return ASPD
  10419. */
  10420. short pc_maxaspd(struct map_session_data *sd) {
  10421. nullpo_ret(sd);
  10422. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10423. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10424. battle_config.max_aspd ));
  10425. }
  10426. /**
  10427. * Calculates total item-group related bonuses for the given item
  10428. * @param sd Player
  10429. * @param nameid Item ID
  10430. * @return Heal rate
  10431. **/
  10432. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10433. short bonus = 0;
  10434. uint8 i;
  10435. if (!sd->itemgrouphealrate_count)
  10436. return bonus;
  10437. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10438. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10439. struct s_item_group_db *group = NULL;
  10440. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10441. continue;
  10442. for (j = 0; j < group->random[0].data_qty; j++) {
  10443. if (group->random[0].data[j].nameid == nameid) {
  10444. bonus += sd->itemgrouphealrate[i]->rate;
  10445. break;
  10446. }
  10447. }
  10448. }
  10449. return bonus;
  10450. }
  10451. /**
  10452. * Calculates total item-group related bonuses for the given item group
  10453. * @param sd Player
  10454. * @param group_id Item Group ID
  10455. * @return Heal rate
  10456. **/
  10457. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10458. short bonus = 0;
  10459. uint8 i;
  10460. if (!sd->itemgrouphealrate_count)
  10461. return bonus;
  10462. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10463. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10464. return sd->itemgrouphealrate[i]->rate;
  10465. }
  10466. return bonus;
  10467. }
  10468. /**
  10469. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10470. * @param eqi Item EQI of enum equip_index
  10471. * @param *equip_index Player's equip_index[]
  10472. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10473. * @return True if item in same inventory index, False if doesn't
  10474. */
  10475. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10476. if (index < 0 || index >= MAX_INVENTORY)
  10477. return true;
  10478. // Dual weapon checks
  10479. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10480. return true;
  10481. // Headgear with Mid & Low location
  10482. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10483. return true;
  10484. // Headgear with Top & Mid or Low location
  10485. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10486. return true;
  10487. // Headgear with Mid & Low location
  10488. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10489. return true;
  10490. // Headgear with Top & Mid or Low location
  10491. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10492. return true;
  10493. return false;
  10494. }
  10495. /**
  10496. * Generate Unique item ID for player
  10497. * @param sd : Player
  10498. * @return A generated Unique item ID
  10499. */
  10500. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10501. nullpo_ret(sd);
  10502. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10503. }
  10504. /**
  10505. * Validating skill from player after logged on
  10506. * @param sd
  10507. **/
  10508. void pc_validate_skill(struct map_session_data *sd) {
  10509. if (sd) {
  10510. uint16 i = 0, count = 0;
  10511. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10512. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10513. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10514. for (i = 0; i < MAX_SKILL; i++) {
  10515. uint16 idx = 0;
  10516. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10517. continue;
  10518. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10519. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10520. count++;
  10521. }
  10522. else
  10523. ShowWarning("pc_validate_skill: Removing invalid skill '%d' from player (AID=%d CID=%d).\n", tmp_skills[i].id, sd->status.account_id, sd->status.char_id);
  10524. }
  10525. }
  10526. }
  10527. /**
  10528. * Toggle to remember if the questinfo is displayed yet or not.
  10529. * @param qi_display Display flag
  10530. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10531. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10532. **/
  10533. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10534. if (show) {
  10535. // Check if need to be displayed
  10536. if ((*qi_display) != 1) {
  10537. (*qi_display) = 1;
  10538. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10539. }
  10540. }
  10541. else {
  10542. // Check if need to be hide
  10543. if ((*qi_display) != 0) {
  10544. (*qi_display) = 0;
  10545. #if PACKETVER >= 20120410
  10546. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10547. #else
  10548. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10549. #endif
  10550. }
  10551. }
  10552. }
  10553. /**
  10554. * Show available NPC Quest / Event Icon Check [Kisuka]
  10555. * @param sd Player
  10556. **/
  10557. void pc_show_questinfo(struct map_session_data *sd) {
  10558. #if PACKETVER >= 20090218
  10559. struct questinfo *qi = NULL;
  10560. unsigned short i;
  10561. uint8 j;
  10562. int8 mystate = 0;
  10563. bool failed = false;
  10564. nullpo_retv(sd);
  10565. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10566. return;
  10567. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10568. return;
  10569. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10570. qi = &map[sd->bl.m].qi_data[i];
  10571. if (!qi)
  10572. continue;
  10573. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10574. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10575. continue;
  10576. }
  10577. // Level range checks
  10578. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10579. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10580. continue;
  10581. }
  10582. // Quest requirements
  10583. if (qi->req_count) {
  10584. failed = false;
  10585. for (j = 0; j < qi->req_count; j++) {
  10586. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10587. mystate = mystate + (mystate < 1);
  10588. if (mystate != qi->req[j].state) {
  10589. failed = true;
  10590. break;
  10591. }
  10592. }
  10593. if (failed) {
  10594. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10595. continue;
  10596. }
  10597. }
  10598. // Job requirements
  10599. if (qi->jobid_count) {
  10600. failed = true;
  10601. for (j = 0; j < qi->jobid_count; j++) {
  10602. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10603. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10604. failed = false;
  10605. break;
  10606. }
  10607. }
  10608. if (!failed)
  10609. continue;
  10610. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10611. }
  10612. else {
  10613. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10614. }
  10615. }
  10616. #endif
  10617. }
  10618. /**
  10619. * Reinit the questinfo for player when changing map
  10620. * @param sd Player
  10621. **/
  10622. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10623. #if PACKETVER >= 20090218
  10624. nullpo_retv(sd);
  10625. if (sd->qi_display) {
  10626. aFree(sd->qi_display);
  10627. sd->qi_display = NULL;
  10628. }
  10629. sd->qi_count = 0;
  10630. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10631. return;
  10632. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10633. return;
  10634. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10635. #endif
  10636. }
  10637. /*==========================================
  10638. * pc Init/Terminate
  10639. *------------------------------------------*/
  10640. void do_final_pc(void) {
  10641. db_destroy(itemcd_db);
  10642. do_final_pc_groups();
  10643. ers_destroy(pc_sc_display_ers);
  10644. ers_destroy(pc_itemgrouphealrate_ers);
  10645. ers_destroy(num_reg_ers);
  10646. ers_destroy(str_reg_ers);
  10647. }
  10648. void do_init_pc(void) {
  10649. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10650. pc_readdb();
  10651. pc_read_motd(); // Read MOTD [Valaris]
  10652. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10653. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10654. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10655. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10656. add_timer_func_list(pc_autosave, "pc_autosave");
  10657. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10658. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10659. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10660. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10661. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10662. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10663. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10664. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10665. // 0=day, 1=night [Yor]
  10666. night_flag = battle_config.night_at_start ? 1 : 0;
  10667. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10668. int day_duration = battle_config.day_duration;
  10669. int night_duration = battle_config.night_duration;
  10670. // add night/day timer [Yor]
  10671. add_timer_func_list(map_day_timer, "map_day_timer");
  10672. add_timer_func_list(map_night_timer, "map_night_timer");
  10673. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10674. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10675. }
  10676. do_init_pc_groups();
  10677. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  10678. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10679. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10680. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10681. ers_chunk_size(pc_sc_display_ers, 150);
  10682. ers_chunk_size(num_reg_ers, 300);
  10683. ers_chunk_size(str_reg_ers, 50);
  10684. }