pc.cpp 448 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/database.hpp"
  11. #include "../common/ers.hpp" // ers_destroy
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "script.hpp" // struct script_reg, struct script_regstr
  50. #include "searchstore.hpp" // struct s_search_store_info
  51. #include "status.hpp" // OPTION_*, struct weapon_atk
  52. #include "storage.hpp"
  53. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  54. #include "vending.hpp" // struct s_vending
  55. using namespace rathena;
  56. JobDatabase job_db;
  57. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  58. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  59. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  60. PlayerStatPointDatabase statpoint_db;
  61. SkillTreeDatabase skill_tree_db;
  62. // timer for night.day implementation
  63. int day_timer_tid = INVALID_TIMER;
  64. int night_timer_tid = INVALID_TIMER;
  65. struct eri *pc_sc_display_ers = NULL;
  66. struct eri *num_reg_ers;
  67. struct eri *str_reg_ers;
  68. int pc_expiration_tid = INVALID_TIMER;
  69. struct fame_list smith_fame_list[MAX_FAME_LIST];
  70. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  71. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  72. const std::string AttendanceDatabase::getDefaultLocation(){
  73. return std::string(db_path) + "/attendance.yml";
  74. }
  75. /**
  76. * Reads and parses an entry from the attendance_db.
  77. * @param node: YAML node containing the entry.
  78. * @return count of successfully parsed rows
  79. */
  80. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  81. uint32 start;
  82. if( !this->asUInt32( node, "Start", start ) ){
  83. return 0;
  84. }
  85. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  86. bool exists = attendance_period != nullptr;
  87. if( !exists ){
  88. if( !this->nodeExists( node, "End" ) ){
  89. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  90. return 0;
  91. }
  92. if( !this->nodeExists( node, "Rewards" ) ){
  93. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  94. return 0;
  95. }
  96. attendance_period = std::make_shared<s_attendance_period>();
  97. attendance_period->start = start;
  98. }
  99. // If it does not exist yet, we need to check it for sure
  100. bool requiresCollisionDetection = !exists;
  101. if( this->nodeExists( node, "End" ) ){
  102. uint32 end;
  103. if( !this->asUInt32( node, "End", end ) ){
  104. return 0;
  105. }
  106. // If the period is outdated already, we do not even bother parsing
  107. if( end < date_get( DT_YYYYMMDD ) ){
  108. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  109. return 0;
  110. }
  111. if( !exists || attendance_period->end != end ){
  112. requiresCollisionDetection = true;
  113. attendance_period->end = end;
  114. }
  115. }
  116. // Collision detection
  117. if( requiresCollisionDetection ){
  118. bool collision = false;
  119. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  120. std::shared_ptr<s_attendance_period> period = pair.second;
  121. if( exists && period->start == attendance_period->start ){
  122. // Dont compare to yourself
  123. continue;
  124. }
  125. // Check if start is inside another period
  126. if( period->start <= attendance_period->start && start <= period->end ){
  127. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  128. collision = true;
  129. break;
  130. }
  131. // Check if end is inside another period
  132. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  133. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  134. collision = true;
  135. break;
  136. }
  137. }
  138. if( collision ){
  139. return 0;
  140. }
  141. }
  142. if( this->nodeExists( node, "Rewards" ) ){
  143. const YAML::Node& rewardsNode = node["Rewards"];
  144. for( const YAML::Node& rewardNode : rewardsNode ){
  145. uint32 day;
  146. if( !this->asUInt32( rewardNode, "Day", day ) ){
  147. continue;
  148. }
  149. day -= 1;
  150. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  151. bool reward_exists = reward != nullptr;
  152. if( !reward_exists ){
  153. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  154. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  155. return 0;
  156. }
  157. reward = std::make_shared<s_attendance_reward>();
  158. }
  159. if( this->nodeExists( rewardNode, "ItemId" ) ){
  160. t_itemid item_id;
  161. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  162. continue;
  163. }
  164. if( item_id == 0 || !itemdb_exists( item_id ) ){
  165. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  166. continue;
  167. }
  168. reward->item_id = item_id;
  169. }
  170. if( this->nodeExists( rewardNode, "Amount" ) ){
  171. uint16 amount;
  172. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  173. continue;
  174. }
  175. if( amount == 0 ){
  176. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  177. amount = 1;
  178. }else if( amount > MAX_AMOUNT ){
  179. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  180. amount = MAX_AMOUNT;
  181. }
  182. reward->amount = amount;
  183. }else{
  184. if( !reward_exists ){
  185. reward->amount = 1;
  186. }
  187. }
  188. if( !reward_exists ){
  189. attendance_period->rewards[day] = reward;
  190. }
  191. }
  192. bool missing_day = false;
  193. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  194. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  195. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  196. missing_day = true;
  197. break;
  198. }
  199. }
  200. if( missing_day ){
  201. return 0;
  202. }
  203. }
  204. if( !exists ){
  205. this->put( start, attendance_period );
  206. }
  207. return 1;
  208. }
  209. AttendanceDatabase attendance_db;
  210. const std::string PenaltyDatabase::getDefaultLocation(){
  211. return std::string( db_path ) + "/level_penalty.yml";
  212. }
  213. uint64 PenaltyDatabase::parseBodyNode( const YAML::Node& node ){
  214. std::string type_constant;
  215. if( !this->asString( node, "Type", type_constant ) ){
  216. return 0;
  217. }
  218. int64 constant_value;
  219. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  220. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  221. return 0;
  222. }
  223. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  224. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  225. return 0;
  226. }
  227. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  228. std::shared_ptr<s_penalty> penalty = this->find( type );
  229. bool exists = penalty != nullptr;
  230. if( !exists ){
  231. penalty = std::make_shared<s_penalty>();
  232. penalty->type = type;
  233. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  234. penalty->rate[i] = UINT16_MAX;
  235. }
  236. }
  237. if( this->nodeExists( node, "LevelDifferences" ) ){
  238. for( const YAML::Node& levelNode : node["LevelDifferences"] ){
  239. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  240. return 0;
  241. }
  242. int32 difference;
  243. if( !this->asInt32( levelNode, "Difference", difference ) ){
  244. return 0;
  245. }
  246. if( std::abs( difference ) > MAX_LEVEL ){
  247. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  248. return 0;
  249. }
  250. uint16 rate;
  251. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  252. return 0;
  253. }
  254. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  255. }
  256. }
  257. if( !exists ){
  258. this->put( type, penalty );
  259. }
  260. return 1;
  261. }
  262. void PenaltyDatabase::loadingFinished(){
  263. for( const auto& pair : *this ){
  264. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  265. uint16 rate = pair.second->rate[i];
  266. // Check if it has been defined
  267. if( rate == UINT16_MAX ){
  268. pair.second->rate[i] = last_rate;
  269. }else{
  270. last_rate = rate;
  271. }
  272. }
  273. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  274. uint16 rate = pair.second->rate[i];
  275. // Check if it has been defined
  276. if( rate == UINT16_MAX ){
  277. pair.second->rate[i] = last_rate;
  278. }else{
  279. last_rate = rate;
  280. }
  281. }
  282. }
  283. }
  284. PenaltyDatabase penalty_db;
  285. #define MOTD_LINE_SIZE 128
  286. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  287. bool reg_load;
  288. /**
  289. * Translation table from athena equip index to aegis bitmask
  290. */
  291. unsigned int equip_bitmask[EQI_MAX] = {
  292. EQP_ACC_L, // EQI_ACC_L
  293. EQP_ACC_R, // EQI_ACC_R
  294. EQP_SHOES, // EQI_SHOES
  295. EQP_GARMENT, // EQI_GARMENT
  296. EQP_HEAD_LOW, // EQI_HEAD_LOW
  297. EQP_HEAD_MID, // EQI_HEAD_MID
  298. EQP_HEAD_TOP, // EQI_HEAD_TOP
  299. EQP_ARMOR, // EQI_ARMOR
  300. EQP_HAND_L, // EQI_HAND_L
  301. EQP_HAND_R, // EQI_HAND_R
  302. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  303. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  304. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  305. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  306. EQP_AMMO, // EQI_AMMO
  307. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  308. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  309. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  310. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  311. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  312. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  313. };
  314. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  315. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  316. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  317. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  318. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  319. };
  320. void pc_set_reg_load( bool val ){
  321. reg_load = val;
  322. }
  323. /**
  324. * Item Cool Down Delay Saving
  325. * Struct item_cd is not a member of struct map_session_data
  326. * to keep cooldowns in memory between player log-ins.
  327. * All cooldowns are reset when server is restarted.
  328. **/
  329. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  330. struct item_cd {
  331. t_tick tick[MAX_ITEMDELAYS]; //tick
  332. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  333. };
  334. /**
  335. * Converts a class to its array index for CLASS_COUNT defined arrays.
  336. * Note that it does not do a validity check for speed purposes, where parsing
  337. * player input make sure to use a pcdb_checkid first!
  338. * @param class_ Job ID see enum e_job
  339. * @return Class Index
  340. */
  341. int pc_class2idx(int class_) {
  342. if (class_ >= JOB_NOVICE_HIGH)
  343. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  344. return class_;
  345. }
  346. /**
  347. * Get player's group ID
  348. * @param sd
  349. * @return Group ID
  350. */
  351. int pc_get_group_id(struct map_session_data *sd) {
  352. return sd->group_id;
  353. }
  354. /** Get player's group Level
  355. * @param sd
  356. * @return Group Level
  357. */
  358. int pc_get_group_level(struct map_session_data *sd) {
  359. return sd->group_level;
  360. }
  361. /**
  362. * Remove a player from queue after timeout
  363. * @param tid: Timer ID
  364. * @param tick: Timer
  365. * @param id: ID
  366. * @return 0 on success or 1 otherwise
  367. */
  368. static TIMER_FUNC(pc_on_expire_active)
  369. {
  370. map_session_data *sd = (map_session_data *)data;
  371. nullpo_retr(1, sd);
  372. sd->tid_queue_active = INVALID_TIMER;
  373. bg_queue_leave(sd);
  374. clif_bg_queue_entry_init(sd);
  375. return 0;
  376. }
  377. /**
  378. * Function used to set timer externally
  379. * @param sd: Player data
  380. */
  381. void pc_set_bg_queue_timer(map_session_data *sd) {
  382. nullpo_retv(sd);
  383. if (sd->tid_queue_active != INVALID_TIMER) {
  384. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  385. sd->tid_queue_active = INVALID_TIMER;
  386. }
  387. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  388. }
  389. /**
  390. * Function used to delete timer externally
  391. * @param sd: Player data
  392. */
  393. void pc_delete_bg_queue_timer(map_session_data *sd) {
  394. nullpo_retv(sd);
  395. if (sd->tid_queue_active != INVALID_TIMER) {
  396. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  397. sd->tid_queue_active = INVALID_TIMER;
  398. }
  399. }
  400. static TIMER_FUNC(pc_invincible_timer){
  401. struct map_session_data *sd;
  402. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  403. return 1;
  404. if(sd->invincible_timer != tid){
  405. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  406. return 0;
  407. }
  408. sd->invincible_timer = INVALID_TIMER;
  409. skill_unit_move(&sd->bl,tick,1);
  410. return 0;
  411. }
  412. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  413. nullpo_retv(sd);
  414. if( sd->invincible_timer != INVALID_TIMER )
  415. delete_timer(sd->invincible_timer,pc_invincible_timer);
  416. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  417. }
  418. void pc_delinvincibletimer(struct map_session_data* sd)
  419. {
  420. nullpo_retv(sd);
  421. if( sd->invincible_timer != INVALID_TIMER )
  422. {
  423. delete_timer(sd->invincible_timer,pc_invincible_timer);
  424. sd->invincible_timer = INVALID_TIMER;
  425. skill_unit_move(&sd->bl,gettick(),1);
  426. }
  427. }
  428. static TIMER_FUNC(pc_spiritball_timer){
  429. struct map_session_data *sd;
  430. int i;
  431. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  432. return 1;
  433. if( sd->spiritball <= 0 )
  434. {
  435. 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);
  436. sd->spiritball = 0;
  437. return 0;
  438. }
  439. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  440. if( i == sd->spiritball )
  441. {
  442. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  443. return 0;
  444. }
  445. sd->spiritball--;
  446. if( i != sd->spiritball )
  447. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  448. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  449. clif_spiritball(&sd->bl);
  450. return 0;
  451. }
  452. /**
  453. * Adds a spiritball to player for 'interval' ms
  454. * @param sd
  455. * @param interval
  456. * @param max
  457. */
  458. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  459. {
  460. int tid;
  461. uint8 i;
  462. nullpo_retv(sd);
  463. if(max > MAX_SPIRITBALL)
  464. max = MAX_SPIRITBALL;
  465. if(sd->spiritball < 0)
  466. sd->spiritball = 0;
  467. if( sd->spiritball && sd->spiritball >= max )
  468. {
  469. if(sd->spirit_timer[0] != INVALID_TIMER)
  470. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  471. sd->spiritball--;
  472. if( sd->spiritball != 0 )
  473. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  474. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  475. }
  476. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  477. 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);
  478. if( i != sd->spiritball )
  479. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  480. sd->spirit_timer[i] = tid;
  481. sd->spiritball++;
  482. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  483. clif_millenniumshield(&sd->bl,sd->spiritball);
  484. else
  485. clif_spiritball(&sd->bl);
  486. }
  487. /**
  488. * Removes number of spiritball from player
  489. * @param sd
  490. * @param count
  491. * @param type 1 = doesn't give client effect
  492. */
  493. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  494. {
  495. uint8 i;
  496. nullpo_retv(sd);
  497. if(sd->spiritball <= 0) {
  498. sd->spiritball = 0;
  499. return;
  500. }
  501. if(count == 0)
  502. return;
  503. if(count > sd->spiritball)
  504. count = sd->spiritball;
  505. sd->spiritball -= count;
  506. if(count > MAX_SPIRITBALL)
  507. count = MAX_SPIRITBALL;
  508. for(i=0;i<count;i++) {
  509. if(sd->spirit_timer[i] != INVALID_TIMER) {
  510. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  511. sd->spirit_timer[i] = INVALID_TIMER;
  512. }
  513. }
  514. for(i=count;i<MAX_SPIRITBALL;i++) {
  515. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  516. sd->spirit_timer[i] = INVALID_TIMER;
  517. }
  518. if(!type) {
  519. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  520. clif_millenniumshield(&sd->bl,sd->spiritball);
  521. else
  522. clif_spiritball(&sd->bl);
  523. }
  524. }
  525. /**
  526. * Adds a soulball to player
  527. * @param sd: Player data
  528. * @param max: Max amount of soulballs
  529. */
  530. int pc_addsoulball(map_session_data *sd, int max)
  531. {
  532. nullpo_ret(sd);
  533. status_change *sc = status_get_sc(&sd->bl);
  534. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  535. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  536. sd->soulball = 0;
  537. }
  538. max = min(max, MAX_SOUL_BALL);
  539. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  540. if (sd->soulball && sd->soulball >= max)
  541. sd->soulball--;
  542. sd->soulball++;
  543. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  544. clif_soulball(sd);
  545. return 0;
  546. }
  547. /**
  548. * Removes number of soulball from player
  549. * @param sd: Player data
  550. * @param count: Amount to remove
  551. * @param type: true = doesn't give client effect
  552. */
  553. int pc_delsoulball(map_session_data *sd, int count, bool type)
  554. {
  555. nullpo_ret(sd);
  556. if (count <= 0)
  557. return 0;
  558. status_change *sc = status_get_sc(&sd->bl);
  559. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  560. sd->soulball = 0;
  561. }else{
  562. sd->soulball -= cap_value(count, 0, sd->soulball);
  563. if (sd->soulball == 0)
  564. status_change_end(&sd->bl, SC_SOULENERGY, INVALID_TIMER);
  565. else
  566. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  567. }
  568. if (!type)
  569. clif_soulball(sd);
  570. return 0;
  571. }
  572. /**
  573. * Adds servantballs to a player
  574. * @param sd: Player data
  575. * @param amount: Amount to add
  576. */
  577. void pc_addservantball( struct map_session_data& sd, int count ){
  578. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  579. clif_servantball( sd );
  580. }
  581. /**
  582. * Removes number of servantballs from player
  583. * @param sd: Player data
  584. * @param count: Amount to remove
  585. */
  586. void pc_delservantball( struct map_session_data& sd, int count ){
  587. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  588. clif_servantball( sd );
  589. }
  590. /**
  591. * Adds abyssballs to a player
  592. * @param sd: Player data
  593. * @param amount: Amount to add
  594. */
  595. void pc_addabyssball( struct map_session_data& sd, int count ){
  596. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  597. clif_abyssball( sd );
  598. }
  599. /**
  600. * Removes number of abyssballs from player
  601. * @param sd: Player data
  602. * @param count: Amount to remove
  603. */
  604. void pc_delabyssball( struct map_session_data& sd, int count ){
  605. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  606. clif_abyssball( sd );
  607. }
  608. /**
  609. * Increases a player's fame points and displays a notice to him
  610. * @param sd Player
  611. * @param count Fame point
  612. * @return true: on success, false: on error
  613. */
  614. bool pc_addfame(map_session_data &sd, int count)
  615. {
  616. enum e_rank ranktype;
  617. sd.status.fame += count;
  618. if (sd.status.fame > MAX_FAME)
  619. sd.status.fame = MAX_FAME;
  620. switch(sd.class_&MAPID_UPPERMASK){
  621. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  622. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  623. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  624. default:
  625. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  626. return false;
  627. }
  628. clif_update_rankingpoint(sd, ranktype, count);
  629. chrif_updatefamelist(sd, ranktype);
  630. return true;
  631. }
  632. /**
  633. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  634. * @param sd
  635. * @param job Job use enum e_mapid
  636. * @return Rank
  637. */
  638. unsigned char pc_famerank(uint32 char_id, int job)
  639. {
  640. uint8 i;
  641. switch(job){
  642. case MAPID_BLACKSMITH: // Blacksmith
  643. for(i = 0; i < MAX_FAME_LIST; i++){
  644. if(smith_fame_list[i].id == char_id)
  645. return i + 1;
  646. }
  647. break;
  648. case MAPID_ALCHEMIST: // Alchemist
  649. for(i = 0; i < MAX_FAME_LIST; i++){
  650. if(chemist_fame_list[i].id == char_id)
  651. return i + 1;
  652. }
  653. break;
  654. case MAPID_TAEKWON: // Taekwon
  655. for(i = 0; i < MAX_FAME_LIST; i++){
  656. if(taekwon_fame_list[i].id == char_id)
  657. return i + 1;
  658. }
  659. break;
  660. }
  661. return 0;
  662. }
  663. /**
  664. * Restart player's HP & SP value
  665. * @param sd
  666. * @param type Restart type: 1 - Normal Resurection
  667. */
  668. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  669. struct status_data *status, *b_status;
  670. nullpo_retv(sd);
  671. b_status = &sd->base_status;
  672. status = &sd->battle_status;
  673. if (type&1) { //Normal resurrection
  674. status->hp = 1; //Otherwise status_heal may fail if dead.
  675. status_heal(&sd->bl, b_status->hp, 0, 1);
  676. if( status->sp < b_status->sp )
  677. status_set_sp(&sd->bl, b_status->sp, 1);
  678. } else { //Just for saving on the char-server (with values as if respawned)
  679. sd->status.hp = b_status->hp;
  680. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  681. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  682. }
  683. }
  684. /*==========================================
  685. Rental System
  686. *------------------------------------------*/
  687. /**
  688. * Ends a rental and removes the item/effect
  689. * @param tid: Tick ID
  690. * @param tick: Timer
  691. * @param id: Timer ID
  692. * @param data: Data
  693. * @return false - failure, true - success
  694. */
  695. TIMER_FUNC(pc_inventory_rental_end){
  696. struct map_session_data *sd = map_id2sd(id);
  697. if( sd == NULL )
  698. return 0;
  699. if( tid != sd->rental_timer ) {
  700. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  701. return 0;
  702. }
  703. pc_inventory_rentals(sd);
  704. return 1;
  705. }
  706. /**
  707. * Removes the rental timer from the player
  708. * @param sd: Player data
  709. */
  710. void pc_inventory_rental_clear(struct map_session_data *sd)
  711. {
  712. if( sd->rental_timer != INVALID_TIMER ) {
  713. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  714. sd->rental_timer = INVALID_TIMER;
  715. }
  716. }
  717. /**
  718. * Check for items in the player's inventory that are rental type
  719. * @param sd: Player data
  720. */
  721. void pc_inventory_rentals(struct map_session_data *sd)
  722. {
  723. int i, c = 0;
  724. unsigned int next_tick = UINT_MAX;
  725. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  726. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  727. continue; // Nothing here
  728. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  729. continue;
  730. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  731. if (sd->inventory_data[i]->unequip_script)
  732. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  733. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  734. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  735. } else {
  736. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  737. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  738. next_tick = umin(expire_tick * 1000U, next_tick);
  739. c++;
  740. }
  741. }
  742. 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
  743. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  744. else
  745. sd->rental_timer = INVALID_TIMER;
  746. }
  747. /**
  748. * Add a rental item to the player and adjusts the rental timer appropriately
  749. * @param sd: Player data
  750. * @param seconds: Rental time
  751. */
  752. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  753. {
  754. t_tick tick = seconds * 1000;
  755. if( sd == NULL )
  756. return;
  757. if( sd->rental_timer != INVALID_TIMER ) {
  758. const struct TimerData * td;
  759. td = get_timer(sd->rental_timer);
  760. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  761. pc_inventory_rental_clear(sd);
  762. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  763. }
  764. } else
  765. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  766. }
  767. /**
  768. * Check if the player can sell the current item
  769. * @param sd: map_session_data of the player
  770. * @param item: struct of the checking item
  771. * @param shoptype: NPC's sub type see enum npc_subtype
  772. * @return bool 'true' is sellable, 'false' otherwise
  773. */
  774. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  775. if (sd == NULL || item == NULL)
  776. return false;
  777. if (!pc_can_give_items(sd))
  778. return false;
  779. if (item->equip > 0 || item->amount < 0)
  780. return false;
  781. if (battle_config.hide_fav_sell && item->favorite)
  782. return false; //Cannot sell favs (optional config)
  783. if (!battle_config.rental_transaction && item->expire_time)
  784. return false; // Cannot Sell Rental Items
  785. if( item->equipSwitch ){
  786. return false;
  787. }
  788. if (itemdb_ishatched_egg(item))
  789. return false;
  790. switch (shoptype) {
  791. case NPCTYPE_SHOP:
  792. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  793. item->bound != BOUND_GUILD ||
  794. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  795. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  796. ))
  797. return true;
  798. break;
  799. case NPCTYPE_ITEMSHOP:
  800. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  801. item->bound != BOUND_GUILD ||
  802. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  803. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  804. ))
  805. return true;
  806. else if (!item->bound) {
  807. struct item_data *itd = itemdb_search(item->nameid);
  808. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  809. return true;
  810. }
  811. break;
  812. }
  813. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  814. return false;
  815. if (item->bound && !pc_can_give_bounded_items(sd))
  816. return false; // Don't allow sale of bound items
  817. return true;
  818. }
  819. /**
  820. * Determines if player can give / drop / trade / vend items
  821. */
  822. bool pc_can_give_items(struct map_session_data *sd)
  823. {
  824. return pc_has_permission(sd, PC_PERM_TRADE);
  825. }
  826. /**
  827. * Determines if player can give / drop / trade / vend bounded items
  828. */
  829. bool pc_can_give_bounded_items(struct map_session_data *sd)
  830. {
  831. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  832. }
  833. /**
  834. * Determine if an item in a player's inventory is tradeable based on several merits.
  835. * Checks for item_trade, bound, and rental restrictions.
  836. * @param sd: Player data
  837. * @param index: Item inventory index
  838. * @return True if the item can be traded or false otherwise
  839. */
  840. bool pc_can_trade_item(map_session_data *sd, int index) {
  841. if (sd && index >= 0) {
  842. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  843. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  844. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  845. );
  846. }
  847. return false;
  848. }
  849. /*==========================================
  850. * Prepares character for saving.
  851. * @param sd
  852. *------------------------------------------*/
  853. void pc_makesavestatus(struct map_session_data *sd) {
  854. nullpo_retv(sd);
  855. if(!battle_config.save_clothcolor)
  856. sd->status.clothes_color = 0;
  857. if(!battle_config.save_body_style)
  858. sd->status.body = 0;
  859. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  860. //status change load/saving. [Skotlex]
  861. #ifdef NEW_CARTS
  862. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  863. #else
  864. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  865. #endif
  866. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  867. if(pc_isdead(sd)){
  868. pc_setrestartvalue(sd, 0);
  869. } else {
  870. sd->status.hp = sd->battle_status.hp;
  871. sd->status.sp = sd->battle_status.sp;
  872. sd->status.ap = sd->battle_status.ap;
  873. }
  874. sd->status.last_point.map = sd->mapindex;
  875. sd->status.last_point.x = sd->bl.x;
  876. sd->status.last_point.y = sd->bl.y;
  877. return;
  878. }
  879. if(pc_isdead(sd)) {
  880. pc_setrestartvalue(sd, 0);
  881. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  882. } else {
  883. sd->status.hp = sd->battle_status.hp;
  884. sd->status.sp = sd->battle_status.sp;
  885. sd->status.ap = sd->battle_status.ap;
  886. sd->status.last_point.map = sd->mapindex;
  887. sd->status.last_point.x = sd->bl.x;
  888. sd->status.last_point.y = sd->bl.y;
  889. }
  890. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  891. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  892. if(mapdata->save.map)
  893. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  894. else
  895. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  896. }
  897. }
  898. /*==========================================
  899. * Off init ? Connection?
  900. *------------------------------------------*/
  901. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  902. nullpo_retv(sd);
  903. sd->bl.id = account_id;
  904. sd->status.account_id = account_id;
  905. sd->status.char_id = char_id;
  906. sd->status.sex = sex;
  907. sd->login_id1 = login_id1;
  908. sd->login_id2 = 0; // at this point, we can not know the value :(
  909. sd->client_tick = client_tick;
  910. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  911. sd->bl.type = BL_PC;
  912. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  913. sd->canlog_tick = gettick();
  914. //Required to prevent homunculus copuing a base speed of 0.
  915. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  916. }
  917. /**
  918. * Get equip point for an equip
  919. * @param sd
  920. * @param id
  921. */
  922. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  923. int ep = 0;
  924. nullpo_ret(sd);
  925. nullpo_ret(id);
  926. if (!itemdb_isequip2(id))
  927. return 0; //Not equippable by players.
  928. ep = id->equip;
  929. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  930. if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
  931. if (ep == EQP_WEAPON)
  932. return EQP_ARMS;
  933. if (ep == EQP_SHADOW_WEAPON)
  934. return EQP_SHADOW_ARMS;
  935. }
  936. }
  937. return ep;
  938. }
  939. /**
  940. * Get equip point for an equip
  941. * @param sd
  942. * @param n Equip index in inventory
  943. */
  944. int pc_equippoint(struct map_session_data *sd,int n){
  945. nullpo_ret(sd);
  946. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  947. }
  948. /**
  949. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  950. * @param sd : player session
  951. * @return 0 sucess, 1:invalid sd
  952. */
  953. void pc_setinventorydata(struct map_session_data *sd)
  954. {
  955. uint8 i;
  956. nullpo_retv(sd);
  957. for(i = 0; i < MAX_INVENTORY; i++) {
  958. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  959. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  960. }
  961. }
  962. /**
  963. * 'Calculates' weapon type
  964. * @param sd : Player
  965. */
  966. void pc_calcweapontype(struct map_session_data *sd)
  967. {
  968. nullpo_retv(sd);
  969. // single-hand
  970. if(sd->weapontype2 == W_FIST) {
  971. sd->status.weapon = sd->weapontype1;
  972. return;
  973. }
  974. if(sd->weapontype1 == W_FIST) {
  975. sd->status.weapon = sd->weapontype2;
  976. return;
  977. }
  978. // dual-wield
  979. sd->status.weapon = W_FIST;
  980. switch (sd->weapontype1){
  981. case W_DAGGER:
  982. switch (sd->weapontype2) {
  983. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  984. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  985. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  986. }
  987. break;
  988. case W_1HSWORD:
  989. switch (sd->weapontype2) {
  990. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  991. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  992. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  993. }
  994. break;
  995. case W_1HAXE:
  996. switch (sd->weapontype2) {
  997. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  998. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  999. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1000. }
  1001. }
  1002. // unknown, default to right hand type
  1003. if (!sd->status.weapon)
  1004. sd->status.weapon = sd->weapontype1;
  1005. }
  1006. /**
  1007. * Set equip index
  1008. * @param sd : Player
  1009. */
  1010. void pc_setequipindex(struct map_session_data *sd)
  1011. {
  1012. uint16 i;
  1013. nullpo_retv(sd);
  1014. for (i = 0; i < EQI_MAX; i++){
  1015. sd->equip_index[i] = -1;
  1016. sd->equip_switch_index[i] = -1;
  1017. }
  1018. for (i = 0; i < MAX_INVENTORY; i++) {
  1019. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1020. continue;
  1021. if (sd->inventory.u.items_inventory[i].equip) {
  1022. uint8 j;
  1023. for (j = 0; j < EQI_MAX; j++)
  1024. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1025. sd->equip_index[j] = i;
  1026. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1027. if (sd->inventory_data[i])
  1028. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1029. else
  1030. sd->weapontype1 = W_FIST;
  1031. }
  1032. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1033. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1034. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1035. else
  1036. sd->weapontype2 = W_FIST;
  1037. }
  1038. }
  1039. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1040. for (uint8 j = 0; j < EQI_MAX; j++) {
  1041. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1042. sd->equip_switch_index[j] = i;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. pc_calcweapontype(sd);
  1048. }
  1049. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  1050. //{
  1051. // int i;
  1052. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1053. // struct item_data *data;
  1054. //
  1055. // //Crafted/made/hatched items.
  1056. // if (itemdb_isspecial(item->card[0]))
  1057. // return 1;
  1058. //
  1059. // /* scan for enchant armor gems */
  1060. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1061. // s = MAX_SLOTS - 1;
  1062. //
  1063. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  1064. // return( i < s ) ? 0 : 1;
  1065. //}
  1066. /**
  1067. * Check if an item is equiped by player
  1068. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1069. * @param sd : player session
  1070. * @param nameid : itemid
  1071. * @return 1:yes, 0:no
  1072. */
  1073. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  1074. {
  1075. uint8 i;
  1076. for( i = 0; i < EQI_MAX; i++ )
  1077. {
  1078. short index = sd->equip_index[i], j;
  1079. if( index < 0 )
  1080. continue;
  1081. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1082. continue;
  1083. if( !sd->inventory_data[index] )
  1084. continue;
  1085. if( sd->inventory_data[index]->nameid == nameid )
  1086. return true;
  1087. for( j = 0; j < MAX_SLOTS; j++ ){
  1088. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1089. return true;
  1090. }
  1091. }
  1092. return false;
  1093. }
  1094. /**
  1095. * Check adoption rules
  1096. * @param p1_sd: Player 1
  1097. * @param p2_sd: Player 2
  1098. * @param b_sd: Player that will be adopted
  1099. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1100. * ADOPT_ALREADY_ADOPTED - Already adopted
  1101. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1102. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1103. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1104. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1105. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1106. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1107. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1108. */
  1109. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1110. {
  1111. if( !p1_sd || !p2_sd || !b_sd )
  1112. return ADOPT_CHARACTER_NOT_FOUND;
  1113. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1114. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1115. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1116. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1117. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1118. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1119. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1120. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1121. // Parents need to have their ring equipped
  1122. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1123. return ADOPT_EQUIP_RINGS;
  1124. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1125. return ADOPT_EQUIP_RINGS;
  1126. // Already adopted a baby
  1127. if( p1_sd->status.child || p2_sd->status.child ) {
  1128. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1129. return ADOPT_MORE_CHILDREN;
  1130. }
  1131. // Parents need at least lvl 70 to adopt
  1132. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1133. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1134. return ADOPT_LEVEL_70;
  1135. }
  1136. if( b_sd->status.partner_id ) {
  1137. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1138. return ADOPT_MARRIED;
  1139. }
  1140. 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 ) )
  1141. return ADOPT_NOT_NOVICE;
  1142. return ADOPT_ALLOWED;
  1143. }
  1144. /*==========================================
  1145. * Adoption Process
  1146. *------------------------------------------*/
  1147. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1148. {
  1149. int job, joblevel;
  1150. t_exp jobexp;
  1151. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1152. return false;
  1153. // Preserve current job levels and progress
  1154. joblevel = b_sd->status.job_level;
  1155. jobexp = b_sd->status.job_exp;
  1156. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1157. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1158. { // Success, proceed to configure parents and baby skills
  1159. p1_sd->status.child = b_sd->status.char_id;
  1160. p2_sd->status.child = b_sd->status.char_id;
  1161. b_sd->status.father = p1_sd->status.char_id;
  1162. b_sd->status.mother = p2_sd->status.char_id;
  1163. // Restore progress
  1164. b_sd->status.job_level = joblevel;
  1165. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1166. b_sd->status.job_exp = jobexp;
  1167. clif_updatestatus(b_sd, SP_JOBEXP);
  1168. // Baby Skills
  1169. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1170. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1171. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1172. // Parents Skills
  1173. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1174. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1175. chrif_save(p1_sd, CSAVE_NORMAL);
  1176. chrif_save(p2_sd, CSAVE_NORMAL);
  1177. chrif_save(b_sd, CSAVE_NORMAL);
  1178. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1179. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1180. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1181. return true;
  1182. }
  1183. return false; // Job Change Fail
  1184. }
  1185. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1186. nullpo_retr( false, sd );
  1187. nullpo_retr( false, item );
  1188. // Calculate the required bit to check
  1189. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1190. size_t index;
  1191. // 2-1
  1192. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1193. index = 1;
  1194. // 2-2
  1195. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1196. index = 2;
  1197. // Basejob
  1198. }else{
  1199. index = 0;
  1200. }
  1201. return ( item->class_base[index] & job ) != 0;
  1202. }
  1203. /*==========================================
  1204. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1205. Returns:
  1206. false : Cannot use/equip
  1207. true : Can use/equip
  1208. * Credits:
  1209. [Inkfish] for first idea
  1210. [Haru] for third-classes extension
  1211. [Cydh] finishing :D
  1212. *------------------------------------------*/
  1213. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1214. while (1) {
  1215. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_BABY|JOBL_THIRD|JOBL_FOURTH))) //normal classes (no upper, no baby, no third, no fourth)
  1216. break;
  1217. #ifndef RENEWAL
  1218. //allow third classes to use trans. class items
  1219. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1220. break;
  1221. //third-baby classes can use same item too
  1222. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1223. break;
  1224. //don't need to decide specific rules for third-classes?
  1225. //items for third classes can be used for all third classes
  1226. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1227. break;
  1228. #else
  1229. //trans. classes (exl. third-trans.)
  1230. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1231. break;
  1232. //baby classes (exl. third-baby)
  1233. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1234. break;
  1235. //third classes (exl. third-trans. and baby-third and fourth)
  1236. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1237. break;
  1238. //trans-third classes (exl. fourth)
  1239. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1240. break;
  1241. //third-baby classes (exl. fourth)
  1242. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1243. break;
  1244. //fourth classes
  1245. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1246. break;
  1247. #endif
  1248. return false;
  1249. }
  1250. return true;
  1251. }
  1252. /*=================================================
  1253. * Checks if the player can equip the item at index n in inventory.
  1254. * @param sd
  1255. * @param n Item index in inventory
  1256. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1257. *------------------------------------------------*/
  1258. uint8 pc_isequip(struct map_session_data *sd,int n)
  1259. {
  1260. struct item_data *item;
  1261. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1262. item = sd->inventory_data[n];
  1263. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1264. return ITEM_EQUIP_ACK_OK;
  1265. if(item == NULL)
  1266. return ITEM_EQUIP_ACK_FAIL;
  1267. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1268. return ITEM_EQUIP_ACK_FAILLEVEL;
  1269. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1270. return ITEM_EQUIP_ACK_FAILLEVEL;
  1271. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1272. return ITEM_EQUIP_ACK_FAIL;
  1273. //fail to equip if item is restricted
  1274. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1275. return ITEM_EQUIP_ACK_FAIL;
  1276. if (item->equip&EQP_AMMO) {
  1277. switch (item->subtype) {
  1278. case AMMO_ARROW:
  1279. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1280. clif_msg(sd, ITEM_NEED_BOW);
  1281. return ITEM_EQUIP_ACK_FAIL;
  1282. }
  1283. break;
  1284. case AMMO_DAGGER:
  1285. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1286. return ITEM_EQUIP_ACK_FAIL;
  1287. break;
  1288. case AMMO_BULLET:
  1289. case AMMO_SHELL:
  1290. if (battle_config.ammo_check_weapon && sd->status.weapon != W_REVOLVER && sd->status.weapon != W_RIFLE && sd->status.weapon != W_GATLING && sd->status.weapon != W_SHOTGUN
  1291. #ifdef RENEWAL
  1292. && sd->status.weapon != W_GRENADE
  1293. #endif
  1294. ) {
  1295. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1296. return ITEM_EQUIP_ACK_FAIL;
  1297. }
  1298. break;
  1299. #ifndef RENEWAL
  1300. case AMMO_GRENADE:
  1301. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1302. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1303. return ITEM_EQUIP_ACK_FAIL;
  1304. }
  1305. break;
  1306. #endif
  1307. case AMMO_CANNONBALL:
  1308. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1309. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1310. return ITEM_EQUIP_ACK_FAIL;
  1311. }
  1312. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1313. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1314. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1315. return ITEM_EQUIP_ACK_FAIL;
  1316. }
  1317. break;
  1318. }
  1319. }
  1320. if (sd->sc.count) {
  1321. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1322. return ITEM_EQUIP_ACK_FAIL;
  1323. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1324. return ITEM_EQUIP_ACK_FAIL;
  1325. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1326. return ITEM_EQUIP_ACK_FAIL;
  1327. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1328. return ITEM_EQUIP_ACK_FAIL;
  1329. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1330. return ITEM_EQUIP_ACK_FAIL;
  1331. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1332. return ITEM_EQUIP_ACK_FAIL;
  1333. if (item->equip & EQP_SHADOW_GEAR && sd->sc.data[SC_SHADOW_STRIP])
  1334. return ITEM_EQUIP_ACK_FAIL;
  1335. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1336. return ITEM_EQUIP_ACK_FAIL;
  1337. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1338. //Spirit of Super Novice equip bonuses. [Skotlex]
  1339. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1340. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1341. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1342. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1343. case W_DAGGER: //All level 4 - Daggers
  1344. case W_1HSWORD: //All level 4 - 1H Swords
  1345. case W_1HAXE: //All level 4 - 1H Axes
  1346. case W_MACE: //All level 4 - 1H Maces
  1347. case W_STAFF: //All level 4 - 1H Staves
  1348. case W_2HSTAFF: //All level 4 - 2H Staves
  1349. return ITEM_EQUIP_ACK_OK;
  1350. }
  1351. }
  1352. }
  1353. //Not equipable by class. [Skotlex]
  1354. if (!pc_job_can_use_item(sd,item))
  1355. return ITEM_EQUIP_ACK_FAIL;
  1356. if (!pc_isItemClass(sd, item))
  1357. return ITEM_EQUIP_ACK_FAIL;
  1358. return ITEM_EQUIP_ACK_OK;
  1359. }
  1360. /*==========================================
  1361. * No problem with the session id
  1362. * set the status that has been sent from char server
  1363. *------------------------------------------*/
  1364. 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)
  1365. {
  1366. t_tick tick = gettick();
  1367. uint32 ip = session[sd->fd]->client_addr;
  1368. sd->login_id2 = login_id2;
  1369. sd->group_id = group_id;
  1370. /* load user permissions */
  1371. pc_group_pc_load(sd);
  1372. memcpy(&sd->status, st, sizeof(*st));
  1373. if (st->sex != sd->status.sex) {
  1374. clif_authfail_fd(sd->fd, 0);
  1375. return false;
  1376. }
  1377. //Set the map-server used job id. [Skotlex]
  1378. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1379. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1380. 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);
  1381. sd->status.class_ = JOB_NOVICE;
  1382. sd->class_ = MAPID_NOVICE;
  1383. } else
  1384. sd->class_ = class_;
  1385. // Checks and fixes to character status data, that are required
  1386. // in case of configuration change or stuff, which cannot be
  1387. // checked on char-server.
  1388. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1389. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1390. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1391. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1392. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1393. sd->state.connect_new = 1;
  1394. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1395. sd->invincible_timer = INVALID_TIMER;
  1396. sd->npc_timer_id = INVALID_TIMER;
  1397. sd->pvp_timer = INVALID_TIMER;
  1398. sd->expiration_tid = INVALID_TIMER;
  1399. sd->autotrade_tid = INVALID_TIMER;
  1400. sd->respawn_tid = INVALID_TIMER;
  1401. sd->tid_queue_active = INVALID_TIMER;
  1402. sd->skill_keep_using.tid = INVALID_TIMER;
  1403. sd->skill_keep_using.skill_id = 0;
  1404. sd->skill_keep_using.level = 0;
  1405. sd->skill_keep_using.target = 0;
  1406. #ifdef SECURE_NPCTIMEOUT
  1407. // Initialize to defaults/expected
  1408. sd->npc_idle_timer = INVALID_TIMER;
  1409. sd->npc_idle_tick = tick;
  1410. sd->npc_idle_type = NPCT_INPUT;
  1411. sd->state.ignoretimeout = false;
  1412. #endif
  1413. sd->canuseitem_tick = tick;
  1414. sd->canusecashfood_tick = tick;
  1415. sd->canequip_tick = tick;
  1416. sd->cantalk_tick = tick;
  1417. sd->canskill_tick = tick;
  1418. sd->cansendmail_tick = tick;
  1419. sd->idletime = last_tick;
  1420. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1421. sd->spirit_timer[i] = INVALID_TIMER;
  1422. if (battle_config.item_auto_get)
  1423. sd->state.autoloot = 10000;
  1424. if (battle_config.disp_experience)
  1425. sd->state.showexp = 1;
  1426. if (battle_config.disp_zeny)
  1427. sd->state.showzeny = 1;
  1428. #ifdef VIP_ENABLE
  1429. if (!battle_config.vip_disp_rate)
  1430. sd->vip.disableshowrate = 1;
  1431. #endif
  1432. if (!(battle_config.display_skill_fail&2))
  1433. sd->state.showdelay = 1;
  1434. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1435. memset(&sd->cart, 0, sizeof(struct s_storage));
  1436. memset(&sd->storage, 0, sizeof(struct s_storage));
  1437. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1438. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1439. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1440. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1441. sd->status.option &= ~OPTION_INVISIBLE;
  1442. }
  1443. status_change_init(&sd->bl);
  1444. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1445. unit_dataset(&sd->bl);
  1446. sd->guild_x = -1;
  1447. sd->guild_y = -1;
  1448. sd->delayed_damage = 0;
  1449. // Event Timers
  1450. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1451. sd->eventtimer[i] = INVALID_TIMER;
  1452. // Rental Timer
  1453. sd->rental_timer = INVALID_TIMER;
  1454. for( int i = 0; i < 3; i++ )
  1455. sd->hate_mob[i] = -1;
  1456. sd->quest_log = NULL;
  1457. sd->num_quests = 0;
  1458. sd->avail_quests = 0;
  1459. sd->save_quest = false;
  1460. sd->count_rewarp = 0;
  1461. sd->mail.pending_weight = 0;
  1462. sd->mail.pending_zeny = 0;
  1463. sd->mail.pending_slots = 0;
  1464. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1465. sd->regs.arrays = NULL;
  1466. sd->vars_dirty = false;
  1467. sd->vars_ok = false;
  1468. sd->vars_received = 0x0;
  1469. //warp player
  1470. enum e_setpos setpos_result = pc_setpos( sd, sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1471. if( setpos_result != SETPOS_OK ){
  1472. 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, setpos_result );
  1473. // try warping to a default map instead (church graveyard)
  1474. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1475. // if we fail again
  1476. clif_authfail_fd(sd->fd, 0);
  1477. return false;
  1478. }
  1479. }
  1480. clif_inventory_expansion_info( sd );
  1481. clif_authok(sd);
  1482. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1483. sd->die_counter=-1;
  1484. //display login notice
  1485. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1486. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1487. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1488. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1489. sd->status.name, sd->status.account_id, sd->status.char_id,
  1490. CONVIP(ip), sd->group_id);
  1491. // Send friends list
  1492. clif_friendslist_send(sd);
  1493. if( !changing_mapservers ) {
  1494. if (battle_config.display_version == 1)
  1495. pc_show_version(sd);
  1496. // Message of the Day [Valaris]
  1497. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1498. if (battle_config.motd_type)
  1499. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1500. else
  1501. clif_displaymessage(sd->fd, motd_text[i]);
  1502. }
  1503. if (expiration_time != 0)
  1504. sd->expiration_time = expiration_time;
  1505. /**
  1506. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1507. **/
  1508. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1509. }
  1510. pc_validate_skill(sd);
  1511. /* [Ind] */
  1512. sd->sc_display = NULL;
  1513. sd->sc_display_count = 0;
  1514. // Player has not yet received the CashShop list
  1515. sd->status.cashshop_sent = false;
  1516. sd->last_addeditem_index = -1;
  1517. sd->bonus_script.head = NULL;
  1518. sd->bonus_script.count = 0;
  1519. // Initialize BG queue
  1520. sd->bg_queue_id = 0;
  1521. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1522. sd->hatEffects = {};
  1523. #endif
  1524. sd->catch_target_class = PET_CATCH_FAIL;
  1525. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1526. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1527. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1528. clif_updatestatus(sd, SP_BASEEXP);
  1529. }
  1530. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1531. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1532. clif_updatestatus(sd, SP_JOBEXP);
  1533. }
  1534. // Request all registries (auth is considered completed whence they arrive)
  1535. intif_request_registry(sd,7);
  1536. return true;
  1537. }
  1538. /*==========================================
  1539. * Closes a connection because it failed to be authenticated from the char server.
  1540. *------------------------------------------*/
  1541. void pc_authfail(struct map_session_data *sd)
  1542. {
  1543. clif_authfail_fd(sd->fd, 0);
  1544. return;
  1545. }
  1546. /**
  1547. * Player register a bl as hatred
  1548. * @param sd : player session
  1549. * @param pos : hate position [0;2]
  1550. * @param bl : target bl
  1551. * @return false:failed, true:success
  1552. */
  1553. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1554. {
  1555. int class_;
  1556. if (!sd || !bl || pos < 0 || pos > 2)
  1557. return false;
  1558. if (sd->hate_mob[pos] != -1)
  1559. { //Can't change hate targets.
  1560. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1561. return false;
  1562. }
  1563. class_ = status_get_class(bl);
  1564. if (!pcdb_checkid(class_)) {
  1565. unsigned int max_hp = status_get_max_hp(bl);
  1566. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1567. return false;
  1568. if (pos != status_get_size(bl))
  1569. return false; //Wrong size
  1570. }
  1571. sd->hate_mob[pos] = class_;
  1572. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1573. clif_hate_info(sd, pos, class_, 1);
  1574. return true;
  1575. }
  1576. /*==========================================
  1577. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1578. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1579. * See intif_parse_StorageReceived() for item operations [lighta]
  1580. *------------------------------------------*/
  1581. void pc_reg_received(struct map_session_data *sd)
  1582. {
  1583. uint8 i;
  1584. sd->vars_ok = true;
  1585. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1586. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1587. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1588. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1589. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1590. if (msg_checklangtype(sd->langtype,true) < 0)
  1591. sd->langtype = 0; //invalid langtype reset to default
  1592. // Cash shop
  1593. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1594. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1595. // Cooking Exp
  1596. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1597. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1598. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1599. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1600. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1601. }
  1602. if (battle_config.feature_banking)
  1603. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1604. if (battle_config.feature_roulette) {
  1605. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1606. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1607. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1608. }
  1609. sd->roulette.prizeIdx = -1;
  1610. //SG map and mob read [Komurka]
  1611. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1612. uint16 j;
  1613. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1614. sd->feel_map[i].index = j;
  1615. sd->feel_map[i].m = map_mapindex2mapid(j);
  1616. } else {
  1617. sd->feel_map[i].index = 0;
  1618. sd->feel_map[i].m = -1;
  1619. }
  1620. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1621. }
  1622. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1623. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1624. sd->cloneskill_idx = skill_get_index(skid);
  1625. if (sd->cloneskill_idx > 0) {
  1626. sd->status.skill[sd->cloneskill_idx].id = skid;
  1627. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1628. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1629. sd->status.skill[sd->cloneskill_idx].lv = i;
  1630. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1631. }
  1632. }
  1633. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1634. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1635. sd->reproduceskill_idx = skill_get_index(skid);
  1636. if (sd->reproduceskill_idx > 0) {
  1637. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1638. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1639. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1640. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1641. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1642. }
  1643. }
  1644. //Weird... maybe registries were reloaded?
  1645. if (sd->state.active)
  1646. return;
  1647. sd->state.active = 1;
  1648. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1649. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1650. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1651. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1652. // Restore IM_CHAR instance to the player
  1653. for (const auto &instance : instances) {
  1654. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1655. sd->instance_id = instance.first;
  1656. break;
  1657. }
  1658. }
  1659. #if PACKETVER_MAIN_NUM < 20190403 || PACKETVER_RE_NUM < 20190320 || PACKETVER_ZERO_NUM < 20190410
  1660. if (sd->instance_id > 0)
  1661. instance_reqinfo(sd, sd->instance_id);
  1662. if (sd->status.party_id > 0)
  1663. party_member_joined(sd);
  1664. if (sd->status.guild_id > 0)
  1665. guild_member_joined(sd);
  1666. if (sd->status.clan_id > 0)
  1667. clan_member_joined(sd);
  1668. #endif
  1669. // pet
  1670. if (sd->status.pet_id > 0)
  1671. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1672. // Homunculus [albator]
  1673. if( sd->status.hom_id > 0 )
  1674. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1675. if( sd->status.mer_id > 0 )
  1676. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1677. if( sd->status.ele_id > 0 )
  1678. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1679. map_addiddb(&sd->bl);
  1680. map_delnickdb(sd->status.char_id, sd->status.name);
  1681. if (!chrif_auth_finished(sd))
  1682. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1683. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1684. chrif_bsdata_request(sd->status.char_id);
  1685. #ifdef VIP_ENABLE
  1686. sd->vip.time = 0;
  1687. sd->vip.enabled = 0;
  1688. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1689. #endif
  1690. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1691. intif_request_questlog(sd);
  1692. if (battle_config.feature_achievement) {
  1693. sd->achievement_data.total_score = 0;
  1694. sd->achievement_data.level = 0;
  1695. sd->achievement_data.save = false;
  1696. sd->achievement_data.count = 0;
  1697. sd->achievement_data.incompleteCount = 0;
  1698. sd->achievement_data.achievements = NULL;
  1699. intif_request_achievements(sd->status.char_id);
  1700. }
  1701. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1702. sd->state.connect_new = 1;
  1703. clif_parse_LoadEndAck(sd->fd, sd);
  1704. if( pc_isinvisible( sd ) ){
  1705. sd->vd.class_ = JT_INVISIBLE;
  1706. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1707. // decrement the number of pvp players on the map
  1708. map_getmapdata( sd->bl.m )->users_pvp--;
  1709. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1710. // unregister the player for ranking
  1711. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1712. sd->pvp_timer = INVALID_TIMER;
  1713. }
  1714. clif_changeoption( &sd->bl );
  1715. }
  1716. }
  1717. channel_autojoin(sd);
  1718. }
  1719. static int pc_calc_skillpoint(struct map_session_data* sd)
  1720. {
  1721. uint16 i, skill_point = 0;
  1722. nullpo_ret(sd);
  1723. for(i = 1; i < MAX_SKILL; i++) {
  1724. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1725. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1726. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1727. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1728. )
  1729. {
  1730. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1731. skill_point += sd->status.skill[i].lv;
  1732. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1733. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1734. }
  1735. }
  1736. }
  1737. return skill_point;
  1738. }
  1739. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1740. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1741. return false;
  1742. /**
  1743. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1744. * skill tree and since they have no icons they'll give resource errors
  1745. * Get ALL skills except npc/guild ones. [Skotlex]
  1746. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1747. **/
  1748. for (const auto &skill : skill_db) {
  1749. uint16 skill_id = skill.second->nameid;
  1750. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1751. continue;
  1752. switch (skill_id) {
  1753. case SM_SELFPROVOKE:
  1754. case AB_DUPLELIGHT_MELEE:
  1755. case AB_DUPLELIGHT_MAGIC:
  1756. case WL_CHAINLIGHTNING_ATK:
  1757. case WL_TETRAVORTEX_FIRE:
  1758. case WL_TETRAVORTEX_WATER:
  1759. case WL_TETRAVORTEX_WIND:
  1760. case WL_TETRAVORTEX_GROUND:
  1761. case WL_SUMMON_ATK_FIRE:
  1762. case WL_SUMMON_ATK_WIND:
  1763. case WL_SUMMON_ATK_WATER:
  1764. case WL_SUMMON_ATK_GROUND:
  1765. case LG_OVERBRAND_BRANDISH:
  1766. case LG_OVERBRAND_PLUSATK:
  1767. case WM_SEVERE_RAINSTORM_MELEE:
  1768. case RL_R_TRIP_PLUSATK:
  1769. case SG_DEVIL:
  1770. case MO_TRIPLEATTACK:
  1771. case RG_SNATCHER:
  1772. continue;
  1773. default:
  1774. {
  1775. uint8 lv = (uint8)skill_get_max(skill_id);
  1776. if (lv > 0) {
  1777. uint16 idx = skill_get_index(skill_id);
  1778. sd->status.skill[idx].id = skill_id;
  1779. if (addlv)
  1780. sd->status.skill[idx].lv = lv;
  1781. }
  1782. }
  1783. break;
  1784. }
  1785. }
  1786. return true;
  1787. }
  1788. /*==========================================
  1789. * Calculation of skill level.
  1790. * @param sd
  1791. *------------------------------------------*/
  1792. void pc_calc_skilltree(struct map_session_data *sd)
  1793. {
  1794. nullpo_retv(sd);
  1795. uint64 job = pc_calc_skilltree_normalize_job(sd);
  1796. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1797. if( class_ == -1 )
  1798. { //Unable to normalize job??
  1799. ShowError( "pc_calc_skilltree: Unable to normalize job %s(%d) for character %s (%d:%d)\n", job_name( sd->status.class_ ), sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id );
  1800. return;
  1801. }
  1802. uint16 job_id = class_;
  1803. class_ = pc_class2idx(class_);
  1804. for (const auto &skill : skill_db) {
  1805. uint16 skill_id = skill.second->nameid;
  1806. uint16 idx = skill_get_index(skill_id);
  1807. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1808. sd->status.skill[idx].id = 0; //First clear skills.
  1809. /* permanent skills that must be re-checked */
  1810. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1811. if (skill_id == 0) {
  1812. sd->status.skill[idx].id = 0;
  1813. sd->status.skill[idx].lv = 0;
  1814. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1815. continue;
  1816. }
  1817. switch (skill_id) {
  1818. case NV_TRICKDEAD:
  1819. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1820. sd->status.skill[idx].id = 0;
  1821. sd->status.skill[idx].lv = 0;
  1822. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1823. }
  1824. break;
  1825. }
  1826. }
  1827. }
  1828. for (const auto &skill : skill_db) {
  1829. uint16 idx = skill_get_index(skill.second->nameid);
  1830. // Restore original level of skills after deleting earned skills.
  1831. if( sd->status.skill[idx].flag != SKILL_FLAG_PERMANENT && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED ) {
  1832. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1833. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1834. }
  1835. }
  1836. // Removes Taekwon Ranker skill bonus
  1837. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1838. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  1839. if (tree != nullptr && !tree->skills.empty()) {
  1840. for (const auto &it : tree->skills) {
  1841. uint16 sk_idx = skill_get_index(it.first);
  1842. if (sk_idx == 0)
  1843. continue;
  1844. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1845. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  1846. continue;
  1847. sd->status.skill[sk_idx].id = 0;
  1848. }
  1849. }
  1850. }
  1851. }
  1852. // Grant all skills
  1853. pc_grant_allskills(sd, false);
  1854. int flag;
  1855. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1856. do {
  1857. flag = 0;
  1858. if (tree == nullptr || tree->skills.empty())
  1859. break;
  1860. for (const auto &skillsit : tree->skills) {
  1861. bool fail = false;
  1862. uint16 skid = skillsit.first;
  1863. uint16 sk_idx = skill_get_index(skid);
  1864. if (sd->status.skill[sk_idx].id)
  1865. continue; //Skill already known.
  1866. if (!battle_config.skillfree) {
  1867. // Checking required skills
  1868. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  1869. if (entry != nullptr && !entry->need.empty()) {
  1870. for (const auto &it : entry->need) {
  1871. uint16 sk_need_id = it.first;
  1872. uint16 sk_need_idx = skill_get_index(sk_need_id);
  1873. if (sk_need_idx > 0) {
  1874. uint16 sk_need = sk_need_id;
  1875. 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)
  1876. sk_need = 0; //Not learned.
  1877. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1878. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1879. else
  1880. sk_need = pc_checkskill(sd,sk_need_id);
  1881. if (sk_need < it.second) {
  1882. fail = true;
  1883. break;
  1884. }
  1885. }
  1886. }
  1887. }
  1888. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  1889. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1890. c_ = pc_class2idx(c_);
  1891. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  1892. fail = true; // base level requirement wasn't satisfied
  1893. }
  1894. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  1895. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1896. c_ = pc_class2idx(c_);
  1897. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  1898. fail = true; // job level requirement wasn't satisfied
  1899. }
  1900. }
  1901. if (!fail) {
  1902. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1903. if (!sd->status.skill[sk_idx].lv && (
  1904. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1905. skill->inf2[INF2_ISWEDDING] ||
  1906. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1907. ))
  1908. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1909. sd->status.skill[sk_idx].id = skid;
  1910. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1911. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1912. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1913. }
  1914. flag = 1; // skill list has changed, perform another pass
  1915. }
  1916. }
  1917. } while(flag);
  1918. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1919. uint16 skid = 0;
  1920. /* Taekwon Ranker Bonus Skill Tree
  1921. ============================================
  1922. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1923. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1924. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1925. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1926. if (tree != nullptr && !tree->skills.empty()) {
  1927. for (const auto &it : tree->skills) {
  1928. skid = it.first;
  1929. uint16 sk_idx = skill_get_index(skid);
  1930. if (sk_idx == 0)
  1931. continue;
  1932. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1933. continue; //Do not include Quest/Wedding skills.
  1934. if( sd->status.skill[sk_idx].id == 0 ) {
  1935. sd->status.skill[sk_idx].id = skid;
  1936. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1937. } else if( skid != NV_BASIC )
  1938. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1939. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1940. }
  1941. }
  1942. }
  1943. // Enable Bard/Dancer spirit linked skills.
  1944. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  1945. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  1946. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  1947. { BA_POEMBRAGI, DC_FORTUNEKISS },
  1948. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  1949. for (const auto &skill : linked_skills) {
  1950. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  1951. continue;
  1952. // Tag it as a non-savable, non-uppable, bonus skill
  1953. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  1954. }
  1955. }
  1956. }
  1957. //Checks if you can learn a new skill after having leveled up a skill.
  1958. static void pc_check_skilltree(struct map_session_data *sd)
  1959. {
  1960. int flag = 0;
  1961. if (battle_config.skillfree)
  1962. return; //Function serves no purpose if this is set
  1963. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  1964. if (c == -1) { //Unable to normalize job??
  1965. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  1966. return;
  1967. }
  1968. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  1969. if (tree == nullptr || tree->skills.empty())
  1970. return;
  1971. do {
  1972. flag = 0;
  1973. for (const auto &skillsit : tree->skills) {
  1974. uint16 skid = skillsit.first;
  1975. uint16 sk_idx = skill_get_index(skid);
  1976. bool fail = false;
  1977. if (sd->status.skill[sk_idx].id) //Already learned
  1978. continue;
  1979. // Checking required skills
  1980. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  1981. if (entry != nullptr && !entry->need.empty()) {
  1982. for (const auto &it : entry->need) {
  1983. uint16 sk_need_id = it.first;
  1984. uint16 sk_need_idx = skill_get_index(sk_need_id);
  1985. if (sk_need_id > 0) {
  1986. short sk_need = sk_need_id;
  1987. 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)
  1988. sk_need = 0; //Not learned.
  1989. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1990. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1991. else
  1992. sk_need = pc_checkskill(sd,sk_need_id);
  1993. if (sk_need < it.second) {
  1994. fail = true;
  1995. break;
  1996. }
  1997. }
  1998. }
  1999. }
  2000. if( fail )
  2001. continue;
  2002. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2003. continue;
  2004. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2005. if( !sd->status.skill[sk_idx].lv && (
  2006. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2007. skill->inf2[INF2_ISWEDDING] ||
  2008. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  2009. ) )
  2010. continue; //Cannot be learned via normal means.
  2011. sd->status.skill[sk_idx].id = skid;
  2012. flag = 1;
  2013. }
  2014. } while(flag);
  2015. }
  2016. // Make sure all the skills are in the correct condition
  2017. // before persisting to the backend.. [MouseJstr]
  2018. void pc_clean_skilltree(struct map_session_data *sd)
  2019. {
  2020. uint16 i;
  2021. for (i = 0; i < MAX_SKILL; i++){
  2022. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2023. sd->status.skill[i].id = 0;
  2024. sd->status.skill[i].lv = 0;
  2025. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2026. }
  2027. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2028. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2029. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2030. }
  2031. }
  2032. }
  2033. uint64 pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  2034. {
  2035. int skill_point, novice_skills;
  2036. uint64 c = sd->class_;
  2037. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2038. return c;
  2039. skill_point = pc_calc_skillpoint(sd);
  2040. // Novice's skill points for basic skill.
  2041. std::shared_ptr<s_job_info> novice_job = job_db.find(JOB_NOVICE);
  2042. novice_skills = novice_job->max_job_level - 1;
  2043. // 1st Class Job LV Check
  2044. if (sd->class_ & JOBL_2 && (sd->class_ & MAPID_UPPERMASK) != MAPID_SUPER_NOVICE && !sd->change_level_2nd) {
  2045. sd->change_level_2nd = job_db.find(pc_mapid2jobid(sd->class_ & MAPID_BASEMASK, sd->status.sex))->max_job_level;
  2046. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  2047. }
  2048. // 2nd Class Job LV Check
  2049. if (sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E && !sd->change_level_3rd) {
  2050. sd->change_level_3rd = job_db.find(pc_mapid2jobid(sd->class_ & MAPID_UPPERMASK, sd->status.sex))->max_job_level;
  2051. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  2052. }
  2053. // 3rd Class Job LV Check
  2054. if (sd->class_ & JOBL_FOURTH && !sd->change_level_4th) {
  2055. sd->change_level_4th = job_db.find(pc_mapid2jobid(sd->class_ & MAPID_THIRDMASK | JOBL_THIRD, sd->status.sex))->max_job_level;
  2056. pc_setglobalreg(sd, add_str(JOBCHANGE4TH_VAR), sd->change_level_4th);
  2057. }
  2058. // Check the skill tree the player has access to depending on the used number of skill points.
  2059. if (skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER) // Novice Skill Tree
  2060. c = MAPID_NOVICE;
  2061. else if (skill_point < novice_skills + (sd->change_level_2nd - 1) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) // 1st Job Skill Tree
  2062. c &= MAPID_BASEMASK;
  2063. else if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1) && (sd->class_&MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E) // 2nd Job Skill Tree
  2064. c &= MAPID_UPPERMASK;
  2065. else if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1) + (sd->change_level_4th - 1)) // 3rd Job Skill Tree
  2066. c &= MAPID_THIRDMASK;
  2067. // Special Masks
  2068. if (sd->class_&JOBL_UPPER)
  2069. c |= JOBL_UPPER;// Rebirth Job
  2070. if (sd->class_&JOBL_BABY)
  2071. c |= JOBL_BABY;// Baby Job
  2072. return c;
  2073. }
  2074. /*==========================================
  2075. * Updates the weight status
  2076. *------------------------------------------
  2077. * 1: overweight 50% for pre-renewal and 70% for renewal
  2078. * 2: overweight 90%
  2079. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2080. */
  2081. void pc_updateweightstatus(struct map_session_data *sd)
  2082. {
  2083. int old_overweight;
  2084. int new_overweight;
  2085. nullpo_retv(sd);
  2086. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  2087. #ifdef RENEWAL
  2088. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2089. #else
  2090. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2091. #endif
  2092. if( old_overweight == new_overweight )
  2093. return; // no change
  2094. // stop old status change
  2095. if( old_overweight == 1 )
  2096. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  2097. else if( old_overweight == 2 )
  2098. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  2099. // start new status change
  2100. if( new_overweight == 1 )
  2101. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2102. else if( new_overweight == 2 )
  2103. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2104. // update overweight status
  2105. sd->regen.state.overweight = new_overweight;
  2106. }
  2107. int pc_disguise(struct map_session_data *sd, int class_)
  2108. {
  2109. if (!class_ && !sd->disguise)
  2110. return 0;
  2111. if (class_ && sd->disguise == class_)
  2112. return 0;
  2113. if(pc_isinvisible(sd))
  2114. { //Character is invisible. Stealth class-change. [Skotlex]
  2115. sd->disguise = class_; //viewdata is set on uncloaking.
  2116. return 2;
  2117. }
  2118. if (sd->bl.prev != NULL) {
  2119. pc_stop_walking(sd, 0);
  2120. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2121. }
  2122. if (!class_) {
  2123. sd->disguise = 0;
  2124. class_ = sd->status.class_;
  2125. } else
  2126. sd->disguise=class_;
  2127. status_set_viewdata(&sd->bl, class_);
  2128. clif_changeoption(&sd->bl);
  2129. if (sd->bl.prev != NULL) {
  2130. clif_spawn(&sd->bl);
  2131. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2132. { //It seems the cart info is lost on undisguise.
  2133. clif_cartlist(sd);
  2134. clif_updatestatus(sd,SP_CARTINFO);
  2135. }
  2136. if (sd->chatID) {
  2137. struct chat_data* cd;
  2138. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2139. clif_dispchat(cd,0);
  2140. }
  2141. }
  2142. return 1;
  2143. }
  2144. /// Show error message
  2145. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2146. /// Check for valid Element, break & show error message if invalid Element
  2147. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2148. /// Check for valid Race, break & show error message if invalid Race
  2149. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2150. /// Check for valid Race2, break & show error message if invalid Race2
  2151. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2152. /// Check for valid Class, break & show error message if invalid Class
  2153. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2154. /// Check for valid Size, break & show error message if invalid Size
  2155. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2156. /// Check for valid SC, break & show error message if invalid SC
  2157. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2158. /**
  2159. * Add auto spell bonus for player while attacking/attacked
  2160. * @param spell: Spell array
  2161. * @param id: Skill to cast
  2162. * @param lv: Skill level
  2163. * @param rate: Success chance
  2164. * @param battle_flag: Battle flag
  2165. * @param card_id: Used to prevent card stacking
  2166. * @param flag: Flags used for extra arguments
  2167. * &0: forces the skill to be casted on self, rather than on the target
  2168. * &1: forces the skill to be casted on target, rather than self
  2169. * &2: random skill level in [1..lv] is chosen
  2170. */
  2171. static void pc_bonus_autospell(std::vector<s_autospell> &spell, uint16 id, uint16 lv, short rate, short battle_flag, t_itemid card_id, uint8 flag)
  2172. {
  2173. if (spell.size() == MAX_PC_BONUS) {
  2174. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2175. return;
  2176. }
  2177. if (!rate)
  2178. return;
  2179. if (!(battle_flag&BF_RANGEMASK))
  2180. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2181. if (!(battle_flag&BF_WEAPONMASK))
  2182. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2183. if (!(battle_flag&BF_SKILLMASK)) {
  2184. if (battle_flag&(BF_MAGIC | BF_MISC))
  2185. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2186. if (battle_flag&BF_WEAPON)
  2187. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2188. }
  2189. for (auto &it : spell) {
  2190. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.battle_flag == battle_flag && it.flag == flag) {
  2191. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2192. return;
  2193. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2194. return;
  2195. }
  2196. }
  2197. struct s_autospell entry = {};
  2198. if (rate < -10000 || rate > 10000)
  2199. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2200. entry.id = id;
  2201. entry.lv = lv;
  2202. entry.rate = cap_value(rate, -10000, 10000);
  2203. entry.battle_flag = battle_flag;
  2204. entry.card_id = card_id;
  2205. entry.flag = flag;
  2206. spell.push_back(entry);
  2207. }
  2208. /**
  2209. * Add auto spell bonus for player while using skills
  2210. * @param spell: Spell array
  2211. * @param src_skill: Trigger skill
  2212. * @param id: Support or target type
  2213. * @param lv: Skill level
  2214. * @param rate: Success chance
  2215. * @param card_id: Used to prevent card stacking
  2216. * @param flag: Flags used for extra arguments
  2217. * &1: forces the skill to be casted on self, rather than on the target of skill
  2218. * &2: random skill level in [1..lv] is chosen
  2219. */
  2220. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, uint16 src_skill, uint16 id, uint16 lv, short rate, t_itemid card_id, uint8 flag)
  2221. {
  2222. if (spell.size() == MAX_PC_BONUS) {
  2223. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2224. return;
  2225. }
  2226. if (!rate)
  2227. return;
  2228. struct s_autospell entry = {};
  2229. if (rate < -10000 || rate > 10000)
  2230. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2231. entry.trigger_skill = src_skill;
  2232. entry.id = id;
  2233. entry.lv = lv;
  2234. entry.rate = cap_value(rate, -10000, 10000);
  2235. entry.card_id = card_id;
  2236. entry.flag = flag;
  2237. spell.push_back(entry);
  2238. }
  2239. /**
  2240. * Add inflict effect bonus for player while attacking/attacked
  2241. * @param effect: Effect array
  2242. * @param sc: SC/Effect type
  2243. * @param rate: Success chance
  2244. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2245. * @param flag: Target flag
  2246. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2247. */
  2248. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2249. {
  2250. if (effect.size() == MAX_PC_BONUS) {
  2251. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2252. return;
  2253. }
  2254. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2255. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2256. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2257. flag |= ATF_TARGET; //Default target: enemy.
  2258. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2259. flag |= ATF_WEAPON; //Default type: weapon.
  2260. if (!duration)
  2261. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2262. for (auto &it : effect) {
  2263. if (it.sc == sc && it.flag == flag) {
  2264. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2265. it.arrow_rate += arrow_rate;
  2266. it.duration = umax(it.duration, duration);
  2267. return;
  2268. }
  2269. }
  2270. struct s_addeffect entry = {};
  2271. if (rate < -10000 || rate > 10000)
  2272. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2273. entry.sc = sc;
  2274. entry.rate = cap_value(rate, -10000, 10000);
  2275. entry.arrow_rate = arrow_rate;
  2276. entry.flag = flag;
  2277. entry.duration = duration;
  2278. effect.push_back(entry);
  2279. }
  2280. /**
  2281. * Add inflict effect bonus for player while attacking using skill
  2282. * @param effect: Effect array
  2283. * @param sc: SC/Effect type
  2284. * @param rate: Success chance
  2285. * @param skill_id: Skill to cast
  2286. * @param target: Target type
  2287. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2288. */
  2289. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  2290. {
  2291. if (effect.size() == MAX_PC_BONUS) {
  2292. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2293. return;
  2294. }
  2295. if (!duration)
  2296. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2297. for (auto &it : effect) {
  2298. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2299. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2300. it.duration = umax(it.duration, duration);
  2301. return;
  2302. }
  2303. }
  2304. struct s_addeffectonskill entry = {};
  2305. if (rate < -10000 || rate > 10000)
  2306. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2307. entry.sc = sc;
  2308. entry.rate = cap_value(rate, -10000, 10000);
  2309. entry.skill_id = skill_id;
  2310. entry.target = target;
  2311. entry.duration = duration;
  2312. effect.push_back(entry);
  2313. }
  2314. /**
  2315. * Adjust/add drop rate modifier for player
  2316. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2317. * @param nameid: item id that will be dropped
  2318. * @param group: group id
  2319. * @param class_: target class
  2320. * @param race: target race. if < 0, means monster_id
  2321. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2322. */
  2323. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2324. {
  2325. if (!nameid && !group) {
  2326. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2327. return;
  2328. }
  2329. if (nameid && !itemdb_exists(nameid)) {
  2330. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2331. return;
  2332. }
  2333. if (group && !itemdb_group.exists(group)) {
  2334. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2335. return;
  2336. }
  2337. if (drop.size() == MAX_PC_BONUS) {
  2338. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %u group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2339. return;
  2340. }
  2341. //Apply config rate adjustment settings.
  2342. if (rate >= 0) { //Absolute drop.
  2343. if (battle_config.item_rate_adddrop != 100)
  2344. rate = rate*battle_config.item_rate_adddrop/100;
  2345. if (rate < battle_config.item_drop_adddrop_min)
  2346. rate = battle_config.item_drop_adddrop_min;
  2347. else if (rate > battle_config.item_drop_adddrop_max)
  2348. rate = battle_config.item_drop_adddrop_max;
  2349. } else { //Relative drop, max/min limits are applied at drop time.
  2350. if (battle_config.item_rate_adddrop != 100)
  2351. rate = rate*battle_config.item_rate_adddrop/100;
  2352. if (rate > -1)
  2353. rate = -1;
  2354. }
  2355. for (auto &it : drop) {
  2356. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2357. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2358. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2359. return;
  2360. }
  2361. }
  2362. struct s_add_drop entry = {};
  2363. if (rate < -10000 || rate > 10000)
  2364. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2365. entry.nameid = nameid;
  2366. entry.group = group;
  2367. entry.race = race;
  2368. entry.class_ = class_;
  2369. entry.rate = cap_value(rate, -10000, 10000);
  2370. drop.push_back(entry);
  2371. }
  2372. s_autobonus::~s_autobonus(){
  2373. if( this->active != INVALID_TIMER ){
  2374. delete_timer( this->active, pc_endautobonus );
  2375. this->active = INVALID_TIMER;
  2376. }
  2377. if( this->bonus_script != nullptr ){
  2378. aFree( this->bonus_script );
  2379. this->bonus_script = nullptr;
  2380. }
  2381. if( this->other_script != nullptr ){
  2382. aFree( this->other_script );
  2383. this->other_script = nullptr;
  2384. }
  2385. }
  2386. /**
  2387. * Add autobonus to player when attacking/attacked
  2388. * @param bonus: Bonus array
  2389. * @param script: Script to execute
  2390. * @param rate: Success chance
  2391. * @param dur: Duration
  2392. * @param flag: Battle flag/skill
  2393. * @param other_script: Secondary script to execute
  2394. * @param pos: Item equip position
  2395. * @param onskill: Skill used to trigger autobonus
  2396. * @return True on success or false otherwise
  2397. */
  2398. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill){
  2399. // Check if the same bonus already exists
  2400. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2401. // Compare based on position and bonus script
  2402. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2403. return false;
  2404. }
  2405. }
  2406. if (bonus.size() == MAX_PC_BONUS) {
  2407. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2408. return false;
  2409. }
  2410. if (!onskill) {
  2411. if (!(flag&BF_RANGEMASK))
  2412. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2413. if (!(flag&BF_WEAPONMASK))
  2414. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2415. if (!(flag&BF_SKILLMASK)) {
  2416. if (flag&(BF_MAGIC | BF_MISC))
  2417. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2418. if (flag&BF_WEAPON)
  2419. flag |= BF_NORMAL | BF_SKILL;
  2420. }
  2421. }
  2422. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2423. if (rate < -10000 || rate > 10000)
  2424. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2425. entry->rate = cap_value(rate, -10000, 10000);
  2426. entry->duration = dur;
  2427. entry->active = INVALID_TIMER;
  2428. entry->atk_type = flag;
  2429. entry->pos = pos;
  2430. entry->bonus_script = aStrdup(script);
  2431. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2432. bonus.push_back(entry);
  2433. return true;
  2434. }
  2435. /**
  2436. * Remove an autobonus from player
  2437. * @param sd: Player data
  2438. * @param bonus: Autobonus array
  2439. * @param restore: Run script on clearing or not
  2440. */
  2441. void pc_delautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2442. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2443. while( it != bonus.end() ){
  2444. std::shared_ptr<s_autobonus> b = *it;
  2445. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2446. unsigned int equip_pos_idx = 0;
  2447. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2448. for (uint8 j = 0; j < EQI_MAX; j++) {
  2449. if (sd.equip_index[j] >= 0)
  2450. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2451. }
  2452. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2453. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2454. }else{
  2455. // Not all required items equipped anymore
  2456. restore = false;
  2457. }
  2458. }
  2459. if( restore ){
  2460. it++;
  2461. continue;
  2462. }
  2463. it = bonus.erase(it);
  2464. }
  2465. }
  2466. /**
  2467. * Execute autobonus on player
  2468. * @param sd: Player data
  2469. * @param autobonus: Autobonus to run
  2470. */
  2471. void pc_exeautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2472. {
  2473. if (autobonus->active != INVALID_TIMER)
  2474. delete_timer(autobonus->active, pc_endautobonus);
  2475. if( autobonus->other_script )
  2476. {
  2477. int j;
  2478. unsigned int equip_pos_idx = 0;
  2479. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2480. for(j = 0; j < EQI_MAX; j++) {
  2481. if(sd.equip_index[j] >= 0)
  2482. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2483. }
  2484. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2485. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2486. }
  2487. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2488. status_calc_pc(&sd,SCO_FORCE);
  2489. }
  2490. /**
  2491. * Remove autobonus timer from player
  2492. */
  2493. TIMER_FUNC(pc_endautobonus){
  2494. struct map_session_data *sd = map_id2sd(id);
  2495. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2496. nullpo_ret(sd);
  2497. nullpo_ret(bonus);
  2498. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2499. if( autobonus->active == tid ){
  2500. autobonus->active = INVALID_TIMER;
  2501. break;
  2502. }
  2503. }
  2504. status_calc_pc(sd,SCO_FORCE);
  2505. return 0;
  2506. }
  2507. /**
  2508. * Add element bonus to player when attacking
  2509. * @param sd: Player data
  2510. * @param ele: Element to adjust
  2511. * @param rate: Success chance
  2512. * @param flag: Battle flag
  2513. */
  2514. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2515. {
  2516. nullpo_retv(sd);
  2517. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2518. if (wd->addele2.size() == MAX_PC_BONUS) {
  2519. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2520. return;
  2521. }
  2522. if (!(flag&BF_RANGEMASK))
  2523. flag |= BF_SHORT | BF_LONG;
  2524. if (!(flag&BF_WEAPONMASK))
  2525. flag |= BF_WEAPON;
  2526. if (!(flag&BF_SKILLMASK)) {
  2527. if (flag&(BF_MAGIC | BF_MISC))
  2528. flag |= BF_SKILL;
  2529. if (flag&BF_WEAPON)
  2530. flag |= BF_NORMAL | BF_SKILL;
  2531. }
  2532. for (auto &it : wd->addele2) {
  2533. if (it.ele == ele && it.flag == flag) {
  2534. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2535. return;
  2536. }
  2537. }
  2538. struct s_addele2 entry = {};
  2539. if (rate < -10000 || rate > 10000)
  2540. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2541. entry.ele = ele;
  2542. entry.rate = cap_value(rate, -10000, 10000);
  2543. entry.flag = flag;
  2544. wd->addele2.push_back(entry);
  2545. }
  2546. /**
  2547. * Reduce element bonus to player when attacking
  2548. * @param sd: Player data
  2549. * @param ele: Element to adjust
  2550. * @param rate: Success chance
  2551. * @param flag: Battle flag
  2552. */
  2553. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2554. {
  2555. nullpo_retv(sd);
  2556. if (sd->subele2.size() == MAX_PC_BONUS) {
  2557. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2558. return;
  2559. }
  2560. if (!(flag&BF_RANGEMASK))
  2561. flag |= BF_SHORT | BF_LONG;
  2562. if (!(flag&BF_WEAPONMASK))
  2563. flag |= BF_WEAPON;
  2564. if (!(flag&BF_SKILLMASK)) {
  2565. if (flag&(BF_MAGIC | BF_MISC))
  2566. flag |= BF_SKILL;
  2567. if (flag&BF_WEAPON)
  2568. flag |= BF_NORMAL | BF_SKILL;
  2569. }
  2570. for (auto &it : sd->subele2) {
  2571. if (it.ele == ele && it.flag == flag) {
  2572. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2573. return;
  2574. }
  2575. }
  2576. struct s_addele2 entry = {};
  2577. if (rate < -10000 || rate > 10000)
  2578. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2579. entry.ele = ele;
  2580. entry.rate = cap_value(rate, -10000, 10000);
  2581. entry.flag = flag;
  2582. sd->subele2.push_back(entry);
  2583. }
  2584. /**
  2585. * Adjust race damage to target when attacking
  2586. * @param sd: Player data
  2587. * @param race: Race to adjust
  2588. * @param rate: Success chance
  2589. * @param flag: Battle flag
  2590. */
  2591. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2592. {
  2593. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2594. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2595. return;
  2596. }
  2597. if (!(flag&BF_RANGEMASK))
  2598. flag |= BF_SHORT | BF_LONG;
  2599. if (!(flag&BF_WEAPONMASK))
  2600. flag |= BF_WEAPON;
  2601. if (!(flag&BF_SKILLMASK)) {
  2602. if (flag&(BF_MAGIC | BF_MISC))
  2603. flag |= BF_SKILL;
  2604. if (flag&BF_WEAPON)
  2605. flag |= BF_NORMAL | BF_SKILL;
  2606. }
  2607. for (auto &it : sd->subrace3) {
  2608. if (it.race == race && it.flag == flag) {
  2609. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2610. return;
  2611. }
  2612. }
  2613. struct s_addrace2 entry = {};
  2614. if (rate < -10000 || rate > 10000)
  2615. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2616. entry.race = race;
  2617. entry.rate = cap_value(rate, -10000, 10000);
  2618. entry.flag = flag;
  2619. sd->subrace3.push_back(entry);
  2620. }
  2621. /**
  2622. * General item bonus for player
  2623. * @param bonus: Bonus array
  2624. * @param id: Key
  2625. * @param val: Value
  2626. * @param cap_rate: If Value is a rate value that needs to be capped
  2627. */
  2628. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2629. {
  2630. for (auto &it : bonus) {
  2631. if (it.id == id) {
  2632. if (cap_rate)
  2633. it.val = cap_value(it.val + val, -10000, 10000);
  2634. else
  2635. it.val += val;
  2636. return;
  2637. }
  2638. }
  2639. struct s_item_bonus entry = {};
  2640. if (cap_rate && (val < -10000 || val > 10000)) {
  2641. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2642. val = cap_value(val, -10000, 10000);
  2643. }
  2644. entry.id = id;
  2645. entry.val = val;
  2646. bonus.push_back(entry);
  2647. }
  2648. /**
  2649. * Remove HP/SP to player when attacking
  2650. * @param bonus: Bonus array
  2651. * @param rate: Success chance
  2652. * @param per: Percentage of HP/SP to vanish
  2653. * @param flag: Battle flag
  2654. */
  2655. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2656. if (bonus.size() == MAX_PC_BONUS) {
  2657. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2658. return;
  2659. }
  2660. if (!(flag&BF_RANGEMASK))
  2661. flag |= BF_SHORT | BF_LONG;
  2662. if (!(flag&BF_WEAPONMASK))
  2663. flag |= BF_WEAPON;
  2664. if (!(flag&BF_SKILLMASK)) {
  2665. if (flag&(BF_MAGIC | BF_MISC))
  2666. flag |= BF_SKILL;
  2667. if (flag&BF_WEAPON)
  2668. flag |= BF_NORMAL | BF_SKILL;
  2669. }
  2670. for (auto &it : bonus) {
  2671. if (it.flag == flag) {
  2672. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2673. it.per += per;
  2674. return;
  2675. }
  2676. }
  2677. struct s_vanish_bonus entry = {};
  2678. if (rate < -10000 || rate > 10000)
  2679. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2680. entry.rate = cap_value(rate, -10000, 10000);
  2681. entry.per = per;
  2682. entry.flag = flag;
  2683. bonus.push_back(entry);
  2684. }
  2685. /*==========================================
  2686. * Add a bonus(type) to player sd
  2687. * format: bonus bBonusName,val;
  2688. * @param sd
  2689. * @param type Bonus type used by bBonusName
  2690. * @param val Value that usually for rate or fixed value
  2691. *------------------------------------------*/
  2692. void pc_bonus(struct map_session_data *sd,int type,int val)
  2693. {
  2694. struct status_data *status;
  2695. int bonus;
  2696. nullpo_retv(sd);
  2697. status = &sd->base_status;
  2698. switch(type){
  2699. case SP_STR:
  2700. case SP_AGI:
  2701. case SP_VIT:
  2702. case SP_INT:
  2703. case SP_DEX:
  2704. case SP_LUK:
  2705. if(sd->state.lr_flag != 2)
  2706. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2707. break;
  2708. case SP_POW:
  2709. case SP_STA:
  2710. case SP_WIS:
  2711. case SP_SPL:
  2712. case SP_CON:
  2713. case SP_CRT:
  2714. if (sd->state.lr_flag != 2)
  2715. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2716. break;
  2717. case SP_ATK1:
  2718. if(!sd->state.lr_flag) {
  2719. bonus = status->rhw.atk + val;
  2720. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2721. }
  2722. else if(sd->state.lr_flag == 1) {
  2723. bonus = status->lhw.atk + val;
  2724. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2725. }
  2726. break;
  2727. case SP_ATK2:
  2728. if(!sd->state.lr_flag) {
  2729. bonus = status->rhw.atk2 + val;
  2730. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2731. }
  2732. else if(sd->state.lr_flag == 1) {
  2733. bonus = status->lhw.atk2 + val;
  2734. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2735. }
  2736. break;
  2737. case SP_BASE_ATK:
  2738. if(sd->state.lr_flag != 2) {
  2739. #ifdef RENEWAL
  2740. bonus = sd->bonus.eatk + val;
  2741. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2742. #else
  2743. bonus = status->batk + val;
  2744. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2745. #endif
  2746. }
  2747. break;
  2748. case SP_DEF1:
  2749. if(sd->state.lr_flag != 2) {
  2750. bonus = status->def + val;
  2751. #ifdef RENEWAL
  2752. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2753. #else
  2754. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2755. #endif
  2756. }
  2757. break;
  2758. case SP_DEF2:
  2759. if(sd->state.lr_flag != 2) {
  2760. bonus = status->def2 + val;
  2761. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2762. }
  2763. break;
  2764. case SP_MDEF1:
  2765. if(sd->state.lr_flag != 2) {
  2766. bonus = status->mdef + val;
  2767. #ifdef RENEWAL
  2768. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2769. #else
  2770. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2771. #endif
  2772. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2773. sd->bonus.shieldmdef += bonus;
  2774. }
  2775. }
  2776. break;
  2777. case SP_MDEF2:
  2778. if(sd->state.lr_flag != 2) {
  2779. bonus = status->mdef2 + val;
  2780. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2781. }
  2782. break;
  2783. case SP_HIT:
  2784. if(sd->state.lr_flag != 2) {
  2785. bonus = status->hit + val;
  2786. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2787. } else
  2788. sd->bonus.arrow_hit+=val;
  2789. break;
  2790. case SP_FLEE1:
  2791. if(sd->state.lr_flag != 2) {
  2792. bonus = status->flee + val;
  2793. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2794. }
  2795. break;
  2796. case SP_FLEE2:
  2797. if(sd->state.lr_flag != 2) {
  2798. bonus = status->flee2 + val*10;
  2799. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2800. }
  2801. break;
  2802. case SP_CRITICAL:
  2803. if(sd->state.lr_flag != 2) {
  2804. bonus = status->cri + val*10;
  2805. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2806. } else
  2807. sd->bonus.arrow_cri += val*10;
  2808. break;
  2809. case SP_PATK:
  2810. if (sd->state.lr_flag != 2) {
  2811. bonus = status->patk + val;
  2812. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2813. }
  2814. break;
  2815. case SP_SMATK:
  2816. if (sd->state.lr_flag != 2) {
  2817. bonus = status->smatk + val;
  2818. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2819. }
  2820. break;
  2821. case SP_RES:
  2822. if (sd->state.lr_flag != 2) {
  2823. bonus = status->res + val;
  2824. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2825. }
  2826. break;
  2827. case SP_MRES:
  2828. if (sd->state.lr_flag != 2) {
  2829. bonus = status->mres + val;
  2830. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2831. }
  2832. break;
  2833. case SP_HPLUS:
  2834. if (sd->state.lr_flag != 2) {
  2835. bonus = status->hplus + val;
  2836. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2837. }
  2838. break;
  2839. case SP_CRATE:
  2840. if (sd->state.lr_flag != 2) {
  2841. bonus = status->crate + val;
  2842. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2843. }
  2844. break;
  2845. case SP_ATKELE:
  2846. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2847. switch (sd->state.lr_flag)
  2848. {
  2849. case 2:
  2850. switch (sd->status.weapon) {
  2851. case W_BOW:
  2852. case W_REVOLVER:
  2853. case W_RIFLE:
  2854. case W_GATLING:
  2855. case W_SHOTGUN:
  2856. case W_GRENADE:
  2857. //Become weapon element.
  2858. status->rhw.ele=val;
  2859. break;
  2860. default: //Become arrow element.
  2861. sd->bonus.arrow_ele=val;
  2862. break;
  2863. }
  2864. break;
  2865. case 1:
  2866. status->lhw.ele=val;
  2867. break;
  2868. default:
  2869. status->rhw.ele=val;
  2870. break;
  2871. }
  2872. break;
  2873. case SP_DEFELE:
  2874. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2875. if(sd->state.lr_flag != 2)
  2876. status->def_ele=val;
  2877. break;
  2878. case SP_MAXHP:
  2879. if(sd->state.lr_flag == 2)
  2880. break;
  2881. sd->bonus.hp += val;
  2882. break;
  2883. case SP_MAXSP:
  2884. if(sd->state.lr_flag == 2)
  2885. break;
  2886. sd->bonus.sp += val;
  2887. break;
  2888. case SP_MAXAP:
  2889. if (sd->state.lr_flag == 2)
  2890. break;
  2891. sd->bonus.ap += val;
  2892. break;
  2893. case SP_MAXHPRATE:
  2894. if(sd->state.lr_flag != 2)
  2895. sd->hprate+=val;
  2896. break;
  2897. case SP_MAXSPRATE:
  2898. if(sd->state.lr_flag != 2)
  2899. sd->sprate+=val;
  2900. break;
  2901. case SP_MAXAPRATE:
  2902. if (sd->state.lr_flag != 2)
  2903. sd->aprate += val;
  2904. break;
  2905. case SP_SPRATE:
  2906. if(sd->state.lr_flag != 2)
  2907. sd->dsprate+=val;
  2908. break;
  2909. case SP_ATTACKRANGE:
  2910. switch (sd->state.lr_flag) {
  2911. case 2:
  2912. switch (sd->status.weapon) {
  2913. case W_BOW:
  2914. case W_REVOLVER:
  2915. case W_RIFLE:
  2916. case W_GATLING:
  2917. case W_SHOTGUN:
  2918. case W_GRENADE:
  2919. status->rhw.range += val;
  2920. }
  2921. break;
  2922. case 1:
  2923. status->lhw.range += val;
  2924. break;
  2925. default:
  2926. status->rhw.range += val;
  2927. break;
  2928. }
  2929. break;
  2930. case SP_SPEED_RATE: //Non stackable increase
  2931. if(sd->state.lr_flag != 2)
  2932. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2933. break;
  2934. case SP_SPEED_ADDRATE: //Stackable increase
  2935. if(sd->state.lr_flag != 2)
  2936. sd->bonus.speed_add_rate -= val;
  2937. break;
  2938. case SP_ASPD: //Raw increase
  2939. if(sd->state.lr_flag != 2)
  2940. sd->bonus.aspd_add -= 10*val;
  2941. break;
  2942. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2943. if(sd->state.lr_flag != 2)
  2944. #ifndef RENEWAL_ASPD
  2945. status->aspd_rate -= 10*val;
  2946. #else
  2947. status->aspd_rate2 += val;
  2948. #endif
  2949. break;
  2950. case SP_HP_RECOV_RATE:
  2951. if(sd->state.lr_flag != 2)
  2952. sd->hprecov_rate += val;
  2953. break;
  2954. case SP_SP_RECOV_RATE:
  2955. if(sd->state.lr_flag != 2)
  2956. sd->sprecov_rate += val;
  2957. break;
  2958. case SP_CRITICAL_DEF:
  2959. if(sd->state.lr_flag != 2)
  2960. sd->bonus.critical_def += val;
  2961. break;
  2962. case SP_NEAR_ATK_DEF:
  2963. if(sd->state.lr_flag != 2)
  2964. sd->bonus.near_attack_def_rate += val;
  2965. break;
  2966. case SP_LONG_ATK_DEF:
  2967. if(sd->state.lr_flag != 2)
  2968. sd->bonus.long_attack_def_rate += val;
  2969. break;
  2970. case SP_DOUBLE_RATE:
  2971. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2972. sd->bonus.double_rate = val;
  2973. break;
  2974. case SP_DOUBLE_ADD_RATE:
  2975. if(sd->state.lr_flag == 0)
  2976. sd->bonus.double_add_rate += val;
  2977. break;
  2978. case SP_MATK_RATE:
  2979. if(sd->state.lr_flag != 2)
  2980. sd->matk_rate += val;
  2981. break;
  2982. case SP_IGNORE_DEF_ELE:
  2983. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2984. if(!sd->state.lr_flag)
  2985. sd->right_weapon.ignore_def_ele |= 1<<val;
  2986. else if(sd->state.lr_flag == 1)
  2987. sd->left_weapon.ignore_def_ele |= 1<<val;
  2988. break;
  2989. case SP_IGNORE_DEF_RACE:
  2990. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2991. if(!sd->state.lr_flag)
  2992. sd->right_weapon.ignore_def_race |= 1<<val;
  2993. else if(sd->state.lr_flag == 1)
  2994. sd->left_weapon.ignore_def_race |= 1<<val;
  2995. break;
  2996. case SP_IGNORE_DEF_CLASS:
  2997. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2998. if(!sd->state.lr_flag)
  2999. sd->right_weapon.ignore_def_class |= 1<<val;
  3000. else if(sd->state.lr_flag == 1)
  3001. sd->left_weapon.ignore_def_class |= 1<<val;
  3002. break;
  3003. case SP_ATK_RATE:
  3004. if(sd->state.lr_flag != 2)
  3005. sd->bonus.atk_rate += val;
  3006. break;
  3007. case SP_MAGIC_ATK_DEF:
  3008. if(sd->state.lr_flag != 2)
  3009. sd->bonus.magic_def_rate += val;
  3010. break;
  3011. case SP_MISC_ATK_DEF:
  3012. if(sd->state.lr_flag != 2)
  3013. sd->bonus.misc_def_rate += val;
  3014. break;
  3015. case SP_IGNORE_MDEF_ELE:
  3016. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3017. if(sd->state.lr_flag != 2)
  3018. sd->bonus.ignore_mdef_ele |= 1<<val;
  3019. break;
  3020. case SP_IGNORE_MDEF_RACE:
  3021. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3022. if(sd->state.lr_flag != 2)
  3023. sd->bonus.ignore_mdef_race |= 1<<val;
  3024. break;
  3025. case SP_PERFECT_HIT_RATE:
  3026. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3027. sd->bonus.perfect_hit = val;
  3028. break;
  3029. case SP_PERFECT_HIT_ADD_RATE:
  3030. if(sd->state.lr_flag != 2)
  3031. sd->bonus.perfect_hit_add += val;
  3032. break;
  3033. case SP_CRITICAL_RATE:
  3034. if(sd->state.lr_flag != 2)
  3035. sd->critical_rate+=val;
  3036. break;
  3037. case SP_DEF_RATIO_ATK_ELE:
  3038. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3039. if(!sd->state.lr_flag)
  3040. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3041. else if(sd->state.lr_flag == 1)
  3042. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3043. break;
  3044. case SP_DEF_RATIO_ATK_RACE:
  3045. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3046. if(!sd->state.lr_flag)
  3047. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3048. else if(sd->state.lr_flag == 1)
  3049. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3050. break;
  3051. case SP_DEF_RATIO_ATK_CLASS:
  3052. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3053. if(!sd->state.lr_flag)
  3054. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3055. else if(sd->state.lr_flag == 1)
  3056. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3057. break;
  3058. case SP_HIT_RATE:
  3059. if(sd->state.lr_flag != 2)
  3060. sd->hit_rate += val;
  3061. break;
  3062. case SP_FLEE_RATE:
  3063. if(sd->state.lr_flag != 2)
  3064. sd->flee_rate += val;
  3065. break;
  3066. case SP_FLEE2_RATE:
  3067. if(sd->state.lr_flag != 2)
  3068. sd->flee2_rate += val;
  3069. break;
  3070. case SP_DEF_RATE:
  3071. if(sd->state.lr_flag != 2)
  3072. sd->def_rate += val;
  3073. break;
  3074. case SP_DEF2_RATE:
  3075. if(sd->state.lr_flag != 2)
  3076. sd->def2_rate += val;
  3077. break;
  3078. case SP_MDEF_RATE:
  3079. if(sd->state.lr_flag != 2)
  3080. sd->mdef_rate += val;
  3081. break;
  3082. case SP_MDEF2_RATE:
  3083. if(sd->state.lr_flag != 2)
  3084. sd->mdef2_rate += val;
  3085. break;
  3086. case SP_PATK_RATE:
  3087. if (sd->state.lr_flag != 2)
  3088. sd->patk_rate += val;
  3089. break;
  3090. case SP_SMATK_RATE:
  3091. if (sd->state.lr_flag != 2)
  3092. sd->smatk_rate += val;
  3093. break;
  3094. case SP_RES_RATE:
  3095. if (sd->state.lr_flag != 2)
  3096. sd->res_rate += val;
  3097. break;
  3098. case SP_MRES_RATE:
  3099. if (sd->state.lr_flag != 2)
  3100. sd->mres_rate += val;
  3101. break;
  3102. case SP_HPLUS_RATE:
  3103. if (sd->state.lr_flag != 2)
  3104. sd->hplus_rate += val;
  3105. break;
  3106. case SP_CRATE_RATE:
  3107. if (sd->state.lr_flag != 2)
  3108. sd->crate_rate += val;
  3109. break;
  3110. case SP_RESTART_FULL_RECOVER:
  3111. if(sd->state.lr_flag != 2)
  3112. sd->special_state.restart_full_recover = 1;
  3113. break;
  3114. case SP_NO_CASTCANCEL:
  3115. if(sd->state.lr_flag != 2)
  3116. sd->special_state.no_castcancel = 1;
  3117. break;
  3118. case SP_NO_CASTCANCEL2:
  3119. if(sd->state.lr_flag != 2)
  3120. sd->special_state.no_castcancel2 = 1;
  3121. break;
  3122. case SP_NO_SIZEFIX:
  3123. if(sd->state.lr_flag != 2)
  3124. sd->special_state.no_sizefix = 1;
  3125. break;
  3126. case SP_NO_MAGIC_DAMAGE:
  3127. if(sd->state.lr_flag == 2)
  3128. break;
  3129. val+= sd->special_state.no_magic_damage;
  3130. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3131. break;
  3132. case SP_NO_WEAPON_DAMAGE:
  3133. if(sd->state.lr_flag == 2)
  3134. break;
  3135. val+= sd->special_state.no_weapon_damage;
  3136. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3137. break;
  3138. case SP_NO_MISC_DAMAGE:
  3139. if(sd->state.lr_flag == 2)
  3140. break;
  3141. val+= sd->special_state.no_misc_damage;
  3142. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3143. break;
  3144. case SP_NO_GEMSTONE:
  3145. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3146. sd->special_state.no_gemstone = 1;
  3147. break;
  3148. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3149. if(sd->state.lr_flag != 2) {
  3150. sd->special_state.intravision = 1;
  3151. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3152. }
  3153. break;
  3154. case SP_NO_KNOCKBACK:
  3155. if(sd->state.lr_flag != 2)
  3156. sd->special_state.no_knockback = 1;
  3157. break;
  3158. case SP_SPLASH_RANGE:
  3159. if(sd->bonus.splash_range < val)
  3160. sd->bonus.splash_range = val;
  3161. break;
  3162. case SP_SPLASH_ADD_RANGE:
  3163. sd->bonus.splash_add_range += val;
  3164. break;
  3165. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3166. if(sd->state.lr_flag != 2)
  3167. sd->bonus.short_weapon_damage_return += val;
  3168. break;
  3169. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3170. if(sd->state.lr_flag != 2)
  3171. sd->bonus.long_weapon_damage_return += val;
  3172. break;
  3173. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3174. if(sd->state.lr_flag != 2)
  3175. sd->bonus.magic_damage_return += val;
  3176. break;
  3177. case SP_REDUCE_DAMAGE_RETURN:
  3178. if (sd->state.lr_flag != 2)
  3179. sd->bonus.reduce_damage_return += val;
  3180. break;
  3181. case SP_ALL_STATS: // [Valaris]
  3182. if(sd->state.lr_flag!=2) {
  3183. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3184. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3185. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3186. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3187. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3188. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3189. }
  3190. break;
  3191. case SP_ALL_TRAIT_STATS:
  3192. if (sd->state.lr_flag != 2) {
  3193. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3194. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3195. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3196. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3197. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3198. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3199. }
  3200. break;
  3201. case SP_AGI_VIT: // [Valaris]
  3202. if(sd->state.lr_flag!=2) {
  3203. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3204. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3205. }
  3206. break;
  3207. case SP_AGI_DEX_STR: // [Valaris]
  3208. if(sd->state.lr_flag!=2) {
  3209. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3210. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3211. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3212. }
  3213. break;
  3214. case SP_PERFECT_HIDE: // [Valaris]
  3215. if(sd->state.lr_flag!=2)
  3216. sd->special_state.perfect_hiding=1;
  3217. break;
  3218. case SP_UNBREAKABLE:
  3219. if(sd->state.lr_flag!=2)
  3220. sd->bonus.unbreakable += val;
  3221. break;
  3222. case SP_UNBREAKABLE_WEAPON:
  3223. if(sd->state.lr_flag != 2)
  3224. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3225. break;
  3226. case SP_UNBREAKABLE_ARMOR:
  3227. if(sd->state.lr_flag != 2)
  3228. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3229. break;
  3230. case SP_UNBREAKABLE_HELM:
  3231. if(sd->state.lr_flag != 2)
  3232. sd->bonus.unbreakable_equip |= EQP_HELM;
  3233. break;
  3234. case SP_UNBREAKABLE_SHIELD:
  3235. if(sd->state.lr_flag != 2)
  3236. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3237. break;
  3238. case SP_UNBREAKABLE_GARMENT:
  3239. if(sd->state.lr_flag != 2)
  3240. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3241. break;
  3242. case SP_UNBREAKABLE_SHOES:
  3243. if(sd->state.lr_flag != 2)
  3244. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3245. break;
  3246. case SP_CLASSCHANGE: // [Valaris]
  3247. if(sd->state.lr_flag !=2)
  3248. sd->bonus.classchange=val;
  3249. break;
  3250. case SP_SHORT_ATK_RATE:
  3251. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3252. sd->bonus.short_attack_atk_rate+=val;
  3253. break;
  3254. case SP_LONG_ATK_RATE:
  3255. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3256. sd->bonus.long_attack_atk_rate+=val;
  3257. break;
  3258. case SP_BREAK_WEAPON_RATE:
  3259. if(sd->state.lr_flag != 2)
  3260. sd->bonus.break_weapon_rate+=val;
  3261. break;
  3262. case SP_BREAK_ARMOR_RATE:
  3263. if(sd->state.lr_flag != 2)
  3264. sd->bonus.break_armor_rate+=val;
  3265. break;
  3266. case SP_ADD_STEAL_RATE:
  3267. if(sd->state.lr_flag != 2)
  3268. sd->bonus.add_steal_rate+=val;
  3269. break;
  3270. case SP_DELAYRATE:
  3271. if(sd->state.lr_flag != 2)
  3272. sd->bonus.delayrate -= val;
  3273. break;
  3274. case SP_CRIT_ATK_RATE:
  3275. if(sd->state.lr_flag != 2)
  3276. sd->bonus.crit_atk_rate += val;
  3277. break;
  3278. case SP_CRIT_DEF_RATE:
  3279. if(sd->state.lr_flag != 2)
  3280. sd->bonus.crit_def_rate += val;
  3281. break;
  3282. case SP_NO_REGEN:
  3283. if(sd->state.lr_flag != 2)
  3284. sd->regen.state.block|=val;
  3285. break;
  3286. case SP_UNSTRIPABLE_WEAPON:
  3287. if(sd->state.lr_flag != 2)
  3288. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3289. break;
  3290. case SP_UNSTRIPABLE:
  3291. case SP_UNSTRIPABLE_ARMOR:
  3292. if(sd->state.lr_flag != 2)
  3293. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3294. break;
  3295. case SP_UNSTRIPABLE_HELM:
  3296. if(sd->state.lr_flag != 2)
  3297. sd->bonus.unstripable_equip |= EQP_HELM;
  3298. break;
  3299. case SP_UNSTRIPABLE_SHIELD:
  3300. if(sd->state.lr_flag != 2)
  3301. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3302. break;
  3303. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3304. if(!sd->state.lr_flag) {
  3305. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3306. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3307. } else if(sd->state.lr_flag == 1) {
  3308. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3309. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3310. }
  3311. break;
  3312. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3313. if(!sd->state.lr_flag) {
  3314. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3315. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3316. } else if(sd->state.lr_flag == 1) {
  3317. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3318. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3319. }
  3320. break;
  3321. case SP_SP_GAIN_VALUE:
  3322. if(!sd->state.lr_flag)
  3323. sd->bonus.sp_gain_value += val;
  3324. break;
  3325. case SP_HP_GAIN_VALUE:
  3326. if(!sd->state.lr_flag)
  3327. sd->bonus.hp_gain_value += val;
  3328. break;
  3329. case SP_LONG_SP_GAIN_VALUE:
  3330. if(!sd->state.lr_flag)
  3331. sd->bonus.long_sp_gain_value += val;
  3332. case SP_LONG_HP_GAIN_VALUE:
  3333. if(!sd->state.lr_flag)
  3334. sd->bonus.long_hp_gain_value += val;
  3335. break;
  3336. case SP_MAGIC_SP_GAIN_VALUE:
  3337. if(!sd->state.lr_flag)
  3338. sd->bonus.magic_sp_gain_value += val;
  3339. break;
  3340. case SP_MAGIC_HP_GAIN_VALUE:
  3341. if(!sd->state.lr_flag)
  3342. sd->bonus.magic_hp_gain_value += val;
  3343. break;
  3344. case SP_ADD_HEAL_RATE:
  3345. if(sd->state.lr_flag != 2)
  3346. sd->bonus.add_heal_rate += val;
  3347. break;
  3348. case SP_ADD_HEAL2_RATE:
  3349. if(sd->state.lr_flag != 2)
  3350. sd->bonus.add_heal2_rate += val;
  3351. break;
  3352. case SP_ADD_ITEM_HEAL_RATE:
  3353. if(sd->state.lr_flag != 2)
  3354. sd->bonus.itemhealrate2 += val;
  3355. break;
  3356. case SP_EMATK:
  3357. if(sd->state.lr_flag != 2)
  3358. sd->bonus.ematk += val;
  3359. break;
  3360. case SP_ADD_VARIABLECAST:
  3361. if (sd->state.lr_flag != 2)
  3362. sd->bonus.add_varcast += val;
  3363. break;
  3364. #ifdef RENEWAL_CAST
  3365. case SP_FIXCASTRATE:
  3366. if(sd->state.lr_flag != 2)
  3367. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3368. break;
  3369. case SP_ADD_FIXEDCAST:
  3370. if(sd->state.lr_flag != 2)
  3371. sd->bonus.add_fixcast += val;
  3372. break;
  3373. case SP_CASTRATE:
  3374. case SP_VARCASTRATE:
  3375. if(sd->state.lr_flag != 2)
  3376. sd->bonus.varcastrate -= val;
  3377. break;
  3378. #else
  3379. case SP_ADD_FIXEDCAST:
  3380. case SP_FIXCASTRATE:
  3381. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3382. break;
  3383. case SP_VARCASTRATE:
  3384. case SP_CASTRATE:
  3385. if(sd->state.lr_flag != 2)
  3386. sd->castrate += val;
  3387. break;
  3388. #endif
  3389. case SP_ADDMAXWEIGHT:
  3390. if (sd->state.lr_flag != 2)
  3391. sd->add_max_weight += val;
  3392. break;
  3393. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3394. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3395. break;
  3396. case SP_ABSORB_DMG_MAXHP2:
  3397. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3398. break;
  3399. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3400. if (sd->state.lr_flag != 2)
  3401. sd->bonus.critical_rangeatk += val*10;
  3402. else
  3403. sd->bonus.arrow_cri += val*10;
  3404. break;
  3405. case SP_WEAPON_ATK_RATE:
  3406. if (sd->state.lr_flag != 2)
  3407. sd->bonus.weapon_atk_rate += val;
  3408. break;
  3409. case SP_WEAPON_MATK_RATE:
  3410. if (sd->state.lr_flag != 2)
  3411. sd->bonus.weapon_matk_rate += val;
  3412. break;
  3413. case SP_NO_MADO_FUEL:
  3414. if (sd->state.lr_flag != 2)
  3415. sd->special_state.no_mado_fuel = 1;
  3416. break;
  3417. case SP_NO_WALK_DELAY:
  3418. if (sd->state.lr_flag != 2)
  3419. sd->special_state.no_walk_delay = 1;
  3420. break;
  3421. case SP_ADD_ITEM_SPHEAL_RATE:
  3422. if(sd->state.lr_flag != 2)
  3423. sd->bonus.itemsphealrate2 += val;
  3424. break;
  3425. default:
  3426. if (current_equip_combo_pos > 0) {
  3427. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%u\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3428. }
  3429. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3430. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%u\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3431. }
  3432. else {
  3433. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3434. }
  3435. break;
  3436. }
  3437. }
  3438. /*==========================================
  3439. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3440. * format: bonus2 bBonusName,type2,val;
  3441. * @param sd
  3442. * @param type Bonus type used by bBonusName
  3443. * @param type2
  3444. * @param val Value that usually for rate or fixed value
  3445. *------------------------------------------*/
  3446. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3447. {
  3448. nullpo_retv(sd);
  3449. switch(type){
  3450. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3451. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3452. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3453. sd->right_weapon.addele[type2]+=val;
  3454. else if(sd->state.lr_flag == 1)
  3455. sd->left_weapon.addele[type2]+=val;
  3456. else if(sd->state.lr_flag == 2)
  3457. sd->indexed_bonus.arrow_addele[type2]+=val;
  3458. break;
  3459. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3460. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3461. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3462. sd->right_weapon.addrace[type2]+=val;
  3463. else if(sd->state.lr_flag == 1)
  3464. sd->left_weapon.addrace[type2]+=val;
  3465. else if(sd->state.lr_flag == 2)
  3466. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3467. break;
  3468. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3469. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3470. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3471. sd->right_weapon.addclass[type2]+=val;
  3472. else if(sd->state.lr_flag == 1)
  3473. sd->left_weapon.addclass[type2]+=val;
  3474. else if(sd->state.lr_flag == 2)
  3475. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3476. break;
  3477. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3478. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3479. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3480. sd->right_weapon.addsize[type2]+=val;
  3481. else if(sd->state.lr_flag == 1)
  3482. sd->left_weapon.addsize[type2]+=val;
  3483. else if(sd->state.lr_flag == 2)
  3484. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3485. break;
  3486. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3487. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3488. if(sd->state.lr_flag != 2)
  3489. sd->indexed_bonus.subele_script[type2] += val;
  3490. break;
  3491. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3492. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3493. if(sd->state.lr_flag != 2)
  3494. sd->indexed_bonus.subrace[type2]+=val;
  3495. break;
  3496. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3497. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3498. if(sd->state.lr_flag != 2)
  3499. sd->indexed_bonus.subclass[type2]+=val;
  3500. break;
  3501. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3502. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3503. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3504. break;
  3505. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3506. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3507. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  3508. break;
  3509. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3510. if(sd->state.lr_flag == 2)
  3511. break;
  3512. if (sd->reseff.size() == MAX_PC_BONUS) {
  3513. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3514. break;
  3515. }
  3516. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3517. break;
  3518. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3519. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3520. if(sd->state.lr_flag != 2)
  3521. sd->indexed_bonus.magic_addele_script[type2] += val;
  3522. break;
  3523. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3524. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3525. if(sd->state.lr_flag != 2)
  3526. sd->indexed_bonus.magic_addrace[type2]+=val;
  3527. break;
  3528. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3529. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3530. if(sd->state.lr_flag != 2)
  3531. sd->indexed_bonus.magic_addclass[type2]+=val;
  3532. break;
  3533. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3534. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3535. if(sd->state.lr_flag != 2)
  3536. sd->indexed_bonus.magic_addsize[type2]+=val;
  3537. break;
  3538. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3539. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3540. if(sd->state.lr_flag != 2)
  3541. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3542. break;
  3543. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3544. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3545. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3546. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3547. break;
  3548. }
  3549. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3550. }
  3551. break;
  3552. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3553. if(sd->state.lr_flag == 2)
  3554. break;
  3555. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3556. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3557. break;
  3558. }
  3559. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3560. break;
  3561. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3562. if(sd->state.lr_flag == 2)
  3563. break;
  3564. if (sd->add_def.size() == MAX_PC_BONUS) {
  3565. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3566. break;
  3567. }
  3568. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3569. break;
  3570. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3571. if(sd->state.lr_flag == 2)
  3572. break;
  3573. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3574. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3575. break;
  3576. }
  3577. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3578. break;
  3579. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3580. if(!sd->state.lr_flag) {
  3581. sd->right_weapon.hp_drain_rate.rate += type2;
  3582. sd->right_weapon.hp_drain_rate.per += val;
  3583. }
  3584. else if(sd->state.lr_flag == 1) {
  3585. sd->left_weapon.hp_drain_rate.rate += type2;
  3586. sd->left_weapon.hp_drain_rate.per += val;
  3587. }
  3588. break;
  3589. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3590. if(!sd->state.lr_flag) {
  3591. sd->right_weapon.sp_drain_rate.rate += type2;
  3592. sd->right_weapon.sp_drain_rate.per += val;
  3593. }
  3594. else if(sd->state.lr_flag == 1) {
  3595. sd->left_weapon.sp_drain_rate.rate += type2;
  3596. sd->left_weapon.sp_drain_rate.per += val;
  3597. }
  3598. break;
  3599. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3600. if(sd->state.lr_flag != 2) {
  3601. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3602. }
  3603. break;
  3604. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3605. if(sd->state.lr_flag != 2) {
  3606. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3607. }
  3608. break;
  3609. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3610. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3611. sd->bonus.get_zeny_rate = val;
  3612. sd->bonus.get_zeny_num = type2;
  3613. }
  3614. break;
  3615. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3616. if(sd->state.lr_flag != 2) {
  3617. sd->bonus.get_zeny_rate += val;
  3618. sd->bonus.get_zeny_num += type2;
  3619. }
  3620. break;
  3621. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3622. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3623. if(sd->state.lr_flag == 2)
  3624. break;
  3625. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3626. sd->special_state.bonus_coma = 1;
  3627. break;
  3628. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3629. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3630. if(sd->state.lr_flag == 2)
  3631. break;
  3632. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3633. sd->special_state.bonus_coma = 1;
  3634. break;
  3635. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3636. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3637. if(sd->state.lr_flag == 2)
  3638. break;
  3639. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3640. sd->special_state.bonus_coma = 1;
  3641. break;
  3642. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3643. if(sd->state.lr_flag != 2)
  3644. sd->indexed_bonus.weapon_atk[type2]+=val;
  3645. break;
  3646. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3647. if(sd->state.lr_flag != 2)
  3648. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3649. break;
  3650. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3651. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3652. if(sd->state.lr_flag != 2)
  3653. sd->indexed_bonus.critaddrace[type2] += val*10;
  3654. break;
  3655. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3656. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3657. if(sd->state.lr_flag != 2)
  3658. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3659. break;
  3660. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3661. if(sd->state.lr_flag == 2)
  3662. break;
  3663. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3664. ShowWarning("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3665. break;
  3666. }
  3667. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3668. break;
  3669. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3670. if(sd->state.lr_flag == 2)
  3671. break;
  3672. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3673. ShowWarning("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3674. break;
  3675. }
  3676. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3677. break;
  3678. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3679. if(sd->state.lr_flag == 2)
  3680. break;
  3681. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3682. ShowWarning("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3683. break;
  3684. }
  3685. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3686. break;
  3687. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3688. if(sd->state.lr_flag == 2)
  3689. break;
  3690. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3691. ShowWarning("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3692. break;
  3693. }
  3694. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3695. break;
  3696. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3697. if(sd->state.lr_flag != 2) {
  3698. sd->hp_loss.value = type2;
  3699. sd->hp_loss.rate = val;
  3700. }
  3701. break;
  3702. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3703. if(sd->state.lr_flag != 2) {
  3704. sd->hp_regen.value = type2;
  3705. sd->hp_regen.rate = val;
  3706. }
  3707. break;
  3708. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3709. if (sd->state.lr_flag != 2) {
  3710. sd->percent_hp_regen.value = type2;
  3711. sd->percent_hp_regen.rate = val;
  3712. }
  3713. break;
  3714. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3715. if (sd->state.lr_flag != 2) {
  3716. sd->percent_sp_regen.value = type2;
  3717. sd->percent_sp_regen.rate = val;
  3718. }
  3719. break;
  3720. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3721. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3722. if(sd->state.lr_flag != 2)
  3723. sd->right_weapon.addrace2[type2] += val;
  3724. else
  3725. sd->left_weapon.addrace2[type2] += val;
  3726. break;
  3727. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3728. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3729. if(sd->state.lr_flag != 2)
  3730. sd->indexed_bonus.subsize[type2]+=val;
  3731. break;
  3732. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  3733. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  3734. if (sd->state.lr_flag != 2)
  3735. sd->indexed_bonus.weapon_subsize[type2] += val;
  3736. break;
  3737. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3738. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3739. if(sd->state.lr_flag != 2)
  3740. sd->indexed_bonus.magic_subsize[type2]+=val;
  3741. break;
  3742. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3743. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3744. if(sd->state.lr_flag != 2)
  3745. sd->indexed_bonus.subrace2[type2]+=val;
  3746. break;
  3747. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3748. if(sd->state.lr_flag == 2)
  3749. break;
  3750. if( !item_db.exists( type2 ) ){
  3751. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3752. break;
  3753. }
  3754. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3755. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3756. break;
  3757. }
  3758. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3759. break;
  3760. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3761. if (sd->state.lr_flag == 2)
  3762. break;
  3763. if (!type2 || !itemdb_group.exists(type2)) {
  3764. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3765. break;
  3766. }
  3767. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  3768. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3769. break;
  3770. }
  3771. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3772. break;
  3773. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3774. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3775. if(sd->state.lr_flag != 2)
  3776. sd->indexed_bonus.expaddrace[type2]+=val;
  3777. break;
  3778. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3779. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3780. if(sd->state.lr_flag != 2)
  3781. sd->indexed_bonus.expaddclass[type2]+=val;
  3782. break;
  3783. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3784. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3785. if(sd->state.lr_flag != 2)
  3786. sd->indexed_bonus.sp_gain_race[type2]+=val;
  3787. break;
  3788. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3789. if (sd->state.lr_flag != 2)
  3790. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3791. break;
  3792. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3793. if (sd->state.lr_flag != 2)
  3794. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3795. break;
  3796. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3797. if(sd->state.lr_flag != 2) {
  3798. sd->sp_loss.value = type2;
  3799. sd->sp_loss.rate = val;
  3800. }
  3801. break;
  3802. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3803. if(sd->state.lr_flag != 2) {
  3804. sd->sp_regen.value = type2;
  3805. sd->sp_regen.rate = val;
  3806. }
  3807. break;
  3808. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3809. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3810. if(!sd->state.lr_flag) {
  3811. sd->right_weapon.hp_drain_race[type2] += val;
  3812. }
  3813. else if(sd->state.lr_flag == 1) {
  3814. sd->left_weapon.hp_drain_race[type2] += val;
  3815. }
  3816. break;
  3817. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3818. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3819. if(!sd->state.lr_flag) {
  3820. sd->right_weapon.sp_drain_race[type2] += val;
  3821. }
  3822. else if(sd->state.lr_flag == 1) {
  3823. sd->left_weapon.sp_drain_race[type2] += val;
  3824. }
  3825. break;
  3826. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3827. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3828. if(!sd->state.lr_flag) {
  3829. sd->right_weapon.hp_drain_class[type2] += val;
  3830. }
  3831. else if(sd->state.lr_flag == 1) {
  3832. sd->left_weapon.hp_drain_class[type2] += val;
  3833. }
  3834. break;
  3835. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3836. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3837. if(!sd->state.lr_flag) {
  3838. sd->right_weapon.sp_drain_class[type2] += val;
  3839. }
  3840. else if(sd->state.lr_flag == 1) {
  3841. sd->left_weapon.sp_drain_class[type2] += val;
  3842. }
  3843. break;
  3844. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3845. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3846. if(sd->state.lr_flag != 2)
  3847. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  3848. break;
  3849. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3850. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3851. if(sd->state.lr_flag != 2)
  3852. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  3853. break;
  3854. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3855. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3856. if(sd->state.lr_flag != 2)
  3857. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  3858. break;
  3859. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3860. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3861. if (sd->state.lr_flag != 2)
  3862. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  3863. break;
  3864. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3865. if(sd->state.lr_flag == 2)
  3866. break;
  3867. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3868. ShowWarning("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3869. break;
  3870. }
  3871. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3872. break;
  3873. case SP_SKILL_DELAY:
  3874. if(sd->state.lr_flag == 2)
  3875. break;
  3876. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3877. ShowWarning("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3878. break;
  3879. }
  3880. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3881. break;
  3882. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3883. if(sd->state.lr_flag == 2)
  3884. break;
  3885. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3886. ShowWarning("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3887. break;
  3888. }
  3889. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3890. break;
  3891. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3892. if (sd->state.lr_flag == 2)
  3893. break;
  3894. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3895. ShowWarning("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3896. break;
  3897. }
  3898. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3899. break;
  3900. #ifdef RENEWAL_CAST
  3901. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3902. if(sd->state.lr_flag == 2)
  3903. break;
  3904. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3905. ShowWarning("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3906. break;
  3907. }
  3908. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3909. break;
  3910. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3911. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3912. if(sd->state.lr_flag == 2)
  3913. break;
  3914. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3915. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3916. break;
  3917. }
  3918. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3919. break;
  3920. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3921. if(sd->state.lr_flag == 2)
  3922. break;
  3923. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3924. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3925. break;
  3926. }
  3927. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3928. break;
  3929. #else
  3930. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3931. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3932. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3933. break;
  3934. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3935. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3936. if(sd->state.lr_flag == 2)
  3937. break;
  3938. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3939. ShowWarning("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", MAX_PC_BONUS, type2, val);
  3940. break;
  3941. }
  3942. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3943. break;
  3944. #endif
  3945. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3946. if(sd->state.lr_flag == 2)
  3947. break;
  3948. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3949. ShowWarning("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3950. break;
  3951. }
  3952. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3953. break;
  3954. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3955. if (sd->subskill.size() == MAX_PC_BONUS) {
  3956. ShowWarning("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3957. break;
  3958. }
  3959. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3960. break;
  3961. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3962. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3963. sd->indexed_bonus.subdefele[type2] += val;
  3964. break;
  3965. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3966. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3967. sd->indexed_bonus.coma_class[type2] += val;
  3968. sd->special_state.bonus_coma = 1;
  3969. break;
  3970. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3971. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3972. sd->indexed_bonus.coma_race[type2] += val;
  3973. sd->special_state.bonus_coma = 1;
  3974. break;
  3975. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3976. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3977. if(sd->state.lr_flag != 2)
  3978. sd->indexed_bonus.magic_addrace2[type2] += val;
  3979. break;
  3980. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3981. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3982. if (sd->state.lr_flag != 2)
  3983. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  3984. break;
  3985. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3986. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3987. if (sd->state.lr_flag != 2)
  3988. sd->indexed_bonus.dropaddrace[type2] += val;
  3989. break;
  3990. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3991. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3992. if (sd->state.lr_flag != 2)
  3993. sd->indexed_bonus.dropaddclass[type2] += val;
  3994. break;
  3995. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  3996. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  3997. sd->indexed_bonus.magic_subdefele[type2] += val;
  3998. break;
  3999. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4000. if( sd->state.lr_flag == 2 ){
  4001. break;
  4002. }
  4003. if( !item_db.exists( type2 ) ){
  4004. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4005. break;
  4006. }
  4007. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4008. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4009. break;
  4010. }
  4011. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4012. break;
  4013. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4014. if( sd->state.lr_flag == 2 ){
  4015. break;
  4016. }
  4017. if( !type2 || !itemdb_group.exists( type2 ) ){
  4018. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4019. break;
  4020. }
  4021. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4022. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4023. break;
  4024. }
  4025. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4026. break;
  4027. default:
  4028. if (current_equip_combo_pos > 0) {
  4029. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%u\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4030. }
  4031. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4032. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%u\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4033. }
  4034. else {
  4035. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4036. }
  4037. break;
  4038. }
  4039. }
  4040. /**
  4041. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4042. * @param sd
  4043. * @param type Bonus type used by bBonusName
  4044. * @param type2
  4045. * @param type3
  4046. * @param val Value that usually for rate or fixed value
  4047. */
  4048. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  4049. {
  4050. nullpo_retv(sd);
  4051. switch(type){
  4052. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4053. if(sd->state.lr_flag != 2)
  4054. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4055. break;
  4056. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4057. if(sd->state.lr_flag != 2)
  4058. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4059. break;
  4060. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4061. if(sd->state.lr_flag != 2)
  4062. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4063. break;
  4064. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4065. if(sd->state.lr_flag != 2)
  4066. {
  4067. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4068. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4069. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4070. }
  4071. break;
  4072. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4073. if(sd->state.lr_flag != 2)
  4074. {
  4075. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4076. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4077. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4078. }
  4079. break;
  4080. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4081. if (sd->state.lr_flag != 2)
  4082. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4083. break;
  4084. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4085. if (sd->state.lr_flag != 2)
  4086. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4087. break;
  4088. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4089. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4090. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4091. break;
  4092. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4093. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4094. if(sd->state.lr_flag != 2)
  4095. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4096. break;
  4097. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4098. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4099. if( sd->state.lr_flag != 2 )
  4100. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4101. break;
  4102. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4103. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4104. if (sd->state.lr_flag != 2)
  4105. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4106. break;
  4107. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4108. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4109. if (sd->state.lr_flag != 2)
  4110. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4111. break;
  4112. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4113. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4114. if (sd->state.lr_flag != 2)
  4115. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4116. break;
  4117. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4118. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4119. if(sd->state.lr_flag != 2) {
  4120. sd->sp_vanish_race[type2].rate += type3;
  4121. sd->sp_vanish_race[type2].per += val;
  4122. }
  4123. break;
  4124. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4125. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4126. if(sd->state.lr_flag != 2) {
  4127. sd->hp_vanish_race[type2].rate += type3;
  4128. sd->hp_vanish_race[type2].per += val;
  4129. }
  4130. break;
  4131. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4132. if(sd->state.lr_flag != 2) {
  4133. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4134. }
  4135. break;
  4136. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4137. if(sd->state.lr_flag != 2) {
  4138. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4139. }
  4140. break;
  4141. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4142. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4143. if (sd->state.lr_flag == 2)
  4144. break;
  4145. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4146. //if (type3 > sd->norecover_state_race[type2].rate) {
  4147. // sd->norecover_state_race[type2].rate = type3;
  4148. // sd->norecover_state_race[type2].tick = val;
  4149. // break;
  4150. //}
  4151. sd->norecover_state_race[type2].rate = type3;
  4152. sd->norecover_state_race[type2].tick = val;
  4153. break;
  4154. default:
  4155. if (current_equip_combo_pos > 0) {
  4156. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%u\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4157. }
  4158. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4159. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%u\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4160. }
  4161. else {
  4162. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4163. }
  4164. break;
  4165. }
  4166. }
  4167. /**
  4168. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4169. * @param sd
  4170. * @param type Bonus type used by bBonusName
  4171. * @param type2
  4172. * @param type3
  4173. * @param type4
  4174. * @param val Value that usually for rate or fixed value
  4175. */
  4176. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4177. {
  4178. nullpo_retv(sd);
  4179. switch(type){
  4180. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4181. if(sd->state.lr_flag != 2)
  4182. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4183. break;
  4184. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4185. if(sd->state.lr_flag != 2)
  4186. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4187. break;
  4188. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4189. if(sd->state.lr_flag != 2)
  4190. {
  4191. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4192. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4193. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4194. }
  4195. break;
  4196. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4197. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4198. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4199. break;
  4200. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4201. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4202. if (sd->state.lr_flag != 2)
  4203. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4204. break;
  4205. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4206. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4207. if( sd->state.lr_flag != 2 )
  4208. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4209. break;
  4210. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4211. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4212. if(sd->state.lr_flag == 2)
  4213. break;
  4214. sd->def_set_race[type2].rate = type3;
  4215. sd->def_set_race[type2].tick = type4;
  4216. sd->def_set_race[type2].value = val;
  4217. break;
  4218. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4219. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4220. if(sd->state.lr_flag == 2)
  4221. break;
  4222. sd->mdef_set_race[type2].rate = type3;
  4223. sd->mdef_set_race[type2].tick = type4;
  4224. sd->mdef_set_race[type2].value = val;
  4225. break;
  4226. default:
  4227. if (current_equip_combo_pos > 0) {
  4228. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4229. }
  4230. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4231. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%u\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4232. }
  4233. else {
  4234. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4235. }
  4236. break;
  4237. }
  4238. }
  4239. /**
  4240. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4241. * @param sd
  4242. * @param type Bonus type used by bBonusName
  4243. * @param type2
  4244. * @param type3
  4245. * @param type4
  4246. * @param val Value that usually for rate or fixed value
  4247. */
  4248. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4249. {
  4250. nullpo_retv(sd);
  4251. switch(type){
  4252. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4253. if(sd->state.lr_flag != 2)
  4254. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4255. break;
  4256. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4257. if(sd->state.lr_flag != 2)
  4258. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4259. break;
  4260. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4261. if(sd->state.lr_flag != 2)
  4262. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4263. break;
  4264. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4265. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4266. if( sd->state.lr_flag != 2 )
  4267. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4268. break;
  4269. default:
  4270. if (current_equip_combo_pos > 0) {
  4271. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4272. }
  4273. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4274. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%u\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4275. }
  4276. else {
  4277. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4278. }
  4279. break;
  4280. }
  4281. }
  4282. /*==========================================
  4283. * Grants a player a given skill. Flag values are:
  4284. * 0 - Grant permanent skill to be bound to skill tree
  4285. * 1 - Grant an item skill (temporary)
  4286. * 2 - Like 1, except the level granted can stack with previously learned level.
  4287. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4288. *------------------------------------------*/
  4289. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4290. uint16 idx = 0;
  4291. nullpo_ret(sd);
  4292. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4293. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4294. return false;
  4295. }
  4296. if (level > MAX_SKILL_LEVEL) {
  4297. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4298. return false;
  4299. }
  4300. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4301. 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);
  4302. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4303. }
  4304. switch (type) {
  4305. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4306. sd->status.skill[idx].id = skill_id;
  4307. sd->status.skill[idx].lv = level;
  4308. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4309. if (level == 0) { //Remove skill.
  4310. sd->status.skill[idx].id = 0;
  4311. clif_deleteskill(sd,skill_id);
  4312. } else
  4313. clif_addskill(sd,skill_id);
  4314. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4315. status_calc_pc(sd, SCO_NONE);
  4316. break;
  4317. case ADDSKILL_TEMP: //Item bonus skill.
  4318. if (sd->status.skill[idx].id != 0) {
  4319. if (sd->status.skill[idx].lv >= level)
  4320. return true;
  4321. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4322. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4323. } else {
  4324. sd->status.skill[idx].id = skill_id;
  4325. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4326. }
  4327. sd->status.skill[idx].lv = level;
  4328. break;
  4329. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4330. if (sd->status.skill[idx].id != 0) {
  4331. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4332. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4333. } else {
  4334. sd->status.skill[idx].id = skill_id;
  4335. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4336. }
  4337. sd->status.skill[idx].lv += level;
  4338. break;
  4339. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4340. sd->status.skill[idx].id = skill_id;
  4341. sd->status.skill[idx].lv = level;
  4342. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4343. if (level == 0) { //Remove skill.
  4344. sd->status.skill[idx].id = 0;
  4345. clif_deleteskill(sd,skill_id);
  4346. } else
  4347. clif_addskill(sd,skill_id);
  4348. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4349. status_calc_pc(sd, SCO_NONE);
  4350. break;
  4351. default:
  4352. return false;
  4353. }
  4354. return true;
  4355. }
  4356. /*==========================================
  4357. * Append a card to an item ?
  4358. *------------------------------------------*/
  4359. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4360. {
  4361. nullpo_ret(sd);
  4362. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4363. return 0;
  4364. }
  4365. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4366. return 0;
  4367. }
  4368. int i;
  4369. t_itemid nameid;
  4370. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4371. struct item_data* item_card = sd->inventory_data[idx_card];
  4372. if(item_eq == nullptr)
  4373. return 0; //Invalid item index.
  4374. if(item_card == nullptr)
  4375. return 0; //Invalid card index.
  4376. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4377. return 0; // target item missing
  4378. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4379. return 0; // target card missing
  4380. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4381. return 0; // only weapons and armor are allowed
  4382. if( item_card->type != IT_CARD )
  4383. return 0; // must be a card
  4384. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4385. return 0; // target must be identified
  4386. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4387. return 0; // card slots reserved for other purposes
  4388. if( (item_eq->equip & item_card->equip) == 0 )
  4389. return 0; // card cannot be compounded on this item type
  4390. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4391. return 0; // attempted to place shield card on left-hand weapon.
  4392. if( item_eq->type == IT_ARMOR && (item_card->equip & EQP_ACC) && ((item_card->equip & EQP_ACC) != EQP_ACC) && ((item_eq->equip & EQP_ACC) != (item_card->equip & EQP_ACC)) )
  4393. return 0; // specific accessory-card can only be inserted to specific accessory.
  4394. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4395. return 0; // item must be unequipped
  4396. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4397. if( i == item_eq->slots )
  4398. return 0; // no free slots
  4399. // remember the card id to insert
  4400. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4401. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4402. {// failed
  4403. clif_insert_card(sd,idx_equip,idx_card,1);
  4404. }
  4405. else
  4406. {// success
  4407. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4408. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4409. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4410. clif_insert_card(sd,idx_equip,idx_card,0);
  4411. }
  4412. return 0;
  4413. }
  4414. /**
  4415. * Returns the count of unidentified items with the option to identify too.
  4416. * @param sd: Player data
  4417. * @param identify_item: Whether or not to identify any unidentified items
  4418. * @return Unidentified items count
  4419. */
  4420. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4421. {
  4422. int unidentified_count = 0;
  4423. for (int i = 0; i < MAX_INVENTORY; i++) {
  4424. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4425. if (identify_item == true) {
  4426. sd->inventory.u.items_inventory[i].identify = 1;
  4427. clif_item_identified(sd,i,0);
  4428. }
  4429. unidentified_count++;
  4430. }
  4431. }
  4432. return unidentified_count;
  4433. }
  4434. //
  4435. // Items
  4436. //
  4437. /*==========================================
  4438. * Update buying value by skills
  4439. *------------------------------------------*/
  4440. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4441. {
  4442. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4443. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4444. rate1 = 5+skill*2-((skill==10)? 1:0);
  4445. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4446. rate2 = 5+skill*4;
  4447. if(rate1 < rate2) rate1 = rate2;
  4448. if(rate1)
  4449. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4450. if(val < battle_config.min_shop_buy)
  4451. val = battle_config.min_shop_buy;
  4452. return val;
  4453. }
  4454. /*==========================================
  4455. * Update selling value by skills
  4456. *------------------------------------------*/
  4457. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4458. {
  4459. int skill,val = orig_value,rate = 0;
  4460. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4461. rate = 5+skill*2-((skill==10)? 1:0);
  4462. if(rate)
  4463. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4464. if (val < battle_config.min_shop_sell)
  4465. val = battle_config.min_shop_sell;
  4466. return val;
  4467. }
  4468. /*==========================================
  4469. * Checking if we have enough place on inventory for new item
  4470. * Make sure to take 30k as limit (for client I guess)
  4471. * @param sd
  4472. * @param nameid
  4473. * @param amount
  4474. * @return e_chkitem_result
  4475. *------------------------------------------*/
  4476. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4477. {
  4478. int i;
  4479. struct item_data* data;
  4480. nullpo_ret(sd);
  4481. if(amount > MAX_AMOUNT)
  4482. return CHKADDITEM_OVERAMOUNT;
  4483. data = itemdb_search(nameid);
  4484. if(!itemdb_isstackable2(data))
  4485. return CHKADDITEM_NEW;
  4486. if( data->stack.inventory && amount > data->stack.amount )
  4487. return CHKADDITEM_OVERAMOUNT;
  4488. if (data->flag.guid)
  4489. return CHKADDITEM_NEW;
  4490. for(i=0;i<MAX_INVENTORY;i++){
  4491. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4492. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4493. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4494. return CHKADDITEM_OVERAMOUNT;
  4495. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4496. if( i >= sd->status.inventory_slots ){
  4497. return CHKADDITEM_OVERAMOUNT;
  4498. }
  4499. return CHKADDITEM_EXIST;
  4500. }
  4501. }
  4502. return CHKADDITEM_NEW;
  4503. }
  4504. /*==========================================
  4505. * Return number of available place in inventory
  4506. * Each non stackable item will reduce place by 1
  4507. * @param sd
  4508. * @return Number of empty slots
  4509. *------------------------------------------*/
  4510. uint8 pc_inventoryblank(struct map_session_data *sd)
  4511. {
  4512. uint16 i;
  4513. uint8 b;
  4514. nullpo_ret(sd);
  4515. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4516. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4517. b++;
  4518. }
  4519. return b;
  4520. }
  4521. /**
  4522. * Attempts to remove zeny from player
  4523. * @param sd: Player
  4524. * @param zeny: Zeny removed
  4525. * @param type: Log type
  4526. * @param tsd: (optional) From who to log (if null take sd)
  4527. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4528. */
  4529. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4530. {
  4531. nullpo_retr(2,sd);
  4532. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4533. if( zeny < 0 )
  4534. {
  4535. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4536. return 1;
  4537. }
  4538. if( sd->status.zeny < zeny )
  4539. return 1; //Not enough.
  4540. sd->status.zeny -= zeny;
  4541. clif_updatestatus(sd,SP_ZENY);
  4542. if(!tsd) tsd = sd;
  4543. log_zeny(sd, type, tsd, -zeny);
  4544. if( zeny > 0 && sd->state.showzeny ) {
  4545. char output[255];
  4546. sprintf(output, "Removed %dz.", zeny);
  4547. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4548. }
  4549. return 0;
  4550. }
  4551. /**
  4552. * Attempts to give zeny to player
  4553. * @param sd: Player
  4554. * @param type: Log type
  4555. * @param tsd: (optional) From who to log (if null take sd)
  4556. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4557. */
  4558. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4559. {
  4560. nullpo_retr(-1,sd);
  4561. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4562. if( zeny < 0 )
  4563. {
  4564. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4565. return 1;
  4566. }
  4567. if( zeny > MAX_ZENY - sd->status.zeny )
  4568. zeny = MAX_ZENY - sd->status.zeny;
  4569. sd->status.zeny += zeny;
  4570. clif_updatestatus(sd,SP_ZENY);
  4571. if(!tsd) tsd = sd;
  4572. log_zeny(sd, type, tsd, zeny);
  4573. if( zeny > 0 && sd->state.showzeny ) {
  4574. char output[255];
  4575. sprintf(output, "Gained %dz.", zeny);
  4576. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4577. }
  4578. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4579. return 0;
  4580. }
  4581. /**
  4582. * Attempts to remove Cash Points from player
  4583. * @param sd: Player
  4584. * @param price: Total points (cash + kafra) the player has to pay
  4585. * @param points: Kafra points the player has to pay
  4586. * @param type: Log type
  4587. * @return -1: Not enough points, otherwise success (cash+points)
  4588. */
  4589. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4590. {
  4591. int cash;
  4592. nullpo_retr(-1,sd);
  4593. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4594. cash = price-points;
  4595. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4596. {
  4597. 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);
  4598. return -1;
  4599. }
  4600. if( cash ){
  4601. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4602. sd->cashPoints -= cash;
  4603. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4604. }
  4605. if( points ){
  4606. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4607. sd->kafraPoints -= points;
  4608. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4609. }
  4610. if( battle_config.cashshop_show_points )
  4611. {
  4612. char output[CHAT_SIZE_MAX];
  4613. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints); // Used %d kafra points and %d cash points. %d kafra and %d cash points remaining.
  4614. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4615. }
  4616. return cash+points;
  4617. }
  4618. /**
  4619. * Attempts to give Cash Points to player
  4620. * @param sd: Player
  4621. * @param cash: Cash points the player gets
  4622. * @param points: Kafra points the player gets
  4623. * @param type: Log type
  4624. * @return -1: Error, otherwise success (cash or points)
  4625. */
  4626. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4627. {
  4628. char output[CHAT_SIZE_MAX];
  4629. nullpo_retr(-1,sd);
  4630. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4631. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4632. if( cash > 0 )
  4633. {
  4634. if( cash > MAX_ZENY-sd->cashPoints )
  4635. {
  4636. 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);
  4637. cash = MAX_ZENY-sd->cashPoints;
  4638. }
  4639. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4640. sd->cashPoints += cash;
  4641. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4642. if( battle_config.cashshop_show_points )
  4643. {
  4644. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4645. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4646. }
  4647. return cash;
  4648. }
  4649. if( points > 0 )
  4650. {
  4651. if( points > MAX_ZENY-sd->kafraPoints )
  4652. {
  4653. 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);
  4654. points = MAX_ZENY-sd->kafraPoints;
  4655. }
  4656. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4657. sd->kafraPoints += points;
  4658. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4659. if( battle_config.cashshop_show_points )
  4660. {
  4661. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4662. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4663. }
  4664. return points;
  4665. }
  4666. return -1; //shouldn't happen but just in case
  4667. }
  4668. /**
  4669. * Searching a specified itemid in inventory and return his stored index
  4670. * @param sd Player
  4671. * @param nameid Find this Item!
  4672. * @return Stored index in inventory, or -1 if not found.
  4673. **/
  4674. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4675. short i;
  4676. nullpo_retr(-1, sd);
  4677. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4678. return ( i < MAX_INVENTORY ) ? i : -1;
  4679. }
  4680. /** Attempt to add a new item to player inventory
  4681. * @param sd
  4682. * @param item_data
  4683. * @param amount
  4684. * @param log_type
  4685. * @return
  4686. * 0 = success
  4687. * 1 = invalid itemid not found or negative amount
  4688. * 2 = overweight
  4689. * 3 = ?
  4690. * 4 = no free place found
  4691. * 5 = max amount reached
  4692. * 6 = ?
  4693. * 7 = stack limitation
  4694. */
  4695. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4696. struct item_data *id;
  4697. int16 i;
  4698. unsigned int w;
  4699. nullpo_retr(ADDITEM_INVALID, sd);
  4700. nullpo_retr(ADDITEM_INVALID, item);
  4701. if( item->nameid == 0 || amount <= 0 )
  4702. return ADDITEM_INVALID;
  4703. if( amount > MAX_AMOUNT )
  4704. return ADDITEM_OVERAMOUNT;
  4705. id = itemdb_search(item->nameid);
  4706. if( id->stack.inventory && amount > id->stack.amount )
  4707. {// item stack limitation
  4708. return ADDITEM_STACKLIMIT;
  4709. }
  4710. w = id->weight*amount;
  4711. if(sd->weight + w > sd->max_weight)
  4712. return ADDITEM_OVERWEIGHT;
  4713. if (id->flag.guid && !item->unique_id)
  4714. item->unique_id = pc_generate_unique_id(sd);
  4715. // Stackable | Non Rental
  4716. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4717. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4718. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4719. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4720. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4721. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4722. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4723. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4724. return ADDITEM_OVERAMOUNT;
  4725. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4726. if( i >= sd->status.inventory_slots ){
  4727. return ADDITEM_OVERAMOUNT;
  4728. }
  4729. sd->inventory.u.items_inventory[i].amount += amount;
  4730. clif_additem(sd,i,amount,0);
  4731. break;
  4732. }
  4733. }
  4734. }else{
  4735. i = MAX_INVENTORY;
  4736. }
  4737. if (i >= MAX_INVENTORY) {
  4738. i = pc_search_inventory(sd,0);
  4739. if( i < 0 )
  4740. return ADDITEM_OVERITEM;
  4741. if( i >= sd->status.inventory_slots ){
  4742. return ADDITEM_OVERITEM;
  4743. }
  4744. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4745. // clear equip and favorite fields first, just in case
  4746. if( item->equip )
  4747. sd->inventory.u.items_inventory[i].equip = 0;
  4748. if( item->favorite )
  4749. sd->inventory.u.items_inventory[i].favorite = 0;
  4750. if( item->equipSwitch )
  4751. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4752. sd->inventory.u.items_inventory[i].amount = amount;
  4753. sd->inventory_data[i] = id;
  4754. sd->last_addeditem_index = i;
  4755. if (!itemdb_isstackable2(id) || id->flag.guid)
  4756. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4757. clif_additem(sd,i,amount,0);
  4758. }
  4759. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4760. sd->weight += w;
  4761. clif_updatestatus(sd,SP_WEIGHT);
  4762. //Auto-equip
  4763. if(id->flag.autoequip)
  4764. pc_equipitem(sd, i, id->equip);
  4765. /* rental item check */
  4766. if( item->expire_time ) {
  4767. if( time(NULL) > item->expire_time ) {
  4768. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4769. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4770. } else {
  4771. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4772. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4773. pc_inventory_rental_add(sd, seconds);
  4774. }
  4775. }
  4776. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4777. pc_show_questinfo(sd);
  4778. return ADDITEM_SUCCESS;
  4779. }
  4780. /*==========================================
  4781. * Remove an item at index n from inventory by amount.
  4782. * @param sd
  4783. * @param n Item index in inventory
  4784. * @param amount
  4785. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4786. * @param reason Delete reason
  4787. * @param log_type e_log_pick_type
  4788. * @return 1 - invalid itemid or negative amount; 0 - Success
  4789. *------------------------------------------*/
  4790. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4791. {
  4792. nullpo_retr(1, sd);
  4793. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4794. return 1;
  4795. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4796. sd->inventory.u.items_inventory[n].amount -= amount;
  4797. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4798. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4799. if(sd->inventory.u.items_inventory[n].equip)
  4800. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4801. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4802. sd->inventory_data[n] = NULL;
  4803. }
  4804. if(!(type&1))
  4805. clif_delitem(sd,n,amount,reason);
  4806. if(!(type&2))
  4807. clif_updatestatus(sd,SP_WEIGHT);
  4808. pc_show_questinfo(sd);
  4809. return 0;
  4810. }
  4811. /*==========================================
  4812. * Attempt to drop an item.
  4813. * @param sd
  4814. * @param n Item index in inventory
  4815. * @param amount Amount of item
  4816. * @return False = fail; True = success
  4817. *------------------------------------------*/
  4818. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4819. {
  4820. nullpo_retr(1, sd);
  4821. if(n < 0 || n >= MAX_INVENTORY)
  4822. return false;
  4823. if(amount <= 0)
  4824. return false;
  4825. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  4826. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4827. sd->inventory.u.items_inventory[n].amount < amount ||
  4828. sd->state.trading || sd->state.vending ||
  4829. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4830. )
  4831. return false;
  4832. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4833. return false;
  4834. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4835. {
  4836. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4837. return false; //Can't drop items in nodrop mapflag maps.
  4838. }
  4839. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4840. {
  4841. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4842. return false;
  4843. }
  4844. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4845. return false;
  4846. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4847. clif_dropitem(sd, n, amount);
  4848. return true;
  4849. }
  4850. /*==========================================
  4851. * Attempt to pick up an item.
  4852. * @param sd
  4853. * @param fitem Item that will be picked
  4854. * @return False = fail; True = success
  4855. *------------------------------------------*/
  4856. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4857. {
  4858. int flag = 0;
  4859. t_tick tick = gettick();
  4860. struct party_data *p = NULL;
  4861. nullpo_ret(sd);
  4862. nullpo_ret(fitem);
  4863. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4864. return false; // Distance is too far
  4865. if (sd->sc.cant.pickup)
  4866. return false;
  4867. if (sd->status.party_id)
  4868. p = party_search(sd->status.party_id);
  4869. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4870. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4871. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4872. if (!(p && p->party.item&1 &&
  4873. first_sd && first_sd->status.party_id == sd->status.party_id
  4874. ))
  4875. return false;
  4876. }
  4877. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4878. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4879. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4880. if (!(p && p->party.item&1 &&
  4881. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4882. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4883. ))
  4884. return false;
  4885. }
  4886. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4887. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4888. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4889. if(!(p && p->party.item&1 &&
  4890. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4891. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4892. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4893. ))
  4894. return false;
  4895. }
  4896. }
  4897. }
  4898. }
  4899. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4900. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4901. clif_additem(sd,0,0,flag);
  4902. return true;
  4903. }
  4904. //Display pickup animation.
  4905. pc_stop_attack(sd);
  4906. clif_takeitem(&sd->bl,&fitem->bl);
  4907. if (fitem->mob_id &&
  4908. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4909. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4910. )
  4911. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4912. map_clearflooritem(&fitem->bl);
  4913. return true;
  4914. }
  4915. /*==========================================
  4916. * Check if item is usable.
  4917. * Return:
  4918. * false = no
  4919. * true = yes
  4920. *------------------------------------------*/
  4921. bool pc_isUseitem(struct map_session_data *sd,int n)
  4922. {
  4923. struct item_data *item;
  4924. t_itemid nameid;
  4925. nullpo_ret(sd);
  4926. item = sd->inventory_data[n];
  4927. nameid = sd->inventory.u.items_inventory[n].nameid;
  4928. if( item == NULL )
  4929. return false;
  4930. //Not consumable item
  4931. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4932. return false;
  4933. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4934. return true;
  4935. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4936. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4937. return false;
  4938. //Prevent mass item usage. [Skotlex]
  4939. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4940. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4941. )
  4942. return false;
  4943. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4944. clif_msg(sd,ITEM_NOUSE_SITTING);
  4945. return false; // You cannot use this item while sitting.
  4946. }
  4947. if (sd->state.storage_flag && item->type != IT_CASH) {
  4948. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4949. return false; // You cannot use this item while storage is open.
  4950. }
  4951. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4952. return false;
  4953. switch( nameid ) {
  4954. case ITEMID_WING_OF_FLY:
  4955. case ITEMID_GIANT_FLY_WING:
  4956. case ITEMID_N_FLY_WING:
  4957. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4958. clif_skill_teleportmessage(sd,0);
  4959. return false;
  4960. }
  4961. if (nameid == ITEMID_GIANT_FLY_WING) {
  4962. struct party_data *pd = party_search(sd->status.party_id);
  4963. if (pd) {
  4964. int i;
  4965. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4966. if (i == MAX_PARTY) { // User is not party leader
  4967. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4968. break;
  4969. }
  4970. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd && pd->data[i].sd != sd && pd->data[i].sd->bl.m == sd->bl.m && !pc_isdead(pd->data[i].sd));
  4971. if (i == MAX_PARTY) { // No party members found on same map
  4972. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4973. break;
  4974. }
  4975. } else {
  4976. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4977. break;
  4978. }
  4979. }
  4980. // Fall through
  4981. case ITEMID_WING_OF_BUTTERFLY:
  4982. case ITEMID_N_BUTTERFLY_WING:
  4983. case ITEMID_DUN_TELE_SCROLL1:
  4984. case ITEMID_DUN_TELE_SCROLL2:
  4985. case ITEMID_DUN_TELE_SCROLL3:
  4986. case ITEMID_WOB_RUNE:
  4987. case ITEMID_WOB_SCHWALTZ:
  4988. case ITEMID_WOB_RACHEL:
  4989. case ITEMID_WOB_LOCAL:
  4990. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4991. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4992. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4993. return false;
  4994. }
  4995. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4996. return false;
  4997. break;
  4998. case ITEMID_MERCENARY_RED_POTION:
  4999. case ITEMID_MERCENARY_BLUE_POTION:
  5000. case ITEMID_M_CENTER_POTION:
  5001. case ITEMID_M_AWAKENING_POTION:
  5002. case ITEMID_M_BERSERK_POTION:
  5003. if( sd->md == NULL || sd->md->db == NULL )
  5004. return false;
  5005. if( sd->md->sc.cant.consume )
  5006. return false;
  5007. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5008. return false;
  5009. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5010. return false;
  5011. break;
  5012. case ITEMID_NEURALIZER:
  5013. if( !mapdata->flag[MF_RESET] )
  5014. return false;
  5015. break;
  5016. }
  5017. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5018. return false; // Mercenary Scrolls
  5019. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5020. if( pc_is90overweight(sd) ) {
  5021. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5022. return false;
  5023. }
  5024. if( !pc_inventoryblank(sd) ) {
  5025. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5026. return false;
  5027. }
  5028. }
  5029. //Gender check
  5030. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5031. return false;
  5032. //Required level check
  5033. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5034. return false;
  5035. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5036. return false;
  5037. //Not equipable by class. [Skotlex]
  5038. if (!pc_job_can_use_item(sd,item))
  5039. return false;
  5040. if (sd->sc.cant.consume)
  5041. return false;
  5042. if (!pc_isItemClass(sd,item))
  5043. return false;
  5044. //Dead Branch items
  5045. if( item->flag.dead_branch )
  5046. log_branch(sd);
  5047. return true;
  5048. }
  5049. /*==========================================
  5050. * Last checks to use an item.
  5051. * Return:
  5052. * 0 = fail
  5053. * 1 = success
  5054. *------------------------------------------*/
  5055. int pc_useitem(struct map_session_data *sd,int n)
  5056. {
  5057. t_tick tick = gettick();
  5058. int amount;
  5059. t_itemid nameid;
  5060. struct script_code *script;
  5061. struct item item;
  5062. struct item_data *id;
  5063. nullpo_ret(sd);
  5064. if (sd->state.mail_writing)
  5065. return 0;
  5066. if (sd->npc_id) {
  5067. if (sd->progressbar.npc_id) {
  5068. clif_progressbar_abort(sd);
  5069. return 0; // First item use attempt cancels the progress bar
  5070. }
  5071. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5072. #ifdef RENEWAL
  5073. clif_msg( sd, WORK_IN_PROGRESS );
  5074. #endif
  5075. return 0;
  5076. }
  5077. }
  5078. item = sd->inventory.u.items_inventory[n];
  5079. id = sd->inventory_data[n];
  5080. if (item.nameid == 0 || item.amount <= 0)
  5081. return 0;
  5082. if( !pc_isUseitem(sd,n) )
  5083. return 0;
  5084. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5085. nameid = id->nameid;
  5086. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5087. return 0;
  5088. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5089. if( id->flag.delay_consume > 0 ) {
  5090. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  5091. return 0;
  5092. else if( pc_issit(sd) )
  5093. return 0;
  5094. }
  5095. //Since most delay-consume items involve using a "skill-type" target cursor,
  5096. //perform a skill-use check before going through. [Skotlex]
  5097. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5098. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5099. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5100. return 0;
  5101. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5102. return 0;
  5103. /* on restricted maps the item is consumed but the effect is not used */
  5104. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5105. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5106. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5107. clif_useitemack(sd,n,item.amount-1,true);
  5108. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5109. }
  5110. return 0;/* regardless, effect is not run */
  5111. }
  5112. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5113. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5114. return 0;
  5115. }
  5116. sd->itemid = item.nameid;
  5117. sd->itemindex = n;
  5118. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5119. sd->catch_target_class = PET_CATCH_FAIL;
  5120. amount = item.amount;
  5121. script = id->script;
  5122. //Check if the item is to be consumed immediately [Skotlex]
  5123. if (id->flag.delay_consume > 0)
  5124. clif_useitemack(sd, n, amount, true);
  5125. else
  5126. {
  5127. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5128. {
  5129. clif_useitemack(sd, n, amount - 1, true);
  5130. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5131. }
  5132. else
  5133. clif_useitemack(sd, n, 0, false);
  5134. }
  5135. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5136. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5137. //Update item use time.
  5138. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5139. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5140. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5141. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5142. potion_flag = 0;
  5143. return 1;
  5144. }
  5145. /**
  5146. * Add item on cart for given index.
  5147. * @param sd
  5148. * @param item
  5149. * @param amount
  5150. * @param log_type
  5151. * @return See pc.hpp::e_additem_result
  5152. */
  5153. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5154. {
  5155. struct item_data *data;
  5156. int i,w;
  5157. nullpo_retr(ADDITEM_INVALID, sd);
  5158. nullpo_retr(ADDITEM_INVALID, item);
  5159. if(item->nameid == 0 || amount <= 0)
  5160. return ADDITEM_INVALID;
  5161. if (itemdb_ishatched_egg(item))
  5162. return ADDITEM_INVALID;
  5163. data = itemdb_search(item->nameid);
  5164. if( data->stack.cart && amount > data->stack.amount )
  5165. {// item stack limitation
  5166. return ADDITEM_STACKLIMIT;
  5167. }
  5168. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5169. { // Check item trade restrictions [Skotlex]
  5170. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5171. return ADDITEM_INVALID;
  5172. }
  5173. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5174. return ADDITEM_OVERWEIGHT;
  5175. i = MAX_CART;
  5176. if( itemdb_isstackable2(data) && !item->expire_time )
  5177. {
  5178. for (i = 0; i < MAX_CART; i++) {
  5179. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5180. && sd->cart.u.items_cart[i].bound == item->bound
  5181. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5182. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5183. )
  5184. break;
  5185. }
  5186. }
  5187. if( i < MAX_CART )
  5188. {// item already in cart, stack it
  5189. if( amount > MAX_AMOUNT - sd->cart.u.items_cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->cart.u.items_cart[i].amount ) )
  5190. return ADDITEM_OVERAMOUNT; // no slot
  5191. sd->cart.u.items_cart[i].amount += amount;
  5192. clif_cart_additem(sd,i,amount);
  5193. }
  5194. else
  5195. {// item not stackable or not present, add it
  5196. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5197. if( i == MAX_CART )
  5198. return ADDITEM_OVERAMOUNT; // no slot
  5199. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5200. sd->cart.u.items_cart[i].id = 0;
  5201. sd->cart.u.items_cart[i].amount = amount;
  5202. sd->cart_num++;
  5203. clif_cart_additem(sd,i,amount);
  5204. }
  5205. sd->cart.u.items_cart[i].favorite = 0; // clear
  5206. sd->cart.u.items_cart[i].equipSwitch = 0;
  5207. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5208. sd->cart_weight += w;
  5209. clif_updatestatus(sd,SP_CARTINFO);
  5210. return ADDITEM_SUCCESS;
  5211. }
  5212. /*==========================================
  5213. * Delete item on cart for given index.
  5214. *------------------------------------------*/
  5215. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5216. {
  5217. nullpo_retv(sd);
  5218. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5219. sd->cart.u.items_cart[n].amount < amount)
  5220. return;
  5221. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5222. sd->cart.u.items_cart[n].amount -= amount;
  5223. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5224. if(sd->cart.u.items_cart[n].amount <= 0) {
  5225. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5226. sd->cart_num--;
  5227. }
  5228. if(!type) {
  5229. clif_cart_delitem(sd,n,amount);
  5230. clif_updatestatus(sd,SP_CARTINFO);
  5231. }
  5232. }
  5233. /*==========================================
  5234. * Transfer item from inventory to cart.
  5235. *------------------------------------------*/
  5236. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  5237. {
  5238. nullpo_retv(sd);
  5239. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5240. return;
  5241. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5242. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5243. return;
  5244. if( item_data->equipSwitch ){
  5245. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5246. return;
  5247. }
  5248. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5249. if (flag == ADDITEM_SUCCESS)
  5250. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5251. else {
  5252. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5253. clif_additem(sd, idx, amount, 0);
  5254. clif_delitem(sd, idx, amount, 0);
  5255. }
  5256. }
  5257. /*==========================================
  5258. * Get number of item in cart.
  5259. * Return:
  5260. -1 = itemid not found or no amount found
  5261. x = remaining itemid on cart after get
  5262. *------------------------------------------*/
  5263. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  5264. {
  5265. struct item* item_data;
  5266. nullpo_retr(-1, sd);
  5267. item_data = &sd->cart.u.items_cart[idx];
  5268. if( item_data->nameid == 0 || item_data->amount == 0 )
  5269. return -1;
  5270. return item_data->amount - amount;
  5271. }
  5272. /*==========================================
  5273. * Retrieve an item at index idx from cart.
  5274. *------------------------------------------*/
  5275. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5276. {
  5277. nullpo_retv(sd);
  5278. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5279. return;
  5280. struct item *item_data=&sd->cart.u.items_cart[idx];
  5281. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5282. return;
  5283. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5284. if (flag == ADDITEM_SUCCESS)
  5285. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5286. else {
  5287. clif_cart_delitem(sd, idx, amount);
  5288. clif_additem(sd, idx, amount, flag);
  5289. clif_cart_additem(sd, idx, amount);
  5290. }
  5291. }
  5292. /*==========================================
  5293. * Bound Item Check
  5294. * Type:
  5295. * 1 Account Bound
  5296. * 2 Guild Bound
  5297. * 3 Party Bound
  5298. * 4 Character Bound
  5299. *------------------------------------------*/
  5300. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5301. {
  5302. int i = 0, j = 0;
  5303. for(i = 0; i < MAX_INVENTORY; i++) {
  5304. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0 && sd->inventory.u.items_inventory[i].bound == type) {
  5305. idxlist[j] = i;
  5306. j++;
  5307. }
  5308. }
  5309. return j;
  5310. }
  5311. /*==========================================
  5312. * Display item stolen msg to player sd
  5313. *------------------------------------------*/
  5314. int pc_show_steal(struct block_list *bl,va_list ap)
  5315. {
  5316. struct map_session_data *sd;
  5317. t_itemid itemid;
  5318. struct item_data *item=NULL;
  5319. char output[100];
  5320. sd=va_arg(ap,struct map_session_data *);
  5321. itemid=va_arg(ap,int);
  5322. if((item=itemdb_exists(itemid))==NULL)
  5323. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5324. else
  5325. sprintf(output,"%s stole %s.",sd->status.name,item->ename.c_str());
  5326. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5327. return 0;
  5328. }
  5329. /**
  5330. * Steal an item from bl (mob).
  5331. * @param sd: Player data
  5332. * @param bl: Object to steal from
  5333. * @param skill_lv: Level of skill used
  5334. * @return True on success or false otherwise
  5335. */
  5336. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5337. {
  5338. int i;
  5339. t_itemid itemid;
  5340. double rate;
  5341. unsigned char flag = 0;
  5342. struct status_data *sd_status, *md_status;
  5343. struct mob_data *md;
  5344. struct item tmp_item;
  5345. if(!sd || !bl || bl->type!=BL_MOB)
  5346. return false;
  5347. md = (TBL_MOB *)bl;
  5348. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5349. return false;
  5350. sd_status= status_get_status_data(&sd->bl);
  5351. md_status= status_get_status_data(bl);
  5352. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5353. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5354. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5355. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5356. ) { //Can't steal from
  5357. md->state.steal_flag = UCHAR_MAX;
  5358. return false;
  5359. }
  5360. // base skill success chance (percentual)
  5361. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5362. rate += sd->bonus.add_steal_rate;
  5363. if( rate < 1
  5364. #ifdef RENEWAL
  5365. || rnd()%100 >= rate
  5366. #endif
  5367. )
  5368. return false;
  5369. // Try dropping one item, in the order from first to last possible slot.
  5370. // Droprate is affected by the skill success rate.
  5371. for( i = 0; i < MAX_MOB_DROP; i++ )
  5372. if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].rate
  5373. #ifndef RENEWAL
  5374. * rate/100.
  5375. #endif
  5376. )
  5377. break;
  5378. if( i == MAX_MOB_DROP )
  5379. return false;
  5380. itemid = md->db->dropitem[i].nameid;
  5381. memset(&tmp_item,0,sizeof(tmp_item));
  5382. tmp_item.nameid = itemid;
  5383. tmp_item.amount = 1;
  5384. tmp_item.identify = itemdb_isidentified(itemid);
  5385. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5386. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5387. //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?
  5388. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5389. if(flag) { //Failed to steal due to overweight
  5390. clif_additem(sd,0,0,flag);
  5391. return false;
  5392. }
  5393. if(battle_config.show_steal_in_same_party)
  5394. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5395. //Logs items, Stolen from mobs [Lupus]
  5396. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5397. //A Rare Steal Global Announce by Lupus
  5398. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5399. struct item_data *i_data;
  5400. char message[128];
  5401. i_data = itemdb_search(itemid);
  5402. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname.c_str(), i_data->ename.c_str(), (float)md->db->dropitem[i].rate / 100);
  5403. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5404. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5405. }
  5406. return true;
  5407. }
  5408. /*==========================================
  5409. * Stole zeny from bl (mob)
  5410. * return
  5411. * 0 = fail
  5412. * 1 = success
  5413. *------------------------------------------*/
  5414. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5415. {
  5416. int rate, target_lv;
  5417. struct mob_data *md;
  5418. if(!sd || !target || target->type != BL_MOB)
  5419. return 0;
  5420. md = (TBL_MOB*)target;
  5421. target_lv = status_get_lv(target);
  5422. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->sc.data[SC_HANDICAPSTATE_FROSTBITE] ||
  5423. md->sc.data[SC_HANDICAPSTATE_SWOONING] || md->sc.data[SC_HANDICAPSTATE_LIGHTNINGSTRIKE] || md->sc.data[SC_HANDICAPSTATE_CRYSTALLIZATION] ||
  5424. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5425. return 0;
  5426. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5427. if(rnd()%1000 < rate)
  5428. {
  5429. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5430. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5431. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5432. md->state.steal_coin_flag = 1;
  5433. return 1;
  5434. }
  5435. return 0;
  5436. }
  5437. /*==========================================
  5438. * Set's a player position.
  5439. * @param sd
  5440. * @param mapindex
  5441. * @param x
  5442. * @param y
  5443. * @param clrtype
  5444. * @return SETPOS_OK Success
  5445. * SETPOS_MAPINDEX Invalid map index
  5446. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5447. * SETPOS_AUTOTRADE Player is in autotrade state
  5448. *------------------------------------------*/
  5449. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5450. {
  5451. nullpo_retr(SETPOS_OK,sd);
  5452. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5453. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5454. return SETPOS_MAPINDEX;
  5455. }
  5456. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5457. return SETPOS_AUTOTRADE;
  5458. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5459. pc_setstand(sd, true);
  5460. pc_setrestartvalue(sd,1);
  5461. }
  5462. int16 m = map_mapindex2mapid(mapindex);
  5463. struct map_data *mapdata = map_getmapdata(m);
  5464. status_change *sc = status_get_sc(&sd->bl);
  5465. sd->state.changemap = (sd->mapindex != mapindex);
  5466. sd->state.warping = 1;
  5467. sd->state.workinprogress = WIP_DISABLE_NONE;
  5468. sd->state.mail_writing = false;
  5469. sd->state.refineui_open = false;
  5470. if( sd->state.changemap ) { // Misc map-changing settings
  5471. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5472. if (curr_map_instance_id != new_map_instance_id) {
  5473. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5474. instance_delusers(curr_map_instance_id);
  5475. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5476. }
  5477. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5478. instance_addusers(new_map_instance_id);
  5479. }
  5480. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5481. bg_team_leave(sd, false, true);
  5482. sd->state.pmap = sd->bl.m;
  5483. if (sc && sc->count) { // Cancel some map related stuff.
  5484. if (sc->cant.warp)
  5485. return SETPOS_MAPINDEX; // You may not get out!
  5486. for (const auto &it : status_db) {
  5487. if (sc->data[it.first]) {
  5488. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5489. status_change_end(&sd->bl, static_cast<sc_type>(it.first), INVALID_TIMER);
  5490. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5491. status_change_entry *sce = sd->sc.data[it.first];
  5492. if (sce->timer != INVALID_TIMER)
  5493. delete_timer(sce->timer, status_change_timer);
  5494. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5495. }
  5496. }
  5497. }
  5498. }
  5499. for(int i = 0; i < EQI_MAX; i++ ) {
  5500. if( sd->equip_index[i] >= 0 )
  5501. if( pc_isequip(sd,sd->equip_index[i]) )
  5502. pc_unequipitem(sd,sd->equip_index[i],2);
  5503. }
  5504. if (battle_config.clear_unit_onwarp&BL_PC)
  5505. skill_clear_unitgroup(&sd->bl);
  5506. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5507. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5508. }
  5509. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5510. guild_send_dot_remove(sd);
  5511. bg_send_dot_remove(sd);
  5512. if (sd->regen.state.gc)
  5513. sd->regen.state.gc = 0;
  5514. // make sure vending is allowed here
  5515. if (sd->state.vending && mapdata && mapdata->flag[MF_NOVENDING]) {
  5516. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5517. vending_closevending(sd);
  5518. }
  5519. channel_pcquit(sd,4); //quit map chan
  5520. // Remove Cloaked NPC if changing to another map
  5521. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5522. block_list *npc_bl = map_id2bl(*it);
  5523. if (npc_bl && npc_bl->m != m) {
  5524. sd->cloaked_npc.erase(it);
  5525. break;
  5526. }
  5527. }
  5528. }
  5529. if( m < 0 )
  5530. {
  5531. uint32 ip;
  5532. uint16 port;
  5533. struct script_state *st;
  5534. //if can't find any map-servers, just abort setting position.
  5535. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5536. return SETPOS_NO_MAPSERVER;
  5537. if (sd->npc_id){
  5538. npc_event_dequeue(sd,false);
  5539. st = sd->st;
  5540. }else{
  5541. st = nullptr;
  5542. }
  5543. if (sd->bg_id) // Switching map servers, remove from bg
  5544. bg_team_leave(sd, false, true);
  5545. if (sd->state.vending) // Stop vending
  5546. vending_closevending(sd);
  5547. npc_script_event(sd, NPCE_LOGOUT);
  5548. //remove from map, THEN change x/y coordinates
  5549. unit_remove_map_pc(sd,clrtype);
  5550. sd->mapindex = mapindex;
  5551. sd->bl.x=x;
  5552. sd->bl.y=y;
  5553. pc_clean_skilltree(sd);
  5554. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5555. chrif_changemapserver(sd, ip, (short)port);
  5556. //Free session data from this map server [Kevin]
  5557. unit_free_pc(sd);
  5558. if( st ){
  5559. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5560. st->state = END;
  5561. }
  5562. return SETPOS_OK;
  5563. }
  5564. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5565. {
  5566. 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);
  5567. x = y = 0; // make it random
  5568. }
  5569. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5570. int c=0;
  5571. do {
  5572. x = rnd()%(mapdata->xs-2)+1;
  5573. y = rnd()%(mapdata->ys-2)+1;
  5574. c++;
  5575. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5576. 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));
  5577. return SETPOS_OK; //preventing warp
  5578. //break; //allow warp anyway
  5579. }
  5580. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5581. }
  5582. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5583. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5584. vending_closevending(sd);
  5585. }
  5586. if(sd->bl.prev != NULL){
  5587. unit_remove_map_pc(sd,clrtype);
  5588. clif_changemap(sd,m,x,y); // [MouseJstr]
  5589. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5590. sd->state.rewarp = 1;
  5591. sd->mapindex = mapindex;
  5592. sd->bl.m = m;
  5593. sd->bl.x = sd->ud.to_x = x;
  5594. sd->bl.y = sd->ud.to_y = y;
  5595. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5596. { // Increased guild castle regen [Valaris]
  5597. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5598. if(gc && gc->guild_id == sd->status.guild_id)
  5599. sd->regen.state.gc = 1;
  5600. }
  5601. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5602. {
  5603. sd->pd->bl.m = m;
  5604. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5605. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5606. sd->pd->ud.dir = sd->ud.dir;
  5607. }
  5608. if( hom_is_active(sd->hd) )
  5609. {
  5610. sd->hd->bl.m = m;
  5611. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5612. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5613. sd->hd->ud.dir = sd->ud.dir;
  5614. }
  5615. if( sd->md )
  5616. {
  5617. sd->md->bl.m = m;
  5618. sd->md->bl.x = sd->md->ud.to_x = x;
  5619. sd->md->bl.y = sd->md->ud.to_y = y;
  5620. sd->md->ud.dir = sd->ud.dir;
  5621. }
  5622. if( sd->ed ) {
  5623. sd->ed->bl.m = m;
  5624. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5625. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5626. sd->ed->ud.dir = sd->ud.dir;
  5627. }
  5628. pc_cell_basilica(sd);
  5629. //check if we gonna be rewarped [lighta]
  5630. if(npc_check_areanpc(1,m,x,y,0)){
  5631. sd->count_rewarp++;
  5632. }
  5633. else
  5634. sd->count_rewarp = 0;
  5635. return SETPOS_OK;
  5636. }
  5637. /*==========================================
  5638. * Warp player sd to random location on current map.
  5639. * May fail if no walkable cell found (1000 attempts).
  5640. * Return:
  5641. * 0 = Success
  5642. * 1,2,3 = Fail
  5643. *------------------------------------------*/
  5644. char pc_randomwarp(struct map_session_data *sd, clr_type type, bool ignore_mapflag)
  5645. {
  5646. int x,y,i=0;
  5647. nullpo_ret(sd);
  5648. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5649. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  5650. return 3;
  5651. do {
  5652. x = rnd()%(mapdata->xs-2)+1;
  5653. y = rnd()%(mapdata->ys-2)+1;
  5654. } while((map_getcell(sd->bl.m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,sd->bl.m,x,y,1))) && (i++) < 1000);
  5655. if (i < 1000)
  5656. return pc_setpos(sd,mapdata->index,x,y,type);
  5657. return 3;
  5658. }
  5659. /*==========================================
  5660. * Records a memo point at sd's current position
  5661. * pos - entry to replace, (-1: shift oldest entry out)
  5662. *------------------------------------------*/
  5663. bool pc_memo(struct map_session_data* sd, int pos)
  5664. {
  5665. int skill;
  5666. nullpo_ret(sd);
  5667. // check mapflags
  5668. if( sd->bl.m >= 0 && (map_getmapflag(sd->bl.m, MF_NOMEMO) || map_getmapflag(sd->bl.m, MF_NOWARPTO)) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  5669. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5670. return false;
  5671. }
  5672. // check inputs
  5673. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5674. return false; // invalid input
  5675. // check required skill level
  5676. skill = pc_checkskill(sd, AL_WARP);
  5677. if( skill < 1 ) {
  5678. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5679. return false;
  5680. }
  5681. if( skill < 2 || skill - 2 < pos ) {
  5682. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5683. return false;
  5684. }
  5685. if( pos == -1 )
  5686. {
  5687. uint8 i;
  5688. // prevent memo-ing the same map multiple times
  5689. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5690. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5691. pos = 0;
  5692. }
  5693. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5694. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5695. return false;
  5696. }
  5697. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5698. sd->status.memo_point[pos].x = sd->bl.x;
  5699. sd->status.memo_point[pos].y = sd->bl.y;
  5700. clif_skill_memomessage(sd, 0);
  5701. return true;
  5702. }
  5703. //
  5704. // Skills
  5705. //
  5706. /**
  5707. * Get the skill current cooldown for player.
  5708. * (get the db base cooldown for skill + player specific cooldown)
  5709. * @param sd : player pointer
  5710. * @param id : skill id
  5711. * @param lv : skill lv
  5712. * @return player skill cooldown
  5713. */
  5714. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5715. if (skill_id == SJ_NOVAEXPLOSING) {
  5716. struct status_change *sc = status_get_sc(&sd->bl);
  5717. if (sc && sc->data[SC_DIMENSION])
  5718. return 0;
  5719. }
  5720. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5721. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  5722. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5723. for (auto &it : sd->skillcooldown) {
  5724. if (it.id == skill_id) {
  5725. cooldown += it.val;
  5726. break;
  5727. }
  5728. }
  5729. return max(0, cooldown);
  5730. }
  5731. /*==========================================
  5732. * Return player sd skill_lv learned for given skill
  5733. *------------------------------------------*/
  5734. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5735. {
  5736. uint16 idx = 0;
  5737. if (sd == NULL)
  5738. return 0;
  5739. #ifdef RENEWAL
  5740. if ((idx = skill_get_index(skill_id)) == 0) {
  5741. #else
  5742. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  5743. if( skill_id >= RK_ENCHANTBLADE ){
  5744. // Silently fail for now -> future update planned
  5745. return 0;
  5746. }
  5747. #endif
  5748. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5749. return 0;
  5750. }
  5751. if (SKILL_CHK_GUILD(skill_id) ) {
  5752. struct guild *g;
  5753. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5754. return guild_checkskill(g,skill_id);
  5755. return 0;
  5756. }
  5757. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5758. }
  5759. /**
  5760. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5761. * @param sd: Player data
  5762. * @param type: Summoner Power Type
  5763. * @return Skill points invested
  5764. */
  5765. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5766. if (sd == nullptr)
  5767. return 0;
  5768. uint8 count = 0;
  5769. switch (type) {
  5770. case SUMMONER_POWER_SEA:
  5771. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5772. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  5773. break;
  5774. case SUMMONER_POWER_LAND:
  5775. count = pc_checkskill(sd, SU_SV_STEMSPEAR) + pc_checkskill(sd, SU_CN_POWDERING) + pc_checkskill(sd, SU_CN_METEOR) + pc_checkskill(sd, SU_SV_ROOTTWIST) +
  5776. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  5777. break;
  5778. case SUMMONER_POWER_LIFE:
  5779. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  5780. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  5781. break;
  5782. }
  5783. return count;
  5784. }
  5785. /**
  5786. * Checks for Imperial Guard's passive skills.
  5787. * @param sd: Player data
  5788. * @param flag:
  5789. * Flag&1 = IG_SHIELD_MASTERY
  5790. * Flag&2 = IG_SPEAR_SWORD_M
  5791. */
  5792. uint8 pc_checkskill_imperial_guard(struct map_session_data *sd, short flag)
  5793. {
  5794. nullpo_retr(0, sd);
  5795. uint8 count = 0;
  5796. if (flag&1 && sd->status.shield > 0)
  5797. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  5798. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  5799. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  5800. return count;
  5801. }
  5802. /**
  5803. * Check if we still have the correct weapon to continue the skill (actually status)
  5804. * If not ending it
  5805. * @param sd
  5806. * @return 0:error, 1:check done
  5807. */
  5808. static void pc_checkallowskill(struct map_session_data *sd)
  5809. {
  5810. nullpo_retv(sd);
  5811. if(!sd->sc.count)
  5812. return;
  5813. for (const auto &it : status_db) {
  5814. sc_type status = it.second->type;
  5815. std::bitset<SCF_MAX> flag = it.second->flag;
  5816. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  5817. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  5818. continue;
  5819. if (sd->sc.data[status] && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  5820. status_change_end(&sd->bl, status, INVALID_TIMER);
  5821. }
  5822. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  5823. if (sd->sc.data[status] && sd->status.shield <= 0)
  5824. status_change_end(&sd->bl, status, INVALID_TIMER);
  5825. }
  5826. }
  5827. }
  5828. /*==========================================
  5829. * Return equipped index of item on player sd at pos
  5830. * Return
  5831. * -1 : Nothing equipped
  5832. * idx : (this index could be used in inventory to found item_data)
  5833. *------------------------------------------*/
  5834. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5835. {
  5836. uint8 i;
  5837. nullpo_retr(-1, sd);
  5838. for(i=0;i<EQI_MAX;i++){
  5839. if(pos & equip_bitmask[i]){
  5840. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5841. // Check all if any match is found
  5842. continue;
  5843. }
  5844. return sd->equip_index[i];
  5845. }
  5846. }
  5847. return -1;
  5848. }
  5849. /*==========================================
  5850. * Check if sd has nameid equipped somewhere
  5851. * @sd : the player session
  5852. * @nameid : id of the item to check
  5853. * @min : : see pc.hpp enum equip_index from ? to @max
  5854. * @max : see pc.hpp enum equip_index for @min to ?
  5855. * -return true,false
  5856. *------------------------------------------*/
  5857. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  5858. {
  5859. int i;
  5860. for(i = min; i < max; i++) {
  5861. if(equip_bitmask[i]) {
  5862. int idx = sd->equip_index[i];
  5863. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5864. return true;
  5865. }
  5866. }
  5867. return false;
  5868. }
  5869. /*==========================================
  5870. * Convert's from the client's lame Job ID system
  5871. * to the map server's 'makes sense' system. [Skotlex]
  5872. *------------------------------------------*/
  5873. uint64 pc_jobid2mapid(unsigned short b_class)
  5874. {
  5875. switch(b_class)
  5876. {
  5877. //Novice And 1-1 Jobs
  5878. case JOB_NOVICE: return MAPID_NOVICE;
  5879. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5880. case JOB_MAGE: return MAPID_MAGE;
  5881. case JOB_ARCHER: return MAPID_ARCHER;
  5882. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5883. case JOB_MERCHANT: return MAPID_MERCHANT;
  5884. case JOB_THIEF: return MAPID_THIEF;
  5885. case JOB_TAEKWON: return MAPID_TAEKWON;
  5886. case JOB_WEDDING: return MAPID_WEDDING;
  5887. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5888. case JOB_NINJA: return MAPID_NINJA;
  5889. case JOB_XMAS: return MAPID_XMAS;
  5890. case JOB_SUMMER: return MAPID_SUMMER;
  5891. case JOB_HANBOK: return MAPID_HANBOK;
  5892. case JOB_GANGSI: return MAPID_GANGSI;
  5893. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5894. case JOB_SUMMER2: return MAPID_SUMMER2;
  5895. //2-1 Jobs
  5896. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5897. case JOB_KNIGHT: return MAPID_KNIGHT;
  5898. case JOB_WIZARD: return MAPID_WIZARD;
  5899. case JOB_HUNTER: return MAPID_HUNTER;
  5900. case JOB_PRIEST: return MAPID_PRIEST;
  5901. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5902. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5903. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5904. case JOB_KAGEROU:
  5905. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5906. case JOB_REBELLION: return MAPID_REBELLION;
  5907. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5908. //2-2 Jobs
  5909. case JOB_CRUSADER: return MAPID_CRUSADER;
  5910. case JOB_SAGE: return MAPID_SAGE;
  5911. case JOB_BARD:
  5912. case JOB_DANCER: return MAPID_BARDDANCER;
  5913. case JOB_MONK: return MAPID_MONK;
  5914. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5915. case JOB_ROGUE: return MAPID_ROGUE;
  5916. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5917. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5918. //Trans Novice And Trans 1-1 Jobs
  5919. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5920. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5921. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5922. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5923. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5924. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5925. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5926. //Trans 2-1 Jobs
  5927. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5928. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5929. case JOB_SNIPER: return MAPID_SNIPER;
  5930. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5931. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5932. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5933. //Trans 2-2 Jobs
  5934. case JOB_PALADIN: return MAPID_PALADIN;
  5935. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5936. case JOB_CLOWN:
  5937. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5938. case JOB_CHAMPION: return MAPID_CHAMPION;
  5939. case JOB_CREATOR: return MAPID_CREATOR;
  5940. case JOB_STALKER: return MAPID_STALKER;
  5941. //Baby Novice And Baby 1-1 Jobs
  5942. case JOB_BABY: return MAPID_BABY;
  5943. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5944. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5945. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5946. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5947. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5948. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5949. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5950. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5951. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5952. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5953. //Baby 2-1 Jobs
  5954. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5955. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5956. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5957. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5958. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5959. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5960. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5961. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5962. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5963. case JOB_BABY_KAGEROU:
  5964. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5965. //Baby 2-2 Jobs
  5966. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5967. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5968. case JOB_BABY_BARD:
  5969. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5970. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5971. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5972. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5973. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5974. //3-1 Jobs
  5975. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5976. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5977. case JOB_WARLOCK: return MAPID_WARLOCK;
  5978. case JOB_RANGER: return MAPID_RANGER;
  5979. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5980. case JOB_MECHANIC: return MAPID_MECHANIC;
  5981. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5982. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5983. //3-2 Jobs
  5984. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5985. case JOB_SORCERER: return MAPID_SORCERER;
  5986. case JOB_MINSTREL:
  5987. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5988. case JOB_SURA: return MAPID_SURA;
  5989. case JOB_GENETIC: return MAPID_GENETIC;
  5990. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5991. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5992. //Trans 3-1 Jobs
  5993. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5994. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5995. case JOB_RANGER_T: return MAPID_RANGER_T;
  5996. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5997. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5998. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5999. //Trans 3-2 Jobs
  6000. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6001. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6002. case JOB_MINSTREL_T:
  6003. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6004. case JOB_SURA_T: return MAPID_SURA_T;
  6005. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6006. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6007. //Baby 3-1 Jobs
  6008. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6009. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6010. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6011. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6012. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6013. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6014. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6015. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6016. //Baby 3-2 Jobs
  6017. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6018. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6019. case JOB_BABY_MINSTREL:
  6020. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6021. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6022. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6023. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6024. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6025. //Doram Jobs
  6026. case JOB_SUMMONER: return MAPID_SUMMONER;
  6027. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6028. //4-1 Jobs
  6029. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6030. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6031. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6032. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6033. case JOB_CARDINAL: return MAPID_CARDINAL;
  6034. case JOB_MEISTER: return MAPID_MEISTER;
  6035. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6036. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6037. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6038. case JOB_SHINKIRO:
  6039. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6040. //4-2 Jobs
  6041. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6042. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6043. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6044. case JOB_TROUBADOUR:
  6045. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6046. case JOB_BIOLO: return MAPID_BIOLO;
  6047. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6048. //Unknown
  6049. default:
  6050. return -1;
  6051. }
  6052. }
  6053. //Reverts the map-style class id to the client-style one.
  6054. int pc_mapid2jobid(uint64 class_, int sex)
  6055. {
  6056. switch(class_) {
  6057. //Novice And 1-1 Jobs
  6058. case MAPID_NOVICE: return JOB_NOVICE;
  6059. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6060. case MAPID_MAGE: return JOB_MAGE;
  6061. case MAPID_ARCHER: return JOB_ARCHER;
  6062. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6063. case MAPID_MERCHANT: return JOB_MERCHANT;
  6064. case MAPID_THIEF: return JOB_THIEF;
  6065. case MAPID_TAEKWON: return JOB_TAEKWON;
  6066. case MAPID_WEDDING: return JOB_WEDDING;
  6067. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6068. case MAPID_NINJA: return JOB_NINJA;
  6069. case MAPID_XMAS: return JOB_XMAS;
  6070. case MAPID_SUMMER: return JOB_SUMMER;
  6071. case MAPID_HANBOK: return JOB_HANBOK;
  6072. case MAPID_GANGSI: return JOB_GANGSI;
  6073. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6074. case MAPID_SUMMER2: return JOB_SUMMER2;
  6075. //2-1 Jobs
  6076. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6077. case MAPID_KNIGHT: return JOB_KNIGHT;
  6078. case MAPID_WIZARD: return JOB_WIZARD;
  6079. case MAPID_HUNTER: return JOB_HUNTER;
  6080. case MAPID_PRIEST: return JOB_PRIEST;
  6081. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6082. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6083. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6084. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6085. case MAPID_REBELLION: return JOB_REBELLION;
  6086. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6087. //2-2 Jobs
  6088. case MAPID_CRUSADER: return JOB_CRUSADER;
  6089. case MAPID_SAGE: return JOB_SAGE;
  6090. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6091. case MAPID_MONK: return JOB_MONK;
  6092. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6093. case MAPID_ROGUE: return JOB_ROGUE;
  6094. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6095. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6096. //Trans Novice And Trans 2-1 Jobs
  6097. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6098. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6099. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6100. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6101. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6102. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6103. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6104. //Trans 2-1 Jobs
  6105. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6106. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6107. case MAPID_SNIPER: return JOB_SNIPER;
  6108. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6109. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6110. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6111. //Trans 2-2 Jobs
  6112. case MAPID_PALADIN: return JOB_PALADIN;
  6113. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6114. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6115. case MAPID_CHAMPION: return JOB_CHAMPION;
  6116. case MAPID_CREATOR: return JOB_CREATOR;
  6117. case MAPID_STALKER: return JOB_STALKER;
  6118. //Baby Novice And Baby 1-1 Jobs
  6119. case MAPID_BABY: return JOB_BABY;
  6120. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6121. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6122. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6123. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6124. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6125. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6126. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6127. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6128. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6129. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6130. //Baby 2-1 Jobs
  6131. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6132. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6133. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6134. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6135. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6136. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6137. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6138. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6139. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6140. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6141. //Baby 2-2 Jobs
  6142. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6143. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6144. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6145. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6146. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6147. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6148. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6149. //3-1 Jobs
  6150. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6151. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6152. case MAPID_WARLOCK: return JOB_WARLOCK;
  6153. case MAPID_RANGER: return JOB_RANGER;
  6154. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6155. case MAPID_MECHANIC: return JOB_MECHANIC;
  6156. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6157. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6158. //3-2 Jobs
  6159. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6160. case MAPID_SORCERER: return JOB_SORCERER;
  6161. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6162. case MAPID_SURA: return JOB_SURA;
  6163. case MAPID_GENETIC: return JOB_GENETIC;
  6164. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6165. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6166. //Trans 3-1 Jobs
  6167. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6168. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6169. case MAPID_RANGER_T: return JOB_RANGER_T;
  6170. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6171. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6172. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6173. //Trans 3-2 Jobs
  6174. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6175. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6176. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6177. case MAPID_SURA_T: return JOB_SURA_T;
  6178. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6179. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6180. //Baby 3-1 Jobs
  6181. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6182. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6183. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6184. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6185. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6186. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6187. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6188. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6189. //Baby 3-2 Jobs
  6190. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6191. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6192. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6193. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6194. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6195. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6196. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6197. //Doram Jobs
  6198. case MAPID_SUMMONER: return JOB_SUMMONER;
  6199. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6200. //4-1 Jobs
  6201. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6202. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6203. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6204. case MAPID_CARDINAL: return JOB_CARDINAL;
  6205. case MAPID_MEISTER: return JOB_MEISTER;
  6206. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6207. //4-2 Jobs
  6208. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6209. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6210. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6211. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6212. case MAPID_BIOLO: return JOB_BIOLO;
  6213. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6214. //Unknown
  6215. default:
  6216. return -1;
  6217. }
  6218. }
  6219. /*====================================================
  6220. * This function return the name of the job (by [Yor])
  6221. *----------------------------------------------------*/
  6222. const char* job_name(int class_)
  6223. {
  6224. switch (class_) {
  6225. case JOB_NOVICE:
  6226. case JOB_SWORDMAN:
  6227. case JOB_MAGE:
  6228. case JOB_ARCHER:
  6229. case JOB_ACOLYTE:
  6230. case JOB_MERCHANT:
  6231. case JOB_THIEF:
  6232. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6233. case JOB_KNIGHT:
  6234. case JOB_PRIEST:
  6235. case JOB_WIZARD:
  6236. case JOB_BLACKSMITH:
  6237. case JOB_HUNTER:
  6238. case JOB_ASSASSIN:
  6239. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6240. case JOB_KNIGHT2:
  6241. return msg_txt(NULL,557);
  6242. case JOB_CRUSADER:
  6243. case JOB_MONK:
  6244. case JOB_SAGE:
  6245. case JOB_ROGUE:
  6246. case JOB_ALCHEMIST:
  6247. case JOB_BARD:
  6248. case JOB_DANCER:
  6249. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6250. case JOB_CRUSADER2:
  6251. return msg_txt(NULL,563);
  6252. case JOB_WEDDING:
  6253. case JOB_SUPER_NOVICE:
  6254. case JOB_GUNSLINGER:
  6255. case JOB_NINJA:
  6256. case JOB_XMAS:
  6257. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6258. case JOB_SUMMER:
  6259. case JOB_SUMMER2:
  6260. return msg_txt(NULL,621);
  6261. case JOB_HANBOK:
  6262. return msg_txt(NULL,694);
  6263. case JOB_OKTOBERFEST:
  6264. return msg_txt(NULL,696);
  6265. case JOB_NOVICE_HIGH:
  6266. case JOB_SWORDMAN_HIGH:
  6267. case JOB_MAGE_HIGH:
  6268. case JOB_ARCHER_HIGH:
  6269. case JOB_ACOLYTE_HIGH:
  6270. case JOB_MERCHANT_HIGH:
  6271. case JOB_THIEF_HIGH:
  6272. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6273. case JOB_LORD_KNIGHT:
  6274. case JOB_HIGH_PRIEST:
  6275. case JOB_HIGH_WIZARD:
  6276. case JOB_WHITESMITH:
  6277. case JOB_SNIPER:
  6278. case JOB_ASSASSIN_CROSS:
  6279. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6280. case JOB_LORD_KNIGHT2:
  6281. return msg_txt(NULL,582);
  6282. case JOB_PALADIN:
  6283. case JOB_CHAMPION:
  6284. case JOB_PROFESSOR:
  6285. case JOB_STALKER:
  6286. case JOB_CREATOR:
  6287. case JOB_CLOWN:
  6288. case JOB_GYPSY:
  6289. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6290. case JOB_PALADIN2:
  6291. return msg_txt(NULL,588);
  6292. case JOB_BABY:
  6293. case JOB_BABY_SWORDMAN:
  6294. case JOB_BABY_MAGE:
  6295. case JOB_BABY_ARCHER:
  6296. case JOB_BABY_ACOLYTE:
  6297. case JOB_BABY_MERCHANT:
  6298. case JOB_BABY_THIEF:
  6299. return msg_txt(NULL,595 - JOB_BABY + class_);
  6300. case JOB_BABY_KNIGHT:
  6301. case JOB_BABY_PRIEST:
  6302. case JOB_BABY_WIZARD:
  6303. case JOB_BABY_BLACKSMITH:
  6304. case JOB_BABY_HUNTER:
  6305. case JOB_BABY_ASSASSIN:
  6306. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6307. case JOB_BABY_KNIGHT2:
  6308. return msg_txt(NULL,602);
  6309. case JOB_BABY_CRUSADER:
  6310. case JOB_BABY_MONK:
  6311. case JOB_BABY_SAGE:
  6312. case JOB_BABY_ROGUE:
  6313. case JOB_BABY_ALCHEMIST:
  6314. case JOB_BABY_BARD:
  6315. case JOB_BABY_DANCER:
  6316. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6317. case JOB_BABY_CRUSADER2:
  6318. return msg_txt(NULL,608);
  6319. case JOB_SUPER_BABY:
  6320. return msg_txt(NULL,615);
  6321. case JOB_TAEKWON:
  6322. return msg_txt(NULL,616);
  6323. case JOB_STAR_GLADIATOR:
  6324. case JOB_STAR_GLADIATOR2:
  6325. return msg_txt(NULL,617);
  6326. case JOB_SOUL_LINKER:
  6327. return msg_txt(NULL,618);
  6328. case JOB_GANGSI:
  6329. case JOB_DEATH_KNIGHT:
  6330. case JOB_DARK_COLLECTOR:
  6331. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6332. case JOB_RUNE_KNIGHT:
  6333. case JOB_WARLOCK:
  6334. case JOB_RANGER:
  6335. case JOB_ARCH_BISHOP:
  6336. case JOB_MECHANIC:
  6337. case JOB_GUILLOTINE_CROSS:
  6338. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6339. case JOB_RUNE_KNIGHT_T:
  6340. case JOB_WARLOCK_T:
  6341. case JOB_RANGER_T:
  6342. case JOB_ARCH_BISHOP_T:
  6343. case JOB_MECHANIC_T:
  6344. case JOB_GUILLOTINE_CROSS_T:
  6345. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6346. case JOB_ROYAL_GUARD:
  6347. case JOB_SORCERER:
  6348. case JOB_MINSTREL:
  6349. case JOB_WANDERER:
  6350. case JOB_SURA:
  6351. case JOB_GENETIC:
  6352. case JOB_SHADOW_CHASER:
  6353. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6354. case JOB_ROYAL_GUARD_T:
  6355. case JOB_SORCERER_T:
  6356. case JOB_MINSTREL_T:
  6357. case JOB_WANDERER_T:
  6358. case JOB_SURA_T:
  6359. case JOB_GENETIC_T:
  6360. case JOB_SHADOW_CHASER_T:
  6361. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6362. case JOB_RUNE_KNIGHT2:
  6363. case JOB_RUNE_KNIGHT_T2:
  6364. return msg_txt(NULL,625);
  6365. case JOB_ROYAL_GUARD2:
  6366. case JOB_ROYAL_GUARD_T2:
  6367. return msg_txt(NULL,631);
  6368. case JOB_RANGER2:
  6369. case JOB_RANGER_T2:
  6370. return msg_txt(NULL,627);
  6371. case JOB_MECHANIC2:
  6372. case JOB_MECHANIC_T2:
  6373. return msg_txt(NULL,629);
  6374. case JOB_BABY_RUNE_KNIGHT:
  6375. case JOB_BABY_WARLOCK:
  6376. case JOB_BABY_RANGER:
  6377. case JOB_BABY_ARCH_BISHOP:
  6378. case JOB_BABY_MECHANIC:
  6379. case JOB_BABY_GUILLOTINE_CROSS:
  6380. case JOB_BABY_ROYAL_GUARD:
  6381. case JOB_BABY_SORCERER:
  6382. case JOB_BABY_MINSTREL:
  6383. case JOB_BABY_WANDERER:
  6384. case JOB_BABY_SURA:
  6385. case JOB_BABY_GENETIC:
  6386. case JOB_BABY_SHADOW_CHASER:
  6387. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6388. case JOB_BABY_RUNE_KNIGHT2:
  6389. return msg_txt(NULL,638);
  6390. case JOB_BABY_ROYAL_GUARD2:
  6391. return msg_txt(NULL,644);
  6392. case JOB_BABY_RANGER2:
  6393. return msg_txt(NULL,640);
  6394. case JOB_BABY_MECHANIC2:
  6395. return msg_txt(NULL,642);
  6396. case JOB_SUPER_NOVICE_E:
  6397. case JOB_SUPER_BABY_E:
  6398. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6399. case JOB_KAGEROU:
  6400. case JOB_OBORO:
  6401. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6402. case JOB_REBELLION:
  6403. return msg_txt(NULL,695);
  6404. case JOB_SUMMONER:
  6405. return msg_txt(NULL,697);
  6406. case JOB_BABY_SUMMONER:
  6407. return msg_txt(NULL,698);
  6408. case JOB_BABY_NINJA:
  6409. return msg_txt(NULL,699);
  6410. case JOB_BABY_KAGEROU:
  6411. case JOB_BABY_OBORO:
  6412. case JOB_BABY_TAEKWON:
  6413. case JOB_BABY_STAR_GLADIATOR:
  6414. case JOB_BABY_SOUL_LINKER:
  6415. case JOB_BABY_GUNSLINGER:
  6416. case JOB_BABY_REBELLION:
  6417. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6418. case JOB_BABY_STAR_GLADIATOR2:
  6419. return msg_txt(NULL,756);
  6420. case JOB_STAR_EMPEROR:
  6421. case JOB_SOUL_REAPER:
  6422. case JOB_BABY_STAR_EMPEROR:
  6423. case JOB_BABY_SOUL_REAPER:
  6424. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6425. case JOB_STAR_EMPEROR2:
  6426. return msg_txt(NULL,782);
  6427. case JOB_BABY_STAR_EMPEROR2:
  6428. return msg_txt(NULL,784);
  6429. case JOB_DRAGON_KNIGHT:
  6430. case JOB_MEISTER:
  6431. case JOB_SHADOW_CROSS:
  6432. case JOB_ARCH_MAGE:
  6433. case JOB_CARDINAL:
  6434. case JOB_WINDHAWK:
  6435. case JOB_IMPERIAL_GUARD:
  6436. case JOB_BIOLO:
  6437. case JOB_ABYSS_CHASER:
  6438. case JOB_ELEMENTAL_MASTER:
  6439. case JOB_INQUISITOR:
  6440. case JOB_TROUBADOUR:
  6441. case JOB_TROUVERE:
  6442. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6443. case JOB_WINDHAWK2:
  6444. return msg_txt( nullptr, 805);
  6445. case JOB_MEISTER2:
  6446. return msg_txt( nullptr, 801 );
  6447. case JOB_DRAGON_KNIGHT2:
  6448. return msg_txt( nullptr, 800 );
  6449. case JOB_IMPERIAL_GUARD2:
  6450. return msg_txt( nullptr, 806 );
  6451. case JOB_SKY_EMPEROR:
  6452. case JOB_SOUL_ASCETIC:
  6453. case JOB_SHINKIRO:
  6454. case JOB_SHIRANUI:
  6455. case JOB_NIGHT_WATCH:
  6456. case JOB_HYPER_NOVICE:
  6457. case JOB_SPIRIT_HANDLER:
  6458. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6459. case JOB_SKY_EMPEROR2:
  6460. return msg_txt( nullptr, 813 );
  6461. default:
  6462. return msg_txt(NULL,655);
  6463. }
  6464. }
  6465. /*====================================================
  6466. * Timered function to make id follow a target.
  6467. * @id = bl.id (player only atm)
  6468. * target is define in sd->followtarget (bl.id)
  6469. * used by pc_follow
  6470. *----------------------------------------------------*/
  6471. TIMER_FUNC(pc_follow_timer){
  6472. struct map_session_data *sd;
  6473. struct block_list *tbl;
  6474. sd = map_id2sd(id);
  6475. nullpo_ret(sd);
  6476. if (sd->followtimer != tid){
  6477. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6478. sd->followtimer = INVALID_TIMER;
  6479. return 0;
  6480. }
  6481. sd->followtimer = INVALID_TIMER;
  6482. tbl = map_id2bl(sd->followtarget);
  6483. if (tbl == NULL || pc_isdead(sd))
  6484. {
  6485. pc_stop_following(sd);
  6486. return 0;
  6487. }
  6488. // either player or target is currently detached from map blocks (could be teleporting),
  6489. // but still connected to this map, so we'll just increment the timer and check back later
  6490. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6491. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6492. {
  6493. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6494. if (!check_distance_bl(&sd->bl, tbl, 5))
  6495. unit_walktobl(&sd->bl, tbl, 5, 0);
  6496. } else
  6497. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6498. }
  6499. sd->followtimer = add_timer(
  6500. tick + 1000, // increase time a bit to loosen up map's load
  6501. pc_follow_timer, sd->bl.id, 0);
  6502. return 0;
  6503. }
  6504. int pc_stop_following (struct map_session_data *sd)
  6505. {
  6506. nullpo_ret(sd);
  6507. if (sd->followtimer != INVALID_TIMER) {
  6508. delete_timer(sd->followtimer,pc_follow_timer);
  6509. sd->followtimer = INVALID_TIMER;
  6510. }
  6511. sd->followtarget = -1;
  6512. sd->ud.target_to = 0;
  6513. unit_stop_walking(&sd->bl, 1);
  6514. return 0;
  6515. }
  6516. int pc_follow(struct map_session_data *sd,int target_id)
  6517. {
  6518. struct block_list *bl = map_id2bl(target_id);
  6519. if (bl == NULL /*|| bl->type != BL_PC*/)
  6520. return 1;
  6521. if (sd->followtimer != INVALID_TIMER)
  6522. pc_stop_following(sd);
  6523. sd->followtarget = target_id;
  6524. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6525. return 0;
  6526. }
  6527. int pc_checkbaselevelup(struct map_session_data *sd) {
  6528. t_exp next = pc_nextbaseexp(sd);
  6529. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6530. return 0;
  6531. uint32 base_level = sd->status.base_level;
  6532. do {
  6533. sd->status.base_exp -= next;
  6534. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6535. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_base > 0 && sd->status.base_level >= battle_config.multi_level_up_base ) ) && sd->status.base_exp > next-1 )
  6536. sd->status.base_exp = next-1;
  6537. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6538. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6539. sd->status.base_level++;
  6540. if( pc_is_maxbaselv(sd) ){
  6541. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6542. break;
  6543. }
  6544. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6545. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6546. status_calc_pet(sd->pd,SCO_NONE);
  6547. clif_updatestatus(sd,SP_STATUSPOINT);
  6548. clif_updatestatus(sd,SP_TRAITPOINT);
  6549. clif_updatestatus(sd,SP_BASELEVEL);
  6550. clif_updatestatus(sd,SP_BASEEXP);
  6551. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6552. status_calc_pc(sd,SCO_FORCE);
  6553. status_percent_heal(&sd->bl,100,100);
  6554. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6555. for (const auto &status : status_db) {
  6556. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6557. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6558. }
  6559. if (sd->state.snovice_dead_flag)
  6560. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6561. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6562. for (const auto &status : status_db) {
  6563. if (status.second->flag[SCF_TAEKWONANGEL])
  6564. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6565. }
  6566. }
  6567. clif_misceffect(&sd->bl,0);
  6568. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6569. if(sd->status.party_id)
  6570. party_send_levelup(sd);
  6571. pc_baselevelchanged(sd);
  6572. for (; base_level <= sd->status.base_level; base_level++) {
  6573. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6574. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6575. }
  6576. return 1;
  6577. }
  6578. void pc_baselevelchanged(struct map_session_data *sd) {
  6579. uint8 i;
  6580. for( i = 0; i < EQI_MAX; i++ ) {
  6581. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6582. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6583. pc_unequipitem(sd, sd->equip_index[i], 3);
  6584. }
  6585. }
  6586. pc_show_questinfo(sd);
  6587. }
  6588. int pc_checkjoblevelup(struct map_session_data *sd)
  6589. {
  6590. t_exp next = pc_nextjobexp(sd);
  6591. nullpo_ret(sd);
  6592. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6593. return 0;
  6594. uint32 job_level = sd->status.job_level;
  6595. do {
  6596. sd->status.job_exp -= next;
  6597. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6598. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_job > 0 && sd->status.job_level >= battle_config.multi_level_up_job ) ) && sd->status.job_exp > next-1 )
  6599. sd->status.job_exp = next-1;
  6600. sd->status.job_level ++;
  6601. sd->status.skill_point ++;
  6602. if( pc_is_maxjoblv(sd) ){
  6603. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6604. break;
  6605. }
  6606. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6607. clif_updatestatus(sd,SP_JOBLEVEL);
  6608. clif_updatestatus(sd,SP_JOBEXP);
  6609. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6610. clif_updatestatus(sd,SP_SKILLPOINT);
  6611. status_calc_pc(sd,SCO_FORCE);
  6612. clif_misceffect(&sd->bl,1);
  6613. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6614. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6615. npc_script_event(sd, NPCE_JOBLVUP);
  6616. for (; job_level <= sd->status.job_level; job_level++)
  6617. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6618. pc_show_questinfo(sd);
  6619. return 1;
  6620. }
  6621. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6622. * @param sd Player
  6623. * @param base_exp Base EXP before peronal bonuses
  6624. * @param job_exp Job EXP before peronal bonuses
  6625. * @param src Block list that affecting the exp calculation
  6626. */
  6627. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6628. {
  6629. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6630. if (src) {
  6631. struct status_data *status = status_get_status_data(src);
  6632. if( sd->indexed_bonus.expaddrace[status->race] )
  6633. bonus += sd->indexed_bonus.expaddrace[status->race];
  6634. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  6635. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  6636. if( sd->indexed_bonus.expaddclass[status->class_] )
  6637. bonus += sd->indexed_bonus.expaddclass[status->class_];
  6638. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  6639. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  6640. if (battle_config.pk_mode &&
  6641. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6642. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6643. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6644. vip_bonus_base = battle_config.vip_base_exp_increase;
  6645. vip_bonus_job = battle_config.vip_job_exp_increase;
  6646. }
  6647. }
  6648. // Give EXPBOOST for quests even if src is NULL.
  6649. if (sd->sc.data[SC_EXPBOOST]) {
  6650. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6651. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6652. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6653. }
  6654. if (*base_exp) {
  6655. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6656. *base_exp = cap_value(exp, 1, MAX_EXP);
  6657. }
  6658. // Give JEXPBOOST for quests even if src is NULL.
  6659. if (sd->sc.data[SC_JEXPBOOST])
  6660. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6661. if (*job_exp) {
  6662. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6663. *job_exp = cap_value(exp, 1, MAX_EXP);
  6664. }
  6665. return;
  6666. }
  6667. /**
  6668. * Show EXP gained by player in percentage by @showexp
  6669. * @param sd Player
  6670. * @param base_exp Base EXP gained/loss
  6671. * @param next_base_exp Base EXP needed for next base level
  6672. * @param job_exp Job EXP gained/loss
  6673. * @param next_job_exp Job EXP needed for next job level
  6674. * @param lost True:EXP penalty, lose EXP
  6675. **/
  6676. void pc_gainexp_disp(struct map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  6677. char output[CHAT_SIZE_MAX];
  6678. nullpo_retv(sd);
  6679. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6680. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6681. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6682. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6683. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6684. }
  6685. /**
  6686. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6687. * @param sd Player
  6688. * @param src EXP source
  6689. * @param base_exp Base EXP gained
  6690. * @param base_exp Job EXP gained
  6691. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6692. * @return
  6693. **/
  6694. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6695. {
  6696. t_exp nextb = 0, nextj = 0;
  6697. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6698. nullpo_retv(sd);
  6699. if(sd->bl.prev == NULL || pc_isdead(sd))
  6700. return;
  6701. if (!(exp_flag&2)) {
  6702. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6703. return; // no exp on pvp maps
  6704. if (sd->status.guild_id>0)
  6705. base_exp -= guild_payexp(sd,base_exp);
  6706. }
  6707. flag = ((base_exp) ? 1 : 0) |
  6708. ((job_exp) ? 2 : 0) |
  6709. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6710. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6711. if (!(exp_flag&2))
  6712. pc_calcexp(sd, &base_exp, &job_exp, src);
  6713. nextb = pc_nextbaseexp(sd);
  6714. nextj = pc_nextjobexp(sd);
  6715. if (flag&4){
  6716. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6717. base_exp = 0;
  6718. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6719. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6720. }
  6721. if (flag&8){
  6722. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6723. job_exp = 0;
  6724. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6725. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6726. }
  6727. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6728. //Note that this value should never be greater than the original
  6729. //therefore no overflow checks are needed. [Skotlex]
  6730. if (nextb > 0) {
  6731. float nextbp = (float) base_exp / (float) nextb;
  6732. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6733. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6734. }
  6735. if (nextj > 0) {
  6736. float nextjp = (float) job_exp / (float) nextj;
  6737. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6738. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6739. }
  6740. }
  6741. // Give EXP for Base Level
  6742. if (base_exp) {
  6743. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6744. if (!pc_checkbaselevelup(sd))
  6745. clif_updatestatus(sd,SP_BASEEXP);
  6746. }
  6747. // Give EXP for Job Level
  6748. if (job_exp) {
  6749. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6750. if (!pc_checkjoblevelup(sd))
  6751. clif_updatestatus(sd,SP_JOBEXP);
  6752. }
  6753. if (flag&1)
  6754. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6755. if (flag&2)
  6756. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6757. if (sd->state.showexp && (base_exp || job_exp))
  6758. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6759. }
  6760. /**
  6761. * Lost Base/Job EXP from a player
  6762. * @param sd Player
  6763. * @param base_exp Base EXP lost
  6764. * @param job_exp Job EXP lost
  6765. **/
  6766. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  6767. nullpo_retv(sd);
  6768. if (base_exp) {
  6769. base_exp = u64min(sd->status.base_exp, base_exp);
  6770. sd->status.base_exp -= base_exp;
  6771. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6772. clif_updatestatus(sd, SP_BASEEXP);
  6773. }
  6774. if (job_exp) {
  6775. job_exp = u64min(sd->status.job_exp, job_exp);
  6776. sd->status.job_exp -= job_exp;
  6777. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6778. clif_updatestatus(sd, SP_JOBEXP);
  6779. }
  6780. if (sd->state.showexp && (base_exp || job_exp))
  6781. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6782. }
  6783. /**
  6784. * Returns max base level for this character's class.
  6785. * @param job_id: Player's class
  6786. * @return Max Base Level
  6787. */
  6788. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  6789. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6790. return job ? job->max_base_level : 0;
  6791. }
  6792. /**
  6793. * Returns max base level for this character.
  6794. * @param sd Player
  6795. * @return Max Base Level
  6796. **/
  6797. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6798. return job_db.get_maxBaseLv(sd->status.class_);
  6799. }
  6800. /**
  6801. * Returns max job level for this character's class.
  6802. * @param job_id: Player's class
  6803. * @return Max Job Level
  6804. */
  6805. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  6806. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6807. return job ? job->max_job_level : 0;
  6808. }
  6809. /**
  6810. * Returns max job level for this character.
  6811. * @param sd Player
  6812. * @return Max Job Level
  6813. **/
  6814. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6815. return job_db.get_maxJobLv(sd->status.class_);
  6816. }
  6817. /**
  6818. * Check if player is reached max base level
  6819. * @param sd
  6820. * @return True if reached max level
  6821. **/
  6822. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6823. nullpo_retr(false, sd);
  6824. return (sd->status.base_level >= pc_maxbaselv(sd));
  6825. }
  6826. /**
  6827. * Check if player is reached max base level
  6828. * @param sd
  6829. * @return True if reached max level
  6830. **/
  6831. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6832. nullpo_retr(false, sd);
  6833. return (sd->status.job_level >= pc_maxjoblv(sd));
  6834. }
  6835. /**
  6836. * Returns base experience for this character's class.
  6837. * @param job_id: Player's class
  6838. * @param level: Player's level
  6839. * @return Base EXP
  6840. */
  6841. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  6842. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6843. return job ? job->base_exp[level - 1] : 0;
  6844. }
  6845. /**
  6846. * Base exp needed for player to level up.
  6847. * @param sd
  6848. * @return Base EXP needed for next base level
  6849. **/
  6850. t_exp pc_nextbaseexp(struct map_session_data *sd){
  6851. nullpo_ret(sd);
  6852. if (sd->status.base_level == 0) // Is this something that possible?
  6853. return 0;
  6854. if (pc_is_maxbaselv(sd))
  6855. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6856. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  6857. }
  6858. /**
  6859. * Returns job experience for this character's class.
  6860. * @param job_id: Player's class
  6861. * @param level: Player's level
  6862. * @return Job EXP
  6863. */
  6864. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  6865. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6866. return job ? job->job_exp[level - 1] : 0;
  6867. }
  6868. /**
  6869. * Job exp needed for player to level up.
  6870. * @param sd
  6871. * @return Job EXP needed for next job level
  6872. **/
  6873. t_exp pc_nextjobexp(struct map_session_data *sd){
  6874. nullpo_ret(sd);
  6875. if (sd->status.job_level == 0) // Is this something that possible?
  6876. return 0;
  6877. if (pc_is_maxjoblv(sd))
  6878. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6879. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  6880. }
  6881. /**
  6882. * Returns max weight base for this character's class.
  6883. * @param job_id: Player's class
  6884. * @return Max weight base
  6885. */
  6886. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  6887. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6888. return job ? job->max_weight_base : 0;
  6889. }
  6890. /// Returns the value of the specified stat.
  6891. int pc_getstat(map_session_data *sd, int type)
  6892. {
  6893. nullpo_retr(-1, sd);
  6894. switch( type ) {
  6895. case SP_STR: return sd->status.str;
  6896. case SP_AGI: return sd->status.agi;
  6897. case SP_VIT: return sd->status.vit;
  6898. case SP_INT: return sd->status.int_;
  6899. case SP_DEX: return sd->status.dex;
  6900. case SP_LUK: return sd->status.luk;
  6901. case SP_POW: return sd->status.pow;
  6902. case SP_STA: return sd->status.sta;
  6903. case SP_WIS: return sd->status.wis;
  6904. case SP_SPL: return sd->status.spl;
  6905. case SP_CON: return sd->status.con;
  6906. case SP_CRT: return sd->status.crt;
  6907. default:
  6908. return -1;
  6909. }
  6910. }
  6911. /// Sets the specified stat to the specified value.
  6912. /// Returns the new value.
  6913. int pc_setstat(struct map_session_data* sd, int type, int val)
  6914. {
  6915. nullpo_retr(-1, sd);
  6916. switch( type ) {
  6917. case SP_STR: sd->status.str = val; break;
  6918. case SP_AGI: sd->status.agi = val; break;
  6919. case SP_VIT: sd->status.vit = val; break;
  6920. case SP_INT: sd->status.int_ = val; break;
  6921. case SP_DEX: sd->status.dex = val; break;
  6922. case SP_LUK: sd->status.luk = val; break;
  6923. case SP_POW: sd->status.pow = val; break;
  6924. case SP_STA: sd->status.sta = val; break;
  6925. case SP_WIS: sd->status.wis = val; break;
  6926. case SP_SPL: sd->status.spl = val; break;
  6927. case SP_CON: sd->status.con = val; break;
  6928. case SP_CRT: sd->status.crt = val; break;
  6929. default:
  6930. return -1;
  6931. }
  6932. return val;
  6933. }
  6934. /**
  6935. * Gets the total number of status points at the provided level.
  6936. * @param level: Player base level.
  6937. * @return Total number of status points at specific base level.
  6938. */
  6939. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  6940. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  6941. if( entry != nullptr ){
  6942. return entry->statpoints;
  6943. }else{
  6944. return 0;
  6945. }
  6946. }
  6947. /**
  6948. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  6949. * @param level: Player base level.
  6950. * @param table: Use table value or formula.
  6951. * @return Status points at specific base level.
  6952. */
  6953. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  6954. uint32 next_level = this->get_table_point( level + 1 );
  6955. uint32 current_level = this->get_table_point( level );
  6956. if( next_level > current_level ){
  6957. return next_level - current_level;
  6958. }else{
  6959. return 0;
  6960. }
  6961. }
  6962. /**
  6963. * Gets the total number of trait points at the provided level.
  6964. * @param level: Player base level.
  6965. * @return Total number of trait points at specific base level.
  6966. */
  6967. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  6968. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  6969. if( entry != nullptr ){
  6970. return entry->traitpoints;
  6971. }else{
  6972. return 0;
  6973. }
  6974. }
  6975. /**
  6976. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  6977. * @param level: Player base level.
  6978. * @param table: Use table value or formula.
  6979. * @return Trait points at specific base level.
  6980. */
  6981. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  6982. uint32 next_level = this->get_trait_table_point( level + 1 );
  6983. uint32 current_level = this->get_trait_table_point( level );
  6984. if( next_level > current_level ){
  6985. return next_level - current_level;
  6986. }else{
  6987. return 0;
  6988. }
  6989. }
  6990. #ifdef RENEWAL_STAT
  6991. /// Renewal status point cost formula
  6992. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6993. #else
  6994. /// Pre-Renewal status point cost formula
  6995. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6996. #endif
  6997. /// Returns the number of stat points needed to change the specified stat by val.
  6998. /// If val is negative, returns the number of stat points that would be needed to
  6999. /// raise the specified stat from (current value - val) to current value.
  7000. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  7001. {
  7002. int low, high, sp = 0, max = 0;
  7003. if ( val == 0 )
  7004. return 0;
  7005. low = pc_getstat(sd,type);
  7006. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7007. if ( low >= max && val > 0 )
  7008. return 0; // Official servers show '0' when max is reached
  7009. high = low + val;
  7010. if ( val < 0 )
  7011. SWAP(low, high);
  7012. for ( ; low < high; low++ )
  7013. sp += PC_STATUS_POINT_COST(low);
  7014. return sp;
  7015. }
  7016. /**
  7017. * Returns the value the specified stat can be increased by with the current
  7018. * amount of available status points for the current character's class.
  7019. *
  7020. * @param sd The target character.
  7021. * @param type Stat to verify.
  7022. * @return Maximum value the stat could grow by.
  7023. */
  7024. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  7025. {
  7026. int base, final_val, status_points, max_param;
  7027. nullpo_ret(sd);
  7028. base = final_val = pc_getstat(sd, type);
  7029. status_points = sd->status.status_point;
  7030. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7031. while (final_val <= max_param && status_points >= 0) {
  7032. status_points -= PC_STATUS_POINT_COST(final_val);
  7033. final_val++;
  7034. }
  7035. final_val--;
  7036. return (final_val > base ? final_val-base : 0);
  7037. }
  7038. /**
  7039. * Raises a stat by the specified amount.
  7040. *
  7041. * Obeys max_parameter limits.
  7042. * Subtracts status points according to the cost of the increased stat points.
  7043. *
  7044. * @param sd The target character.
  7045. * @param type The stat to change (see enum _sp)
  7046. * @param increase The stat increase (strictly positive) amount.
  7047. * @retval true if the stat was increased by any amount.
  7048. * @retval false if there were no changes.
  7049. */
  7050. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  7051. {
  7052. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7053. nullpo_ret(sd);
  7054. // check conditions
  7055. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7056. clif_statusupack(sd, type, 0, 0);
  7057. return false;
  7058. }
  7059. // check limits
  7060. current = pc_getstat(sd, type);
  7061. max_increase = pc_maxparameterincrease(sd, type);
  7062. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7063. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7064. clif_statusupack(sd, type, 0, 0);
  7065. return false;
  7066. }
  7067. // check status points
  7068. needed_points = pc_need_status_point(sd, type, increase);
  7069. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7070. clif_statusupack(sd, type, 0, 0);
  7071. return false;
  7072. }
  7073. // set new values
  7074. final_value = pc_setstat(sd, type, current + increase);
  7075. sd->status.status_point -= needed_points;
  7076. status_calc_pc(sd,SCO_NONE);
  7077. // update increase cost indicator
  7078. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7079. // update statpoint count
  7080. clif_updatestatus(sd, SP_STATUSPOINT);
  7081. // update stat value
  7082. clif_statusupack(sd, type, 1, final_value); // required
  7083. if( final_value > 255 )
  7084. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7085. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7086. return true;
  7087. }
  7088. /**
  7089. * Raises a stat by the specified amount.
  7090. *
  7091. * Obeys max_parameter limits.
  7092. * Does not subtract status points for the cost of the modified stat points.
  7093. *
  7094. * @param sd The target character.
  7095. * @param type The stat to change (see enum _sp)
  7096. * @param val The stat increase (or decrease) amount.
  7097. * @return the stat increase amount.
  7098. * @retval 0 if no changes were made.
  7099. */
  7100. int pc_statusup2(struct map_session_data* sd, int type, int val)
  7101. {
  7102. int max, need;
  7103. nullpo_ret(sd);
  7104. if( type < SP_STR || type > SP_LUK )
  7105. {
  7106. clif_statusupack(sd,type,0,0);
  7107. return 0;
  7108. }
  7109. need = pc_need_status_point(sd,type,1);
  7110. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7111. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7112. status_calc_pc(sd,SCO_NONE);
  7113. // update increase cost indicator
  7114. if( need != pc_need_status_point(sd,type,1) )
  7115. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7116. // update stat value
  7117. clif_statusupack(sd,type,1,val); // required
  7118. if( val > 255 )
  7119. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7120. return val;
  7121. }
  7122. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7123. /// If val is negative, returns the number of trait stat points that would be needed to
  7124. /// raise the specified trait stat from (current value - val) to current value.
  7125. int pc_need_trait_point(struct map_session_data* sd, int type, int val)
  7126. {
  7127. nullpo_retr(0, sd);
  7128. if (val == 0 || type < SP_POW || type > SP_CRT)
  7129. return 0;
  7130. int low = pc_getstat(sd, type);
  7131. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7132. if (low >= max && val > 0)
  7133. return 0; // Official servers show '0' when max is reached
  7134. int high = low + val, sp = 0;
  7135. if (val < 0)
  7136. SWAP(low, high);
  7137. for (; low < high; low++)
  7138. sp += 1;
  7139. return sp;
  7140. }
  7141. /**
  7142. * Returns the value the specified trait stat can be increased by with the current
  7143. * amount of available trait status points for the current character's class.
  7144. *
  7145. * @param sd The target character.
  7146. * @param type Trait stat to verify.
  7147. * @return Maximum value the stat could grow by.
  7148. */
  7149. int pc_maxtraitparameterincrease(struct map_session_data* sd, int type)
  7150. {
  7151. nullpo_ret(sd);
  7152. if( type < SP_POW || type > SP_CRT ){
  7153. return 0;
  7154. }
  7155. int base, final_val = pc_getstat(sd, type);
  7156. int trait_points = sd->status.trait_point;
  7157. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7158. base = final_val;
  7159. while (final_val <= max_param && trait_points >= 0) {
  7160. trait_points -= 1;
  7161. final_val++;
  7162. }
  7163. final_val--;
  7164. return (final_val > base ? final_val - base : 0);
  7165. }
  7166. /**
  7167. * Raises a trait stat by the specified amount.
  7168. *
  7169. * Obeys max_traitparameter limits.
  7170. * Subtracts trait status points according to the cost of the increased trait stat points.
  7171. *
  7172. * @param sd The target character.
  7173. * @param type The stat to change (see enum _sp)
  7174. * @param increase The stat increase (strictly positive) amount.
  7175. * @retval true if the trait stat was increased by any amount.
  7176. * @retval false if there were no changes.
  7177. */
  7178. bool pc_traitstatusup(struct map_session_data* sd, int type, int increase)
  7179. {
  7180. nullpo_ret(sd);
  7181. // check conditions
  7182. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7183. clif_statusupack(sd, type, 0, 0);
  7184. return false;
  7185. }
  7186. // check limits
  7187. int current = pc_getstat(sd, type);
  7188. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7189. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7190. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7191. clif_statusupack(sd, type, 0, 0);
  7192. return false;
  7193. }
  7194. // check status points
  7195. int needed_points = pc_need_trait_point(sd, type, increase);
  7196. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7197. clif_statusupack(sd, type, 0, 0);
  7198. return false;
  7199. }
  7200. // set new values
  7201. int final_value = pc_setstat(sd, type, current + increase);
  7202. sd->status.trait_point -= needed_points;
  7203. status_calc_pc(sd, SCO_NONE);
  7204. // update increase cost indicator
  7205. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7206. // update statpoint count
  7207. clif_updatestatus(sd, SP_TRAITPOINT);
  7208. // update stat value
  7209. clif_statusupack(sd, type, 1, final_value); // required
  7210. if (final_value > 255)
  7211. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7212. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7213. return true;
  7214. }
  7215. /**
  7216. * Raises a trait stat by the specified amount.
  7217. *
  7218. * Obeys max_trait_parameter limits.
  7219. * Does not subtract status points for the cost of the modified stat points.
  7220. *
  7221. * @param sd The target character.
  7222. * @param type The stat to change (see enum _sp)
  7223. * @param val The stat increase (or decrease) amount.
  7224. * @return the stat increase amount.
  7225. * @retval 0 if no changes were made.
  7226. */
  7227. int pc_traitstatusup2(struct map_session_data* sd, int type, int val)
  7228. {
  7229. nullpo_ret(sd);
  7230. if (type < SP_POW || type > SP_CRT) {
  7231. clif_statusupack(sd, type, 0, 0);
  7232. return 0;
  7233. }
  7234. int need = pc_need_trait_point(sd, type, 1);
  7235. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7236. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7237. status_calc_pc(sd, SCO_NONE);
  7238. // update increase cost indicator
  7239. if (need != pc_need_trait_point(sd, type, 1))
  7240. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7241. // update stat value
  7242. clif_statusupack(sd, type, 1, val); // required
  7243. if (val > 255)
  7244. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7245. return val;
  7246. }
  7247. /*==========================================
  7248. * Update skill_lv for player sd
  7249. * Skill point allocation
  7250. *------------------------------------------*/
  7251. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  7252. {
  7253. uint16 idx = skill_get_index(skill_id);
  7254. nullpo_retv(sd);
  7255. if (!idx) {
  7256. if (skill_id)
  7257. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7258. return;
  7259. }
  7260. // Level up guild skill
  7261. if (SKILL_CHK_GUILD(skill_id)) {
  7262. guild_skillup(sd, skill_id);
  7263. return;
  7264. }
  7265. // Level up homunculus skill
  7266. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7267. hom_skillup(sd->hd, skill_id);
  7268. return;
  7269. }
  7270. else {
  7271. if( sd->status.skill_point > 0 &&
  7272. sd->status.skill[idx].id &&
  7273. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7274. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7275. {
  7276. int lv, range, upgradable;
  7277. sd->status.skill[idx].lv++;
  7278. sd->status.skill_point--;
  7279. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7280. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7281. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7282. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7283. else
  7284. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7285. lv = sd->status.skill[idx].lv;
  7286. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7287. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7288. clif_skillup(sd,skill_id,lv,range,upgradable);
  7289. clif_updatestatus(sd,SP_SKILLPOINT);
  7290. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7291. clif_updatestatus(sd,SP_CARTINFO);
  7292. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7293. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7294. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7295. clif_skillinfoblock(sd);
  7296. }
  7297. //else
  7298. // 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);
  7299. }
  7300. }
  7301. /*==========================================
  7302. * /allskill
  7303. *------------------------------------------*/
  7304. int pc_allskillup(struct map_session_data *sd)
  7305. {
  7306. int i;
  7307. nullpo_ret(sd);
  7308. for (i = 0; i < MAX_SKILL; i++) {
  7309. 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) {
  7310. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7311. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7312. if (sd->status.skill[i].lv == 0)
  7313. sd->status.skill[i].id = 0;
  7314. }
  7315. }
  7316. if (!pc_grant_allskills(sd, true)) {
  7317. uint16 sk_id;
  7318. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7319. if (tree != nullptr && !tree->skills.empty()) {
  7320. for (const auto &skillsit : tree->skills) {
  7321. sk_id = skillsit.first;
  7322. uint16 sk_idx = skill_get_index(sk_id);
  7323. if (sk_idx == 0)
  7324. continue;
  7325. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7326. if (
  7327. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7328. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7329. sk_id == SG_DEVIL
  7330. )
  7331. continue; //Cannot be learned normally.
  7332. sd->status.skill[sk_idx].id = sk_id;
  7333. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7334. }
  7335. }
  7336. }
  7337. status_calc_pc(sd,SCO_NONE);
  7338. //Required because if you could level up all skills previously,
  7339. //the update will not be sent as only the lv variable changes.
  7340. clif_skillinfoblock(sd);
  7341. return 0;
  7342. }
  7343. /*==========================================
  7344. * /resetlvl
  7345. *------------------------------------------*/
  7346. int pc_resetlvl(struct map_session_data* sd,int type)
  7347. {
  7348. int i;
  7349. nullpo_ret(sd);
  7350. if (type != 3) //Also reset skills
  7351. pc_resetskill(sd, 0);
  7352. if(type == 1){
  7353. sd->status.skill_point=0;
  7354. sd->status.trait_point = 0;
  7355. sd->status.base_level=1;
  7356. sd->status.job_level=1;
  7357. sd->status.base_exp=0;
  7358. sd->status.job_exp=0;
  7359. if(sd->sc.option !=0)
  7360. sd->sc.option = 0;
  7361. sd->status.str=1;
  7362. sd->status.agi=1;
  7363. sd->status.vit=1;
  7364. sd->status.int_=1;
  7365. sd->status.dex=1;
  7366. sd->status.luk=1;
  7367. sd->status.pow=0;
  7368. sd->status.sta=0;
  7369. sd->status.wis=0;
  7370. sd->status.spl=0;
  7371. sd->status.con=0;
  7372. sd->status.crt=0;
  7373. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7374. sd->status.status_point=100; // not 88 [celest]
  7375. // give platinum skills upon changing
  7376. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7377. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7378. }
  7379. }
  7380. if(type == 2){
  7381. sd->status.skill_point=0;
  7382. sd->status.base_level=1;
  7383. sd->status.job_level=1;
  7384. sd->status.base_exp=0;
  7385. sd->status.job_exp=0;
  7386. }
  7387. if(type == 3){
  7388. sd->status.base_level=1;
  7389. sd->status.base_exp=0;
  7390. }
  7391. if(type == 4){
  7392. sd->status.job_level=1;
  7393. sd->status.job_exp=0;
  7394. }
  7395. clif_updatestatus(sd,SP_STATUSPOINT);
  7396. clif_updatestatus(sd,SP_TRAITPOINT);
  7397. clif_updatestatus(sd,SP_STR);
  7398. clif_updatestatus(sd,SP_AGI);
  7399. clif_updatestatus(sd,SP_VIT);
  7400. clif_updatestatus(sd,SP_INT);
  7401. clif_updatestatus(sd,SP_DEX);
  7402. clif_updatestatus(sd,SP_LUK);
  7403. clif_updatestatus(sd,SP_POW);
  7404. clif_updatestatus(sd,SP_STA);
  7405. clif_updatestatus(sd,SP_WIS);
  7406. clif_updatestatus(sd,SP_SPL);
  7407. clif_updatestatus(sd,SP_CON);
  7408. clif_updatestatus(sd,SP_CRT);
  7409. clif_updatestatus(sd,SP_BASELEVEL);
  7410. clif_updatestatus(sd,SP_JOBLEVEL);
  7411. clif_updatestatus(sd,SP_STATUSPOINT);
  7412. clif_updatestatus(sd,SP_BASEEXP);
  7413. clif_updatestatus(sd,SP_JOBEXP);
  7414. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7415. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7416. clif_updatestatus(sd,SP_SKILLPOINT);
  7417. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7418. clif_updatestatus(sd,SP_UAGI);
  7419. clif_updatestatus(sd,SP_UVIT);
  7420. clif_updatestatus(sd,SP_UINT);
  7421. clif_updatestatus(sd,SP_UDEX);
  7422. clif_updatestatus(sd,SP_ULUK); // End Addition
  7423. clif_updatestatus(sd,SP_UPOW);
  7424. clif_updatestatus(sd,SP_USTA);
  7425. clif_updatestatus(sd,SP_UWIS);
  7426. clif_updatestatus(sd,SP_USPL);
  7427. clif_updatestatus(sd,SP_UCON);
  7428. clif_updatestatus(sd,SP_UCRT);
  7429. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7430. if(sd->equip_index[i] >= 0)
  7431. if(pc_isequip(sd,sd->equip_index[i]))
  7432. pc_unequipitem(sd,sd->equip_index[i],2);
  7433. }
  7434. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7435. party_send_levelup(sd);
  7436. status_calc_pc(sd, SCO_FORCE);
  7437. clif_skillinfoblock(sd);
  7438. return 0;
  7439. }
  7440. /*==========================================
  7441. * /resetstate
  7442. *------------------------------------------*/
  7443. int pc_resetstate(struct map_session_data* sd)
  7444. {
  7445. nullpo_ret(sd);
  7446. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7447. ShowError( "pc_resetstate: Capping the Level to %d to reset the stats of %d:%d, the base level (%d) is greater than the max level supported.\n",
  7448. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7449. sd->status.base_level = pc_maxbaselv( sd );
  7450. clif_updatestatus( sd, SP_BASELEVEL );
  7451. }
  7452. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7453. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7454. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7455. sd->status.status_point += battle_config.transcendent_status_points;
  7456. }
  7457. if ((sd->class_&JOBL_FOURTH) != 0) {
  7458. sd->status.trait_point += battle_config.trait_points_job_change;
  7459. }
  7460. pc_setstat(sd, SP_STR, 1);
  7461. pc_setstat(sd, SP_AGI, 1);
  7462. pc_setstat(sd, SP_VIT, 1);
  7463. pc_setstat(sd, SP_INT, 1);
  7464. pc_setstat(sd, SP_DEX, 1);
  7465. pc_setstat(sd, SP_LUK, 1);
  7466. pc_setstat(sd, SP_POW, 0);
  7467. pc_setstat(sd, SP_STA, 0);
  7468. pc_setstat(sd, SP_WIS, 0);
  7469. pc_setstat(sd, SP_SPL, 0);
  7470. pc_setstat(sd, SP_CON, 0);
  7471. pc_setstat(sd, SP_CRT, 0);
  7472. clif_updatestatus(sd,SP_STR);
  7473. clif_updatestatus(sd,SP_AGI);
  7474. clif_updatestatus(sd,SP_VIT);
  7475. clif_updatestatus(sd,SP_INT);
  7476. clif_updatestatus(sd,SP_DEX);
  7477. clif_updatestatus(sd,SP_LUK);
  7478. clif_updatestatus(sd,SP_POW);
  7479. clif_updatestatus(sd,SP_STA);
  7480. clif_updatestatus(sd,SP_WIS);
  7481. clif_updatestatus(sd,SP_SPL);
  7482. clif_updatestatus(sd,SP_CON);
  7483. clif_updatestatus(sd,SP_CRT);
  7484. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7485. clif_updatestatus(sd,SP_UAGI);
  7486. clif_updatestatus(sd,SP_UVIT);
  7487. clif_updatestatus(sd,SP_UINT);
  7488. clif_updatestatus(sd,SP_UDEX);
  7489. clif_updatestatus(sd,SP_ULUK); // End Addition
  7490. clif_updatestatus(sd,SP_UPOW);
  7491. clif_updatestatus(sd,SP_USTA);
  7492. clif_updatestatus(sd,SP_UWIS);
  7493. clif_updatestatus(sd,SP_USPL);
  7494. clif_updatestatus(sd,SP_UCON);
  7495. clif_updatestatus(sd,SP_UCRT);
  7496. clif_updatestatus(sd,SP_STATUSPOINT);
  7497. clif_updatestatus(sd,SP_TRAITPOINT);
  7498. if( sd->mission_mobid ) { //bugreport:2200
  7499. sd->mission_mobid = 0;
  7500. sd->mission_count = 0;
  7501. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7502. }
  7503. status_calc_pc(sd, SCO_NONE);
  7504. return 1;
  7505. }
  7506. /*==========================================
  7507. * /resetskill
  7508. * if flag&1, perform block resync and status_calc call.
  7509. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7510. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7511. *------------------------------------------*/
  7512. int pc_resetskill(struct map_session_data* sd, int flag)
  7513. {
  7514. int i, skill_point=0;
  7515. nullpo_ret(sd);
  7516. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7517. return 0;
  7518. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7519. /**
  7520. * 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)
  7521. **/
  7522. if( pc_is_taekwon_ranker(sd) )
  7523. return 0;
  7524. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7525. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7526. i = sd->sc.option;
  7527. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7528. i &= ~OPTION_RIDING;
  7529. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7530. i &= ~OPTION_FALCON;
  7531. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7532. i &= ~OPTION_DRAGON;
  7533. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7534. i &= ~OPTION_WUG;
  7535. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7536. i &= ~OPTION_WUGRIDER;
  7537. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7538. i &= ~OPTION_MADOGEAR;
  7539. #ifndef NEW_CARTS
  7540. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7541. i &= ~OPTION_CART;
  7542. #else
  7543. if( sd->sc.data[SC_PUSH_CART] )
  7544. pc_setcart(sd, 0);
  7545. #endif
  7546. if( i != sd->sc.option )
  7547. pc_setoption(sd, i);
  7548. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7549. hom_vaporize(sd, HOM_ST_ACTIVE);
  7550. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  7551. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7552. if (sd->sc.data[SC_SOULATTACK] && pc_checkskill(sd, SU_SOULATTACK))
  7553. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  7554. }
  7555. for (const auto &skill : skill_db) {
  7556. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7557. uint8 lv = sd->status.skill[idx].lv;
  7558. if (lv == 0 || skill_id == 0)
  7559. continue;
  7560. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7561. continue;
  7562. // Don't reset trick dead if not a novice/baby
  7563. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7564. {
  7565. sd->status.skill[idx].lv = 0;
  7566. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7567. continue;
  7568. }
  7569. // do not reset basic skill
  7570. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7571. continue;
  7572. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7573. continue;
  7574. if( flag&4 && !skill_ischangesex(skill_id) )
  7575. continue;
  7576. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7577. { //Only handle quest skills in a special way when you can't learn them manually
  7578. if( battle_config.quest_skill_reset && !(flag&2) )
  7579. { //Wipe them
  7580. sd->status.skill[idx].lv = 0;
  7581. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7582. }
  7583. continue;
  7584. }
  7585. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7586. skill_point += lv;
  7587. else
  7588. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7589. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7590. if( !(flag&2) )
  7591. {// reset
  7592. sd->status.skill[idx].lv = 0;
  7593. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7594. }
  7595. }
  7596. if( flag&2 || !skill_point ) return skill_point;
  7597. sd->status.skill_point += skill_point;
  7598. if (flag&1) {
  7599. clif_updatestatus(sd,SP_SKILLPOINT);
  7600. clif_skillinfoblock(sd);
  7601. status_calc_pc(sd, SCO_FORCE);
  7602. }
  7603. return skill_point;
  7604. }
  7605. /*==========================================
  7606. * /resetfeel [Komurka]
  7607. *------------------------------------------*/
  7608. int pc_resetfeel(struct map_session_data* sd)
  7609. {
  7610. int i;
  7611. nullpo_ret(sd);
  7612. for (i=0; i<MAX_PC_FEELHATE; i++)
  7613. {
  7614. sd->feel_map[i].m = -1;
  7615. sd->feel_map[i].index = 0;
  7616. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7617. }
  7618. return 0;
  7619. }
  7620. int pc_resethate(struct map_session_data* sd)
  7621. {
  7622. int i;
  7623. nullpo_ret(sd);
  7624. for (i=0; i<MAX_PC_FEELHATE; i++)
  7625. {
  7626. sd->hate_mob[i] = -1;
  7627. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7628. }
  7629. return 0;
  7630. }
  7631. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7632. {
  7633. int bonus = 0;
  7634. nullpo_ret(sd);
  7635. skill_id = skill_dummy2skill_id(skill_id);
  7636. for (auto &it : sd->skillatk) {
  7637. if (it.id == skill_id) {
  7638. bonus += it.val;
  7639. break;
  7640. }
  7641. }
  7642. return bonus;
  7643. }
  7644. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7645. {
  7646. int bonus = 0;
  7647. nullpo_ret(sd);
  7648. skill_id = skill_dummy2skill_id(skill_id);
  7649. for (auto &it : sd->subskill) {
  7650. if (it.id == skill_id) {
  7651. bonus += it.val;
  7652. break;
  7653. }
  7654. }
  7655. return bonus;
  7656. }
  7657. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7658. int bonus = sd->bonus.add_heal_rate;
  7659. nullpo_ret(sd);
  7660. skill_id = skill_dummy2skill_id(skill_id);
  7661. if( bonus ) {
  7662. switch( skill_id ) {
  7663. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7664. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7665. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7666. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7667. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7668. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  7669. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  7670. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  7671. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  7672. }
  7673. }
  7674. for (auto &it : sd->skillheal) {
  7675. if (it.id == skill_id) {
  7676. bonus += it.val;
  7677. break;
  7678. }
  7679. }
  7680. return bonus;
  7681. }
  7682. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7683. int bonus = sd->bonus.add_heal2_rate;
  7684. skill_id = skill_dummy2skill_id(skill_id);
  7685. for (auto &it : sd->skillheal2) {
  7686. if (it.id == skill_id) {
  7687. bonus += it.val;
  7688. break;
  7689. }
  7690. }
  7691. return bonus;
  7692. }
  7693. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7694. {
  7695. if( !pc_isdead(sd) )
  7696. return; // not applicable
  7697. if( sd->bg_id && bg_member_respawn(sd) )
  7698. return; // member revived by battleground
  7699. pc_setstand(sd, true);
  7700. pc_setrestartvalue(sd,3);
  7701. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7702. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7703. }
  7704. static TIMER_FUNC(pc_respawn_timer){
  7705. struct map_session_data *sd = map_id2sd(id);
  7706. if( sd != NULL )
  7707. {
  7708. sd->pvp_point=0;
  7709. sd->respawn_tid = INVALID_TIMER;
  7710. pc_respawn(sd,CLR_OUTSIGHT);
  7711. }
  7712. return 0;
  7713. }
  7714. /*==========================================
  7715. * Invoked when a player has received damage
  7716. *------------------------------------------*/
  7717. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  7718. {
  7719. if (ap) clif_updatestatus(sd,SP_AP);
  7720. if (sp) clif_updatestatus(sd,SP_SP);
  7721. if (hp) clif_updatestatus(sd,SP_HP);
  7722. else return;
  7723. if (!src)
  7724. return;
  7725. if( pc_issit(sd) ) {
  7726. pc_setstand(sd, true);
  7727. skill_sit(sd,0);
  7728. }
  7729. if (sd->progressbar.npc_id)
  7730. clif_progressbar_abort(sd);
  7731. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7732. pet_target_check(sd->pd,src,1);
  7733. if( sd->status.ele_id > 0 )
  7734. elemental_set_target(sd,src);
  7735. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7736. sd->canlog_tick = gettick();
  7737. }
  7738. TIMER_FUNC(pc_close_npc_timer){
  7739. TBL_PC *sd = map_id2sd(id);
  7740. if(sd) pc_close_npc(sd,data);
  7741. return 0;
  7742. }
  7743. /**
  7744. * Method to properly close a NPC for player and clear anything related.
  7745. * @param sd: Player attached
  7746. * @param flag: Method of closure
  7747. * 1: Produce a close button and end the NPC
  7748. * 2: End the NPC (best for no dialog windows)
  7749. */
  7750. void pc_close_npc(struct map_session_data *sd,int flag)
  7751. {
  7752. nullpo_retv(sd);
  7753. if (sd->npc_id || sd->npc_shopid) {
  7754. if (sd->state.using_fake_npc) {
  7755. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7756. sd->state.using_fake_npc = 0;
  7757. }
  7758. if (sd->st) {
  7759. if(sd->st->state == RUN){ //wait ending code execution
  7760. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7761. return;
  7762. }
  7763. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7764. sd->st->mes_active = 0;
  7765. }
  7766. sd->state.menu_or_input = 0;
  7767. sd->npc_menu = 0;
  7768. sd->npc_shopid = 0;
  7769. #ifdef SECURE_NPCTIMEOUT
  7770. if( sd->npc_idle_timer != INVALID_TIMER ){
  7771. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  7772. sd->npc_idle_timer = INVALID_TIMER;
  7773. }
  7774. #endif
  7775. if (sd->st) {
  7776. if (sd->st->state == CLOSE) {
  7777. clif_scriptclose(sd, sd->npc_id);
  7778. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7779. sd->st->state = END; // Force to end now
  7780. }
  7781. if (sd->st->state == END) { // free attached scripts that are waiting
  7782. script_free_state(sd->st);
  7783. sd->st = NULL;
  7784. sd->npc_id = 0;
  7785. }
  7786. }
  7787. }
  7788. }
  7789. /*==========================================
  7790. * Invoked when a player has negative current hp
  7791. *------------------------------------------*/
  7792. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7793. {
  7794. int i=0,k=0;
  7795. t_tick tick = gettick();
  7796. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7797. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7798. // Super Novices have no kill or die functions attached when saved by their angel
  7799. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7800. t_exp exp = pc_nextbaseexp(sd);
  7801. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  7802. sd->state.snovice_dead_flag = 1;
  7803. pc_setrestartvalue(sd,1);
  7804. status_percent_heal(&sd->bl, 100, 100);
  7805. clif_resurrection(&sd->bl, 1);
  7806. if(battle_config.pc_invincible_time)
  7807. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7808. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  7809. if(mapdata_flag_gvg2(mapdata))
  7810. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7811. return 0;
  7812. }
  7813. }
  7814. for(k = 0; k < MAX_DEVOTION; k++) {
  7815. if (sd->devotion[k]){
  7816. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7817. if (devsd)
  7818. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7819. sd->devotion[k] = 0;
  7820. }
  7821. }
  7822. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7823. if (sd->stellar_mark[k]) {
  7824. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7825. if (smarksd)
  7826. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7827. sd->stellar_mark[k] = 0;
  7828. }
  7829. }
  7830. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7831. if (sd->united_soul[k]) {
  7832. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7833. if (usoulsd)
  7834. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7835. sd->united_soul[k] = 0;
  7836. }
  7837. }
  7838. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  7839. if (sd->servant_sign[k]) {
  7840. struct map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  7841. if (ssignsd)
  7842. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN, INVALID_TIMER);
  7843. sd->servant_sign[k] = 0;
  7844. }
  7845. }
  7846. if(sd->shadowform_id) { //if we were target of shadowform
  7847. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7848. sd->shadowform_id = 0; //should be remove on status end anyway
  7849. }
  7850. if(sd->status.pet_id > 0 && sd->pd) {
  7851. struct pet_data *pd = sd->pd;
  7852. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7853. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7854. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7855. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7856. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7857. }
  7858. if( sd->pd->target_id ) // Unlock all targets...
  7859. pet_unlocktarget(sd->pd);
  7860. }
  7861. if (hom_is_active(sd->hd) && battle_config.homunculus_auto_vapor && get_percentage(sd->hd->battle_status.hp, sd->hd->battle_status.max_hp) >= battle_config.homunculus_auto_vapor)
  7862. hom_vaporize(sd, HOM_ST_ACTIVE);
  7863. if( sd->md )
  7864. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7865. if( sd->ed )
  7866. elemental_delete(sd->ed);
  7867. // Leave duel if you die [LuzZza]
  7868. if(battle_config.duel_autoleave_when_die) {
  7869. if(sd->duel_group > 0)
  7870. duel_leave(sd->duel_group, sd);
  7871. if(sd->duel_invite > 0)
  7872. duel_reject(sd->duel_invite, sd);
  7873. }
  7874. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  7875. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  7876. sd->skill_keep_using.tid = INVALID_TIMER;
  7877. }
  7878. pc_close_npc(sd,2); //close npc if we were using one
  7879. /* e.g. not killed thru pc_damage */
  7880. if( pc_issit(sd) ) {
  7881. clif_status_load(&sd->bl,EFST_SIT,0);
  7882. }
  7883. pc_setdead(sd);
  7884. clif_party_dead( sd );
  7885. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  7886. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7887. if (battle_config.loose_ap_on_death == 1)
  7888. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  7889. //Reset menu skills/item skills
  7890. if ((sd->skillitem) != 0)
  7891. sd->skillitem = sd->skillitemlv = 0;
  7892. if ((sd->menuskill_id) != 0)
  7893. sd->menuskill_id = sd->menuskill_val = 0;
  7894. //Reset ticks.
  7895. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7896. if ( sd->spiritball !=0 )
  7897. pc_delspiritball(sd,sd->spiritball,0);
  7898. if (sd->soulball != 0)
  7899. pc_delsoulball(sd, sd->soulball, false);
  7900. if (sd->servantball != 0)
  7901. pc_delservantball( *sd, sd->servantball );
  7902. if (sd->abyssball != 0)
  7903. pc_delabyssball( *sd, sd->abyssball );
  7904. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7905. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7906. if (src)
  7907. switch (src->type) {
  7908. case BL_MOB:
  7909. {
  7910. struct mob_data *md=(struct mob_data *)src;
  7911. if(md->target_id==sd->bl.id)
  7912. mob_unlocktarget(md,tick);
  7913. if(battle_config.mobs_level_up && md->status.hp &&
  7914. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7915. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7916. ) { // monster level up [Valaris]
  7917. clif_misceffect(&md->bl,0);
  7918. md->level++;
  7919. status_calc_mob(md, SCO_NONE);
  7920. status_percent_heal(src,10,0);
  7921. if( battle_config.show_mob_info&4 )
  7922. {// update name with new level
  7923. clif_name_area(&md->bl);
  7924. }
  7925. }
  7926. src = battle_get_master(src); // Maybe Player Summon
  7927. }
  7928. break;
  7929. case BL_PET: //Pass on to master...
  7930. case BL_HOM:
  7931. case BL_MER:
  7932. src = battle_get_master(src);
  7933. break;
  7934. }
  7935. if (src && src->type == BL_PC) {
  7936. struct map_session_data *ssd = (struct map_session_data *)src;
  7937. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7938. npc_script_event(ssd, NPCE_KILLPC);
  7939. if (battle_config.pk_mode&2) {
  7940. ssd->status.manner -= 5;
  7941. if(ssd->status.manner < 0)
  7942. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7943. #if 0
  7944. // PK/Karma system code (not enabled yet) [celest]
  7945. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7946. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7947. // karma going down = more 'good' / more honourable.
  7948. // The Karma System way...
  7949. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7950. sd->status.karma--;
  7951. ssd->status.karma--;
  7952. }
  7953. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7954. ssd->status.karma++;
  7955. // or the PK System way...
  7956. if (sd->status.karma > 0) // player killed is dishonourable?
  7957. ssd->status.karma--; // honour points earned
  7958. sd->status.karma++; // honour points lost
  7959. // To-do: Receive exp on certain occasions
  7960. #endif
  7961. }
  7962. }
  7963. if(battle_config.bone_drop==2
  7964. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7965. {
  7966. struct item item_tmp;
  7967. memset(&item_tmp,0,sizeof(item_tmp));
  7968. item_tmp.nameid=ITEMID_SKULL_;
  7969. item_tmp.identify=1;
  7970. item_tmp.card[0]=CARD0_CREATE;
  7971. item_tmp.card[1]=0;
  7972. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7973. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7974. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7975. }
  7976. //Remove bonus_script when dead
  7977. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7978. // changed penalty options, added death by player if pk_mode [Valaris]
  7979. if(battle_config.death_penalty_type
  7980. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7981. && !sd->sc.cant.deathpenalty
  7982. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7983. {
  7984. t_exp base_penalty = 0;
  7985. t_exp job_penalty = 0;
  7986. uint32 zeny_penalty = 0;
  7987. if (pc_isvip(sd)) { // EXP penalty for VIP
  7988. base_penalty = battle_config.vip_exp_penalty_base;
  7989. job_penalty = battle_config.vip_exp_penalty_job;
  7990. zeny_penalty = battle_config.vip_zeny_penalty;
  7991. } else {
  7992. base_penalty = battle_config.death_penalty_base;
  7993. job_penalty = battle_config.death_penalty_job;
  7994. zeny_penalty = battle_config.zeny_penalty;
  7995. }
  7996. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7997. switch (battle_config.death_penalty_type) {
  7998. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7999. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8000. }
  8001. if (base_penalty){ //recheck after altering to speedup
  8002. if (battle_config.pk_mode && src && src->type==BL_PC)
  8003. base_penalty *= 2;
  8004. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8005. }
  8006. }
  8007. else
  8008. base_penalty = 0;
  8009. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8010. switch (battle_config.death_penalty_type) {
  8011. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8012. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8013. }
  8014. if (job_penalty) {
  8015. if (battle_config.pk_mode && src && src->type==BL_PC)
  8016. job_penalty *= 2;
  8017. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8018. }
  8019. }
  8020. else
  8021. job_penalty = 0;
  8022. if (base_penalty || job_penalty) {
  8023. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8024. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8025. pc_lostexp(sd, base_penalty, job_penalty);
  8026. }
  8027. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8028. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8029. if(zeny_penalty)
  8030. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8031. }
  8032. }
  8033. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8034. for (const auto &it : mapdata->drop_list) {
  8035. int id = it.drop_id, per = it.drop_per;
  8036. enum e_nightmare_drop_type type = it.drop_type;
  8037. if(id == 0)
  8038. continue;
  8039. if(id == -1){
  8040. int eq_num=0,eq_n[MAX_INVENTORY];
  8041. memset(eq_n,0,sizeof(eq_n));
  8042. for(i=0;i<MAX_INVENTORY;i++) {
  8043. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8044. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8045. || type&NMDT_ALL)
  8046. {
  8047. int l;
  8048. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8049. if( l < MAX_INVENTORY )
  8050. eq_n[l] = i;
  8051. eq_num++;
  8052. }
  8053. }
  8054. if(eq_num > 0){
  8055. int n = eq_n[rnd()%eq_num];
  8056. if(rnd()%10000 < per) {
  8057. if(sd->inventory.u.items_inventory[n].equip)
  8058. pc_unequipitem(sd,n,3);
  8059. pc_dropitem(sd,n,1);
  8060. }
  8061. }
  8062. }
  8063. else if(id > 0) {
  8064. for(i=0;i<MAX_INVENTORY;i++){
  8065. if(sd->inventory.u.items_inventory[i].nameid == id
  8066. && rnd()%10000 < per
  8067. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8068. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8069. || type&NMDT_ALL) ){
  8070. if(sd->inventory.u.items_inventory[i].equip)
  8071. pc_unequipitem(sd,i,3);
  8072. pc_dropitem(sd,i,1);
  8073. break;
  8074. }
  8075. }
  8076. }
  8077. }
  8078. }
  8079. // pvp
  8080. // disable certain pvp functions on pk_mode [Valaris]
  8081. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8082. sd->pvp_point -= 5;
  8083. sd->pvp_lost++;
  8084. if( src && src->type == BL_PC ) {
  8085. struct map_session_data *ssd = (struct map_session_data *)src;
  8086. ssd->pvp_point++;
  8087. ssd->pvp_won++;
  8088. }
  8089. if( sd->pvp_point < 0 ) {
  8090. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8091. return 1|8;
  8092. }
  8093. }
  8094. //GvG
  8095. if( mapdata_flag_gvg2(mapdata) ) {
  8096. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8097. return 1|8;
  8098. }
  8099. else if( sd->bg_id ) {
  8100. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8101. if (bg) {
  8102. if (bg->cemetery.map > 0) { // Respawn by BG
  8103. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8104. return 1|8;
  8105. }
  8106. }
  8107. }
  8108. //Reset "can log out" tick.
  8109. if( battle_config.prevent_logout )
  8110. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8111. return 1;
  8112. }
  8113. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8114. if(hp) clif_updatestatus(sd,SP_HP);
  8115. if(sp) clif_updatestatus(sd,SP_SP);
  8116. if(ap) clif_updatestatus(sd,SP_AP);
  8117. pc_setstand(sd, true);
  8118. if(battle_config.pc_invincible_time > 0)
  8119. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8120. if( sd->state.gmaster_flag ) {
  8121. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8122. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8123. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8124. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8125. }
  8126. }
  8127. bool pc_revive_item(struct map_session_data *sd) {
  8128. nullpo_retr(false, sd);
  8129. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8130. return false;
  8131. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8132. return false;
  8133. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8134. uint8 hp = 100, sp = 100;
  8135. if (item_position < 0) {
  8136. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  8137. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  8138. sp = 0;
  8139. }
  8140. else
  8141. return false;
  8142. }
  8143. if (!status_revive(&sd->bl, hp, sp))
  8144. return false;
  8145. if (item_position < 0)
  8146. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  8147. else
  8148. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8149. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8150. return true;
  8151. }
  8152. // script
  8153. //
  8154. /*==========================================
  8155. * script reading pc status registry
  8156. *------------------------------------------*/
  8157. int64 pc_readparam(struct map_session_data* sd,int64 type)
  8158. {
  8159. int64 val = 0;
  8160. nullpo_ret(sd);
  8161. switch(type) {
  8162. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8163. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8164. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8165. case SP_ZENY: val = sd->status.zeny; break;
  8166. case SP_BASELEVEL: val = sd->status.base_level; break;
  8167. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8168. case SP_CLASS: val = sd->status.class_; break;
  8169. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8170. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8171. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8172. case SP_SEX: val = sd->status.sex; break;
  8173. case SP_WEIGHT: val = sd->weight; break;
  8174. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8175. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8176. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8177. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8178. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8179. case SP_HP: val = sd->battle_status.hp; break;
  8180. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8181. case SP_SP: val = sd->battle_status.sp; break;
  8182. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8183. case SP_AP: val = sd->battle_status.ap; break;
  8184. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8185. case SP_STR: val = sd->status.str; break;
  8186. case SP_AGI: val = sd->status.agi; break;
  8187. case SP_VIT: val = sd->status.vit; break;
  8188. case SP_INT: val = sd->status.int_; break;
  8189. case SP_DEX: val = sd->status.dex; break;
  8190. case SP_LUK: val = sd->status.luk; break;
  8191. case SP_POW: val = sd->status.pow; break;
  8192. case SP_STA: val = sd->status.sta; break;
  8193. case SP_WIS: val = sd->status.wis; break;
  8194. case SP_SPL: val = sd->status.spl; break;
  8195. case SP_CON: val = sd->status.con; break;
  8196. case SP_CRT: val = sd->status.crt; break;
  8197. case SP_KARMA: val = sd->status.karma; break;
  8198. case SP_MANNER: val = sd->status.manner; break;
  8199. case SP_FAME: val = sd->status.fame; break;
  8200. case SP_KILLERRID: val = sd->killerrid; break;
  8201. case SP_KILLEDRID: val = sd->killedrid; break;
  8202. case SP_KILLEDGID: val = sd->killedgid; break;
  8203. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8204. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8205. case SP_CHARRENAME: val = sd->status.rename; break;
  8206. case SP_CHARFONT: val = sd->status.font; break;
  8207. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8208. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8209. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8210. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8211. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8212. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8213. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8214. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8215. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8216. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8217. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8218. case SP_BASE_ATK:
  8219. #ifdef RENEWAL
  8220. val = sd->bonus.eatk;
  8221. #else
  8222. val = sd->battle_status.batk;
  8223. #endif
  8224. break;
  8225. case SP_DEF1: val = sd->battle_status.def; break;
  8226. case SP_DEF2: val = sd->battle_status.def2; break;
  8227. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8228. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8229. case SP_HIT: val = sd->battle_status.hit; break;
  8230. case SP_FLEE1: val = sd->battle_status.flee; break;
  8231. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8232. case SP_PATK: val = sd->battle_status.patk; break;
  8233. case SP_SMATK: val = sd->battle_status.smatk; break;
  8234. case SP_RES: val = sd->battle_status.res; break;
  8235. case SP_MRES: val = sd->battle_status.mres; break;
  8236. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8237. case SP_CRATE: val = sd->battle_status.crate; break;
  8238. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8239. case SP_MAXHPRATE: val = sd->hprate; break;
  8240. case SP_MAXSPRATE: val = sd->sprate; break;
  8241. case SP_MAXAPRATE: val = sd->aprate; break;
  8242. case SP_SPRATE: val = sd->dsprate; break;
  8243. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8244. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8245. case SP_ASPD_RATE:
  8246. #ifndef RENEWAL_ASPD
  8247. val = sd->battle_status.aspd_rate;
  8248. #else
  8249. val = sd->battle_status.aspd_rate2;
  8250. #endif
  8251. break;
  8252. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8253. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8254. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8255. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8256. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8257. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8258. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8259. case SP_MATK_RATE: val = sd->matk_rate; break;
  8260. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8261. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8262. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8263. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8264. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8265. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8266. case SP_HIT_RATE: val = sd->hit_rate; break;
  8267. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8268. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8269. case SP_DEF_RATE: val = sd->def_rate; break;
  8270. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8271. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8272. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8273. case SP_PATK_RATE: val = sd->patk_rate; break;
  8274. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8275. case SP_RES_RATE: val = sd->res_rate; break;
  8276. case SP_MRES_RATE: val = sd->mres_rate; break;
  8277. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8278. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8279. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8280. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8281. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8282. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8283. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8284. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8285. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8286. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8287. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8288. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8289. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8290. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8291. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8292. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8293. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8294. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8295. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8296. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8297. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8298. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8299. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8300. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8301. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8302. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8303. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8304. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8305. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8306. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8307. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8308. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8309. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8310. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8311. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8312. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8313. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8314. case SP_UNSTRIPABLE:
  8315. case SP_UNSTRIPABLE_ARMOR:
  8316. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8317. break;
  8318. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8319. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8320. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8321. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8322. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8323. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8324. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8325. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8326. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8327. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8328. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8329. case SP_EMATK: val = sd->bonus.ematk; break;
  8330. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8331. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8332. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8333. case SP_CASTRATE:
  8334. case SP_VARCASTRATE:
  8335. #ifdef RENEWAL_CAST
  8336. val = sd->bonus.varcastrate; break;
  8337. #else
  8338. val = sd->castrate; break;
  8339. #endif
  8340. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8341. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8342. default:
  8343. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8344. return -1;
  8345. }
  8346. return val;
  8347. }
  8348. /*==========================================
  8349. * script set pc status registry
  8350. *------------------------------------------*/
  8351. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  8352. {
  8353. nullpo_retr(false,sd);
  8354. int val = static_cast<unsigned int>(val_tmp);
  8355. switch(type){
  8356. case SP_BASELEVEL:
  8357. if (val > pc_maxbaselv(sd)) //Capping to max
  8358. val = pc_maxbaselv(sd);
  8359. if (val > sd->status.base_level) {
  8360. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8361. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8362. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8363. }
  8364. }
  8365. sd->status.base_level = val;
  8366. sd->status.base_exp = 0;
  8367. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8368. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8369. clif_updatestatus(sd, SP_STATUSPOINT);
  8370. clif_updatestatus(sd, SP_TRAITPOINT);
  8371. clif_updatestatus(sd, SP_BASEEXP);
  8372. status_calc_pc(sd, SCO_FORCE);
  8373. if(sd->status.party_id)
  8374. party_send_levelup(sd);
  8375. break;
  8376. case SP_JOBLEVEL:
  8377. if (val >= sd->status.job_level) {
  8378. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8379. sd->status.skill_point += val - sd->status.job_level;
  8380. clif_updatestatus(sd, SP_SKILLPOINT);
  8381. }
  8382. sd->status.job_level = val;
  8383. sd->status.job_exp = 0;
  8384. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8385. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8386. clif_updatestatus(sd, SP_JOBEXP);
  8387. status_calc_pc(sd, SCO_FORCE);
  8388. break;
  8389. case SP_SKILLPOINT:
  8390. sd->status.skill_point = val;
  8391. break;
  8392. case SP_STATUSPOINT:
  8393. sd->status.status_point = val;
  8394. break;
  8395. case SP_TRAITPOINT:
  8396. sd->status.trait_point = val;
  8397. break;
  8398. case SP_ZENY:
  8399. if( val < 0 )
  8400. return false;// can't set negative zeny
  8401. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8402. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8403. break;
  8404. case SP_BASEEXP:
  8405. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8406. if (val_tmp < sd->status.base_exp) // Lost
  8407. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8408. else // Gained
  8409. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8410. return true;
  8411. case SP_JOBEXP:
  8412. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8413. if (val_tmp < sd->status.job_exp) // Lost
  8414. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8415. else // Gained
  8416. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8417. return true;
  8418. case SP_SEX:
  8419. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8420. break;
  8421. case SP_WEIGHT:
  8422. sd->weight = val;
  8423. break;
  8424. case SP_MAXWEIGHT:
  8425. sd->max_weight = val;
  8426. break;
  8427. case SP_HP:
  8428. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8429. break;
  8430. case SP_MAXHP:
  8431. if (sd->status.base_level < 100)
  8432. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8433. else if (sd->status.base_level < 151)
  8434. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8435. else
  8436. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8437. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8438. {
  8439. sd->battle_status.hp = sd->battle_status.max_hp;
  8440. clif_updatestatus(sd, SP_HP);
  8441. }
  8442. break;
  8443. case SP_SP:
  8444. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8445. break;
  8446. case SP_MAXSP:
  8447. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8448. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8449. {
  8450. sd->battle_status.sp = sd->battle_status.max_sp;
  8451. clif_updatestatus(sd, SP_SP);
  8452. }
  8453. break;
  8454. case SP_AP:
  8455. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8456. break;
  8457. case SP_MAXAP:
  8458. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8459. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8460. sd->battle_status.ap = sd->battle_status.max_ap;
  8461. clif_updatestatus(sd, SP_AP);
  8462. }
  8463. break;
  8464. case SP_STR:
  8465. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8466. break;
  8467. case SP_AGI:
  8468. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8469. break;
  8470. case SP_VIT:
  8471. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8472. break;
  8473. case SP_INT:
  8474. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8475. break;
  8476. case SP_DEX:
  8477. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8478. break;
  8479. case SP_LUK:
  8480. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8481. break;
  8482. case SP_POW:
  8483. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8484. break;
  8485. case SP_STA:
  8486. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8487. break;
  8488. case SP_WIS:
  8489. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8490. break;
  8491. case SP_SPL:
  8492. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8493. break;
  8494. case SP_CON:
  8495. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8496. break;
  8497. case SP_CRT:
  8498. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8499. break;
  8500. case SP_KARMA:
  8501. sd->status.karma = val;
  8502. break;
  8503. case SP_MANNER:
  8504. sd->status.manner = val;
  8505. if( val < 0 )
  8506. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8507. else {
  8508. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  8509. clif_manner_message(sd, 5);
  8510. }
  8511. return true; // status_change_start/status_change_end already sends packets warning the client
  8512. case SP_FAME:
  8513. sd->status.fame = val;
  8514. break;
  8515. case SP_KILLERRID:
  8516. sd->killerrid = val;
  8517. return true;
  8518. case SP_KILLEDRID:
  8519. sd->killedrid = val;
  8520. return true;
  8521. case SP_KILLEDGID:
  8522. sd->killedgid = val;
  8523. return true;
  8524. case SP_CHARMOVE:
  8525. sd->status.character_moves = val;
  8526. return true;
  8527. case SP_CHARRENAME:
  8528. sd->status.rename = val;
  8529. return true;
  8530. case SP_CHARFONT:
  8531. sd->status.font = val;
  8532. clif_font(sd);
  8533. return true;
  8534. case SP_BANK_VAULT:
  8535. if (val < 0)
  8536. return false;
  8537. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8538. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8539. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8540. return true;
  8541. case SP_ROULETTE_BRONZE:
  8542. sd->roulette_point.bronze = val;
  8543. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8544. return true;
  8545. case SP_ROULETTE_SILVER:
  8546. sd->roulette_point.silver = val;
  8547. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8548. return true;
  8549. case SP_ROULETTE_GOLD:
  8550. sd->roulette_point.gold = val;
  8551. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8552. return true;
  8553. case SP_CASHPOINTS:
  8554. if (val < 0)
  8555. return false;
  8556. if (!sd->state.connect_new)
  8557. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  8558. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  8559. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8560. return true;
  8561. case SP_KAFRAPOINTS:
  8562. if (val < 0)
  8563. return false;
  8564. if (!sd->state.connect_new)
  8565. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  8566. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  8567. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8568. return true;
  8569. case SP_PCDIECOUNTER:
  8570. if (val < 0)
  8571. return false;
  8572. if (sd->die_counter == val)
  8573. return true;
  8574. sd->die_counter = val;
  8575. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8576. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8577. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8578. return true;
  8579. case SP_COOKMASTERY:
  8580. if (sd->cook_mastery == val)
  8581. return true;
  8582. val = cap_value(val, 0, 1999);
  8583. sd->cook_mastery = val;
  8584. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8585. return true;
  8586. default:
  8587. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8588. return false;
  8589. }
  8590. clif_updatestatus(sd,static_cast<int>(type));
  8591. return true;
  8592. }
  8593. /*==========================================
  8594. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8595. *------------------------------------------*/
  8596. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  8597. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  8598. if (type&2) {
  8599. if (hp || type&4)
  8600. clif_heal(sd->fd,SP_HP,hp);
  8601. if (sp)
  8602. clif_heal(sd->fd,SP_SP,sp);
  8603. if (ap)
  8604. clif_heal(sd->fd,SP_AP,ap);
  8605. } else {
  8606. if(hp)
  8607. clif_updatestatus(sd,SP_HP);
  8608. if(sp)
  8609. clif_updatestatus(sd,SP_SP);
  8610. if (ap)
  8611. clif_updatestatus(sd,SP_AP);
  8612. }
  8613. return;
  8614. }
  8615. /**
  8616. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  8617. * @param sd: Player data
  8618. * @param itemid: Item ID
  8619. * @param hp: HP to heal
  8620. * @param sp: SP to heal
  8621. * @return Amount healed to an object
  8622. */
  8623. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  8624. {
  8625. int bonus, tmp, penalty = 0;
  8626. if (hp) {
  8627. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8628. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8629. if (potion_flag == 2) {
  8630. bonus += bonus * 50 / 100;
  8631. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  8632. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  8633. }
  8634. //All item bonuses.
  8635. bonus += sd->bonus.itemhealrate2;
  8636. //Item Group bonuses
  8637. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  8638. //Individual item bonuses.
  8639. for(const auto &it : sd->itemhealrate) {
  8640. if (it.id == itemid) {
  8641. bonus += bonus * it.val / 100;
  8642. break;
  8643. }
  8644. }
  8645. // Recovery Potion
  8646. if (sd->sc.data[SC_INCHEALRATE])
  8647. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  8648. // 2014 Halloween Event : Pumpkin Bonus
  8649. if (sd->sc.data[SC_MTF_PUMPKIN]) {
  8650. if (itemid == ITEMID_PUMPKIN)
  8651. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  8652. else if (itemid == ITEMID_COOKIE_BAT)
  8653. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val2;
  8654. }
  8655. tmp = hp * bonus / 100; // Overflow check
  8656. if (bonus != 100 && tmp > hp)
  8657. hp = tmp;
  8658. }
  8659. if (sp) {
  8660. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8661. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8662. if (potion_flag == 2)
  8663. bonus += bonus * 50 / 100;
  8664. // All item bonuses.
  8665. bonus += sd->bonus.itemsphealrate2;
  8666. // Item Group bonuses
  8667. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  8668. // Individual item bonuses.
  8669. for( const auto &it : sd->itemsphealrate ){
  8670. if( it.id == itemid ){
  8671. bonus += bonus * it.val / 100;
  8672. break;
  8673. }
  8674. }
  8675. tmp = sp * bonus / 100; // Overflow check
  8676. if (bonus != 100 && tmp > sp)
  8677. sp = tmp;
  8678. }
  8679. if (sd->sc.count) {
  8680. // Critical Wound and Death Hurt stack
  8681. if (sd->sc.data[SC_CRITICALWOUND])
  8682. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  8683. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  8684. penalty += 20;
  8685. if (sd->sc.data[SC_NORECOVER_STATE])
  8686. penalty = 100;
  8687. if (sd->sc.data[SC_VITALITYACTIVATION])
  8688. hp += hp / 2; // 1.5 times
  8689. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  8690. hp += hp / 10;
  8691. sp += sp / 10;
  8692. }
  8693. #ifdef RENEWAL
  8694. if (sd->sc.data[SC_APPLEIDUN])
  8695. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  8696. #endif
  8697. if (penalty > 0) {
  8698. hp -= hp * penalty / 100;
  8699. sp -= sp * penalty / 100;
  8700. }
  8701. #ifdef RENEWAL
  8702. if (sd->sc.data[SC_EXTREMITYFIST2])
  8703. sp = 0;
  8704. #endif
  8705. if (sd->sc.data[SC_BITESCAR])
  8706. hp = 0;
  8707. }
  8708. return status_heal(&sd->bl, hp, sp, 1);
  8709. }
  8710. /*==========================================
  8711. * HP/SP Recovery
  8712. * Heal player hp nad/or sp by rate
  8713. *------------------------------------------*/
  8714. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8715. {
  8716. nullpo_ret(sd);
  8717. if (hp > 100) hp = 100;
  8718. else if (hp <-100) hp = -100;
  8719. if (sp > 100) sp = 100;
  8720. else if (sp <-100) sp = -100;
  8721. if(hp >= 0 && sp >= 0) //Heal
  8722. return status_percent_heal(&sd->bl, hp, sp);
  8723. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8724. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8725. //Crossed signs
  8726. if(hp) {
  8727. if(hp > 0)
  8728. status_percent_heal(&sd->bl, hp, 0);
  8729. else
  8730. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8731. }
  8732. if(sp) {
  8733. if(sp > 0)
  8734. status_percent_heal(&sd->bl, 0, sp);
  8735. else
  8736. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8737. }
  8738. return 0;
  8739. }
  8740. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8741. {
  8742. struct mob_data *md;
  8743. int flag;
  8744. md = (struct mob_data *)bl;
  8745. nullpo_ret(md);
  8746. flag = va_arg(ap, int);
  8747. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8748. status_kill(&md->bl);
  8749. return 1;
  8750. }
  8751. /**
  8752. * Called when player changes job
  8753. * Rewrote to make it tidider [Celest]
  8754. * @param sd
  8755. * @param job JOB ID. See enum e_job
  8756. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8757. * @return True if success, false if failed
  8758. **/
  8759. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8760. {
  8761. int i, fame_flag = 0;
  8762. nullpo_retr(false,sd);
  8763. if (job < 0)
  8764. return false;
  8765. //Normalize job.
  8766. uint64 b_class = pc_jobid2mapid(job);
  8767. if (b_class == -1)
  8768. return false;
  8769. switch (upper) {
  8770. case 1:
  8771. b_class|= JOBL_UPPER;
  8772. break;
  8773. case 2:
  8774. b_class|= JOBL_BABY;
  8775. break;
  8776. }
  8777. //This will automatically adjust bard/dancer classes to the correct gender
  8778. //That is, if you try to jobchange into dancer, it will turn you to bard.
  8779. job = pc_mapid2jobid(b_class, sd->status.sex);
  8780. if (job == -1)
  8781. return false;
  8782. if ((unsigned short)b_class == sd->class_)
  8783. return false; //Nothing to change.
  8784. // If the job does not exist in the job db, dont allow changing to it
  8785. if( !job_db.exists( job ) ){
  8786. return false;
  8787. }
  8788. // changing from 1st to 2nd job
  8789. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  8790. sd->change_level_2nd = sd->status.job_level;
  8791. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  8792. }
  8793. // changing from 2nd to 3rd job
  8794. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  8795. sd->change_level_3rd = sd->status.job_level;
  8796. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  8797. }
  8798. // changing from 3rd to 4th job
  8799. else if ((b_class&JOBL_FOURTH) && !(sd->class_&JOBL_FOURTH)) {
  8800. sd->change_level_4th = sd->status.job_level;
  8801. pc_setglobalreg(sd, add_str(JOBCHANGE4TH_VAR), sd->change_level_4th);
  8802. }
  8803. if(sd->cloneskill_idx > 0) {
  8804. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8805. sd->status.skill[sd->cloneskill_idx].id = 0;
  8806. sd->status.skill[sd->cloneskill_idx].lv = 0;
  8807. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  8808. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  8809. }
  8810. sd->cloneskill_idx = 0;
  8811. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  8812. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  8813. }
  8814. if(sd->reproduceskill_idx > 0) {
  8815. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8816. sd->status.skill[sd->reproduceskill_idx].id = 0;
  8817. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  8818. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  8819. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  8820. }
  8821. sd->reproduceskill_idx = 0;
  8822. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8823. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8824. }
  8825. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8826. status_db.changeSkillTree(sd);
  8827. }
  8828. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8829. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  8830. pc_resetfeel(sd);
  8831. pc_resethate(sd);
  8832. }
  8833. // Reset body style to 0 before changing job to avoid
  8834. // errors since not every job has a alternate outfit.
  8835. sd->status.body = 0;
  8836. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8837. sd->status.class_ = job;
  8838. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8839. uint64 previous_class = sd->class_;
  8840. sd->class_ = (unsigned short)b_class;
  8841. sd->status.job_level=1;
  8842. sd->status.job_exp=0;
  8843. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8844. sd->status.base_level = pc_maxbaselv(sd);
  8845. sd->status.base_exp=0;
  8846. pc_resetstate(sd);
  8847. clif_updatestatus(sd,SP_STATUSPOINT);
  8848. clif_updatestatus(sd,SP_TRAITPOINT);
  8849. clif_updatestatus(sd,SP_BASELEVEL);
  8850. clif_updatestatus(sd,SP_BASEEXP);
  8851. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8852. }
  8853. // Give or reduce transcendent status points
  8854. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8855. sd->status.status_point += battle_config.transcendent_status_points;
  8856. clif_updatestatus(sd,SP_STATUSPOINT);
  8857. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8858. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8859. // The player already used his bonus points, so we have to reset his status points
  8860. pc_resetstate(sd);
  8861. }else{
  8862. sd->status.status_point -= battle_config.transcendent_status_points;
  8863. clif_updatestatus(sd,SP_STATUSPOINT);
  8864. }
  8865. }
  8866. // Give or reduce trait status points
  8867. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  8868. sd->status.trait_point += battle_config.trait_points_job_change;
  8869. clif_updatestatus(sd, SP_TRAITPOINT);
  8870. clif_updatestatus(sd, SP_UPOW);
  8871. clif_updatestatus(sd, SP_USTA);
  8872. clif_updatestatus(sd, SP_UWIS);
  8873. clif_updatestatus(sd, SP_USPL);
  8874. clif_updatestatus(sd, SP_UCON);
  8875. clif_updatestatus(sd, SP_UCRT);
  8876. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  8877. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  8878. // Player may have already used the trait status points. Force a reset.
  8879. pc_resetstate(sd);
  8880. } else {
  8881. sd->status.trait_point = 0;
  8882. clif_updatestatus(sd, SP_TRAITPOINT);
  8883. clif_updatestatus(sd, SP_UPOW);
  8884. clif_updatestatus(sd, SP_USTA);
  8885. clif_updatestatus(sd, SP_UWIS);
  8886. clif_updatestatus(sd, SP_USPL);
  8887. clif_updatestatus(sd, SP_UCON);
  8888. clif_updatestatus(sd, SP_UCRT);
  8889. }
  8890. }
  8891. clif_updatestatus(sd,SP_JOBLEVEL);
  8892. clif_updatestatus(sd,SP_JOBEXP);
  8893. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8894. for(i=0;i<EQI_MAX;i++) {
  8895. if(sd->equip_index[i] >= 0)
  8896. if(pc_isequip(sd,sd->equip_index[i]))
  8897. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8898. }
  8899. //Change look, if disguised, you need to undisguise
  8900. //to correctly calculate new job sprite without
  8901. if (sd->disguise)
  8902. pc_disguise(sd, 0);
  8903. status_set_viewdata(&sd->bl, job);
  8904. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8905. #if PACKETVER >= 20151001
  8906. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8907. #endif
  8908. if(sd->vd.cloth_color)
  8909. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8910. /*
  8911. if(sd->vd.body_style)
  8912. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8913. */
  8914. //Update skill tree.
  8915. pc_calc_skilltree(sd);
  8916. clif_skillinfoblock(sd);
  8917. if (sd->ed)
  8918. elemental_delete(sd->ed);
  8919. if (sd->state.vending)
  8920. vending_closevending(sd);
  8921. if (sd->state.buyingstore)
  8922. buyingstore_close(sd);
  8923. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8924. //Remove peco/cart/falcon
  8925. i = sd->sc.option;
  8926. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8927. i&=~OPTION_RIDING;
  8928. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8929. i&=~OPTION_FALCON;
  8930. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8931. i&=~OPTION_DRAGON;
  8932. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8933. i&=~OPTION_WUGRIDER;
  8934. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8935. i&=~OPTION_WUG;
  8936. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8937. i&=~OPTION_MADOGEAR;
  8938. #ifndef NEW_CARTS
  8939. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8940. i&=~OPTION_CART;
  8941. #else
  8942. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8943. pc_setcart(sd, 0);
  8944. #endif
  8945. if(i != sd->sc.option)
  8946. pc_setoption(sd, i);
  8947. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8948. hom_vaporize(sd, HOM_ST_ACTIVE);
  8949. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8950. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8951. if (sd->sc.data[SC_SOULATTACK] && !pc_checkskill(sd, SU_SOULATTACK))
  8952. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  8953. if( sd->sc.data[SC_SPIRIT] ){
  8954. status_change_end( &sd->bl, SC_SPIRIT, INVALID_TIMER );
  8955. }
  8956. if(sd->status.manner < 0)
  8957. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8958. status_calc_pc(sd,SCO_FORCE);
  8959. pc_checkallowskill(sd);
  8960. pc_equiplookall(sd);
  8961. pc_show_questinfo(sd);
  8962. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8963. if( sd->status.party_id ){
  8964. struct party_data* p;
  8965. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8966. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8967. if( i < MAX_PARTY ){
  8968. p->party.member[i].class_ = sd->status.class_;
  8969. clif_party_job_and_level(sd);
  8970. }
  8971. }
  8972. }
  8973. chrif_save(sd, CSAVE_NORMAL);
  8974. //if you were previously famous, not anymore.
  8975. if (fame_flag)
  8976. chrif_buildfamelist();
  8977. else if (sd->status.fame > 0) {
  8978. //It may be that now they are famous?
  8979. switch (sd->class_&MAPID_UPPERMASK) {
  8980. case MAPID_BLACKSMITH:
  8981. case MAPID_ALCHEMIST:
  8982. case MAPID_TAEKWON:
  8983. chrif_buildfamelist();
  8984. break;
  8985. }
  8986. }
  8987. return true;
  8988. }
  8989. /*==========================================
  8990. * Tell client player sd has change equipement
  8991. *------------------------------------------*/
  8992. void pc_equiplookall(struct map_session_data *sd)
  8993. {
  8994. nullpo_retv(sd);
  8995. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8996. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8997. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8998. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8999. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9000. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9001. }
  9002. /*==========================================
  9003. * Tell client player sd has change look (hair,equip...)
  9004. *------------------------------------------*/
  9005. void pc_changelook(struct map_session_data *sd,int type,int val) {
  9006. nullpo_retv(sd);
  9007. switch(type) {
  9008. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9009. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9010. if (sd->status.hair != val) {
  9011. sd->status.hair = val;
  9012. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9013. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9014. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9015. }
  9016. break;
  9017. case LOOK_WEAPON:
  9018. sd->status.weapon = val;
  9019. break;
  9020. case LOOK_HEAD_BOTTOM:
  9021. sd->status.head_bottom = val;
  9022. sd->setlook_head_bottom = val;
  9023. break;
  9024. case LOOK_HEAD_TOP:
  9025. sd->status.head_top = val;
  9026. sd->setlook_head_top = val;
  9027. break;
  9028. case LOOK_HEAD_MID:
  9029. sd->status.head_mid = val;
  9030. sd->setlook_head_mid = val;
  9031. break;
  9032. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9033. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9034. if (sd->status.hair_color != val) {
  9035. sd->status.hair_color = val;
  9036. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9037. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9038. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9039. }
  9040. break;
  9041. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9042. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9043. sd->status.clothes_color = val;
  9044. break;
  9045. case LOOK_SHIELD:
  9046. sd->status.shield = val;
  9047. break;
  9048. case LOOK_SHOES:
  9049. break;
  9050. case LOOK_ROBE:
  9051. sd->status.robe = val;
  9052. sd->setlook_robe = val;
  9053. break;
  9054. case LOOK_BODY2:
  9055. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9056. sd->status.body = val;
  9057. break;
  9058. }
  9059. clif_changelook(&sd->bl, type, val);
  9060. }
  9061. /*==========================================
  9062. * Give an option (type) to player (sd) and display it to client
  9063. *------------------------------------------*/
  9064. void pc_setoption(struct map_session_data *sd,int type, int subtype)
  9065. {
  9066. int p_type, new_look=0;
  9067. nullpo_retv(sd);
  9068. p_type = sd->sc.option;
  9069. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9070. sd->sc.option=type;
  9071. clif_changeoption(&sd->bl);
  9072. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9073. { // Mounting
  9074. clif_status_load(&sd->bl,EFST_RIDING,1);
  9075. status_calc_pc(sd,SCO_NONE);
  9076. }
  9077. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9078. { // Dismount
  9079. clif_status_load(&sd->bl,EFST_RIDING,0);
  9080. status_calc_pc(sd,SCO_NONE);
  9081. }
  9082. #ifndef NEW_CARTS
  9083. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9084. clif_cartlist(sd);
  9085. clif_updatestatus(sd, SP_CARTINFO);
  9086. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9087. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9088. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9089. clif_clearcart(sd->fd);
  9090. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9091. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9092. }
  9093. #endif
  9094. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9095. clif_status_load(&sd->bl,EFST_FALCON,1);
  9096. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9097. clif_status_load(&sd->bl,EFST_FALCON,0);
  9098. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9099. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9100. status_calc_pc(sd,SCO_NONE);
  9101. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9102. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9103. status_calc_pc(sd,SCO_NONE);
  9104. }
  9105. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9106. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9107. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9108. status_change_end(&sd->bl, SC_MADOGEAR, INVALID_TIMER);
  9109. }
  9110. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9111. new_look = JOB_STAR_GLADIATOR2;
  9112. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9113. new_look = -1;
  9114. if (sd->disguise || !new_look)
  9115. return; //Disguises break sprite changes
  9116. if (new_look < 0) { //Restore normal look.
  9117. status_set_viewdata(&sd->bl, sd->status.class_);
  9118. new_look = sd->vd.class_;
  9119. }
  9120. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9121. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9122. if (sd->vd.cloth_color)
  9123. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9124. if( sd->vd.body_style )
  9125. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9126. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9127. }
  9128. /**
  9129. * Give player a cart
  9130. * @param sd Player
  9131. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9132. **/
  9133. bool pc_setcart(struct map_session_data *sd,int type) {
  9134. #ifndef NEW_CARTS
  9135. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9136. int option;
  9137. #endif
  9138. nullpo_retr(false,sd);
  9139. if( type < 0 || type > MAX_CARTS )
  9140. return false;// Never trust the values sent by the client! [Skotlex]
  9141. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9142. return false;// Push cart is required
  9143. #ifdef NEW_CARTS
  9144. switch( type ) {
  9145. case 0:
  9146. if( !sd->sc.data[SC_PUSH_CART] )
  9147. return 0;
  9148. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  9149. clif_clearcart(sd->fd);
  9150. break;
  9151. default:/* everything else is an allowed ID so we can move on */
  9152. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  9153. clif_cartlist(sd);
  9154. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9155. }
  9156. clif_updatestatus(sd, SP_CARTINFO);
  9157. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9158. break;
  9159. }
  9160. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9161. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9162. #else
  9163. // Update option
  9164. option = sd->sc.option;
  9165. option &= ~OPTION_CART;// clear cart bits
  9166. option |= cart[type]; // set cart
  9167. pc_setoption(sd, option);
  9168. #endif
  9169. return true;
  9170. }
  9171. /*==========================================
  9172. * Give player a falcon
  9173. *------------------------------------------*/
  9174. void pc_setfalcon(struct map_session_data* sd, int flag)
  9175. {
  9176. if( flag ){
  9177. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9178. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9179. } else if( pc_isfalcon(sd) ){
  9180. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9181. }
  9182. }
  9183. /*==========================================
  9184. * Set player riding
  9185. *------------------------------------------*/
  9186. void pc_setriding(struct map_session_data* sd, int flag)
  9187. {
  9188. if( sd->sc.data[SC_ALL_RIDING] )
  9189. return;
  9190. if( flag ){
  9191. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9192. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9193. } else if( pc_isriding(sd) ){
  9194. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9195. }
  9196. }
  9197. /**
  9198. * Give player a mado
  9199. * @param sd: Player
  9200. * @param flag: Enable or disable mado
  9201. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9202. */
  9203. void pc_setmadogear(struct map_session_data *sd, bool flag, e_mado_type type)
  9204. {
  9205. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9206. return;
  9207. if (flag) {
  9208. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9209. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9210. } else if (pc_ismadogear(sd)) {
  9211. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9212. }
  9213. }
  9214. /*==========================================
  9215. * Check if player can drop an item
  9216. *------------------------------------------*/
  9217. bool pc_candrop(struct map_session_data *sd, struct item *item)
  9218. {
  9219. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9220. return false;
  9221. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9222. return false;
  9223. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9224. }
  9225. /*==========================================
  9226. * Read '@type' variables (temporary numeric char reg)
  9227. *------------------------------------------*/
  9228. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  9229. {
  9230. return i64db_i64get(sd->regs.vars, reg);
  9231. }
  9232. /*==========================================
  9233. * Set '@type' variables (temporary numeric char reg)
  9234. *------------------------------------------*/
  9235. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  9236. {
  9237. uint32 index = script_getvaridx(reg);
  9238. nullpo_retr(false, sd);
  9239. if( val ) {
  9240. i64db_i64put(sd->regs.vars, reg, val);
  9241. if( index )
  9242. script_array_update(&sd->regs, reg, false);
  9243. } else {
  9244. i64db_remove(sd->regs.vars, reg);
  9245. if( index )
  9246. script_array_update(&sd->regs, reg, true);
  9247. }
  9248. return true;
  9249. }
  9250. /*==========================================
  9251. * Read '@type$' variables (temporary string char reg)
  9252. *------------------------------------------*/
  9253. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  9254. {
  9255. struct script_reg_str *p = NULL;
  9256. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9257. return p ? p->value : NULL;
  9258. }
  9259. /*==========================================
  9260. * Set '@type$' variables (temporary string char reg)
  9261. *------------------------------------------*/
  9262. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  9263. {
  9264. struct script_reg_str *p = NULL;
  9265. unsigned int index = script_getvaridx(reg);
  9266. DBData prev;
  9267. nullpo_retr(false, sd);
  9268. if( str[0] ) {
  9269. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9270. p->value = aStrdup(str);
  9271. p->flag.type = 1;
  9272. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9273. p = (struct script_reg_str *)db_data2ptr(&prev);
  9274. if( p->value )
  9275. aFree(p->value);
  9276. ers_free(str_reg_ers, p);
  9277. } else {
  9278. if( index )
  9279. script_array_update(&sd->regs, reg, false);
  9280. }
  9281. } else {
  9282. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9283. p = (struct script_reg_str *)db_data2ptr(&prev);
  9284. if( p->value )
  9285. aFree(p->value);
  9286. ers_free(str_reg_ers, p);
  9287. if( index )
  9288. script_array_update(&sd->regs, reg, true);
  9289. }
  9290. }
  9291. return true;
  9292. }
  9293. /**
  9294. * Serves the following variable types:
  9295. * - 'type' (permanent numeric char reg)
  9296. * - '#type' (permanent numeric account reg)
  9297. * - '##type' (permanent numeric account reg2)
  9298. **/
  9299. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  9300. {
  9301. struct script_reg_num *p = NULL;
  9302. if (!sd->vars_ok) {
  9303. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9304. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9305. //intif->request_registry(sd,type==3?4:type);
  9306. set_eof(sd->fd);
  9307. return 0;
  9308. }
  9309. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9310. return p ? p->value : 0;
  9311. }
  9312. /**
  9313. * Serves the following variable types:
  9314. * - 'type$' (permanent str char reg)
  9315. * - '#type$' (permanent str account reg)
  9316. * - '##type$' (permanent str account reg2)
  9317. **/
  9318. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  9319. {
  9320. struct script_reg_str *p = NULL;
  9321. if (!sd->vars_ok) {
  9322. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9323. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9324. //intif->request_registry(sd,type==3?4:type);
  9325. set_eof(sd->fd);
  9326. return NULL;
  9327. }
  9328. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9329. return p ? p->value : NULL;
  9330. }
  9331. /**
  9332. * Serves the following variable types:
  9333. * - 'type' (permanent numeric char reg)
  9334. * - '#type' (permanent numeric account reg)
  9335. * - '##type' (permanent numeric account reg2)
  9336. **/
  9337. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  9338. {
  9339. struct script_reg_num *p = NULL;
  9340. const char *regname = get_str(script_getvarid(reg));
  9341. uint32 index = script_getvaridx(reg);
  9342. if ( !reg_load && !sd->vars_ok ) {
  9343. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9344. return false;
  9345. }
  9346. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9347. if( val ) {
  9348. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9349. script_array_update(&sd->regs, reg, false);
  9350. p->value = val;
  9351. } else {
  9352. p->value = 0;
  9353. if( index )
  9354. script_array_update(&sd->regs, reg, true);
  9355. }
  9356. if (!reg_load)
  9357. p->flag.update = 1;/* either way, it will require either delete or replace */
  9358. } else if( val ) {
  9359. DBData prev;
  9360. if( index )
  9361. script_array_update(&sd->regs, reg, false);
  9362. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9363. p->value = val;
  9364. if (!reg_load)
  9365. p->flag.update = 1;
  9366. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9367. p = (struct script_reg_num *)db_data2ptr(&prev);
  9368. ers_free(num_reg_ers, p);
  9369. }
  9370. }
  9371. if (!reg_load && p)
  9372. sd->vars_dirty = true;
  9373. return true;
  9374. }
  9375. /**
  9376. * Serves the following variable types:
  9377. * - 'type$' (permanent str char reg)
  9378. * - '#type$' (permanent str account reg)
  9379. * - '##type$' (permanent str account reg2)
  9380. **/
  9381. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  9382. {
  9383. struct script_reg_str *p = NULL;
  9384. const char *regname = get_str(script_getvarid(reg));
  9385. unsigned int index = script_getvaridx(reg);
  9386. size_t vlen = 0;
  9387. if (!reg_load && !sd->vars_ok) {
  9388. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9389. return false;
  9390. }
  9391. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9392. {
  9393. ShowError("pc_check_RegistryVariableLength: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%zu\n", sd->status.account_id, sd->status.char_id, val, vlen);
  9394. return false;
  9395. }
  9396. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9397. if( val[0] ) {
  9398. if( p->value )
  9399. aFree(p->value);
  9400. else if ( index ) // an entry that was deleted, so we reset
  9401. script_array_update(&sd->regs, reg, false);
  9402. p->value = aStrdup(val);
  9403. } else {
  9404. if (p->value)
  9405. aFree(p->value);
  9406. p->value = NULL;
  9407. if( index )
  9408. script_array_update(&sd->regs, reg, true);
  9409. }
  9410. if( !reg_load )
  9411. p->flag.update = 1; // either way, it will require either delete or replace
  9412. } else if( val[0] ) {
  9413. DBData prev;
  9414. if( index )
  9415. script_array_update(&sd->regs, reg, false);
  9416. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9417. p->value = aStrdup(val);
  9418. if( !reg_load )
  9419. p->flag.update = 1;
  9420. p->flag.type = 1;
  9421. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9422. p = (struct script_reg_str *)db_data2ptr(&prev);
  9423. if( p->value )
  9424. aFree(p->value);
  9425. ers_free(str_reg_ers, p);
  9426. }
  9427. }
  9428. if( !reg_load && p )
  9429. sd->vars_dirty = true;
  9430. return true;
  9431. }
  9432. /**
  9433. * Set value of player variable
  9434. * @param sd Player
  9435. * @param reg Variable name
  9436. * @param value
  9437. * @return True if success, false if failed.
  9438. **/
  9439. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  9440. char prefix = reg[0];
  9441. nullpo_retr(false, sd);
  9442. if (reg[strlen(reg)-1] == '$') {
  9443. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9444. return false;
  9445. }
  9446. val = cap_value(val, INT_MIN, INT_MAX);
  9447. switch (prefix) {
  9448. case '.':
  9449. case '\'':
  9450. case '$':
  9451. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9452. return false;
  9453. case '@':
  9454. return pc_setreg(sd, add_str(reg), val);
  9455. case '#':
  9456. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9457. default:
  9458. return pc_setglobalreg(sd, add_str(reg), val);
  9459. }
  9460. return false;
  9461. }
  9462. /**
  9463. * Get value of player variable
  9464. * @param sd Player
  9465. * @param reg Variable name
  9466. * @return Variable value or 0 if failed.
  9467. **/
  9468. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  9469. char prefix = reg[0];
  9470. nullpo_ret(sd);
  9471. if (reg[strlen(reg)-1] == '$') {
  9472. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9473. return 0;
  9474. }
  9475. switch (prefix) {
  9476. case '.':
  9477. case '\'':
  9478. case '$':
  9479. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9480. return 0;
  9481. case '@':
  9482. return pc_readreg(sd, add_str(reg));
  9483. case '#':
  9484. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9485. default:
  9486. return pc_readglobalreg(sd, add_str(reg));
  9487. }
  9488. return 0;
  9489. }
  9490. /*==========================================
  9491. * Exec eventtimer for player sd (retrieved from map_session (id))
  9492. *------------------------------------------*/
  9493. static TIMER_FUNC(pc_eventtimer){
  9494. struct map_session_data *sd=map_id2sd(id);
  9495. char *p = (char *)data;
  9496. int i;
  9497. if(sd==NULL)
  9498. return 0;
  9499. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9500. if( i < MAX_EVENTTIMER )
  9501. {
  9502. sd->eventtimer[i] = INVALID_TIMER;
  9503. sd->eventcount--;
  9504. npc_event(sd,p,0);
  9505. }
  9506. else
  9507. ShowError("pc_eventtimer: no such event timer\n");
  9508. if (p) aFree(p);
  9509. return 0;
  9510. }
  9511. /*==========================================
  9512. * Add eventtimer for player sd ?
  9513. *------------------------------------------*/
  9514. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  9515. {
  9516. int i;
  9517. nullpo_retr(false,sd);
  9518. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9519. if( i == MAX_EVENTTIMER )
  9520. return false;
  9521. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9522. sd->eventcount++;
  9523. return true;
  9524. }
  9525. /*==========================================
  9526. * Del eventtimer for player sd ?
  9527. *------------------------------------------*/
  9528. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  9529. {
  9530. char* p = NULL;
  9531. int i;
  9532. nullpo_retr(false,sd);
  9533. if (sd->eventcount == 0)
  9534. return false;
  9535. // find the named event timer
  9536. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9537. sd->eventtimer[i] != INVALID_TIMER &&
  9538. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9539. strcmp(p, name) == 0
  9540. );
  9541. if( i == MAX_EVENTTIMER )
  9542. return false; // not found
  9543. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9544. sd->eventtimer[i] = INVALID_TIMER;
  9545. if(sd->eventcount > 0)
  9546. sd->eventcount--;
  9547. aFree(p);
  9548. return true;
  9549. }
  9550. /*==========================================
  9551. * Update eventtimer count for player sd
  9552. *------------------------------------------*/
  9553. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  9554. {
  9555. int i;
  9556. nullpo_retv(sd);
  9557. for(i=0;i<MAX_EVENTTIMER;i++)
  9558. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9559. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9560. addt_tickimer(sd->eventtimer[i],tick);
  9561. break;
  9562. }
  9563. }
  9564. /*==========================================
  9565. * Remove all eventtimer for player sd
  9566. *------------------------------------------*/
  9567. void pc_cleareventtimer(struct map_session_data *sd)
  9568. {
  9569. int i;
  9570. nullpo_retv(sd);
  9571. if (sd->eventcount == 0)
  9572. return;
  9573. for(i=0;i<MAX_EVENTTIMER;i++){
  9574. if( sd->eventtimer[i] != INVALID_TIMER ){
  9575. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9576. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9577. sd->eventtimer[i] = INVALID_TIMER;
  9578. if(sd->eventcount > 0) //avoid looping to max val
  9579. sd->eventcount--;
  9580. if (p) aFree(p);
  9581. }
  9582. }
  9583. }
  9584. /**
  9585. * Called when an item with combo is worn
  9586. * @param sd: Player data
  9587. * @param data: Item data
  9588. * @return Number of succeeded combo(s)
  9589. */
  9590. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  9591. int success = 0;
  9592. for (const auto &item_combo : data->combos) {
  9593. bool do_continue = false;
  9594. // Ensure this isn't a duplicate combo
  9595. for (const auto player_combo : sd->combos) {
  9596. if (player_combo->id == item_combo->id) {
  9597. do_continue = true;
  9598. break;
  9599. }
  9600. }
  9601. // Combo already equipped
  9602. if (do_continue)
  9603. continue;
  9604. size_t nb_itemCombo = item_combo->nameid.size();
  9605. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9606. continue;
  9607. struct s_itemchk {
  9608. int idx;
  9609. t_itemid nameid, card[MAX_SLOTS];
  9610. s_itemchk() : idx(0), nameid(0), card() {};
  9611. };
  9612. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9613. size_t j;
  9614. unsigned int pos = 0;
  9615. for (j = 0; j < nb_itemCombo; j++) {
  9616. t_itemid id = item_combo->nameid[j];
  9617. bool found = false;
  9618. for (int16 k = 0; k < EQI_MAX; k++) {
  9619. short index = sd->equip_index[k];
  9620. if (index < 0)
  9621. continue;
  9622. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  9623. continue;
  9624. if (!sd->inventory_data[index])
  9625. continue;
  9626. if (itemdb_type(id) != IT_CARD) {
  9627. if (sd->inventory_data[index]->nameid != id)
  9628. continue;
  9629. if (j > 0) { // Check if this item not already used
  9630. do_continue = false;
  9631. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  9632. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  9633. do_continue = true;
  9634. break;
  9635. }
  9636. }
  9637. if (do_continue)
  9638. continue;
  9639. }
  9640. combo_idx[j].nameid = id;
  9641. combo_idx[j].idx = index;
  9642. pos |= sd->inventory.u.items_inventory[index].equip;
  9643. found = true;
  9644. break;
  9645. } else { // Cards and enchants
  9646. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  9647. continue;
  9648. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  9649. do_continue = false;
  9650. if (sd->inventory.u.items_inventory[index].card[z] != id)
  9651. continue;
  9652. if (j > 0) {
  9653. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  9654. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  9655. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  9656. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  9657. do_continue = true;
  9658. break;
  9659. }
  9660. }
  9661. }
  9662. }
  9663. }
  9664. if (do_continue)
  9665. continue;
  9666. combo_idx[j].nameid = id;
  9667. combo_idx[j].idx = index;
  9668. combo_idx[j].card[z] = id;
  9669. pos |= sd->inventory.u.items_inventory[index].equip;
  9670. found = true;
  9671. break;
  9672. }
  9673. }
  9674. }
  9675. if (!found)
  9676. break; // Unable to found all the IDs for this combo, return
  9677. }
  9678. // Broke out of the count loop without finding all IDs, move to the next combo
  9679. if (j < nb_itemCombo)
  9680. continue;
  9681. // All items in the combo are matching
  9682. auto entry = std::make_shared<s_combos>();
  9683. entry->bonus = item_combo->script;
  9684. entry->id = item_combo->id;
  9685. entry->pos = pos;
  9686. sd->combos.push_back(entry);
  9687. combo_idx.clear();
  9688. success++;
  9689. }
  9690. return success;
  9691. }
  9692. /**
  9693. * Called when an item with combo is removed
  9694. * @param sd: Player data
  9695. * @param data: Item data
  9696. * @return Number of removed combo(s)
  9697. */
  9698. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  9699. if (sd->combos.empty())
  9700. return 0; // Nothing to do here, player has no combos
  9701. int retval = 0;
  9702. for (const auto &item_combo : data->combos) {
  9703. std::shared_ptr<s_combos> del_combo = nullptr;
  9704. // Check if this combo exists on this player
  9705. for (const auto &player_combo : sd->combos) {
  9706. if (player_combo->id == item_combo->id) {
  9707. del_combo = player_combo;
  9708. break;
  9709. }
  9710. }
  9711. // No match, skip this combo
  9712. if (del_combo == nullptr)
  9713. continue;
  9714. util::vector_erase_if_exists(sd->combos, del_combo);
  9715. retval++;
  9716. // Check if combo requirements still fit
  9717. if (pc_checkcombo(sd, data))
  9718. continue;
  9719. // It's empty, clear all the memory
  9720. if (sd->combos.empty()) {
  9721. sd->combos.clear();
  9722. return retval; // Return at this point as there are no more combos to check
  9723. }
  9724. }
  9725. return retval;
  9726. }
  9727. /**
  9728. * Load combo data(s) of player
  9729. * @param sd: Player data
  9730. * @return ret numbers of succeed combo
  9731. */
  9732. int pc_load_combo(struct map_session_data *sd) {
  9733. int ret = 0;
  9734. for (int16 i = 0; i < EQI_MAX; i++) {
  9735. item_data *id;
  9736. short idx = sd->equip_index[i];
  9737. if (idx < 0 || !(id = sd->inventory_data[idx]))
  9738. continue;
  9739. if (!id->combos.empty())
  9740. ret += pc_checkcombo(sd, id);
  9741. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9742. item_data *data;
  9743. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  9744. if (!sd->inventory.u.items_inventory[idx].card[j])
  9745. continue;
  9746. if ((data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j])) != nullptr) {
  9747. if (!data->combos.empty())
  9748. ret += pc_checkcombo(sd, data);
  9749. }
  9750. }
  9751. }
  9752. }
  9753. return ret;
  9754. }
  9755. /*==========================================
  9756. * Equip item on player sd at req_pos from inventory index n
  9757. * return: false - fail; true - success
  9758. *------------------------------------------*/
  9759. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9760. {
  9761. int i, pos, flag = 0, iflag;
  9762. struct item_data *id;
  9763. uint8 res = ITEM_EQUIP_ACK_OK;
  9764. short* equip_index;
  9765. nullpo_retr(false,sd);
  9766. if( n < 0 || n >= MAX_INVENTORY ) {
  9767. if( equipswitch ){
  9768. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9769. }else{
  9770. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9771. }
  9772. return false;
  9773. }
  9774. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9775. if( equipswitch ){
  9776. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9777. }else{
  9778. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9779. }
  9780. return false;
  9781. }
  9782. if (!(id = sd->inventory_data[n]))
  9783. return false;
  9784. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9785. if(battle_config.battle_log && !equipswitch)
  9786. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9787. if((res = pc_isequip(sd,n))) {
  9788. if( equipswitch ){
  9789. clif_equipswitch_add( sd, n, req_pos, res );
  9790. }else{
  9791. clif_equipitemack(sd,n,0,res); // fail
  9792. }
  9793. return false;
  9794. }
  9795. if( equipswitch && id->type == IT_AMMO ){
  9796. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9797. return false;
  9798. }
  9799. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9800. if( equipswitch ){
  9801. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9802. }else{
  9803. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9804. }
  9805. return false;
  9806. }
  9807. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9808. if( equipswitch ){
  9809. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9810. }else{
  9811. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9812. }
  9813. return false;
  9814. }
  9815. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9816. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9817. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9818. clif_notify_bindOnEquip(sd,n);
  9819. }
  9820. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9821. pos = req_pos&EQP_ACC;
  9822. if (pos == EQP_ACC) //User specified both slots.
  9823. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9824. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  9825. if (!sd->inventory.u.items_inventory[n].card[i])
  9826. continue;
  9827. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9828. if (card_data) {
  9829. int card_pos = card_data->equip;
  9830. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9831. pos = card_pos; // Use the card's equip position
  9832. break;
  9833. }
  9834. }
  9835. }
  9836. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9837. pos = (req_pos&EQP_ARMS);
  9838. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9839. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9840. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9841. pos = req_pos&EQP_SHADOW_ACC;
  9842. if (pos == EQP_SHADOW_ACC)
  9843. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9844. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9845. pos = (req_pos&EQP_SHADOW_ARMS);
  9846. if( pos == EQP_SHADOW_ARMS )
  9847. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9848. }
  9849. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9850. //Update skill-block range database when weapon range changes. [Skotlex]
  9851. i = equip_index[EQI_HAND_R];
  9852. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9853. flag = 1;
  9854. else
  9855. flag = id->range != sd->inventory_data[i]->range;
  9856. }
  9857. if( equipswitch ){
  9858. for( i = 0; i < EQI_MAX; i++ ){
  9859. if( pos&equip_bitmask[i] ){
  9860. // If there was already an item assigned to this slot
  9861. if( sd->equip_switch_index[i] >= 0 ){
  9862. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9863. }
  9864. // Assign the new index to it
  9865. sd->equip_switch_index[i] = n;
  9866. }
  9867. }
  9868. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9869. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9870. return true;
  9871. }else{
  9872. for(i=0;i<EQI_MAX;i++) {
  9873. if(pos & equip_bitmask[i]) {
  9874. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9875. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9876. sd->equip_index[i] = n;
  9877. }
  9878. }
  9879. pc_equipswitch_remove(sd, n);
  9880. if(pos==EQP_AMMO) {
  9881. clif_arrowequip(sd,n);
  9882. clif_arrow_fail(sd,3);
  9883. }
  9884. else
  9885. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9886. sd->inventory.u.items_inventory[n].equip = pos;
  9887. }
  9888. if(pos & EQP_HAND_R) {
  9889. if(id)
  9890. sd->weapontype1 = id->subtype;
  9891. else
  9892. sd->weapontype1 = W_FIST;
  9893. pc_calcweapontype(sd);
  9894. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9895. }
  9896. if(pos & EQP_HAND_L) {
  9897. if(id) {
  9898. if(id->type == IT_WEAPON) {
  9899. sd->status.shield = W_FIST;
  9900. sd->weapontype2 = id->subtype;
  9901. }
  9902. else
  9903. if(id->type == IT_ARMOR) {
  9904. sd->status.shield = id->look;
  9905. sd->weapontype2 = W_FIST;
  9906. }
  9907. }
  9908. else
  9909. sd->status.shield = sd->weapontype2 = W_FIST;
  9910. pc_calcweapontype(sd);
  9911. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9912. }
  9913. if(pos & EQP_SHOES)
  9914. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9915. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9916. short idx = sd->equip_index[EQI_AMMO];
  9917. if (idx >= 0) {
  9918. switch (sd->inventory_data[idx]->subtype) {
  9919. case AMMO_ARROW:
  9920. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  9921. pc_unequipitem(sd, idx, 2 | 4);
  9922. break;
  9923. case AMMO_BULLET:
  9924. case AMMO_SHELL:
  9925. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  9926. #ifdef RENEWAL
  9927. && id->subtype != W_GRENADE
  9928. #endif
  9929. )
  9930. pc_unequipitem(sd, idx, 2 | 4);
  9931. break;
  9932. #ifndef RENEWAL
  9933. case AMMO_GRENADE:
  9934. if (id->subtype != W_GRENADE)
  9935. pc_unequipitem(sd, idx, 2 | 4);
  9936. break;
  9937. #endif
  9938. }
  9939. }
  9940. }
  9941. pc_set_costume_view(sd);
  9942. pc_checkallowskill(sd); //Check if status changes should be halted.
  9943. iflag = sd->npc_item_flag;
  9944. // Check for combos (MUST be before status_calc_pc)
  9945. if (!id->combos.empty())
  9946. pc_checkcombo(sd, id);
  9947. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9948. ; // No cards
  9949. else {
  9950. for (i = 0; i < MAX_SLOTS; i++) {
  9951. item_data *data;
  9952. if (!sd->inventory.u.items_inventory[n].card[i])
  9953. continue;
  9954. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  9955. if (!data->combos.empty())
  9956. pc_checkcombo(sd, data);
  9957. }
  9958. }
  9959. }
  9960. status_calc_pc(sd,SCO_NONE);
  9961. if (flag) //Update skill data
  9962. clif_skillinfoblock(sd);
  9963. //OnEquip script [Skotlex]
  9964. if (id) {
  9965. //only run the script if item isn't restricted
  9966. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9967. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9968. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9969. ; //No cards
  9970. else {
  9971. for( i = 0; i < MAX_SLOTS; i++ ) {
  9972. struct item_data *data;
  9973. if (!sd->inventory.u.items_inventory[n].card[i])
  9974. continue;
  9975. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9976. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9977. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9978. }
  9979. }
  9980. }
  9981. }
  9982. sd->npc_item_flag = iflag;
  9983. return true;
  9984. }
  9985. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  9986. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  9987. while( it != bonus.end() ){
  9988. std::shared_ptr<s_autobonus> b = *it;
  9989. if( ( b->pos & position ) != b->pos ){
  9990. it++;
  9991. continue;
  9992. }
  9993. it = bonus.erase( it );
  9994. }
  9995. }
  9996. /**
  9997. * Recalculate player status on unequip
  9998. * @param sd: Player data
  9999. * @param n: Item inventory index
  10000. * @param flag: Whether to recalculate a player's status or not
  10001. * @return True on success or false on failure
  10002. */
  10003. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  10004. int i, iflag;
  10005. bool status_calc = false;
  10006. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10007. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10008. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10009. sd->inventory.u.items_inventory[n].equip = 0;
  10010. if (!(flag & 4))
  10011. pc_checkallowskill(sd);
  10012. iflag = sd->npc_item_flag;
  10013. // Check for combos (MUST be before status_calc_pc)
  10014. if (sd->inventory_data[n]) {
  10015. if (!sd->inventory_data[n]->combos.empty()) {
  10016. if (pc_removecombo(sd, sd->inventory_data[n]))
  10017. status_calc = true;
  10018. }
  10019. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10020. ; // No cards
  10021. else {
  10022. for (i = 0; i < MAX_SLOTS; i++) {
  10023. item_data *data;
  10024. if (!sd->inventory.u.items_inventory[n].card[i])
  10025. continue;
  10026. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  10027. if (!data->combos.empty()) {
  10028. if (pc_removecombo(sd, data))
  10029. status_calc = true;
  10030. }
  10031. }
  10032. }
  10033. }
  10034. }
  10035. if (flag & 1 || status_calc) {
  10036. pc_checkallowskill(sd);
  10037. status_calc_pc(sd, SCO_FORCE);
  10038. }
  10039. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10040. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  10041. //OnUnEquip script [Skotlex]
  10042. if (sd->inventory_data[n]) {
  10043. if (sd->inventory_data[n]->unequip_script)
  10044. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10045. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10046. ; //No cards
  10047. else {
  10048. for (i = 0; i < MAX_SLOTS; i++) {
  10049. struct item_data *data;
  10050. if (!sd->inventory.u.items_inventory[n].card[i])
  10051. continue;
  10052. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  10053. if (data->unequip_script)
  10054. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10055. }
  10056. }
  10057. }
  10058. }
  10059. sd->npc_item_flag = iflag;
  10060. }
  10061. /**
  10062. * Called when attempting to unequip an item from a player
  10063. * @param sd: Player data
  10064. * @param n: Item inventory index
  10065. * @param flag: Type of unequip
  10066. * 0 - only unequip
  10067. * 1 - calculate status after unequipping
  10068. * 2 - force unequip
  10069. * 4 - unequip by switching equipment
  10070. * @return True on success or false on failure
  10071. */
  10072. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  10073. int i, pos;
  10074. nullpo_retr(false,sd);
  10075. if (n < 0 || n >= MAX_INVENTORY) {
  10076. clif_unequipitemack(sd,0,0,0);
  10077. return false;
  10078. }
  10079. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10080. clif_unequipitemack(sd,n,0,0);
  10081. return false; //Nothing to unequip
  10082. }
  10083. // status change that makes player cannot unequip equipment
  10084. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10085. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10086. {
  10087. clif_unequipitemack(sd,n,0,0);
  10088. return false;
  10089. }
  10090. if (battle_config.battle_log)
  10091. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10092. for(i = 0; i < EQI_MAX; i++) {
  10093. if (pos & equip_bitmask[i])
  10094. sd->equip_index[i] = -1;
  10095. }
  10096. if(pos & EQP_HAND_R) {
  10097. sd->weapontype1 = 0;
  10098. sd->status.weapon = sd->weapontype2;
  10099. pc_calcweapontype(sd);
  10100. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10101. if( !battle_config.dancing_weaponswitch_fix )
  10102. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  10103. #ifdef RENEWAL
  10104. if (battle_config.switch_remove_edp&2) {
  10105. #else
  10106. if (battle_config.switch_remove_edp&1) {
  10107. #endif
  10108. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  10109. }
  10110. }
  10111. if(pos & EQP_HAND_L) {
  10112. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10113. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10114. sd->status.shield = sd->weapontype2 = W_FIST;
  10115. pc_calcweapontype(sd);
  10116. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10117. }
  10118. if(pos & EQP_SHOES)
  10119. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10120. clif_unequipitemack(sd,n,pos,1);
  10121. pc_set_costume_view(sd);
  10122. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  10123. // On weapon change (right and left hand)
  10124. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10125. if (battle_config.ammo_unequip && !(flag & 4)) {
  10126. switch (sd->inventory_data[n]->subtype) {
  10127. case W_BOW:
  10128. case W_MUSICAL:
  10129. case W_WHIP:
  10130. case W_REVOLVER:
  10131. case W_RIFLE:
  10132. case W_GATLING:
  10133. case W_SHOTGUN:
  10134. case W_GRENADE: {
  10135. short idx = sd->equip_index[EQI_AMMO];
  10136. if (idx >= 0) {
  10137. sd->equip_index[EQI_AMMO] = -1;
  10138. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10139. pc_unequipitem_sub(sd, idx, 0);
  10140. }
  10141. }
  10142. break;
  10143. }
  10144. }
  10145. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  10146. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  10147. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  10148. #ifdef RENEWAL
  10149. status_change_end(&sd->bl, SC_MAXOVERTHRUST, INVALID_TIMER);
  10150. #endif
  10151. }
  10152. // On armor change
  10153. if (pos & EQP_ARMOR) {
  10154. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  10155. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  10156. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  10157. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  10158. }
  10159. // On equipment change
  10160. #ifndef RENEWAL
  10161. if (!(flag&4))
  10162. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  10163. #endif
  10164. // On ammo change
  10165. if (sd->inventory_data[n]->type == IT_AMMO && (sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET || sd->inventory_data[n]->nameid != ITEMID_PURIFICATION_BULLET || sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET_))
  10166. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  10167. pc_unequipitem_sub(sd, n, flag);
  10168. return true;
  10169. }
  10170. int pc_equipswitch( struct map_session_data* sd, int index ){
  10171. // Get the target equip mask
  10172. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10173. // Get the currently equipped item
  10174. short equippedItem = pc_checkequip( sd, position, true );
  10175. // No item equipped at the target
  10176. if( equippedItem == -1 ){
  10177. // Remove it from the equip switch
  10178. pc_equipswitch_remove( sd, index );
  10179. pc_equipitem( sd, index, position );
  10180. return position;
  10181. }else{
  10182. std::map<int, int> unequipped;
  10183. int unequipped_position = 0;
  10184. // Unequip all items that interfere
  10185. for( int i = 0; i < EQI_MAX; i++ ){
  10186. int unequip_index = sd->equip_index[i];
  10187. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10188. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10189. // Store the unequipped index and position mask for later
  10190. unequipped[unequip_index] = unequip_item->equip;
  10191. // Keep the position for later
  10192. unequipped_position |= unequip_item->equip;
  10193. // Unequip the item
  10194. pc_unequipitem( sd, unequip_index, 0 );
  10195. }
  10196. }
  10197. int all_position = position | unequipped_position;
  10198. // Equip everything that is hit by the mask
  10199. for( int i = 0; i < EQI_MAX; i++ ){
  10200. int exchange_index = sd->equip_switch_index[i];
  10201. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10202. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10203. // Store the target position
  10204. int exchange_position = exchange_item->equipSwitch;
  10205. // Remove the item from equip switch
  10206. pc_equipswitch_remove( sd, exchange_index );
  10207. // Equip the item at the destinated position
  10208. pc_equipitem( sd, exchange_index, exchange_position );
  10209. }
  10210. }
  10211. // Place all unequipped items into the equip switch window
  10212. for( std::pair<int, int> pair : unequipped ){
  10213. int unequipped_index = pair.first;
  10214. int unequipped_position = pair.second;
  10215. // Rebuild the index cache
  10216. for( int i = 0; i < EQI_MAX; i++ ){
  10217. if( unequipped_position & equip_bitmask[i] ){
  10218. sd->equip_switch_index[i] = unequipped_index;
  10219. }
  10220. }
  10221. // Set the correct position mask
  10222. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10223. // Notify the client
  10224. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10225. }
  10226. return all_position;
  10227. }
  10228. }
  10229. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  10230. struct item* item = &sd->inventory.u.items_inventory[index];
  10231. if( !item->equipSwitch ){
  10232. return;
  10233. }
  10234. for( int i = 0; i < EQI_MAX; i++ ){
  10235. // If a match is found
  10236. if( sd->equip_switch_index[i] == index ){
  10237. // Remove it from the slot
  10238. sd->equip_switch_index[i] = -1;
  10239. }
  10240. }
  10241. // Send out one packet for all slots using the current item's mask
  10242. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10243. item->equipSwitch = 0;
  10244. }
  10245. /*==========================================
  10246. * Checking if player (sd) has an invalid item
  10247. * and is unequiped on map load (item_noequip)
  10248. *------------------------------------------*/
  10249. void pc_checkitem(struct map_session_data *sd) {
  10250. int i, calc_flag = 0;
  10251. struct item* it;
  10252. nullpo_retv(sd);
  10253. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10254. return;
  10255. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10256. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10257. it = &sd->inventory.u.items_inventory[i];
  10258. if( it->nameid == 0 )
  10259. continue;
  10260. if( !it->equip )
  10261. continue;
  10262. if( it->equip&~pc_equippoint(sd,i) ) {
  10263. pc_unequipitem(sd, i, 2);
  10264. calc_flag = 1;
  10265. continue;
  10266. }
  10267. 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) ) {
  10268. pc_unequipitem(sd, i, 2);
  10269. calc_flag = 1;
  10270. continue;
  10271. }
  10272. }
  10273. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10274. it = &sd->inventory.u.items_inventory[i];
  10275. if( it->nameid == 0 )
  10276. continue;
  10277. if( !it->equipSwitch )
  10278. continue;
  10279. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10280. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10281. for( int j = 0; j < EQI_MAX; j++ ){
  10282. if( sd->equip_switch_index[j] == i ){
  10283. sd->equip_switch_index[j] = -1;
  10284. }
  10285. }
  10286. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10287. continue;
  10288. }
  10289. }
  10290. if( calc_flag && sd->state.active ) {
  10291. pc_checkallowskill(sd);
  10292. status_calc_pc(sd,SCO_NONE);
  10293. }
  10294. }
  10295. /*==========================================
  10296. * Checks for unavailable items and removes them.
  10297. * @param sd: Player data
  10298. * @param type Forced check:
  10299. * 1 - Inventory
  10300. * 2 - Cart
  10301. * 4 - Storage
  10302. *------------------------------------------*/
  10303. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  10304. {
  10305. int i;
  10306. t_itemid nameid;
  10307. char output[256];
  10308. nullpo_retv(sd);
  10309. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10310. for(i = 0; i < MAX_INVENTORY; i++) {
  10311. nameid = sd->inventory.u.items_inventory[i].nameid;
  10312. if (!nameid)
  10313. continue;
  10314. if (!itemdb_available(nameid)) {
  10315. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10316. clif_displaymessage(sd->fd, output);
  10317. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  10318. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10319. continue;
  10320. }
  10321. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10322. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10323. }
  10324. }
  10325. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10326. for(i = 0; i < MAX_CART; i++) {
  10327. nameid = sd->cart.u.items_cart[i].nameid;
  10328. if (!nameid)
  10329. continue;
  10330. if (!itemdb_available(nameid)) {
  10331. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10332. clif_displaymessage(sd->fd, output);
  10333. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  10334. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10335. continue;
  10336. }
  10337. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10338. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10339. }
  10340. }
  10341. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10342. for(i = 0; i < sd->storage.max_amount; i++) {
  10343. nameid = sd->storage.u.items_storage[i].nameid;
  10344. if (!nameid)
  10345. continue;
  10346. if (!itemdb_available(nameid)) {
  10347. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10348. clif_displaymessage(sd->fd, output);
  10349. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  10350. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10351. continue;
  10352. }
  10353. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10354. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10355. }
  10356. }
  10357. }
  10358. /*==========================================
  10359. * Update PVP rank for sd1 in cmp to sd2
  10360. *------------------------------------------*/
  10361. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10362. {
  10363. struct map_session_data *sd1,*sd2;
  10364. sd1=(struct map_session_data *)bl;
  10365. sd2=va_arg(ap,struct map_session_data *);
  10366. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10367. {// cannot register pvp rank for hidden GMs
  10368. return 0;
  10369. }
  10370. if( sd1->pvp_point > sd2->pvp_point )
  10371. sd2->pvp_rank++;
  10372. return 0;
  10373. }
  10374. /*==========================================
  10375. * Calculate new rank beetween all present players (map_foreachinallarea)
  10376. * and display result
  10377. *------------------------------------------*/
  10378. int pc_calc_pvprank(struct map_session_data *sd)
  10379. {
  10380. int old = sd->pvp_rank;
  10381. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10382. sd->pvp_rank=1;
  10383. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10384. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10385. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10386. return sd->pvp_rank;
  10387. }
  10388. /*==========================================
  10389. * Calculate next sd ranking calculation from config
  10390. *------------------------------------------*/
  10391. TIMER_FUNC(pc_calc_pvprank_timer){
  10392. struct map_session_data *sd;
  10393. sd=map_id2sd(id);
  10394. if(sd==NULL)
  10395. return 0;
  10396. sd->pvp_timer = INVALID_TIMER;
  10397. if( pc_isinvisible(sd) )
  10398. {// do not calculate the pvp rank for a hidden GM
  10399. return 0;
  10400. }
  10401. if( pc_calc_pvprank(sd) > 0 )
  10402. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10403. return 0;
  10404. }
  10405. /*==========================================
  10406. * Checking if sd is married
  10407. * Return:
  10408. * partner_id = yes
  10409. * 0 = no
  10410. *------------------------------------------*/
  10411. int pc_ismarried(struct map_session_data *sd)
  10412. {
  10413. if(sd == NULL)
  10414. return -1;
  10415. if(sd->status.partner_id > 0)
  10416. return sd->status.partner_id;
  10417. else
  10418. return 0;
  10419. }
  10420. /*==========================================
  10421. * Marry player sd to player dstsd
  10422. * Return:
  10423. * false = fail
  10424. * true = success
  10425. *------------------------------------------*/
  10426. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  10427. {
  10428. if(sd == NULL || dstsd == NULL ||
  10429. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10430. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10431. return false;
  10432. sd->status.partner_id = dstsd->status.char_id;
  10433. dstsd->status.partner_id = sd->status.char_id;
  10434. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10435. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10436. return true;
  10437. }
  10438. /*==========================================
  10439. * Divorce sd from its partner
  10440. * Return:
  10441. * false = fail
  10442. * true = success
  10443. *------------------------------------------*/
  10444. bool pc_divorce(struct map_session_data *sd)
  10445. {
  10446. struct map_session_data *p_sd;
  10447. int i;
  10448. if( sd == NULL || !pc_ismarried(sd) )
  10449. return false;
  10450. if( !sd->status.partner_id )
  10451. return false; // Char is not married
  10452. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10453. { // Lets char server do the divorce
  10454. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10455. return false; // No char server connected
  10456. return true;
  10457. }
  10458. // Both players online, lets do the divorce manually
  10459. sd->status.partner_id = 0;
  10460. p_sd->status.partner_id = 0;
  10461. for( i = 0; i < MAX_INVENTORY; i++ )
  10462. {
  10463. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10464. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10465. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10466. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10467. }
  10468. clif_divorced(sd, p_sd->status.name);
  10469. clif_divorced(p_sd, sd->status.name);
  10470. return true;
  10471. }
  10472. /**
  10473. * Get the partner map_session_data of a player
  10474. * @param sd : the husband|wife session
  10475. * @return partner session or NULL
  10476. */
  10477. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  10478. if (!sd || !pc_ismarried(sd))
  10479. return NULL;
  10480. return map_charid2sd(sd->status.partner_id);
  10481. }
  10482. /**
  10483. * Get the father map_session_data of a player
  10484. * @param sd : the baby session
  10485. * @return father session or NULL
  10486. */
  10487. struct map_session_data *pc_get_father (struct map_session_data *sd){
  10488. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10489. return NULL;
  10490. return map_charid2sd(sd->status.father);
  10491. }
  10492. /**
  10493. * Get the mother map_session_data of a player
  10494. * @param sd : the baby session
  10495. * @return mother session or NULL
  10496. */
  10497. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  10498. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10499. return NULL;
  10500. return map_charid2sd(sd->status.mother);
  10501. }
  10502. /*==========================================
  10503. * Get sd children charid. (Need to be married)
  10504. *------------------------------------------*/
  10505. struct map_session_data *pc_get_child (struct map_session_data *sd)
  10506. {
  10507. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10508. // charid2sd returns NULL if not found
  10509. return NULL;
  10510. return map_charid2sd(sd->status.child);
  10511. }
  10512. /*==========================================
  10513. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10514. *------------------------------------------*/
  10515. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  10516. {
  10517. int hp = 0, sp = 0;
  10518. if( pc_isdead(sd) )
  10519. return;
  10520. if (sd->hp_loss.value) {
  10521. sd->hp_loss.tick += diff_tick;
  10522. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10523. hp += sd->hp_loss.value;
  10524. sd->hp_loss.tick -= sd->hp_loss.rate;
  10525. }
  10526. if(hp >= sd->battle_status.hp)
  10527. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10528. }
  10529. if (sd->sp_loss.value) {
  10530. sd->sp_loss.tick += diff_tick;
  10531. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10532. sp += sd->sp_loss.value;
  10533. sd->sp_loss.tick -= sd->sp_loss.rate;
  10534. }
  10535. }
  10536. if (hp > 0 || sp > 0)
  10537. status_zap(&sd->bl, hp, sp);
  10538. }
  10539. //Character regen. Flag is used to know which types of regen can take place.
  10540. //&1: HP regen
  10541. //&2: SP regen
  10542. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  10543. {
  10544. int hp = 0, sp = 0;
  10545. if (sd->hp_regen.value) {
  10546. sd->hp_regen.tick += diff_tick;
  10547. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10548. hp += sd->hp_regen.value;
  10549. sd->hp_regen.tick -= sd->hp_regen.rate;
  10550. }
  10551. }
  10552. if (sd->sp_regen.value) {
  10553. sd->sp_regen.tick += diff_tick;
  10554. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10555. sp += sd->sp_regen.value;
  10556. sd->sp_regen.tick -= sd->sp_regen.rate;
  10557. }
  10558. }
  10559. if (sd->percent_hp_regen.value) {
  10560. sd->percent_hp_regen.tick += diff_tick;
  10561. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10562. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10563. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10564. }
  10565. }
  10566. if (sd->percent_sp_regen.value) {
  10567. sd->percent_sp_regen.tick += diff_tick;
  10568. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10569. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10570. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10571. }
  10572. }
  10573. if (hp > 0 || sp > 0)
  10574. status_heal(&sd->bl, hp, sp, 0);
  10575. }
  10576. /*==========================================
  10577. * Memo player sd savepoint. (map,x,y)
  10578. *------------------------------------------*/
  10579. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  10580. {
  10581. nullpo_retv(sd);
  10582. sd->status.save_point.map = mapindex;
  10583. sd->status.save_point.x = x;
  10584. sd->status.save_point.y = y;
  10585. }
  10586. /*==========================================
  10587. * Save 1 player data at autosave interval
  10588. *------------------------------------------*/
  10589. static TIMER_FUNC(pc_autosave){
  10590. int interval;
  10591. struct s_mapiterator* iter;
  10592. struct map_session_data* sd;
  10593. static int last_save_id = 0, save_flag = 0;
  10594. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10595. save_flag = 0;
  10596. else
  10597. save_flag = 1; //Noone was saved, so save first found char.
  10598. iter = mapit_getallusers();
  10599. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10600. {
  10601. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10602. continue;
  10603. if(sd->bl.id == last_save_id && save_flag != 1) {
  10604. save_flag = 1;
  10605. continue;
  10606. }
  10607. if(save_flag != 1) //Not our turn to save yet.
  10608. continue;
  10609. //Save char.
  10610. last_save_id = sd->bl.id;
  10611. save_flag = 2;
  10612. if (pc_isvip(sd)) // Check if we're still VIP
  10613. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10614. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10615. break;
  10616. }
  10617. mapit_free(iter);
  10618. interval = autosave_interval/(map_usercount()+1);
  10619. if(interval < minsave_interval)
  10620. interval = minsave_interval;
  10621. add_timer(gettick()+interval,pc_autosave,0,0);
  10622. return 0;
  10623. }
  10624. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  10625. {
  10626. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  10627. { //Night/day state does not match.
  10628. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  10629. sd->state.night = night_flag;
  10630. return 1;
  10631. }
  10632. return 0;
  10633. }
  10634. /*================================================
  10635. * timer to do the day [Yor]
  10636. * data: 0 = called by timer, 1 = gmcommand/script
  10637. *------------------------------------------------*/
  10638. TIMER_FUNC(map_day_timer){
  10639. char tmp_soutput[1024];
  10640. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  10641. return 0;
  10642. if (!night_flag)
  10643. return 0; //Already day.
  10644. night_flag = 0; // 0=day, 1=night [Yor]
  10645. map_foreachpc(pc_daynight_timer_sub);
  10646. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  10647. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10648. return 0;
  10649. }
  10650. /*================================================
  10651. * timer to do the night [Yor]
  10652. * data: 0 = called by timer, 1 = gmcommand/script
  10653. *------------------------------------------------*/
  10654. TIMER_FUNC(map_night_timer){
  10655. char tmp_soutput[1024];
  10656. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  10657. return 0;
  10658. if (night_flag)
  10659. return 0; //Already nigth.
  10660. night_flag = 1; // 0=day, 1=night [Yor]
  10661. map_foreachpc(pc_daynight_timer_sub);
  10662. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  10663. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10664. return 0;
  10665. }
  10666. /**
  10667. * Attempt to stand up a player
  10668. * @param sd
  10669. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  10670. * @return True if success, Fals if failed
  10671. */
  10672. bool pc_setstand(struct map_session_data *sd, bool force){
  10673. nullpo_ret(sd);
  10674. // Cannot stand yet
  10675. // TODO: Move to SCS_NOSTAND [Cydh]
  10676. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  10677. return false;
  10678. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  10679. clif_status_load(&sd->bl,EFST_SIT,0);
  10680. clif_standing(&sd->bl); //Inform area PC is standing
  10681. //Reset sitting tick.
  10682. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10683. if( pc_isdead( sd ) ){
  10684. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10685. clif_party_dead( sd );
  10686. }else{
  10687. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10688. }
  10689. return true;
  10690. }
  10691. /**
  10692. * Calculate Overheat value
  10693. * @param sd: Player data
  10694. * @param heat: Amount of Heat to adjust
  10695. **/
  10696. void pc_overheat(struct map_session_data *sd, int16 heat) {
  10697. nullpo_retv(sd);
  10698. status_change_entry *sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT];
  10699. if (sce) {
  10700. static std::vector<int16> limit = { 150, 200, 280, 360, 450 };
  10701. uint16 skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, (uint16)(limit.size()-1));
  10702. sce->val1 += heat;
  10703. sce->val1 = cap_value(sce->val1, 0, 1000);
  10704. if (sd->sc.data[SC_OVERHEAT])
  10705. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  10706. if (sce->val1 > limit[skill_lv])
  10707. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  10708. } else if (heat > 0)
  10709. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10710. }
  10711. /**
  10712. * Check if player is autolooting given itemID.
  10713. */
  10714. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10715. {
  10716. uint8 i = 0;
  10717. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10718. return true;
  10719. if (!sd->state.autolooting)
  10720. return false;
  10721. if (sd->state.autolooting)
  10722. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10723. return (i != AUTOLOOTITEM_SIZE);
  10724. }
  10725. /**
  10726. * Checks if player can use @/#command
  10727. * @param sd Player map session data
  10728. * @param command Command name without @/# and params
  10729. * @param type is it atcommand or charcommand
  10730. */
  10731. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  10732. {
  10733. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  10734. }
  10735. /**
  10736. * Checks if commands used by a player should be logged
  10737. * according to their group setting.
  10738. * @param sd Player map session data
  10739. */
  10740. bool pc_should_log_commands(struct map_session_data *sd)
  10741. {
  10742. return pc_group_should_log_commands(pc_get_group_id(sd));
  10743. }
  10744. /**
  10745. * Spirit Charm expiration timer.
  10746. * @see TimerFunc
  10747. */
  10748. static TIMER_FUNC(pc_spiritcharm_timer){
  10749. struct map_session_data *sd;
  10750. int i;
  10751. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10752. return 1;
  10753. if (sd->spiritcharm <= 0) {
  10754. 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);
  10755. sd->spiritcharm = 0;
  10756. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10757. return 0;
  10758. }
  10759. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10760. if (i == sd->spiritcharm) {
  10761. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10762. return 0;
  10763. }
  10764. sd->spiritcharm--;
  10765. if (i != sd->spiritcharm)
  10766. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10767. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10768. if (sd->spiritcharm <= 0)
  10769. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10770. clif_spiritcharm(sd);
  10771. return 0;
  10772. }
  10773. /**
  10774. * Adds a spirit charm.
  10775. * @param sd: Target character
  10776. * @param interval: Duration
  10777. * @param max: Maximum amount of charms to add
  10778. * @param type: Charm type (@see spirit_charm_types)
  10779. */
  10780. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10781. {
  10782. int tid, i;
  10783. nullpo_retv(sd);
  10784. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10785. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10786. if (max > MAX_SPIRITCHARM)
  10787. max = MAX_SPIRITCHARM;
  10788. if (sd->spiritcharm < 0)
  10789. sd->spiritcharm = 0;
  10790. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10791. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10792. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10793. sd->spiritcharm--;
  10794. if (sd->spiritcharm != 0)
  10795. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10796. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10797. }
  10798. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10799. 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);
  10800. if (i != sd->spiritcharm)
  10801. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10802. sd->spiritcharm_timer[i] = tid;
  10803. sd->spiritcharm++;
  10804. sd->spiritcharm_type = type;
  10805. clif_spiritcharm(sd);
  10806. }
  10807. /**
  10808. * Removes one or more spirit charms.
  10809. * @param sd: The target character
  10810. * @param count: Amount of charms to remove
  10811. * @param type: Type of charm to remove
  10812. */
  10813. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10814. {
  10815. int i;
  10816. nullpo_retv(sd);
  10817. if (sd->spiritcharm_type != type)
  10818. return;
  10819. if (sd->spiritcharm <= 0) {
  10820. sd->spiritcharm = 0;
  10821. return;
  10822. }
  10823. if (count <= 0)
  10824. return;
  10825. if (count > sd->spiritcharm)
  10826. count = sd->spiritcharm;
  10827. sd->spiritcharm -= count;
  10828. if (count > MAX_SPIRITCHARM)
  10829. count = MAX_SPIRITCHARM;
  10830. for (i = 0; i < count; i++) {
  10831. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10832. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10833. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10834. }
  10835. }
  10836. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10837. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10838. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10839. }
  10840. if (sd->spiritcharm <= 0)
  10841. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10842. clif_spiritcharm(sd);
  10843. }
  10844. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10845. /**
  10846. * Renewal EXP/Item Drop rate modifier based on level penalty
  10847. * @param level_diff: Monster and Player level difference
  10848. * @param mob_class: Monster class
  10849. * @param mode: Monster mode
  10850. * @param type: 1 - EXP, 2 - Item Drop
  10851. * @return Penalty rate
  10852. */
  10853. uint16 pc_level_penalty_mod( struct map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  10854. // No player was attached, we don't use any modifier (100 = rates are not touched)
  10855. if( sd == nullptr ){
  10856. return 100;
  10857. }
  10858. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  10859. return 100;
  10860. }
  10861. int monster_level;
  10862. if( md != nullptr ){
  10863. monster_level = md->level;
  10864. mob = md->db;
  10865. }else if( mob != nullptr ){
  10866. monster_level = mob->lv;
  10867. }else{
  10868. return 100;
  10869. }
  10870. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  10871. return 100;
  10872. }
  10873. int level_difference = monster_level - sd->status.base_level;
  10874. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  10875. if( penalty != nullptr ){
  10876. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  10877. }else{
  10878. return 100;
  10879. }
  10880. }
  10881. #endif
  10882. int pc_split_str(char *str,char **val,int num)
  10883. {
  10884. int i;
  10885. for (i=0; i<num && str; i++){
  10886. val[i] = str;
  10887. str = strchr(str,',');
  10888. if (str && i<num-1) //Do not remove a trailing comma.
  10889. *str++=0;
  10890. }
  10891. return i;
  10892. }
  10893. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10894. {
  10895. static int warning=0;
  10896. int i,j;
  10897. for (i=0; i<max; i++) {
  10898. double f;
  10899. if (!str) break;
  10900. f = atof(str);
  10901. if (f < 0)
  10902. val[i] = 0;
  10903. else if (f > UINT_MAX) {
  10904. val[i] = UINT_MAX;
  10905. if (!warning) {
  10906. warning = 1;
  10907. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10908. }
  10909. } else
  10910. val[i] = (unsigned int)f;
  10911. str = strchr(str,sep);
  10912. if (str)
  10913. *str++=0;
  10914. }
  10915. //Zero up the remaining.
  10916. for(j=i; j < max; j++)
  10917. val[j] = 0;
  10918. return i;
  10919. }
  10920. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  10921. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  10922. if (tree != nullptr)
  10923. return util::umap_find(tree->skills, skill_id);
  10924. return nullptr;
  10925. }
  10926. const std::string SkillTreeDatabase::getDefaultLocation() {
  10927. return std::string(db_path) + "/skill_tree.yml";
  10928. }
  10929. /**
  10930. * Reads and parses an entry from the skill_tree.
  10931. * @param node: YAML node containing the entry.
  10932. * @return count of successfully parsed rows
  10933. */
  10934. uint64 SkillTreeDatabase::parseBodyNode(const YAML::Node &node) {
  10935. std::string job_name;
  10936. if (!this->asString(node, "Job", job_name))
  10937. return 0;
  10938. int64 constant;
  10939. std::string job_name_constant = "JOB_" + job_name;
  10940. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  10941. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  10942. return 0;
  10943. }
  10944. uint16 job_id = static_cast<uint16>(constant);
  10945. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  10946. bool exists = tree != nullptr;
  10947. if (!exists)
  10948. tree = std::make_shared<s_skill_tree>();
  10949. if (this->nodeExists(node, "Inherit")) {
  10950. const YAML::Node &InheritNode = node["Inherit"];
  10951. for (const auto &Inheritit : InheritNode) {
  10952. std::string inheritname = Inheritit.first.as<std::string>();
  10953. std::string inheritname_constant = "JOB_" + inheritname;
  10954. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  10955. this->invalidWarning(InheritNode[inheritname], "Invalid job %s.\n", inheritname.c_str());
  10956. return 0;
  10957. }
  10958. bool active;
  10959. if (!this->asBool(InheritNode, inheritname, active))
  10960. return 0;
  10961. uint16 inherit_job = static_cast<uint16>(constant);
  10962. if (!active) {
  10963. if (exists)
  10964. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  10965. }
  10966. else {
  10967. if (!util::vector_exists(tree->inherit_job, inherit_job))
  10968. tree->inherit_job.push_back(inherit_job);
  10969. }
  10970. }
  10971. }
  10972. if (this->nodeExists(node, "Tree")) {
  10973. for (const auto &it : node["Tree"]) {
  10974. std::string skill_name;
  10975. if (!this->asString(it, "Name", skill_name))
  10976. return 0;
  10977. uint16 skill_id = skill_name2id(skill_name.c_str());
  10978. if (skill_id == 0) {
  10979. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  10980. return 0;
  10981. }
  10982. if (!skill_get_index(skill_id)) {
  10983. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  10984. return 0;
  10985. }
  10986. std::shared_ptr<s_skill_tree_entry> entry;
  10987. bool skill_exists = tree->skills.count(skill_id) > 0;
  10988. if (skill_exists)
  10989. entry = tree->skills[skill_id];
  10990. else
  10991. entry = std::make_shared<s_skill_tree_entry>();
  10992. entry->skill_id = skill_id;
  10993. uint16 max_lv;
  10994. if (!this->asUInt16(it, "MaxLevel", max_lv))
  10995. return 0;
  10996. if (max_lv > MAX_SKILL_LEVEL) {
  10997. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  10998. return 0;
  10999. }
  11000. uint16 skill_lv_max = skill_get_max(skill_id);
  11001. if (max_lv > skill_lv_max) {
  11002. this->invalidWarning(it["MaxLevel"], "Skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), max_lv, skill_lv_max);
  11003. max_lv = skill_lv_max;
  11004. }
  11005. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11006. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11007. // this->invalidWarning(it["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name.c_str(), job_name.c_str());
  11008. // continue;
  11009. // }
  11010. entry->max_lv = max_lv;
  11011. if (this->nodeExists(it, "BaseLevel")) {
  11012. uint32 baselv;
  11013. if (!this->asUInt32(it, "BaseLevel", baselv))
  11014. return 0;
  11015. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11016. if (baselv > baselv_max) {
  11017. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11018. skill_id, baselv, job_name.c_str(), baselv_max);
  11019. baselv = baselv_max;
  11020. }
  11021. entry->baselv = baselv;
  11022. } else {
  11023. if (!skill_exists)
  11024. entry->baselv = 0;
  11025. }
  11026. if (this->nodeExists(it, "JobLevel")) {
  11027. uint32 joblv;
  11028. if (!this->asUInt32(it, "JobLevel", joblv))
  11029. return 0;
  11030. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11031. if (joblv > joblv_max) {
  11032. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11033. skill_id, joblv, job_name.c_str(), joblv_max);
  11034. joblv = joblv_max;
  11035. }
  11036. entry->joblv = joblv;
  11037. } else {
  11038. if (!skill_exists)
  11039. entry->joblv = 0;
  11040. }
  11041. if (this->nodeExists(it, "Requires")) {
  11042. for (const auto &Requiresit : it["Requires"]) {
  11043. if (!this->nodesExist(Requiresit, { "Name" }))
  11044. return 0;
  11045. std::string skill_name_req;
  11046. if (!this->asString(Requiresit, "Name", skill_name_req))
  11047. return 0;
  11048. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11049. if (skill_id_req == 0) {
  11050. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11051. return 0;
  11052. }
  11053. uint16 lv_req;
  11054. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11055. return 0;
  11056. if (lv_req > MAX_SKILL_LEVEL) {
  11057. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11058. return 0;
  11059. }
  11060. uint16 lv_req_max = skill_get_max(skill_id_req);
  11061. if (lv_req > lv_req_max) {
  11062. this->invalidWarning(it["MaxLevel"], "Required skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), lv_req, lv_req_max);
  11063. lv_req = lv_req_max;
  11064. }
  11065. if (lv_req == 0) {
  11066. if (entry->need.erase(skill_id_req) == 0)
  11067. this->invalidWarning(Requiresit["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name_req.c_str(), job_name.c_str());
  11068. continue;
  11069. }
  11070. entry->need[skill_id_req] = lv_req;
  11071. }
  11072. }
  11073. if (this->nodeExists(it, "Exclude")) {
  11074. bool exclude;
  11075. if (!this->asBool(it, "Exclude", exclude))
  11076. return 0;
  11077. entry->exclude_inherit = exclude;
  11078. } else {
  11079. if (!skill_exists)
  11080. entry->exclude_inherit = false;
  11081. }
  11082. if (!skill_exists)
  11083. tree->skills.insert({ skill_id, entry });
  11084. }
  11085. }
  11086. if (!exists)
  11087. this->put(job_id, tree);
  11088. return true;
  11089. }
  11090. void SkillTreeDatabase::loadingFinished() {
  11091. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11092. for (auto &data : *this) {
  11093. if (data.second->inherit_job.empty())
  11094. continue;
  11095. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11096. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11097. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11098. for (const auto &inherit_job : data.second->inherit_job) {
  11099. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11100. if (tree == nullptr || tree->skills.empty())
  11101. continue;
  11102. for (const auto &it : tree->skills) {
  11103. if (it.second->exclude_inherit)
  11104. continue;
  11105. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11106. continue;
  11107. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11108. skill_tree->skills[it.first] = it.second;
  11109. else
  11110. skill_tree->skills.insert({ it.first, it.second });
  11111. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11112. if (skill->baselv > baselv_max) {
  11113. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11114. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11115. skill->baselv = baselv_max;
  11116. }
  11117. if (skill->joblv > joblv_max) {
  11118. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11119. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11120. skill->joblv = joblv_max;
  11121. }
  11122. }
  11123. }
  11124. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11125. job_tree.insert({ data.first, skill_tree });
  11126. }
  11127. if (!job_tree.empty()) {
  11128. for (auto &data : *this) {
  11129. if (job_tree.count(data.first) == 0)
  11130. continue;
  11131. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11132. }
  11133. }
  11134. // remove skills with max_lv = 0
  11135. for (const auto &job : *this) {
  11136. if (job.second->skills.empty())
  11137. continue;
  11138. auto it = job.second->skills.begin();
  11139. while( it != job.second->skills.end() ){
  11140. if( it->second->max_lv == 0 ){
  11141. it = job.second->skills.erase( it );
  11142. }else{
  11143. it++;
  11144. }
  11145. }
  11146. }
  11147. }
  11148. /**
  11149. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11150. * @param level: Base level of player
  11151. * @param job_id: Job ID @see enum e_job
  11152. * @return base_hp
  11153. * @author [Cydh]
  11154. */
  11155. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11156. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11157. double base_hp = 35 + level * (job->hp_multiplicator / 100.);
  11158. #ifndef RENEWAL
  11159. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11160. base_hp += 90;
  11161. #endif
  11162. for (uint16 i = 2; i <= level; i++)
  11163. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11164. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11165. base_hp += floor((base_hp / 2) + 0.5);
  11166. return (unsigned int)base_hp;
  11167. }
  11168. /**
  11169. * Calculates base sp of player.
  11170. * @param level: Base level of player
  11171. * @param job_id: Job ID @see enum e_job
  11172. * @return base_sp
  11173. * @author [Playtester]
  11174. */
  11175. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11176. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11177. double base_sp = 10 + floor(level * (job->sp_factor / 100.));
  11178. switch (job_id) {
  11179. case JOB_NINJA:
  11180. if (level >= 10)
  11181. base_sp -= 22;
  11182. else
  11183. base_sp = 11 + 3*level;
  11184. break;
  11185. case JOB_GUNSLINGER:
  11186. if (level > 10)
  11187. base_sp -= 18;
  11188. else
  11189. base_sp = 9 + 3*level;
  11190. break;
  11191. case JOB_SUMMONER:
  11192. case JOB_SPIRIT_HANDLER:
  11193. base_sp -= floor(base_sp / 2);
  11194. break;
  11195. }
  11196. return (unsigned int)base_sp;
  11197. }
  11198. const std::string JobDatabase::getDefaultLocation() {
  11199. return std::string(db_path) + "/job_stats.yml";
  11200. }
  11201. /**
  11202. * Reads and parses an entry from the job_db.
  11203. * @param node: YAML node containing the entry.
  11204. * @return count of successfully parsed rows
  11205. */
  11206. uint64 JobDatabase::parseBodyNode(const YAML::Node &node) {
  11207. if (this->nodeExists(node, "Jobs")) {
  11208. const YAML::Node &jobsNode = node["Jobs"];
  11209. for (const auto &jobit : jobsNode) {
  11210. std::string job_name = jobit.first.as<std::string>(), job_name_constant = "JOB_" + job_name;
  11211. int64 job_id;
  11212. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11213. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11214. return 0;
  11215. }
  11216. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11217. bool exists = job != nullptr;
  11218. if (!exists) {
  11219. job = std::make_shared<s_job_info>();
  11220. job->job_bonus.resize(MAX_LEVEL);
  11221. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11222. job->base_hp.resize(MAX_LEVEL);
  11223. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11224. job->base_sp.resize(MAX_LEVEL);
  11225. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11226. job->base_ap.resize(MAX_LEVEL);
  11227. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11228. }
  11229. if (this->nodeExists(node, "MaxWeight")) {
  11230. uint32 weight;
  11231. if (!this->asUInt32(node, "MaxWeight", weight))
  11232. return 0;
  11233. job->max_weight_base = weight;
  11234. } else {
  11235. if (!exists)
  11236. job->max_weight_base = 20000;
  11237. }
  11238. if (this->nodeExists(node, "HPFactor")) {
  11239. uint32 hp;
  11240. if (!this->asUInt32(node, "HPFactor", hp))
  11241. return 0;
  11242. job->hp_factor = hp;
  11243. } else {
  11244. if (!exists)
  11245. job->hp_factor = 20000;
  11246. }
  11247. if (this->nodeExists(node, "HPMultiplicator")) {
  11248. uint32 hp;
  11249. if (!this->asUInt32(node, "HPMultiplicator", hp))
  11250. return 0;
  11251. job->hp_multiplicator = hp;
  11252. } else {
  11253. if (!exists)
  11254. job->hp_multiplicator = 500;
  11255. }
  11256. if (this->nodeExists(node, "SPFactor")) {
  11257. uint32 sp;
  11258. if (!this->asUInt32(node, "SPFactor", sp))
  11259. return 0;
  11260. job->sp_factor = sp;
  11261. } else {
  11262. if (!exists)
  11263. job->sp_factor = 100;
  11264. }
  11265. if (this->nodeExists(node, "BaseASPD")) {
  11266. const YAML::Node &aspdNode = node["BaseASPD"];
  11267. uint8 max = MAX_WEAPON_TYPE;
  11268. #ifdef RENEWAL // Renewal adds an extra column for shields
  11269. max += 1;
  11270. #endif
  11271. if (!exists) {
  11272. job->aspd_base.resize(max);
  11273. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11274. }
  11275. for (const auto &aspdit : aspdNode) {
  11276. std::string weapon = aspdit.first.as<std::string>(), weapon_constant = "W_" + weapon;
  11277. int64 constant;
  11278. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11279. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11280. continue;
  11281. }
  11282. if (constant < W_FIST || constant > max) {
  11283. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11284. continue;
  11285. }
  11286. int16 aspd;
  11287. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11288. return 0;
  11289. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11290. }
  11291. } else {
  11292. if (!exists) {
  11293. uint8 max = MAX_WEAPON_TYPE;
  11294. #ifdef RENEWAL // Renewal adds an extra column for shields
  11295. max += 1;
  11296. #endif
  11297. job->aspd_base.resize(max);
  11298. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11299. }
  11300. }
  11301. if (this->nodeExists(node, "MaxStats")) {
  11302. const YAML::Node &statNode = node["MaxStats"];
  11303. for (const auto &statit : statNode) {
  11304. std::string stat = statit.first.as<std::string>(), stat_constant = "PARAM_" + stat;
  11305. int64 constant;
  11306. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11307. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11308. continue;
  11309. }
  11310. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11311. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11312. continue;
  11313. }
  11314. uint16 max;
  11315. if (!this->asUInt16(statNode, stat.c_str(), max))
  11316. return 0;
  11317. job->max_param[constant] = max;
  11318. }
  11319. }
  11320. if (this->nodeExists(node, "MaxBaseLevel")) {
  11321. uint16 level;
  11322. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11323. return 0;
  11324. if (level == 0 || level > MAX_LEVEL) {
  11325. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11326. level = MAX_LEVEL;
  11327. }
  11328. job->max_base_level = level;
  11329. } else {
  11330. if (!exists)
  11331. job->max_base_level = MAX_LEVEL;
  11332. }
  11333. if (this->nodeExists(node, "BaseExp")) {
  11334. for (const YAML::Node &bexpNode : node["BaseExp"]) {
  11335. uint16 level;
  11336. if (!this->asUInt16(bexpNode, "Level", level))
  11337. return 0;
  11338. if (level > job->max_base_level)
  11339. continue;
  11340. if (level == 0 || level > MAX_LEVEL) {
  11341. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11342. return 0;
  11343. }
  11344. if (this->nodeExists(bexpNode, "Exp")) {
  11345. t_exp exp;
  11346. if (!this->asUInt64(bexpNode, "Exp", exp))
  11347. return 0;
  11348. job->base_exp[level - 1] = exp;
  11349. }
  11350. }
  11351. }
  11352. if (this->nodeExists(node, "MaxJobLevel")) {
  11353. uint16 level;
  11354. if (!this->asUInt16(node, "MaxJobLevel", level))
  11355. return 0;
  11356. if (level == 0 || level > MAX_LEVEL) {
  11357. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11358. level = MAX_LEVEL;
  11359. }
  11360. job->max_job_level = level;
  11361. } else {
  11362. if (!exists)
  11363. job->max_job_level = MAX_LEVEL;
  11364. }
  11365. if (this->nodeExists(node, "JobExp")) {
  11366. for (const YAML::Node &jexpNode : node["JobExp"]) {
  11367. uint16 level;
  11368. if (!this->asUInt16(jexpNode, "Level", level))
  11369. return 0;
  11370. if (level > job->max_job_level)
  11371. continue;
  11372. if (level == 0 || level > MAX_LEVEL) {
  11373. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11374. return 0;
  11375. }
  11376. if (this->nodeExists(jexpNode, "Exp")) {
  11377. t_exp exp;
  11378. if (!this->asUInt64(jexpNode, "Exp", exp))
  11379. return 0;
  11380. job->job_exp[level - 1] = exp;
  11381. }
  11382. }
  11383. }
  11384. if (this->nodeExists(node, "BonusStats")) {
  11385. const YAML::Node &bonusNode = node["BonusStats"];
  11386. for (const YAML::Node &levelNode : bonusNode) {
  11387. uint16 level;
  11388. if (!this->asUInt16(levelNode, "Level", level))
  11389. return 0;
  11390. if (level == 0 || level > MAX_LEVEL) {
  11391. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11392. return 0;
  11393. }
  11394. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11395. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11396. int16 change;
  11397. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11398. return 0;
  11399. job->job_bonus[level - 1][idx] = change;
  11400. }
  11401. }
  11402. }
  11403. }
  11404. #ifdef HP_SP_TABLES
  11405. if (this->nodeExists(node, "BaseHp")) {
  11406. for (const YAML::Node &bhpNode : node["BaseHp"]) {
  11407. uint16 level;
  11408. if (!this->asUInt16(bhpNode, "Level", level))
  11409. return 0;
  11410. if (level > job->max_base_level)
  11411. continue;
  11412. if (level == 0 || level > MAX_LEVEL) {
  11413. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11414. return 0;
  11415. }
  11416. if (this->nodeExists(bhpNode, "Hp")) {
  11417. uint32 points;
  11418. if (!this->asUInt32(bhpNode, "Hp", points))
  11419. return 0;
  11420. job->base_hp[level - 1] = points;
  11421. }
  11422. }
  11423. }
  11424. if (this->nodeExists(node, "BaseSp")) {
  11425. for (const YAML::Node &bspNode : node["BaseSp"]) {
  11426. uint16 level;
  11427. if (!this->asUInt16(bspNode, "Level", level))
  11428. return 0;
  11429. if (level > job->max_base_level)
  11430. continue;
  11431. if (level == 0 || level > MAX_LEVEL) {
  11432. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11433. return 0;
  11434. }
  11435. if (this->nodeExists(bspNode, "Sp")) {
  11436. uint32 points;
  11437. if (!this->asUInt32(bspNode, "Sp", points))
  11438. return 0;
  11439. job->base_sp[level - 1] = points;
  11440. }
  11441. }
  11442. }
  11443. if (this->nodeExists(node, "BaseAp")) {
  11444. for (const YAML::Node &bapNode : node["BaseAp"]) {
  11445. uint16 level;
  11446. if (!this->asUInt16(bapNode, "Level", level))
  11447. return 0;
  11448. if (level > job->max_base_level)
  11449. continue;
  11450. if (level == 0 || level > MAX_LEVEL) {
  11451. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11452. return 0;
  11453. }
  11454. if (this->nodeExists(bapNode, "Ap")) {
  11455. uint32 points;
  11456. if (!this->asUInt32(bapNode, "Ap", points))
  11457. return 0;
  11458. job->base_ap[level - 1] = points;
  11459. }
  11460. }
  11461. }
  11462. #endif
  11463. if (!exists)
  11464. this->put(static_cast<uint16>(job_id), job);
  11465. }
  11466. }
  11467. return 1;
  11468. }
  11469. void JobDatabase::loadingFinished() {
  11470. // Checking if all class have their data
  11471. for (auto &jobIt : *this) {
  11472. uint16 job_id = jobIt.first;
  11473. if (!pcdb_checkid(job_id))
  11474. continue;
  11475. if (job_id == JOB_WEDDING || job_id == JOB_XMAS || job_id == JOB_SUMMER || job_id == JOB_HANBOK || job_id == JOB_OKTOBERFEST || job_id == JOB_SUMMER2)
  11476. continue; // Classes that do not need exp tables.
  11477. std::shared_ptr<s_job_info> job = jobIt.second;
  11478. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11479. if (!maxBaseLv)
  11480. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11481. if (!maxJobLv)
  11482. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11483. // Init and checking the empty value of Base HP/SP [Cydh]
  11484. if (job->base_hp.empty())
  11485. job->base_hp.resize(maxBaseLv);
  11486. for (uint16 j = 0; j < maxBaseLv; j++) {
  11487. if (job->base_hp[j] == 0)
  11488. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11489. }
  11490. if (job->base_sp.empty())
  11491. job->base_sp.resize(maxBaseLv);
  11492. for (uint16 j = 0; j < maxBaseLv; j++) {
  11493. if (job->base_sp[j] == 0)
  11494. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11495. }
  11496. // Resize to the maximum base level
  11497. if (job->base_hp.capacity() > maxBaseLv)
  11498. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11499. if (job->base_sp.capacity() > maxBaseLv)
  11500. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11501. if (job->base_ap.capacity() > maxBaseLv)
  11502. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11503. // Resize to the maximum job level
  11504. if (job->job_bonus.capacity() > maxJobLv)
  11505. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11506. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11507. // Store total
  11508. int16 current = 0;
  11509. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11510. // Add the bonus from this job level
  11511. current += job->job_bonus[job_level][parameter];
  11512. // Set the new total on this job level
  11513. job->job_bonus[job_level][parameter] = current;
  11514. }
  11515. }
  11516. uint64 class_ = pc_jobid2mapid( job_id );
  11517. // Set normal status limits
  11518. uint16 max = battle_config.max_parameter;
  11519. do{
  11520. // Always check babies first
  11521. if( class_ & JOBL_BABY ){
  11522. if( class_ & JOBL_THIRD ){
  11523. max = battle_config.max_baby_third_parameter;
  11524. break;
  11525. }else{
  11526. max = battle_config.max_baby_parameter;
  11527. break;
  11528. }
  11529. }
  11530. // Summoner
  11531. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11532. max = battle_config.max_summoner_parameter;
  11533. break;
  11534. }
  11535. // Extended classes
  11536. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11537. max = battle_config.max_extended_parameter;
  11538. break;
  11539. }
  11540. if( class_ & JOBL_FOURTH ){
  11541. max = battle_config.max_fourth_parameter;
  11542. break;
  11543. }
  11544. // 3rd class
  11545. if( class_ & JOBL_THIRD ){
  11546. // Transcendent
  11547. if( class_ & JOBL_UPPER ){
  11548. max = battle_config.max_third_trans_parameter;
  11549. break;
  11550. }else{
  11551. max = battle_config.max_third_parameter;
  11552. break;
  11553. }
  11554. }
  11555. // Transcendent
  11556. if( class_ & JOBL_UPPER ){
  11557. max = battle_config.max_trans_parameter;
  11558. break;
  11559. }
  11560. }while( false );
  11561. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11562. // If it is not explicitly set in the database file
  11563. if( job->max_param[parameter] == 0 ){
  11564. job->max_param[parameter] = max;
  11565. }
  11566. }
  11567. // Set trait status limit
  11568. if( class_ & JOBL_FOURTH ){
  11569. max = battle_config.max_trait_parameter;
  11570. }else{
  11571. max = 0;
  11572. }
  11573. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11574. // If it is not explicitly set in the database file
  11575. if( job->max_param[parameter] == 0 ){
  11576. job->max_param[parameter] = max;
  11577. }
  11578. }
  11579. }
  11580. }
  11581. /**
  11582. * Read job_noenter_map.txt
  11583. **/
  11584. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11585. int class_ = -1;
  11586. int64 class_tmp;
  11587. if (ISDIGIT(str[0][0])) {
  11588. class_ = atoi(str[0]);
  11589. } else {
  11590. if (!script_get_constant(str[0], &class_tmp)) {
  11591. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11592. return false;
  11593. }
  11594. class_ = static_cast<int>(class_tmp);
  11595. }
  11596. if (!pcdb_checkid(class_)) {
  11597. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11598. return false;
  11599. }
  11600. std::shared_ptr<s_job_info> job = job_db.find(class_);
  11601. if (job == nullptr) {
  11602. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  11603. return false;
  11604. }
  11605. job->noenter_map.zone = atoi(str[1]);
  11606. job->noenter_map.group_lv = atoi(str[2]);
  11607. return true;
  11608. }
  11609. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  11610. return std::string(db_path) + "/statpoint.yml";
  11611. }
  11612. uint64 PlayerStatPointDatabase::parseBodyNode(const YAML::Node &node) {
  11613. if (!this->nodesExist(node, { "Level", "Points" })) {
  11614. return 0;
  11615. }
  11616. uint16 level;
  11617. if (!this->asUInt16(node, "Level", level))
  11618. return 0;
  11619. uint32 point;
  11620. if (!this->asUInt32(node, "Points", point))
  11621. return 0;
  11622. if (level == 0) {
  11623. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  11624. return 0;
  11625. }
  11626. if (level > MAX_LEVEL) {
  11627. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  11628. return 0;
  11629. }
  11630. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11631. bool exists = entry != nullptr;
  11632. if( !exists ){
  11633. entry = std::make_shared<s_statpoint_entry>();
  11634. entry->level = level;
  11635. entry->statpoints = point;
  11636. }
  11637. if( this->nodeExists( node, "TraitPoints" ) ){
  11638. uint32 traitpoints;
  11639. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  11640. return 0;
  11641. }
  11642. entry->traitpoints = traitpoints;
  11643. }else{
  11644. if( !exists ){
  11645. entry->traitpoints = 0;
  11646. }
  11647. }
  11648. if( !exists ){
  11649. this->put( level, entry );
  11650. }
  11651. return 1;
  11652. }
  11653. /**
  11654. * Generate the remaining parts of the db if necessary.
  11655. */
  11656. void PlayerStatPointDatabase::loadingFinished(){
  11657. const uint16 trait_start_level = 200;
  11658. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  11659. if( level_one == nullptr ){
  11660. if( battle_config.use_statpoint_table ){
  11661. ShowError( "Missing status points for Level 1\n" );
  11662. }
  11663. level_one = std::make_shared<s_statpoint_entry>();
  11664. level_one->level = 1;
  11665. level_one->statpoints = start_status_points;
  11666. level_one->traitpoints = 0;
  11667. this->put( 1, level_one );
  11668. }else if( battle_config.use_statpoint_table ){
  11669. if( level_one->statpoints != start_status_points ){
  11670. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, start_status_points );
  11671. level_one->statpoints = start_status_points;
  11672. }
  11673. }else{
  11674. level_one->statpoints = start_status_points;
  11675. level_one->traitpoints = 0;
  11676. }
  11677. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  11678. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  11679. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11680. bool exists = entry != nullptr;
  11681. if( !exists ){
  11682. entry = std::make_shared<s_statpoint_entry>();
  11683. entry->level = level;
  11684. this->put( level, entry );
  11685. }
  11686. if( !battle_config.use_statpoint_table || !exists ){
  11687. if( battle_config.use_statpoint_table ){
  11688. ShowError("Missing status points for Level %hu\n", level);
  11689. }
  11690. if( level <= trait_start_level ){
  11691. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  11692. }else{
  11693. entry->statpoints = last_level->statpoints;
  11694. }
  11695. }
  11696. if( !battle_config.use_traitpoint_table || !exists ){
  11697. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  11698. ShowError( "Missing trait points for Level %hu\n", level );
  11699. }
  11700. if( level > trait_start_level ){
  11701. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  11702. }else{
  11703. entry->traitpoints = 0;
  11704. }
  11705. }
  11706. // Store it for next iteration
  11707. last_level = entry;
  11708. }
  11709. }
  11710. /*==========================================
  11711. * pc DB reading.
  11712. * job_stats.yml - Job values
  11713. * skill_tree.txt - skill tree for every class
  11714. * attr_fix.yml - elemental adjustment table
  11715. *------------------------------------------*/
  11716. void pc_readdb(void) {
  11717. int i, s = 1;
  11718. const char* dbsubpath[] = {
  11719. "",
  11720. "/" DBIMPORT,
  11721. //add other path here
  11722. };
  11723. //reset
  11724. job_db.clear(); // job_info table
  11725. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11726. penalty_db.load();
  11727. #endif
  11728. statpoint_db.clear();
  11729. job_db.load();
  11730. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  11731. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  11732. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  11733. char* dbsubpath1 = (char*)aMalloc(n1+1);
  11734. char* dbsubpath2 = (char*)aMalloc(n2+1);
  11735. if(i==0) {
  11736. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11737. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  11738. }
  11739. else {
  11740. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11741. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  11742. }
  11743. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  11744. aFree(dbsubpath1);
  11745. aFree(dbsubpath2);
  11746. }
  11747. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  11748. skill_tree_db.reload();
  11749. statpoint_db.load();
  11750. }
  11751. // Read MOTD on startup. [Valaris]
  11752. int pc_read_motd(void)
  11753. {
  11754. FILE* fp;
  11755. // clear old MOTD
  11756. memset(motd_text, 0, sizeof(motd_text));
  11757. // read current MOTD
  11758. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  11759. {
  11760. unsigned int entries = 0;
  11761. char buf[CHAT_SIZE_MAX];
  11762. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  11763. {
  11764. unsigned int lines = 0;
  11765. size_t len;
  11766. lines++;
  11767. if( buf[0] == '/' && buf[1] == '/' )
  11768. continue;
  11769. len = strlen(buf);
  11770. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  11771. len--;
  11772. if( len ) {
  11773. char * ptr;
  11774. buf[len] = 0;
  11775. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  11776. 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);
  11777. }
  11778. else {// empty line
  11779. buf[0] = ' ';
  11780. buf[1] = 0;
  11781. }
  11782. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  11783. entries++;
  11784. }
  11785. fclose(fp);
  11786. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  11787. }
  11788. else
  11789. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  11790. return 0;
  11791. }
  11792. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  11793. int i,cursor = 0;
  11794. struct item_cd* cd = NULL;
  11795. if( load ) {
  11796. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  11797. // no item cooldown is associated with this character
  11798. return;
  11799. }
  11800. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11801. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  11802. sd->item_delay[cursor].tick = cd->tick[i];
  11803. sd->item_delay[cursor].nameid = cd->nameid[i];
  11804. cursor++;
  11805. }
  11806. }
  11807. idb_remove(itemcd_db,sd->status.char_id);
  11808. } else {
  11809. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  11810. // create a new skill cooldown object for map storage
  11811. CREATE( cd, struct item_cd, 1 );
  11812. idb_put( itemcd_db, sd->status.char_id, cd );
  11813. }
  11814. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11815. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  11816. cd->tick[cursor] = sd->item_delay[i].tick;
  11817. cd->nameid[cursor] = sd->item_delay[i].nameid;
  11818. cursor++;
  11819. }
  11820. }
  11821. }
  11822. return;
  11823. }
  11824. /**
  11825. * Add item delay to player's item delay data
  11826. * @param sd Player
  11827. * @param id Item data
  11828. * @param tick Current tick
  11829. * @param n Item index in inventory
  11830. * @return 0: No delay, can consume item.
  11831. * 1: Has delay, cancel consumption.
  11832. **/
  11833. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  11834. int i;
  11835. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  11836. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  11837. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  11838. if( i < MAX_ITEMDELAYS ) {
  11839. if( sd->item_delay[i].nameid ) {// found
  11840. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  11841. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  11842. char e_msg[CHAT_SIZE_MAX];
  11843. if( e_tick > 99 )
  11844. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  11845. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  11846. else
  11847. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  11848. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  11849. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  11850. return 1; // Delay has not expired yet
  11851. }
  11852. } else {// not yet used item (all slots are initially empty)
  11853. sd->item_delay[i].nameid = id->nameid;
  11854. }
  11855. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  11856. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  11857. } else {// should not happen
  11858. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  11859. }
  11860. //clean up used delays so we can give room for more
  11861. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11862. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  11863. sd->item_delay[i].tick = 0;
  11864. sd->item_delay[i].nameid = 0;
  11865. }
  11866. }
  11867. return 0;
  11868. }
  11869. /**
  11870. * Check if player has delay to reuse item
  11871. * @param sd Player
  11872. * @param id Item data
  11873. * @param tick Current tick
  11874. * @param n Item index in inventory
  11875. * @return 0: No delay, can consume item.
  11876. * 1: Has delay, cancel consumption.
  11877. **/
  11878. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  11879. struct status_change *sc = NULL;
  11880. nullpo_retr(0, sd);
  11881. nullpo_retr(0, id);
  11882. // Do normal delay assignment
  11883. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  11884. return pc_itemcd_add(sd, id, tick, n);
  11885. // Send reply of delay remains
  11886. if (sc->data[id->delay.sc]) {
  11887. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  11888. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  11889. return 1;
  11890. }
  11891. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  11892. return 0;
  11893. }
  11894. /**
  11895. * Clear the dmglog data from player
  11896. * @param sd
  11897. * @param md
  11898. **/
  11899. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  11900. {
  11901. uint8 i;
  11902. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  11903. if (i < DAMAGELOG_SIZE) {
  11904. md->dmglog[i].id = 0;
  11905. md->dmglog[i].dmg = 0;
  11906. md->dmglog[i].flag = 0;
  11907. }
  11908. }
  11909. /**
  11910. * Add log to player's dmglog
  11911. * @param sd
  11912. * @param id Monster's GID
  11913. **/
  11914. void pc_damage_log_add(struct map_session_data *sd, int id)
  11915. {
  11916. uint8 i = 0;
  11917. if (!sd || !id)
  11918. return;
  11919. //Only store new data, don't need to renew the old one with same id
  11920. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  11921. if (i < DAMAGELOG_SIZE_PC)
  11922. return;
  11923. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  11924. if (sd->dmglog[i] == 0) {
  11925. sd->dmglog[i] = id;
  11926. return;
  11927. }
  11928. }
  11929. }
  11930. /**
  11931. * Clear dmglog data from player
  11932. * @param sd
  11933. * @param id Monster's id
  11934. **/
  11935. void pc_damage_log_clear(struct map_session_data *sd, int id)
  11936. {
  11937. uint8 i;
  11938. struct mob_data *md = NULL;
  11939. if (!sd)
  11940. return;
  11941. if (!id) {
  11942. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  11943. if( !sd->dmglog[i] ) //skip the empty value
  11944. continue;
  11945. if ((md = map_id2md(sd->dmglog[i])))
  11946. pc_clear_log_damage_sub(sd->status.char_id,md);
  11947. sd->dmglog[i] = 0;
  11948. }
  11949. }
  11950. else {
  11951. if ((md = map_id2md(id)))
  11952. pc_clear_log_damage_sub(sd->status.char_id,md);
  11953. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  11954. if (i < DAMAGELOG_SIZE_PC)
  11955. sd->dmglog[i] = 0;
  11956. }
  11957. }
  11958. /**
  11959. * Status change data arrived from char-server
  11960. * @param sd: Player data
  11961. */
  11962. void pc_scdata_received(struct map_session_data *sd) {
  11963. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  11964. clif_weight_limit( sd );
  11965. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  11966. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  11967. }
  11968. sd->state.pc_loaded = true;
  11969. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  11970. sd->state.connect_new = 1;
  11971. clif_parse_LoadEndAck(sd->fd, sd);
  11972. }
  11973. if (pc_iscarton(sd)) {
  11974. sd->cart_weight_max = 0; // Force a client refesh
  11975. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  11976. }
  11977. if (sd->sc.data[SC_SOULENERGY])
  11978. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  11979. }
  11980. /**
  11981. * Check player account expiration time and rental item expirations
  11982. * @param sd: Player data
  11983. */
  11984. void pc_check_expiration(struct map_session_data *sd) {
  11985. #ifndef ENABLE_SC_SAVING
  11986. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  11987. #endif
  11988. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  11989. time_t exp_time = sd->expiration_time;
  11990. char tmpstr[1024];
  11991. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  11992. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  11993. pc_expire_check(sd);
  11994. }
  11995. }
  11996. TIMER_FUNC(pc_expiration_timer){
  11997. struct map_session_data *sd = map_id2sd(id);
  11998. if( !sd ) return 0;
  11999. sd->expiration_tid = INVALID_TIMER;
  12000. if( sd->fd )
  12001. clif_authfail_fd(sd->fd,10);
  12002. map_quit(sd);
  12003. return 0;
  12004. }
  12005. TIMER_FUNC(pc_autotrade_timer){
  12006. struct map_session_data *sd = map_id2sd(id);
  12007. if (!sd)
  12008. return 0;
  12009. sd->autotrade_tid = INVALID_TIMER;
  12010. if (sd->state.autotrade&2)
  12011. vending_reopen(sd);
  12012. if (sd->state.autotrade&4)
  12013. buyingstore_reopen(sd);
  12014. if (!sd->vender_id && !sd->buyer_id) {
  12015. sd->state.autotrade = 0;
  12016. map_quit(sd);
  12017. }
  12018. return 0;
  12019. }
  12020. /* this timer exists only when a character with a expire timer > 24h is online */
  12021. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12022. TIMER_FUNC(pc_global_expiration_timer){
  12023. struct s_mapiterator* iter;
  12024. struct map_session_data* sd;
  12025. iter = mapit_getallusers();
  12026. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12027. if( sd->expiration_time )
  12028. pc_expire_check(sd);
  12029. mapit_free(iter);
  12030. return 0;
  12031. }
  12032. void pc_expire_check(struct map_session_data *sd) {
  12033. /* ongoing timer */
  12034. if( sd->expiration_tid != INVALID_TIMER )
  12035. return;
  12036. /* not within the next 24h, enable the global check */
  12037. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12038. /* global check not running, enable */
  12039. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12040. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12041. return;
  12042. }
  12043. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12044. }
  12045. /**
  12046. * Deposit some money to bank
  12047. * @param sd
  12048. * @param money Amount of money to deposit
  12049. **/
  12050. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  12051. unsigned int limit_check = money + sd->bank_vault;
  12052. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12053. return BDA_OVERFLOW;
  12054. } else if ( money > sd->status.zeny ) {
  12055. return BDA_NO_MONEY;
  12056. }
  12057. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12058. return BDA_NO_MONEY;
  12059. sd->bank_vault += money;
  12060. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12061. if( save_settings&CHARSAVE_BANK )
  12062. chrif_save(sd, CSAVE_NORMAL);
  12063. return BDA_SUCCESS;
  12064. }
  12065. /**
  12066. * Withdraw money from bank
  12067. * @param sd
  12068. * @param money Amount of money that will be withdrawn
  12069. **/
  12070. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  12071. unsigned int limit_check = money + sd->status.zeny;
  12072. if( money <= 0 ) {
  12073. return BWA_UNKNOWN_ERROR;
  12074. } else if ( money > sd->bank_vault ) {
  12075. return BWA_NO_MONEY;
  12076. } else if ( limit_check > MAX_ZENY ) {
  12077. /* no official response for this scenario exists. */
  12078. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12079. return BWA_UNKNOWN_ERROR;
  12080. }
  12081. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12082. return BWA_NO_MONEY;
  12083. sd->bank_vault -= money;
  12084. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12085. if( save_settings&CHARSAVE_BANK )
  12086. chrif_save(sd, CSAVE_NORMAL);
  12087. return BWA_SUCCESS;
  12088. }
  12089. /**
  12090. * Clear Crimson Marker data from caster
  12091. * @param sd: Player
  12092. **/
  12093. void pc_crimson_marker_clear(struct map_session_data *sd) {
  12094. uint8 i;
  12095. if (!sd)
  12096. return;
  12097. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12098. struct block_list *bl = NULL;
  12099. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12100. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  12101. sd->c_marker[i] = 0;
  12102. }
  12103. }
  12104. /**
  12105. * Show version to player
  12106. * @param sd: Player
  12107. **/
  12108. void pc_show_version(struct map_session_data *sd) {
  12109. const char* svn = get_svn_revision();
  12110. char buf[CHAT_SIZE_MAX];
  12111. if( svn[0] != UNKNOWN_VERSION )
  12112. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12113. else {
  12114. const char* git = get_git_hash();
  12115. if( git[0] != UNKNOWN_VERSION )
  12116. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12117. else
  12118. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12119. }
  12120. clif_displaymessage(sd->fd,buf);
  12121. }
  12122. /**
  12123. * Run bonus_script on player
  12124. * @param sd
  12125. * @author [Cydh]
  12126. **/
  12127. void pc_bonus_script(struct map_session_data *sd) {
  12128. t_tick now = gettick();
  12129. struct linkdb_node *node = NULL, *next = NULL;
  12130. if (!sd || !(node = sd->bonus_script.head))
  12131. return;
  12132. while (node) {
  12133. struct s_bonus_script_entry *entry = NULL;
  12134. next = node->next;
  12135. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12136. // Only start timer for new bonus_script
  12137. if (entry->tid == INVALID_TIMER) {
  12138. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12139. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12140. entry->tick += now;
  12141. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12142. }
  12143. if (entry->script)
  12144. run_script(entry->script, 0, sd->bl.id, 0);
  12145. else
  12146. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12147. }
  12148. node = next;
  12149. }
  12150. }
  12151. /**
  12152. * Add bonus_script to player
  12153. * @param sd Player
  12154. * @param script_str Script string
  12155. * @param dur Duration in ms
  12156. * @param icon EFST
  12157. * @param flag Flags @see enum e_bonus_script_flags
  12158. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12159. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12160. * @author [Cydh]
  12161. **/
  12162. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_type icon, uint16 flag, uint8 type) {
  12163. struct script_code *script = NULL;
  12164. struct linkdb_node *node = NULL;
  12165. struct s_bonus_script_entry *entry = NULL;
  12166. if (!sd)
  12167. return NULL;
  12168. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12169. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12170. return NULL;
  12171. }
  12172. // Duplication checks
  12173. if ((node = sd->bonus_script.head)) {
  12174. while (node) {
  12175. entry = (struct s_bonus_script_entry *)node->data;
  12176. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12177. t_tick newdur = gettick() + dur;
  12178. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12179. sett_tickimer(entry->tid, newdur);
  12180. script_free_code(script);
  12181. return NULL;
  12182. }
  12183. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12184. break;
  12185. else { // No duplicate bonus
  12186. script_free_code(script);
  12187. return NULL;
  12188. }
  12189. }
  12190. node = node->next;
  12191. }
  12192. }
  12193. CREATE(entry, struct s_bonus_script_entry, 1);
  12194. entry->script_buf = StringBuf_Malloc();
  12195. StringBuf_AppendStr(entry->script_buf, script_str);
  12196. entry->tid = INVALID_TIMER;
  12197. entry->flag = flag;
  12198. entry->icon = icon;
  12199. entry->tick = dur; // Use duration first, on run change to expire time
  12200. entry->type = type;
  12201. entry->script = script;
  12202. sd->bonus_script.count++;
  12203. return entry;
  12204. }
  12205. /**
  12206. * Remove bonus_script data from player
  12207. * @param sd: Target player
  12208. * @param list: Bonus script entry from player
  12209. * @author [Cydh]
  12210. **/
  12211. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  12212. if (entry->tid != INVALID_TIMER)
  12213. delete_timer(entry->tid, pc_bonus_script_timer);
  12214. if (entry->script)
  12215. script_free_code(entry->script);
  12216. if (entry->script_buf)
  12217. StringBuf_Free(entry->script_buf);
  12218. if (sd) {
  12219. if (entry->icon != EFST_BLANK)
  12220. clif_status_load(&sd->bl, entry->icon, 0);
  12221. if (sd->bonus_script.count > 0)
  12222. sd->bonus_script.count--;
  12223. }
  12224. aFree(entry);
  12225. }
  12226. /**
  12227. * Do final process if no entry left
  12228. * @param sd
  12229. **/
  12230. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  12231. if (sd->bonus_script.count == 0) {
  12232. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12233. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12234. linkdb_final(&sd->bonus_script.head);
  12235. }
  12236. }
  12237. /**
  12238. * Timer for bonus_script
  12239. * @param tid
  12240. * @param tick
  12241. * @param id
  12242. * @param data
  12243. * @author [Cydh]
  12244. **/
  12245. TIMER_FUNC(pc_bonus_script_timer){
  12246. struct map_session_data *sd;
  12247. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12248. sd = map_id2sd(id);
  12249. if (!sd) {
  12250. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12251. return 0;
  12252. }
  12253. if (tid == INVALID_TIMER)
  12254. return 0;
  12255. if (!sd->bonus_script.head || entry == NULL) {
  12256. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12257. return 0;
  12258. }
  12259. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12260. pc_bonus_script_free_entry(sd, entry);
  12261. pc_bonus_script_check_final(sd);
  12262. status_calc_pc(sd,SCO_NONE);
  12263. return 0;
  12264. }
  12265. /**
  12266. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12267. * @param sd: Target player
  12268. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12269. * @author [Cydh]
  12270. **/
  12271. void pc_bonus_script_clear(struct map_session_data *sd, uint32 flag) {
  12272. struct linkdb_node *node = NULL;
  12273. uint16 count = 0;
  12274. if (!sd || !(node = sd->bonus_script.head))
  12275. return;
  12276. while (node) {
  12277. struct linkdb_node *next = node->next;
  12278. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12279. if (entry && (
  12280. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12281. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12282. (flag&entry->flag) || // Matched flag
  12283. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12284. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12285. )
  12286. )
  12287. {
  12288. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12289. pc_bonus_script_free_entry(sd, entry);
  12290. count++;
  12291. }
  12292. node = next;
  12293. }
  12294. pc_bonus_script_check_final(sd);
  12295. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12296. status_calc_pc(sd,SCO_NONE);
  12297. }
  12298. /** [Cydh]
  12299. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12300. * @param sd: Target player
  12301. */
  12302. void pc_cell_basilica(struct map_session_data *sd) {
  12303. nullpo_retv(sd);
  12304. #ifdef RENEWAL
  12305. enum sc_type type = SC_BASILICA_CELL;
  12306. #else
  12307. enum sc_type type = SC_BASILICA;
  12308. #endif
  12309. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12310. if (sd->sc.data[type])
  12311. status_change_end(&sd->bl, type,INVALID_TIMER);
  12312. }
  12313. else if (!sd->sc.data[type])
  12314. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12315. }
  12316. /** [Cydh]
  12317. * Get maximum specified parameter for specified class
  12318. * @param sd: Player data
  12319. * @param param: Max parameter to check
  12320. * @return max_param
  12321. */
  12322. uint16 pc_maxparameter(struct map_session_data *sd, e_params param) {
  12323. nullpo_retr(0, sd);
  12324. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12325. if( job == nullptr || param == PARAM_MAX ){
  12326. return 0;
  12327. }
  12328. return job->max_param[param];
  12329. }
  12330. /**
  12331. * Get max ASPD for player based on Class
  12332. * @param sd Player
  12333. * @return ASPD
  12334. */
  12335. short pc_maxaspd(struct map_session_data *sd) {
  12336. nullpo_ret(sd);
  12337. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12338. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12339. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12340. battle_config.max_aspd ));
  12341. }
  12342. /**
  12343. * Calculates total item-group related bonuses for the given item
  12344. * @param sd Player
  12345. * @param nameid Item ID
  12346. * @return Heal rate
  12347. **/
  12348. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12349. if (bonuses.empty())
  12350. return 0;
  12351. short bonus = 0;
  12352. for (const auto &it : bonuses) {
  12353. uint16 group_id = it.id;
  12354. if (group_id == 0)
  12355. continue;
  12356. if (itemdb_group.item_exists(group_id, nameid))
  12357. bonus += it.val;
  12358. }
  12359. return bonus;
  12360. }
  12361. /**
  12362. * Calculates total item-group related bonuses for the given item group
  12363. * @param sd Player
  12364. * @param group_id Item Group ID
  12365. * @return Heal rate
  12366. **/
  12367. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12368. if (bonuses.empty())
  12369. return 0;
  12370. for (const auto &it : bonuses) {
  12371. if (it.id == group_id)
  12372. return it.val;
  12373. }
  12374. return 0;
  12375. }
  12376. /**
  12377. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12378. * @param eqi Item EQI of enum equip_index
  12379. * @param *equip_index Player's equip_index[]
  12380. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12381. * @return True if item in same inventory index, False if doesn't
  12382. */
  12383. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12384. if (index < 0 || index >= MAX_INVENTORY)
  12385. return true;
  12386. // Dual weapon checks
  12387. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12388. return true;
  12389. // Headgear with Mid & Low location
  12390. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12391. return true;
  12392. // Headgear with Top & Mid or Low location
  12393. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12394. return true;
  12395. // Headgear with Mid & Low location
  12396. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12397. return true;
  12398. // Headgear with Top & Mid or Low location
  12399. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12400. return true;
  12401. return false;
  12402. }
  12403. /**
  12404. * Generate Unique item ID for player
  12405. * @param sd : Player
  12406. * @return A generated Unique item ID
  12407. */
  12408. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  12409. nullpo_ret(sd);
  12410. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12411. }
  12412. /**
  12413. * Validating skill from player after logged on
  12414. * @param sd
  12415. **/
  12416. void pc_validate_skill(struct map_session_data *sd) {
  12417. if (sd) {
  12418. uint16 i = 0, count = 0;
  12419. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12420. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12421. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12422. for (i = 0; i < MAX_SKILL; i++) {
  12423. uint16 idx = 0;
  12424. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12425. continue;
  12426. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12427. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12428. count++;
  12429. }
  12430. else
  12431. 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);
  12432. }
  12433. }
  12434. }
  12435. /**
  12436. * Show available NPC Quest / Event Icon Check [Kisuka]
  12437. * @param sd Player
  12438. **/
  12439. void pc_show_questinfo(struct map_session_data *sd) {
  12440. #if PACKETVER >= 20090218
  12441. nullpo_retv(sd);
  12442. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12443. return;
  12444. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12445. nullpo_retv(mapdata);
  12446. if (mapdata->qi_npc.empty())
  12447. return;
  12448. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12449. return; // init was not called yet
  12450. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12451. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12452. if (!nd || nd->qi_data.empty())
  12453. continue;
  12454. bool show = false;
  12455. for (auto &qi : nd->qi_data) {
  12456. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12457. show = true;
  12458. // Check if need to be displayed
  12459. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12460. sd->qi_display[i].is_active = true;
  12461. sd->qi_display[i].icon = qi->icon;
  12462. sd->qi_display[i].color = qi->color;
  12463. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12464. }
  12465. break;
  12466. }
  12467. }
  12468. if (show == false) {
  12469. // Check if need to be hide
  12470. if (sd->qi_display[i].is_active) {
  12471. sd->qi_display[i].is_active = false;
  12472. sd->qi_display[i].icon = QTYPE_NONE;
  12473. sd->qi_display[i].color = QMARK_NONE;
  12474. #if PACKETVER >= 20120410
  12475. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12476. #else
  12477. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12478. #endif
  12479. }
  12480. }
  12481. }
  12482. #endif
  12483. }
  12484. /**
  12485. * Reinit the questinfo for player when changing map
  12486. * @param sd Player
  12487. **/
  12488. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  12489. #if PACKETVER >= 20090218
  12490. nullpo_retv(sd);
  12491. sd->qi_display.clear();
  12492. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12493. return;
  12494. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12495. nullpo_retv(mapdata);
  12496. if (mapdata->qi_npc.empty())
  12497. return;
  12498. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12499. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12500. sd->qi_display.push_back( s_qi_display() );
  12501. }
  12502. #endif
  12503. }
  12504. /**
  12505. * Check if a job is allowed to enter the map
  12506. * @param jobid Job ID see enum e_job or sd->status.class_
  12507. * @param m ID -an index- for direct indexing map[] array
  12508. * @return 1 if job is allowed, 0 otherwise
  12509. **/
  12510. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12511. // Map is other map server.
  12512. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12513. if (m < 0)
  12514. return true;
  12515. struct map_data *mapdata = map_getmapdata(m);
  12516. if (!mapdata->cell)
  12517. return false;
  12518. if (!pcdb_checkid(jobid))
  12519. return false;
  12520. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12521. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12522. return true;
  12523. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12524. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12525. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12526. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12527. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12528. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12529. )
  12530. return false;
  12531. return true;
  12532. }
  12533. /**
  12534. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12535. * @param sd
  12536. **/
  12537. void pc_set_costume_view(struct map_session_data *sd) {
  12538. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12539. struct item_data *id = NULL;
  12540. nullpo_retv(sd);
  12541. head_low = sd->status.head_bottom;
  12542. head_mid = sd->status.head_mid;
  12543. head_top = sd->status.head_top;
  12544. robe = sd->status.robe;
  12545. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12546. //Added check to prevent sending the same look on multiple slots ->
  12547. //causes client to redraw item on top of itself. (suggested by Lupus)
  12548. // Normal headgear checks
  12549. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12550. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12551. sd->status.head_bottom = id->look;
  12552. else
  12553. sd->status.head_bottom = 0;
  12554. }
  12555. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12556. if (!(id->equip&(EQP_HEAD_TOP)))
  12557. sd->status.head_mid = id->look;
  12558. else
  12559. sd->status.head_mid = 0;
  12560. }
  12561. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12562. sd->status.head_top = id->look;
  12563. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12564. sd->status.robe = id->look;
  12565. // Costumes check
  12566. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12567. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12568. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12569. sd->status.head_bottom = id->look;
  12570. else
  12571. sd->status.head_bottom = 0;
  12572. }
  12573. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12574. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12575. sd->status.head_mid = id->look;
  12576. else
  12577. sd->status.head_mid = 0;
  12578. }
  12579. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12580. sd->status.head_top = id->look;
  12581. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12582. sd->status.robe = id->look;
  12583. }
  12584. if (sd->setlook_head_bottom)
  12585. sd->status.head_bottom = sd->setlook_head_bottom;
  12586. if (sd->setlook_head_mid)
  12587. sd->status.head_mid = sd->setlook_head_mid;
  12588. if (sd->setlook_head_top)
  12589. sd->status.head_top = sd->setlook_head_top;
  12590. if (sd->setlook_robe)
  12591. sd->status.robe = sd->setlook_robe;
  12592. if (head_low != sd->status.head_bottom)
  12593. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12594. if (head_mid != sd->status.head_mid)
  12595. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12596. if (head_top != sd->status.head_top)
  12597. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12598. if (robe != sd->status.robe)
  12599. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  12600. }
  12601. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  12602. uint32 date = date_get(DT_YYYYMMDD);
  12603. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  12604. std::shared_ptr<s_attendance_period> period = pair.second;
  12605. if( period->start <= date && period->end >= date ){
  12606. return period;
  12607. }
  12608. }
  12609. return nullptr;
  12610. }
  12611. bool pc_attendance_enabled(){
  12612. // Check if the attendance feature is disabled
  12613. if( !battle_config.feature_attendance ){
  12614. return false;
  12615. }
  12616. // Check if there is a running attendance period
  12617. return pc_attendance_period() != nullptr;
  12618. }
  12619. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  12620. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  12621. }
  12622. int32 pc_attendance_counter( struct map_session_data* sd ){
  12623. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12624. // No running attendance period
  12625. if( period == nullptr ){
  12626. return 0;
  12627. }
  12628. // Get the counter for the current period
  12629. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12630. // Check if we have a remaining counter from a previous period
  12631. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  12632. // Reset the counter to zero
  12633. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  12634. return 0;
  12635. }
  12636. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  12637. }
  12638. void pc_attendance_claim_reward( struct map_session_data* sd ){
  12639. // If the user's group does not have the permission
  12640. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  12641. return;
  12642. }
  12643. // Check if the attendance feature is disabled
  12644. if( !pc_attendance_enabled() ){
  12645. return;
  12646. }
  12647. // Check if the user already got his reward today
  12648. if( pc_attendance_rewarded_today( sd ) ){
  12649. return;
  12650. }
  12651. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12652. attendance_counter += 1;
  12653. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12654. if( period == nullptr ){
  12655. return;
  12656. }
  12657. if( period->rewards.size() < attendance_counter ){
  12658. return;
  12659. }
  12660. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  12661. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  12662. if( save_settings&CHARSAVE_ATTENDANCE )
  12663. chrif_save(sd, CSAVE_NORMAL);
  12664. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  12665. struct mail_message msg;
  12666. memset( &msg, 0, sizeof( struct mail_message ) );
  12667. msg.dest_id = sd->status.char_id;
  12668. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  12669. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  12670. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  12671. msg.item[0].nameid = reward->item_id;
  12672. msg.item[0].amount = reward->amount;
  12673. msg.item[0].identify = 1;
  12674. msg.status = MAIL_NEW;
  12675. msg.type = MAIL_INBOX_NORMAL;
  12676. msg.timestamp = time(NULL);
  12677. intif_Mail_send(0, &msg);
  12678. clif_attendence_response( sd, attendance_counter );
  12679. }
  12680. /*==========================================
  12681. * pc Init/Terminate
  12682. *------------------------------------------*/
  12683. void do_final_pc(void) {
  12684. db_destroy(itemcd_db);
  12685. do_final_pc_groups();
  12686. ers_destroy(pc_sc_display_ers);
  12687. ers_destroy(num_reg_ers);
  12688. ers_destroy(str_reg_ers);
  12689. attendance_db.clear();
  12690. penalty_db.clear();
  12691. }
  12692. void do_init_pc(void) {
  12693. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  12694. pc_readdb();
  12695. pc_read_motd(); // Read MOTD [Valaris]
  12696. attendance_db.load();
  12697. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  12698. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  12699. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  12700. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  12701. add_timer_func_list(pc_autosave, "pc_autosave");
  12702. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  12703. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  12704. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  12705. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  12706. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  12707. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  12708. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  12709. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  12710. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  12711. // 0=day, 1=night [Yor]
  12712. night_flag = battle_config.night_at_start ? 1 : 0;
  12713. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  12714. int day_duration = battle_config.day_duration;
  12715. int night_duration = battle_config.night_duration;
  12716. // add night/day timer [Yor]
  12717. add_timer_func_list(map_day_timer, "map_day_timer");
  12718. add_timer_func_list(map_night_timer, "map_night_timer");
  12719. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  12720. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  12721. }
  12722. do_init_pc_groups();
  12723. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  12724. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  12725. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  12726. ers_chunk_size(pc_sc_display_ers, 150);
  12727. ers_chunk_size(num_reg_ers, 300);
  12728. ers_chunk_size(str_reg_ers, 50);
  12729. }