pc.cpp 419 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717
  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. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  57. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  58. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  59. PlayerStatPointDatabase statpoint_db;
  60. // h-files are for declarations, not for implementations... [Shinomori]
  61. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  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. struct s_job_info job_info[CLASS_COUNT];
  73. const std::string AttendanceDatabase::getDefaultLocation(){
  74. return std::string(db_path) + "/attendance.yml";
  75. }
  76. /**
  77. * Reads and parses an entry from the attendance_db.
  78. * @param node: YAML node containing the entry.
  79. * @return count of successfully parsed rows
  80. */
  81. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  82. uint32 start;
  83. if( !this->asUInt32( node, "Start", start ) ){
  84. return 0;
  85. }
  86. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  87. bool exists = attendance_period != nullptr;
  88. if( !exists ){
  89. if( !this->nodeExists( node, "End" ) ){
  90. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  91. return 0;
  92. }
  93. if( !this->nodeExists( node, "Rewards" ) ){
  94. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  95. return 0;
  96. }
  97. attendance_period = std::make_shared<s_attendance_period>();
  98. attendance_period->start = start;
  99. }
  100. // If it does not exist yet, we need to check it for sure
  101. bool requiresCollisionDetection = !exists;
  102. if( this->nodeExists( node, "End" ) ){
  103. uint32 end;
  104. if( !this->asUInt32( node, "End", end ) ){
  105. return 0;
  106. }
  107. // If the period is outdated already, we do not even bother parsing
  108. if( end < date_get( DT_YYYYMMDD ) ){
  109. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  110. return 0;
  111. }
  112. if( !exists || attendance_period->end != end ){
  113. requiresCollisionDetection = true;
  114. attendance_period->end = end;
  115. }
  116. }
  117. // Collision detection
  118. if( requiresCollisionDetection ){
  119. bool collision = false;
  120. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  121. std::shared_ptr<s_attendance_period> period = pair.second;
  122. if( exists && period->start == attendance_period->start ){
  123. // Dont compare to yourself
  124. continue;
  125. }
  126. // Check if start is inside another period
  127. if( period->start <= attendance_period->start && start <= period->end ){
  128. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  129. collision = true;
  130. break;
  131. }
  132. // Check if end is inside another period
  133. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  134. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  135. collision = true;
  136. break;
  137. }
  138. }
  139. if( collision ){
  140. return 0;
  141. }
  142. }
  143. if( this->nodeExists( node, "Rewards" ) ){
  144. const YAML::Node& rewardsNode = node["Rewards"];
  145. for( const YAML::Node& rewardNode : rewardsNode ){
  146. uint32 day;
  147. if( !this->asUInt32( rewardNode, "Day", day ) ){
  148. continue;
  149. }
  150. day -= 1;
  151. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  152. bool reward_exists = reward != nullptr;
  153. if( !reward_exists ){
  154. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  155. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  156. return 0;
  157. }
  158. reward = std::make_shared<s_attendance_reward>();
  159. }
  160. if( this->nodeExists( rewardNode, "ItemId" ) ){
  161. t_itemid item_id;
  162. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  163. continue;
  164. }
  165. if( item_id == 0 || !itemdb_exists( item_id ) ){
  166. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  167. continue;
  168. }
  169. reward->item_id = item_id;
  170. }
  171. if( this->nodeExists( rewardNode, "Amount" ) ){
  172. uint16 amount;
  173. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  174. continue;
  175. }
  176. if( amount == 0 ){
  177. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  178. amount = 1;
  179. }else if( amount > MAX_AMOUNT ){
  180. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  181. amount = MAX_AMOUNT;
  182. }
  183. reward->amount = amount;
  184. }else{
  185. if( !reward_exists ){
  186. reward->amount = 1;
  187. }
  188. }
  189. if( !reward_exists ){
  190. attendance_period->rewards[day] = reward;
  191. }
  192. }
  193. bool missing_day = false;
  194. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  195. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  196. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  197. missing_day = true;
  198. break;
  199. }
  200. }
  201. if( missing_day ){
  202. return 0;
  203. }
  204. }
  205. if( !exists ){
  206. this->put( start, attendance_period );
  207. }
  208. return 1;
  209. }
  210. AttendanceDatabase attendance_db;
  211. const std::string PenaltyDatabase::getDefaultLocation(){
  212. return std::string( db_path ) + "/level_penalty.yml";
  213. }
  214. uint64 PenaltyDatabase::parseBodyNode( const YAML::Node& node ){
  215. std::string type_constant;
  216. if( !this->asString( node, "Type", type_constant ) ){
  217. return 0;
  218. }
  219. int64 constant_value;
  220. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  221. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  222. return 0;
  223. }
  224. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  225. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  226. return 0;
  227. }
  228. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  229. std::shared_ptr<s_penalty> penalty = this->find( type );
  230. bool exists = penalty != nullptr;
  231. if( !exists ){
  232. penalty = std::make_shared<s_penalty>();
  233. penalty->type = type;
  234. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  235. penalty->rate[i] = UINT16_MAX;
  236. }
  237. }
  238. if( this->nodeExists( node, "LevelDifferences" ) ){
  239. for( const YAML::Node& levelNode : node["LevelDifferences"] ){
  240. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  241. return 0;
  242. }
  243. int32 difference;
  244. if( !this->asInt32( levelNode, "Difference", difference ) ){
  245. return 0;
  246. }
  247. if( std::abs( difference ) > MAX_LEVEL ){
  248. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  249. return 0;
  250. }
  251. uint16 rate;
  252. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  253. return 0;
  254. }
  255. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  256. }
  257. }
  258. if( !exists ){
  259. this->put( type, penalty );
  260. }
  261. return 1;
  262. }
  263. void PenaltyDatabase::loadingFinished(){
  264. for( const auto& pair : *this ){
  265. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  266. uint16 rate = pair.second->rate[i];
  267. // Check if it has been defined
  268. if( rate == UINT16_MAX ){
  269. pair.second->rate[i] = last_rate;
  270. }else{
  271. last_rate = rate;
  272. }
  273. }
  274. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  275. uint16 rate = pair.second->rate[i];
  276. // Check if it has been defined
  277. if( rate == UINT16_MAX ){
  278. pair.second->rate[i] = last_rate;
  279. }else{
  280. last_rate = rate;
  281. }
  282. }
  283. }
  284. }
  285. PenaltyDatabase penalty_db;
  286. #define MOTD_LINE_SIZE 128
  287. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  288. bool reg_load;
  289. /**
  290. * Translation table from athena equip index to aegis bitmask
  291. */
  292. unsigned int equip_bitmask[EQI_MAX] = {
  293. EQP_ACC_L, // EQI_ACC_L
  294. EQP_ACC_R, // EQI_ACC_R
  295. EQP_SHOES, // EQI_SHOES
  296. EQP_GARMENT, // EQI_GARMENT
  297. EQP_HEAD_LOW, // EQI_HEAD_LOW
  298. EQP_HEAD_MID, // EQI_HEAD_MID
  299. EQP_HEAD_TOP, // EQI_HEAD_TOP
  300. EQP_ARMOR, // EQI_ARMOR
  301. EQP_HAND_L, // EQI_HAND_L
  302. EQP_HAND_R, // EQI_HAND_R
  303. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  304. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  305. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  306. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  307. EQP_AMMO, // EQI_AMMO
  308. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  309. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  310. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  311. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  312. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  313. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  314. };
  315. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  316. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  317. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  318. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  319. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  320. };
  321. void pc_set_reg_load( bool val ){
  322. reg_load = val;
  323. }
  324. /**
  325. * Item Cool Down Delay Saving
  326. * Struct item_cd is not a member of struct map_session_data
  327. * to keep cooldowns in memory between player log-ins.
  328. * All cooldowns are reset when server is restarted.
  329. **/
  330. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  331. struct item_cd {
  332. t_tick tick[MAX_ITEMDELAYS]; //tick
  333. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  334. };
  335. /**
  336. * Converts a class to its array index for CLASS_COUNT defined arrays.
  337. * Note that it does not do a validity check for speed purposes, where parsing
  338. * player input make sure to use a pcdb_checkid first!
  339. * @param class_ Job ID see enum e_job
  340. * @return Class Index
  341. */
  342. int pc_class2idx(int class_) {
  343. if (class_ >= JOB_NOVICE_HIGH)
  344. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  345. return class_;
  346. }
  347. /**
  348. * Get player's group ID
  349. * @param sd
  350. * @return Group ID
  351. */
  352. int pc_get_group_id(struct map_session_data *sd) {
  353. return sd->group_id;
  354. }
  355. /** Get player's group Level
  356. * @param sd
  357. * @return Group Level
  358. */
  359. int pc_get_group_level(struct map_session_data *sd) {
  360. return sd->group_level;
  361. }
  362. /**
  363. * Remove a player from queue after timeout
  364. * @param tid: Timer ID
  365. * @param tick: Timer
  366. * @param id: ID
  367. * @return 0 on success or 1 otherwise
  368. */
  369. static TIMER_FUNC(pc_on_expire_active)
  370. {
  371. map_session_data *sd = (map_session_data *)data;
  372. nullpo_retr(1, sd);
  373. sd->tid_queue_active = INVALID_TIMER;
  374. bg_queue_leave(sd);
  375. clif_bg_queue_entry_init(sd);
  376. return 0;
  377. }
  378. /**
  379. * Function used to set timer externally
  380. * @param sd: Player data
  381. */
  382. void pc_set_bg_queue_timer(map_session_data *sd) {
  383. nullpo_retv(sd);
  384. if (sd->tid_queue_active != INVALID_TIMER) {
  385. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  386. sd->tid_queue_active = INVALID_TIMER;
  387. }
  388. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  389. }
  390. /**
  391. * Function used to delete timer externally
  392. * @param sd: Player data
  393. */
  394. void pc_delete_bg_queue_timer(map_session_data *sd) {
  395. nullpo_retv(sd);
  396. if (sd->tid_queue_active != INVALID_TIMER) {
  397. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  398. sd->tid_queue_active = INVALID_TIMER;
  399. }
  400. }
  401. static TIMER_FUNC(pc_invincible_timer){
  402. struct map_session_data *sd;
  403. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  404. return 1;
  405. if(sd->invincible_timer != tid){
  406. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  407. return 0;
  408. }
  409. sd->invincible_timer = INVALID_TIMER;
  410. skill_unit_move(&sd->bl,tick,1);
  411. return 0;
  412. }
  413. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  414. nullpo_retv(sd);
  415. if( sd->invincible_timer != INVALID_TIMER )
  416. delete_timer(sd->invincible_timer,pc_invincible_timer);
  417. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  418. }
  419. void pc_delinvincibletimer(struct map_session_data* sd)
  420. {
  421. nullpo_retv(sd);
  422. if( sd->invincible_timer != INVALID_TIMER )
  423. {
  424. delete_timer(sd->invincible_timer,pc_invincible_timer);
  425. sd->invincible_timer = INVALID_TIMER;
  426. skill_unit_move(&sd->bl,gettick(),1);
  427. }
  428. }
  429. static TIMER_FUNC(pc_spiritball_timer){
  430. struct map_session_data *sd;
  431. int i;
  432. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  433. return 1;
  434. if( sd->spiritball <= 0 )
  435. {
  436. 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);
  437. sd->spiritball = 0;
  438. return 0;
  439. }
  440. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  441. if( i == sd->spiritball )
  442. {
  443. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  444. return 0;
  445. }
  446. sd->spiritball--;
  447. if( i != sd->spiritball )
  448. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  449. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  450. clif_spiritball(&sd->bl);
  451. return 0;
  452. }
  453. /**
  454. * Adds a spiritball to player for 'interval' ms
  455. * @param sd
  456. * @param interval
  457. * @param max
  458. */
  459. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  460. {
  461. int tid;
  462. uint8 i;
  463. nullpo_retv(sd);
  464. if(max > MAX_SPIRITBALL)
  465. max = MAX_SPIRITBALL;
  466. if(sd->spiritball < 0)
  467. sd->spiritball = 0;
  468. if( sd->spiritball && sd->spiritball >= max )
  469. {
  470. if(sd->spirit_timer[0] != INVALID_TIMER)
  471. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  472. sd->spiritball--;
  473. if( sd->spiritball != 0 )
  474. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  475. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  476. }
  477. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  478. 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);
  479. if( i != sd->spiritball )
  480. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  481. sd->spirit_timer[i] = tid;
  482. sd->spiritball++;
  483. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  484. clif_millenniumshield(&sd->bl,sd->spiritball);
  485. else
  486. clif_spiritball(&sd->bl);
  487. }
  488. /**
  489. * Removes number of spiritball from player
  490. * @param sd
  491. * @param count
  492. * @param type 1 = doesn't give client effect
  493. */
  494. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  495. {
  496. uint8 i;
  497. nullpo_retv(sd);
  498. if(sd->spiritball <= 0) {
  499. sd->spiritball = 0;
  500. return;
  501. }
  502. if(count == 0)
  503. return;
  504. if(count > sd->spiritball)
  505. count = sd->spiritball;
  506. sd->spiritball -= count;
  507. if(count > MAX_SPIRITBALL)
  508. count = MAX_SPIRITBALL;
  509. for(i=0;i<count;i++) {
  510. if(sd->spirit_timer[i] != INVALID_TIMER) {
  511. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  512. sd->spirit_timer[i] = INVALID_TIMER;
  513. }
  514. }
  515. for(i=count;i<MAX_SPIRITBALL;i++) {
  516. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  517. sd->spirit_timer[i] = INVALID_TIMER;
  518. }
  519. if(!type) {
  520. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  521. clif_millenniumshield(&sd->bl,sd->spiritball);
  522. else
  523. clif_spiritball(&sd->bl);
  524. }
  525. }
  526. /**
  527. * Adds a soulball to player
  528. * @param sd: Player data
  529. * @param max: Max amount of soulballs
  530. */
  531. int pc_addsoulball(map_session_data *sd, int max)
  532. {
  533. nullpo_ret(sd);
  534. status_change *sc = status_get_sc(&sd->bl);
  535. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  536. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  537. sd->soulball = 0;
  538. }
  539. max = min(max, MAX_SOUL_BALL);
  540. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  541. if (sd->soulball && sd->soulball >= max)
  542. sd->soulball--;
  543. sd->soulball++;
  544. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  545. clif_soulball(sd);
  546. return 0;
  547. }
  548. /**
  549. * Removes number of soulball from player
  550. * @param sd: Player data
  551. * @param count: Amount to remove
  552. * @param type: true = doesn't give client effect
  553. */
  554. int pc_delsoulball(map_session_data *sd, int count, bool type)
  555. {
  556. nullpo_ret(sd);
  557. if (count <= 0)
  558. return 0;
  559. status_change *sc = status_get_sc(&sd->bl);
  560. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  561. sd->soulball = 0;
  562. }else{
  563. sd->soulball -= cap_value(count, 0, sd->soulball);
  564. if (sd->soulball == 0)
  565. status_change_end(&sd->bl, SC_SOULENERGY, INVALID_TIMER);
  566. else
  567. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  568. }
  569. if (!type)
  570. clif_soulball(sd);
  571. return 0;
  572. }
  573. /**
  574. * Increases a player's fame points and displays a notice to him
  575. * @param sd Player
  576. * @param count Fame point
  577. */
  578. void pc_addfame(struct map_session_data *sd,int count)
  579. {
  580. enum e_rank ranktype;
  581. nullpo_retv(sd);
  582. sd->status.fame += count;
  583. if(sd->status.fame > MAX_FAME)
  584. sd->status.fame = MAX_FAME;
  585. switch(sd->class_&MAPID_UPPERMASK){
  586. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  587. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  588. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  589. default:
  590. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  591. return;
  592. }
  593. clif_update_rankingpoint(sd,ranktype,count);
  594. chrif_updatefamelist(sd);
  595. }
  596. /**
  597. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  598. * @param sd
  599. * @param job Job use enum e_mapid
  600. * @return Rank
  601. */
  602. unsigned char pc_famerank(uint32 char_id, int job)
  603. {
  604. uint8 i;
  605. switch(job){
  606. case MAPID_BLACKSMITH: // Blacksmith
  607. for(i = 0; i < MAX_FAME_LIST; i++){
  608. if(smith_fame_list[i].id == char_id)
  609. return i + 1;
  610. }
  611. break;
  612. case MAPID_ALCHEMIST: // Alchemist
  613. for(i = 0; i < MAX_FAME_LIST; i++){
  614. if(chemist_fame_list[i].id == char_id)
  615. return i + 1;
  616. }
  617. break;
  618. case MAPID_TAEKWON: // Taekwon
  619. for(i = 0; i < MAX_FAME_LIST; i++){
  620. if(taekwon_fame_list[i].id == char_id)
  621. return i + 1;
  622. }
  623. break;
  624. }
  625. return 0;
  626. }
  627. /**
  628. * Restart player's HP & SP value
  629. * @param sd
  630. * @param type Restart type: 1 - Normal Resurection
  631. */
  632. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  633. struct status_data *status, *b_status;
  634. nullpo_retv(sd);
  635. b_status = &sd->base_status;
  636. status = &sd->battle_status;
  637. if (type&1) { //Normal resurrection
  638. status->hp = 1; //Otherwise status_heal may fail if dead.
  639. status_heal(&sd->bl, b_status->hp, 0, 1);
  640. if( status->sp < b_status->sp )
  641. status_set_sp(&sd->bl, b_status->sp, 1);
  642. } else { //Just for saving on the char-server (with values as if respawned)
  643. sd->status.hp = b_status->hp;
  644. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  645. }
  646. }
  647. /*==========================================
  648. Rental System
  649. *------------------------------------------*/
  650. /**
  651. * Ends a rental and removes the item/effect
  652. * @param tid: Tick ID
  653. * @param tick: Timer
  654. * @param id: Timer ID
  655. * @param data: Data
  656. * @return false - failure, true - success
  657. */
  658. TIMER_FUNC(pc_inventory_rental_end){
  659. struct map_session_data *sd = map_id2sd(id);
  660. if( sd == NULL )
  661. return 0;
  662. if( tid != sd->rental_timer ) {
  663. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  664. return 0;
  665. }
  666. pc_inventory_rentals(sd);
  667. return 1;
  668. }
  669. /**
  670. * Removes the rental timer from the player
  671. * @param sd: Player data
  672. */
  673. void pc_inventory_rental_clear(struct map_session_data *sd)
  674. {
  675. if( sd->rental_timer != INVALID_TIMER ) {
  676. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  677. sd->rental_timer = INVALID_TIMER;
  678. }
  679. }
  680. /**
  681. * Check for items in the player's inventory that are rental type
  682. * @param sd: Player data
  683. */
  684. void pc_inventory_rentals(struct map_session_data *sd)
  685. {
  686. int i, c = 0;
  687. unsigned int next_tick = UINT_MAX;
  688. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  689. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  690. continue; // Nothing here
  691. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  692. continue;
  693. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  694. if (sd->inventory_data[i]->unequip_script)
  695. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  696. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  697. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  698. } else {
  699. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  700. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  701. next_tick = umin(expire_tick * 1000U, next_tick);
  702. c++;
  703. }
  704. }
  705. 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
  706. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  707. else
  708. sd->rental_timer = INVALID_TIMER;
  709. }
  710. /**
  711. * Add a rental item to the player and adjusts the rental timer appropriately
  712. * @param sd: Player data
  713. * @param seconds: Rental time
  714. */
  715. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  716. {
  717. t_tick tick = seconds * 1000;
  718. if( sd == NULL )
  719. return;
  720. if( sd->rental_timer != INVALID_TIMER ) {
  721. const struct TimerData * td;
  722. td = get_timer(sd->rental_timer);
  723. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  724. pc_inventory_rental_clear(sd);
  725. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  726. }
  727. } else
  728. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  729. }
  730. /**
  731. * Check if the player can sell the current item
  732. * @param sd: map_session_data of the player
  733. * @param item: struct of the checking item
  734. * @param shoptype: NPC's sub type see enum npc_subtype
  735. * @return bool 'true' is sellable, 'false' otherwise
  736. */
  737. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  738. if (sd == NULL || item == NULL)
  739. return false;
  740. if (item->equip > 0 || item->amount < 0)
  741. return false;
  742. if (battle_config.hide_fav_sell && item->favorite)
  743. return false; //Cannot sell favs (optional config)
  744. if (!battle_config.rental_transaction && item->expire_time)
  745. return false; // Cannot Sell Rental Items
  746. if( item->equipSwitch ){
  747. return false;
  748. }
  749. if (itemdb_ishatched_egg(item))
  750. return false;
  751. switch (shoptype) {
  752. case NPCTYPE_SHOP:
  753. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  754. item->bound != BOUND_GUILD ||
  755. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  756. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  757. ))
  758. return true;
  759. break;
  760. case NPCTYPE_ITEMSHOP:
  761. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  762. item->bound != BOUND_GUILD ||
  763. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  764. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  765. ))
  766. return true;
  767. else if (!item->bound) {
  768. struct item_data *itd = itemdb_search(item->nameid);
  769. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  770. return true;
  771. }
  772. break;
  773. }
  774. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  775. return false;
  776. if (item->bound && !pc_can_give_bounded_items(sd))
  777. return false; // Don't allow sale of bound items
  778. return true;
  779. }
  780. /**
  781. * Determines if player can give / drop / trade / vend items
  782. */
  783. bool pc_can_give_items(struct map_session_data *sd)
  784. {
  785. return pc_has_permission(sd, PC_PERM_TRADE);
  786. }
  787. /**
  788. * Determines if player can give / drop / trade / vend bounded items
  789. */
  790. bool pc_can_give_bounded_items(struct map_session_data *sd)
  791. {
  792. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  793. }
  794. /**
  795. * Determine if an item in a player's inventory is tradeable based on several merits.
  796. * Checks for item_trade, bound, and rental restrictions.
  797. * @param sd: Player data
  798. * @param index: Item inventory index
  799. * @return True if the item can be traded or false otherwise
  800. */
  801. bool pc_can_trade_item(map_session_data *sd, int index) {
  802. if (sd && index >= 0) {
  803. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  804. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  805. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  806. );
  807. }
  808. return false;
  809. }
  810. /*==========================================
  811. * Prepares character for saving.
  812. * @param sd
  813. *------------------------------------------*/
  814. void pc_makesavestatus(struct map_session_data *sd) {
  815. nullpo_retv(sd);
  816. if(!battle_config.save_clothcolor)
  817. sd->status.clothes_color = 0;
  818. if(!battle_config.save_body_style)
  819. sd->status.body = 0;
  820. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  821. //status change load/saving. [Skotlex]
  822. #ifdef NEW_CARTS
  823. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  824. #else
  825. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  826. #endif
  827. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  828. if(pc_isdead(sd)){
  829. pc_setrestartvalue(sd, 0);
  830. } else {
  831. sd->status.hp = sd->battle_status.hp;
  832. sd->status.sp = sd->battle_status.sp;
  833. }
  834. sd->status.last_point.map = sd->mapindex;
  835. sd->status.last_point.x = sd->bl.x;
  836. sd->status.last_point.y = sd->bl.y;
  837. return;
  838. }
  839. if(pc_isdead(sd)) {
  840. pc_setrestartvalue(sd, 0);
  841. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  842. } else {
  843. sd->status.hp = sd->battle_status.hp;
  844. sd->status.sp = sd->battle_status.sp;
  845. sd->status.last_point.map = sd->mapindex;
  846. sd->status.last_point.x = sd->bl.x;
  847. sd->status.last_point.y = sd->bl.y;
  848. }
  849. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  850. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  851. if(mapdata->save.map)
  852. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  853. else
  854. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  855. }
  856. }
  857. /*==========================================
  858. * Off init ? Connection?
  859. *------------------------------------------*/
  860. 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) {
  861. nullpo_retv(sd);
  862. sd->bl.id = account_id;
  863. sd->status.account_id = account_id;
  864. sd->status.char_id = char_id;
  865. sd->status.sex = sex;
  866. sd->login_id1 = login_id1;
  867. sd->login_id2 = 0; // at this point, we can not know the value :(
  868. sd->client_tick = client_tick;
  869. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  870. sd->bl.type = BL_PC;
  871. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  872. sd->canlog_tick = gettick();
  873. //Required to prevent homunculus copuing a base speed of 0.
  874. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  875. }
  876. /**
  877. * Get equip point for an equip
  878. * @param sd
  879. * @param id
  880. */
  881. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  882. int ep = 0;
  883. nullpo_ret(sd);
  884. nullpo_ret(id);
  885. if (!itemdb_isequip2(id))
  886. return 0; //Not equippable by players.
  887. ep = id->equip;
  888. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  889. 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]
  890. if (ep == EQP_WEAPON)
  891. return EQP_ARMS;
  892. if (ep == EQP_SHADOW_WEAPON)
  893. return EQP_SHADOW_ARMS;
  894. }
  895. }
  896. return ep;
  897. }
  898. /**
  899. * Get equip point for an equip
  900. * @param sd
  901. * @param n Equip index in inventory
  902. */
  903. int pc_equippoint(struct map_session_data *sd,int n){
  904. nullpo_ret(sd);
  905. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  906. }
  907. /**
  908. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  909. * @param sd : player session
  910. * @return 0 sucess, 1:invalid sd
  911. */
  912. void pc_setinventorydata(struct map_session_data *sd)
  913. {
  914. uint8 i;
  915. nullpo_retv(sd);
  916. for(i = 0; i < MAX_INVENTORY; i++) {
  917. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  918. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  919. }
  920. }
  921. /**
  922. * 'Calculates' weapon type
  923. * @param sd : Player
  924. */
  925. void pc_calcweapontype(struct map_session_data *sd)
  926. {
  927. nullpo_retv(sd);
  928. // single-hand
  929. if(sd->weapontype2 == W_FIST) {
  930. sd->status.weapon = sd->weapontype1;
  931. return;
  932. }
  933. if(sd->weapontype1 == W_FIST) {
  934. sd->status.weapon = sd->weapontype2;
  935. return;
  936. }
  937. // dual-wield
  938. sd->status.weapon = W_FIST;
  939. switch (sd->weapontype1){
  940. case W_DAGGER:
  941. switch (sd->weapontype2) {
  942. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  943. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  944. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  945. }
  946. break;
  947. case W_1HSWORD:
  948. switch (sd->weapontype2) {
  949. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  950. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  951. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  952. }
  953. break;
  954. case W_1HAXE:
  955. switch (sd->weapontype2) {
  956. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  957. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  958. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  959. }
  960. }
  961. // unknown, default to right hand type
  962. if (!sd->status.weapon)
  963. sd->status.weapon = sd->weapontype1;
  964. }
  965. /**
  966. * Set equip index
  967. * @param sd : Player
  968. */
  969. void pc_setequipindex(struct map_session_data *sd)
  970. {
  971. uint16 i;
  972. nullpo_retv(sd);
  973. for (i = 0; i < EQI_MAX; i++){
  974. sd->equip_index[i] = -1;
  975. sd->equip_switch_index[i] = -1;
  976. }
  977. for (i = 0; i < MAX_INVENTORY; i++) {
  978. if (sd->inventory.u.items_inventory[i].nameid == 0)
  979. continue;
  980. if (sd->inventory.u.items_inventory[i].equip) {
  981. uint8 j;
  982. for (j = 0; j < EQI_MAX; j++)
  983. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  984. sd->equip_index[j] = i;
  985. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  986. if (sd->inventory_data[i])
  987. sd->weapontype1 = sd->inventory_data[i]->subtype;
  988. else
  989. sd->weapontype1 = W_FIST;
  990. }
  991. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  992. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  993. sd->weapontype2 = sd->inventory_data[i]->subtype;
  994. else
  995. sd->weapontype2 = W_FIST;
  996. }
  997. }
  998. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  999. for (uint8 j = 0; j < EQI_MAX; j++) {
  1000. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1001. sd->equip_switch_index[j] = i;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. pc_calcweapontype(sd);
  1007. }
  1008. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  1009. //{
  1010. // int i;
  1011. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1012. // struct item_data *data;
  1013. //
  1014. // //Crafted/made/hatched items.
  1015. // if (itemdb_isspecial(item->card[0]))
  1016. // return 1;
  1017. //
  1018. // /* scan for enchant armor gems */
  1019. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1020. // s = MAX_SLOTS - 1;
  1021. //
  1022. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  1023. // return( i < s ) ? 0 : 1;
  1024. //}
  1025. /**
  1026. * Check if an item is equiped by player
  1027. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1028. * @param sd : player session
  1029. * @param nameid : itemid
  1030. * @return 1:yes, 0:no
  1031. */
  1032. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  1033. {
  1034. uint8 i;
  1035. for( i = 0; i < EQI_MAX; i++ )
  1036. {
  1037. short index = sd->equip_index[i], j;
  1038. if( index < 0 )
  1039. continue;
  1040. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1041. continue;
  1042. if( !sd->inventory_data[index] )
  1043. continue;
  1044. if( sd->inventory_data[index]->nameid == nameid )
  1045. return true;
  1046. for( j = 0; j < sd->inventory_data[index]->slots; j++ ){
  1047. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1048. return true;
  1049. }
  1050. }
  1051. return false;
  1052. }
  1053. /**
  1054. * Check adoption rules
  1055. * @param p1_sd: Player 1
  1056. * @param p2_sd: Player 2
  1057. * @param b_sd: Player that will be adopted
  1058. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1059. * ADOPT_ALREADY_ADOPTED - Already adopted
  1060. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1061. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1062. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1063. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1064. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1065. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1066. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1067. */
  1068. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1069. {
  1070. if( !p1_sd || !p2_sd || !b_sd )
  1071. return ADOPT_CHARACTER_NOT_FOUND;
  1072. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1073. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1074. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1075. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1076. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1077. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1078. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1079. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1080. // Parents need to have their ring equipped
  1081. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1082. return ADOPT_EQUIP_RINGS;
  1083. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1084. return ADOPT_EQUIP_RINGS;
  1085. // Already adopted a baby
  1086. if( p1_sd->status.child || p2_sd->status.child ) {
  1087. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1088. return ADOPT_MORE_CHILDREN;
  1089. }
  1090. // Parents need at least lvl 70 to adopt
  1091. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1092. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1093. return ADOPT_LEVEL_70;
  1094. }
  1095. if( b_sd->status.partner_id ) {
  1096. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1097. return ADOPT_MARRIED;
  1098. }
  1099. 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 ) )
  1100. return ADOPT_NOT_NOVICE;
  1101. return ADOPT_ALLOWED;
  1102. }
  1103. /*==========================================
  1104. * Adoption Process
  1105. *------------------------------------------*/
  1106. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1107. {
  1108. int job, joblevel;
  1109. t_exp jobexp;
  1110. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1111. return false;
  1112. // Preserve current job levels and progress
  1113. joblevel = b_sd->status.job_level;
  1114. jobexp = b_sd->status.job_exp;
  1115. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1116. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1117. { // Success, proceed to configure parents and baby skills
  1118. p1_sd->status.child = b_sd->status.char_id;
  1119. p2_sd->status.child = b_sd->status.char_id;
  1120. b_sd->status.father = p1_sd->status.char_id;
  1121. b_sd->status.mother = p2_sd->status.char_id;
  1122. // Restore progress
  1123. b_sd->status.job_level = joblevel;
  1124. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1125. b_sd->status.job_exp = jobexp;
  1126. clif_updatestatus(b_sd, SP_JOBEXP);
  1127. // Baby Skills
  1128. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1129. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1130. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1131. // Parents Skills
  1132. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1133. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1134. chrif_save(p1_sd, CSAVE_NORMAL);
  1135. chrif_save(p2_sd, CSAVE_NORMAL);
  1136. chrif_save(b_sd, CSAVE_NORMAL);
  1137. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1138. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1139. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1140. return true;
  1141. }
  1142. return false; // Job Change Fail
  1143. }
  1144. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1145. nullpo_retr( false, sd );
  1146. nullpo_retr( false, item );
  1147. // Calculate the required bit to check
  1148. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1149. size_t index;
  1150. // 2-1
  1151. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1152. index = 1;
  1153. // 2-2
  1154. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1155. index = 2;
  1156. // Basejob
  1157. }else{
  1158. index = 0;
  1159. }
  1160. return ( item->class_base[index] & job ) != 0;
  1161. }
  1162. /*==========================================
  1163. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1164. Returns:
  1165. false : Cannot use/equip
  1166. true : Can use/equip
  1167. * Credits:
  1168. [Inkfish] for first idea
  1169. [Haru] for third-classes extension
  1170. [Cydh] finishing :D
  1171. *------------------------------------------*/
  1172. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1173. while (1) {
  1174. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1175. break;
  1176. #ifndef RENEWAL
  1177. //allow third classes to use trans. class items
  1178. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1179. break;
  1180. //third-baby classes can use same item too
  1181. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1182. break;
  1183. //don't need to decide specific rules for third-classes?
  1184. //items for third classes can be used for all third classes
  1185. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1186. break;
  1187. #else
  1188. //trans. classes (exl. third-trans.)
  1189. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1190. break;
  1191. //baby classes (exl. third-baby)
  1192. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1193. break;
  1194. //third classes (exl. third-trans. and baby-third)
  1195. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1196. break;
  1197. //trans-third classes
  1198. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1199. break;
  1200. //third-baby classes
  1201. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1202. break;
  1203. #endif
  1204. return false;
  1205. }
  1206. return true;
  1207. }
  1208. /*=================================================
  1209. * Checks if the player can equip the item at index n in inventory.
  1210. * @param sd
  1211. * @param n Item index in inventory
  1212. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1213. *------------------------------------------------*/
  1214. uint8 pc_isequip(struct map_session_data *sd,int n)
  1215. {
  1216. struct item_data *item;
  1217. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1218. item = sd->inventory_data[n];
  1219. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1220. return ITEM_EQUIP_ACK_OK;
  1221. if(item == NULL)
  1222. return ITEM_EQUIP_ACK_FAIL;
  1223. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1224. return ITEM_EQUIP_ACK_FAILLEVEL;
  1225. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1226. return ITEM_EQUIP_ACK_FAILLEVEL;
  1227. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1228. return ITEM_EQUIP_ACK_FAIL;
  1229. //fail to equip if item is restricted
  1230. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1231. return ITEM_EQUIP_ACK_FAIL;
  1232. if (item->equip&EQP_AMMO) {
  1233. switch (item->subtype) {
  1234. case AMMO_ARROW:
  1235. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1236. clif_msg(sd, ITEM_NEED_BOW);
  1237. return ITEM_EQUIP_ACK_FAIL;
  1238. }
  1239. break;
  1240. case AMMO_DAGGER:
  1241. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1242. return ITEM_EQUIP_ACK_FAIL;
  1243. break;
  1244. case AMMO_BULLET:
  1245. case AMMO_SHELL:
  1246. 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
  1247. #ifdef RENEWAL
  1248. && sd->status.weapon != W_GRENADE
  1249. #endif
  1250. ) {
  1251. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1252. return ITEM_EQUIP_ACK_FAIL;
  1253. }
  1254. break;
  1255. #ifndef RENEWAL
  1256. case AMMO_GRENADE:
  1257. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1258. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1259. return ITEM_EQUIP_ACK_FAIL;
  1260. }
  1261. break;
  1262. #endif
  1263. case AMMO_CANNONBALL:
  1264. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1265. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1266. return ITEM_EQUIP_ACK_FAIL;
  1267. }
  1268. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1269. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1270. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1271. return ITEM_EQUIP_ACK_FAIL;
  1272. }
  1273. break;
  1274. }
  1275. }
  1276. if (sd->sc.count) {
  1277. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1278. return ITEM_EQUIP_ACK_FAIL;
  1279. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1280. return ITEM_EQUIP_ACK_FAIL;
  1281. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1282. return ITEM_EQUIP_ACK_FAIL;
  1283. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1284. return ITEM_EQUIP_ACK_FAIL;
  1285. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1286. return ITEM_EQUIP_ACK_FAIL;
  1287. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1288. return ITEM_EQUIP_ACK_FAIL;
  1289. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1290. return ITEM_EQUIP_ACK_FAIL;
  1291. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1292. //Spirit of Super Novice equip bonuses. [Skotlex]
  1293. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1294. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1295. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1296. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1297. case W_DAGGER: //All level 4 - Daggers
  1298. case W_1HSWORD: //All level 4 - 1H Swords
  1299. case W_1HAXE: //All level 4 - 1H Axes
  1300. case W_MACE: //All level 4 - 1H Maces
  1301. case W_STAFF: //All level 4 - 1H Staves
  1302. case W_2HSTAFF: //All level 4 - 2H Staves
  1303. return ITEM_EQUIP_ACK_OK;
  1304. }
  1305. }
  1306. }
  1307. //Not equipable by class. [Skotlex]
  1308. if (!pc_job_can_use_item(sd,item))
  1309. return ITEM_EQUIP_ACK_FAIL;
  1310. if (!pc_isItemClass(sd, item))
  1311. return ITEM_EQUIP_ACK_FAIL;
  1312. return ITEM_EQUIP_ACK_OK;
  1313. }
  1314. /*==========================================
  1315. * No problem with the session id
  1316. * set the status that has been sent from char server
  1317. *------------------------------------------*/
  1318. 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)
  1319. {
  1320. int i;
  1321. t_tick tick = gettick();
  1322. uint32 ip = session[sd->fd]->client_addr;
  1323. sd->login_id2 = login_id2;
  1324. sd->group_id = group_id;
  1325. /* load user permissions */
  1326. pc_group_pc_load(sd);
  1327. memcpy(&sd->status, st, sizeof(*st));
  1328. if (st->sex != sd->status.sex) {
  1329. clif_authfail_fd(sd->fd, 0);
  1330. return false;
  1331. }
  1332. //Set the map-server used job id. [Skotlex]
  1333. i = pc_jobid2mapid(sd->status.class_);
  1334. if (i == -1) { //Invalid class?
  1335. 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);
  1336. sd->status.class_ = JOB_NOVICE;
  1337. sd->class_ = MAPID_NOVICE;
  1338. } else
  1339. sd->class_ = i;
  1340. // Checks and fixes to character status data, that are required
  1341. // in case of configuration change or stuff, which cannot be
  1342. // checked on char-server.
  1343. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1344. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1345. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1346. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1347. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1348. sd->state.connect_new = 1;
  1349. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1350. sd->invincible_timer = INVALID_TIMER;
  1351. sd->npc_timer_id = INVALID_TIMER;
  1352. sd->pvp_timer = INVALID_TIMER;
  1353. sd->expiration_tid = INVALID_TIMER;
  1354. sd->autotrade_tid = INVALID_TIMER;
  1355. sd->respawn_tid = INVALID_TIMER;
  1356. sd->tid_queue_active = INVALID_TIMER;
  1357. sd->skill_keep_using.tid = INVALID_TIMER;
  1358. sd->skill_keep_using.skill_id = 0;
  1359. sd->skill_keep_using.level = 0;
  1360. sd->skill_keep_using.target = 0;
  1361. #ifdef SECURE_NPCTIMEOUT
  1362. // Initialize to defaults/expected
  1363. sd->npc_idle_timer = INVALID_TIMER;
  1364. sd->npc_idle_tick = tick;
  1365. sd->npc_idle_type = NPCT_INPUT;
  1366. sd->state.ignoretimeout = false;
  1367. #endif
  1368. sd->canuseitem_tick = tick;
  1369. sd->canusecashfood_tick = tick;
  1370. sd->canequip_tick = tick;
  1371. sd->cantalk_tick = tick;
  1372. sd->canskill_tick = tick;
  1373. sd->cansendmail_tick = tick;
  1374. sd->idletime = last_tick;
  1375. for(i = 0; i < MAX_SPIRITBALL; i++)
  1376. sd->spirit_timer[i] = INVALID_TIMER;
  1377. if (battle_config.item_auto_get)
  1378. sd->state.autoloot = 10000;
  1379. if (battle_config.disp_experience)
  1380. sd->state.showexp = 1;
  1381. if (battle_config.disp_zeny)
  1382. sd->state.showzeny = 1;
  1383. #ifdef VIP_ENABLE
  1384. if (!battle_config.vip_disp_rate)
  1385. sd->vip.disableshowrate = 1;
  1386. #endif
  1387. if (!(battle_config.display_skill_fail&2))
  1388. sd->state.showdelay = 1;
  1389. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1390. memset(&sd->cart, 0, sizeof(struct s_storage));
  1391. memset(&sd->storage, 0, sizeof(struct s_storage));
  1392. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1393. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1394. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1395. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1396. sd->status.option &= ~OPTION_INVISIBLE;
  1397. }
  1398. status_change_init(&sd->bl);
  1399. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1400. unit_dataset(&sd->bl);
  1401. sd->guild_x = -1;
  1402. sd->guild_y = -1;
  1403. sd->delayed_damage = 0;
  1404. // Event Timers
  1405. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1406. sd->eventtimer[i] = INVALID_TIMER;
  1407. // Rental Timer
  1408. sd->rental_timer = INVALID_TIMER;
  1409. for( i = 0; i < 3; i++ )
  1410. sd->hate_mob[i] = -1;
  1411. sd->quest_log = NULL;
  1412. sd->num_quests = 0;
  1413. sd->avail_quests = 0;
  1414. sd->save_quest = false;
  1415. sd->count_rewarp = 0;
  1416. sd->mail.pending_weight = 0;
  1417. sd->mail.pending_zeny = 0;
  1418. sd->mail.pending_slots = 0;
  1419. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1420. sd->regs.arrays = NULL;
  1421. sd->vars_dirty = false;
  1422. sd->vars_ok = false;
  1423. sd->vars_received = 0x0;
  1424. //warp player
  1425. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1426. ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);
  1427. // try warping to a default map instead (church graveyard)
  1428. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1429. // if we fail again
  1430. clif_authfail_fd(sd->fd, 0);
  1431. return false;
  1432. }
  1433. }
  1434. clif_authok(sd);
  1435. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1436. sd->die_counter=-1;
  1437. //display login notice
  1438. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1439. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1440. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1441. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1442. sd->status.name, sd->status.account_id, sd->status.char_id,
  1443. CONVIP(ip), sd->group_id);
  1444. // Send friends list
  1445. clif_friendslist_send(sd);
  1446. if( !changing_mapservers ) {
  1447. if (battle_config.display_version == 1)
  1448. pc_show_version(sd);
  1449. // Message of the Day [Valaris]
  1450. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1451. if (battle_config.motd_type)
  1452. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1453. else
  1454. clif_displaymessage(sd->fd, motd_text[i]);
  1455. }
  1456. if (expiration_time != 0)
  1457. sd->expiration_time = expiration_time;
  1458. /**
  1459. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1460. **/
  1461. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1462. }
  1463. pc_validate_skill(sd);
  1464. /* [Ind] */
  1465. sd->sc_display = NULL;
  1466. sd->sc_display_count = 0;
  1467. // Player has not yet received the CashShop list
  1468. sd->status.cashshop_sent = false;
  1469. sd->last_addeditem_index = -1;
  1470. sd->bonus_script.head = NULL;
  1471. sd->bonus_script.count = 0;
  1472. // Initialize BG queue
  1473. sd->bg_queue_id = 0;
  1474. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1475. sd->hatEffects = {};
  1476. #endif
  1477. sd->catch_target_class = PET_CATCH_FAIL;
  1478. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1479. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1480. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1481. clif_updatestatus(sd, SP_BASEEXP);
  1482. }
  1483. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1484. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1485. clif_updatestatus(sd, SP_JOBEXP);
  1486. }
  1487. // Request all registries (auth is considered completed whence they arrive)
  1488. intif_request_registry(sd,7);
  1489. return true;
  1490. }
  1491. /*==========================================
  1492. * Closes a connection because it failed to be authenticated from the char server.
  1493. *------------------------------------------*/
  1494. void pc_authfail(struct map_session_data *sd)
  1495. {
  1496. clif_authfail_fd(sd->fd, 0);
  1497. return;
  1498. }
  1499. /**
  1500. * Player register a bl as hatred
  1501. * @param sd : player session
  1502. * @param pos : hate position [0;2]
  1503. * @param bl : target bl
  1504. * @return false:failed, true:success
  1505. */
  1506. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1507. {
  1508. int class_;
  1509. if (!sd || !bl || pos < 0 || pos > 2)
  1510. return false;
  1511. if (sd->hate_mob[pos] != -1)
  1512. { //Can't change hate targets.
  1513. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1514. return false;
  1515. }
  1516. class_ = status_get_class(bl);
  1517. if (!pcdb_checkid(class_)) {
  1518. unsigned int max_hp = status_get_max_hp(bl);
  1519. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1520. return false;
  1521. if (pos != status_get_size(bl))
  1522. return false; //Wrong size
  1523. }
  1524. sd->hate_mob[pos] = class_;
  1525. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1526. clif_hate_info(sd, pos, class_, 1);
  1527. return true;
  1528. }
  1529. /*==========================================
  1530. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1531. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1532. * See intif_parse_StorageReceived() for item operations [lighta]
  1533. *------------------------------------------*/
  1534. void pc_reg_received(struct map_session_data *sd)
  1535. {
  1536. uint8 i;
  1537. sd->vars_ok = true;
  1538. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1539. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1540. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1541. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1542. if (msg_checklangtype(sd->langtype,true) < 0)
  1543. sd->langtype = 0; //invalid langtype reset to default
  1544. // Cash shop
  1545. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1546. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1547. // Cooking Exp
  1548. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1549. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1550. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1551. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1552. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1553. }
  1554. if (battle_config.feature_banking)
  1555. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1556. if (battle_config.feature_roulette) {
  1557. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1558. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1559. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1560. }
  1561. sd->roulette.prizeIdx = -1;
  1562. //SG map and mob read [Komurka]
  1563. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1564. uint16 j;
  1565. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1566. sd->feel_map[i].index = j;
  1567. sd->feel_map[i].m = map_mapindex2mapid(j);
  1568. } else {
  1569. sd->feel_map[i].index = 0;
  1570. sd->feel_map[i].m = -1;
  1571. }
  1572. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1573. }
  1574. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1575. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1576. sd->cloneskill_idx = skill_get_index(skid);
  1577. if (sd->cloneskill_idx > 0) {
  1578. sd->status.skill[sd->cloneskill_idx].id = skid;
  1579. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1580. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1581. sd->status.skill[sd->cloneskill_idx].lv = i;
  1582. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1583. }
  1584. }
  1585. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1586. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1587. sd->reproduceskill_idx = skill_get_index(skid);
  1588. if (sd->reproduceskill_idx > 0) {
  1589. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1590. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1591. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1592. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1593. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1594. }
  1595. }
  1596. //Weird... maybe registries were reloaded?
  1597. if (sd->state.active)
  1598. return;
  1599. sd->state.active = 1;
  1600. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1601. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1602. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1603. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1604. // Restore IM_CHAR instance to the player
  1605. for (const auto &instance : instances) {
  1606. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1607. sd->instance_id = instance.first;
  1608. break;
  1609. }
  1610. }
  1611. #if PACKETVER_MAIN_NUM < 20190403 || PACKETVER_RE_NUM < 20190320 || PACKETVER_ZERO_NUM < 20190410
  1612. if (sd->instance_id > 0)
  1613. instance_reqinfo(sd, sd->instance_id);
  1614. if (sd->status.party_id > 0)
  1615. party_member_joined(sd);
  1616. if (sd->status.guild_id > 0)
  1617. guild_member_joined(sd);
  1618. if (sd->status.clan_id > 0)
  1619. clan_member_joined(sd);
  1620. #endif
  1621. // pet
  1622. if (sd->status.pet_id > 0)
  1623. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1624. // Homunculus [albator]
  1625. if( sd->status.hom_id > 0 )
  1626. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1627. if( sd->status.mer_id > 0 )
  1628. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1629. if( sd->status.ele_id > 0 )
  1630. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1631. map_addiddb(&sd->bl);
  1632. map_delnickdb(sd->status.char_id, sd->status.name);
  1633. if (!chrif_auth_finished(sd))
  1634. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1635. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1636. chrif_bsdata_request(sd->status.char_id);
  1637. #ifdef VIP_ENABLE
  1638. sd->vip.time = 0;
  1639. sd->vip.enabled = 0;
  1640. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1641. #endif
  1642. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1643. intif_request_questlog(sd);
  1644. if (battle_config.feature_achievement) {
  1645. sd->achievement_data.total_score = 0;
  1646. sd->achievement_data.level = 0;
  1647. sd->achievement_data.save = false;
  1648. sd->achievement_data.count = 0;
  1649. sd->achievement_data.incompleteCount = 0;
  1650. sd->achievement_data.achievements = NULL;
  1651. intif_request_achievements(sd->status.char_id);
  1652. }
  1653. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1654. sd->state.connect_new = 1;
  1655. clif_parse_LoadEndAck(sd->fd, sd);
  1656. if( pc_isinvisible( sd ) ){
  1657. sd->vd.class_ = JT_INVISIBLE;
  1658. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1659. // decrement the number of pvp players on the map
  1660. map_getmapdata( sd->bl.m )->users_pvp--;
  1661. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1662. // unregister the player for ranking
  1663. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1664. sd->pvp_timer = INVALID_TIMER;
  1665. }
  1666. clif_changeoption( &sd->bl );
  1667. }
  1668. }
  1669. channel_autojoin(sd);
  1670. }
  1671. static int pc_calc_skillpoint(struct map_session_data* sd)
  1672. {
  1673. uint16 i, skill_point = 0;
  1674. nullpo_ret(sd);
  1675. for(i = 1; i < MAX_SKILL; i++) {
  1676. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1677. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1678. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1679. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1680. )
  1681. {
  1682. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1683. skill_point += sd->status.skill[i].lv;
  1684. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1685. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1686. }
  1687. }
  1688. }
  1689. return skill_point;
  1690. }
  1691. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1692. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1693. return false;
  1694. /**
  1695. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1696. * skill tree and since they have no icons they'll give resource errors
  1697. * Get ALL skills except npc/guild ones. [Skotlex]
  1698. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1699. **/
  1700. for (const auto &skill : skill_db) {
  1701. uint16 skill_id = skill.second->nameid;
  1702. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1703. continue;
  1704. switch (skill_id) {
  1705. case SM_SELFPROVOKE:
  1706. case AB_DUPLELIGHT_MELEE:
  1707. case AB_DUPLELIGHT_MAGIC:
  1708. case WL_CHAINLIGHTNING_ATK:
  1709. case WL_TETRAVORTEX_FIRE:
  1710. case WL_TETRAVORTEX_WATER:
  1711. case WL_TETRAVORTEX_WIND:
  1712. case WL_TETRAVORTEX_GROUND:
  1713. case WL_SUMMON_ATK_FIRE:
  1714. case WL_SUMMON_ATK_WIND:
  1715. case WL_SUMMON_ATK_WATER:
  1716. case WL_SUMMON_ATK_GROUND:
  1717. case LG_OVERBRAND_BRANDISH:
  1718. case LG_OVERBRAND_PLUSATK:
  1719. case WM_SEVERE_RAINSTORM_MELEE:
  1720. case RL_R_TRIP_PLUSATK:
  1721. case SG_DEVIL:
  1722. case MO_TRIPLEATTACK:
  1723. case RG_SNATCHER:
  1724. continue;
  1725. default:
  1726. {
  1727. uint8 lv = (uint8)skill_get_max(skill_id);
  1728. if (lv > 0) {
  1729. uint16 idx = skill_get_index(skill_id);
  1730. sd->status.skill[idx].id = skill_id;
  1731. if (addlv)
  1732. sd->status.skill[idx].lv = lv;
  1733. }
  1734. }
  1735. break;
  1736. }
  1737. }
  1738. return true;
  1739. }
  1740. /*==========================================
  1741. * Calculation of skill level.
  1742. * @param sd
  1743. *------------------------------------------*/
  1744. void pc_calc_skilltree(struct map_session_data *sd)
  1745. {
  1746. nullpo_retv(sd);
  1747. int job = pc_calc_skilltree_normalize_job(sd);
  1748. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1749. if( class_ == -1 )
  1750. { //Unable to normalize job??
  1751. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", job, sd->status.name, sd->status.account_id, sd->status.char_id);
  1752. return;
  1753. }
  1754. class_ = pc_class2idx(class_);
  1755. for (const auto &skill : skill_db) {
  1756. uint16 skill_id = skill.second->nameid;
  1757. uint16 idx = skill_get_index(skill_id);
  1758. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1759. sd->status.skill[idx].id = 0; //First clear skills.
  1760. /* permanent skills that must be re-checked */
  1761. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1762. if (skill_id == 0) {
  1763. sd->status.skill[idx].id = 0;
  1764. sd->status.skill[idx].lv = 0;
  1765. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1766. continue;
  1767. }
  1768. switch (skill_id) {
  1769. case NV_TRICKDEAD:
  1770. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1771. sd->status.skill[idx].id = 0;
  1772. sd->status.skill[idx].lv = 0;
  1773. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1774. }
  1775. break;
  1776. }
  1777. }
  1778. }
  1779. for (const auto &skill : skill_db) {
  1780. uint16 idx = skill_get_index(skill.second->nameid);
  1781. // Restore original level of skills after deleting earned skills.
  1782. 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 ) {
  1783. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1784. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1785. }
  1786. }
  1787. // Removes Taekwon Ranker skill bonus
  1788. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1789. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1790. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1791. uint16 sk_id = skill_tree[c_][i].skill_id;
  1792. uint16 sk_idx = 0;
  1793. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1794. continue;
  1795. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1796. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1797. continue;
  1798. sd->status.skill[sk_idx].id = 0;
  1799. }
  1800. }
  1801. }
  1802. // Grant all skills
  1803. pc_grant_allskills(sd, false);
  1804. int flag;
  1805. do {
  1806. uint16 skid = 0;
  1807. flag = 0;
  1808. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1809. bool fail = false;
  1810. uint16 sk_idx = skill_get_index(skid);
  1811. if (sd->status.skill[sk_idx].id)
  1812. continue; //Skill already known.
  1813. if (!battle_config.skillfree) {
  1814. // Checking required skills
  1815. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1816. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1817. uint16 sk_need_idx = 0;
  1818. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1819. short sk_need = sk_need_id;
  1820. 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)
  1821. sk_need = 0; //Not learned.
  1822. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1823. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1824. else
  1825. sk_need = pc_checkskill(sd,sk_need_id);
  1826. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1827. fail = true;
  1828. break;
  1829. }
  1830. }
  1831. }
  1832. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1833. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1834. c_ = pc_class2idx(c_);
  1835. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1836. fail = true; // base level requirement wasn't satisfied
  1837. }
  1838. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1839. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1840. c_ = pc_class2idx(c_);
  1841. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1842. fail = true; // job level requirement wasn't satisfied
  1843. }
  1844. }
  1845. if (!fail) {
  1846. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1847. if (!sd->status.skill[sk_idx].lv && (
  1848. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1849. skill->inf2[INF2_ISWEDDING] ||
  1850. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1851. ))
  1852. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1853. sd->status.skill[sk_idx].id = skid;
  1854. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1855. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1856. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1857. }
  1858. flag = 1; // skill list has changed, perform another pass
  1859. }
  1860. }
  1861. } while(flag);
  1862. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1863. uint16 skid = 0;
  1864. /* Taekwon Ranker Bonus Skill Tree
  1865. ============================================
  1866. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1867. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1868. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1869. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1870. uint16 sk_idx = skill_get_index(skid);
  1871. if (sk_idx == 0)
  1872. continue;
  1873. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1874. continue; //Do not include Quest/Wedding skills.
  1875. if( sd->status.skill[sk_idx].id == 0 ) {
  1876. sd->status.skill[sk_idx].id = skid;
  1877. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1878. } else if( skid != NV_BASIC )
  1879. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1880. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1881. }
  1882. }
  1883. // Enable Bard/Dancer spirit linked skills.
  1884. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  1885. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  1886. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  1887. { BA_POEMBRAGI, DC_FORTUNEKISS },
  1888. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  1889. for (const auto &skill : linked_skills) {
  1890. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  1891. continue;
  1892. // Tag it as a non-savable, non-uppable, bonus skill
  1893. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  1894. }
  1895. }
  1896. }
  1897. //Checks if you can learn a new skill after having leveled up a skill.
  1898. static void pc_check_skilltree(struct map_session_data *sd)
  1899. {
  1900. int i, flag = 0;
  1901. int c = 0;
  1902. if (battle_config.skillfree)
  1903. return; //Function serves no purpose if this is set
  1904. i = pc_calc_skilltree_normalize_job(sd);
  1905. c = pc_mapid2jobid(i, sd->status.sex);
  1906. if (c == -1) { //Unable to normalize job??
  1907. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1908. return;
  1909. }
  1910. c = pc_class2idx(c);
  1911. do {
  1912. uint16 skid = 0;
  1913. flag = 0;
  1914. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1915. uint16 sk_idx = skill_get_index(skid);
  1916. bool fail = false;
  1917. uint8 j = 0;
  1918. if (sd->status.skill[sk_idx].id) //Already learned
  1919. continue;
  1920. // Checking required skills
  1921. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1922. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1923. uint16 sk_need_idx = 0;
  1924. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1925. short sk_need = sk_need_id;
  1926. 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)
  1927. sk_need = 0; //Not learned.
  1928. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1929. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1930. else
  1931. sk_need = pc_checkskill(sd,sk_need_id);
  1932. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1933. fail = true;
  1934. break;
  1935. }
  1936. }
  1937. }
  1938. if( fail )
  1939. continue;
  1940. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1941. continue;
  1942. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1943. if( !sd->status.skill[sk_idx].lv && (
  1944. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1945. skill->inf2[INF2_ISWEDDING] ||
  1946. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1947. ) )
  1948. continue; //Cannot be learned via normal means.
  1949. sd->status.skill[sk_idx].id = skid;
  1950. flag = 1;
  1951. }
  1952. } while(flag);
  1953. }
  1954. // Make sure all the skills are in the correct condition
  1955. // before persisting to the backend.. [MouseJstr]
  1956. void pc_clean_skilltree(struct map_session_data *sd)
  1957. {
  1958. uint16 i;
  1959. for (i = 0; i < MAX_SKILL; i++){
  1960. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1961. sd->status.skill[i].id = 0;
  1962. sd->status.skill[i].lv = 0;
  1963. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1964. }
  1965. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1966. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1967. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1968. }
  1969. }
  1970. }
  1971. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1972. {
  1973. int skill_point, novice_skills;
  1974. int c = sd->class_;
  1975. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1976. return c;
  1977. skill_point = pc_calc_skillpoint(sd);
  1978. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1979. // limit 1st class and above to novice job levels
  1980. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1981. {
  1982. c = MAPID_NOVICE;
  1983. }
  1984. // limit 2nd class and above to first class job levels (super novices are exempt)
  1985. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1986. {
  1987. // regenerate change_level_2nd
  1988. if (!sd->change_level_2nd)
  1989. {
  1990. if (sd->class_&JOBL_THIRD)
  1991. {
  1992. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1993. if (!sd->change_level_3rd)
  1994. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1995. else
  1996. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1997. - (sd->status.job_level - 1)
  1998. - (sd->change_level_3rd - 1)
  1999. - novice_skills;
  2000. }
  2001. else
  2002. {
  2003. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  2004. - (sd->status.job_level - 1)
  2005. - novice_skills;
  2006. }
  2007. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  2008. }
  2009. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  2010. {
  2011. c &= MAPID_BASEMASK;
  2012. }
  2013. // limit 3rd class to 2nd class/trans job levels
  2014. else if(sd->class_&JOBL_THIRD)
  2015. {
  2016. // regenerate change_level_3rd
  2017. if (!sd->change_level_3rd)
  2018. {
  2019. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  2020. - (sd->status.job_level - 1)
  2021. - (sd->change_level_2nd - 1)
  2022. - novice_skills;
  2023. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  2024. }
  2025. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  2026. c &= MAPID_UPPERMASK;
  2027. }
  2028. }
  2029. // restore non-limiting flags
  2030. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  2031. return c;
  2032. }
  2033. /*==========================================
  2034. * Updates the weight status
  2035. *------------------------------------------
  2036. * 1: overweight 50% for pre-renewal and 70% for renewal
  2037. * 2: overweight 90%
  2038. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2039. */
  2040. void pc_updateweightstatus(struct map_session_data *sd)
  2041. {
  2042. int old_overweight;
  2043. int new_overweight;
  2044. nullpo_retv(sd);
  2045. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  2046. #ifdef RENEWAL
  2047. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2048. #else
  2049. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2050. #endif
  2051. if( old_overweight == new_overweight )
  2052. return; // no change
  2053. // stop old status change
  2054. if( old_overweight == 1 )
  2055. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  2056. else if( old_overweight == 2 )
  2057. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  2058. // start new status change
  2059. if( new_overweight == 1 )
  2060. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2061. else if( new_overweight == 2 )
  2062. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2063. // update overweight status
  2064. sd->regen.state.overweight = new_overweight;
  2065. }
  2066. int pc_disguise(struct map_session_data *sd, int class_)
  2067. {
  2068. if (!class_ && !sd->disguise)
  2069. return 0;
  2070. if (class_ && sd->disguise == class_)
  2071. return 0;
  2072. if(pc_isinvisible(sd))
  2073. { //Character is invisible. Stealth class-change. [Skotlex]
  2074. sd->disguise = class_; //viewdata is set on uncloaking.
  2075. return 2;
  2076. }
  2077. if (sd->bl.prev != NULL) {
  2078. pc_stop_walking(sd, 0);
  2079. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2080. }
  2081. if (!class_) {
  2082. sd->disguise = 0;
  2083. class_ = sd->status.class_;
  2084. } else
  2085. sd->disguise=class_;
  2086. status_set_viewdata(&sd->bl, class_);
  2087. clif_changeoption(&sd->bl);
  2088. if (sd->bl.prev != NULL) {
  2089. clif_spawn(&sd->bl);
  2090. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2091. { //It seems the cart info is lost on undisguise.
  2092. clif_cartlist(sd);
  2093. clif_updatestatus(sd,SP_CARTINFO);
  2094. }
  2095. if (sd->chatID) {
  2096. struct chat_data* cd;
  2097. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2098. clif_dispchat(cd,0);
  2099. }
  2100. }
  2101. return 1;
  2102. }
  2103. /// Show error message
  2104. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2105. /// Check for valid Element, break & show error message if invalid Element
  2106. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2107. /// Check for valid Race, break & show error message if invalid Race
  2108. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2109. /// Check for valid Race2, break & show error message if invalid Race2
  2110. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2111. /// Check for valid Class, break & show error message if invalid Class
  2112. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2113. /// Check for valid Size, break & show error message if invalid Size
  2114. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2115. /// Check for valid SC, break & show error message if invalid SC
  2116. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2117. /**
  2118. * Add auto spell bonus for player while attacking/attacked
  2119. * @param spell: Spell array
  2120. * @param id: Skill to cast
  2121. * @param lv: Skill level
  2122. * @param rate: Success chance
  2123. * @param flag: Battle flag
  2124. * @param card_id: Used to prevent card stacking
  2125. */
  2126. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  2127. {
  2128. if (spell.size() == MAX_PC_BONUS) {
  2129. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2130. return;
  2131. }
  2132. if (!rate)
  2133. return;
  2134. if (!(flag&BF_RANGEMASK))
  2135. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2136. if (!(flag&BF_WEAPONMASK))
  2137. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2138. if (!(flag&BF_SKILLMASK)) {
  2139. if (flag&(BF_MAGIC | BF_MISC))
  2140. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2141. if (flag&BF_WEAPON)
  2142. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2143. }
  2144. for (auto &it : spell) {
  2145. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2146. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2147. return;
  2148. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2149. return;
  2150. }
  2151. }
  2152. struct s_autospell entry = {};
  2153. if (rate < -10000 || rate > 10000)
  2154. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2155. entry.id = id;
  2156. entry.lv = lv;
  2157. entry.rate = cap_value(rate, -10000, 10000);
  2158. entry.flag = flag;
  2159. entry.card_id = card_id;
  2160. spell.push_back(entry);
  2161. }
  2162. /**
  2163. * Add auto spell bonus for player while using skills
  2164. * @param spell: Spell array
  2165. * @param src_skill: Trigger skill
  2166. * @param id: Support or target type
  2167. * @param lv: Skill level
  2168. * @param rate: Success chance
  2169. * @param card_id: Used to prevent card stacking
  2170. */
  2171. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2172. {
  2173. if (spell.size() == MAX_PC_BONUS) {
  2174. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2175. return;
  2176. }
  2177. if (!rate)
  2178. return;
  2179. struct s_autospell entry = {};
  2180. if (rate < -10000 || rate > 10000)
  2181. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2182. entry.flag = src_skill;
  2183. entry.id = id;
  2184. entry.lv = lv;
  2185. entry.rate = cap_value(rate, -10000, 10000);
  2186. entry.card_id = card_id;
  2187. spell.push_back(entry);
  2188. }
  2189. /**
  2190. * Add inflict effect bonus for player while attacking/attacked
  2191. * @param effect: Effect array
  2192. * @param sc: SC/Effect type
  2193. * @param rate: Success chance
  2194. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2195. * @param flag: Target flag
  2196. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2197. */
  2198. 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)
  2199. {
  2200. if (effect.size() == MAX_PC_BONUS) {
  2201. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2202. return;
  2203. }
  2204. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2205. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2206. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2207. flag |= ATF_TARGET; //Default target: enemy.
  2208. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2209. flag |= ATF_WEAPON; //Default type: weapon.
  2210. if (!duration)
  2211. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2212. for (auto &it : effect) {
  2213. if (it.sc == sc && it.flag == flag) {
  2214. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2215. it.arrow_rate += arrow_rate;
  2216. it.duration = umax(it.duration, duration);
  2217. return;
  2218. }
  2219. }
  2220. struct s_addeffect entry = {};
  2221. if (rate < -10000 || rate > 10000)
  2222. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2223. entry.sc = sc;
  2224. entry.rate = cap_value(rate, -10000, 10000);
  2225. entry.arrow_rate = arrow_rate;
  2226. entry.flag = flag;
  2227. entry.duration = duration;
  2228. effect.push_back(entry);
  2229. }
  2230. /**
  2231. * Add inflict effect bonus for player while attacking using skill
  2232. * @param effect: Effect array
  2233. * @param sc: SC/Effect type
  2234. * @param rate: Success chance
  2235. * @param skill_id: Skill to cast
  2236. * @param target: Target type
  2237. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2238. */
  2239. 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)
  2240. {
  2241. if (effect.size() == MAX_PC_BONUS) {
  2242. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2243. return;
  2244. }
  2245. if (!duration)
  2246. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2247. for (auto &it : effect) {
  2248. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2249. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2250. it.duration = umax(it.duration, duration);
  2251. return;
  2252. }
  2253. }
  2254. struct s_addeffectonskill entry = {};
  2255. if (rate < -10000 || rate > 10000)
  2256. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2257. entry.sc = sc;
  2258. entry.rate = cap_value(rate, -10000, 10000);
  2259. entry.skill_id = skill_id;
  2260. entry.target = target;
  2261. entry.duration = duration;
  2262. effect.push_back(entry);
  2263. }
  2264. /**
  2265. * Adjust/add drop rate modifier for player
  2266. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2267. * @param nameid: item id that will be dropped
  2268. * @param group: group id
  2269. * @param class_: target class
  2270. * @param race: target race. if < 0, means monster_id
  2271. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2272. */
  2273. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2274. {
  2275. if (!nameid && !group) {
  2276. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2277. return;
  2278. }
  2279. if (nameid && !itemdb_exists(nameid)) {
  2280. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2281. return;
  2282. }
  2283. if (group && !itemdb_group.exists(group)) {
  2284. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2285. return;
  2286. }
  2287. if (drop.size() == MAX_PC_BONUS) {
  2288. 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);
  2289. return;
  2290. }
  2291. //Apply config rate adjustment settings.
  2292. if (rate >= 0) { //Absolute drop.
  2293. if (battle_config.item_rate_adddrop != 100)
  2294. rate = rate*battle_config.item_rate_adddrop/100;
  2295. if (rate < battle_config.item_drop_adddrop_min)
  2296. rate = battle_config.item_drop_adddrop_min;
  2297. else if (rate > battle_config.item_drop_adddrop_max)
  2298. rate = battle_config.item_drop_adddrop_max;
  2299. } else { //Relative drop, max/min limits are applied at drop time.
  2300. if (battle_config.item_rate_adddrop != 100)
  2301. rate = rate*battle_config.item_rate_adddrop/100;
  2302. if (rate > -1)
  2303. rate = -1;
  2304. }
  2305. for (auto &it : drop) {
  2306. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2307. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2308. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2309. return;
  2310. }
  2311. }
  2312. struct s_add_drop entry = {};
  2313. if (rate < -10000 || rate > 10000)
  2314. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2315. entry.nameid = nameid;
  2316. entry.group = group;
  2317. entry.race = race;
  2318. entry.class_ = class_;
  2319. entry.rate = cap_value(rate, -10000, 10000);
  2320. drop.push_back(entry);
  2321. }
  2322. s_autobonus::~s_autobonus(){
  2323. if( this->active != INVALID_TIMER ){
  2324. delete_timer( this->active, pc_endautobonus );
  2325. this->active = INVALID_TIMER;
  2326. }
  2327. if( this->bonus_script != nullptr ){
  2328. aFree( this->bonus_script );
  2329. this->bonus_script = nullptr;
  2330. }
  2331. if( this->other_script != nullptr ){
  2332. aFree( this->other_script );
  2333. this->other_script = nullptr;
  2334. }
  2335. }
  2336. /**
  2337. * Add autobonus to player when attacking/attacked
  2338. * @param bonus: Bonus array
  2339. * @param script: Script to execute
  2340. * @param rate: Success chance
  2341. * @param dur: Duration
  2342. * @param flag: Battle flag/skill
  2343. * @param other_script: Secondary script to execute
  2344. * @param pos: Item equip position
  2345. * @param onskill: Skill used to trigger autobonus
  2346. * @return True on success or false otherwise
  2347. */
  2348. 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){
  2349. // Check if the same bonus already exists
  2350. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2351. // Compare based on position and bonus script
  2352. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2353. return false;
  2354. }
  2355. }
  2356. if (bonus.size() == MAX_PC_BONUS) {
  2357. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2358. return false;
  2359. }
  2360. if (!onskill) {
  2361. if (!(flag&BF_RANGEMASK))
  2362. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2363. if (!(flag&BF_WEAPONMASK))
  2364. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2365. if (!(flag&BF_SKILLMASK)) {
  2366. if (flag&(BF_MAGIC | BF_MISC))
  2367. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2368. if (flag&BF_WEAPON)
  2369. flag |= BF_NORMAL | BF_SKILL;
  2370. }
  2371. }
  2372. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2373. if (rate < -10000 || rate > 10000)
  2374. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2375. entry->rate = cap_value(rate, -10000, 10000);
  2376. entry->duration = dur;
  2377. entry->active = INVALID_TIMER;
  2378. entry->atk_type = flag;
  2379. entry->pos = pos;
  2380. entry->bonus_script = aStrdup(script);
  2381. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2382. bonus.push_back(entry);
  2383. return true;
  2384. }
  2385. /**
  2386. * Remove an autobonus from player
  2387. * @param sd: Player data
  2388. * @param bonus: Autobonus array
  2389. * @param restore: Run script on clearing or not
  2390. */
  2391. void pc_delautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2392. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2393. while( it != bonus.end() ){
  2394. std::shared_ptr<s_autobonus> b = *it;
  2395. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2396. unsigned int equip_pos_idx = 0;
  2397. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2398. for (uint8 j = 0; j < EQI_MAX; j++) {
  2399. if (sd.equip_index[j] >= 0)
  2400. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2401. }
  2402. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2403. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2404. }else{
  2405. // Not all required items equipped anymore
  2406. restore = false;
  2407. }
  2408. }
  2409. if( restore ){
  2410. it++;
  2411. continue;
  2412. }
  2413. it = bonus.erase(it);
  2414. }
  2415. }
  2416. /**
  2417. * Execute autobonus on player
  2418. * @param sd: Player data
  2419. * @param autobonus: Autobonus to run
  2420. */
  2421. void pc_exeautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2422. {
  2423. if (autobonus->active != INVALID_TIMER)
  2424. delete_timer(autobonus->active, pc_endautobonus);
  2425. if( autobonus->other_script )
  2426. {
  2427. int j;
  2428. unsigned int equip_pos_idx = 0;
  2429. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2430. for(j = 0; j < EQI_MAX; j++) {
  2431. if(sd.equip_index[j] >= 0)
  2432. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2433. }
  2434. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2435. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2436. }
  2437. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2438. status_calc_pc(&sd,SCO_FORCE);
  2439. }
  2440. /**
  2441. * Remove autobonus timer from player
  2442. */
  2443. TIMER_FUNC(pc_endautobonus){
  2444. struct map_session_data *sd = map_id2sd(id);
  2445. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2446. nullpo_ret(sd);
  2447. nullpo_ret(bonus);
  2448. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2449. if( autobonus->active == tid ){
  2450. autobonus->active = INVALID_TIMER;
  2451. break;
  2452. }
  2453. }
  2454. status_calc_pc(sd,SCO_FORCE);
  2455. return 0;
  2456. }
  2457. /**
  2458. * Add element bonus to player when attacking
  2459. * @param sd: Player data
  2460. * @param ele: Element to adjust
  2461. * @param rate: Success chance
  2462. * @param flag: Battle flag
  2463. */
  2464. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2465. {
  2466. nullpo_retv(sd);
  2467. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2468. if (wd->addele2.size() == MAX_PC_BONUS) {
  2469. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2470. return;
  2471. }
  2472. if (!(flag&BF_RANGEMASK))
  2473. flag |= BF_SHORT | BF_LONG;
  2474. if (!(flag&BF_WEAPONMASK))
  2475. flag |= BF_WEAPON;
  2476. if (!(flag&BF_SKILLMASK)) {
  2477. if (flag&(BF_MAGIC | BF_MISC))
  2478. flag |= BF_SKILL;
  2479. if (flag&BF_WEAPON)
  2480. flag |= BF_NORMAL | BF_SKILL;
  2481. }
  2482. for (auto &it : wd->addele2) {
  2483. if (it.ele == ele && it.flag == flag) {
  2484. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2485. return;
  2486. }
  2487. }
  2488. struct s_addele2 entry = {};
  2489. if (rate < -10000 || rate > 10000)
  2490. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2491. entry.ele = ele;
  2492. entry.rate = cap_value(rate, -10000, 10000);
  2493. entry.flag = flag;
  2494. wd->addele2.push_back(entry);
  2495. }
  2496. /**
  2497. * Reduce element bonus to player when attacking
  2498. * @param sd: Player data
  2499. * @param ele: Element to adjust
  2500. * @param rate: Success chance
  2501. * @param flag: Battle flag
  2502. */
  2503. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2504. {
  2505. nullpo_retv(sd);
  2506. if (sd->subele2.size() == MAX_PC_BONUS) {
  2507. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2508. return;
  2509. }
  2510. if (!(flag&BF_RANGEMASK))
  2511. flag |= BF_SHORT | BF_LONG;
  2512. if (!(flag&BF_WEAPONMASK))
  2513. flag |= BF_WEAPON;
  2514. if (!(flag&BF_SKILLMASK)) {
  2515. if (flag&(BF_MAGIC | BF_MISC))
  2516. flag |= BF_SKILL;
  2517. if (flag&BF_WEAPON)
  2518. flag |= BF_NORMAL | BF_SKILL;
  2519. }
  2520. for (auto &it : sd->subele2) {
  2521. if (it.ele == ele && it.flag == flag) {
  2522. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2523. return;
  2524. }
  2525. }
  2526. struct s_addele2 entry = {};
  2527. if (rate < -10000 || rate > 10000)
  2528. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2529. entry.ele = ele;
  2530. entry.rate = cap_value(rate, -10000, 10000);
  2531. entry.flag = flag;
  2532. sd->subele2.push_back(entry);
  2533. }
  2534. /**
  2535. * Adjust race damage to target when attacking
  2536. * @param sd: Player data
  2537. * @param race: Race to adjust
  2538. * @param rate: Success chance
  2539. * @param flag: Battle flag
  2540. */
  2541. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2542. {
  2543. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2544. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2545. return;
  2546. }
  2547. if (!(flag&BF_RANGEMASK))
  2548. flag |= BF_SHORT | BF_LONG;
  2549. if (!(flag&BF_WEAPONMASK))
  2550. flag |= BF_WEAPON;
  2551. if (!(flag&BF_SKILLMASK)) {
  2552. if (flag&(BF_MAGIC | BF_MISC))
  2553. flag |= BF_SKILL;
  2554. if (flag&BF_WEAPON)
  2555. flag |= BF_NORMAL | BF_SKILL;
  2556. }
  2557. for (auto &it : sd->subrace3) {
  2558. if (it.race == race && it.flag == flag) {
  2559. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2560. return;
  2561. }
  2562. }
  2563. struct s_addrace2 entry = {};
  2564. if (rate < -10000 || rate > 10000)
  2565. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2566. entry.race = race;
  2567. entry.rate = cap_value(rate, -10000, 10000);
  2568. entry.flag = flag;
  2569. sd->subrace3.push_back(entry);
  2570. }
  2571. /**
  2572. * General item bonus for player
  2573. * @param bonus: Bonus array
  2574. * @param id: Key
  2575. * @param val: Value
  2576. * @param cap_rate: If Value is a rate value that needs to be capped
  2577. */
  2578. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2579. {
  2580. for (auto &it : bonus) {
  2581. if (it.id == id) {
  2582. if (cap_rate)
  2583. it.val = cap_value(it.val + val, -10000, 10000);
  2584. else
  2585. it.val += val;
  2586. return;
  2587. }
  2588. }
  2589. struct s_item_bonus entry = {};
  2590. if (cap_rate && (val < -10000 || val > 10000)) {
  2591. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2592. val = cap_value(val, -10000, 10000);
  2593. }
  2594. entry.id = id;
  2595. entry.val = val;
  2596. bonus.push_back(entry);
  2597. }
  2598. /**
  2599. * Remove HP/SP to player when attacking
  2600. * @param bonus: Bonus array
  2601. * @param rate: Success chance
  2602. * @param per: Percentage of HP/SP to vanish
  2603. * @param flag: Battle flag
  2604. */
  2605. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2606. if (bonus.size() == MAX_PC_BONUS) {
  2607. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2608. return;
  2609. }
  2610. if (!(flag&BF_RANGEMASK))
  2611. flag |= BF_SHORT | BF_LONG;
  2612. if (!(flag&BF_WEAPONMASK))
  2613. flag |= BF_WEAPON;
  2614. if (!(flag&BF_SKILLMASK)) {
  2615. if (flag&(BF_MAGIC | BF_MISC))
  2616. flag |= BF_SKILL;
  2617. if (flag&BF_WEAPON)
  2618. flag |= BF_NORMAL | BF_SKILL;
  2619. }
  2620. for (auto &it : bonus) {
  2621. if (it.flag == flag) {
  2622. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2623. it.per += per;
  2624. return;
  2625. }
  2626. }
  2627. struct s_vanish_bonus entry = {};
  2628. if (rate < -10000 || rate > 10000)
  2629. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2630. entry.rate = cap_value(rate, -10000, 10000);
  2631. entry.per = per;
  2632. entry.flag = flag;
  2633. bonus.push_back(entry);
  2634. }
  2635. /*==========================================
  2636. * Add a bonus(type) to player sd
  2637. * format: bonus bBonusName,val;
  2638. * @param sd
  2639. * @param type Bonus type used by bBonusName
  2640. * @param val Value that usually for rate or fixed value
  2641. *------------------------------------------*/
  2642. void pc_bonus(struct map_session_data *sd,int type,int val)
  2643. {
  2644. struct status_data *status;
  2645. int bonus;
  2646. nullpo_retv(sd);
  2647. status = &sd->base_status;
  2648. switch(type){
  2649. case SP_STR:
  2650. case SP_AGI:
  2651. case SP_VIT:
  2652. case SP_INT:
  2653. case SP_DEX:
  2654. case SP_LUK:
  2655. if(sd->state.lr_flag != 2)
  2656. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2657. break;
  2658. case SP_ATK1:
  2659. if(!sd->state.lr_flag) {
  2660. bonus = status->rhw.atk + val;
  2661. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2662. }
  2663. else if(sd->state.lr_flag == 1) {
  2664. bonus = status->lhw.atk + val;
  2665. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2666. }
  2667. break;
  2668. case SP_ATK2:
  2669. if(!sd->state.lr_flag) {
  2670. bonus = status->rhw.atk2 + val;
  2671. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2672. }
  2673. else if(sd->state.lr_flag == 1) {
  2674. bonus = status->lhw.atk2 + val;
  2675. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2676. }
  2677. break;
  2678. case SP_BASE_ATK:
  2679. if(sd->state.lr_flag != 2) {
  2680. #ifdef RENEWAL
  2681. bonus = sd->bonus.eatk + val;
  2682. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2683. #else
  2684. bonus = status->batk + val;
  2685. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2686. #endif
  2687. }
  2688. break;
  2689. case SP_DEF1:
  2690. if(sd->state.lr_flag != 2) {
  2691. bonus = status->def + val;
  2692. #ifdef RENEWAL
  2693. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2694. #else
  2695. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2696. #endif
  2697. }
  2698. break;
  2699. case SP_DEF2:
  2700. if(sd->state.lr_flag != 2) {
  2701. bonus = status->def2 + val;
  2702. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2703. }
  2704. break;
  2705. case SP_MDEF1:
  2706. if(sd->state.lr_flag != 2) {
  2707. bonus = status->mdef + val;
  2708. #ifdef RENEWAL
  2709. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2710. #else
  2711. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2712. #endif
  2713. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2714. sd->bonus.shieldmdef += bonus;
  2715. }
  2716. }
  2717. break;
  2718. case SP_MDEF2:
  2719. if(sd->state.lr_flag != 2) {
  2720. bonus = status->mdef2 + val;
  2721. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2722. }
  2723. break;
  2724. case SP_HIT:
  2725. if(sd->state.lr_flag != 2) {
  2726. bonus = status->hit + val;
  2727. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2728. } else
  2729. sd->bonus.arrow_hit+=val;
  2730. break;
  2731. case SP_FLEE1:
  2732. if(sd->state.lr_flag != 2) {
  2733. bonus = status->flee + val;
  2734. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2735. }
  2736. break;
  2737. case SP_FLEE2:
  2738. if(sd->state.lr_flag != 2) {
  2739. bonus = status->flee2 + val*10;
  2740. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2741. }
  2742. break;
  2743. case SP_CRITICAL:
  2744. if(sd->state.lr_flag != 2) {
  2745. bonus = status->cri + val*10;
  2746. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2747. } else
  2748. sd->bonus.arrow_cri += val*10;
  2749. break;
  2750. case SP_ATKELE:
  2751. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2752. switch (sd->state.lr_flag)
  2753. {
  2754. case 2:
  2755. switch (sd->status.weapon) {
  2756. case W_BOW:
  2757. case W_REVOLVER:
  2758. case W_RIFLE:
  2759. case W_GATLING:
  2760. case W_SHOTGUN:
  2761. case W_GRENADE:
  2762. //Become weapon element.
  2763. status->rhw.ele=val;
  2764. break;
  2765. default: //Become arrow element.
  2766. sd->bonus.arrow_ele=val;
  2767. break;
  2768. }
  2769. break;
  2770. case 1:
  2771. status->lhw.ele=val;
  2772. break;
  2773. default:
  2774. status->rhw.ele=val;
  2775. break;
  2776. }
  2777. break;
  2778. case SP_DEFELE:
  2779. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2780. if(sd->state.lr_flag != 2)
  2781. status->def_ele=val;
  2782. break;
  2783. case SP_MAXHP:
  2784. if(sd->state.lr_flag == 2)
  2785. break;
  2786. sd->bonus.hp += val;
  2787. break;
  2788. case SP_MAXSP:
  2789. if(sd->state.lr_flag == 2)
  2790. break;
  2791. sd->bonus.sp += val;
  2792. break;
  2793. case SP_MAXHPRATE:
  2794. if(sd->state.lr_flag != 2)
  2795. sd->hprate+=val;
  2796. break;
  2797. case SP_MAXSPRATE:
  2798. if(sd->state.lr_flag != 2)
  2799. sd->sprate+=val;
  2800. break;
  2801. case SP_SPRATE:
  2802. if(sd->state.lr_flag != 2)
  2803. sd->dsprate+=val;
  2804. break;
  2805. case SP_ATTACKRANGE:
  2806. switch (sd->state.lr_flag) {
  2807. case 2:
  2808. switch (sd->status.weapon) {
  2809. case W_BOW:
  2810. case W_REVOLVER:
  2811. case W_RIFLE:
  2812. case W_GATLING:
  2813. case W_SHOTGUN:
  2814. case W_GRENADE:
  2815. status->rhw.range += val;
  2816. }
  2817. break;
  2818. case 1:
  2819. status->lhw.range += val;
  2820. break;
  2821. default:
  2822. status->rhw.range += val;
  2823. break;
  2824. }
  2825. break;
  2826. case SP_SPEED_RATE: //Non stackable increase
  2827. if(sd->state.lr_flag != 2)
  2828. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2829. break;
  2830. case SP_SPEED_ADDRATE: //Stackable increase
  2831. if(sd->state.lr_flag != 2)
  2832. sd->bonus.speed_add_rate -= val;
  2833. break;
  2834. case SP_ASPD: //Raw increase
  2835. if(sd->state.lr_flag != 2)
  2836. sd->bonus.aspd_add -= 10*val;
  2837. break;
  2838. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2839. if(sd->state.lr_flag != 2)
  2840. #ifndef RENEWAL_ASPD
  2841. status->aspd_rate -= 10*val;
  2842. #else
  2843. status->aspd_rate2 += val;
  2844. #endif
  2845. break;
  2846. case SP_HP_RECOV_RATE:
  2847. if(sd->state.lr_flag != 2)
  2848. sd->hprecov_rate += val;
  2849. break;
  2850. case SP_SP_RECOV_RATE:
  2851. if(sd->state.lr_flag != 2)
  2852. sd->sprecov_rate += val;
  2853. break;
  2854. case SP_CRITICAL_DEF:
  2855. if(sd->state.lr_flag != 2)
  2856. sd->bonus.critical_def += val;
  2857. break;
  2858. case SP_NEAR_ATK_DEF:
  2859. if(sd->state.lr_flag != 2)
  2860. sd->bonus.near_attack_def_rate += val;
  2861. break;
  2862. case SP_LONG_ATK_DEF:
  2863. if(sd->state.lr_flag != 2)
  2864. sd->bonus.long_attack_def_rate += val;
  2865. break;
  2866. case SP_DOUBLE_RATE:
  2867. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2868. sd->bonus.double_rate = val;
  2869. break;
  2870. case SP_DOUBLE_ADD_RATE:
  2871. if(sd->state.lr_flag == 0)
  2872. sd->bonus.double_add_rate += val;
  2873. break;
  2874. case SP_MATK_RATE:
  2875. if(sd->state.lr_flag != 2)
  2876. sd->matk_rate += val;
  2877. break;
  2878. case SP_IGNORE_DEF_ELE:
  2879. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2880. if(!sd->state.lr_flag)
  2881. sd->right_weapon.ignore_def_ele |= 1<<val;
  2882. else if(sd->state.lr_flag == 1)
  2883. sd->left_weapon.ignore_def_ele |= 1<<val;
  2884. break;
  2885. case SP_IGNORE_DEF_RACE:
  2886. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2887. if(!sd->state.lr_flag)
  2888. sd->right_weapon.ignore_def_race |= 1<<val;
  2889. else if(sd->state.lr_flag == 1)
  2890. sd->left_weapon.ignore_def_race |= 1<<val;
  2891. break;
  2892. case SP_IGNORE_DEF_CLASS:
  2893. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2894. if(!sd->state.lr_flag)
  2895. sd->right_weapon.ignore_def_class |= 1<<val;
  2896. else if(sd->state.lr_flag == 1)
  2897. sd->left_weapon.ignore_def_class |= 1<<val;
  2898. break;
  2899. case SP_ATK_RATE:
  2900. if(sd->state.lr_flag != 2)
  2901. sd->bonus.atk_rate += val;
  2902. break;
  2903. case SP_MAGIC_ATK_DEF:
  2904. if(sd->state.lr_flag != 2)
  2905. sd->bonus.magic_def_rate += val;
  2906. break;
  2907. case SP_MISC_ATK_DEF:
  2908. if(sd->state.lr_flag != 2)
  2909. sd->bonus.misc_def_rate += val;
  2910. break;
  2911. case SP_IGNORE_MDEF_ELE:
  2912. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2913. if(sd->state.lr_flag != 2)
  2914. sd->bonus.ignore_mdef_ele |= 1<<val;
  2915. break;
  2916. case SP_IGNORE_MDEF_RACE:
  2917. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2918. if(sd->state.lr_flag != 2)
  2919. sd->bonus.ignore_mdef_race |= 1<<val;
  2920. break;
  2921. case SP_PERFECT_HIT_RATE:
  2922. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2923. sd->bonus.perfect_hit = val;
  2924. break;
  2925. case SP_PERFECT_HIT_ADD_RATE:
  2926. if(sd->state.lr_flag != 2)
  2927. sd->bonus.perfect_hit_add += val;
  2928. break;
  2929. case SP_CRITICAL_RATE:
  2930. if(sd->state.lr_flag != 2)
  2931. sd->critical_rate+=val;
  2932. break;
  2933. case SP_DEF_RATIO_ATK_ELE:
  2934. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2935. if(!sd->state.lr_flag)
  2936. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2937. else if(sd->state.lr_flag == 1)
  2938. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2939. break;
  2940. case SP_DEF_RATIO_ATK_RACE:
  2941. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2942. if(!sd->state.lr_flag)
  2943. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2944. else if(sd->state.lr_flag == 1)
  2945. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2946. break;
  2947. case SP_DEF_RATIO_ATK_CLASS:
  2948. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2949. if(!sd->state.lr_flag)
  2950. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2951. else if(sd->state.lr_flag == 1)
  2952. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2953. break;
  2954. case SP_HIT_RATE:
  2955. if(sd->state.lr_flag != 2)
  2956. sd->hit_rate += val;
  2957. break;
  2958. case SP_FLEE_RATE:
  2959. if(sd->state.lr_flag != 2)
  2960. sd->flee_rate += val;
  2961. break;
  2962. case SP_FLEE2_RATE:
  2963. if(sd->state.lr_flag != 2)
  2964. sd->flee2_rate += val;
  2965. break;
  2966. case SP_DEF_RATE:
  2967. if(sd->state.lr_flag != 2)
  2968. sd->def_rate += val;
  2969. break;
  2970. case SP_DEF2_RATE:
  2971. if(sd->state.lr_flag != 2)
  2972. sd->def2_rate += val;
  2973. break;
  2974. case SP_MDEF_RATE:
  2975. if(sd->state.lr_flag != 2)
  2976. sd->mdef_rate += val;
  2977. break;
  2978. case SP_MDEF2_RATE:
  2979. if(sd->state.lr_flag != 2)
  2980. sd->mdef2_rate += val;
  2981. break;
  2982. case SP_RESTART_FULL_RECOVER:
  2983. if(sd->state.lr_flag != 2)
  2984. sd->special_state.restart_full_recover = 1;
  2985. break;
  2986. case SP_NO_CASTCANCEL:
  2987. if(sd->state.lr_flag != 2)
  2988. sd->special_state.no_castcancel = 1;
  2989. break;
  2990. case SP_NO_CASTCANCEL2:
  2991. if(sd->state.lr_flag != 2)
  2992. sd->special_state.no_castcancel2 = 1;
  2993. break;
  2994. case SP_NO_SIZEFIX:
  2995. if(sd->state.lr_flag != 2)
  2996. sd->special_state.no_sizefix = 1;
  2997. break;
  2998. case SP_NO_MAGIC_DAMAGE:
  2999. if(sd->state.lr_flag == 2)
  3000. break;
  3001. val+= sd->special_state.no_magic_damage;
  3002. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3003. break;
  3004. case SP_NO_WEAPON_DAMAGE:
  3005. if(sd->state.lr_flag == 2)
  3006. break;
  3007. val+= sd->special_state.no_weapon_damage;
  3008. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3009. break;
  3010. case SP_NO_MISC_DAMAGE:
  3011. if(sd->state.lr_flag == 2)
  3012. break;
  3013. val+= sd->special_state.no_misc_damage;
  3014. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3015. break;
  3016. case SP_NO_GEMSTONE:
  3017. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3018. sd->special_state.no_gemstone = 1;
  3019. break;
  3020. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3021. if(sd->state.lr_flag != 2) {
  3022. sd->special_state.intravision = 1;
  3023. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3024. }
  3025. break;
  3026. case SP_NO_KNOCKBACK:
  3027. if(sd->state.lr_flag != 2)
  3028. sd->special_state.no_knockback = 1;
  3029. break;
  3030. case SP_SPLASH_RANGE:
  3031. if(sd->bonus.splash_range < val)
  3032. sd->bonus.splash_range = val;
  3033. break;
  3034. case SP_SPLASH_ADD_RANGE:
  3035. sd->bonus.splash_add_range += val;
  3036. break;
  3037. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3038. if(sd->state.lr_flag != 2)
  3039. sd->bonus.short_weapon_damage_return += val;
  3040. break;
  3041. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3042. if(sd->state.lr_flag != 2)
  3043. sd->bonus.long_weapon_damage_return += val;
  3044. break;
  3045. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3046. if(sd->state.lr_flag != 2)
  3047. sd->bonus.magic_damage_return += val;
  3048. break;
  3049. case SP_REDUCE_DAMAGE_RETURN:
  3050. if (sd->state.lr_flag != 2)
  3051. sd->bonus.reduce_damage_return += val;
  3052. break;
  3053. case SP_ALL_STATS: // [Valaris]
  3054. if(sd->state.lr_flag!=2) {
  3055. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3056. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3057. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3058. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3059. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3060. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3061. }
  3062. break;
  3063. case SP_AGI_VIT: // [Valaris]
  3064. if(sd->state.lr_flag!=2) {
  3065. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3066. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3067. }
  3068. break;
  3069. case SP_AGI_DEX_STR: // [Valaris]
  3070. if(sd->state.lr_flag!=2) {
  3071. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3072. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3073. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3074. }
  3075. break;
  3076. case SP_PERFECT_HIDE: // [Valaris]
  3077. if(sd->state.lr_flag!=2)
  3078. sd->special_state.perfect_hiding=1;
  3079. break;
  3080. case SP_UNBREAKABLE:
  3081. if(sd->state.lr_flag!=2)
  3082. sd->bonus.unbreakable += val;
  3083. break;
  3084. case SP_UNBREAKABLE_WEAPON:
  3085. if(sd->state.lr_flag != 2)
  3086. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3087. break;
  3088. case SP_UNBREAKABLE_ARMOR:
  3089. if(sd->state.lr_flag != 2)
  3090. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3091. break;
  3092. case SP_UNBREAKABLE_HELM:
  3093. if(sd->state.lr_flag != 2)
  3094. sd->bonus.unbreakable_equip |= EQP_HELM;
  3095. break;
  3096. case SP_UNBREAKABLE_SHIELD:
  3097. if(sd->state.lr_flag != 2)
  3098. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3099. break;
  3100. case SP_UNBREAKABLE_GARMENT:
  3101. if(sd->state.lr_flag != 2)
  3102. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3103. break;
  3104. case SP_UNBREAKABLE_SHOES:
  3105. if(sd->state.lr_flag != 2)
  3106. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3107. break;
  3108. case SP_CLASSCHANGE: // [Valaris]
  3109. if(sd->state.lr_flag !=2)
  3110. sd->bonus.classchange=val;
  3111. break;
  3112. case SP_SHORT_ATK_RATE:
  3113. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3114. sd->bonus.short_attack_atk_rate+=val;
  3115. break;
  3116. case SP_LONG_ATK_RATE:
  3117. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3118. sd->bonus.long_attack_atk_rate+=val;
  3119. break;
  3120. case SP_BREAK_WEAPON_RATE:
  3121. if(sd->state.lr_flag != 2)
  3122. sd->bonus.break_weapon_rate+=val;
  3123. break;
  3124. case SP_BREAK_ARMOR_RATE:
  3125. if(sd->state.lr_flag != 2)
  3126. sd->bonus.break_armor_rate+=val;
  3127. break;
  3128. case SP_ADD_STEAL_RATE:
  3129. if(sd->state.lr_flag != 2)
  3130. sd->bonus.add_steal_rate+=val;
  3131. break;
  3132. case SP_DELAYRATE:
  3133. if(sd->state.lr_flag != 2)
  3134. sd->bonus.delayrate -= val;
  3135. break;
  3136. case SP_CRIT_ATK_RATE:
  3137. if(sd->state.lr_flag != 2)
  3138. sd->bonus.crit_atk_rate += val;
  3139. break;
  3140. case SP_CRIT_DEF_RATE:
  3141. if(sd->state.lr_flag != 2)
  3142. sd->bonus.crit_def_rate += val;
  3143. break;
  3144. case SP_NO_REGEN:
  3145. if(sd->state.lr_flag != 2)
  3146. sd->regen.state.block|=val;
  3147. break;
  3148. case SP_UNSTRIPABLE_WEAPON:
  3149. if(sd->state.lr_flag != 2)
  3150. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3151. break;
  3152. case SP_UNSTRIPABLE:
  3153. case SP_UNSTRIPABLE_ARMOR:
  3154. if(sd->state.lr_flag != 2)
  3155. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3156. break;
  3157. case SP_UNSTRIPABLE_HELM:
  3158. if(sd->state.lr_flag != 2)
  3159. sd->bonus.unstripable_equip |= EQP_HELM;
  3160. break;
  3161. case SP_UNSTRIPABLE_SHIELD:
  3162. if(sd->state.lr_flag != 2)
  3163. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3164. break;
  3165. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3166. if(!sd->state.lr_flag) {
  3167. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3168. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3169. } else if(sd->state.lr_flag == 1) {
  3170. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3171. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3172. }
  3173. break;
  3174. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3175. if(!sd->state.lr_flag) {
  3176. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3177. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3178. } else if(sd->state.lr_flag == 1) {
  3179. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3180. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3181. }
  3182. break;
  3183. case SP_SP_GAIN_VALUE:
  3184. if(!sd->state.lr_flag)
  3185. sd->bonus.sp_gain_value += val;
  3186. break;
  3187. case SP_HP_GAIN_VALUE:
  3188. if(!sd->state.lr_flag)
  3189. sd->bonus.hp_gain_value += val;
  3190. break;
  3191. case SP_LONG_SP_GAIN_VALUE:
  3192. if(!sd->state.lr_flag)
  3193. sd->bonus.long_sp_gain_value += val;
  3194. case SP_LONG_HP_GAIN_VALUE:
  3195. if(!sd->state.lr_flag)
  3196. sd->bonus.long_hp_gain_value += val;
  3197. break;
  3198. case SP_MAGIC_SP_GAIN_VALUE:
  3199. if(!sd->state.lr_flag)
  3200. sd->bonus.magic_sp_gain_value += val;
  3201. break;
  3202. case SP_MAGIC_HP_GAIN_VALUE:
  3203. if(!sd->state.lr_flag)
  3204. sd->bonus.magic_hp_gain_value += val;
  3205. break;
  3206. case SP_ADD_HEAL_RATE:
  3207. if(sd->state.lr_flag != 2)
  3208. sd->bonus.add_heal_rate += val;
  3209. break;
  3210. case SP_ADD_HEAL2_RATE:
  3211. if(sd->state.lr_flag != 2)
  3212. sd->bonus.add_heal2_rate += val;
  3213. break;
  3214. case SP_ADD_ITEM_HEAL_RATE:
  3215. if(sd->state.lr_flag != 2)
  3216. sd->bonus.itemhealrate2 += val;
  3217. break;
  3218. case SP_EMATK:
  3219. if(sd->state.lr_flag != 2)
  3220. sd->bonus.ematk += val;
  3221. break;
  3222. case SP_ADD_VARIABLECAST:
  3223. if (sd->state.lr_flag != 2)
  3224. sd->bonus.add_varcast += val;
  3225. break;
  3226. #ifdef RENEWAL_CAST
  3227. case SP_FIXCASTRATE:
  3228. if(sd->state.lr_flag != 2)
  3229. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3230. break;
  3231. case SP_ADD_FIXEDCAST:
  3232. if(sd->state.lr_flag != 2)
  3233. sd->bonus.add_fixcast += val;
  3234. break;
  3235. case SP_CASTRATE:
  3236. case SP_VARCASTRATE:
  3237. if(sd->state.lr_flag != 2)
  3238. sd->bonus.varcastrate -= val;
  3239. break;
  3240. #else
  3241. case SP_ADD_FIXEDCAST:
  3242. case SP_FIXCASTRATE:
  3243. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3244. break;
  3245. case SP_VARCASTRATE:
  3246. case SP_CASTRATE:
  3247. if(sd->state.lr_flag != 2)
  3248. sd->castrate += val;
  3249. break;
  3250. #endif
  3251. case SP_ADDMAXWEIGHT:
  3252. if (sd->state.lr_flag != 2)
  3253. sd->add_max_weight += val;
  3254. break;
  3255. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3256. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3257. break;
  3258. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3259. if (sd->state.lr_flag != 2)
  3260. sd->bonus.critical_rangeatk += val*10;
  3261. else
  3262. sd->bonus.arrow_cri += val*10;
  3263. break;
  3264. case SP_WEAPON_ATK_RATE:
  3265. if (sd->state.lr_flag != 2)
  3266. sd->bonus.weapon_atk_rate += val;
  3267. break;
  3268. case SP_WEAPON_MATK_RATE:
  3269. if (sd->state.lr_flag != 2)
  3270. sd->bonus.weapon_matk_rate += val;
  3271. break;
  3272. case SP_NO_MADO_FUEL:
  3273. if (sd->state.lr_flag != 2)
  3274. sd->special_state.no_mado_fuel = 1;
  3275. break;
  3276. case SP_NO_WALK_DELAY:
  3277. if (sd->state.lr_flag != 2)
  3278. sd->special_state.no_walk_delay = 1;
  3279. break;
  3280. default:
  3281. if (current_equip_combo_pos > 0) {
  3282. 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);
  3283. }
  3284. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3285. 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);
  3286. }
  3287. else {
  3288. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3289. }
  3290. break;
  3291. }
  3292. }
  3293. /*==========================================
  3294. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3295. * format: bonus2 bBonusName,type2,val;
  3296. * @param sd
  3297. * @param type Bonus type used by bBonusName
  3298. * @param type2
  3299. * @param val Value that usually for rate or fixed value
  3300. *------------------------------------------*/
  3301. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3302. {
  3303. nullpo_retv(sd);
  3304. switch(type){
  3305. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3306. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3307. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3308. sd->right_weapon.addele[type2]+=val;
  3309. else if(sd->state.lr_flag == 1)
  3310. sd->left_weapon.addele[type2]+=val;
  3311. else if(sd->state.lr_flag == 2)
  3312. sd->indexed_bonus.arrow_addele[type2]+=val;
  3313. break;
  3314. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3315. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3316. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3317. sd->right_weapon.addrace[type2]+=val;
  3318. else if(sd->state.lr_flag == 1)
  3319. sd->left_weapon.addrace[type2]+=val;
  3320. else if(sd->state.lr_flag == 2)
  3321. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3322. break;
  3323. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3324. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3325. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3326. sd->right_weapon.addclass[type2]+=val;
  3327. else if(sd->state.lr_flag == 1)
  3328. sd->left_weapon.addclass[type2]+=val;
  3329. else if(sd->state.lr_flag == 2)
  3330. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3331. break;
  3332. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3333. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3334. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3335. sd->right_weapon.addsize[type2]+=val;
  3336. else if(sd->state.lr_flag == 1)
  3337. sd->left_weapon.addsize[type2]+=val;
  3338. else if(sd->state.lr_flag == 2)
  3339. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3340. break;
  3341. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3342. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3343. if(sd->state.lr_flag != 2)
  3344. sd->indexed_bonus.subele_script[type2] += val;
  3345. break;
  3346. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3347. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3348. if(sd->state.lr_flag != 2)
  3349. sd->indexed_bonus.subrace[type2]+=val;
  3350. break;
  3351. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3352. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3353. if(sd->state.lr_flag != 2)
  3354. sd->indexed_bonus.subclass[type2]+=val;
  3355. break;
  3356. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3357. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3358. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3359. break;
  3360. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3361. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3362. 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);
  3363. break;
  3364. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3365. if(sd->state.lr_flag == 2)
  3366. break;
  3367. if (sd->reseff.size() == MAX_PC_BONUS) {
  3368. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3369. break;
  3370. }
  3371. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3372. break;
  3373. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3374. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3375. if(sd->state.lr_flag != 2)
  3376. sd->indexed_bonus.magic_addele_script[type2] += val;
  3377. break;
  3378. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3379. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3380. if(sd->state.lr_flag != 2)
  3381. sd->indexed_bonus.magic_addrace[type2]+=val;
  3382. break;
  3383. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3384. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3385. if(sd->state.lr_flag != 2)
  3386. sd->indexed_bonus.magic_addclass[type2]+=val;
  3387. break;
  3388. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3389. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3390. if(sd->state.lr_flag != 2)
  3391. sd->indexed_bonus.magic_addsize[type2]+=val;
  3392. break;
  3393. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3394. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3395. if(sd->state.lr_flag != 2)
  3396. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3397. break;
  3398. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3399. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3400. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3401. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3402. break;
  3403. }
  3404. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3405. }
  3406. break;
  3407. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3408. if(sd->state.lr_flag == 2)
  3409. break;
  3410. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3411. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3412. break;
  3413. }
  3414. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3415. break;
  3416. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3417. if(sd->state.lr_flag == 2)
  3418. break;
  3419. if (sd->add_def.size() == MAX_PC_BONUS) {
  3420. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3421. break;
  3422. }
  3423. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3424. break;
  3425. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3426. if(sd->state.lr_flag == 2)
  3427. break;
  3428. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3429. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3430. break;
  3431. }
  3432. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3433. break;
  3434. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3435. if(!sd->state.lr_flag) {
  3436. sd->right_weapon.hp_drain_rate.rate += type2;
  3437. sd->right_weapon.hp_drain_rate.per += val;
  3438. }
  3439. else if(sd->state.lr_flag == 1) {
  3440. sd->left_weapon.hp_drain_rate.rate += type2;
  3441. sd->left_weapon.hp_drain_rate.per += val;
  3442. }
  3443. break;
  3444. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3445. if(!sd->state.lr_flag) {
  3446. sd->right_weapon.sp_drain_rate.rate += type2;
  3447. sd->right_weapon.sp_drain_rate.per += val;
  3448. }
  3449. else if(sd->state.lr_flag == 1) {
  3450. sd->left_weapon.sp_drain_rate.rate += type2;
  3451. sd->left_weapon.sp_drain_rate.per += val;
  3452. }
  3453. break;
  3454. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3455. if(sd->state.lr_flag != 2) {
  3456. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3457. }
  3458. break;
  3459. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3460. if(sd->state.lr_flag != 2) {
  3461. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3462. }
  3463. break;
  3464. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3465. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3466. sd->bonus.get_zeny_rate = val;
  3467. sd->bonus.get_zeny_num = type2;
  3468. }
  3469. break;
  3470. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3471. if(sd->state.lr_flag != 2) {
  3472. sd->bonus.get_zeny_rate += val;
  3473. sd->bonus.get_zeny_num += type2;
  3474. }
  3475. break;
  3476. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3477. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3478. if(sd->state.lr_flag == 2)
  3479. break;
  3480. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3481. sd->special_state.bonus_coma = 1;
  3482. break;
  3483. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3484. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3485. if(sd->state.lr_flag == 2)
  3486. break;
  3487. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3488. sd->special_state.bonus_coma = 1;
  3489. break;
  3490. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3491. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3492. if(sd->state.lr_flag == 2)
  3493. break;
  3494. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3495. sd->special_state.bonus_coma = 1;
  3496. break;
  3497. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3498. if(sd->state.lr_flag != 2)
  3499. sd->indexed_bonus.weapon_atk[type2]+=val;
  3500. break;
  3501. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3502. if(sd->state.lr_flag != 2)
  3503. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3504. break;
  3505. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3506. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3507. if(sd->state.lr_flag != 2)
  3508. sd->indexed_bonus.critaddrace[type2] += val*10;
  3509. break;
  3510. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3511. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3512. if(sd->state.lr_flag != 2)
  3513. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3514. break;
  3515. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3516. if(sd->state.lr_flag == 2)
  3517. break;
  3518. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3519. 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);
  3520. break;
  3521. }
  3522. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3523. break;
  3524. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3525. if(sd->state.lr_flag == 2)
  3526. break;
  3527. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3528. 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);
  3529. break;
  3530. }
  3531. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3532. break;
  3533. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3534. if(sd->state.lr_flag == 2)
  3535. break;
  3536. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3537. 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);
  3538. break;
  3539. }
  3540. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3541. break;
  3542. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3543. if(sd->state.lr_flag == 2)
  3544. break;
  3545. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3546. 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);
  3547. break;
  3548. }
  3549. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3550. break;
  3551. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3552. if(sd->state.lr_flag != 2) {
  3553. sd->hp_loss.value = type2;
  3554. sd->hp_loss.rate = val;
  3555. }
  3556. break;
  3557. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3558. if(sd->state.lr_flag != 2) {
  3559. sd->hp_regen.value = type2;
  3560. sd->hp_regen.rate = val;
  3561. }
  3562. break;
  3563. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3564. if (sd->state.lr_flag != 2) {
  3565. sd->percent_hp_regen.value = type2;
  3566. sd->percent_hp_regen.rate = val;
  3567. }
  3568. break;
  3569. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3570. if (sd->state.lr_flag != 2) {
  3571. sd->percent_sp_regen.value = type2;
  3572. sd->percent_sp_regen.rate = val;
  3573. }
  3574. break;
  3575. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3576. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3577. if(sd->state.lr_flag != 2)
  3578. sd->right_weapon.addrace2[type2] += val;
  3579. else
  3580. sd->left_weapon.addrace2[type2] += val;
  3581. break;
  3582. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3583. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3584. if(sd->state.lr_flag != 2)
  3585. sd->indexed_bonus.subsize[type2]+=val;
  3586. break;
  3587. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3588. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3589. if(sd->state.lr_flag != 2)
  3590. sd->indexed_bonus.magic_subsize[type2]+=val;
  3591. break;
  3592. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3593. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3594. if(sd->state.lr_flag != 2)
  3595. sd->indexed_bonus.subrace2[type2]+=val;
  3596. break;
  3597. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3598. if(sd->state.lr_flag == 2)
  3599. break;
  3600. if (!itemdb_exists(type2)) {
  3601. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3602. break;
  3603. }
  3604. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3605. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3606. break;
  3607. }
  3608. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3609. break;
  3610. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3611. if (sd->state.lr_flag == 2)
  3612. break;
  3613. if (!type2 || !itemdb_group.exists(type2)) {
  3614. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3615. break;
  3616. }
  3617. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3618. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3619. break;
  3620. }
  3621. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3622. break;
  3623. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3624. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3625. if(sd->state.lr_flag != 2)
  3626. sd->indexed_bonus.expaddrace[type2]+=val;
  3627. break;
  3628. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3629. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3630. if(sd->state.lr_flag != 2)
  3631. sd->indexed_bonus.expaddclass[type2]+=val;
  3632. break;
  3633. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3634. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3635. if(sd->state.lr_flag != 2)
  3636. sd->indexed_bonus.sp_gain_race[type2]+=val;
  3637. break;
  3638. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3639. if (sd->state.lr_flag != 2)
  3640. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3641. break;
  3642. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3643. if (sd->state.lr_flag != 2)
  3644. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3645. break;
  3646. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3647. if(sd->state.lr_flag != 2) {
  3648. sd->sp_loss.value = type2;
  3649. sd->sp_loss.rate = val;
  3650. }
  3651. break;
  3652. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3653. if(sd->state.lr_flag != 2) {
  3654. sd->sp_regen.value = type2;
  3655. sd->sp_regen.rate = val;
  3656. }
  3657. break;
  3658. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3659. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3660. if(!sd->state.lr_flag) {
  3661. sd->right_weapon.hp_drain_race[type2] += val;
  3662. }
  3663. else if(sd->state.lr_flag == 1) {
  3664. sd->left_weapon.hp_drain_race[type2] += val;
  3665. }
  3666. break;
  3667. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3668. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3669. if(!sd->state.lr_flag) {
  3670. sd->right_weapon.sp_drain_race[type2] += val;
  3671. }
  3672. else if(sd->state.lr_flag == 1) {
  3673. sd->left_weapon.sp_drain_race[type2] += val;
  3674. }
  3675. break;
  3676. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3677. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3678. if(!sd->state.lr_flag) {
  3679. sd->right_weapon.hp_drain_class[type2] += val;
  3680. }
  3681. else if(sd->state.lr_flag == 1) {
  3682. sd->left_weapon.hp_drain_class[type2] += val;
  3683. }
  3684. break;
  3685. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3686. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3687. if(!sd->state.lr_flag) {
  3688. sd->right_weapon.sp_drain_class[type2] += val;
  3689. }
  3690. else if(sd->state.lr_flag == 1) {
  3691. sd->left_weapon.sp_drain_class[type2] += val;
  3692. }
  3693. break;
  3694. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3695. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3696. if(sd->state.lr_flag != 2)
  3697. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  3698. break;
  3699. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3700. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3701. if(sd->state.lr_flag != 2)
  3702. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  3703. break;
  3704. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3705. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3706. if(sd->state.lr_flag != 2)
  3707. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  3708. break;
  3709. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3710. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3711. if (sd->state.lr_flag != 2)
  3712. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  3713. break;
  3714. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3715. if(sd->state.lr_flag == 2)
  3716. break;
  3717. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3718. 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);
  3719. break;
  3720. }
  3721. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3722. break;
  3723. case SP_SKILL_DELAY:
  3724. if(sd->state.lr_flag == 2)
  3725. break;
  3726. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3727. 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);
  3728. break;
  3729. }
  3730. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3731. break;
  3732. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3733. if(sd->state.lr_flag == 2)
  3734. break;
  3735. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3736. 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);
  3737. break;
  3738. }
  3739. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3740. break;
  3741. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3742. if (sd->state.lr_flag == 2)
  3743. break;
  3744. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3745. 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);
  3746. break;
  3747. }
  3748. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3749. break;
  3750. #ifdef RENEWAL_CAST
  3751. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3752. if(sd->state.lr_flag == 2)
  3753. break;
  3754. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3755. 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);
  3756. break;
  3757. }
  3758. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3759. break;
  3760. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3761. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3762. if(sd->state.lr_flag == 2)
  3763. break;
  3764. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3765. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3766. break;
  3767. }
  3768. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3769. break;
  3770. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3771. if(sd->state.lr_flag == 2)
  3772. break;
  3773. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3774. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3775. break;
  3776. }
  3777. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3778. break;
  3779. #else
  3780. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3781. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3782. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3783. break;
  3784. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3785. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3786. if(sd->state.lr_flag == 2)
  3787. break;
  3788. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3789. 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);
  3790. break;
  3791. }
  3792. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3793. break;
  3794. #endif
  3795. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3796. if(sd->state.lr_flag == 2)
  3797. break;
  3798. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3799. 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);
  3800. break;
  3801. }
  3802. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3803. break;
  3804. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3805. if (sd->subskill.size() == MAX_PC_BONUS) {
  3806. 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);
  3807. break;
  3808. }
  3809. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3810. break;
  3811. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3812. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3813. sd->indexed_bonus.subdefele[type2] += val;
  3814. break;
  3815. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3816. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3817. sd->indexed_bonus.coma_class[type2] += val;
  3818. sd->special_state.bonus_coma = 1;
  3819. break;
  3820. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3821. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3822. sd->indexed_bonus.coma_race[type2] += val;
  3823. sd->special_state.bonus_coma = 1;
  3824. break;
  3825. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3826. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3827. if(sd->state.lr_flag != 2)
  3828. sd->indexed_bonus.magic_addrace2[type2] += val;
  3829. break;
  3830. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3831. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3832. if (sd->state.lr_flag != 2)
  3833. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  3834. break;
  3835. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3836. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3837. if (sd->state.lr_flag != 2)
  3838. sd->indexed_bonus.dropaddrace[type2] += val;
  3839. break;
  3840. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3841. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3842. if (sd->state.lr_flag != 2)
  3843. sd->indexed_bonus.dropaddclass[type2] += val;
  3844. break;
  3845. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  3846. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  3847. sd->indexed_bonus.magic_subdefele[type2] += val;
  3848. break;
  3849. default:
  3850. if (current_equip_combo_pos > 0) {
  3851. 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);
  3852. }
  3853. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3854. 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);
  3855. }
  3856. else {
  3857. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3858. }
  3859. break;
  3860. }
  3861. }
  3862. /**
  3863. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3864. * @param sd
  3865. * @param type Bonus type used by bBonusName
  3866. * @param type2
  3867. * @param type3
  3868. * @param val Value that usually for rate or fixed value
  3869. */
  3870. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3871. {
  3872. nullpo_retv(sd);
  3873. switch(type){
  3874. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3875. if(sd->state.lr_flag != 2)
  3876. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3877. break;
  3878. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3879. if(sd->state.lr_flag != 2)
  3880. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3881. break;
  3882. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3883. if(sd->state.lr_flag != 2)
  3884. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3885. break;
  3886. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3887. if(sd->state.lr_flag != 2)
  3888. {
  3889. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3890. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3891. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3892. }
  3893. break;
  3894. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3895. if(sd->state.lr_flag != 2)
  3896. {
  3897. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3898. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3899. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3900. }
  3901. break;
  3902. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3903. if (sd->state.lr_flag != 2)
  3904. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3905. break;
  3906. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3907. if (sd->state.lr_flag != 2)
  3908. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3909. break;
  3910. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3911. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3912. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3913. break;
  3914. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3915. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3916. if(sd->state.lr_flag != 2)
  3917. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3918. break;
  3919. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3920. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3921. if( sd->state.lr_flag != 2 )
  3922. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3923. break;
  3924. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3925. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3926. if (sd->state.lr_flag != 2)
  3927. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3928. break;
  3929. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3930. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3931. if (sd->state.lr_flag != 2)
  3932. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3933. break;
  3934. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  3935. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  3936. if (sd->state.lr_flag != 2)
  3937. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  3938. break;
  3939. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3940. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3941. if(sd->state.lr_flag != 2) {
  3942. sd->sp_vanish_race[type2].rate += type3;
  3943. sd->sp_vanish_race[type2].per += val;
  3944. }
  3945. break;
  3946. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3947. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3948. if(sd->state.lr_flag != 2) {
  3949. sd->hp_vanish_race[type2].rate += type3;
  3950. sd->hp_vanish_race[type2].per += val;
  3951. }
  3952. break;
  3953. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3954. if(sd->state.lr_flag != 2) {
  3955. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3956. }
  3957. break;
  3958. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3959. if(sd->state.lr_flag != 2) {
  3960. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3961. }
  3962. break;
  3963. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3964. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3965. if (sd->state.lr_flag == 2)
  3966. break;
  3967. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3968. //if (type3 > sd->norecover_state_race[type2].rate) {
  3969. // sd->norecover_state_race[type2].rate = type3;
  3970. // sd->norecover_state_race[type2].tick = val;
  3971. // break;
  3972. //}
  3973. sd->norecover_state_race[type2].rate = type3;
  3974. sd->norecover_state_race[type2].tick = val;
  3975. break;
  3976. default:
  3977. if (current_equip_combo_pos > 0) {
  3978. 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);
  3979. }
  3980. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3981. 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);
  3982. }
  3983. else {
  3984. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3985. }
  3986. break;
  3987. }
  3988. }
  3989. /**
  3990. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3991. * @param sd
  3992. * @param type Bonus type used by bBonusName
  3993. * @param type2
  3994. * @param type3
  3995. * @param type4
  3996. * @param val Value that usually for rate or fixed value
  3997. */
  3998. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3999. {
  4000. nullpo_retv(sd);
  4001. switch(type){
  4002. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4003. if(sd->state.lr_flag != 2)
  4004. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  4005. break;
  4006. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4007. if(sd->state.lr_flag != 2)
  4008. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  4009. break;
  4010. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4011. if(sd->state.lr_flag != 2)
  4012. {
  4013. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4014. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4015. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  4016. }
  4017. break;
  4018. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4019. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4020. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4021. break;
  4022. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4023. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4024. if (sd->state.lr_flag != 2)
  4025. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4026. break;
  4027. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4028. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4029. if( sd->state.lr_flag != 2 )
  4030. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4031. break;
  4032. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4033. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4034. if(sd->state.lr_flag == 2)
  4035. break;
  4036. sd->def_set_race[type2].rate = type3;
  4037. sd->def_set_race[type2].tick = type4;
  4038. sd->def_set_race[type2].value = val;
  4039. break;
  4040. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4041. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4042. if(sd->state.lr_flag == 2)
  4043. break;
  4044. sd->mdef_set_race[type2].rate = type3;
  4045. sd->mdef_set_race[type2].tick = type4;
  4046. sd->mdef_set_race[type2].value = val;
  4047. break;
  4048. default:
  4049. if (current_equip_combo_pos > 0) {
  4050. 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);
  4051. }
  4052. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4053. 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);
  4054. }
  4055. else {
  4056. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4057. }
  4058. break;
  4059. }
  4060. }
  4061. /**
  4062. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4063. * @param sd
  4064. * @param type Bonus type used by bBonusName
  4065. * @param type2
  4066. * @param type3
  4067. * @param type4
  4068. * @param val Value that usually for rate or fixed value
  4069. */
  4070. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4071. {
  4072. nullpo_retv(sd);
  4073. switch(type){
  4074. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4075. if(sd->state.lr_flag != 2)
  4076. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  4077. break;
  4078. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4079. if(sd->state.lr_flag != 2)
  4080. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  4081. break;
  4082. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4083. if(sd->state.lr_flag != 2)
  4084. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  4085. break;
  4086. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4087. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4088. if( sd->state.lr_flag != 2 )
  4089. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4090. break;
  4091. default:
  4092. if (current_equip_combo_pos > 0) {
  4093. 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);
  4094. }
  4095. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4096. 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);
  4097. }
  4098. else {
  4099. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4100. }
  4101. break;
  4102. }
  4103. }
  4104. /*==========================================
  4105. * Grants a player a given skill. Flag values are:
  4106. * 0 - Grant permanent skill to be bound to skill tree
  4107. * 1 - Grant an item skill (temporary)
  4108. * 2 - Like 1, except the level granted can stack with previously learned level.
  4109. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4110. *------------------------------------------*/
  4111. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4112. uint16 idx = 0;
  4113. nullpo_ret(sd);
  4114. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4115. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4116. return false;
  4117. }
  4118. if (level > MAX_SKILL_LEVEL) {
  4119. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4120. return false;
  4121. }
  4122. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4123. 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);
  4124. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4125. }
  4126. switch (type) {
  4127. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4128. sd->status.skill[idx].id = skill_id;
  4129. sd->status.skill[idx].lv = level;
  4130. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4131. if (level == 0) { //Remove skill.
  4132. sd->status.skill[idx].id = 0;
  4133. clif_deleteskill(sd,skill_id);
  4134. } else
  4135. clif_addskill(sd,skill_id);
  4136. 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.
  4137. status_calc_pc(sd, SCO_NONE);
  4138. break;
  4139. case ADDSKILL_TEMP: //Item bonus skill.
  4140. if (sd->status.skill[idx].id != 0) {
  4141. if (sd->status.skill[idx].lv >= level)
  4142. return true;
  4143. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4144. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4145. } else {
  4146. sd->status.skill[idx].id = skill_id;
  4147. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4148. }
  4149. sd->status.skill[idx].lv = level;
  4150. break;
  4151. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4152. if (sd->status.skill[idx].id != 0) {
  4153. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4154. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4155. } else {
  4156. sd->status.skill[idx].id = skill_id;
  4157. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4158. }
  4159. sd->status.skill[idx].lv += level;
  4160. break;
  4161. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4162. sd->status.skill[idx].id = skill_id;
  4163. sd->status.skill[idx].lv = level;
  4164. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4165. if (level == 0) { //Remove skill.
  4166. sd->status.skill[idx].id = 0;
  4167. clif_deleteskill(sd,skill_id);
  4168. } else
  4169. clif_addskill(sd,skill_id);
  4170. 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.
  4171. status_calc_pc(sd, SCO_NONE);
  4172. break;
  4173. default:
  4174. return false;
  4175. }
  4176. return true;
  4177. }
  4178. /*==========================================
  4179. * Append a card to an item ?
  4180. *------------------------------------------*/
  4181. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4182. {
  4183. nullpo_ret(sd);
  4184. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4185. return 0;
  4186. }
  4187. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4188. return 0;
  4189. }
  4190. int i;
  4191. t_itemid nameid;
  4192. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4193. struct item_data* item_card = sd->inventory_data[idx_card];
  4194. if(item_eq == nullptr)
  4195. return 0; //Invalid item index.
  4196. if(item_card == nullptr)
  4197. return 0; //Invalid card index.
  4198. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4199. return 0; // target item missing
  4200. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4201. return 0; // target card missing
  4202. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4203. return 0; // only weapons and armor are allowed
  4204. if( item_card->type != IT_CARD )
  4205. return 0; // must be a card
  4206. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4207. return 0; // target must be identified
  4208. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4209. return 0; // card slots reserved for other purposes
  4210. if( (item_eq->equip & item_card->equip) == 0 )
  4211. return 0; // card cannot be compounded on this item type
  4212. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4213. return 0; // attempted to place shield card on left-hand weapon.
  4214. 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)) )
  4215. return 0; // specific accessory-card can only be inserted to specific accessory.
  4216. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4217. return 0; // item must be unequipped
  4218. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4219. if( i == item_eq->slots )
  4220. return 0; // no free slots
  4221. // remember the card id to insert
  4222. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4223. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4224. {// failed
  4225. clif_insert_card(sd,idx_equip,idx_card,1);
  4226. }
  4227. else
  4228. {// success
  4229. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4230. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4231. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4232. clif_insert_card(sd,idx_equip,idx_card,0);
  4233. }
  4234. return 0;
  4235. }
  4236. /**
  4237. * Returns the count of unidentified items with the option to identify too.
  4238. * @param sd: Player data
  4239. * @param identify_item: Whether or not to identify any unidentified items
  4240. * @return Unidentified items count
  4241. */
  4242. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4243. {
  4244. int unidentified_count = 0;
  4245. for (int i = 0; i < MAX_INVENTORY; i++) {
  4246. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4247. if (identify_item == true) {
  4248. sd->inventory.u.items_inventory[i].identify = 1;
  4249. clif_item_identified(sd,i,0);
  4250. }
  4251. unidentified_count++;
  4252. }
  4253. }
  4254. return unidentified_count;
  4255. }
  4256. //
  4257. // Items
  4258. //
  4259. /*==========================================
  4260. * Update buying value by skills
  4261. *------------------------------------------*/
  4262. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4263. {
  4264. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4265. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4266. rate1 = 5+skill*2-((skill==10)? 1:0);
  4267. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4268. rate2 = 5+skill*4;
  4269. if(rate1 < rate2) rate1 = rate2;
  4270. if(rate1)
  4271. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4272. if(val < battle_config.min_shop_buy)
  4273. val = battle_config.min_shop_buy;
  4274. return val;
  4275. }
  4276. /*==========================================
  4277. * Update selling value by skills
  4278. *------------------------------------------*/
  4279. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4280. {
  4281. int skill,val = orig_value,rate = 0;
  4282. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4283. rate = 5+skill*2-((skill==10)? 1:0);
  4284. if(rate)
  4285. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4286. if (val < battle_config.min_shop_sell)
  4287. val = battle_config.min_shop_sell;
  4288. return val;
  4289. }
  4290. /*==========================================
  4291. * Checking if we have enough place on inventory for new item
  4292. * Make sure to take 30k as limit (for client I guess)
  4293. * @param sd
  4294. * @param nameid
  4295. * @param amount
  4296. * @return e_chkitem_result
  4297. *------------------------------------------*/
  4298. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4299. {
  4300. int i;
  4301. struct item_data* data;
  4302. nullpo_ret(sd);
  4303. if(amount > MAX_AMOUNT)
  4304. return CHKADDITEM_OVERAMOUNT;
  4305. data = itemdb_search(nameid);
  4306. if(!itemdb_isstackable2(data))
  4307. return CHKADDITEM_NEW;
  4308. if( data->stack.inventory && amount > data->stack.amount )
  4309. return CHKADDITEM_OVERAMOUNT;
  4310. if (data->flag.guid)
  4311. return CHKADDITEM_NEW;
  4312. for(i=0;i<MAX_INVENTORY;i++){
  4313. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4314. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4315. 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 ) )
  4316. return CHKADDITEM_OVERAMOUNT;
  4317. return CHKADDITEM_EXIST;
  4318. }
  4319. }
  4320. return CHKADDITEM_NEW;
  4321. }
  4322. /*==========================================
  4323. * Return number of available place in inventory
  4324. * Each non stackable item will reduce place by 1
  4325. * @param sd
  4326. * @return Number of empty slots
  4327. *------------------------------------------*/
  4328. uint8 pc_inventoryblank(struct map_session_data *sd)
  4329. {
  4330. uint8 i, b;
  4331. nullpo_ret(sd);
  4332. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4333. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4334. b++;
  4335. }
  4336. return b;
  4337. }
  4338. /**
  4339. * Attempts to remove zeny from player
  4340. * @param sd: Player
  4341. * @param zeny: Zeny removed
  4342. * @param type: Log type
  4343. * @param tsd: (optional) From who to log (if null take sd)
  4344. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4345. */
  4346. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4347. {
  4348. nullpo_retr(2,sd);
  4349. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4350. if( zeny < 0 )
  4351. {
  4352. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4353. return 1;
  4354. }
  4355. if( sd->status.zeny < zeny )
  4356. return 1; //Not enough.
  4357. sd->status.zeny -= zeny;
  4358. clif_updatestatus(sd,SP_ZENY);
  4359. if(!tsd) tsd = sd;
  4360. log_zeny(sd, type, tsd, -zeny);
  4361. if( zeny > 0 && sd->state.showzeny ) {
  4362. char output[255];
  4363. sprintf(output, "Removed %dz.", zeny);
  4364. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4365. }
  4366. return 0;
  4367. }
  4368. /**
  4369. * Attempts to give zeny to player
  4370. * @param sd: Player
  4371. * @param type: Log type
  4372. * @param tsd: (optional) From who to log (if null take sd)
  4373. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4374. */
  4375. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4376. {
  4377. nullpo_retr(-1,sd);
  4378. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4379. if( zeny < 0 )
  4380. {
  4381. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4382. return 1;
  4383. }
  4384. if( zeny > MAX_ZENY - sd->status.zeny )
  4385. zeny = MAX_ZENY - sd->status.zeny;
  4386. sd->status.zeny += zeny;
  4387. clif_updatestatus(sd,SP_ZENY);
  4388. if(!tsd) tsd = sd;
  4389. log_zeny(sd, type, tsd, zeny);
  4390. if( zeny > 0 && sd->state.showzeny ) {
  4391. char output[255];
  4392. sprintf(output, "Gained %dz.", zeny);
  4393. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4394. }
  4395. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4396. return 0;
  4397. }
  4398. /**
  4399. * Attempts to remove Cash Points from player
  4400. * @param sd: Player
  4401. * @param price: Total points (cash + kafra) the player has to pay
  4402. * @param points: Kafra points the player has to pay
  4403. * @param type: Log type
  4404. * @return -1: Not enough points, otherwise success (cash+points)
  4405. */
  4406. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4407. {
  4408. int cash;
  4409. nullpo_retr(-1,sd);
  4410. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4411. cash = price-points;
  4412. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4413. {
  4414. 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);
  4415. return -1;
  4416. }
  4417. if( cash ){
  4418. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4419. sd->cashPoints -= cash;
  4420. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4421. }
  4422. if( points ){
  4423. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4424. sd->kafraPoints -= points;
  4425. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4426. }
  4427. if( battle_config.cashshop_show_points )
  4428. {
  4429. char output[CHAT_SIZE_MAX];
  4430. 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.
  4431. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4432. }
  4433. return cash+points;
  4434. }
  4435. /**
  4436. * Attempts to give Cash Points to player
  4437. * @param sd: Player
  4438. * @param cash: Cash points the player gets
  4439. * @param points: Kafra points the player gets
  4440. * @param type: Log type
  4441. * @return -1: Error, otherwise success (cash or points)
  4442. */
  4443. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4444. {
  4445. char output[CHAT_SIZE_MAX];
  4446. nullpo_retr(-1,sd);
  4447. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4448. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4449. if( cash > 0 )
  4450. {
  4451. if( cash > MAX_ZENY-sd->cashPoints )
  4452. {
  4453. 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);
  4454. cash = MAX_ZENY-sd->cashPoints;
  4455. }
  4456. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4457. sd->cashPoints += cash;
  4458. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4459. if( battle_config.cashshop_show_points )
  4460. {
  4461. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4462. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4463. }
  4464. return cash;
  4465. }
  4466. if( points > 0 )
  4467. {
  4468. if( points > MAX_ZENY-sd->kafraPoints )
  4469. {
  4470. 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);
  4471. points = MAX_ZENY-sd->kafraPoints;
  4472. }
  4473. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4474. sd->kafraPoints += points;
  4475. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4476. if( battle_config.cashshop_show_points )
  4477. {
  4478. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4479. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4480. }
  4481. return points;
  4482. }
  4483. return -1; //shouldn't happen but just in case
  4484. }
  4485. /**
  4486. * Searching a specified itemid in inventory and return his stored index
  4487. * @param sd Player
  4488. * @param nameid Find this Item!
  4489. * @return Stored index in inventory, or -1 if not found.
  4490. **/
  4491. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4492. short i;
  4493. nullpo_retr(-1, sd);
  4494. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4495. return ( i < MAX_INVENTORY ) ? i : -1;
  4496. }
  4497. /** Attempt to add a new item to player inventory
  4498. * @param sd
  4499. * @param item_data
  4500. * @param amount
  4501. * @param log_type
  4502. * @return
  4503. * 0 = success
  4504. * 1 = invalid itemid not found or negative amount
  4505. * 2 = overweight
  4506. * 3 = ?
  4507. * 4 = no free place found
  4508. * 5 = max amount reached
  4509. * 6 = ?
  4510. * 7 = stack limitation
  4511. */
  4512. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4513. struct item_data *id;
  4514. int16 i;
  4515. unsigned int w;
  4516. nullpo_retr(ADDITEM_INVALID, sd);
  4517. nullpo_retr(ADDITEM_INVALID, item);
  4518. if( item->nameid == 0 || amount <= 0 )
  4519. return ADDITEM_INVALID;
  4520. if( amount > MAX_AMOUNT )
  4521. return ADDITEM_OVERAMOUNT;
  4522. id = itemdb_search(item->nameid);
  4523. if( id->stack.inventory && amount > id->stack.amount )
  4524. {// item stack limitation
  4525. return ADDITEM_STACKLIMIT;
  4526. }
  4527. w = id->weight*amount;
  4528. if(sd->weight + w > sd->max_weight)
  4529. return ADDITEM_OVERWEIGHT;
  4530. i = MAX_INVENTORY;
  4531. if (id->flag.guid && !item->unique_id)
  4532. item->unique_id = pc_generate_unique_id(sd);
  4533. // Stackable | Non Rental
  4534. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4535. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4536. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4537. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4538. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4539. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4540. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4541. 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 ) )
  4542. return ADDITEM_OVERAMOUNT;
  4543. sd->inventory.u.items_inventory[i].amount += amount;
  4544. clif_additem(sd,i,amount,0);
  4545. break;
  4546. }
  4547. }
  4548. }
  4549. if (i >= MAX_INVENTORY) {
  4550. i = pc_search_inventory(sd,0);
  4551. if( i < 0 )
  4552. return ADDITEM_OVERITEM;
  4553. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4554. // clear equip and favorite fields first, just in case
  4555. if( item->equip )
  4556. sd->inventory.u.items_inventory[i].equip = 0;
  4557. if( item->favorite )
  4558. sd->inventory.u.items_inventory[i].favorite = 0;
  4559. if( item->equipSwitch )
  4560. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4561. sd->inventory.u.items_inventory[i].amount = amount;
  4562. sd->inventory_data[i] = id;
  4563. sd->last_addeditem_index = i;
  4564. if (!itemdb_isstackable2(id) || id->flag.guid)
  4565. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4566. clif_additem(sd,i,amount,0);
  4567. }
  4568. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4569. sd->weight += w;
  4570. clif_updatestatus(sd,SP_WEIGHT);
  4571. //Auto-equip
  4572. if(id->flag.autoequip)
  4573. pc_equipitem(sd, i, id->equip);
  4574. /* rental item check */
  4575. if( item->expire_time ) {
  4576. if( time(NULL) > item->expire_time ) {
  4577. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4578. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4579. } else {
  4580. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4581. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4582. pc_inventory_rental_add(sd, seconds);
  4583. }
  4584. }
  4585. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4586. pc_show_questinfo(sd);
  4587. return ADDITEM_SUCCESS;
  4588. }
  4589. /*==========================================
  4590. * Remove an item at index n from inventory by amount.
  4591. * @param sd
  4592. * @param n Item index in inventory
  4593. * @param amount
  4594. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4595. * @param reason Delete reason
  4596. * @param log_type e_log_pick_type
  4597. * @return 1 - invalid itemid or negative amount; 0 - Success
  4598. *------------------------------------------*/
  4599. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4600. {
  4601. nullpo_retr(1, sd);
  4602. 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)
  4603. return 1;
  4604. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4605. sd->inventory.u.items_inventory[n].amount -= amount;
  4606. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4607. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4608. if(sd->inventory.u.items_inventory[n].equip)
  4609. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4610. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4611. sd->inventory_data[n] = NULL;
  4612. }
  4613. if(!(type&1))
  4614. clif_delitem(sd,n,amount,reason);
  4615. if(!(type&2))
  4616. clif_updatestatus(sd,SP_WEIGHT);
  4617. pc_show_questinfo(sd);
  4618. return 0;
  4619. }
  4620. /*==========================================
  4621. * Attempt to drop an item.
  4622. * @param sd
  4623. * @param n Item index in inventory
  4624. * @param amount Amount of item
  4625. * @return False = fail; True = success
  4626. *------------------------------------------*/
  4627. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4628. {
  4629. nullpo_retr(1, sd);
  4630. if(n < 0 || n >= MAX_INVENTORY)
  4631. return false;
  4632. if(amount <= 0)
  4633. return false;
  4634. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  4635. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4636. sd->inventory.u.items_inventory[n].amount < amount ||
  4637. sd->state.trading || sd->state.vending ||
  4638. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4639. )
  4640. return false;
  4641. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4642. return false;
  4643. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4644. {
  4645. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4646. return false; //Can't drop items in nodrop mapflag maps.
  4647. }
  4648. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4649. {
  4650. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4651. return false;
  4652. }
  4653. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4654. return false;
  4655. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4656. clif_dropitem(sd, n, amount);
  4657. return true;
  4658. }
  4659. /*==========================================
  4660. * Attempt to pick up an item.
  4661. * @param sd
  4662. * @param fitem Item that will be picked
  4663. * @return False = fail; True = success
  4664. *------------------------------------------*/
  4665. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4666. {
  4667. int flag = 0;
  4668. t_tick tick = gettick();
  4669. struct party_data *p = NULL;
  4670. nullpo_ret(sd);
  4671. nullpo_ret(fitem);
  4672. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4673. return false; // Distance is too far
  4674. if (sd->sc.cant.pickup)
  4675. return false;
  4676. if (sd->status.party_id)
  4677. p = party_search(sd->status.party_id);
  4678. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4679. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4680. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4681. if (!(p && p->party.item&1 &&
  4682. first_sd && first_sd->status.party_id == sd->status.party_id
  4683. ))
  4684. return false;
  4685. }
  4686. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4687. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4688. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4689. if (!(p && p->party.item&1 &&
  4690. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4691. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4692. ))
  4693. return false;
  4694. }
  4695. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4696. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4697. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4698. if(!(p && p->party.item&1 &&
  4699. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4700. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4701. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4702. ))
  4703. return false;
  4704. }
  4705. }
  4706. }
  4707. }
  4708. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4709. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4710. clif_additem(sd,0,0,flag);
  4711. return true;
  4712. }
  4713. //Display pickup animation.
  4714. pc_stop_attack(sd);
  4715. clif_takeitem(&sd->bl,&fitem->bl);
  4716. if (fitem->mob_id &&
  4717. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4718. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4719. )
  4720. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4721. map_clearflooritem(&fitem->bl);
  4722. return true;
  4723. }
  4724. /*==========================================
  4725. * Check if item is usable.
  4726. * Return:
  4727. * false = no
  4728. * true = yes
  4729. *------------------------------------------*/
  4730. bool pc_isUseitem(struct map_session_data *sd,int n)
  4731. {
  4732. struct item_data *item;
  4733. t_itemid nameid;
  4734. nullpo_ret(sd);
  4735. item = sd->inventory_data[n];
  4736. nameid = sd->inventory.u.items_inventory[n].nameid;
  4737. if( item == NULL )
  4738. return false;
  4739. //Not consumable item
  4740. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4741. return false;
  4742. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4743. return true;
  4744. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4745. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4746. return false;
  4747. //Prevent mass item usage. [Skotlex]
  4748. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4749. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4750. )
  4751. return false;
  4752. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4753. clif_msg(sd,ITEM_NOUSE_SITTING);
  4754. return false; // You cannot use this item while sitting.
  4755. }
  4756. if (sd->state.storage_flag && item->type != IT_CASH) {
  4757. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4758. return false; // You cannot use this item while storage is open.
  4759. }
  4760. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4761. return false;
  4762. switch( nameid ) {
  4763. case ITEMID_WING_OF_FLY:
  4764. case ITEMID_GIANT_FLY_WING:
  4765. case ITEMID_N_FLY_WING:
  4766. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4767. clif_skill_teleportmessage(sd,0);
  4768. return false;
  4769. }
  4770. if (nameid == ITEMID_GIANT_FLY_WING) {
  4771. struct party_data *pd = party_search(sd->status.party_id);
  4772. if (pd) {
  4773. int i;
  4774. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4775. if (i == MAX_PARTY) { // User is not party leader
  4776. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4777. break;
  4778. }
  4779. 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));
  4780. if (i == MAX_PARTY) { // No party members found on same map
  4781. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4782. break;
  4783. }
  4784. } else {
  4785. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4786. break;
  4787. }
  4788. }
  4789. // Fall through
  4790. case ITEMID_WING_OF_BUTTERFLY:
  4791. case ITEMID_N_BUTTERFLY_WING:
  4792. case ITEMID_DUN_TELE_SCROLL1:
  4793. case ITEMID_DUN_TELE_SCROLL2:
  4794. case ITEMID_DUN_TELE_SCROLL3:
  4795. case ITEMID_WOB_RUNE:
  4796. case ITEMID_WOB_SCHWALTZ:
  4797. case ITEMID_WOB_RACHEL:
  4798. case ITEMID_WOB_LOCAL:
  4799. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4800. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4801. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4802. return false;
  4803. }
  4804. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4805. return false;
  4806. break;
  4807. case ITEMID_MERCENARY_RED_POTION:
  4808. case ITEMID_MERCENARY_BLUE_POTION:
  4809. case ITEMID_M_CENTER_POTION:
  4810. case ITEMID_M_AWAKENING_POTION:
  4811. case ITEMID_M_BERSERK_POTION:
  4812. if( sd->md == NULL || sd->md->db == NULL )
  4813. return false;
  4814. if( sd->md->sc.data[SC_BERSERK] )
  4815. return false;
  4816. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4817. return false;
  4818. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4819. return false;
  4820. break;
  4821. case ITEMID_NEURALIZER:
  4822. if( !mapdata->flag[MF_RESET] )
  4823. return false;
  4824. break;
  4825. }
  4826. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4827. return false; // Mercenary Scrolls
  4828. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4829. if( pc_is90overweight(sd) ) {
  4830. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4831. return false;
  4832. }
  4833. if( !pc_inventoryblank(sd) ) {
  4834. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4835. return false;
  4836. }
  4837. }
  4838. //Gender check
  4839. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  4840. return false;
  4841. //Required level check
  4842. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4843. return false;
  4844. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4845. return false;
  4846. //Not equipable by class. [Skotlex]
  4847. if (!pc_job_can_use_item(sd,item))
  4848. return false;
  4849. if (sd->sc.count && (
  4850. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4851. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4852. sd->sc.data[SC_TRICKDEAD] ||
  4853. sd->sc.data[SC_HIDING] ||
  4854. sd->sc.data[SC__SHADOWFORM] ||
  4855. sd->sc.data[SC__INVISIBILITY] ||
  4856. sd->sc.data[SC__MANHOLE] ||
  4857. sd->sc.data[SC_DEEPSLEEP] ||
  4858. sd->sc.data[SC_CRYSTALIZE] ||
  4859. sd->sc.data[SC_KAGEHUMI] ||
  4860. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4861. sd->sc.data[SC_KINGS_GRACE] ||
  4862. sd->sc.data[SC_SUHIDE]))
  4863. return false;
  4864. if (!pc_isItemClass(sd,item))
  4865. return false;
  4866. //Dead Branch items
  4867. if( item->flag.dead_branch )
  4868. log_branch(sd);
  4869. return true;
  4870. }
  4871. /*==========================================
  4872. * Last checks to use an item.
  4873. * Return:
  4874. * 0 = fail
  4875. * 1 = success
  4876. *------------------------------------------*/
  4877. int pc_useitem(struct map_session_data *sd,int n)
  4878. {
  4879. t_tick tick = gettick();
  4880. int amount;
  4881. t_itemid nameid;
  4882. struct script_code *script;
  4883. struct item item;
  4884. struct item_data *id;
  4885. nullpo_ret(sd);
  4886. if (sd->state.mail_writing)
  4887. return 0;
  4888. if (sd->npc_id) {
  4889. if (sd->progressbar.npc_id) {
  4890. clif_progressbar_abort(sd);
  4891. return 0; // First item use attempt cancels the progress bar
  4892. }
  4893. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4894. #ifdef RENEWAL
  4895. clif_msg( sd, WORK_IN_PROGRESS );
  4896. #endif
  4897. return 0;
  4898. }
  4899. }
  4900. item = sd->inventory.u.items_inventory[n];
  4901. id = sd->inventory_data[n];
  4902. if (item.nameid == 0 || item.amount <= 0)
  4903. return 0;
  4904. if( !pc_isUseitem(sd,n) )
  4905. return 0;
  4906. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4907. nameid = id->nameid;
  4908. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4909. return 0;
  4910. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4911. if( id->flag.delay_consume > 0 ) {
  4912. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4913. return 0;
  4914. else if( pc_issit(sd) )
  4915. return 0;
  4916. }
  4917. //Since most delay-consume items involve using a "skill-type" target cursor,
  4918. //perform a skill-use check before going through. [Skotlex]
  4919. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4920. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4921. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4922. return 0;
  4923. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4924. return 0;
  4925. /* on restricted maps the item is consumed but the effect is not used */
  4926. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4927. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4928. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  4929. clif_useitemack(sd,n,item.amount-1,true);
  4930. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4931. }
  4932. return 0;/* regardless, effect is not run */
  4933. }
  4934. sd->itemid = item.nameid;
  4935. sd->itemindex = n;
  4936. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4937. sd->catch_target_class = PET_CATCH_FAIL;
  4938. amount = item.amount;
  4939. script = id->script;
  4940. //Check if the item is to be consumed immediately [Skotlex]
  4941. if (id->flag.delay_consume > 0)
  4942. clif_useitemack(sd, n, amount, true);
  4943. else
  4944. {
  4945. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4946. {
  4947. clif_useitemack(sd, n, amount - 1, true);
  4948. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4949. }
  4950. else
  4951. clif_useitemack(sd, n, 0, false);
  4952. }
  4953. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4954. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4955. //Update item use time.
  4956. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4957. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  4958. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4959. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4960. potion_flag = 0;
  4961. return 1;
  4962. }
  4963. /**
  4964. * Add item on cart for given index.
  4965. * @param sd
  4966. * @param item
  4967. * @param amount
  4968. * @param log_type
  4969. * @return See pc.hpp::e_additem_result
  4970. */
  4971. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4972. {
  4973. struct item_data *data;
  4974. int i,w;
  4975. nullpo_retr(ADDITEM_INVALID, sd);
  4976. nullpo_retr(ADDITEM_INVALID, item);
  4977. if(item->nameid == 0 || amount <= 0)
  4978. return ADDITEM_INVALID;
  4979. if (itemdb_ishatched_egg(item))
  4980. return ADDITEM_INVALID;
  4981. data = itemdb_search(item->nameid);
  4982. if( data->stack.cart && amount > data->stack.amount )
  4983. {// item stack limitation
  4984. return ADDITEM_STACKLIMIT;
  4985. }
  4986. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4987. { // Check item trade restrictions [Skotlex]
  4988. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4989. return ADDITEM_INVALID;
  4990. }
  4991. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4992. return ADDITEM_OVERWEIGHT;
  4993. i = MAX_CART;
  4994. if( itemdb_isstackable2(data) && !item->expire_time )
  4995. {
  4996. for (i = 0; i < MAX_CART; i++) {
  4997. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4998. && sd->cart.u.items_cart[i].bound == item->bound
  4999. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5000. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5001. )
  5002. break;
  5003. }
  5004. }
  5005. if( i < MAX_CART )
  5006. {// item already in cart, stack it
  5007. 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 ) )
  5008. return ADDITEM_OVERAMOUNT; // no slot
  5009. sd->cart.u.items_cart[i].amount += amount;
  5010. clif_cart_additem(sd,i,amount);
  5011. }
  5012. else
  5013. {// item not stackable or not present, add it
  5014. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5015. if( i == MAX_CART )
  5016. return ADDITEM_OVERAMOUNT; // no slot
  5017. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5018. sd->cart.u.items_cart[i].id = 0;
  5019. sd->cart.u.items_cart[i].amount = amount;
  5020. sd->cart_num++;
  5021. clif_cart_additem(sd,i,amount);
  5022. }
  5023. sd->cart.u.items_cart[i].favorite = 0; // clear
  5024. sd->cart.u.items_cart[i].equipSwitch = 0;
  5025. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5026. sd->cart_weight += w;
  5027. clif_updatestatus(sd,SP_CARTINFO);
  5028. return ADDITEM_SUCCESS;
  5029. }
  5030. /*==========================================
  5031. * Delete item on cart for given index.
  5032. *------------------------------------------*/
  5033. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5034. {
  5035. nullpo_retv(sd);
  5036. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5037. sd->cart.u.items_cart[n].amount < amount)
  5038. return;
  5039. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5040. sd->cart.u.items_cart[n].amount -= amount;
  5041. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5042. if(sd->cart.u.items_cart[n].amount <= 0) {
  5043. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5044. sd->cart_num--;
  5045. }
  5046. if(!type) {
  5047. clif_cart_delitem(sd,n,amount);
  5048. clif_updatestatus(sd,SP_CARTINFO);
  5049. }
  5050. }
  5051. /*==========================================
  5052. * Transfer item from inventory to cart.
  5053. *------------------------------------------*/
  5054. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  5055. {
  5056. nullpo_retv(sd);
  5057. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5058. return;
  5059. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5060. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5061. return;
  5062. if( item_data->equipSwitch ){
  5063. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5064. return;
  5065. }
  5066. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5067. if (flag == ADDITEM_SUCCESS)
  5068. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5069. else {
  5070. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5071. clif_additem(sd, idx, amount, 0);
  5072. clif_delitem(sd, idx, amount, 0);
  5073. }
  5074. }
  5075. /*==========================================
  5076. * Get number of item in cart.
  5077. * Return:
  5078. -1 = itemid not found or no amount found
  5079. x = remaining itemid on cart after get
  5080. *------------------------------------------*/
  5081. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  5082. {
  5083. struct item* item_data;
  5084. nullpo_retr(-1, sd);
  5085. item_data = &sd->cart.u.items_cart[idx];
  5086. if( item_data->nameid == 0 || item_data->amount == 0 )
  5087. return -1;
  5088. return item_data->amount - amount;
  5089. }
  5090. /*==========================================
  5091. * Retrieve an item at index idx from cart.
  5092. *------------------------------------------*/
  5093. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5094. {
  5095. nullpo_retv(sd);
  5096. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5097. return;
  5098. struct item *item_data=&sd->cart.u.items_cart[idx];
  5099. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5100. return;
  5101. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5102. if (flag == ADDITEM_SUCCESS)
  5103. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5104. else {
  5105. clif_cart_delitem(sd, idx, amount);
  5106. clif_additem(sd, idx, amount, flag);
  5107. clif_cart_additem(sd, idx, amount);
  5108. }
  5109. }
  5110. /*==========================================
  5111. * Bound Item Check
  5112. * Type:
  5113. * 1 Account Bound
  5114. * 2 Guild Bound
  5115. * 3 Party Bound
  5116. * 4 Character Bound
  5117. *------------------------------------------*/
  5118. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5119. {
  5120. int i = 0, j = 0;
  5121. for(i = 0; i < MAX_INVENTORY; i++) {
  5122. 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) {
  5123. idxlist[j] = i;
  5124. j++;
  5125. }
  5126. }
  5127. return j;
  5128. }
  5129. /*==========================================
  5130. * Display item stolen msg to player sd
  5131. *------------------------------------------*/
  5132. int pc_show_steal(struct block_list *bl,va_list ap)
  5133. {
  5134. struct map_session_data *sd;
  5135. t_itemid itemid;
  5136. struct item_data *item=NULL;
  5137. char output[100];
  5138. sd=va_arg(ap,struct map_session_data *);
  5139. itemid=va_arg(ap,int);
  5140. if((item=itemdb_exists(itemid))==NULL)
  5141. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5142. else
  5143. sprintf(output,"%s stole %s.",sd->status.name,item->ename.c_str());
  5144. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5145. return 0;
  5146. }
  5147. /**
  5148. * Steal an item from bl (mob).
  5149. * @param sd: Player data
  5150. * @param bl: Object to steal from
  5151. * @param skill_lv: Level of skill used
  5152. * @return True on success or false otherwise
  5153. */
  5154. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5155. {
  5156. int i;
  5157. t_itemid itemid;
  5158. double rate;
  5159. unsigned char flag = 0;
  5160. struct status_data *sd_status, *md_status;
  5161. struct mob_data *md;
  5162. struct item tmp_item;
  5163. if(!sd || !bl || bl->type!=BL_MOB)
  5164. return false;
  5165. md = (TBL_MOB *)bl;
  5166. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5167. return false;
  5168. sd_status= status_get_status_data(&sd->bl);
  5169. md_status= status_get_status_data(bl);
  5170. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5171. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5172. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5173. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5174. ) { //Can't steal from
  5175. md->state.steal_flag = UCHAR_MAX;
  5176. return false;
  5177. }
  5178. // base skill success chance (percentual)
  5179. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5180. rate += sd->bonus.add_steal_rate;
  5181. if( rate < 1
  5182. #ifdef RENEWAL
  5183. || rnd()%100 >= rate
  5184. #endif
  5185. )
  5186. return false;
  5187. // Try dropping one item, in the order from first to last possible slot.
  5188. // Droprate is affected by the skill success rate.
  5189. for( i = 0; i < MAX_MOB_DROP; i++ )
  5190. 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
  5191. #ifndef RENEWAL
  5192. * rate/100.
  5193. #endif
  5194. )
  5195. break;
  5196. if( i == MAX_MOB_DROP )
  5197. return false;
  5198. itemid = md->db->dropitem[i].nameid;
  5199. memset(&tmp_item,0,sizeof(tmp_item));
  5200. tmp_item.nameid = itemid;
  5201. tmp_item.amount = 1;
  5202. tmp_item.identify = itemdb_isidentified(itemid);
  5203. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5204. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5205. //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?
  5206. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5207. if(flag) { //Failed to steal due to overweight
  5208. clif_additem(sd,0,0,flag);
  5209. return false;
  5210. }
  5211. if(battle_config.show_steal_in_same_party)
  5212. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5213. //Logs items, Stolen from mobs [Lupus]
  5214. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5215. //A Rare Steal Global Announce by Lupus
  5216. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5217. struct item_data *i_data;
  5218. char message[128];
  5219. i_data = itemdb_search(itemid);
  5220. 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);
  5221. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5222. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5223. }
  5224. return true;
  5225. }
  5226. /*==========================================
  5227. * Stole zeny from bl (mob)
  5228. * return
  5229. * 0 = fail
  5230. * 1 = success
  5231. *------------------------------------------*/
  5232. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5233. {
  5234. int rate, target_lv;
  5235. struct mob_data *md;
  5236. if(!sd || !target || target->type != BL_MOB)
  5237. return 0;
  5238. md = (TBL_MOB*)target;
  5239. target_lv = status_get_lv(target);
  5240. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5241. return 0;
  5242. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5243. if(rnd()%1000 < rate)
  5244. {
  5245. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5246. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5247. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5248. md->state.steal_coin_flag = 1;
  5249. return 1;
  5250. }
  5251. return 0;
  5252. }
  5253. /*==========================================
  5254. * Set's a player position.
  5255. * @param sd
  5256. * @param mapindex
  5257. * @param x
  5258. * @param y
  5259. * @param clrtype
  5260. * @return SETPOS_OK Success
  5261. * SETPOS_MAPINDEX Invalid map index
  5262. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5263. * SETPOS_AUTOTRADE Player is in autotrade state
  5264. *------------------------------------------*/
  5265. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5266. {
  5267. nullpo_retr(SETPOS_OK,sd);
  5268. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5269. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5270. return SETPOS_MAPINDEX;
  5271. }
  5272. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5273. return SETPOS_AUTOTRADE;
  5274. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5275. pc_setstand(sd, true);
  5276. pc_setrestartvalue(sd,1);
  5277. }
  5278. int16 m = map_mapindex2mapid(mapindex);
  5279. struct map_data *mapdata = map_getmapdata(m);
  5280. status_change *sc = status_get_sc(&sd->bl);
  5281. sd->state.changemap = (sd->mapindex != mapindex);
  5282. sd->state.warping = 1;
  5283. sd->state.workinprogress = WIP_DISABLE_NONE;
  5284. sd->state.mail_writing = false;
  5285. sd->state.refineui_open = false;
  5286. if( sd->state.changemap ) { // Misc map-changing settings
  5287. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5288. if (curr_map_instance_id != new_map_instance_id) {
  5289. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5290. instance_delusers(curr_map_instance_id);
  5291. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5292. }
  5293. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5294. instance_addusers(new_map_instance_id);
  5295. }
  5296. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5297. bg_team_leave(sd, false, true);
  5298. sd->state.pmap = sd->bl.m;
  5299. if (sc && sc->count) { // Cancel some map related stuff.
  5300. if (sc->data[SC_JAILED])
  5301. return SETPOS_MAPINDEX; //You may not get out!
  5302. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5303. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5304. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5305. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5306. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5307. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5308. if (sc->data[SC_KNOWLEDGE]) {
  5309. struct status_change_entry *sce = sc->data[SC_KNOWLEDGE];
  5310. if (sce->timer != INVALID_TIMER)
  5311. delete_timer(sce->timer, status_change_timer);
  5312. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5313. }
  5314. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5315. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5316. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5317. status_change_end(&sd->bl, SC_SUHIDE, INVALID_TIMER);
  5318. }
  5319. for(int i = 0; i < EQI_MAX; i++ ) {
  5320. if( sd->equip_index[i] >= 0 )
  5321. if( pc_isequip(sd,sd->equip_index[i]) )
  5322. pc_unequipitem(sd,sd->equip_index[i],2);
  5323. }
  5324. if (battle_config.clear_unit_onwarp&BL_PC)
  5325. skill_clear_unitgroup(&sd->bl);
  5326. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5327. guild_send_dot_remove(sd);
  5328. bg_send_dot_remove(sd);
  5329. if (sd->regen.state.gc)
  5330. sd->regen.state.gc = 0;
  5331. // make sure vending is allowed here
  5332. if (sd->state.vending && mapdata && mapdata->flag[MF_NOVENDING]) {
  5333. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5334. vending_closevending(sd);
  5335. }
  5336. channel_pcquit(sd,4); //quit map chan
  5337. // Remove Cloaked NPC if changing to another map
  5338. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5339. block_list *npc_bl = map_id2bl(*it);
  5340. if (npc_bl && npc_bl->m != m) {
  5341. sd->cloaked_npc.erase(it);
  5342. break;
  5343. }
  5344. }
  5345. }
  5346. if( m < 0 )
  5347. {
  5348. uint32 ip;
  5349. uint16 port;
  5350. struct script_state *st;
  5351. //if can't find any map-servers, just abort setting position.
  5352. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5353. return SETPOS_NO_MAPSERVER;
  5354. if (sd->npc_id){
  5355. npc_event_dequeue(sd,false);
  5356. st = sd->st;
  5357. }else{
  5358. st = nullptr;
  5359. }
  5360. if (sd->bg_id) // Switching map servers, remove from bg
  5361. bg_team_leave(sd, false, true);
  5362. if (sd->state.vending) // Stop vending
  5363. vending_closevending(sd);
  5364. npc_script_event(sd, NPCE_LOGOUT);
  5365. //remove from map, THEN change x/y coordinates
  5366. unit_remove_map_pc(sd,clrtype);
  5367. sd->mapindex = mapindex;
  5368. sd->bl.x=x;
  5369. sd->bl.y=y;
  5370. pc_clean_skilltree(sd);
  5371. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5372. chrif_changemapserver(sd, ip, (short)port);
  5373. //Free session data from this map server [Kevin]
  5374. unit_free_pc(sd);
  5375. if( st ){
  5376. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5377. st->state = END;
  5378. }
  5379. return SETPOS_OK;
  5380. }
  5381. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5382. {
  5383. 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);
  5384. x = y = 0; // make it random
  5385. }
  5386. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5387. int c=0;
  5388. do {
  5389. x = rnd()%(mapdata->xs-2)+1;
  5390. y = rnd()%(mapdata->ys-2)+1;
  5391. c++;
  5392. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5393. 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));
  5394. return SETPOS_OK; //preventing warp
  5395. //break; //allow warp anyway
  5396. }
  5397. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5398. }
  5399. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5400. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5401. vending_closevending(sd);
  5402. }
  5403. if(sd->bl.prev != NULL){
  5404. unit_remove_map_pc(sd,clrtype);
  5405. clif_changemap(sd,m,x,y); // [MouseJstr]
  5406. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5407. sd->state.rewarp = 1;
  5408. sd->mapindex = mapindex;
  5409. sd->bl.m = m;
  5410. sd->bl.x = sd->ud.to_x = x;
  5411. sd->bl.y = sd->ud.to_y = y;
  5412. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5413. { // Increased guild castle regen [Valaris]
  5414. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5415. if(gc && gc->guild_id == sd->status.guild_id)
  5416. sd->regen.state.gc = 1;
  5417. }
  5418. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5419. {
  5420. sd->pd->bl.m = m;
  5421. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5422. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5423. sd->pd->ud.dir = sd->ud.dir;
  5424. }
  5425. if( hom_is_active(sd->hd) )
  5426. {
  5427. sd->hd->bl.m = m;
  5428. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5429. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5430. sd->hd->ud.dir = sd->ud.dir;
  5431. }
  5432. if( sd->md )
  5433. {
  5434. sd->md->bl.m = m;
  5435. sd->md->bl.x = sd->md->ud.to_x = x;
  5436. sd->md->bl.y = sd->md->ud.to_y = y;
  5437. sd->md->ud.dir = sd->ud.dir;
  5438. }
  5439. if( sd->ed ) {
  5440. sd->ed->bl.m = m;
  5441. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5442. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5443. sd->ed->ud.dir = sd->ud.dir;
  5444. }
  5445. pc_cell_basilica(sd);
  5446. //check if we gonna be rewarped [lighta]
  5447. if(npc_check_areanpc(1,m,x,y,0)){
  5448. sd->count_rewarp++;
  5449. }
  5450. else
  5451. sd->count_rewarp = 0;
  5452. return SETPOS_OK;
  5453. }
  5454. /*==========================================
  5455. * Warp player sd to random location on current map.
  5456. * May fail if no walkable cell found (1000 attempts).
  5457. * Return:
  5458. * 0 = Success
  5459. * 1,2,3 = Fail
  5460. *------------------------------------------*/
  5461. char pc_randomwarp(struct map_session_data *sd, clr_type type, bool ignore_mapflag)
  5462. {
  5463. int x,y,i=0;
  5464. nullpo_ret(sd);
  5465. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5466. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  5467. return 3;
  5468. do {
  5469. x = rnd()%(mapdata->xs-2)+1;
  5470. y = rnd()%(mapdata->ys-2)+1;
  5471. } 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);
  5472. if (i < 1000)
  5473. return pc_setpos(sd,mapdata->index,x,y,type);
  5474. return 3;
  5475. }
  5476. /*==========================================
  5477. * Records a memo point at sd's current position
  5478. * pos - entry to replace, (-1: shift oldest entry out)
  5479. *------------------------------------------*/
  5480. bool pc_memo(struct map_session_data* sd, int pos)
  5481. {
  5482. int skill;
  5483. nullpo_ret(sd);
  5484. // check mapflags
  5485. 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) ) {
  5486. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5487. return false;
  5488. }
  5489. // check inputs
  5490. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5491. return false; // invalid input
  5492. // check required skill level
  5493. skill = pc_checkskill(sd, AL_WARP);
  5494. if( skill < 1 ) {
  5495. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5496. return false;
  5497. }
  5498. if( skill < 2 || skill - 2 < pos ) {
  5499. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5500. return false;
  5501. }
  5502. if( pos == -1 )
  5503. {
  5504. uint8 i;
  5505. // prevent memo-ing the same map multiple times
  5506. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5507. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5508. pos = 0;
  5509. }
  5510. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5511. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5512. return false;
  5513. }
  5514. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5515. sd->status.memo_point[pos].x = sd->bl.x;
  5516. sd->status.memo_point[pos].y = sd->bl.y;
  5517. clif_skill_memomessage(sd, 0);
  5518. return true;
  5519. }
  5520. //
  5521. // Skills
  5522. //
  5523. /**
  5524. * Get the skill current cooldown for player.
  5525. * (get the db base cooldown for skill + player specific cooldown)
  5526. * @param sd : player pointer
  5527. * @param id : skill id
  5528. * @param lv : skill lv
  5529. * @return player skill cooldown
  5530. */
  5531. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5532. if (skill_id == SJ_NOVAEXPLOSING) {
  5533. struct status_change *sc = status_get_sc(&sd->bl);
  5534. if (sc && sc->data[SC_DIMENSION])
  5535. return 0;
  5536. }
  5537. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5538. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  5539. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5540. for (auto &it : sd->skillcooldown) {
  5541. if (it.id == skill_id) {
  5542. cooldown += it.val;
  5543. break;
  5544. }
  5545. }
  5546. return max(0, cooldown);
  5547. }
  5548. /*==========================================
  5549. * Return player sd skill_lv learned for given skill
  5550. *------------------------------------------*/
  5551. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5552. {
  5553. uint16 idx = 0;
  5554. if (sd == NULL)
  5555. return 0;
  5556. if ((idx = skill_get_index(skill_id)) == 0) {
  5557. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5558. return 0;
  5559. }
  5560. if (SKILL_CHK_GUILD(skill_id) ) {
  5561. struct guild *g;
  5562. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5563. return guild_checkskill(g,skill_id);
  5564. return 0;
  5565. }
  5566. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5567. }
  5568. /**
  5569. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5570. * @param sd: Player data
  5571. * @param type: Summoner Power Type
  5572. * @return Skill points invested
  5573. */
  5574. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5575. if (sd == nullptr)
  5576. return 0;
  5577. uint8 count = 0;
  5578. switch (type) {
  5579. case SUMMONER_POWER_SEA:
  5580. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5581. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  5582. break;
  5583. case SUMMONER_POWER_LAND:
  5584. 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) +
  5585. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  5586. break;
  5587. case SUMMONER_POWER_LIFE:
  5588. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  5589. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  5590. break;
  5591. }
  5592. return count;
  5593. }
  5594. /**
  5595. * Check if we still have the correct weapon to continue the skill (actually status)
  5596. * If not ending it
  5597. * @param sd
  5598. * @return 0:error, 1:check done
  5599. */
  5600. static void pc_checkallowskill(struct map_session_data *sd)
  5601. {
  5602. const enum sc_type scw_list[] = {
  5603. SC_TWOHANDQUICKEN,
  5604. SC_ONEHAND,
  5605. SC_AURABLADE,
  5606. SC_PARRYING,
  5607. SC_SPEARQUICKEN,
  5608. SC_ADRENALINE,
  5609. SC_ADRENALINE2,
  5610. SC_DANCING,
  5611. SC_GATLINGFEVER,
  5612. };
  5613. uint8 i;
  5614. nullpo_retv(sd);
  5615. if(!sd->sc.count)
  5616. return;
  5617. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5618. { // Skills requiring specific weapon types
  5619. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5620. continue;
  5621. if(sd->sc.data[scw_list[i]] &&
  5622. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5623. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5624. }
  5625. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5626. // Spurt requires bare hands (feet, in fact xD)
  5627. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5628. if(sd->status.shield <= 0) { // Skills requiring a shield
  5629. const enum sc_type scs_list[] = {
  5630. SC_AUTOGUARD,
  5631. SC_DEFENDER,
  5632. SC_REFLECTSHIELD,
  5633. SC_REFLECTDAMAGE
  5634. };
  5635. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5636. if(sd->sc.data[scs_list[i]])
  5637. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5638. }
  5639. }
  5640. /*==========================================
  5641. * Return equipped index of item on player sd at pos
  5642. * Return
  5643. * -1 : Nothing equipped
  5644. * idx : (this index could be used in inventory to found item_data)
  5645. *------------------------------------------*/
  5646. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5647. {
  5648. uint8 i;
  5649. nullpo_retr(-1, sd);
  5650. for(i=0;i<EQI_MAX;i++){
  5651. if(pos & equip_bitmask[i]){
  5652. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5653. // Check all if any match is found
  5654. continue;
  5655. }
  5656. return sd->equip_index[i];
  5657. }
  5658. }
  5659. return -1;
  5660. }
  5661. /*==========================================
  5662. * Check if sd has nameid equipped somewhere
  5663. * @sd : the player session
  5664. * @nameid : id of the item to check
  5665. * @min : : see pc.hpp enum equip_index from ? to @max
  5666. * @max : see pc.hpp enum equip_index for @min to ?
  5667. * -return true,false
  5668. *------------------------------------------*/
  5669. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  5670. {
  5671. int i;
  5672. for(i = min; i < max; i++) {
  5673. if(equip_bitmask[i]) {
  5674. int idx = sd->equip_index[i];
  5675. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5676. return true;
  5677. }
  5678. }
  5679. return false;
  5680. }
  5681. /*==========================================
  5682. * Convert's from the client's lame Job ID system
  5683. * to the map server's 'makes sense' system. [Skotlex]
  5684. *------------------------------------------*/
  5685. int pc_jobid2mapid(unsigned short b_class)
  5686. {
  5687. switch(b_class)
  5688. {
  5689. //Novice And 1-1 Jobs
  5690. case JOB_NOVICE: return MAPID_NOVICE;
  5691. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5692. case JOB_MAGE: return MAPID_MAGE;
  5693. case JOB_ARCHER: return MAPID_ARCHER;
  5694. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5695. case JOB_MERCHANT: return MAPID_MERCHANT;
  5696. case JOB_THIEF: return MAPID_THIEF;
  5697. case JOB_TAEKWON: return MAPID_TAEKWON;
  5698. case JOB_WEDDING: return MAPID_WEDDING;
  5699. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5700. case JOB_NINJA: return MAPID_NINJA;
  5701. case JOB_XMAS: return MAPID_XMAS;
  5702. case JOB_SUMMER: return MAPID_SUMMER;
  5703. case JOB_HANBOK: return MAPID_HANBOK;
  5704. case JOB_GANGSI: return MAPID_GANGSI;
  5705. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5706. case JOB_SUMMER2: return MAPID_SUMMER2;
  5707. //2-1 Jobs
  5708. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5709. case JOB_KNIGHT: return MAPID_KNIGHT;
  5710. case JOB_WIZARD: return MAPID_WIZARD;
  5711. case JOB_HUNTER: return MAPID_HUNTER;
  5712. case JOB_PRIEST: return MAPID_PRIEST;
  5713. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5714. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5715. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5716. case JOB_KAGEROU:
  5717. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5718. case JOB_REBELLION: return MAPID_REBELLION;
  5719. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5720. //2-2 Jobs
  5721. case JOB_CRUSADER: return MAPID_CRUSADER;
  5722. case JOB_SAGE: return MAPID_SAGE;
  5723. case JOB_BARD:
  5724. case JOB_DANCER: return MAPID_BARDDANCER;
  5725. case JOB_MONK: return MAPID_MONK;
  5726. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5727. case JOB_ROGUE: return MAPID_ROGUE;
  5728. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5729. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5730. //Trans Novice And Trans 1-1 Jobs
  5731. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5732. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5733. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5734. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5735. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5736. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5737. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5738. //Trans 2-1 Jobs
  5739. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5740. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5741. case JOB_SNIPER: return MAPID_SNIPER;
  5742. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5743. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5744. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5745. //Trans 2-2 Jobs
  5746. case JOB_PALADIN: return MAPID_PALADIN;
  5747. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5748. case JOB_CLOWN:
  5749. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5750. case JOB_CHAMPION: return MAPID_CHAMPION;
  5751. case JOB_CREATOR: return MAPID_CREATOR;
  5752. case JOB_STALKER: return MAPID_STALKER;
  5753. //Baby Novice And Baby 1-1 Jobs
  5754. case JOB_BABY: return MAPID_BABY;
  5755. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5756. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5757. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5758. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5759. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5760. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5761. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5762. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5763. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5764. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5765. //Baby 2-1 Jobs
  5766. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5767. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5768. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5769. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5770. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5771. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5772. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5773. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5774. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5775. case JOB_BABY_KAGEROU:
  5776. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5777. //Baby 2-2 Jobs
  5778. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5779. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5780. case JOB_BABY_BARD:
  5781. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5782. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5783. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5784. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5785. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5786. //3-1 Jobs
  5787. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5788. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5789. case JOB_WARLOCK: return MAPID_WARLOCK;
  5790. case JOB_RANGER: return MAPID_RANGER;
  5791. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5792. case JOB_MECHANIC: return MAPID_MECHANIC;
  5793. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5794. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5795. //3-2 Jobs
  5796. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5797. case JOB_SORCERER: return MAPID_SORCERER;
  5798. case JOB_MINSTREL:
  5799. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5800. case JOB_SURA: return MAPID_SURA;
  5801. case JOB_GENETIC: return MAPID_GENETIC;
  5802. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5803. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5804. //Trans 3-1 Jobs
  5805. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5806. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5807. case JOB_RANGER_T: return MAPID_RANGER_T;
  5808. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5809. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5810. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5811. //Trans 3-2 Jobs
  5812. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5813. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5814. case JOB_MINSTREL_T:
  5815. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5816. case JOB_SURA_T: return MAPID_SURA_T;
  5817. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5818. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5819. //Baby 3-1 Jobs
  5820. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5821. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5822. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5823. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5824. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5825. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5826. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5827. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5828. //Baby 3-2 Jobs
  5829. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5830. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5831. case JOB_BABY_MINSTREL:
  5832. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5833. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5834. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5835. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5836. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5837. //Doram Jobs
  5838. case JOB_SUMMONER: return MAPID_SUMMONER;
  5839. default:
  5840. return -1;
  5841. }
  5842. }
  5843. //Reverts the map-style class id to the client-style one.
  5844. int pc_mapid2jobid(unsigned short class_, int sex)
  5845. {
  5846. switch(class_) {
  5847. //Novice And 1-1 Jobs
  5848. case MAPID_NOVICE: return JOB_NOVICE;
  5849. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5850. case MAPID_MAGE: return JOB_MAGE;
  5851. case MAPID_ARCHER: return JOB_ARCHER;
  5852. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5853. case MAPID_MERCHANT: return JOB_MERCHANT;
  5854. case MAPID_THIEF: return JOB_THIEF;
  5855. case MAPID_TAEKWON: return JOB_TAEKWON;
  5856. case MAPID_WEDDING: return JOB_WEDDING;
  5857. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5858. case MAPID_NINJA: return JOB_NINJA;
  5859. case MAPID_XMAS: return JOB_XMAS;
  5860. case MAPID_SUMMER: return JOB_SUMMER;
  5861. case MAPID_HANBOK: return JOB_HANBOK;
  5862. case MAPID_GANGSI: return JOB_GANGSI;
  5863. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5864. case MAPID_SUMMER2: return JOB_SUMMER2;
  5865. //2-1 Jobs
  5866. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5867. case MAPID_KNIGHT: return JOB_KNIGHT;
  5868. case MAPID_WIZARD: return JOB_WIZARD;
  5869. case MAPID_HUNTER: return JOB_HUNTER;
  5870. case MAPID_PRIEST: return JOB_PRIEST;
  5871. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5872. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5873. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5874. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5875. case MAPID_REBELLION: return JOB_REBELLION;
  5876. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5877. //2-2 Jobs
  5878. case MAPID_CRUSADER: return JOB_CRUSADER;
  5879. case MAPID_SAGE: return JOB_SAGE;
  5880. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5881. case MAPID_MONK: return JOB_MONK;
  5882. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5883. case MAPID_ROGUE: return JOB_ROGUE;
  5884. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5885. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5886. //Trans Novice And Trans 2-1 Jobs
  5887. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5888. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5889. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5890. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5891. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5892. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5893. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5894. //Trans 2-1 Jobs
  5895. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5896. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5897. case MAPID_SNIPER: return JOB_SNIPER;
  5898. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5899. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5900. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5901. //Trans 2-2 Jobs
  5902. case MAPID_PALADIN: return JOB_PALADIN;
  5903. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5904. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5905. case MAPID_CHAMPION: return JOB_CHAMPION;
  5906. case MAPID_CREATOR: return JOB_CREATOR;
  5907. case MAPID_STALKER: return JOB_STALKER;
  5908. //Baby Novice And Baby 1-1 Jobs
  5909. case MAPID_BABY: return JOB_BABY;
  5910. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5911. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5912. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5913. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5914. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5915. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5916. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5917. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5918. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5919. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5920. //Baby 2-1 Jobs
  5921. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5922. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5923. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5924. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5925. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5926. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5927. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5928. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5929. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5930. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5931. //Baby 2-2 Jobs
  5932. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5933. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5934. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5935. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5936. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5937. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5938. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5939. //3-1 Jobs
  5940. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5941. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5942. case MAPID_WARLOCK: return JOB_WARLOCK;
  5943. case MAPID_RANGER: return JOB_RANGER;
  5944. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5945. case MAPID_MECHANIC: return JOB_MECHANIC;
  5946. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5947. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5948. //3-2 Jobs
  5949. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5950. case MAPID_SORCERER: return JOB_SORCERER;
  5951. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5952. case MAPID_SURA: return JOB_SURA;
  5953. case MAPID_GENETIC: return JOB_GENETIC;
  5954. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5955. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5956. //Trans 3-1 Jobs
  5957. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5958. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5959. case MAPID_RANGER_T: return JOB_RANGER_T;
  5960. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5961. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5962. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5963. //Trans 3-2 Jobs
  5964. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5965. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5966. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5967. case MAPID_SURA_T: return JOB_SURA_T;
  5968. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5969. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5970. //Baby 3-1 Jobs
  5971. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5972. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5973. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5974. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5975. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5976. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5977. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5978. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5979. //Baby 3-2 Jobs
  5980. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5981. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5982. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5983. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5984. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5985. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5986. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5987. //Doram Jobs
  5988. case MAPID_SUMMONER: return JOB_SUMMONER;
  5989. default:
  5990. return -1;
  5991. }
  5992. }
  5993. /*====================================================
  5994. * This function return the name of the job (by [Yor])
  5995. *----------------------------------------------------*/
  5996. const char* job_name(int class_)
  5997. {
  5998. switch (class_) {
  5999. case JOB_NOVICE:
  6000. case JOB_SWORDMAN:
  6001. case JOB_MAGE:
  6002. case JOB_ARCHER:
  6003. case JOB_ACOLYTE:
  6004. case JOB_MERCHANT:
  6005. case JOB_THIEF:
  6006. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6007. case JOB_KNIGHT:
  6008. case JOB_PRIEST:
  6009. case JOB_WIZARD:
  6010. case JOB_BLACKSMITH:
  6011. case JOB_HUNTER:
  6012. case JOB_ASSASSIN:
  6013. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6014. case JOB_KNIGHT2:
  6015. return msg_txt(NULL,557);
  6016. case JOB_CRUSADER:
  6017. case JOB_MONK:
  6018. case JOB_SAGE:
  6019. case JOB_ROGUE:
  6020. case JOB_ALCHEMIST:
  6021. case JOB_BARD:
  6022. case JOB_DANCER:
  6023. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6024. case JOB_CRUSADER2:
  6025. return msg_txt(NULL,563);
  6026. case JOB_WEDDING:
  6027. case JOB_SUPER_NOVICE:
  6028. case JOB_GUNSLINGER:
  6029. case JOB_NINJA:
  6030. case JOB_XMAS:
  6031. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6032. case JOB_SUMMER:
  6033. case JOB_SUMMER2:
  6034. return msg_txt(NULL,621);
  6035. case JOB_HANBOK:
  6036. return msg_txt(NULL,694);
  6037. case JOB_OKTOBERFEST:
  6038. return msg_txt(NULL,696);
  6039. case JOB_NOVICE_HIGH:
  6040. case JOB_SWORDMAN_HIGH:
  6041. case JOB_MAGE_HIGH:
  6042. case JOB_ARCHER_HIGH:
  6043. case JOB_ACOLYTE_HIGH:
  6044. case JOB_MERCHANT_HIGH:
  6045. case JOB_THIEF_HIGH:
  6046. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6047. case JOB_LORD_KNIGHT:
  6048. case JOB_HIGH_PRIEST:
  6049. case JOB_HIGH_WIZARD:
  6050. case JOB_WHITESMITH:
  6051. case JOB_SNIPER:
  6052. case JOB_ASSASSIN_CROSS:
  6053. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6054. case JOB_LORD_KNIGHT2:
  6055. return msg_txt(NULL,582);
  6056. case JOB_PALADIN:
  6057. case JOB_CHAMPION:
  6058. case JOB_PROFESSOR:
  6059. case JOB_STALKER:
  6060. case JOB_CREATOR:
  6061. case JOB_CLOWN:
  6062. case JOB_GYPSY:
  6063. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6064. case JOB_PALADIN2:
  6065. return msg_txt(NULL,588);
  6066. case JOB_BABY:
  6067. case JOB_BABY_SWORDMAN:
  6068. case JOB_BABY_MAGE:
  6069. case JOB_BABY_ARCHER:
  6070. case JOB_BABY_ACOLYTE:
  6071. case JOB_BABY_MERCHANT:
  6072. case JOB_BABY_THIEF:
  6073. return msg_txt(NULL,595 - JOB_BABY + class_);
  6074. case JOB_BABY_KNIGHT:
  6075. case JOB_BABY_PRIEST:
  6076. case JOB_BABY_WIZARD:
  6077. case JOB_BABY_BLACKSMITH:
  6078. case JOB_BABY_HUNTER:
  6079. case JOB_BABY_ASSASSIN:
  6080. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6081. case JOB_BABY_KNIGHT2:
  6082. return msg_txt(NULL,602);
  6083. case JOB_BABY_CRUSADER:
  6084. case JOB_BABY_MONK:
  6085. case JOB_BABY_SAGE:
  6086. case JOB_BABY_ROGUE:
  6087. case JOB_BABY_ALCHEMIST:
  6088. case JOB_BABY_BARD:
  6089. case JOB_BABY_DANCER:
  6090. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6091. case JOB_BABY_CRUSADER2:
  6092. return msg_txt(NULL,608);
  6093. case JOB_SUPER_BABY:
  6094. return msg_txt(NULL,615);
  6095. case JOB_TAEKWON:
  6096. return msg_txt(NULL,616);
  6097. case JOB_STAR_GLADIATOR:
  6098. case JOB_STAR_GLADIATOR2:
  6099. return msg_txt(NULL,617);
  6100. case JOB_SOUL_LINKER:
  6101. return msg_txt(NULL,618);
  6102. case JOB_GANGSI:
  6103. case JOB_DEATH_KNIGHT:
  6104. case JOB_DARK_COLLECTOR:
  6105. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6106. case JOB_RUNE_KNIGHT:
  6107. case JOB_WARLOCK:
  6108. case JOB_RANGER:
  6109. case JOB_ARCH_BISHOP:
  6110. case JOB_MECHANIC:
  6111. case JOB_GUILLOTINE_CROSS:
  6112. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6113. case JOB_RUNE_KNIGHT_T:
  6114. case JOB_WARLOCK_T:
  6115. case JOB_RANGER_T:
  6116. case JOB_ARCH_BISHOP_T:
  6117. case JOB_MECHANIC_T:
  6118. case JOB_GUILLOTINE_CROSS_T:
  6119. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6120. case JOB_ROYAL_GUARD:
  6121. case JOB_SORCERER:
  6122. case JOB_MINSTREL:
  6123. case JOB_WANDERER:
  6124. case JOB_SURA:
  6125. case JOB_GENETIC:
  6126. case JOB_SHADOW_CHASER:
  6127. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6128. case JOB_ROYAL_GUARD_T:
  6129. case JOB_SORCERER_T:
  6130. case JOB_MINSTREL_T:
  6131. case JOB_WANDERER_T:
  6132. case JOB_SURA_T:
  6133. case JOB_GENETIC_T:
  6134. case JOB_SHADOW_CHASER_T:
  6135. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6136. case JOB_RUNE_KNIGHT2:
  6137. case JOB_RUNE_KNIGHT_T2:
  6138. return msg_txt(NULL,625);
  6139. case JOB_ROYAL_GUARD2:
  6140. case JOB_ROYAL_GUARD_T2:
  6141. return msg_txt(NULL,631);
  6142. case JOB_RANGER2:
  6143. case JOB_RANGER_T2:
  6144. return msg_txt(NULL,627);
  6145. case JOB_MECHANIC2:
  6146. case JOB_MECHANIC_T2:
  6147. return msg_txt(NULL,629);
  6148. case JOB_BABY_RUNE_KNIGHT:
  6149. case JOB_BABY_WARLOCK:
  6150. case JOB_BABY_RANGER:
  6151. case JOB_BABY_ARCH_BISHOP:
  6152. case JOB_BABY_MECHANIC:
  6153. case JOB_BABY_GUILLOTINE_CROSS:
  6154. case JOB_BABY_ROYAL_GUARD:
  6155. case JOB_BABY_SORCERER:
  6156. case JOB_BABY_MINSTREL:
  6157. case JOB_BABY_WANDERER:
  6158. case JOB_BABY_SURA:
  6159. case JOB_BABY_GENETIC:
  6160. case JOB_BABY_SHADOW_CHASER:
  6161. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6162. case JOB_BABY_RUNE_KNIGHT2:
  6163. return msg_txt(NULL,638);
  6164. case JOB_BABY_ROYAL_GUARD2:
  6165. return msg_txt(NULL,644);
  6166. case JOB_BABY_RANGER2:
  6167. return msg_txt(NULL,640);
  6168. case JOB_BABY_MECHANIC2:
  6169. return msg_txt(NULL,642);
  6170. case JOB_SUPER_NOVICE_E:
  6171. case JOB_SUPER_BABY_E:
  6172. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6173. case JOB_KAGEROU:
  6174. case JOB_OBORO:
  6175. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6176. case JOB_REBELLION:
  6177. return msg_txt(NULL,695);
  6178. case JOB_SUMMONER:
  6179. return msg_txt(NULL,697);
  6180. case JOB_BABY_SUMMONER:
  6181. return msg_txt(NULL,698);
  6182. case JOB_BABY_NINJA:
  6183. return msg_txt(NULL,699);
  6184. case JOB_BABY_KAGEROU:
  6185. case JOB_BABY_OBORO:
  6186. case JOB_BABY_TAEKWON:
  6187. case JOB_BABY_STAR_GLADIATOR:
  6188. case JOB_BABY_SOUL_LINKER:
  6189. case JOB_BABY_GUNSLINGER:
  6190. case JOB_BABY_REBELLION:
  6191. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6192. case JOB_BABY_STAR_GLADIATOR2:
  6193. return msg_txt(NULL,756);
  6194. case JOB_STAR_EMPEROR:
  6195. case JOB_SOUL_REAPER:
  6196. case JOB_BABY_STAR_EMPEROR:
  6197. case JOB_BABY_SOUL_REAPER:
  6198. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6199. case JOB_STAR_EMPEROR2:
  6200. return msg_txt(NULL,782);
  6201. case JOB_BABY_STAR_EMPEROR2:
  6202. return msg_txt(NULL,784);
  6203. default:
  6204. return msg_txt(NULL,655);
  6205. }
  6206. }
  6207. /*====================================================
  6208. * Timered function to make id follow a target.
  6209. * @id = bl.id (player only atm)
  6210. * target is define in sd->followtarget (bl.id)
  6211. * used by pc_follow
  6212. *----------------------------------------------------*/
  6213. TIMER_FUNC(pc_follow_timer){
  6214. struct map_session_data *sd;
  6215. struct block_list *tbl;
  6216. sd = map_id2sd(id);
  6217. nullpo_ret(sd);
  6218. if (sd->followtimer != tid){
  6219. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6220. sd->followtimer = INVALID_TIMER;
  6221. return 0;
  6222. }
  6223. sd->followtimer = INVALID_TIMER;
  6224. tbl = map_id2bl(sd->followtarget);
  6225. if (tbl == NULL || pc_isdead(sd))
  6226. {
  6227. pc_stop_following(sd);
  6228. return 0;
  6229. }
  6230. // either player or target is currently detached from map blocks (could be teleporting),
  6231. // but still connected to this map, so we'll just increment the timer and check back later
  6232. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6233. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6234. {
  6235. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6236. if (!check_distance_bl(&sd->bl, tbl, 5))
  6237. unit_walktobl(&sd->bl, tbl, 5, 0);
  6238. } else
  6239. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6240. }
  6241. sd->followtimer = add_timer(
  6242. tick + 1000, // increase time a bit to loosen up map's load
  6243. pc_follow_timer, sd->bl.id, 0);
  6244. return 0;
  6245. }
  6246. int pc_stop_following (struct map_session_data *sd)
  6247. {
  6248. nullpo_ret(sd);
  6249. if (sd->followtimer != INVALID_TIMER) {
  6250. delete_timer(sd->followtimer,pc_follow_timer);
  6251. sd->followtimer = INVALID_TIMER;
  6252. }
  6253. sd->followtarget = -1;
  6254. sd->ud.target_to = 0;
  6255. unit_stop_walking(&sd->bl, 1);
  6256. return 0;
  6257. }
  6258. int pc_follow(struct map_session_data *sd,int target_id)
  6259. {
  6260. struct block_list *bl = map_id2bl(target_id);
  6261. if (bl == NULL /*|| bl->type != BL_PC*/)
  6262. return 1;
  6263. if (sd->followtimer != INVALID_TIMER)
  6264. pc_stop_following(sd);
  6265. sd->followtarget = target_id;
  6266. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6267. return 0;
  6268. }
  6269. int pc_checkbaselevelup(struct map_session_data *sd) {
  6270. t_exp next = pc_nextbaseexp(sd);
  6271. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6272. return 0;
  6273. uint32 base_level = sd->status.base_level;
  6274. do {
  6275. sd->status.base_exp -= next;
  6276. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6277. 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 )
  6278. sd->status.base_exp = next-1;
  6279. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level++);
  6280. if( pc_is_maxbaselv(sd) ){
  6281. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6282. break;
  6283. }
  6284. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6285. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6286. status_calc_pet(sd->pd,SCO_NONE);
  6287. clif_updatestatus(sd,SP_STATUSPOINT);
  6288. clif_updatestatus(sd,SP_BASELEVEL);
  6289. clif_updatestatus(sd,SP_BASEEXP);
  6290. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6291. status_calc_pc(sd,SCO_FORCE);
  6292. status_percent_heal(&sd->bl,100,100);
  6293. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6294. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6295. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6296. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6297. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6298. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6299. if (sd->state.snovice_dead_flag)
  6300. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6301. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6302. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6303. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6304. }
  6305. clif_misceffect(&sd->bl,0);
  6306. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6307. if(sd->status.party_id)
  6308. party_send_levelup(sd);
  6309. pc_baselevelchanged(sd);
  6310. for (; base_level <= sd->status.base_level; base_level++) {
  6311. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6312. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6313. }
  6314. return 1;
  6315. }
  6316. void pc_baselevelchanged(struct map_session_data *sd) {
  6317. uint8 i;
  6318. for( i = 0; i < EQI_MAX; i++ ) {
  6319. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6320. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6321. pc_unequipitem(sd, sd->equip_index[i], 3);
  6322. }
  6323. }
  6324. pc_show_questinfo(sd);
  6325. }
  6326. int pc_checkjoblevelup(struct map_session_data *sd)
  6327. {
  6328. t_exp next = pc_nextjobexp(sd);
  6329. nullpo_ret(sd);
  6330. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6331. return 0;
  6332. uint32 job_level = sd->status.job_level;
  6333. do {
  6334. sd->status.job_exp -= next;
  6335. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6336. 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 )
  6337. sd->status.job_exp = next-1;
  6338. sd->status.job_level ++;
  6339. sd->status.skill_point ++;
  6340. if( pc_is_maxjoblv(sd) ){
  6341. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6342. break;
  6343. }
  6344. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6345. clif_updatestatus(sd,SP_JOBLEVEL);
  6346. clif_updatestatus(sd,SP_JOBEXP);
  6347. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6348. clif_updatestatus(sd,SP_SKILLPOINT);
  6349. status_calc_pc(sd,SCO_FORCE);
  6350. clif_misceffect(&sd->bl,1);
  6351. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6352. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6353. npc_script_event(sd, NPCE_JOBLVUP);
  6354. for (; job_level <= sd->status.job_level; job_level++)
  6355. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6356. pc_show_questinfo(sd);
  6357. return 1;
  6358. }
  6359. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6360. * @param sd Player
  6361. * @param base_exp Base EXP before peronal bonuses
  6362. * @param job_exp Job EXP before peronal bonuses
  6363. * @param src Block list that affecting the exp calculation
  6364. */
  6365. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6366. {
  6367. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6368. if (src) {
  6369. struct status_data *status = status_get_status_data(src);
  6370. if( sd->indexed_bonus.expaddrace[status->race] )
  6371. bonus += sd->indexed_bonus.expaddrace[status->race];
  6372. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  6373. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  6374. if( sd->indexed_bonus.expaddclass[status->class_] )
  6375. bonus += sd->indexed_bonus.expaddclass[status->class_];
  6376. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  6377. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  6378. if (battle_config.pk_mode &&
  6379. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6380. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6381. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6382. vip_bonus_base = battle_config.vip_base_exp_increase;
  6383. vip_bonus_job = battle_config.vip_job_exp_increase;
  6384. }
  6385. }
  6386. // Give EXPBOOST for quests even if src is NULL.
  6387. if (sd->sc.data[SC_EXPBOOST]) {
  6388. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6389. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6390. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6391. }
  6392. if (*base_exp) {
  6393. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6394. *base_exp = cap_value(exp, 1, MAX_EXP);
  6395. }
  6396. // Give JEXPBOOST for quests even if src is NULL.
  6397. if (sd->sc.data[SC_JEXPBOOST])
  6398. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6399. if (*job_exp) {
  6400. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6401. *job_exp = cap_value(exp, 1, MAX_EXP);
  6402. }
  6403. return;
  6404. }
  6405. /**
  6406. * Show EXP gained by player in percentage by @showexp
  6407. * @param sd Player
  6408. * @param base_exp Base EXP gained/loss
  6409. * @param next_base_exp Base EXP needed for next base level
  6410. * @param job_exp Job EXP gained/loss
  6411. * @param next_job_exp Job EXP needed for next job level
  6412. * @param lost True:EXP penalty, lose EXP
  6413. **/
  6414. 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) {
  6415. char output[CHAT_SIZE_MAX];
  6416. nullpo_retv(sd);
  6417. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6418. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6419. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6420. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6421. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6422. }
  6423. /**
  6424. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6425. * @param sd Player
  6426. * @param src EXP source
  6427. * @param base_exp Base EXP gained
  6428. * @param base_exp Job EXP gained
  6429. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6430. * @return
  6431. **/
  6432. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6433. {
  6434. t_exp nextb = 0, nextj = 0;
  6435. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6436. nullpo_retv(sd);
  6437. if(sd->bl.prev == NULL || pc_isdead(sd))
  6438. return;
  6439. if (!(exp_flag&2)) {
  6440. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6441. return; // no exp on pvp maps
  6442. if (sd->status.guild_id>0)
  6443. base_exp -= guild_payexp(sd,base_exp);
  6444. }
  6445. flag = ((base_exp) ? 1 : 0) |
  6446. ((job_exp) ? 2 : 0) |
  6447. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6448. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6449. if (!(exp_flag&2))
  6450. pc_calcexp(sd, &base_exp, &job_exp, src);
  6451. nextb = pc_nextbaseexp(sd);
  6452. nextj = pc_nextjobexp(sd);
  6453. if (flag&4){
  6454. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6455. base_exp = 0;
  6456. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6457. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6458. }
  6459. if (flag&8){
  6460. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6461. job_exp = 0;
  6462. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6463. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6464. }
  6465. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6466. //Note that this value should never be greater than the original
  6467. //therefore no overflow checks are needed. [Skotlex]
  6468. if (nextb > 0) {
  6469. float nextbp = (float) base_exp / (float) nextb;
  6470. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6471. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6472. }
  6473. if (nextj > 0) {
  6474. float nextjp = (float) job_exp / (float) nextj;
  6475. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6476. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6477. }
  6478. }
  6479. // Give EXP for Base Level
  6480. if (base_exp) {
  6481. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6482. if (!pc_checkbaselevelup(sd))
  6483. clif_updatestatus(sd,SP_BASEEXP);
  6484. }
  6485. // Give EXP for Job Level
  6486. if (job_exp) {
  6487. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6488. if (!pc_checkjoblevelup(sd))
  6489. clif_updatestatus(sd,SP_JOBEXP);
  6490. }
  6491. if (flag&1)
  6492. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6493. if (flag&2)
  6494. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6495. if (sd->state.showexp && (base_exp || job_exp))
  6496. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6497. }
  6498. /**
  6499. * Lost Base/Job EXP from a player
  6500. * @param sd Player
  6501. * @param base_exp Base EXP lost
  6502. * @param job_exp Job EXP lost
  6503. **/
  6504. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  6505. nullpo_retv(sd);
  6506. if (base_exp) {
  6507. base_exp = u64min(sd->status.base_exp, base_exp);
  6508. sd->status.base_exp -= base_exp;
  6509. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6510. clif_updatestatus(sd, SP_BASEEXP);
  6511. }
  6512. if (job_exp) {
  6513. job_exp = u64min(sd->status.job_exp, job_exp);
  6514. sd->status.job_exp -= job_exp;
  6515. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6516. clif_updatestatus(sd, SP_JOBEXP);
  6517. }
  6518. if (sd->state.showexp && (base_exp || job_exp))
  6519. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6520. }
  6521. /**
  6522. * Returns max base level for this character's class.
  6523. * @param class_: Player's class
  6524. * @return Max Base Level
  6525. */
  6526. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6527. return job_info[pc_class2idx(class_)].max_level[0];
  6528. }
  6529. /**
  6530. * Returns max base level for this character.
  6531. * @param sd Player
  6532. * @return Max Base Level
  6533. **/
  6534. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6535. return pc_class_maxbaselv(sd->status.class_);
  6536. }
  6537. /**
  6538. * Returns max job level for this character's class.
  6539. * @param class_: Player's class
  6540. * @return Max Job Level
  6541. */
  6542. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6543. return job_info[pc_class2idx(class_)].max_level[1];
  6544. }
  6545. /**
  6546. * Returns max job level for this character.
  6547. * @param sd Player
  6548. * @return Max Job Level
  6549. **/
  6550. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6551. return pc_class_maxjoblv(sd->status.class_);
  6552. }
  6553. /**
  6554. * Check if player is reached max base level
  6555. * @param sd
  6556. * @return True if reached max level
  6557. **/
  6558. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6559. nullpo_retr(false, sd);
  6560. return (sd->status.base_level >= pc_maxbaselv(sd));
  6561. }
  6562. /**
  6563. * Check if player is reached max base level
  6564. * @param sd
  6565. * @return True if reached max level
  6566. **/
  6567. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6568. nullpo_retr(false, sd);
  6569. return (sd->status.job_level >= pc_maxjoblv(sd));
  6570. }
  6571. /**
  6572. * Base exp needed for player to level up.
  6573. * @param sd
  6574. * @return Base EXP needed for next base level
  6575. **/
  6576. t_exp pc_nextbaseexp(struct map_session_data *sd){
  6577. nullpo_ret(sd);
  6578. if (sd->status.base_level == 0) // Is this something that possible?
  6579. return 0;
  6580. if (pc_is_maxbaselv(sd))
  6581. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6582. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6583. }
  6584. /**
  6585. * Job exp needed for player to level up.
  6586. * @param sd
  6587. * @return Job EXP needed for next job level
  6588. **/
  6589. t_exp pc_nextjobexp(struct map_session_data *sd){
  6590. nullpo_ret(sd);
  6591. if (sd->status.job_level == 0) // Is this something that possible?
  6592. return 0;
  6593. if (pc_is_maxjoblv(sd))
  6594. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6595. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6596. }
  6597. /// Returns the value of the specified stat.
  6598. static int pc_getstat(struct map_session_data* sd, int type)
  6599. {
  6600. nullpo_retr(-1, sd);
  6601. switch( type ) {
  6602. case SP_STR: return sd->status.str;
  6603. case SP_AGI: return sd->status.agi;
  6604. case SP_VIT: return sd->status.vit;
  6605. case SP_INT: return sd->status.int_;
  6606. case SP_DEX: return sd->status.dex;
  6607. case SP_LUK: return sd->status.luk;
  6608. default:
  6609. return -1;
  6610. }
  6611. }
  6612. /// Sets the specified stat to the specified value.
  6613. /// Returns the new value.
  6614. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6615. {
  6616. nullpo_retr(-1, sd);
  6617. switch( type ) {
  6618. case SP_STR: sd->status.str = val; break;
  6619. case SP_AGI: sd->status.agi = val; break;
  6620. case SP_VIT: sd->status.vit = val; break;
  6621. case SP_INT: sd->status.int_ = val; break;
  6622. case SP_DEX: sd->status.dex = val; break;
  6623. case SP_LUK: sd->status.luk = val; break;
  6624. default:
  6625. return -1;
  6626. }
  6627. return val;
  6628. }
  6629. /**
  6630. * Gets the total number of status points at the provided level.
  6631. * @param level: Player base level.
  6632. * @return Total number of status points at specific base level.
  6633. */
  6634. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  6635. return this->statpoint_table[level];
  6636. }
  6637. /**
  6638. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  6639. * @param level: Player base level.
  6640. * @param table: Use table value or formula.
  6641. * @return Status points at specific base level.
  6642. */
  6643. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  6644. if (this->statpoint_table[level+1] > this->statpoint_table[level])
  6645. return (this->statpoint_table[level+1] - this->statpoint_table[level]);
  6646. return 0;
  6647. }
  6648. #ifdef RENEWAL_STAT
  6649. /// Renewal status point cost formula
  6650. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6651. #else
  6652. /// Pre-Renewal status point cost formula
  6653. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6654. #endif
  6655. /// Returns the number of stat points needed to change the specified stat by val.
  6656. /// If val is negative, returns the number of stat points that would be needed to
  6657. /// raise the specified stat from (current value - val) to current value.
  6658. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6659. {
  6660. int low, high, sp = 0, max = 0;
  6661. if ( val == 0 )
  6662. return 0;
  6663. low = pc_getstat(sd,type);
  6664. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6665. if ( low >= max && val > 0 )
  6666. return 0; // Official servers show '0' when max is reached
  6667. high = low + val;
  6668. if ( val < 0 )
  6669. SWAP(low, high);
  6670. for ( ; low < high; low++ )
  6671. sp += PC_STATUS_POINT_COST(low);
  6672. return sp;
  6673. }
  6674. /**
  6675. * Returns the value the specified stat can be increased by with the current
  6676. * amount of available status points for the current character's class.
  6677. *
  6678. * @param sd The target character.
  6679. * @param type Stat to verify.
  6680. * @return Maximum value the stat could grow by.
  6681. */
  6682. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6683. {
  6684. int base, final_val, status_points, max_param;
  6685. nullpo_ret(sd);
  6686. base = final_val = pc_getstat(sd, type);
  6687. status_points = sd->status.status_point;
  6688. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6689. while (final_val <= max_param && status_points >= 0) {
  6690. status_points -= PC_STATUS_POINT_COST(final_val);
  6691. final_val++;
  6692. }
  6693. final_val--;
  6694. return (final_val > base ? final_val-base : 0);
  6695. }
  6696. /**
  6697. * Raises a stat by the specified amount.
  6698. *
  6699. * Obeys max_parameter limits.
  6700. * Subtracts status points according to the cost of the increased stat points.
  6701. *
  6702. * @param sd The target character.
  6703. * @param type The stat to change (see enum _sp)
  6704. * @param increase The stat increase (strictly positive) amount.
  6705. * @retval true if the stat was increased by any amount.
  6706. * @retval false if there were no changes.
  6707. */
  6708. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6709. {
  6710. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6711. nullpo_ret(sd);
  6712. // check conditions
  6713. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6714. clif_statusupack(sd, type, 0, 0);
  6715. return false;
  6716. }
  6717. // check limits
  6718. current = pc_getstat(sd, type);
  6719. max_increase = pc_maxparameterincrease(sd, type);
  6720. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6721. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6722. clif_statusupack(sd, type, 0, 0);
  6723. return false;
  6724. }
  6725. // check status points
  6726. needed_points = pc_need_status_point(sd, type, increase);
  6727. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6728. clif_statusupack(sd, type, 0, 0);
  6729. return false;
  6730. }
  6731. // set new values
  6732. final_value = pc_setstat(sd, type, current + increase);
  6733. sd->status.status_point -= needed_points;
  6734. status_calc_pc(sd,SCO_NONE);
  6735. // update increase cost indicator
  6736. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6737. // update statpoint count
  6738. clif_updatestatus(sd, SP_STATUSPOINT);
  6739. // update stat value
  6740. clif_statusupack(sd, type, 1, final_value); // required
  6741. if( final_value > 255 )
  6742. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6743. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6744. return true;
  6745. }
  6746. /**
  6747. * Raises a stat by the specified amount.
  6748. *
  6749. * Obeys max_parameter limits.
  6750. * Does not subtract status points for the cost of the modified stat points.
  6751. *
  6752. * @param sd The target character.
  6753. * @param type The stat to change (see enum _sp)
  6754. * @param val The stat increase (or decrease) amount.
  6755. * @return the stat increase amount.
  6756. * @retval 0 if no changes were made.
  6757. */
  6758. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6759. {
  6760. int max, need;
  6761. nullpo_ret(sd);
  6762. if( type < SP_STR || type > SP_LUK )
  6763. {
  6764. clif_statusupack(sd,type,0,0);
  6765. return 0;
  6766. }
  6767. need = pc_need_status_point(sd,type,1);
  6768. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6769. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6770. status_calc_pc(sd,SCO_NONE);
  6771. // update increase cost indicator
  6772. if( need != pc_need_status_point(sd,type,1) )
  6773. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6774. // update stat value
  6775. clif_statusupack(sd,type,1,val); // required
  6776. if( val > 255 )
  6777. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6778. return val;
  6779. }
  6780. /*==========================================
  6781. * Update skill_lv for player sd
  6782. * Skill point allocation
  6783. *------------------------------------------*/
  6784. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6785. {
  6786. uint16 idx = skill_get_index(skill_id);
  6787. nullpo_retv(sd);
  6788. if (!idx) {
  6789. if (skill_id)
  6790. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6791. return;
  6792. }
  6793. // Level up guild skill
  6794. if (SKILL_CHK_GUILD(skill_id)) {
  6795. guild_skillup(sd, skill_id);
  6796. return;
  6797. }
  6798. // Level up homunculus skill
  6799. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6800. hom_skillup(sd->hd, skill_id);
  6801. return;
  6802. }
  6803. else {
  6804. if( sd->status.skill_point > 0 &&
  6805. sd->status.skill[idx].id &&
  6806. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6807. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6808. {
  6809. int lv, range, upgradable;
  6810. sd->status.skill[idx].lv++;
  6811. sd->status.skill_point--;
  6812. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  6813. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6814. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6815. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6816. else
  6817. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6818. lv = sd->status.skill[idx].lv;
  6819. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6820. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6821. clif_skillup(sd,skill_id,lv,range,upgradable);
  6822. clif_updatestatus(sd,SP_SKILLPOINT);
  6823. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6824. clif_updatestatus(sd,SP_CARTINFO);
  6825. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  6826. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6827. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6828. clif_skillinfoblock(sd);
  6829. }
  6830. //else
  6831. // 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);
  6832. }
  6833. }
  6834. /*==========================================
  6835. * /allskill
  6836. *------------------------------------------*/
  6837. int pc_allskillup(struct map_session_data *sd)
  6838. {
  6839. int i;
  6840. nullpo_ret(sd);
  6841. for (i = 0; i < MAX_SKILL; i++) {
  6842. 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) {
  6843. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6844. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6845. if (sd->status.skill[i].lv == 0)
  6846. sd->status.skill[i].id = 0;
  6847. }
  6848. }
  6849. if (!pc_grant_allskills(sd, true)) {
  6850. uint16 sk_id;
  6851. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6852. uint16 sk_idx = skill_get_index(sk_id);
  6853. if (sk_id == 0 || sk_idx == 0)
  6854. continue;
  6855. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6856. if (
  6857. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6858. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6859. sk_id == SG_DEVIL
  6860. )
  6861. continue; //Cannot be learned normally.
  6862. sd->status.skill[sk_idx].id = sk_id;
  6863. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6864. }
  6865. }
  6866. status_calc_pc(sd,SCO_NONE);
  6867. //Required because if you could level up all skills previously,
  6868. //the update will not be sent as only the lv variable changes.
  6869. clif_skillinfoblock(sd);
  6870. return 0;
  6871. }
  6872. /*==========================================
  6873. * /resetlvl
  6874. *------------------------------------------*/
  6875. int pc_resetlvl(struct map_session_data* sd,int type)
  6876. {
  6877. int i;
  6878. nullpo_ret(sd);
  6879. if (type != 3) //Also reset skills
  6880. pc_resetskill(sd, 0);
  6881. if(type == 1){
  6882. sd->status.skill_point=0;
  6883. sd->status.base_level=1;
  6884. sd->status.job_level=1;
  6885. sd->status.base_exp=0;
  6886. sd->status.job_exp=0;
  6887. if(sd->sc.option !=0)
  6888. sd->sc.option = 0;
  6889. sd->status.str=1;
  6890. sd->status.agi=1;
  6891. sd->status.vit=1;
  6892. sd->status.int_=1;
  6893. sd->status.dex=1;
  6894. sd->status.luk=1;
  6895. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6896. sd->status.status_point=100; // not 88 [celest]
  6897. // give platinum skills upon changing
  6898. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6899. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6900. }
  6901. }
  6902. if(type == 2){
  6903. sd->status.skill_point=0;
  6904. sd->status.base_level=1;
  6905. sd->status.job_level=1;
  6906. sd->status.base_exp=0;
  6907. sd->status.job_exp=0;
  6908. }
  6909. if(type == 3){
  6910. sd->status.base_level=1;
  6911. sd->status.base_exp=0;
  6912. }
  6913. if(type == 4){
  6914. sd->status.job_level=1;
  6915. sd->status.job_exp=0;
  6916. }
  6917. clif_updatestatus(sd,SP_STATUSPOINT);
  6918. clif_updatestatus(sd,SP_STR);
  6919. clif_updatestatus(sd,SP_AGI);
  6920. clif_updatestatus(sd,SP_VIT);
  6921. clif_updatestatus(sd,SP_INT);
  6922. clif_updatestatus(sd,SP_DEX);
  6923. clif_updatestatus(sd,SP_LUK);
  6924. clif_updatestatus(sd,SP_BASELEVEL);
  6925. clif_updatestatus(sd,SP_JOBLEVEL);
  6926. clif_updatestatus(sd,SP_STATUSPOINT);
  6927. clif_updatestatus(sd,SP_BASEEXP);
  6928. clif_updatestatus(sd,SP_JOBEXP);
  6929. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6930. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6931. clif_updatestatus(sd,SP_SKILLPOINT);
  6932. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6933. clif_updatestatus(sd,SP_UAGI);
  6934. clif_updatestatus(sd,SP_UVIT);
  6935. clif_updatestatus(sd,SP_UINT);
  6936. clif_updatestatus(sd,SP_UDEX);
  6937. clif_updatestatus(sd,SP_ULUK); // End Addition
  6938. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6939. if(sd->equip_index[i] >= 0)
  6940. if(pc_isequip(sd,sd->equip_index[i]))
  6941. pc_unequipitem(sd,sd->equip_index[i],2);
  6942. }
  6943. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6944. party_send_levelup(sd);
  6945. status_calc_pc(sd, SCO_FORCE);
  6946. clif_skillinfoblock(sd);
  6947. return 0;
  6948. }
  6949. /*==========================================
  6950. * /resetstate
  6951. *------------------------------------------*/
  6952. int pc_resetstate(struct map_session_data* sd)
  6953. {
  6954. nullpo_ret(sd);
  6955. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  6956. 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",
  6957. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  6958. sd->status.base_level = pc_maxbaselv( sd );
  6959. clif_updatestatus( sd, SP_BASELEVEL );
  6960. }
  6961. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  6962. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  6963. sd->status.status_point += battle_config.transcendent_status_points;
  6964. }
  6965. pc_setstat(sd, SP_STR, 1);
  6966. pc_setstat(sd, SP_AGI, 1);
  6967. pc_setstat(sd, SP_VIT, 1);
  6968. pc_setstat(sd, SP_INT, 1);
  6969. pc_setstat(sd, SP_DEX, 1);
  6970. pc_setstat(sd, SP_LUK, 1);
  6971. clif_updatestatus(sd,SP_STR);
  6972. clif_updatestatus(sd,SP_AGI);
  6973. clif_updatestatus(sd,SP_VIT);
  6974. clif_updatestatus(sd,SP_INT);
  6975. clif_updatestatus(sd,SP_DEX);
  6976. clif_updatestatus(sd,SP_LUK);
  6977. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6978. clif_updatestatus(sd,SP_UAGI);
  6979. clif_updatestatus(sd,SP_UVIT);
  6980. clif_updatestatus(sd,SP_UINT);
  6981. clif_updatestatus(sd,SP_UDEX);
  6982. clif_updatestatus(sd,SP_ULUK); // End Addition
  6983. clif_updatestatus(sd,SP_STATUSPOINT);
  6984. if( sd->mission_mobid ) { //bugreport:2200
  6985. sd->mission_mobid = 0;
  6986. sd->mission_count = 0;
  6987. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6988. }
  6989. status_calc_pc(sd, SCO_NONE);
  6990. return 1;
  6991. }
  6992. /*==========================================
  6993. * /resetskill
  6994. * if flag&1, perform block resync and status_calc call.
  6995. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6996. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6997. *------------------------------------------*/
  6998. int pc_resetskill(struct map_session_data* sd, int flag)
  6999. {
  7000. int i, skill_point=0;
  7001. nullpo_ret(sd);
  7002. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7003. return 0;
  7004. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7005. /**
  7006. * 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)
  7007. **/
  7008. if( pc_is_taekwon_ranker(sd) )
  7009. return 0;
  7010. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7011. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7012. i = sd->sc.option;
  7013. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7014. i &= ~OPTION_RIDING;
  7015. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7016. i &= ~OPTION_FALCON;
  7017. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7018. i &= ~OPTION_DRAGON;
  7019. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7020. i &= ~OPTION_WUG;
  7021. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7022. i &= ~OPTION_WUGRIDER;
  7023. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7024. i &= ~OPTION_MADOGEAR;
  7025. #ifndef NEW_CARTS
  7026. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7027. i &= ~OPTION_CART;
  7028. #else
  7029. if( sd->sc.data[SC_PUSH_CART] )
  7030. pc_setcart(sd, 0);
  7031. #endif
  7032. if( i != sd->sc.option )
  7033. pc_setoption(sd, i);
  7034. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7035. hom_vaporize(sd, HOM_ST_ACTIVE);
  7036. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  7037. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7038. if (sd->sc.data[SC_SOULATTACK] && pc_checkskill(sd, SU_SOULATTACK))
  7039. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  7040. }
  7041. for (const auto &skill : skill_db) {
  7042. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7043. uint8 lv = sd->status.skill[idx].lv;
  7044. if (lv == 0 || skill_id == 0)
  7045. continue;
  7046. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7047. continue;
  7048. // Don't reset trick dead if not a novice/baby
  7049. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7050. {
  7051. sd->status.skill[idx].lv = 0;
  7052. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7053. continue;
  7054. }
  7055. // do not reset basic skill
  7056. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7057. continue;
  7058. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7059. continue;
  7060. if( flag&4 && !skill_ischangesex(skill_id) )
  7061. continue;
  7062. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7063. { //Only handle quest skills in a special way when you can't learn them manually
  7064. if( battle_config.quest_skill_reset && !(flag&2) )
  7065. { //Wipe them
  7066. sd->status.skill[idx].lv = 0;
  7067. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7068. }
  7069. continue;
  7070. }
  7071. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7072. skill_point += lv;
  7073. else
  7074. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7075. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7076. if( !(flag&2) )
  7077. {// reset
  7078. sd->status.skill[idx].lv = 0;
  7079. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7080. }
  7081. }
  7082. if( flag&2 || !skill_point ) return skill_point;
  7083. sd->status.skill_point += skill_point;
  7084. if (flag&1) {
  7085. clif_updatestatus(sd,SP_SKILLPOINT);
  7086. clif_skillinfoblock(sd);
  7087. status_calc_pc(sd, SCO_FORCE);
  7088. }
  7089. return skill_point;
  7090. }
  7091. /*==========================================
  7092. * /resetfeel [Komurka]
  7093. *------------------------------------------*/
  7094. int pc_resetfeel(struct map_session_data* sd)
  7095. {
  7096. int i;
  7097. nullpo_ret(sd);
  7098. for (i=0; i<MAX_PC_FEELHATE; i++)
  7099. {
  7100. sd->feel_map[i].m = -1;
  7101. sd->feel_map[i].index = 0;
  7102. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7103. }
  7104. return 0;
  7105. }
  7106. int pc_resethate(struct map_session_data* sd)
  7107. {
  7108. int i;
  7109. nullpo_ret(sd);
  7110. for (i=0; i<MAX_PC_FEELHATE; i++)
  7111. {
  7112. sd->hate_mob[i] = -1;
  7113. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7114. }
  7115. return 0;
  7116. }
  7117. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7118. {
  7119. int bonus = 0;
  7120. nullpo_ret(sd);
  7121. skill_id = skill_dummy2skill_id(skill_id);
  7122. for (auto &it : sd->skillatk) {
  7123. if (it.id == skill_id) {
  7124. bonus += it.val;
  7125. break;
  7126. }
  7127. }
  7128. return bonus;
  7129. }
  7130. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7131. {
  7132. int bonus = 0;
  7133. nullpo_ret(sd);
  7134. skill_id = skill_dummy2skill_id(skill_id);
  7135. for (auto &it : sd->subskill) {
  7136. if (it.id == skill_id) {
  7137. bonus += it.val;
  7138. break;
  7139. }
  7140. }
  7141. return bonus;
  7142. }
  7143. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7144. int bonus = sd->bonus.add_heal_rate;
  7145. nullpo_ret(sd);
  7146. skill_id = skill_dummy2skill_id(skill_id);
  7147. if( bonus ) {
  7148. switch( skill_id ) {
  7149. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7150. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7151. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7152. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7153. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7154. }
  7155. }
  7156. for (auto &it : sd->skillheal) {
  7157. if (it.id == skill_id) {
  7158. bonus += it.val;
  7159. break;
  7160. }
  7161. }
  7162. return bonus;
  7163. }
  7164. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7165. int bonus = sd->bonus.add_heal2_rate;
  7166. skill_id = skill_dummy2skill_id(skill_id);
  7167. for (auto &it : sd->skillheal2) {
  7168. if (it.id == skill_id) {
  7169. bonus += it.val;
  7170. break;
  7171. }
  7172. }
  7173. return bonus;
  7174. }
  7175. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7176. {
  7177. if( !pc_isdead(sd) )
  7178. return; // not applicable
  7179. if( sd->bg_id && bg_member_respawn(sd) )
  7180. return; // member revived by battleground
  7181. pc_setstand(sd, true);
  7182. pc_setrestartvalue(sd,3);
  7183. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7184. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7185. }
  7186. static TIMER_FUNC(pc_respawn_timer){
  7187. struct map_session_data *sd = map_id2sd(id);
  7188. if( sd != NULL )
  7189. {
  7190. sd->pvp_point=0;
  7191. sd->respawn_tid = INVALID_TIMER;
  7192. pc_respawn(sd,CLR_OUTSIGHT);
  7193. }
  7194. return 0;
  7195. }
  7196. /*==========================================
  7197. * Invoked when a player has received damage
  7198. *------------------------------------------*/
  7199. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  7200. {
  7201. if (sp) clif_updatestatus(sd,SP_SP);
  7202. if (hp) clif_updatestatus(sd,SP_HP);
  7203. else return;
  7204. if (!src)
  7205. return;
  7206. if( pc_issit(sd) ) {
  7207. pc_setstand(sd, true);
  7208. skill_sit(sd,0);
  7209. }
  7210. if (sd->progressbar.npc_id)
  7211. clif_progressbar_abort(sd);
  7212. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7213. pet_target_check(sd->pd,src,1);
  7214. if( sd->status.ele_id > 0 )
  7215. elemental_set_target(sd,src);
  7216. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7217. sd->canlog_tick = gettick();
  7218. }
  7219. TIMER_FUNC(pc_close_npc_timer){
  7220. TBL_PC *sd = map_id2sd(id);
  7221. if(sd) pc_close_npc(sd,data);
  7222. return 0;
  7223. }
  7224. /**
  7225. * Method to properly close a NPC for player and clear anything related.
  7226. * @param sd: Player attached
  7227. * @param flag: Method of closure
  7228. * 1: Produce a close button and end the NPC
  7229. * 2: End the NPC (best for no dialog windows)
  7230. */
  7231. void pc_close_npc(struct map_session_data *sd,int flag)
  7232. {
  7233. nullpo_retv(sd);
  7234. if (sd->npc_id || sd->npc_shopid) {
  7235. if (sd->state.using_fake_npc) {
  7236. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7237. sd->state.using_fake_npc = 0;
  7238. }
  7239. if (sd->st) {
  7240. if(sd->st->state == RUN){ //wait ending code execution
  7241. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7242. return;
  7243. }
  7244. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7245. sd->st->mes_active = 0;
  7246. }
  7247. sd->state.menu_or_input = 0;
  7248. sd->npc_menu = 0;
  7249. sd->npc_shopid = 0;
  7250. #ifdef SECURE_NPCTIMEOUT
  7251. if( sd->npc_idle_timer != INVALID_TIMER ){
  7252. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  7253. sd->npc_idle_timer = INVALID_TIMER;
  7254. }
  7255. #endif
  7256. if (sd->st) {
  7257. if (sd->st->state == CLOSE) {
  7258. clif_scriptclose(sd, sd->npc_id);
  7259. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7260. sd->st->state = END; // Force to end now
  7261. }
  7262. if (sd->st->state == END) { // free attached scripts that are waiting
  7263. script_free_state(sd->st);
  7264. sd->st = NULL;
  7265. sd->npc_id = 0;
  7266. }
  7267. }
  7268. }
  7269. }
  7270. /*==========================================
  7271. * Invoked when a player has negative current hp
  7272. *------------------------------------------*/
  7273. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7274. {
  7275. int i=0,k=0;
  7276. t_tick tick = gettick();
  7277. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7278. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7279. // Super Novices have no kill or die functions attached when saved by their angel
  7280. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7281. t_exp exp = pc_nextbaseexp(sd);
  7282. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  7283. sd->state.snovice_dead_flag = 1;
  7284. pc_setrestartvalue(sd,1);
  7285. status_percent_heal(&sd->bl, 100, 100);
  7286. clif_resurrection(&sd->bl, 1);
  7287. if(battle_config.pc_invincible_time)
  7288. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7289. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7290. if(mapdata_flag_gvg2(mapdata))
  7291. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7292. return 0;
  7293. }
  7294. }
  7295. for(k = 0; k < MAX_DEVOTION; k++) {
  7296. if (sd->devotion[k]){
  7297. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7298. if (devsd)
  7299. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7300. sd->devotion[k] = 0;
  7301. }
  7302. }
  7303. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7304. if (sd->stellar_mark[k]) {
  7305. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7306. if (smarksd)
  7307. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7308. sd->stellar_mark[k] = 0;
  7309. }
  7310. }
  7311. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7312. if (sd->united_soul[k]) {
  7313. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7314. if (usoulsd)
  7315. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7316. sd->united_soul[k] = 0;
  7317. }
  7318. }
  7319. if(sd->shadowform_id) { //if we were target of shadowform
  7320. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7321. sd->shadowform_id = 0; //should be remove on status end anyway
  7322. }
  7323. if(sd->status.pet_id > 0 && sd->pd) {
  7324. struct pet_data *pd = sd->pd;
  7325. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7326. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7327. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7328. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7329. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7330. }
  7331. if( sd->pd->target_id ) // Unlock all targets...
  7332. pet_unlocktarget(sd->pd);
  7333. }
  7334. 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)
  7335. hom_vaporize(sd, HOM_ST_ACTIVE);
  7336. if( sd->md )
  7337. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7338. if( sd->ed )
  7339. elemental_delete(sd->ed);
  7340. // Leave duel if you die [LuzZza]
  7341. if(battle_config.duel_autoleave_when_die) {
  7342. if(sd->duel_group > 0)
  7343. duel_leave(sd->duel_group, sd);
  7344. if(sd->duel_invite > 0)
  7345. duel_reject(sd->duel_invite, sd);
  7346. }
  7347. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  7348. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  7349. sd->skill_keep_using.tid = INVALID_TIMER;
  7350. }
  7351. pc_close_npc(sd,2); //close npc if we were using one
  7352. /* e.g. not killed thru pc_damage */
  7353. if( pc_issit(sd) ) {
  7354. clif_status_load(&sd->bl,EFST_SIT,0);
  7355. }
  7356. pc_setdead(sd);
  7357. clif_party_dead( sd );
  7358. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  7359. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7360. //Reset menu skills/item skills
  7361. if ((sd->skillitem) != 0)
  7362. sd->skillitem = sd->skillitemlv = 0;
  7363. if ((sd->menuskill_id) != 0)
  7364. sd->menuskill_id = sd->menuskill_val = 0;
  7365. //Reset ticks.
  7366. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7367. if ( sd->spiritball !=0 )
  7368. pc_delspiritball(sd,sd->spiritball,0);
  7369. if (sd->soulball != 0)
  7370. pc_delsoulball(sd, sd->soulball, false);
  7371. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7372. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7373. if (src)
  7374. switch (src->type) {
  7375. case BL_MOB:
  7376. {
  7377. struct mob_data *md=(struct mob_data *)src;
  7378. if(md->target_id==sd->bl.id)
  7379. mob_unlocktarget(md,tick);
  7380. if(battle_config.mobs_level_up && md->status.hp &&
  7381. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7382. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7383. ) { // monster level up [Valaris]
  7384. clif_misceffect(&md->bl,0);
  7385. md->level++;
  7386. status_calc_mob(md, SCO_NONE);
  7387. status_percent_heal(src,10,0);
  7388. if( battle_config.show_mob_info&4 )
  7389. {// update name with new level
  7390. clif_name_area(&md->bl);
  7391. }
  7392. }
  7393. src = battle_get_master(src); // Maybe Player Summon
  7394. }
  7395. break;
  7396. case BL_PET: //Pass on to master...
  7397. case BL_HOM:
  7398. case BL_MER:
  7399. src = battle_get_master(src);
  7400. break;
  7401. }
  7402. if (src && src->type == BL_PC) {
  7403. struct map_session_data *ssd = (struct map_session_data *)src;
  7404. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7405. npc_script_event(ssd, NPCE_KILLPC);
  7406. if (battle_config.pk_mode&2) {
  7407. ssd->status.manner -= 5;
  7408. if(ssd->status.manner < 0)
  7409. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7410. #if 0
  7411. // PK/Karma system code (not enabled yet) [celest]
  7412. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7413. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7414. // karma going down = more 'good' / more honourable.
  7415. // The Karma System way...
  7416. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7417. sd->status.karma--;
  7418. ssd->status.karma--;
  7419. }
  7420. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7421. ssd->status.karma++;
  7422. // or the PK System way...
  7423. if (sd->status.karma > 0) // player killed is dishonourable?
  7424. ssd->status.karma--; // honour points earned
  7425. sd->status.karma++; // honour points lost
  7426. // To-do: Receive exp on certain occasions
  7427. #endif
  7428. }
  7429. }
  7430. if(battle_config.bone_drop==2
  7431. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7432. {
  7433. struct item item_tmp;
  7434. memset(&item_tmp,0,sizeof(item_tmp));
  7435. item_tmp.nameid=ITEMID_SKULL_;
  7436. item_tmp.identify=1;
  7437. item_tmp.card[0]=CARD0_CREATE;
  7438. item_tmp.card[1]=0;
  7439. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7440. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7441. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7442. }
  7443. //Remove bonus_script when dead
  7444. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7445. // changed penalty options, added death by player if pk_mode [Valaris]
  7446. if(battle_config.death_penalty_type
  7447. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7448. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7449. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7450. {
  7451. t_exp base_penalty = 0;
  7452. t_exp job_penalty = 0;
  7453. uint32 zeny_penalty = 0;
  7454. if (pc_isvip(sd)) { // EXP penalty for VIP
  7455. base_penalty = battle_config.vip_exp_penalty_base;
  7456. job_penalty = battle_config.vip_exp_penalty_job;
  7457. zeny_penalty = battle_config.vip_zeny_penalty;
  7458. } else {
  7459. base_penalty = battle_config.death_penalty_base;
  7460. job_penalty = battle_config.death_penalty_job;
  7461. zeny_penalty = battle_config.zeny_penalty;
  7462. }
  7463. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7464. switch (battle_config.death_penalty_type) {
  7465. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7466. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7467. }
  7468. if (base_penalty){ //recheck after altering to speedup
  7469. if (battle_config.pk_mode && src && src->type==BL_PC)
  7470. base_penalty *= 2;
  7471. base_penalty = u64min(sd->status.base_exp, base_penalty);
  7472. }
  7473. }
  7474. else
  7475. base_penalty = 0;
  7476. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7477. switch (battle_config.death_penalty_type) {
  7478. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7479. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7480. }
  7481. if (job_penalty) {
  7482. if (battle_config.pk_mode && src && src->type==BL_PC)
  7483. job_penalty *= 2;
  7484. job_penalty = u64min(sd->status.job_exp, job_penalty);
  7485. }
  7486. }
  7487. else
  7488. job_penalty = 0;
  7489. if (base_penalty || job_penalty) {
  7490. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  7491. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  7492. pc_lostexp(sd, base_penalty, job_penalty);
  7493. }
  7494. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7495. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7496. if(zeny_penalty)
  7497. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7498. }
  7499. }
  7500. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7501. for (const auto &it : mapdata->drop_list) {
  7502. int id = it.drop_id, per = it.drop_per;
  7503. enum e_nightmare_drop_type type = it.drop_type;
  7504. if(id == 0)
  7505. continue;
  7506. if(id == -1){
  7507. int eq_num=0,eq_n[MAX_INVENTORY];
  7508. memset(eq_n,0,sizeof(eq_n));
  7509. for(i=0;i<MAX_INVENTORY;i++) {
  7510. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7511. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7512. || type&NMDT_ALL)
  7513. {
  7514. int l;
  7515. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7516. if( l < MAX_INVENTORY )
  7517. eq_n[l] = i;
  7518. eq_num++;
  7519. }
  7520. }
  7521. if(eq_num > 0){
  7522. int n = eq_n[rnd()%eq_num];
  7523. if(rnd()%10000 < per) {
  7524. if(sd->inventory.u.items_inventory[n].equip)
  7525. pc_unequipitem(sd,n,3);
  7526. pc_dropitem(sd,n,1);
  7527. }
  7528. }
  7529. }
  7530. else if(id > 0) {
  7531. for(i=0;i<MAX_INVENTORY;i++){
  7532. if(sd->inventory.u.items_inventory[i].nameid == id
  7533. && rnd()%10000 < per
  7534. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7535. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7536. || type&NMDT_ALL) ){
  7537. if(sd->inventory.u.items_inventory[i].equip)
  7538. pc_unequipitem(sd,i,3);
  7539. pc_dropitem(sd,i,1);
  7540. break;
  7541. }
  7542. }
  7543. }
  7544. }
  7545. }
  7546. // pvp
  7547. // disable certain pvp functions on pk_mode [Valaris]
  7548. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7549. sd->pvp_point -= 5;
  7550. sd->pvp_lost++;
  7551. if( src && src->type == BL_PC ) {
  7552. struct map_session_data *ssd = (struct map_session_data *)src;
  7553. ssd->pvp_point++;
  7554. ssd->pvp_won++;
  7555. }
  7556. if( sd->pvp_point < 0 ) {
  7557. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7558. return 1|8;
  7559. }
  7560. }
  7561. //GvG
  7562. if( mapdata_flag_gvg2(mapdata) ) {
  7563. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7564. return 1|8;
  7565. }
  7566. else if( sd->bg_id ) {
  7567. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7568. if (bg) {
  7569. if (bg->cemetery.map > 0) { // Respawn by BG
  7570. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7571. return 1|8;
  7572. }
  7573. }
  7574. }
  7575. //Reset "can log out" tick.
  7576. if( battle_config.prevent_logout )
  7577. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7578. return 1;
  7579. }
  7580. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7581. if(hp) clif_updatestatus(sd,SP_HP);
  7582. if(sp) clif_updatestatus(sd,SP_SP);
  7583. pc_setstand(sd, true);
  7584. if(battle_config.pc_invincible_time > 0)
  7585. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7586. if( sd->state.gmaster_flag ) {
  7587. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7588. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7589. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7590. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7591. }
  7592. }
  7593. bool pc_revive_item(struct map_session_data *sd) {
  7594. nullpo_retr(false, sd);
  7595. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7596. return false;
  7597. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7598. return false;
  7599. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7600. uint8 hp = 100, sp = 100;
  7601. if (item_position < 0) {
  7602. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7603. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7604. sp = 0;
  7605. }
  7606. else
  7607. return false;
  7608. }
  7609. if (!status_revive(&sd->bl, hp, sp))
  7610. return false;
  7611. if (item_position < 0)
  7612. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7613. else
  7614. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7615. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7616. return true;
  7617. }
  7618. // script
  7619. //
  7620. /*==========================================
  7621. * script reading pc status registry
  7622. *------------------------------------------*/
  7623. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7624. {
  7625. int64 val = 0;
  7626. nullpo_ret(sd);
  7627. switch(type) {
  7628. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7629. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7630. case SP_ZENY: val = sd->status.zeny; break;
  7631. case SP_BASELEVEL: val = sd->status.base_level; break;
  7632. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7633. case SP_CLASS: val = sd->status.class_; break;
  7634. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7635. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7636. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7637. case SP_SEX: val = sd->status.sex; break;
  7638. case SP_WEIGHT: val = sd->weight; break;
  7639. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7640. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  7641. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  7642. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  7643. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  7644. case SP_HP: val = sd->battle_status.hp; break;
  7645. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7646. case SP_SP: val = sd->battle_status.sp; break;
  7647. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7648. case SP_STR: val = sd->status.str; break;
  7649. case SP_AGI: val = sd->status.agi; break;
  7650. case SP_VIT: val = sd->status.vit; break;
  7651. case SP_INT: val = sd->status.int_; break;
  7652. case SP_DEX: val = sd->status.dex; break;
  7653. case SP_LUK: val = sd->status.luk; break;
  7654. case SP_KARMA: val = sd->status.karma; break;
  7655. case SP_MANNER: val = sd->status.manner; break;
  7656. case SP_FAME: val = sd->status.fame; break;
  7657. case SP_KILLERRID: val = sd->killerrid; break;
  7658. case SP_KILLEDRID: val = sd->killedrid; break;
  7659. case SP_KILLEDGID: val = sd->killedgid; break;
  7660. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7661. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7662. case SP_CHARRENAME: val = sd->status.rename; break;
  7663. case SP_CHARFONT: val = sd->status.font; break;
  7664. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7665. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7666. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7667. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7668. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7669. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7670. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7671. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7672. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7673. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7674. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7675. case SP_BASE_ATK:
  7676. #ifdef RENEWAL
  7677. val = sd->bonus.eatk;
  7678. #else
  7679. val = sd->battle_status.batk;
  7680. #endif
  7681. break;
  7682. case SP_DEF1: val = sd->battle_status.def; break;
  7683. case SP_DEF2: val = sd->battle_status.def2; break;
  7684. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7685. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7686. case SP_HIT: val = sd->battle_status.hit; break;
  7687. case SP_FLEE1: val = sd->battle_status.flee; break;
  7688. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7689. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7690. case SP_MAXHPRATE: val = sd->hprate; break;
  7691. case SP_MAXSPRATE: val = sd->sprate; break;
  7692. case SP_SPRATE: val = sd->dsprate; break;
  7693. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7694. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7695. case SP_ASPD_RATE:
  7696. #ifndef RENEWAL_ASPD
  7697. val = sd->battle_status.aspd_rate;
  7698. #else
  7699. val = sd->battle_status.aspd_rate2;
  7700. #endif
  7701. break;
  7702. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7703. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7704. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7705. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7706. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7707. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7708. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7709. case SP_MATK_RATE: val = sd->matk_rate; break;
  7710. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7711. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7712. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7713. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7714. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7715. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7716. case SP_HIT_RATE: val = sd->hit_rate; break;
  7717. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7718. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7719. case SP_DEF_RATE: val = sd->def_rate; break;
  7720. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7721. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7722. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7723. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7724. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7725. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7726. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7727. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7728. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7729. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7730. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7731. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7732. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7733. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7734. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7735. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7736. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7737. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7738. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7739. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7740. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  7741. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7742. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7743. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7744. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7745. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7746. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7747. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7748. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7749. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7750. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  7751. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7752. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7753. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7754. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7755. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  7756. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7757. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7758. case SP_UNSTRIPABLE:
  7759. case SP_UNSTRIPABLE_ARMOR:
  7760. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7761. break;
  7762. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7763. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7764. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7765. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7766. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7767. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7768. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7769. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7770. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7771. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7772. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7773. case SP_EMATK: val = sd->bonus.ematk; break;
  7774. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7775. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7776. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7777. case SP_CASTRATE:
  7778. case SP_VARCASTRATE:
  7779. #ifdef RENEWAL_CAST
  7780. val = sd->bonus.varcastrate; break;
  7781. #else
  7782. val = sd->castrate; break;
  7783. #endif
  7784. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  7785. default:
  7786. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7787. return -1;
  7788. }
  7789. return val;
  7790. }
  7791. /*==========================================
  7792. * script set pc status registry
  7793. *------------------------------------------*/
  7794. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7795. {
  7796. nullpo_retr(false,sd);
  7797. int val = static_cast<unsigned int>(val_tmp);
  7798. switch(type){
  7799. case SP_BASELEVEL:
  7800. if (val > pc_maxbaselv(sd)) //Capping to max
  7801. val = pc_maxbaselv(sd);
  7802. if (val > sd->status.base_level) {
  7803. int i = 0;
  7804. int stat=0;
  7805. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7806. stat += statpoint_db.pc_gets_status_point(sd->status.base_level + i);
  7807. sd->status.status_point += stat;
  7808. }
  7809. sd->status.base_level = val;
  7810. sd->status.base_exp = 0;
  7811. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7812. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7813. clif_updatestatus(sd, SP_STATUSPOINT);
  7814. clif_updatestatus(sd, SP_BASEEXP);
  7815. status_calc_pc(sd, SCO_FORCE);
  7816. if(sd->status.party_id)
  7817. party_send_levelup(sd);
  7818. break;
  7819. case SP_JOBLEVEL:
  7820. if (val >= sd->status.job_level) {
  7821. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7822. sd->status.skill_point += val - sd->status.job_level;
  7823. clif_updatestatus(sd, SP_SKILLPOINT);
  7824. }
  7825. sd->status.job_level = val;
  7826. sd->status.job_exp = 0;
  7827. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7828. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7829. clif_updatestatus(sd, SP_JOBEXP);
  7830. status_calc_pc(sd, SCO_FORCE);
  7831. break;
  7832. case SP_SKILLPOINT:
  7833. sd->status.skill_point = val;
  7834. break;
  7835. case SP_STATUSPOINT:
  7836. sd->status.status_point = val;
  7837. break;
  7838. case SP_ZENY:
  7839. if( val < 0 )
  7840. return false;// can't set negative zeny
  7841. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7842. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7843. break;
  7844. case SP_BASEEXP:
  7845. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  7846. if (val_tmp < sd->status.base_exp) // Lost
  7847. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  7848. else // Gained
  7849. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  7850. return true;
  7851. case SP_JOBEXP:
  7852. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  7853. if (val_tmp < sd->status.job_exp) // Lost
  7854. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  7855. else // Gained
  7856. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  7857. return true;
  7858. case SP_SEX:
  7859. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7860. break;
  7861. case SP_WEIGHT:
  7862. sd->weight = val;
  7863. break;
  7864. case SP_MAXWEIGHT:
  7865. sd->max_weight = val;
  7866. break;
  7867. case SP_HP:
  7868. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7869. break;
  7870. case SP_MAXHP:
  7871. if (sd->status.base_level < 100)
  7872. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7873. else if (sd->status.base_level < 151)
  7874. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7875. else
  7876. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7877. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7878. {
  7879. sd->battle_status.hp = sd->battle_status.max_hp;
  7880. clif_updatestatus(sd, SP_HP);
  7881. }
  7882. break;
  7883. case SP_SP:
  7884. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7885. break;
  7886. case SP_MAXSP:
  7887. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7888. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7889. {
  7890. sd->battle_status.sp = sd->battle_status.max_sp;
  7891. clif_updatestatus(sd, SP_SP);
  7892. }
  7893. break;
  7894. case SP_STR:
  7895. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7896. break;
  7897. case SP_AGI:
  7898. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7899. break;
  7900. case SP_VIT:
  7901. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7902. break;
  7903. case SP_INT:
  7904. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7905. break;
  7906. case SP_DEX:
  7907. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7908. break;
  7909. case SP_LUK:
  7910. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7911. break;
  7912. case SP_KARMA:
  7913. sd->status.karma = val;
  7914. break;
  7915. case SP_MANNER:
  7916. sd->status.manner = val;
  7917. if( val < 0 )
  7918. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7919. else {
  7920. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7921. clif_manner_message(sd, 5);
  7922. }
  7923. return true; // status_change_start/status_change_end already sends packets warning the client
  7924. case SP_FAME:
  7925. sd->status.fame = val;
  7926. break;
  7927. case SP_KILLERRID:
  7928. sd->killerrid = val;
  7929. return true;
  7930. case SP_KILLEDRID:
  7931. sd->killedrid = val;
  7932. return true;
  7933. case SP_KILLEDGID:
  7934. sd->killedgid = val;
  7935. return true;
  7936. case SP_CHARMOVE:
  7937. sd->status.character_moves = val;
  7938. return true;
  7939. case SP_CHARRENAME:
  7940. sd->status.rename = val;
  7941. return true;
  7942. case SP_CHARFONT:
  7943. sd->status.font = val;
  7944. clif_font(sd);
  7945. return true;
  7946. case SP_BANK_VAULT:
  7947. if (val < 0)
  7948. return false;
  7949. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7950. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7951. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7952. return true;
  7953. case SP_ROULETTE_BRONZE:
  7954. sd->roulette_point.bronze = val;
  7955. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7956. return true;
  7957. case SP_ROULETTE_SILVER:
  7958. sd->roulette_point.silver = val;
  7959. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7960. return true;
  7961. case SP_ROULETTE_GOLD:
  7962. sd->roulette_point.gold = val;
  7963. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7964. return true;
  7965. case SP_CASHPOINTS:
  7966. if (val < 0)
  7967. return false;
  7968. if (!sd->state.connect_new)
  7969. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7970. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7971. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7972. return true;
  7973. case SP_KAFRAPOINTS:
  7974. if (val < 0)
  7975. return false;
  7976. if (!sd->state.connect_new)
  7977. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7978. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7979. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7980. return true;
  7981. case SP_PCDIECOUNTER:
  7982. if (val < 0)
  7983. return false;
  7984. if (sd->die_counter == val)
  7985. return true;
  7986. sd->die_counter = val;
  7987. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7988. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7989. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7990. return true;
  7991. case SP_COOKMASTERY:
  7992. if (val < 0)
  7993. return false;
  7994. if (sd->cook_mastery == val)
  7995. return true;
  7996. val = cap_value(val, 0, 1999);
  7997. sd->cook_mastery = val;
  7998. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7999. return true;
  8000. default:
  8001. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8002. return false;
  8003. }
  8004. clif_updatestatus(sd,static_cast<int>(type));
  8005. return true;
  8006. }
  8007. /*==========================================
  8008. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8009. *------------------------------------------*/
  8010. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  8011. {
  8012. if (type&2) {
  8013. if (hp || type&4)
  8014. clif_heal(sd->fd,SP_HP,hp);
  8015. if (sp)
  8016. clif_heal(sd->fd,SP_SP,sp);
  8017. } else {
  8018. if(hp)
  8019. clif_updatestatus(sd,SP_HP);
  8020. if(sp)
  8021. clif_updatestatus(sd,SP_SP);
  8022. }
  8023. return;
  8024. }
  8025. /**
  8026. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  8027. * @param sd: Player data
  8028. * @param itemid: Item ID
  8029. * @param hp: HP to heal
  8030. * @param sp: SP to heal
  8031. * @return Amount healed to an object
  8032. */
  8033. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  8034. {
  8035. int bonus, tmp, penalty = 0;
  8036. if (hp) {
  8037. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8038. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8039. if (potion_flag == 2) {
  8040. bonus += bonus * 50 / 100;
  8041. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  8042. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  8043. }
  8044. //All item bonuses.
  8045. bonus += sd->bonus.itemhealrate2;
  8046. //Item Group bonuses
  8047. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid) / 100;
  8048. //Individual item bonuses.
  8049. for(const auto &it : sd->itemhealrate) {
  8050. if (it.id == itemid) {
  8051. bonus += bonus * it.val / 100;
  8052. break;
  8053. }
  8054. }
  8055. // Recovery Potion
  8056. if (sd->sc.data[SC_INCHEALRATE])
  8057. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  8058. // 2014 Halloween Event : Pumpkin Bonus
  8059. if (sd->sc.data[SC_MTF_PUMPKIN]) {
  8060. if (itemid == ITEMID_PUMPKIN)
  8061. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  8062. else if (itemid == ITEMID_COOKIE_BAT)
  8063. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val2;
  8064. }
  8065. tmp = hp * bonus / 100; // Overflow check
  8066. if (bonus != 100 && tmp > hp)
  8067. hp = tmp;
  8068. }
  8069. if (sp) {
  8070. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8071. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8072. if (potion_flag == 2)
  8073. bonus += bonus * 50 / 100;
  8074. tmp = sp * bonus / 100; // Overflow check
  8075. if (bonus != 100 && tmp > sp)
  8076. sp = tmp;
  8077. }
  8078. if (sd->sc.count) {
  8079. // Critical Wound and Death Hurt stack
  8080. if (sd->sc.data[SC_CRITICALWOUND])
  8081. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  8082. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  8083. penalty += 20;
  8084. if (sd->sc.data[SC_NORECOVER_STATE])
  8085. penalty = 100;
  8086. if (sd->sc.data[SC_VITALITYACTIVATION])
  8087. hp += hp / 2; // 1.5 times
  8088. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  8089. hp += hp / 10;
  8090. sp += sp / 10;
  8091. }
  8092. #ifdef RENEWAL
  8093. if (sd->sc.data[SC_APPLEIDUN])
  8094. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  8095. #endif
  8096. if (penalty > 0) {
  8097. hp -= hp * penalty / 100;
  8098. sp -= sp * penalty / 100;
  8099. }
  8100. #ifdef RENEWAL
  8101. if (sd->sc.data[SC_EXTREMITYFIST2])
  8102. sp = 0;
  8103. #endif
  8104. if (sd->sc.data[SC_BITESCAR])
  8105. hp = 0;
  8106. }
  8107. return status_heal(&sd->bl, hp, sp, 1);
  8108. }
  8109. /*==========================================
  8110. * HP/SP Recovery
  8111. * Heal player hp nad/or sp by rate
  8112. *------------------------------------------*/
  8113. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8114. {
  8115. nullpo_ret(sd);
  8116. if (hp > 100) hp = 100;
  8117. else if (hp <-100) hp = -100;
  8118. if (sp > 100) sp = 100;
  8119. else if (sp <-100) sp = -100;
  8120. if(hp >= 0 && sp >= 0) //Heal
  8121. return status_percent_heal(&sd->bl, hp, sp);
  8122. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8123. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8124. //Crossed signs
  8125. if(hp) {
  8126. if(hp > 0)
  8127. status_percent_heal(&sd->bl, hp, 0);
  8128. else
  8129. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8130. }
  8131. if(sp) {
  8132. if(sp > 0)
  8133. status_percent_heal(&sd->bl, 0, sp);
  8134. else
  8135. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8136. }
  8137. return 0;
  8138. }
  8139. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8140. {
  8141. struct mob_data *md;
  8142. int flag;
  8143. md = (struct mob_data *)bl;
  8144. nullpo_ret(md);
  8145. flag = va_arg(ap, int);
  8146. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8147. status_kill(&md->bl);
  8148. return 1;
  8149. }
  8150. /**
  8151. * Called when player changes job
  8152. * Rewrote to make it tidider [Celest]
  8153. * @param sd
  8154. * @param job JOB ID. See enum e_job
  8155. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8156. * @return True if success, false if failed
  8157. **/
  8158. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8159. {
  8160. int i, fame_flag = 0;
  8161. int b_class;
  8162. nullpo_retr(false,sd);
  8163. if (job < 0)
  8164. return false;
  8165. //Normalize job.
  8166. b_class = pc_jobid2mapid(job);
  8167. if (b_class == -1)
  8168. return false;
  8169. switch (upper) {
  8170. case 1:
  8171. b_class|= JOBL_UPPER;
  8172. break;
  8173. case 2:
  8174. b_class|= JOBL_BABY;
  8175. break;
  8176. }
  8177. //This will automatically adjust bard/dancer classes to the correct gender
  8178. //That is, if you try to jobchange into dancer, it will turn you to bard.
  8179. job = pc_mapid2jobid(b_class, sd->status.sex);
  8180. if (job == -1)
  8181. return false;
  8182. if ((unsigned short)b_class == sd->class_)
  8183. return false; //Nothing to change.
  8184. // changing from 1st to 2nd job
  8185. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  8186. sd->change_level_2nd = sd->status.job_level;
  8187. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  8188. }
  8189. // changing from 2nd to 3rd job
  8190. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  8191. sd->change_level_3rd = sd->status.job_level;
  8192. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  8193. }
  8194. if(sd->cloneskill_idx > 0) {
  8195. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8196. sd->status.skill[sd->cloneskill_idx].id = 0;
  8197. sd->status.skill[sd->cloneskill_idx].lv = 0;
  8198. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  8199. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  8200. }
  8201. sd->cloneskill_idx = 0;
  8202. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  8203. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  8204. }
  8205. if(sd->reproduceskill_idx > 0) {
  8206. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8207. sd->status.skill[sd->reproduceskill_idx].id = 0;
  8208. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  8209. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  8210. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  8211. }
  8212. sd->reproduceskill_idx = 0;
  8213. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8214. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8215. }
  8216. // Give or reduce transcendent status points
  8217. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8218. sd->status.status_point += battle_config.transcendent_status_points;
  8219. clif_updatestatus(sd,SP_STATUSPOINT);
  8220. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8221. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8222. // The player already used his bonus points, so we have to reset his status points
  8223. pc_resetstate(sd);
  8224. }else{
  8225. sd->status.status_point -= battle_config.transcendent_status_points;
  8226. clif_updatestatus(sd,SP_STATUSPOINT);
  8227. }
  8228. }
  8229. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8230. const int class_ = pc_class2idx(sd->status.class_);
  8231. uint16 skill_id;
  8232. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  8233. //Remove status specific to your current tree skills.
  8234. enum sc_type sc = status_skill2sc(skill_id);
  8235. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  8236. status_change_end(&sd->bl, sc, INVALID_TIMER);
  8237. }
  8238. }
  8239. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8240. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  8241. pc_resetfeel(sd);
  8242. pc_resethate(sd);
  8243. }
  8244. // Reset body style to 0 before changing job to avoid
  8245. // errors since not every job has a alternate outfit.
  8246. sd->status.body = 0;
  8247. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8248. sd->status.class_ = job;
  8249. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8250. sd->class_ = (unsigned short)b_class;
  8251. sd->status.job_level=1;
  8252. sd->status.job_exp=0;
  8253. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8254. sd->status.base_level = pc_maxbaselv(sd);
  8255. sd->status.base_exp=0;
  8256. pc_resetstate(sd);
  8257. clif_updatestatus(sd,SP_STATUSPOINT);
  8258. clif_updatestatus(sd,SP_BASELEVEL);
  8259. clif_updatestatus(sd,SP_BASEEXP);
  8260. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8261. }
  8262. clif_updatestatus(sd,SP_JOBLEVEL);
  8263. clif_updatestatus(sd,SP_JOBEXP);
  8264. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8265. for(i=0;i<EQI_MAX;i++) {
  8266. if(sd->equip_index[i] >= 0)
  8267. if(pc_isequip(sd,sd->equip_index[i]))
  8268. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8269. }
  8270. //Change look, if disguised, you need to undisguise
  8271. //to correctly calculate new job sprite without
  8272. if (sd->disguise)
  8273. pc_disguise(sd, 0);
  8274. status_set_viewdata(&sd->bl, job);
  8275. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8276. #if PACKETVER >= 20151001
  8277. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8278. #endif
  8279. if(sd->vd.cloth_color)
  8280. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8281. /*
  8282. if(sd->vd.body_style)
  8283. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8284. */
  8285. //Update skill tree.
  8286. pc_calc_skilltree(sd);
  8287. clif_skillinfoblock(sd);
  8288. if (sd->ed)
  8289. elemental_delete(sd->ed);
  8290. if (sd->state.vending)
  8291. vending_closevending(sd);
  8292. if (sd->state.buyingstore)
  8293. buyingstore_close(sd);
  8294. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8295. //Remove peco/cart/falcon
  8296. i = sd->sc.option;
  8297. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8298. i&=~OPTION_RIDING;
  8299. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8300. i&=~OPTION_FALCON;
  8301. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8302. i&=~OPTION_DRAGON;
  8303. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8304. i&=~OPTION_WUGRIDER;
  8305. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8306. i&=~OPTION_WUG;
  8307. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8308. i&=~OPTION_MADOGEAR;
  8309. #ifndef NEW_CARTS
  8310. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8311. i&=~OPTION_CART;
  8312. #else
  8313. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8314. pc_setcart(sd, 0);
  8315. #endif
  8316. if(i != sd->sc.option)
  8317. pc_setoption(sd, i);
  8318. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8319. hom_vaporize(sd, HOM_ST_ACTIVE);
  8320. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8321. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8322. if (sd->sc.data[SC_SOULATTACK] && !pc_checkskill(sd, SU_SOULATTACK))
  8323. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  8324. if(sd->status.manner < 0)
  8325. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8326. status_calc_pc(sd,SCO_FORCE);
  8327. pc_checkallowskill(sd);
  8328. pc_equiplookall(sd);
  8329. pc_show_questinfo(sd);
  8330. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8331. if( sd->status.party_id ){
  8332. struct party_data* p;
  8333. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8334. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8335. if( i < MAX_PARTY ){
  8336. p->party.member[i].class_ = sd->status.class_;
  8337. clif_party_job_and_level(sd);
  8338. }
  8339. }
  8340. }
  8341. chrif_save(sd, CSAVE_NORMAL);
  8342. //if you were previously famous, not anymore.
  8343. if (fame_flag)
  8344. chrif_buildfamelist();
  8345. else if (sd->status.fame > 0) {
  8346. //It may be that now they are famous?
  8347. switch (sd->class_&MAPID_UPPERMASK) {
  8348. case MAPID_BLACKSMITH:
  8349. case MAPID_ALCHEMIST:
  8350. case MAPID_TAEKWON:
  8351. chrif_buildfamelist();
  8352. break;
  8353. }
  8354. }
  8355. return true;
  8356. }
  8357. /*==========================================
  8358. * Tell client player sd has change equipement
  8359. *------------------------------------------*/
  8360. void pc_equiplookall(struct map_session_data *sd)
  8361. {
  8362. nullpo_retv(sd);
  8363. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8364. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8365. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8366. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8367. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8368. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8369. }
  8370. /*==========================================
  8371. * Tell client player sd has change look (hair,equip...)
  8372. *------------------------------------------*/
  8373. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8374. nullpo_retv(sd);
  8375. switch(type) {
  8376. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8377. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8378. if (sd->status.hair != val) {
  8379. sd->status.hair = val;
  8380. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8381. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8382. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8383. }
  8384. break;
  8385. case LOOK_WEAPON:
  8386. sd->status.weapon = val;
  8387. break;
  8388. case LOOK_HEAD_BOTTOM:
  8389. sd->status.head_bottom = val;
  8390. sd->setlook_head_bottom = val;
  8391. break;
  8392. case LOOK_HEAD_TOP:
  8393. sd->status.head_top = val;
  8394. sd->setlook_head_top = val;
  8395. break;
  8396. case LOOK_HEAD_MID:
  8397. sd->status.head_mid = val;
  8398. sd->setlook_head_mid = val;
  8399. break;
  8400. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8401. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8402. if (sd->status.hair_color != val) {
  8403. sd->status.hair_color = val;
  8404. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8405. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8406. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8407. }
  8408. break;
  8409. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8410. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8411. sd->status.clothes_color = val;
  8412. break;
  8413. case LOOK_SHIELD:
  8414. sd->status.shield = val;
  8415. break;
  8416. case LOOK_SHOES:
  8417. break;
  8418. case LOOK_ROBE:
  8419. sd->status.robe = val;
  8420. sd->setlook_robe = val;
  8421. break;
  8422. case LOOK_BODY2:
  8423. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8424. sd->status.body = val;
  8425. break;
  8426. }
  8427. clif_changelook(&sd->bl, type, val);
  8428. }
  8429. /*==========================================
  8430. * Give an option (type) to player (sd) and display it to client
  8431. *------------------------------------------*/
  8432. void pc_setoption(struct map_session_data *sd,int type, int subtype)
  8433. {
  8434. int p_type, new_look=0;
  8435. nullpo_retv(sd);
  8436. p_type = sd->sc.option;
  8437. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8438. sd->sc.option=type;
  8439. clif_changeoption(&sd->bl);
  8440. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8441. { // Mounting
  8442. clif_status_load(&sd->bl,EFST_RIDING,1);
  8443. status_calc_pc(sd,SCO_NONE);
  8444. }
  8445. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8446. { // Dismount
  8447. clif_status_load(&sd->bl,EFST_RIDING,0);
  8448. status_calc_pc(sd,SCO_NONE);
  8449. }
  8450. #ifndef NEW_CARTS
  8451. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8452. clif_cartlist(sd);
  8453. clif_updatestatus(sd, SP_CARTINFO);
  8454. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8455. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8456. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8457. clif_clearcart(sd->fd);
  8458. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8459. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8460. }
  8461. #endif
  8462. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8463. clif_status_load(&sd->bl,EFST_FALCON,1);
  8464. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8465. clif_status_load(&sd->bl,EFST_FALCON,0);
  8466. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8467. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8468. status_calc_pc(sd,SCO_NONE);
  8469. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8470. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8471. status_calc_pc(sd,SCO_NONE);
  8472. }
  8473. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8474. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  8475. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8476. status_change_end(&sd->bl, SC_MADOGEAR, INVALID_TIMER);
  8477. }
  8478. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8479. new_look = JOB_STAR_GLADIATOR2;
  8480. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8481. new_look = -1;
  8482. if (sd->disguise || !new_look)
  8483. return; //Disguises break sprite changes
  8484. if (new_look < 0) { //Restore normal look.
  8485. status_set_viewdata(&sd->bl, sd->status.class_);
  8486. new_look = sd->vd.class_;
  8487. }
  8488. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8489. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8490. if (sd->vd.cloth_color)
  8491. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8492. if( sd->vd.body_style )
  8493. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8494. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8495. }
  8496. /**
  8497. * Give player a cart
  8498. * @param sd Player
  8499. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8500. **/
  8501. bool pc_setcart(struct map_session_data *sd,int type) {
  8502. #ifndef NEW_CARTS
  8503. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8504. int option;
  8505. #endif
  8506. nullpo_retr(false,sd);
  8507. if( type < 0 || type > MAX_CARTS )
  8508. return false;// Never trust the values sent by the client! [Skotlex]
  8509. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8510. return false;// Push cart is required
  8511. #ifdef NEW_CARTS
  8512. switch( type ) {
  8513. case 0:
  8514. if( !sd->sc.data[SC_PUSH_CART] )
  8515. return 0;
  8516. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8517. clif_clearcart(sd->fd);
  8518. break;
  8519. default:/* everything else is an allowed ID so we can move on */
  8520. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8521. clif_cartlist(sd);
  8522. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8523. }
  8524. clif_updatestatus(sd, SP_CARTINFO);
  8525. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8526. break;
  8527. }
  8528. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8529. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8530. #else
  8531. // Update option
  8532. option = sd->sc.option;
  8533. option &= ~OPTION_CART;// clear cart bits
  8534. option |= cart[type]; // set cart
  8535. pc_setoption(sd, option);
  8536. #endif
  8537. return true;
  8538. }
  8539. /*==========================================
  8540. * Give player a falcon
  8541. *------------------------------------------*/
  8542. void pc_setfalcon(struct map_session_data* sd, int flag)
  8543. {
  8544. if( flag ){
  8545. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8546. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8547. } else if( pc_isfalcon(sd) ){
  8548. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8549. }
  8550. }
  8551. /*==========================================
  8552. * Set player riding
  8553. *------------------------------------------*/
  8554. void pc_setriding(struct map_session_data* sd, int flag)
  8555. {
  8556. if( sd->sc.data[SC_ALL_RIDING] )
  8557. return;
  8558. if( flag ){
  8559. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8560. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8561. } else if( pc_isriding(sd) ){
  8562. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8563. }
  8564. }
  8565. /**
  8566. * Give player a mado
  8567. * @param sd: Player
  8568. * @param flag: Enable or disable mado
  8569. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  8570. */
  8571. void pc_setmadogear(struct map_session_data *sd, bool flag, e_mado_type type)
  8572. {
  8573. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  8574. return;
  8575. if (flag) {
  8576. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  8577. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  8578. } else if (pc_ismadogear(sd)) {
  8579. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  8580. }
  8581. }
  8582. /*==========================================
  8583. * Check if player can drop an item
  8584. *------------------------------------------*/
  8585. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8586. {
  8587. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8588. return false;
  8589. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8590. return false;
  8591. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8592. }
  8593. /**
  8594. * Determines whether a player can attack based on status changes
  8595. * Why not use status_check_skilluse?
  8596. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8597. * Even ground-based attacks should be blocked by these statuses
  8598. * Called from unit_attack and unit_attack_timer_sub
  8599. * @retval true Can attack
  8600. **/
  8601. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8602. nullpo_retr(false, sd);
  8603. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8604. return false;
  8605. if (sd->state.block_action & PCBLOCK_ATTACK)
  8606. return false;
  8607. if(
  8608. #ifdef RENEWAL
  8609. sd->sc.data[SC_BASILICA_CELL] ||
  8610. #else
  8611. sd->sc.data[SC_BASILICA] ||
  8612. #endif
  8613. sd->sc.data[SC__SHADOWFORM] ||
  8614. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8615. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8616. sd->sc.data[SC_CRYSTALIZE] ||
  8617. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8618. sd->sc.data[SC_TRICKDEAD] ||
  8619. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8620. sd->sc.data[SC_BLADESTOP] ||
  8621. sd->sc.data[SC_DEEPSLEEP] ||
  8622. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8623. sd->sc.data[SC_KINGS_GRACE] )
  8624. return false;
  8625. return true;
  8626. }
  8627. /*==========================================
  8628. * Read '@type' variables (temporary numeric char reg)
  8629. *------------------------------------------*/
  8630. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8631. {
  8632. return i64db_i64get(sd->regs.vars, reg);
  8633. }
  8634. /*==========================================
  8635. * Set '@type' variables (temporary numeric char reg)
  8636. *------------------------------------------*/
  8637. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8638. {
  8639. uint32 index = script_getvaridx(reg);
  8640. nullpo_retr(false, sd);
  8641. if( val ) {
  8642. i64db_i64put(sd->regs.vars, reg, val);
  8643. if( index )
  8644. script_array_update(&sd->regs, reg, false);
  8645. } else {
  8646. i64db_remove(sd->regs.vars, reg);
  8647. if( index )
  8648. script_array_update(&sd->regs, reg, true);
  8649. }
  8650. return true;
  8651. }
  8652. /*==========================================
  8653. * Read '@type$' variables (temporary string char reg)
  8654. *------------------------------------------*/
  8655. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8656. {
  8657. struct script_reg_str *p = NULL;
  8658. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8659. return p ? p->value : NULL;
  8660. }
  8661. /*==========================================
  8662. * Set '@type$' variables (temporary string char reg)
  8663. *------------------------------------------*/
  8664. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8665. {
  8666. struct script_reg_str *p = NULL;
  8667. unsigned int index = script_getvaridx(reg);
  8668. DBData prev;
  8669. nullpo_retr(false, sd);
  8670. if( str[0] ) {
  8671. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8672. p->value = aStrdup(str);
  8673. p->flag.type = 1;
  8674. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8675. p = (struct script_reg_str *)db_data2ptr(&prev);
  8676. if( p->value )
  8677. aFree(p->value);
  8678. ers_free(str_reg_ers, p);
  8679. } else {
  8680. if( index )
  8681. script_array_update(&sd->regs, reg, false);
  8682. }
  8683. } else {
  8684. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8685. p = (struct script_reg_str *)db_data2ptr(&prev);
  8686. if( p->value )
  8687. aFree(p->value);
  8688. ers_free(str_reg_ers, p);
  8689. if( index )
  8690. script_array_update(&sd->regs, reg, true);
  8691. }
  8692. }
  8693. return true;
  8694. }
  8695. /**
  8696. * Serves the following variable types:
  8697. * - 'type' (permanent numeric char reg)
  8698. * - '#type' (permanent numeric account reg)
  8699. * - '##type' (permanent numeric account reg2)
  8700. **/
  8701. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8702. {
  8703. struct script_reg_num *p = NULL;
  8704. if (!sd->vars_ok) {
  8705. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8706. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8707. //intif->request_registry(sd,type==3?4:type);
  8708. set_eof(sd->fd);
  8709. return 0;
  8710. }
  8711. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8712. return p ? p->value : 0;
  8713. }
  8714. /**
  8715. * Serves the following variable types:
  8716. * - 'type$' (permanent str char reg)
  8717. * - '#type$' (permanent str account reg)
  8718. * - '##type$' (permanent str account reg2)
  8719. **/
  8720. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8721. {
  8722. struct script_reg_str *p = NULL;
  8723. if (!sd->vars_ok) {
  8724. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8725. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8726. //intif->request_registry(sd,type==3?4:type);
  8727. set_eof(sd->fd);
  8728. return NULL;
  8729. }
  8730. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8731. return p ? p->value : NULL;
  8732. }
  8733. /**
  8734. * Serves the following variable types:
  8735. * - 'type' (permanent numeric char reg)
  8736. * - '#type' (permanent numeric account reg)
  8737. * - '##type' (permanent numeric account reg2)
  8738. **/
  8739. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8740. {
  8741. struct script_reg_num *p = NULL;
  8742. const char *regname = get_str(script_getvarid(reg));
  8743. uint32 index = script_getvaridx(reg);
  8744. if ( !reg_load && !sd->vars_ok ) {
  8745. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8746. return false;
  8747. }
  8748. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8749. if( val ) {
  8750. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8751. script_array_update(&sd->regs, reg, false);
  8752. p->value = val;
  8753. } else {
  8754. p->value = 0;
  8755. if( index )
  8756. script_array_update(&sd->regs, reg, true);
  8757. }
  8758. if (!reg_load)
  8759. p->flag.update = 1;/* either way, it will require either delete or replace */
  8760. } else if( val ) {
  8761. DBData prev;
  8762. if( index )
  8763. script_array_update(&sd->regs, reg, false);
  8764. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8765. p->value = val;
  8766. if (!reg_load)
  8767. p->flag.update = 1;
  8768. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8769. p = (struct script_reg_num *)db_data2ptr(&prev);
  8770. ers_free(num_reg_ers, p);
  8771. }
  8772. }
  8773. if (!reg_load && p)
  8774. sd->vars_dirty = true;
  8775. return true;
  8776. }
  8777. /**
  8778. * Serves the following variable types:
  8779. * - 'type$' (permanent str char reg)
  8780. * - '#type$' (permanent str account reg)
  8781. * - '##type$' (permanent str account reg2)
  8782. **/
  8783. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8784. {
  8785. struct script_reg_str *p = NULL;
  8786. const char *regname = get_str(script_getvarid(reg));
  8787. unsigned int index = script_getvaridx(reg);
  8788. size_t vlen = 0;
  8789. if (!reg_load && !sd->vars_ok) {
  8790. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8791. return false;
  8792. }
  8793. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8794. {
  8795. 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);
  8796. return false;
  8797. }
  8798. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8799. if( val[0] ) {
  8800. if( p->value )
  8801. aFree(p->value);
  8802. else if ( index ) // an entry that was deleted, so we reset
  8803. script_array_update(&sd->regs, reg, false);
  8804. p->value = aStrdup(val);
  8805. } else {
  8806. if (p->value)
  8807. aFree(p->value);
  8808. p->value = NULL;
  8809. if( index )
  8810. script_array_update(&sd->regs, reg, true);
  8811. }
  8812. if( !reg_load )
  8813. p->flag.update = 1; // either way, it will require either delete or replace
  8814. } else if( val[0] ) {
  8815. DBData prev;
  8816. if( index )
  8817. script_array_update(&sd->regs, reg, false);
  8818. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8819. p->value = aStrdup(val);
  8820. if( !reg_load )
  8821. p->flag.update = 1;
  8822. p->flag.type = 1;
  8823. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8824. p = (struct script_reg_str *)db_data2ptr(&prev);
  8825. if( p->value )
  8826. aFree(p->value);
  8827. ers_free(str_reg_ers, p);
  8828. }
  8829. }
  8830. if( !reg_load && p )
  8831. sd->vars_dirty = true;
  8832. return true;
  8833. }
  8834. /**
  8835. * Set value of player variable
  8836. * @param sd Player
  8837. * @param reg Variable name
  8838. * @param value
  8839. * @return True if success, false if failed.
  8840. **/
  8841. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8842. char prefix = reg[0];
  8843. nullpo_retr(false, sd);
  8844. if (reg[strlen(reg)-1] == '$') {
  8845. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8846. return false;
  8847. }
  8848. val = cap_value(val, INT_MIN, INT_MAX);
  8849. switch (prefix) {
  8850. case '.':
  8851. case '\'':
  8852. case '$':
  8853. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8854. return false;
  8855. case '@':
  8856. return pc_setreg(sd, add_str(reg), val);
  8857. case '#':
  8858. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8859. default:
  8860. return pc_setglobalreg(sd, add_str(reg), val);
  8861. }
  8862. return false;
  8863. }
  8864. /**
  8865. * Get value of player variable
  8866. * @param sd Player
  8867. * @param reg Variable name
  8868. * @return Variable value or 0 if failed.
  8869. **/
  8870. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8871. char prefix = reg[0];
  8872. nullpo_ret(sd);
  8873. if (reg[strlen(reg)-1] == '$') {
  8874. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8875. return 0;
  8876. }
  8877. switch (prefix) {
  8878. case '.':
  8879. case '\'':
  8880. case '$':
  8881. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8882. return 0;
  8883. case '@':
  8884. return pc_readreg(sd, add_str(reg));
  8885. case '#':
  8886. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8887. default:
  8888. return pc_readglobalreg(sd, add_str(reg));
  8889. }
  8890. return 0;
  8891. }
  8892. /*==========================================
  8893. * Exec eventtimer for player sd (retrieved from map_session (id))
  8894. *------------------------------------------*/
  8895. static TIMER_FUNC(pc_eventtimer){
  8896. struct map_session_data *sd=map_id2sd(id);
  8897. char *p = (char *)data;
  8898. int i;
  8899. if(sd==NULL)
  8900. return 0;
  8901. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8902. if( i < MAX_EVENTTIMER )
  8903. {
  8904. sd->eventtimer[i] = INVALID_TIMER;
  8905. sd->eventcount--;
  8906. npc_event(sd,p,0);
  8907. }
  8908. else
  8909. ShowError("pc_eventtimer: no such event timer\n");
  8910. if (p) aFree(p);
  8911. return 0;
  8912. }
  8913. /*==========================================
  8914. * Add eventtimer for player sd ?
  8915. *------------------------------------------*/
  8916. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8917. {
  8918. int i;
  8919. nullpo_retr(false,sd);
  8920. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8921. if( i == MAX_EVENTTIMER )
  8922. return false;
  8923. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8924. sd->eventcount++;
  8925. return true;
  8926. }
  8927. /*==========================================
  8928. * Del eventtimer for player sd ?
  8929. *------------------------------------------*/
  8930. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8931. {
  8932. char* p = NULL;
  8933. int i;
  8934. nullpo_retr(false,sd);
  8935. if (sd->eventcount == 0)
  8936. return false;
  8937. // find the named event timer
  8938. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8939. sd->eventtimer[i] != INVALID_TIMER &&
  8940. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8941. strcmp(p, name) == 0
  8942. );
  8943. if( i == MAX_EVENTTIMER )
  8944. return false; // not found
  8945. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8946. sd->eventtimer[i] = INVALID_TIMER;
  8947. if(sd->eventcount > 0)
  8948. sd->eventcount--;
  8949. aFree(p);
  8950. return true;
  8951. }
  8952. /*==========================================
  8953. * Update eventtimer count for player sd
  8954. *------------------------------------------*/
  8955. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8956. {
  8957. int i;
  8958. nullpo_retv(sd);
  8959. for(i=0;i<MAX_EVENTTIMER;i++)
  8960. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8961. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8962. addt_tickimer(sd->eventtimer[i],tick);
  8963. break;
  8964. }
  8965. }
  8966. /*==========================================
  8967. * Remove all eventtimer for player sd
  8968. *------------------------------------------*/
  8969. void pc_cleareventtimer(struct map_session_data *sd)
  8970. {
  8971. int i;
  8972. nullpo_retv(sd);
  8973. if (sd->eventcount == 0)
  8974. return;
  8975. for(i=0;i<MAX_EVENTTIMER;i++){
  8976. if( sd->eventtimer[i] != INVALID_TIMER ){
  8977. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8978. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8979. sd->eventtimer[i] = INVALID_TIMER;
  8980. if(sd->eventcount > 0) //avoid looping to max val
  8981. sd->eventcount--;
  8982. if (p) aFree(p);
  8983. }
  8984. }
  8985. }
  8986. /**
  8987. * Called when an item with combo is worn
  8988. * @param sd: Player data
  8989. * @param data: Item data
  8990. * @return Number of succeeded combo(s)
  8991. */
  8992. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  8993. int success = 0;
  8994. for (const auto &item_combo : data->combos) {
  8995. bool do_continue = false;
  8996. // Ensure this isn't a duplicate combo
  8997. for (const auto player_combo : sd->combos) {
  8998. if (player_combo->id == item_combo->id) {
  8999. do_continue = true;
  9000. break;
  9001. }
  9002. }
  9003. // Combo already equipped
  9004. if (do_continue)
  9005. continue;
  9006. size_t nb_itemCombo = item_combo->nameid.size();
  9007. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9008. continue;
  9009. struct s_itemchk {
  9010. int idx;
  9011. t_itemid nameid, card[MAX_SLOTS];
  9012. s_itemchk() : idx(0), nameid(0), card() {};
  9013. };
  9014. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9015. size_t j;
  9016. unsigned int pos = 0;
  9017. for (j = 0; j < nb_itemCombo; j++) {
  9018. t_itemid id = item_combo->nameid[j];
  9019. bool found = false;
  9020. for (int16 k = 0; k < EQI_MAX; k++) {
  9021. short index = sd->equip_index[k];
  9022. if (index < 0)
  9023. continue;
  9024. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  9025. continue;
  9026. if (!sd->inventory_data[index])
  9027. continue;
  9028. if (itemdb_type(id) != IT_CARD) {
  9029. if (sd->inventory_data[index]->nameid != id)
  9030. continue;
  9031. if (j > 0) { // Check if this item not already used
  9032. do_continue = false;
  9033. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  9034. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  9035. do_continue = true;
  9036. break;
  9037. }
  9038. }
  9039. if (do_continue)
  9040. continue;
  9041. }
  9042. combo_idx[j].nameid = id;
  9043. combo_idx[j].idx = index;
  9044. pos |= sd->inventory.u.items_inventory[index].equip;
  9045. found = true;
  9046. break;
  9047. } else { // Cards and enchants
  9048. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  9049. continue;
  9050. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  9051. do_continue = false;
  9052. if (sd->inventory.u.items_inventory[index].card[z] != id)
  9053. continue;
  9054. if (j > 0) {
  9055. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  9056. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  9057. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  9058. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  9059. do_continue = true;
  9060. break;
  9061. }
  9062. }
  9063. }
  9064. }
  9065. }
  9066. if (do_continue)
  9067. continue;
  9068. combo_idx[j].nameid = id;
  9069. combo_idx[j].idx = index;
  9070. combo_idx[j].card[z] = id;
  9071. pos |= sd->inventory.u.items_inventory[index].equip;
  9072. found = true;
  9073. break;
  9074. }
  9075. }
  9076. }
  9077. if (!found)
  9078. break; // Unable to found all the IDs for this combo, return
  9079. }
  9080. // Broke out of the count loop without finding all IDs, move to the next combo
  9081. if (j < nb_itemCombo)
  9082. continue;
  9083. // All items in the combo are matching
  9084. auto entry = std::make_shared<s_combos>();
  9085. entry->bonus = item_combo->script;
  9086. entry->id = item_combo->id;
  9087. entry->pos = pos;
  9088. sd->combos.push_back(entry);
  9089. combo_idx.clear();
  9090. success++;
  9091. }
  9092. return success;
  9093. }
  9094. /**
  9095. * Called when an item with combo is removed
  9096. * @param sd: Player data
  9097. * @param data: Item data
  9098. * @return Number of removed combo(s)
  9099. */
  9100. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  9101. if (sd->combos.empty())
  9102. return 0; // Nothing to do here, player has no combos
  9103. int retval = 0;
  9104. for (const auto &item_combo : data->combos) {
  9105. std::shared_ptr<s_combos> del_combo = nullptr;
  9106. // Check if this combo exists on this player
  9107. for (const auto &player_combo : sd->combos) {
  9108. if (player_combo->id == item_combo->id) {
  9109. del_combo = player_combo;
  9110. break;
  9111. }
  9112. }
  9113. // No match, skip this combo
  9114. if (del_combo == nullptr)
  9115. continue;
  9116. util::vector_erase_if_exists(sd->combos, del_combo);
  9117. retval++;
  9118. // Check if combo requirements still fit
  9119. if (pc_checkcombo(sd, data))
  9120. continue;
  9121. // It's empty, clear all the memory
  9122. if (sd->combos.empty()) {
  9123. sd->combos.clear();
  9124. return retval; // Return at this point as there are no more combos to check
  9125. }
  9126. }
  9127. return retval;
  9128. }
  9129. /**
  9130. * Load combo data(s) of player
  9131. * @param sd: Player data
  9132. * @return ret numbers of succeed combo
  9133. */
  9134. int pc_load_combo(struct map_session_data *sd) {
  9135. int ret = 0;
  9136. for (int16 i = 0; i < EQI_MAX; i++) {
  9137. item_data *id;
  9138. short idx = sd->equip_index[i];
  9139. if (idx < 0 || !(id = sd->inventory_data[idx]))
  9140. continue;
  9141. if (!id->combos.empty())
  9142. ret += pc_checkcombo(sd, id);
  9143. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9144. item_data *data;
  9145. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  9146. if (!sd->inventory.u.items_inventory[idx].card[j])
  9147. continue;
  9148. if ((data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j])) != nullptr) {
  9149. if (!data->combos.empty())
  9150. ret += pc_checkcombo(sd, data);
  9151. }
  9152. }
  9153. }
  9154. }
  9155. return ret;
  9156. }
  9157. /*==========================================
  9158. * Equip item on player sd at req_pos from inventory index n
  9159. * return: false - fail; true - success
  9160. *------------------------------------------*/
  9161. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9162. {
  9163. int i, pos, flag = 0, iflag;
  9164. struct item_data *id;
  9165. uint8 res = ITEM_EQUIP_ACK_OK;
  9166. short* equip_index;
  9167. nullpo_retr(false,sd);
  9168. if( n < 0 || n >= MAX_INVENTORY ) {
  9169. if( equipswitch ){
  9170. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9171. }else{
  9172. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9173. }
  9174. return false;
  9175. }
  9176. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9177. if( equipswitch ){
  9178. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9179. }else{
  9180. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9181. }
  9182. return false;
  9183. }
  9184. if (!(id = sd->inventory_data[n]))
  9185. return false;
  9186. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9187. if(battle_config.battle_log && !equipswitch)
  9188. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9189. if((res = pc_isequip(sd,n))) {
  9190. if( equipswitch ){
  9191. clif_equipswitch_add( sd, n, req_pos, res );
  9192. }else{
  9193. clif_equipitemack(sd,n,0,res); // fail
  9194. }
  9195. return false;
  9196. }
  9197. if( equipswitch && id->type == IT_AMMO ){
  9198. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9199. return false;
  9200. }
  9201. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9202. if( equipswitch ){
  9203. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9204. }else{
  9205. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9206. }
  9207. return false;
  9208. }
  9209. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9210. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9211. if( equipswitch ){
  9212. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9213. }else{
  9214. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9215. }
  9216. return false;
  9217. }
  9218. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9219. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9220. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9221. clif_notify_bindOnEquip(sd,n);
  9222. }
  9223. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9224. pos = req_pos&EQP_ACC;
  9225. if (pos == EQP_ACC) //User specified both slots.
  9226. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9227. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  9228. if (!sd->inventory.u.items_inventory[n].card[i])
  9229. continue;
  9230. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9231. if (card_data) {
  9232. int card_pos = card_data->equip;
  9233. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9234. pos = card_pos; // Use the card's equip position
  9235. break;
  9236. }
  9237. }
  9238. }
  9239. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9240. pos = (req_pos&EQP_ARMS);
  9241. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9242. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9243. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9244. pos = req_pos&EQP_SHADOW_ACC;
  9245. if (pos == EQP_SHADOW_ACC)
  9246. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9247. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9248. pos = (req_pos&EQP_SHADOW_ARMS);
  9249. if( pos == EQP_SHADOW_ARMS )
  9250. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9251. }
  9252. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9253. //Update skill-block range database when weapon range changes. [Skotlex]
  9254. i = equip_index[EQI_HAND_R];
  9255. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9256. flag = 1;
  9257. else
  9258. flag = id->range != sd->inventory_data[i]->range;
  9259. }
  9260. if( equipswitch ){
  9261. for( i = 0; i < EQI_MAX; i++ ){
  9262. if( pos&equip_bitmask[i] ){
  9263. // If there was already an item assigned to this slot
  9264. if( sd->equip_switch_index[i] >= 0 ){
  9265. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9266. }
  9267. // Assign the new index to it
  9268. sd->equip_switch_index[i] = n;
  9269. }
  9270. }
  9271. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9272. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9273. return true;
  9274. }else{
  9275. for(i=0;i<EQI_MAX;i++) {
  9276. if(pos & equip_bitmask[i]) {
  9277. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9278. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9279. sd->equip_index[i] = n;
  9280. }
  9281. }
  9282. pc_equipswitch_remove(sd, n);
  9283. if(pos==EQP_AMMO) {
  9284. clif_arrowequip(sd,n);
  9285. clif_arrow_fail(sd,3);
  9286. }
  9287. else
  9288. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9289. sd->inventory.u.items_inventory[n].equip = pos;
  9290. }
  9291. if(pos & EQP_HAND_R) {
  9292. if(id)
  9293. sd->weapontype1 = id->subtype;
  9294. else
  9295. sd->weapontype1 = W_FIST;
  9296. pc_calcweapontype(sd);
  9297. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9298. }
  9299. if(pos & EQP_HAND_L) {
  9300. if(id) {
  9301. if(id->type == IT_WEAPON) {
  9302. sd->status.shield = W_FIST;
  9303. sd->weapontype2 = id->subtype;
  9304. }
  9305. else
  9306. if(id->type == IT_ARMOR) {
  9307. sd->status.shield = id->look;
  9308. sd->weapontype2 = W_FIST;
  9309. }
  9310. }
  9311. else
  9312. sd->status.shield = sd->weapontype2 = W_FIST;
  9313. pc_calcweapontype(sd);
  9314. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9315. }
  9316. if(pos & EQP_SHOES)
  9317. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9318. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9319. short idx = sd->equip_index[EQI_AMMO];
  9320. if (idx >= 0) {
  9321. switch (sd->inventory_data[idx]->subtype) {
  9322. case AMMO_ARROW:
  9323. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  9324. pc_unequipitem(sd, idx, 2 | 4);
  9325. break;
  9326. case AMMO_BULLET:
  9327. case AMMO_SHELL:
  9328. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  9329. #ifdef RENEWAL
  9330. && id->subtype != W_GRENADE
  9331. #endif
  9332. )
  9333. pc_unequipitem(sd, idx, 2 | 4);
  9334. break;
  9335. #ifndef RENEWAL
  9336. case AMMO_GRENADE:
  9337. if (id->subtype != W_GRENADE)
  9338. pc_unequipitem(sd, idx, 2 | 4);
  9339. break;
  9340. #endif
  9341. }
  9342. }
  9343. }
  9344. pc_set_costume_view(sd);
  9345. pc_checkallowskill(sd); //Check if status changes should be halted.
  9346. iflag = sd->npc_item_flag;
  9347. // Check for combos (MUST be before status_calc_pc)
  9348. if (!id->combos.empty())
  9349. pc_checkcombo(sd, id);
  9350. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9351. ; // No cards
  9352. else {
  9353. for (i = 0; i < MAX_SLOTS; i++) {
  9354. item_data *data;
  9355. if (!sd->inventory.u.items_inventory[n].card[i])
  9356. continue;
  9357. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  9358. if (!data->combos.empty())
  9359. pc_checkcombo(sd, data);
  9360. }
  9361. }
  9362. }
  9363. status_calc_pc(sd,SCO_NONE);
  9364. if (flag) //Update skill data
  9365. clif_skillinfoblock(sd);
  9366. //OnEquip script [Skotlex]
  9367. if (id) {
  9368. //only run the script if item isn't restricted
  9369. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9370. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9371. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9372. ; //No cards
  9373. else {
  9374. for( i = 0; i < MAX_SLOTS; i++ ) {
  9375. struct item_data *data;
  9376. if (!sd->inventory.u.items_inventory[n].card[i])
  9377. continue;
  9378. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9379. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9380. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9381. }
  9382. }
  9383. }
  9384. }
  9385. sd->npc_item_flag = iflag;
  9386. return true;
  9387. }
  9388. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  9389. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  9390. while( it != bonus.end() ){
  9391. std::shared_ptr<s_autobonus> b = *it;
  9392. if( ( b->pos & position ) != b->pos ){
  9393. it++;
  9394. continue;
  9395. }
  9396. it = bonus.erase( it );
  9397. }
  9398. }
  9399. /**
  9400. * Recalculate player status on unequip
  9401. * @param sd: Player data
  9402. * @param n: Item inventory index
  9403. * @param flag: Whether to recalculate a player's status or not
  9404. * @return True on success or false on failure
  9405. */
  9406. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9407. int i, iflag;
  9408. bool status_calc = false;
  9409. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  9410. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  9411. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  9412. sd->inventory.u.items_inventory[n].equip = 0;
  9413. if (!(flag & 4))
  9414. pc_checkallowskill(sd);
  9415. iflag = sd->npc_item_flag;
  9416. // Check for combos (MUST be before status_calc_pc)
  9417. if (sd->inventory_data[n]) {
  9418. if (!sd->inventory_data[n]->combos.empty()) {
  9419. if (pc_removecombo(sd, sd->inventory_data[n]))
  9420. status_calc = true;
  9421. }
  9422. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9423. ; // No cards
  9424. else {
  9425. for (i = 0; i < MAX_SLOTS; i++) {
  9426. item_data *data;
  9427. if (!sd->inventory.u.items_inventory[n].card[i])
  9428. continue;
  9429. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  9430. if (!data->combos.empty()) {
  9431. if (pc_removecombo(sd, data))
  9432. status_calc = true;
  9433. }
  9434. }
  9435. }
  9436. }
  9437. }
  9438. if (flag & 1 || status_calc) {
  9439. pc_checkallowskill(sd);
  9440. status_calc_pc(sd, SCO_FORCE);
  9441. }
  9442. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9443. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9444. //OnUnEquip script [Skotlex]
  9445. if (sd->inventory_data[n]) {
  9446. if (sd->inventory_data[n]->unequip_script)
  9447. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9448. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9449. ; //No cards
  9450. else {
  9451. for (i = 0; i < MAX_SLOTS; i++) {
  9452. struct item_data *data;
  9453. if (!sd->inventory.u.items_inventory[n].card[i])
  9454. continue;
  9455. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9456. if (data->unequip_script)
  9457. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9458. }
  9459. }
  9460. }
  9461. }
  9462. sd->npc_item_flag = iflag;
  9463. }
  9464. /**
  9465. * Called when attempting to unequip an item from a player
  9466. * @param sd: Player data
  9467. * @param n: Item inventory index
  9468. * @param flag: Type of unequip
  9469. * 0 - only unequip
  9470. * 1 - calculate status after unequipping
  9471. * 2 - force unequip
  9472. * 4 - unequip by switching equipment
  9473. * @return True on success or false on failure
  9474. */
  9475. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9476. int i, pos;
  9477. nullpo_retr(false,sd);
  9478. if (n < 0 || n >= MAX_INVENTORY) {
  9479. clif_unequipitemack(sd,0,0,0);
  9480. return false;
  9481. }
  9482. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9483. clif_unequipitemack(sd,n,0,0);
  9484. return false; //Nothing to unequip
  9485. }
  9486. // status change that makes player cannot unequip equipment
  9487. if (!(flag&2) && sd->sc.count &&
  9488. (sd->sc.data[SC_BERSERK] ||
  9489. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9490. sd->sc.data[SC__BLOODYLUST] ||
  9491. sd->sc.data[SC_KYOUGAKU] ||
  9492. (sd->sc.data[SC_PYROCLASTIC] &&
  9493. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9494. {
  9495. clif_unequipitemack(sd,n,0,0);
  9496. return false;
  9497. }
  9498. if (battle_config.battle_log)
  9499. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9500. for(i = 0; i < EQI_MAX; i++) {
  9501. if (pos & equip_bitmask[i])
  9502. sd->equip_index[i] = -1;
  9503. }
  9504. if(pos & EQP_HAND_R) {
  9505. sd->weapontype1 = 0;
  9506. sd->status.weapon = sd->weapontype2;
  9507. pc_calcweapontype(sd);
  9508. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9509. if( !battle_config.dancing_weaponswitch_fix )
  9510. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9511. #ifdef RENEWAL
  9512. if (battle_config.switch_remove_edp&2) {
  9513. #else
  9514. if (battle_config.switch_remove_edp&1) {
  9515. #endif
  9516. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9517. }
  9518. }
  9519. if(pos & EQP_HAND_L) {
  9520. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9521. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9522. sd->status.shield = sd->weapontype2 = W_FIST;
  9523. pc_calcweapontype(sd);
  9524. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9525. }
  9526. if(pos & EQP_SHOES)
  9527. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9528. clif_unequipitemack(sd,n,pos,1);
  9529. pc_set_costume_view(sd);
  9530. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9531. // On weapon change (right and left hand)
  9532. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9533. if (battle_config.ammo_unequip && !(flag & 4)) {
  9534. switch (sd->inventory_data[n]->subtype) {
  9535. case W_BOW:
  9536. case W_MUSICAL:
  9537. case W_WHIP:
  9538. case W_REVOLVER:
  9539. case W_RIFLE:
  9540. case W_GATLING:
  9541. case W_SHOTGUN:
  9542. case W_GRENADE: {
  9543. short idx = sd->equip_index[EQI_AMMO];
  9544. if (idx >= 0) {
  9545. sd->equip_index[EQI_AMMO] = -1;
  9546. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9547. pc_unequipitem_sub(sd, idx, 0);
  9548. }
  9549. }
  9550. break;
  9551. }
  9552. }
  9553. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9554. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9555. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9556. }
  9557. // On armor change
  9558. if (pos & EQP_ARMOR) {
  9559. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9560. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9561. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9562. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9563. }
  9564. // On equipment change
  9565. #ifndef RENEWAL
  9566. if (!(flag&4))
  9567. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9568. #endif
  9569. // On ammo change
  9570. 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_))
  9571. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9572. pc_unequipitem_sub(sd, n, flag);
  9573. return true;
  9574. }
  9575. int pc_equipswitch( struct map_session_data* sd, int index ){
  9576. // Get the target equip mask
  9577. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9578. // Get the currently equipped item
  9579. short equippedItem = pc_checkequip( sd, position, true );
  9580. // No item equipped at the target
  9581. if( equippedItem == -1 ){
  9582. // Remove it from the equip switch
  9583. pc_equipswitch_remove( sd, index );
  9584. pc_equipitem( sd, index, position );
  9585. return position;
  9586. }else{
  9587. std::map<int, int> unequipped;
  9588. int unequipped_position = 0;
  9589. // Unequip all items that interfere
  9590. for( int i = 0; i < EQI_MAX; i++ ){
  9591. int unequip_index = sd->equip_index[i];
  9592. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9593. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9594. // Store the unequipped index and position mask for later
  9595. unequipped[unequip_index] = unequip_item->equip;
  9596. // Keep the position for later
  9597. unequipped_position |= unequip_item->equip;
  9598. // Unequip the item
  9599. pc_unequipitem( sd, unequip_index, 0 );
  9600. }
  9601. }
  9602. int all_position = position | unequipped_position;
  9603. // Equip everything that is hit by the mask
  9604. for( int i = 0; i < EQI_MAX; i++ ){
  9605. int exchange_index = sd->equip_switch_index[i];
  9606. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9607. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9608. // Store the target position
  9609. int exchange_position = exchange_item->equipSwitch;
  9610. // Remove the item from equip switch
  9611. pc_equipswitch_remove( sd, exchange_index );
  9612. // Equip the item at the destinated position
  9613. pc_equipitem( sd, exchange_index, exchange_position );
  9614. }
  9615. }
  9616. // Place all unequipped items into the equip switch window
  9617. for( std::pair<int, int> pair : unequipped ){
  9618. int unequipped_index = pair.first;
  9619. int unequipped_position = pair.second;
  9620. // Rebuild the index cache
  9621. for( int i = 0; i < EQI_MAX; i++ ){
  9622. if( unequipped_position & equip_bitmask[i] ){
  9623. sd->equip_switch_index[i] = unequipped_index;
  9624. }
  9625. }
  9626. // Set the correct position mask
  9627. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9628. // Notify the client
  9629. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9630. }
  9631. return all_position;
  9632. }
  9633. }
  9634. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9635. struct item* item = &sd->inventory.u.items_inventory[index];
  9636. if( !item->equipSwitch ){
  9637. return;
  9638. }
  9639. for( int i = 0; i < EQI_MAX; i++ ){
  9640. // If a match is found
  9641. if( sd->equip_switch_index[i] == index ){
  9642. // Remove it from the slot
  9643. sd->equip_switch_index[i] = -1;
  9644. }
  9645. }
  9646. // Send out one packet for all slots using the current item's mask
  9647. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9648. item->equipSwitch = 0;
  9649. }
  9650. /*==========================================
  9651. * Checking if player (sd) has an invalid item
  9652. * and is unequiped on map load (item_noequip)
  9653. *------------------------------------------*/
  9654. void pc_checkitem(struct map_session_data *sd) {
  9655. int i, calc_flag = 0;
  9656. struct item* it;
  9657. nullpo_retv(sd);
  9658. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9659. return;
  9660. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9661. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9662. it = &sd->inventory.u.items_inventory[i];
  9663. if( it->nameid == 0 )
  9664. continue;
  9665. if( !it->equip )
  9666. continue;
  9667. if( it->equip&~pc_equippoint(sd,i) ) {
  9668. pc_unequipitem(sd, i, 2);
  9669. calc_flag = 1;
  9670. continue;
  9671. }
  9672. 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) ) {
  9673. pc_unequipitem(sd, i, 2);
  9674. calc_flag = 1;
  9675. continue;
  9676. }
  9677. }
  9678. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9679. it = &sd->inventory.u.items_inventory[i];
  9680. if( it->nameid == 0 )
  9681. continue;
  9682. if( !it->equipSwitch )
  9683. continue;
  9684. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9685. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9686. for( int j = 0; j < EQI_MAX; j++ ){
  9687. if( sd->equip_switch_index[j] == i ){
  9688. sd->equip_switch_index[j] = -1;
  9689. }
  9690. }
  9691. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9692. continue;
  9693. }
  9694. }
  9695. if( calc_flag && sd->state.active ) {
  9696. pc_checkallowskill(sd);
  9697. status_calc_pc(sd,SCO_NONE);
  9698. }
  9699. }
  9700. /*==========================================
  9701. * Checks for unavailable items and removes them.
  9702. * @param sd: Player data
  9703. * @param type Forced check:
  9704. * 1 - Inventory
  9705. * 2 - Cart
  9706. * 4 - Storage
  9707. *------------------------------------------*/
  9708. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9709. {
  9710. int i;
  9711. t_itemid nameid;
  9712. char output[256];
  9713. nullpo_retv(sd);
  9714. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9715. for(i = 0; i < MAX_INVENTORY; i++) {
  9716. nameid = sd->inventory.u.items_inventory[i].nameid;
  9717. if (!nameid)
  9718. continue;
  9719. if (!itemdb_available(nameid)) {
  9720. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  9721. clif_displaymessage(sd->fd, output);
  9722. 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);
  9723. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9724. continue;
  9725. }
  9726. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9727. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9728. }
  9729. }
  9730. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9731. for(i = 0; i < MAX_CART; i++) {
  9732. nameid = sd->cart.u.items_cart[i].nameid;
  9733. if (!nameid)
  9734. continue;
  9735. if (!itemdb_available(nameid)) {
  9736. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  9737. clif_displaymessage(sd->fd, output);
  9738. 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);
  9739. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9740. continue;
  9741. }
  9742. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9743. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9744. }
  9745. }
  9746. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9747. for(i = 0; i < sd->storage.max_amount; i++) {
  9748. nameid = sd->storage.u.items_storage[i].nameid;
  9749. if (!nameid)
  9750. continue;
  9751. if (!itemdb_available(nameid)) {
  9752. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  9753. clif_displaymessage(sd->fd, output);
  9754. 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);
  9755. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9756. continue;
  9757. }
  9758. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9759. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9760. }
  9761. }
  9762. }
  9763. /*==========================================
  9764. * Update PVP rank for sd1 in cmp to sd2
  9765. *------------------------------------------*/
  9766. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9767. {
  9768. struct map_session_data *sd1,*sd2;
  9769. sd1=(struct map_session_data *)bl;
  9770. sd2=va_arg(ap,struct map_session_data *);
  9771. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9772. {// cannot register pvp rank for hidden GMs
  9773. return 0;
  9774. }
  9775. if( sd1->pvp_point > sd2->pvp_point )
  9776. sd2->pvp_rank++;
  9777. return 0;
  9778. }
  9779. /*==========================================
  9780. * Calculate new rank beetween all present players (map_foreachinallarea)
  9781. * and display result
  9782. *------------------------------------------*/
  9783. int pc_calc_pvprank(struct map_session_data *sd)
  9784. {
  9785. int old = sd->pvp_rank;
  9786. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9787. sd->pvp_rank=1;
  9788. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9789. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9790. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9791. return sd->pvp_rank;
  9792. }
  9793. /*==========================================
  9794. * Calculate next sd ranking calculation from config
  9795. *------------------------------------------*/
  9796. TIMER_FUNC(pc_calc_pvprank_timer){
  9797. struct map_session_data *sd;
  9798. sd=map_id2sd(id);
  9799. if(sd==NULL)
  9800. return 0;
  9801. sd->pvp_timer = INVALID_TIMER;
  9802. if( pc_isinvisible(sd) )
  9803. {// do not calculate the pvp rank for a hidden GM
  9804. return 0;
  9805. }
  9806. if( pc_calc_pvprank(sd) > 0 )
  9807. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9808. return 0;
  9809. }
  9810. /*==========================================
  9811. * Checking if sd is married
  9812. * Return:
  9813. * partner_id = yes
  9814. * 0 = no
  9815. *------------------------------------------*/
  9816. int pc_ismarried(struct map_session_data *sd)
  9817. {
  9818. if(sd == NULL)
  9819. return -1;
  9820. if(sd->status.partner_id > 0)
  9821. return sd->status.partner_id;
  9822. else
  9823. return 0;
  9824. }
  9825. /*==========================================
  9826. * Marry player sd to player dstsd
  9827. * Return:
  9828. * false = fail
  9829. * true = success
  9830. *------------------------------------------*/
  9831. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9832. {
  9833. if(sd == NULL || dstsd == NULL ||
  9834. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9835. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9836. return false;
  9837. sd->status.partner_id = dstsd->status.char_id;
  9838. dstsd->status.partner_id = sd->status.char_id;
  9839. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9840. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9841. return true;
  9842. }
  9843. /*==========================================
  9844. * Divorce sd from its partner
  9845. * Return:
  9846. * false = fail
  9847. * true = success
  9848. *------------------------------------------*/
  9849. bool pc_divorce(struct map_session_data *sd)
  9850. {
  9851. struct map_session_data *p_sd;
  9852. int i;
  9853. if( sd == NULL || !pc_ismarried(sd) )
  9854. return false;
  9855. if( !sd->status.partner_id )
  9856. return false; // Char is not married
  9857. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9858. { // Lets char server do the divorce
  9859. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9860. return false; // No char server connected
  9861. return true;
  9862. }
  9863. // Both players online, lets do the divorce manually
  9864. sd->status.partner_id = 0;
  9865. p_sd->status.partner_id = 0;
  9866. for( i = 0; i < MAX_INVENTORY; i++ )
  9867. {
  9868. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9869. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9870. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9871. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9872. }
  9873. clif_divorced(sd, p_sd->status.name);
  9874. clif_divorced(p_sd, sd->status.name);
  9875. return true;
  9876. }
  9877. /**
  9878. * Get the partner map_session_data of a player
  9879. * @param sd : the husband|wife session
  9880. * @return partner session or NULL
  9881. */
  9882. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9883. if (!sd || !pc_ismarried(sd))
  9884. return NULL;
  9885. return map_charid2sd(sd->status.partner_id);
  9886. }
  9887. /**
  9888. * Get the father map_session_data of a player
  9889. * @param sd : the baby session
  9890. * @return father session or NULL
  9891. */
  9892. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9893. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9894. return NULL;
  9895. return map_charid2sd(sd->status.father);
  9896. }
  9897. /**
  9898. * Get the mother map_session_data of a player
  9899. * @param sd : the baby session
  9900. * @return mother session or NULL
  9901. */
  9902. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9903. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9904. return NULL;
  9905. return map_charid2sd(sd->status.mother);
  9906. }
  9907. /*==========================================
  9908. * Get sd children charid. (Need to be married)
  9909. *------------------------------------------*/
  9910. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9911. {
  9912. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9913. // charid2sd returns NULL if not found
  9914. return NULL;
  9915. return map_charid2sd(sd->status.child);
  9916. }
  9917. /*==========================================
  9918. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9919. *------------------------------------------*/
  9920. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9921. {
  9922. int hp = 0, sp = 0;
  9923. if( pc_isdead(sd) )
  9924. return;
  9925. if (sd->hp_loss.value) {
  9926. sd->hp_loss.tick += diff_tick;
  9927. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9928. hp += sd->hp_loss.value;
  9929. sd->hp_loss.tick -= sd->hp_loss.rate;
  9930. }
  9931. if(hp >= sd->battle_status.hp)
  9932. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9933. }
  9934. if (sd->sp_loss.value) {
  9935. sd->sp_loss.tick += diff_tick;
  9936. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9937. sp += sd->sp_loss.value;
  9938. sd->sp_loss.tick -= sd->sp_loss.rate;
  9939. }
  9940. }
  9941. if (hp > 0 || sp > 0)
  9942. status_zap(&sd->bl, hp, sp);
  9943. }
  9944. //Character regen. Flag is used to know which types of regen can take place.
  9945. //&1: HP regen
  9946. //&2: SP regen
  9947. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9948. {
  9949. int hp = 0, sp = 0;
  9950. if (sd->hp_regen.value) {
  9951. sd->hp_regen.tick += diff_tick;
  9952. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9953. hp += sd->hp_regen.value;
  9954. sd->hp_regen.tick -= sd->hp_regen.rate;
  9955. }
  9956. }
  9957. if (sd->sp_regen.value) {
  9958. sd->sp_regen.tick += diff_tick;
  9959. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9960. sp += sd->sp_regen.value;
  9961. sd->sp_regen.tick -= sd->sp_regen.rate;
  9962. }
  9963. }
  9964. if (sd->percent_hp_regen.value) {
  9965. sd->percent_hp_regen.tick += diff_tick;
  9966. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9967. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9968. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9969. }
  9970. }
  9971. if (sd->percent_sp_regen.value) {
  9972. sd->percent_sp_regen.tick += diff_tick;
  9973. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9974. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9975. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9976. }
  9977. }
  9978. if (hp > 0 || sp > 0)
  9979. status_heal(&sd->bl, hp, sp, 0);
  9980. }
  9981. /*==========================================
  9982. * Memo player sd savepoint. (map,x,y)
  9983. *------------------------------------------*/
  9984. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9985. {
  9986. nullpo_retv(sd);
  9987. sd->status.save_point.map = mapindex;
  9988. sd->status.save_point.x = x;
  9989. sd->status.save_point.y = y;
  9990. }
  9991. /*==========================================
  9992. * Save 1 player data at autosave interval
  9993. *------------------------------------------*/
  9994. static TIMER_FUNC(pc_autosave){
  9995. int interval;
  9996. struct s_mapiterator* iter;
  9997. struct map_session_data* sd;
  9998. static int last_save_id = 0, save_flag = 0;
  9999. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10000. save_flag = 0;
  10001. else
  10002. save_flag = 1; //Noone was saved, so save first found char.
  10003. iter = mapit_getallusers();
  10004. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10005. {
  10006. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10007. continue;
  10008. if(sd->bl.id == last_save_id && save_flag != 1) {
  10009. save_flag = 1;
  10010. continue;
  10011. }
  10012. if(save_flag != 1) //Not our turn to save yet.
  10013. continue;
  10014. //Save char.
  10015. last_save_id = sd->bl.id;
  10016. save_flag = 2;
  10017. if (pc_isvip(sd)) // Check if we're still VIP
  10018. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10019. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10020. break;
  10021. }
  10022. mapit_free(iter);
  10023. interval = autosave_interval/(map_usercount()+1);
  10024. if(interval < minsave_interval)
  10025. interval = minsave_interval;
  10026. add_timer(gettick()+interval,pc_autosave,0,0);
  10027. return 0;
  10028. }
  10029. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  10030. {
  10031. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  10032. { //Night/day state does not match.
  10033. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  10034. sd->state.night = night_flag;
  10035. return 1;
  10036. }
  10037. return 0;
  10038. }
  10039. /*================================================
  10040. * timer to do the day [Yor]
  10041. * data: 0 = called by timer, 1 = gmcommand/script
  10042. *------------------------------------------------*/
  10043. TIMER_FUNC(map_day_timer){
  10044. char tmp_soutput[1024];
  10045. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  10046. return 0;
  10047. if (!night_flag)
  10048. return 0; //Already day.
  10049. night_flag = 0; // 0=day, 1=night [Yor]
  10050. map_foreachpc(pc_daynight_timer_sub);
  10051. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  10052. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10053. return 0;
  10054. }
  10055. /*================================================
  10056. * timer to do the night [Yor]
  10057. * data: 0 = called by timer, 1 = gmcommand/script
  10058. *------------------------------------------------*/
  10059. TIMER_FUNC(map_night_timer){
  10060. char tmp_soutput[1024];
  10061. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  10062. return 0;
  10063. if (night_flag)
  10064. return 0; //Already nigth.
  10065. night_flag = 1; // 0=day, 1=night [Yor]
  10066. map_foreachpc(pc_daynight_timer_sub);
  10067. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  10068. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10069. return 0;
  10070. }
  10071. /**
  10072. * Attempt to stand up a player
  10073. * @param sd
  10074. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  10075. * @return True if success, Fals if failed
  10076. */
  10077. bool pc_setstand(struct map_session_data *sd, bool force){
  10078. nullpo_ret(sd);
  10079. // Cannot stand yet
  10080. // TODO: Move to SCS_NOSTAND [Cydh]
  10081. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  10082. return false;
  10083. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  10084. clif_status_load(&sd->bl,EFST_SIT,0);
  10085. clif_standing(&sd->bl); //Inform area PC is standing
  10086. //Reset sitting tick.
  10087. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10088. if( pc_isdead( sd ) ){
  10089. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10090. clif_party_dead( sd );
  10091. }else{
  10092. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10093. }
  10094. return true;
  10095. }
  10096. /**
  10097. * Calculate Overheat value
  10098. * @param sd: Player data
  10099. * @param heat: Amount of Heat to adjust
  10100. **/
  10101. void pc_overheat(struct map_session_data *sd, int16 heat) {
  10102. nullpo_retv(sd);
  10103. status_change_entry *sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT];
  10104. if (sce) {
  10105. static std::vector<int16> limit = { 150, 200, 280, 360, 450 };
  10106. uint16 skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, (uint16)(limit.size()-1));
  10107. sce->val1 += heat;
  10108. sce->val1 = cap_value(sce->val1, 0, 1000);
  10109. if (sd->sc.data[SC_OVERHEAT])
  10110. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  10111. if (sce->val1 > limit[skill_lv])
  10112. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  10113. } else if (heat > 0)
  10114. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10115. }
  10116. /**
  10117. * Check if player is autolooting given itemID.
  10118. */
  10119. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10120. {
  10121. uint8 i = 0;
  10122. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10123. return true;
  10124. if (!sd->state.autolooting)
  10125. return false;
  10126. if (sd->state.autolooting)
  10127. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10128. return (i != AUTOLOOTITEM_SIZE);
  10129. }
  10130. /**
  10131. * Checks if player can use @/#command
  10132. * @param sd Player map session data
  10133. * @param command Command name without @/# and params
  10134. * @param type is it atcommand or charcommand
  10135. */
  10136. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  10137. {
  10138. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  10139. }
  10140. /**
  10141. * Checks if commands used by a player should be logged
  10142. * according to their group setting.
  10143. * @param sd Player map session data
  10144. */
  10145. bool pc_should_log_commands(struct map_session_data *sd)
  10146. {
  10147. return pc_group_should_log_commands(pc_get_group_id(sd));
  10148. }
  10149. /**
  10150. * Spirit Charm expiration timer.
  10151. * @see TimerFunc
  10152. */
  10153. static TIMER_FUNC(pc_spiritcharm_timer){
  10154. struct map_session_data *sd;
  10155. int i;
  10156. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10157. return 1;
  10158. if (sd->spiritcharm <= 0) {
  10159. 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);
  10160. sd->spiritcharm = 0;
  10161. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10162. return 0;
  10163. }
  10164. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10165. if (i == sd->spiritcharm) {
  10166. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10167. return 0;
  10168. }
  10169. sd->spiritcharm--;
  10170. if (i != sd->spiritcharm)
  10171. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10172. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10173. if (sd->spiritcharm <= 0)
  10174. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10175. clif_spiritcharm(sd);
  10176. return 0;
  10177. }
  10178. /**
  10179. * Adds a spirit charm.
  10180. * @param sd: Target character
  10181. * @param interval: Duration
  10182. * @param max: Maximum amount of charms to add
  10183. * @param type: Charm type (@see spirit_charm_types)
  10184. */
  10185. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10186. {
  10187. int tid, i;
  10188. nullpo_retv(sd);
  10189. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10190. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10191. if (max > MAX_SPIRITCHARM)
  10192. max = MAX_SPIRITCHARM;
  10193. if (sd->spiritcharm < 0)
  10194. sd->spiritcharm = 0;
  10195. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10196. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10197. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10198. sd->spiritcharm--;
  10199. if (sd->spiritcharm != 0)
  10200. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10201. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10202. }
  10203. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10204. 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);
  10205. if (i != sd->spiritcharm)
  10206. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10207. sd->spiritcharm_timer[i] = tid;
  10208. sd->spiritcharm++;
  10209. sd->spiritcharm_type = type;
  10210. clif_spiritcharm(sd);
  10211. }
  10212. /**
  10213. * Removes one or more spirit charms.
  10214. * @param sd: The target character
  10215. * @param count: Amount of charms to remove
  10216. * @param type: Type of charm to remove
  10217. */
  10218. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10219. {
  10220. int i;
  10221. nullpo_retv(sd);
  10222. if (sd->spiritcharm_type != type)
  10223. return;
  10224. if (sd->spiritcharm <= 0) {
  10225. sd->spiritcharm = 0;
  10226. return;
  10227. }
  10228. if (count <= 0)
  10229. return;
  10230. if (count > sd->spiritcharm)
  10231. count = sd->spiritcharm;
  10232. sd->spiritcharm -= count;
  10233. if (count > MAX_SPIRITCHARM)
  10234. count = MAX_SPIRITCHARM;
  10235. for (i = 0; i < count; i++) {
  10236. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10237. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10238. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10239. }
  10240. }
  10241. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10242. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10243. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10244. }
  10245. if (sd->spiritcharm <= 0)
  10246. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10247. clif_spiritcharm(sd);
  10248. }
  10249. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10250. /**
  10251. * Renewal EXP/Item Drop rate modifier based on level penalty
  10252. * @param level_diff: Monster and Player level difference
  10253. * @param mob_class: Monster class
  10254. * @param mode: Monster mode
  10255. * @param type: 1 - EXP, 2 - Item Drop
  10256. * @return Penalty rate
  10257. */
  10258. uint16 pc_level_penalty_mod( struct map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  10259. // No player was attached, we don't use any modifier (100 = rates are not touched)
  10260. if( sd == nullptr ){
  10261. return 100;
  10262. }
  10263. int monster_level;
  10264. if( md != nullptr ){
  10265. monster_level = md->level;
  10266. mob = md->db;
  10267. }else if( mob != nullptr ){
  10268. monster_level = mob->lv;
  10269. }else{
  10270. return 100;
  10271. }
  10272. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  10273. return 100;
  10274. }
  10275. int level_difference = monster_level - sd->status.base_level;
  10276. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  10277. if( penalty != nullptr ){
  10278. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  10279. }else{
  10280. return 100;
  10281. }
  10282. }
  10283. #endif
  10284. int pc_split_str(char *str,char **val,int num)
  10285. {
  10286. int i;
  10287. for (i=0; i<num && str; i++){
  10288. val[i] = str;
  10289. str = strchr(str,',');
  10290. if (str && i<num-1) //Do not remove a trailing comma.
  10291. *str++=0;
  10292. }
  10293. return i;
  10294. }
  10295. int pc_split_atoi(char* str, int* val, char sep, int max)
  10296. {
  10297. int i,j;
  10298. for (i=0; i<max; i++) {
  10299. if (!str) break;
  10300. val[i] = atoi(str);
  10301. str = strchr(str,sep);
  10302. if (str)
  10303. *str++=0;
  10304. }
  10305. //Zero up the remaining.
  10306. for(j=i; j < max; j++)
  10307. val[j] = 0;
  10308. return i;
  10309. }
  10310. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10311. {
  10312. static int warning=0;
  10313. int i,j;
  10314. for (i=0; i<max; i++) {
  10315. double f;
  10316. if (!str) break;
  10317. f = atof(str);
  10318. if (f < 0)
  10319. val[i] = 0;
  10320. else if (f > UINT_MAX) {
  10321. val[i] = UINT_MAX;
  10322. if (!warning) {
  10323. warning = 1;
  10324. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10325. }
  10326. } else
  10327. val[i] = (unsigned int)f;
  10328. str = strchr(str,sep);
  10329. if (str)
  10330. *str++=0;
  10331. }
  10332. //Zero up the remaining.
  10333. for(j=i; j < max; j++)
  10334. val[j] = 0;
  10335. return i;
  10336. }
  10337. /*==========================================
  10338. * sub DB reading.
  10339. * Function used to read skill_tree.txt
  10340. *------------------------------------------*/
  10341. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10342. {
  10343. uint32 baselv, joblv, baselv_max, joblv_max;
  10344. uint16 skill_id, skill_lv, skill_lv_max;
  10345. int idx, class_;
  10346. unsigned int i, offset, skill_idx;
  10347. class_ = atoi(fields[0]);
  10348. skill_id = (uint16)atoi(fields[1]);
  10349. skill_lv = (uint16)atoi(fields[2]);
  10350. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10351. baselv = (uint32)atoi(fields[3]);
  10352. joblv = (uint32)atoi(fields[4]);
  10353. offset = 5;
  10354. }
  10355. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10356. baselv = joblv = 0;
  10357. offset = 3;
  10358. }
  10359. else {
  10360. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10361. return false;
  10362. }
  10363. if(!pcdb_checkid(class_))
  10364. {
  10365. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10366. return false;
  10367. }
  10368. idx = pc_class2idx(class_);
  10369. if (!skill_get_index(skill_id)) {
  10370. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10371. return false;
  10372. }
  10373. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10374. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10375. skill_lv = skill_lv_max;
  10376. }
  10377. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10378. ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
  10379. baselv = baselv_max;
  10380. }
  10381. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10382. ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
  10383. joblv = joblv_max;
  10384. }
  10385. //This is to avoid adding two lines for the same skill. [Skotlex]
  10386. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
  10387. if( skill_idx == MAX_SKILL_TREE )
  10388. {
  10389. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
  10390. return false;
  10391. }
  10392. else if(skill_tree[idx][skill_idx].skill_id)
  10393. {
  10394. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10395. }
  10396. skill_tree[idx][skill_idx].skill_id = skill_id;
  10397. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10398. skill_tree[idx][skill_idx].baselv = baselv;
  10399. skill_tree[idx][skill_idx].joblv = joblv;
  10400. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10401. {
  10402. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10403. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10404. if (skill_id == 0) {
  10405. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10406. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10407. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10408. }
  10409. continue;
  10410. }
  10411. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10412. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10413. return false;
  10414. }
  10415. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10416. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10417. skill_lv = skill_lv_max;
  10418. }
  10419. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10420. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10421. }
  10422. return true;
  10423. }
  10424. /** [Cydh]
  10425. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10426. * @param level Base level of player
  10427. * @param class_ Job ID @see enum e_job
  10428. * @return base_hp
  10429. */
  10430. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10431. double base_hp;
  10432. uint16 i, idx = pc_class2idx(class_);
  10433. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10434. #ifndef RENEWAL
  10435. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10436. #endif
  10437. for (i = 2; i <= level; i++)
  10438. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10439. if (class_ == JOB_SUMMONER)
  10440. base_hp += floor((base_hp / 2) + 0.5);
  10441. return (unsigned int)base_hp;
  10442. }
  10443. /** [Playtester]
  10444. * Calculates base sp of player.
  10445. * @param level Base level of player
  10446. * @param class_ Job ID @see enum e_job
  10447. * @return base_sp
  10448. */
  10449. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10450. double base_sp;
  10451. uint16 idx = pc_class2idx(class_);
  10452. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10453. switch (class_) {
  10454. case JOB_NINJA:
  10455. if (level >= 10)
  10456. base_sp -= 22;
  10457. else
  10458. base_sp = 11 + 3*level;
  10459. break;
  10460. case JOB_GUNSLINGER:
  10461. if (level > 10)
  10462. base_sp -= 18;
  10463. else
  10464. base_sp = 9 + 3*level;
  10465. break;
  10466. case JOB_SUMMONER:
  10467. base_sp -= floor(base_sp / 2);
  10468. break;
  10469. }
  10470. return (unsigned int)base_sp;
  10471. }
  10472. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10473. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10474. int idx, class_;
  10475. unsigned int i;
  10476. class_ = atoi(fields[0]);
  10477. if (!pcdb_checkid(class_)) {
  10478. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10479. return false;
  10480. }
  10481. idx = pc_class2idx(class_);
  10482. job_info[idx].max_weight_base = atoi(fields[1]);
  10483. job_info[idx].hp_factor = atoi(fields[2]);
  10484. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10485. job_info[idx].sp_factor = atoi(fields[4]);
  10486. #ifdef RENEWAL_ASPD
  10487. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10488. #else
  10489. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10490. #endif
  10491. {
  10492. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10493. }
  10494. return true;
  10495. }
  10496. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10497. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10498. {
  10499. int idx, class_, i;
  10500. class_ = atoi(fields[0]);
  10501. if(!pcdb_checkid(class_))
  10502. {
  10503. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10504. return false;
  10505. }
  10506. idx = pc_class2idx(class_);
  10507. for(i = 1; i < columns; i++)
  10508. {
  10509. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10510. }
  10511. return true;
  10512. }
  10513. //Reading job_exp.txt line
  10514. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10515. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10516. {
  10517. int idx, i, type;
  10518. int job_id,job_count,jobs[CLASS_COUNT];
  10519. unsigned int ui, maxlvl;
  10520. maxlvl = atoi(fields[0]);
  10521. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10522. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10523. return false;
  10524. }
  10525. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10526. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10527. return false;
  10528. }
  10529. type = atoi(fields[2]);
  10530. if(type < 0 || type > 1){
  10531. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10532. return false;
  10533. }
  10534. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10535. if (job_count < 1)
  10536. return false;
  10537. job_id = jobs[0];
  10538. if(!pcdb_checkid(job_id)){
  10539. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10540. return false;
  10541. }
  10542. idx = pc_class2idx(job_id);
  10543. job_info[idx].max_level[type] = maxlvl;
  10544. for(i=0; i<maxlvl; i++){
  10545. t_exp exp = strtoull( fields[3 + i], nullptr, 10 );
  10546. if( exp == 0 ){
  10547. ShowWarning( "pc_readdb_job_exp: No value defined for level %d in line %d. Defaulting to MAX_EXP...\n", i + 1, current );
  10548. exp = MAX_EXP;
  10549. }else if( exp > MAX_EXP ){
  10550. ShowWarning( "pc_readdb_job_exp: Value %" PRIu64 " is too high, capping to %" PRIu64 "...\n", exp, MAX_EXP );
  10551. exp = MAX_EXP;
  10552. }
  10553. job_info[idx].exp_table[type][i] = exp;
  10554. }
  10555. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10556. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10557. //should look like this:
  10558. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10559. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10560. job_info[idx].max_level[type]--;
  10561. if (job_info[idx].max_level[type] < maxlvl) {
  10562. ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
  10563. ShowInfo("Filling the missing values with the last exp entry.\n");
  10564. //Fill the requested values with the last entry.
  10565. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10566. for (; ui+2 < maxlvl; ui++)
  10567. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10568. job_info[idx].max_level[type] = maxlvl;
  10569. }
  10570. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10571. for (i = 1; i < job_count; i++) {
  10572. job_id = jobs[i];
  10573. if (!pcdb_checkid(job_id)) {
  10574. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10575. continue;
  10576. }
  10577. idx = pc_class2idx(job_id);
  10578. memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
  10579. job_info[idx].max_level[type] = maxlvl;
  10580. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10581. }
  10582. return true;
  10583. }
  10584. /**
  10585. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10586. * startlvl,endlvl,class,type,values...
  10587. */
  10588. #ifdef HP_SP_TABLES
  10589. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10590. {
  10591. int i, startlvl, endlvl;
  10592. int job_count,jobs[CLASS_COUNT];
  10593. short type;
  10594. startlvl = atoi(fields[0]);
  10595. if(startlvl<1){
  10596. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10597. return false;
  10598. }
  10599. endlvl = atoi(fields[1]);
  10600. if(endlvl<1 || endlvl<startlvl){
  10601. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10602. return false;
  10603. }
  10604. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10605. ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
  10606. return false;
  10607. }
  10608. type = atoi(fields[3]);
  10609. if(type < 0 || type > 1){
  10610. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10611. return false;
  10612. }
  10613. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10614. if (job_count < 1)
  10615. return false;
  10616. for (i = 0; i < job_count; i++) {
  10617. int idx, job_id = jobs[i], use_endlvl;
  10618. if (!pcdb_checkid(job_id)) {
  10619. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10620. return false;
  10621. }
  10622. idx = pc_class2idx(job_id);
  10623. if (startlvl > job_info[idx].max_level[0]) {
  10624. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10625. return false;
  10626. }
  10627. //Just read until available max level for this job, don't use MAX_LEVEL!
  10628. use_endlvl = endlvl;
  10629. if (use_endlvl > job_info[idx].max_level[0])
  10630. use_endlvl = job_info[idx].max_level[0];
  10631. if(type == 0) { //hp type
  10632. uint16 j;
  10633. for(j = 0; j < use_endlvl; j++) {
  10634. if (atoi(fields[j+4])) {
  10635. uint16 lvl_idx = startlvl-1+j;
  10636. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10637. //Tells if this HP is lower than previous level (but not for 99->100)
  10638. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10639. ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10640. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10641. }
  10642. }
  10643. }
  10644. else { //sp type
  10645. uint16 j;
  10646. for(j = 0; j < use_endlvl; j++) {
  10647. if (atoi(fields[j+4])) {
  10648. uint16 lvl_idx = startlvl-1+j;
  10649. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10650. //Tells if this SP is lower than previous level (but not for 99->100)
  10651. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10652. ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10653. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10654. }
  10655. }
  10656. }
  10657. }
  10658. return true;
  10659. }
  10660. #endif
  10661. /** [Cydh]
  10662. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10663. */
  10664. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10665. {
  10666. int64 class_tmp;
  10667. int idx, class_;
  10668. uint16 str, agi, vit, int_, dex, luk;
  10669. script_get_constant(trim(fields[0]),&class_tmp);
  10670. class_ = static_cast<int>(class_tmp);
  10671. if ((idx = pc_class2idx(class_)) < 0) {
  10672. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10673. return false;
  10674. }
  10675. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10676. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10677. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10678. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10679. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10680. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10681. job_info[idx].max_param.str = str;
  10682. job_info[idx].max_param.agi = agi;
  10683. job_info[idx].max_param.vit = vit;
  10684. job_info[idx].max_param.int_ = int_;
  10685. job_info[idx].max_param.dex = dex;
  10686. job_info[idx].max_param.luk = luk;
  10687. return true;
  10688. }
  10689. /**
  10690. * Read job_noenter_map.txt
  10691. **/
  10692. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10693. int idx, class_ = -1;
  10694. int64 class_tmp;
  10695. if (ISDIGIT(str[0][0])) {
  10696. class_ = atoi(str[0]);
  10697. } else {
  10698. if (!script_get_constant(str[0], &class_tmp)) {
  10699. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10700. return false;
  10701. }
  10702. class_ = static_cast<int>(class_tmp);
  10703. }
  10704. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10705. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10706. return false;
  10707. }
  10708. job_info[idx].noenter_map.zone = atoi(str[1]);
  10709. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10710. return true;
  10711. }
  10712. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  10713. return std::string(db_path) + "/statpoint.yml";
  10714. }
  10715. uint64 PlayerStatPointDatabase::parseBodyNode(const YAML::Node &node) {
  10716. if (!this->nodesExist(node, { "Level", "Points" })) {
  10717. return 0;
  10718. }
  10719. uint16 level;
  10720. if (!this->asUInt16(node, "Level", level))
  10721. return 0;
  10722. uint32 point;
  10723. if (!this->asUInt32(node, "Points", point))
  10724. return 0;
  10725. if (level == 0) {
  10726. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  10727. return 0;
  10728. }
  10729. if (level > MAX_LEVEL) {
  10730. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  10731. return 0;
  10732. }
  10733. this->statpoint_table[level] = point;
  10734. return 1;
  10735. }
  10736. /**
  10737. * Generate the remaining parts of the db if necessary.
  10738. */
  10739. void PlayerStatPointDatabase::loadingFinished() {
  10740. if( battle_config.use_statpoint_table ){
  10741. this->statpoint_table[1] = start_status_points;
  10742. }
  10743. if( this->statpoint_table[1] != start_status_points ){
  10744. ShowError( "Status points for Level 1 (=%d) do not match inter_athena.conf value (=%d).\n", this->statpoint_table[1], start_status_points );
  10745. this->statpoint_table[1] = start_status_points;
  10746. }
  10747. for (uint16 level = 2; level <= MAX_LEVEL; level++) {
  10748. if (!battle_config.use_statpoint_table || util::umap_find(this->statpoint_table, level) == nullptr) {
  10749. if (battle_config.use_statpoint_table)
  10750. ShowError("Missing status points for Level %d\n", level);
  10751. this->statpoint_table[level] = this->statpoint_table[level-1] + ((level-1+15) / 5);
  10752. }
  10753. }
  10754. }
  10755. /*==========================================
  10756. * pc DB reading.
  10757. * job_exp.txt - required experience values
  10758. * skill_tree.txt - skill tree for every class
  10759. * attr_fix.yml - elemental adjustment table
  10760. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10761. * job_db2.txt - job,stats bonuses/lvl
  10762. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10763. *------------------------------------------*/
  10764. void pc_readdb(void) {
  10765. int i, s = 1;
  10766. const char* dbsubpath[] = {
  10767. "",
  10768. "/" DBIMPORT,
  10769. //add other path here
  10770. };
  10771. //reset
  10772. memset(job_info,0,sizeof(job_info)); // job_info table
  10773. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10774. penalty_db.load();
  10775. #endif
  10776. statpoint_db.clear();
  10777. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10778. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10779. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10780. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10781. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10782. if(i==0) {
  10783. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10784. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10785. }
  10786. else {
  10787. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10788. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10789. }
  10790. if (i == 0)
  10791. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10792. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10793. #else
  10794. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10795. #endif
  10796. else
  10797. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10798. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10799. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10800. #ifdef HP_SP_TABLES
  10801. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
  10802. #endif
  10803. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10804. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10805. aFree(dbsubpath1);
  10806. aFree(dbsubpath2);
  10807. }
  10808. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10809. memset(skill_tree, 0, sizeof(skill_tree));
  10810. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
  10811. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
  10812. statpoint_db.load();
  10813. //Checking if all class have their data
  10814. for (i = 0; i < JOB_MAX; i++) {
  10815. int idx;
  10816. uint16 j;
  10817. if (!pcdb_checkid(i))
  10818. continue;
  10819. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10820. continue; //Classes that do not need exp tables.
  10821. idx = pc_class2idx(i);
  10822. if (!job_info[idx].max_level[0])
  10823. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10824. if (!job_info[idx].max_level[1])
  10825. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10826. //Init and checking the empty value of Base HP/SP [Cydh]
  10827. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10828. if (job_info[idx].base_hp[j] == 0)
  10829. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10830. if (job_info[idx].base_sp[j] == 0)
  10831. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10832. }
  10833. }
  10834. }
  10835. // Read MOTD on startup. [Valaris]
  10836. int pc_read_motd(void)
  10837. {
  10838. FILE* fp;
  10839. // clear old MOTD
  10840. memset(motd_text, 0, sizeof(motd_text));
  10841. // read current MOTD
  10842. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10843. {
  10844. unsigned int entries = 0;
  10845. char buf[CHAT_SIZE_MAX];
  10846. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10847. {
  10848. unsigned int lines = 0;
  10849. size_t len;
  10850. lines++;
  10851. if( buf[0] == '/' && buf[1] == '/' )
  10852. continue;
  10853. len = strlen(buf);
  10854. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10855. len--;
  10856. if( len ) {
  10857. char * ptr;
  10858. buf[len] = 0;
  10859. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10860. 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);
  10861. }
  10862. else {// empty line
  10863. buf[0] = ' ';
  10864. buf[1] = 0;
  10865. }
  10866. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10867. entries++;
  10868. }
  10869. fclose(fp);
  10870. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10871. }
  10872. else
  10873. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10874. return 0;
  10875. }
  10876. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10877. int i,cursor = 0;
  10878. struct item_cd* cd = NULL;
  10879. if( load ) {
  10880. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10881. // no item cooldown is associated with this character
  10882. return;
  10883. }
  10884. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10885. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10886. sd->item_delay[cursor].tick = cd->tick[i];
  10887. sd->item_delay[cursor].nameid = cd->nameid[i];
  10888. cursor++;
  10889. }
  10890. }
  10891. idb_remove(itemcd_db,sd->status.char_id);
  10892. } else {
  10893. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10894. // create a new skill cooldown object for map storage
  10895. CREATE( cd, struct item_cd, 1 );
  10896. idb_put( itemcd_db, sd->status.char_id, cd );
  10897. }
  10898. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10899. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10900. cd->tick[cursor] = sd->item_delay[i].tick;
  10901. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10902. cursor++;
  10903. }
  10904. }
  10905. }
  10906. return;
  10907. }
  10908. /**
  10909. * Add item delay to player's item delay data
  10910. * @param sd Player
  10911. * @param id Item data
  10912. * @param tick Current tick
  10913. * @param n Item index in inventory
  10914. * @return 0: No delay, can consume item.
  10915. * 1: Has delay, cancel consumption.
  10916. **/
  10917. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10918. int i;
  10919. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10920. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10921. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10922. if( i < MAX_ITEMDELAYS ) {
  10923. if( sd->item_delay[i].nameid ) {// found
  10924. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10925. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10926. char e_msg[CHAT_SIZE_MAX];
  10927. if( e_tick > 99 )
  10928. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10929. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  10930. else
  10931. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10932. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  10933. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10934. return 1; // Delay has not expired yet
  10935. }
  10936. } else {// not yet used item (all slots are initially empty)
  10937. sd->item_delay[i].nameid = id->nameid;
  10938. }
  10939. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10940. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  10941. } else {// should not happen
  10942. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  10943. }
  10944. //clean up used delays so we can give room for more
  10945. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10946. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10947. sd->item_delay[i].tick = 0;
  10948. sd->item_delay[i].nameid = 0;
  10949. }
  10950. }
  10951. return 0;
  10952. }
  10953. /**
  10954. * Check if player has delay to reuse item
  10955. * @param sd Player
  10956. * @param id Item data
  10957. * @param tick Current tick
  10958. * @param n Item index in inventory
  10959. * @return 0: No delay, can consume item.
  10960. * 1: Has delay, cancel consumption.
  10961. **/
  10962. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10963. struct status_change *sc = NULL;
  10964. nullpo_retr(0, sd);
  10965. nullpo_retr(0, id);
  10966. // Do normal delay assignment
  10967. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  10968. return pc_itemcd_add(sd, id, tick, n);
  10969. // Send reply of delay remains
  10970. if (sc->data[id->delay.sc]) {
  10971. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  10972. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10973. return 1;
  10974. }
  10975. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  10976. return 0;
  10977. }
  10978. /**
  10979. * Clear the dmglog data from player
  10980. * @param sd
  10981. * @param md
  10982. **/
  10983. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10984. {
  10985. uint8 i;
  10986. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10987. if (i < DAMAGELOG_SIZE) {
  10988. md->dmglog[i].id = 0;
  10989. md->dmglog[i].dmg = 0;
  10990. md->dmglog[i].flag = 0;
  10991. }
  10992. }
  10993. /**
  10994. * Add log to player's dmglog
  10995. * @param sd
  10996. * @param id Monster's GID
  10997. **/
  10998. void pc_damage_log_add(struct map_session_data *sd, int id)
  10999. {
  11000. uint8 i = 0;
  11001. if (!sd || !id)
  11002. return;
  11003. //Only store new data, don't need to renew the old one with same id
  11004. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  11005. if (i < DAMAGELOG_SIZE_PC)
  11006. return;
  11007. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  11008. if (sd->dmglog[i] == 0) {
  11009. sd->dmglog[i] = id;
  11010. return;
  11011. }
  11012. }
  11013. }
  11014. /**
  11015. * Clear dmglog data from player
  11016. * @param sd
  11017. * @param id Monster's id
  11018. **/
  11019. void pc_damage_log_clear(struct map_session_data *sd, int id)
  11020. {
  11021. uint8 i;
  11022. struct mob_data *md = NULL;
  11023. if (!sd)
  11024. return;
  11025. if (!id) {
  11026. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  11027. if( !sd->dmglog[i] ) //skip the empty value
  11028. continue;
  11029. if ((md = map_id2md(sd->dmglog[i])))
  11030. pc_clear_log_damage_sub(sd->status.char_id,md);
  11031. sd->dmglog[i] = 0;
  11032. }
  11033. }
  11034. else {
  11035. if ((md = map_id2md(id)))
  11036. pc_clear_log_damage_sub(sd->status.char_id,md);
  11037. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  11038. if (i < DAMAGELOG_SIZE_PC)
  11039. sd->dmglog[i] = 0;
  11040. }
  11041. }
  11042. /**
  11043. * Status change data arrived from char-server
  11044. * @param sd: Player data
  11045. */
  11046. void pc_scdata_received(struct map_session_data *sd) {
  11047. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  11048. clif_weight_limit( sd );
  11049. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  11050. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  11051. }
  11052. sd->state.pc_loaded = true;
  11053. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  11054. sd->state.connect_new = 1;
  11055. clif_parse_LoadEndAck(sd->fd, sd);
  11056. }
  11057. if (pc_iscarton(sd)) {
  11058. sd->cart_weight_max = 0; // Force a client refesh
  11059. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  11060. }
  11061. if (sd->sc.data[SC_SOULENERGY])
  11062. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  11063. }
  11064. /**
  11065. * Check player account expiration time and rental item expirations
  11066. * @param sd: Player data
  11067. */
  11068. void pc_check_expiration(struct map_session_data *sd) {
  11069. #ifndef ENABLE_SC_SAVING
  11070. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  11071. #endif
  11072. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  11073. time_t exp_time = sd->expiration_time;
  11074. char tmpstr[1024];
  11075. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  11076. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  11077. pc_expire_check(sd);
  11078. }
  11079. }
  11080. TIMER_FUNC(pc_expiration_timer){
  11081. struct map_session_data *sd = map_id2sd(id);
  11082. if( !sd ) return 0;
  11083. sd->expiration_tid = INVALID_TIMER;
  11084. if( sd->fd )
  11085. clif_authfail_fd(sd->fd,10);
  11086. map_quit(sd);
  11087. return 0;
  11088. }
  11089. TIMER_FUNC(pc_autotrade_timer){
  11090. struct map_session_data *sd = map_id2sd(id);
  11091. if (!sd)
  11092. return 0;
  11093. sd->autotrade_tid = INVALID_TIMER;
  11094. if (sd->state.autotrade&2)
  11095. vending_reopen(sd);
  11096. if (sd->state.autotrade&4)
  11097. buyingstore_reopen(sd);
  11098. if (!sd->vender_id && !sd->buyer_id) {
  11099. sd->state.autotrade = 0;
  11100. map_quit(sd);
  11101. }
  11102. return 0;
  11103. }
  11104. /* this timer exists only when a character with a expire timer > 24h is online */
  11105. /* it loops thru online players once an hour to check whether a new < 24h is available */
  11106. TIMER_FUNC(pc_global_expiration_timer){
  11107. struct s_mapiterator* iter;
  11108. struct map_session_data* sd;
  11109. iter = mapit_getallusers();
  11110. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11111. if( sd->expiration_time )
  11112. pc_expire_check(sd);
  11113. mapit_free(iter);
  11114. return 0;
  11115. }
  11116. void pc_expire_check(struct map_session_data *sd) {
  11117. /* ongoing timer */
  11118. if( sd->expiration_tid != INVALID_TIMER )
  11119. return;
  11120. /* not within the next 24h, enable the global check */
  11121. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  11122. /* global check not running, enable */
  11123. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  11124. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  11125. return;
  11126. }
  11127. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  11128. }
  11129. /**
  11130. * Deposit some money to bank
  11131. * @param sd
  11132. * @param money Amount of money to deposit
  11133. **/
  11134. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  11135. unsigned int limit_check = money + sd->bank_vault;
  11136. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  11137. return BDA_OVERFLOW;
  11138. } else if ( money > sd->status.zeny ) {
  11139. return BDA_NO_MONEY;
  11140. }
  11141. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11142. return BDA_NO_MONEY;
  11143. sd->bank_vault += money;
  11144. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11145. if( save_settings&CHARSAVE_BANK )
  11146. chrif_save(sd, CSAVE_NORMAL);
  11147. return BDA_SUCCESS;
  11148. }
  11149. /**
  11150. * Withdraw money from bank
  11151. * @param sd
  11152. * @param money Amount of money that will be withdrawn
  11153. **/
  11154. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  11155. unsigned int limit_check = money + sd->status.zeny;
  11156. if( money <= 0 ) {
  11157. return BWA_UNKNOWN_ERROR;
  11158. } else if ( money > sd->bank_vault ) {
  11159. return BWA_NO_MONEY;
  11160. } else if ( limit_check > MAX_ZENY ) {
  11161. /* no official response for this scenario exists. */
  11162. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  11163. return BWA_UNKNOWN_ERROR;
  11164. }
  11165. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11166. return BWA_NO_MONEY;
  11167. sd->bank_vault -= money;
  11168. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11169. if( save_settings&CHARSAVE_BANK )
  11170. chrif_save(sd, CSAVE_NORMAL);
  11171. return BWA_SUCCESS;
  11172. }
  11173. /**
  11174. * Clear Crimson Marker data from caster
  11175. * @param sd: Player
  11176. **/
  11177. void pc_crimson_marker_clear(struct map_session_data *sd) {
  11178. uint8 i;
  11179. if (!sd)
  11180. return;
  11181. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  11182. struct block_list *bl = NULL;
  11183. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  11184. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  11185. sd->c_marker[i] = 0;
  11186. }
  11187. }
  11188. /**
  11189. * Show version to player
  11190. * @param sd: Player
  11191. **/
  11192. void pc_show_version(struct map_session_data *sd) {
  11193. const char* svn = get_svn_revision();
  11194. char buf[CHAT_SIZE_MAX];
  11195. if( svn[0] != UNKNOWN_VERSION )
  11196. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  11197. else {
  11198. const char* git = get_git_hash();
  11199. if( git[0] != UNKNOWN_VERSION )
  11200. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  11201. else
  11202. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  11203. }
  11204. clif_displaymessage(sd->fd,buf);
  11205. }
  11206. /**
  11207. * Run bonus_script on player
  11208. * @param sd
  11209. * @author [Cydh]
  11210. **/
  11211. void pc_bonus_script(struct map_session_data *sd) {
  11212. t_tick now = gettick();
  11213. struct linkdb_node *node = NULL, *next = NULL;
  11214. if (!sd || !(node = sd->bonus_script.head))
  11215. return;
  11216. while (node) {
  11217. struct s_bonus_script_entry *entry = NULL;
  11218. next = node->next;
  11219. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  11220. // Only start timer for new bonus_script
  11221. if (entry->tid == INVALID_TIMER) {
  11222. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  11223. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  11224. entry->tick += now;
  11225. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  11226. }
  11227. if (entry->script)
  11228. run_script(entry->script, 0, sd->bl.id, 0);
  11229. else
  11230. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  11231. }
  11232. node = next;
  11233. }
  11234. }
  11235. /**
  11236. * Add bonus_script to player
  11237. * @param sd Player
  11238. * @param script_str Script string
  11239. * @param dur Duration in ms
  11240. * @param icon EFST
  11241. * @param flag Flags @see enum e_bonus_script_flags
  11242. * @param type 0 - None, 1 - Buff, 2 - Debuff
  11243. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  11244. * @author [Cydh]
  11245. **/
  11246. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_types icon, uint16 flag, uint8 type) {
  11247. struct script_code *script = NULL;
  11248. struct linkdb_node *node = NULL;
  11249. struct s_bonus_script_entry *entry = NULL;
  11250. if (!sd)
  11251. return NULL;
  11252. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11253. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11254. return NULL;
  11255. }
  11256. // Duplication checks
  11257. if ((node = sd->bonus_script.head)) {
  11258. while (node) {
  11259. entry = (struct s_bonus_script_entry *)node->data;
  11260. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11261. t_tick newdur = gettick() + dur;
  11262. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11263. sett_tickimer(entry->tid, newdur);
  11264. script_free_code(script);
  11265. return NULL;
  11266. }
  11267. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11268. break;
  11269. else { // No duplicate bonus
  11270. script_free_code(script);
  11271. return NULL;
  11272. }
  11273. }
  11274. node = node->next;
  11275. }
  11276. }
  11277. CREATE(entry, struct s_bonus_script_entry, 1);
  11278. entry->script_buf = StringBuf_Malloc();
  11279. StringBuf_AppendStr(entry->script_buf, script_str);
  11280. entry->tid = INVALID_TIMER;
  11281. entry->flag = flag;
  11282. entry->icon = icon;
  11283. entry->tick = dur; // Use duration first, on run change to expire time
  11284. entry->type = type;
  11285. entry->script = script;
  11286. sd->bonus_script.count++;
  11287. return entry;
  11288. }
  11289. /**
  11290. * Remove bonus_script data from player
  11291. * @param sd: Target player
  11292. * @param list: Bonus script entry from player
  11293. * @author [Cydh]
  11294. **/
  11295. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11296. if (entry->tid != INVALID_TIMER)
  11297. delete_timer(entry->tid, pc_bonus_script_timer);
  11298. if (entry->script)
  11299. script_free_code(entry->script);
  11300. if (entry->script_buf)
  11301. StringBuf_Free(entry->script_buf);
  11302. if (sd) {
  11303. if (entry->icon != EFST_BLANK)
  11304. clif_status_load(&sd->bl, entry->icon, 0);
  11305. if (sd->bonus_script.count > 0)
  11306. sd->bonus_script.count--;
  11307. }
  11308. aFree(entry);
  11309. }
  11310. /**
  11311. * Do final process if no entry left
  11312. * @param sd
  11313. **/
  11314. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11315. if (sd->bonus_script.count == 0) {
  11316. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11317. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11318. linkdb_final(&sd->bonus_script.head);
  11319. }
  11320. }
  11321. /**
  11322. * Timer for bonus_script
  11323. * @param tid
  11324. * @param tick
  11325. * @param id
  11326. * @param data
  11327. * @author [Cydh]
  11328. **/
  11329. TIMER_FUNC(pc_bonus_script_timer){
  11330. struct map_session_data *sd;
  11331. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11332. sd = map_id2sd(id);
  11333. if (!sd) {
  11334. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11335. return 0;
  11336. }
  11337. if (tid == INVALID_TIMER)
  11338. return 0;
  11339. if (!sd->bonus_script.head || entry == NULL) {
  11340. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11341. return 0;
  11342. }
  11343. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11344. pc_bonus_script_free_entry(sd, entry);
  11345. pc_bonus_script_check_final(sd);
  11346. status_calc_pc(sd,SCO_NONE);
  11347. return 0;
  11348. }
  11349. /**
  11350. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11351. * @param sd: Target player
  11352. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11353. * @author [Cydh]
  11354. **/
  11355. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11356. struct linkdb_node *node = NULL;
  11357. uint16 count = 0;
  11358. if (!sd || !(node = sd->bonus_script.head))
  11359. return;
  11360. while (node) {
  11361. struct linkdb_node *next = node->next;
  11362. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11363. if (entry && (
  11364. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11365. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11366. (flag&entry->flag) || // Matched flag
  11367. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11368. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11369. )
  11370. )
  11371. {
  11372. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11373. pc_bonus_script_free_entry(sd, entry);
  11374. count++;
  11375. }
  11376. node = next;
  11377. }
  11378. pc_bonus_script_check_final(sd);
  11379. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11380. status_calc_pc(sd,SCO_NONE);
  11381. }
  11382. /** [Cydh]
  11383. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11384. * @param sd: Target player
  11385. */
  11386. void pc_cell_basilica(struct map_session_data *sd) {
  11387. nullpo_retv(sd);
  11388. #ifdef RENEWAL
  11389. enum sc_type type = SC_BASILICA_CELL;
  11390. #else
  11391. enum sc_type type = SC_BASILICA;
  11392. #endif
  11393. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11394. if (sd->sc.data[type])
  11395. status_change_end(&sd->bl, type,INVALID_TIMER);
  11396. }
  11397. else if (!sd->sc.data[type])
  11398. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11399. }
  11400. /** [Cydh]
  11401. * Get maximum specified parameter for specified class
  11402. * @param class_: sd->class
  11403. * @param sex: sd->status.sex
  11404. * @param flag: parameter will be checked
  11405. * @return max_param
  11406. */
  11407. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11408. int idx = -1, class_ = sd->class_;
  11409. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11410. short max_param = 0;
  11411. switch (param) {
  11412. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11413. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11414. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11415. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11416. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11417. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11418. }
  11419. if (max_param > 0)
  11420. return max_param;
  11421. }
  11422. // Always check babies first
  11423. if( class_ & JOBL_BABY ){
  11424. if( class_ & JOBL_THIRD ){
  11425. return battle_config.max_baby_third_parameter;
  11426. }else{
  11427. return battle_config.max_baby_parameter;
  11428. }
  11429. }
  11430. // Summoner
  11431. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11432. return battle_config.max_summoner_parameter;
  11433. }
  11434. // Extended classes
  11435. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11436. return battle_config.max_extended_parameter;
  11437. }
  11438. // 3rd class
  11439. if( class_ & JOBL_THIRD ){
  11440. // Transcendent
  11441. if( class_ & JOBL_UPPER ){
  11442. return battle_config.max_third_trans_parameter;
  11443. }else{
  11444. return battle_config.max_third_parameter;
  11445. }
  11446. }
  11447. // Transcendent
  11448. if( class_ & JOBL_UPPER ){
  11449. return battle_config.max_trans_parameter;
  11450. }
  11451. return battle_config.max_parameter;
  11452. }
  11453. /**
  11454. * Get max ASPD for player based on Class
  11455. * @param sd Player
  11456. * @return ASPD
  11457. */
  11458. short pc_maxaspd(struct map_session_data *sd) {
  11459. nullpo_ret(sd);
  11460. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11461. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  11462. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  11463. battle_config.max_aspd ));
  11464. }
  11465. /**
  11466. * Calculates total item-group related bonuses for the given item
  11467. * @param sd Player
  11468. * @param nameid Item ID
  11469. * @return Heal rate
  11470. **/
  11471. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid) {
  11472. if (sd->itemgrouphealrate.empty())
  11473. return 0;
  11474. short bonus = 0;
  11475. for (const auto &it : sd->itemgrouphealrate) {
  11476. uint16 group_id = it.id;
  11477. if (group_id == 0)
  11478. continue;
  11479. if (itemdb_group.item_exists(group_id, nameid))
  11480. bonus += it.val;
  11481. }
  11482. return bonus;
  11483. }
  11484. /**
  11485. * Calculates total item-group related bonuses for the given item group
  11486. * @param sd Player
  11487. * @param group_id Item Group ID
  11488. * @return Heal rate
  11489. **/
  11490. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11491. if (sd->itemgrouphealrate.empty())
  11492. return 0;
  11493. for (const auto &it : sd->itemgrouphealrate) {
  11494. if (it.id == group_id)
  11495. return it.val;
  11496. }
  11497. return 0;
  11498. }
  11499. /**
  11500. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11501. * @param eqi Item EQI of enum equip_index
  11502. * @param *equip_index Player's equip_index[]
  11503. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11504. * @return True if item in same inventory index, False if doesn't
  11505. */
  11506. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11507. if (index < 0 || index >= MAX_INVENTORY)
  11508. return true;
  11509. // Dual weapon checks
  11510. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11511. return true;
  11512. // Headgear with Mid & Low location
  11513. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11514. return true;
  11515. // Headgear with Top & Mid or Low location
  11516. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11517. return true;
  11518. // Headgear with Mid & Low location
  11519. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11520. return true;
  11521. // Headgear with Top & Mid or Low location
  11522. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11523. return true;
  11524. return false;
  11525. }
  11526. /**
  11527. * Generate Unique item ID for player
  11528. * @param sd : Player
  11529. * @return A generated Unique item ID
  11530. */
  11531. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11532. nullpo_ret(sd);
  11533. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11534. }
  11535. /**
  11536. * Validating skill from player after logged on
  11537. * @param sd
  11538. **/
  11539. void pc_validate_skill(struct map_session_data *sd) {
  11540. if (sd) {
  11541. uint16 i = 0, count = 0;
  11542. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11543. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11544. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11545. for (i = 0; i < MAX_SKILL; i++) {
  11546. uint16 idx = 0;
  11547. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11548. continue;
  11549. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11550. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11551. count++;
  11552. }
  11553. else
  11554. 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);
  11555. }
  11556. }
  11557. }
  11558. /**
  11559. * Show available NPC Quest / Event Icon Check [Kisuka]
  11560. * @param sd Player
  11561. **/
  11562. void pc_show_questinfo(struct map_session_data *sd) {
  11563. #if PACKETVER >= 20090218
  11564. nullpo_retv(sd);
  11565. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11566. return;
  11567. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11568. nullpo_retv(mapdata);
  11569. if (mapdata->qi_npc.empty())
  11570. return;
  11571. if (mapdata->qi_npc.size() != sd->qi_display.size())
  11572. return; // init was not called yet
  11573. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  11574. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  11575. if (!nd || nd->qi_data.empty())
  11576. continue;
  11577. bool show = false;
  11578. for (auto &qi : nd->qi_data) {
  11579. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  11580. show = true;
  11581. // Check if need to be displayed
  11582. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  11583. sd->qi_display[i].is_active = true;
  11584. sd->qi_display[i].icon = qi->icon;
  11585. sd->qi_display[i].color = qi->color;
  11586. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  11587. }
  11588. break;
  11589. }
  11590. }
  11591. if (show == false) {
  11592. // Check if need to be hide
  11593. if (sd->qi_display[i].is_active) {
  11594. sd->qi_display[i].is_active = false;
  11595. sd->qi_display[i].icon = QTYPE_NONE;
  11596. sd->qi_display[i].color = QMARK_NONE;
  11597. #if PACKETVER >= 20120410
  11598. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  11599. #else
  11600. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  11601. #endif
  11602. }
  11603. }
  11604. }
  11605. #endif
  11606. }
  11607. /**
  11608. * Reinit the questinfo for player when changing map
  11609. * @param sd Player
  11610. **/
  11611. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11612. #if PACKETVER >= 20090218
  11613. nullpo_retv(sd);
  11614. sd->qi_display.clear();
  11615. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11616. return;
  11617. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11618. nullpo_retv(mapdata);
  11619. if (mapdata->qi_npc.empty())
  11620. return;
  11621. sd->qi_display.reserve( mapdata->qi_npc.size() );
  11622. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  11623. sd->qi_display.push_back( s_qi_display() );
  11624. }
  11625. #endif
  11626. }
  11627. /**
  11628. * Check if a job is allowed to enter the map
  11629. * @param jobid Job ID see enum e_job or sd->status.class_
  11630. * @param m ID -an index- for direct indexing map[] array
  11631. * @return 1 if job is allowed, 0 otherwise
  11632. **/
  11633. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11634. uint16 idx = 0;
  11635. // Map is other map server.
  11636. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11637. if (m < 0)
  11638. return true;
  11639. struct map_data *mapdata = map_getmapdata(m);
  11640. if (!mapdata->cell)
  11641. return false;
  11642. if (!pcdb_checkid(jobid))
  11643. return false;
  11644. idx = pc_class2idx(jobid);
  11645. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11646. return true;
  11647. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11648. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11649. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11650. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11651. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11652. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11653. )
  11654. return false;
  11655. return true;
  11656. }
  11657. /**
  11658. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11659. * @param sd
  11660. **/
  11661. void pc_set_costume_view(struct map_session_data *sd) {
  11662. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11663. struct item_data *id = NULL;
  11664. nullpo_retv(sd);
  11665. head_low = sd->status.head_bottom;
  11666. head_mid = sd->status.head_mid;
  11667. head_top = sd->status.head_top;
  11668. robe = sd->status.robe;
  11669. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11670. //Added check to prevent sending the same look on multiple slots ->
  11671. //causes client to redraw item on top of itself. (suggested by Lupus)
  11672. // Normal headgear checks
  11673. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11674. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11675. sd->status.head_bottom = id->look;
  11676. else
  11677. sd->status.head_bottom = 0;
  11678. }
  11679. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11680. if (!(id->equip&(EQP_HEAD_TOP)))
  11681. sd->status.head_mid = id->look;
  11682. else
  11683. sd->status.head_mid = 0;
  11684. }
  11685. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11686. sd->status.head_top = id->look;
  11687. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11688. sd->status.robe = id->look;
  11689. // Costumes check
  11690. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11691. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11692. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11693. sd->status.head_bottom = id->look;
  11694. else
  11695. sd->status.head_bottom = 0;
  11696. }
  11697. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11698. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11699. sd->status.head_mid = id->look;
  11700. else
  11701. sd->status.head_mid = 0;
  11702. }
  11703. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11704. sd->status.head_top = id->look;
  11705. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11706. sd->status.robe = id->look;
  11707. }
  11708. if (sd->setlook_head_bottom)
  11709. sd->status.head_bottom = sd->setlook_head_bottom;
  11710. if (sd->setlook_head_mid)
  11711. sd->status.head_mid = sd->setlook_head_mid;
  11712. if (sd->setlook_head_top)
  11713. sd->status.head_top = sd->setlook_head_top;
  11714. if (sd->setlook_robe)
  11715. sd->status.robe = sd->setlook_robe;
  11716. if (head_low != sd->status.head_bottom)
  11717. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11718. if (head_mid != sd->status.head_mid)
  11719. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11720. if (head_top != sd->status.head_top)
  11721. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11722. if (robe != sd->status.robe)
  11723. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11724. }
  11725. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11726. uint32 date = date_get(DT_YYYYMMDD);
  11727. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11728. std::shared_ptr<s_attendance_period> period = pair.second;
  11729. if( period->start <= date && period->end >= date ){
  11730. return period;
  11731. }
  11732. }
  11733. return nullptr;
  11734. }
  11735. bool pc_attendance_enabled(){
  11736. // Check if the attendance feature is disabled
  11737. if( !battle_config.feature_attendance ){
  11738. return false;
  11739. }
  11740. // Check if there is a running attendance period
  11741. return pc_attendance_period() != nullptr;
  11742. }
  11743. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11744. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11745. }
  11746. int32 pc_attendance_counter( struct map_session_data* sd ){
  11747. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11748. // No running attendance period
  11749. if( period == nullptr ){
  11750. return 0;
  11751. }
  11752. // Get the counter for the current period
  11753. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11754. // Check if we have a remaining counter from a previous period
  11755. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11756. // Reset the counter to zero
  11757. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11758. return 0;
  11759. }
  11760. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11761. }
  11762. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11763. // If the user's group does not have the permission
  11764. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11765. return;
  11766. }
  11767. // Check if the attendance feature is disabled
  11768. if( !pc_attendance_enabled() ){
  11769. return;
  11770. }
  11771. // Check if the user already got his reward today
  11772. if( pc_attendance_rewarded_today( sd ) ){
  11773. return;
  11774. }
  11775. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11776. attendance_counter += 1;
  11777. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11778. if( period == nullptr ){
  11779. return;
  11780. }
  11781. if( period->rewards.size() < attendance_counter ){
  11782. return;
  11783. }
  11784. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11785. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11786. if( save_settings&CHARSAVE_ATTENDANCE )
  11787. chrif_save(sd, CSAVE_NORMAL);
  11788. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11789. struct mail_message msg;
  11790. memset( &msg, 0, sizeof( struct mail_message ) );
  11791. msg.dest_id = sd->status.char_id;
  11792. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11793. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11794. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11795. msg.item[0].nameid = reward->item_id;
  11796. msg.item[0].amount = reward->amount;
  11797. msg.item[0].identify = 1;
  11798. msg.status = MAIL_NEW;
  11799. msg.type = MAIL_INBOX_NORMAL;
  11800. msg.timestamp = time(NULL);
  11801. intif_Mail_send(0, &msg);
  11802. clif_attendence_response( sd, attendance_counter );
  11803. }
  11804. /*==========================================
  11805. * pc Init/Terminate
  11806. *------------------------------------------*/
  11807. void do_final_pc(void) {
  11808. db_destroy(itemcd_db);
  11809. do_final_pc_groups();
  11810. ers_destroy(pc_sc_display_ers);
  11811. ers_destroy(num_reg_ers);
  11812. ers_destroy(str_reg_ers);
  11813. attendance_db.clear();
  11814. penalty_db.clear();
  11815. }
  11816. void do_init_pc(void) {
  11817. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11818. pc_readdb();
  11819. pc_read_motd(); // Read MOTD [Valaris]
  11820. attendance_db.load();
  11821. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11822. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11823. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11824. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11825. add_timer_func_list(pc_autosave, "pc_autosave");
  11826. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11827. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11828. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11829. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11830. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11831. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11832. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11833. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  11834. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11835. // 0=day, 1=night [Yor]
  11836. night_flag = battle_config.night_at_start ? 1 : 0;
  11837. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11838. int day_duration = battle_config.day_duration;
  11839. int night_duration = battle_config.night_duration;
  11840. // add night/day timer [Yor]
  11841. add_timer_func_list(map_day_timer, "map_day_timer");
  11842. add_timer_func_list(map_night_timer, "map_night_timer");
  11843. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11844. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11845. }
  11846. do_init_pc_groups();
  11847. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11848. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11849. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11850. ers_chunk_size(pc_sc_display_ers, 150);
  11851. ers_chunk_size(num_reg_ers, 300);
  11852. ers_chunk_size(str_reg_ers, 50);
  11853. }