pc.cpp 416 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603
  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. static unsigned int statp[MAX_LEVEL+1];
  60. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  61. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  62. #endif
  63. // h-files are for declarations, not for implementations... [Shinomori]
  64. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  65. // timer for night.day implementation
  66. int day_timer_tid = INVALID_TIMER;
  67. int night_timer_tid = INVALID_TIMER;
  68. struct eri *pc_sc_display_ers = NULL;
  69. struct eri *num_reg_ers;
  70. struct eri *str_reg_ers;
  71. int pc_expiration_tid = INVALID_TIMER;
  72. struct fame_list smith_fame_list[MAX_FAME_LIST];
  73. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  74. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  75. struct s_job_info job_info[CLASS_COUNT];
  76. const std::string AttendanceDatabase::getDefaultLocation(){
  77. return std::string(db_path) + "/attendance.yml";
  78. }
  79. /**
  80. * Reads and parses an entry from the attendance_db.
  81. * @param node: YAML node containing the entry.
  82. * @return count of successfully parsed rows
  83. */
  84. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  85. uint32 start;
  86. if( !this->asUInt32( node, "Start", start ) ){
  87. return 0;
  88. }
  89. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  90. bool exists = attendance_period != nullptr;
  91. if( !exists ){
  92. if( !this->nodeExists( node, "End" ) ){
  93. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  94. return 0;
  95. }
  96. if( !this->nodeExists( node, "Rewards" ) ){
  97. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  98. return 0;
  99. }
  100. attendance_period = std::make_shared<s_attendance_period>();
  101. attendance_period->start = start;
  102. }
  103. // If it does not exist yet, we need to check it for sure
  104. bool requiresCollisionDetection = !exists;
  105. if( this->nodeExists( node, "End" ) ){
  106. uint32 end;
  107. if( !this->asUInt32( node, "End", end ) ){
  108. return 0;
  109. }
  110. // If the period is outdated already, we do not even bother parsing
  111. if( end < date_get( DT_YYYYMMDD ) ){
  112. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  113. return 0;
  114. }
  115. if( !exists || attendance_period->end != end ){
  116. requiresCollisionDetection = true;
  117. attendance_period->end = end;
  118. }
  119. }
  120. // Collision detection
  121. if( requiresCollisionDetection ){
  122. bool collision = false;
  123. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  124. std::shared_ptr<s_attendance_period> period = pair.second;
  125. if( exists && period->start == attendance_period->start ){
  126. // Dont compare to yourself
  127. continue;
  128. }
  129. // Check if start is inside another period
  130. if( period->start <= attendance_period->start && start <= period->end ){
  131. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  132. collision = true;
  133. break;
  134. }
  135. // Check if end is inside another period
  136. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  137. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  138. collision = true;
  139. break;
  140. }
  141. }
  142. if( collision ){
  143. return 0;
  144. }
  145. }
  146. if( this->nodeExists( node, "Rewards" ) ){
  147. const YAML::Node& rewardsNode = node["Rewards"];
  148. for( const YAML::Node& rewardNode : rewardsNode ){
  149. uint32 day;
  150. if( !this->asUInt32( rewardNode, "Day", day ) ){
  151. continue;
  152. }
  153. day -= 1;
  154. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  155. bool reward_exists = reward != nullptr;
  156. if( !reward_exists ){
  157. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  158. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  159. return 0;
  160. }
  161. reward = std::make_shared<s_attendance_reward>();
  162. }
  163. if( this->nodeExists( rewardNode, "ItemId" ) ){
  164. t_itemid item_id;
  165. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  166. continue;
  167. }
  168. if( item_id == 0 || !itemdb_exists( item_id ) ){
  169. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  170. continue;
  171. }
  172. reward->item_id = item_id;
  173. }
  174. if( this->nodeExists( rewardNode, "Amount" ) ){
  175. uint16 amount;
  176. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  177. continue;
  178. }
  179. if( amount == 0 ){
  180. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  181. amount = 1;
  182. }else if( amount > MAX_AMOUNT ){
  183. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  184. amount = MAX_AMOUNT;
  185. }
  186. reward->amount = amount;
  187. }else{
  188. if( !reward_exists ){
  189. reward->amount = 1;
  190. }
  191. }
  192. if( !reward_exists ){
  193. attendance_period->rewards[day] = reward;
  194. }
  195. }
  196. bool missing_day = false;
  197. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  198. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  199. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  200. missing_day = true;
  201. break;
  202. }
  203. }
  204. if( missing_day ){
  205. return 0;
  206. }
  207. }
  208. if( !exists ){
  209. this->put( start, attendance_period );
  210. }
  211. return 1;
  212. }
  213. AttendanceDatabase attendance_db;
  214. #define MOTD_LINE_SIZE 128
  215. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  216. bool reg_load;
  217. /**
  218. * Translation table from athena equip index to aegis bitmask
  219. */
  220. unsigned int equip_bitmask[EQI_MAX] = {
  221. EQP_ACC_L, // EQI_ACC_L
  222. EQP_ACC_R, // EQI_ACC_R
  223. EQP_SHOES, // EQI_SHOES
  224. EQP_GARMENT, // EQI_GARMENT
  225. EQP_HEAD_LOW, // EQI_HEAD_LOW
  226. EQP_HEAD_MID, // EQI_HEAD_MID
  227. EQP_HEAD_TOP, // EQI_HEAD_TOP
  228. EQP_ARMOR, // EQI_ARMOR
  229. EQP_HAND_L, // EQI_HAND_L
  230. EQP_HAND_R, // EQI_HAND_R
  231. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  232. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  233. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  234. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  235. EQP_AMMO, // EQI_AMMO
  236. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  237. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  238. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  239. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  240. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  241. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  242. };
  243. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  244. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  245. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  246. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  247. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  248. };
  249. void pc_set_reg_load( bool val ){
  250. reg_load = val;
  251. }
  252. /**
  253. * Item Cool Down Delay Saving
  254. * Struct item_cd is not a member of struct map_session_data
  255. * to keep cooldowns in memory between player log-ins.
  256. * All cooldowns are reset when server is restarted.
  257. **/
  258. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  259. struct item_cd {
  260. t_tick tick[MAX_ITEMDELAYS]; //tick
  261. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  262. };
  263. /**
  264. * Converts a class to its array index for CLASS_COUNT defined arrays.
  265. * Note that it does not do a validity check for speed purposes, where parsing
  266. * player input make sure to use a pcdb_checkid first!
  267. * @param class_ Job ID see enum e_job
  268. * @return Class Index
  269. */
  270. int pc_class2idx(int class_) {
  271. if (class_ >= JOB_NOVICE_HIGH)
  272. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  273. return class_;
  274. }
  275. /**
  276. * Get player's group ID
  277. * @param sd
  278. * @return Group ID
  279. */
  280. int pc_get_group_id(struct map_session_data *sd) {
  281. return sd->group_id;
  282. }
  283. /** Get player's group Level
  284. * @param sd
  285. * @return Group Level
  286. */
  287. int pc_get_group_level(struct map_session_data *sd) {
  288. return sd->group_level;
  289. }
  290. /**
  291. * Remove a player from queue after timeout
  292. * @param tid: Timer ID
  293. * @param tick: Timer
  294. * @param id: ID
  295. * @return 0 on success or 1 otherwise
  296. */
  297. static TIMER_FUNC(pc_on_expire_active)
  298. {
  299. map_session_data *sd = (map_session_data *)data;
  300. nullpo_retr(1, sd);
  301. sd->tid_queue_active = INVALID_TIMER;
  302. bg_queue_leave(sd);
  303. clif_bg_queue_entry_init(sd);
  304. return 0;
  305. }
  306. /**
  307. * Function used to set timer externally
  308. * @param sd: Player data
  309. */
  310. void pc_set_bg_queue_timer(map_session_data *sd) {
  311. nullpo_retv(sd);
  312. if (sd->tid_queue_active != INVALID_TIMER) {
  313. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  314. sd->tid_queue_active = INVALID_TIMER;
  315. }
  316. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  317. }
  318. /**
  319. * Function used to delete timer externally
  320. * @param sd: Player data
  321. */
  322. void pc_delete_bg_queue_timer(map_session_data *sd) {
  323. nullpo_retv(sd);
  324. if (sd->tid_queue_active != INVALID_TIMER) {
  325. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  326. sd->tid_queue_active = INVALID_TIMER;
  327. }
  328. }
  329. static TIMER_FUNC(pc_invincible_timer){
  330. struct map_session_data *sd;
  331. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  332. return 1;
  333. if(sd->invincible_timer != tid){
  334. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  335. return 0;
  336. }
  337. sd->invincible_timer = INVALID_TIMER;
  338. skill_unit_move(&sd->bl,tick,1);
  339. return 0;
  340. }
  341. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  342. nullpo_retv(sd);
  343. if( sd->invincible_timer != INVALID_TIMER )
  344. delete_timer(sd->invincible_timer,pc_invincible_timer);
  345. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  346. }
  347. void pc_delinvincibletimer(struct map_session_data* sd)
  348. {
  349. nullpo_retv(sd);
  350. if( sd->invincible_timer != INVALID_TIMER )
  351. {
  352. delete_timer(sd->invincible_timer,pc_invincible_timer);
  353. sd->invincible_timer = INVALID_TIMER;
  354. skill_unit_move(&sd->bl,gettick(),1);
  355. }
  356. }
  357. static TIMER_FUNC(pc_spiritball_timer){
  358. struct map_session_data *sd;
  359. int i;
  360. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  361. return 1;
  362. if( sd->spiritball <= 0 )
  363. {
  364. 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);
  365. sd->spiritball = 0;
  366. return 0;
  367. }
  368. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  369. if( i == sd->spiritball )
  370. {
  371. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  372. return 0;
  373. }
  374. sd->spiritball--;
  375. if( i != sd->spiritball )
  376. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  377. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  378. clif_spiritball(&sd->bl);
  379. return 0;
  380. }
  381. /**
  382. * Adds a spiritball to player for 'interval' ms
  383. * @param sd
  384. * @param interval
  385. * @param max
  386. */
  387. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  388. {
  389. int tid;
  390. uint8 i;
  391. nullpo_retv(sd);
  392. if(max > MAX_SPIRITBALL)
  393. max = MAX_SPIRITBALL;
  394. if(sd->spiritball < 0)
  395. sd->spiritball = 0;
  396. if( sd->spiritball && sd->spiritball >= max )
  397. {
  398. if(sd->spirit_timer[0] != INVALID_TIMER)
  399. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  400. sd->spiritball--;
  401. if( sd->spiritball != 0 )
  402. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  403. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  404. }
  405. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  406. 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);
  407. if( i != sd->spiritball )
  408. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  409. sd->spirit_timer[i] = tid;
  410. sd->spiritball++;
  411. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  412. clif_millenniumshield(&sd->bl,sd->spiritball);
  413. else
  414. clif_spiritball(&sd->bl);
  415. }
  416. /**
  417. * Removes number of spiritball from player
  418. * @param sd
  419. * @param count
  420. * @param type 1 = doesn't give client effect
  421. */
  422. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  423. {
  424. uint8 i;
  425. nullpo_retv(sd);
  426. if(sd->spiritball <= 0) {
  427. sd->spiritball = 0;
  428. return;
  429. }
  430. if(count == 0)
  431. return;
  432. if(count > sd->spiritball)
  433. count = sd->spiritball;
  434. sd->spiritball -= count;
  435. if(count > MAX_SPIRITBALL)
  436. count = MAX_SPIRITBALL;
  437. for(i=0;i<count;i++) {
  438. if(sd->spirit_timer[i] != INVALID_TIMER) {
  439. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  440. sd->spirit_timer[i] = INVALID_TIMER;
  441. }
  442. }
  443. for(i=count;i<MAX_SPIRITBALL;i++) {
  444. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  445. sd->spirit_timer[i] = INVALID_TIMER;
  446. }
  447. if(!type) {
  448. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  449. clif_millenniumshield(&sd->bl,sd->spiritball);
  450. else
  451. clif_spiritball(&sd->bl);
  452. }
  453. }
  454. /**
  455. * Adds a soulball to player
  456. * @param sd: Player data
  457. * @param max: Max amount of soulballs
  458. */
  459. int pc_addsoulball(map_session_data *sd, int max)
  460. {
  461. nullpo_ret(sd);
  462. status_change *sc = status_get_sc(&sd->bl);
  463. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  464. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  465. sd->soulball = 0;
  466. }
  467. max = min(max, MAX_SOUL_BALL);
  468. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  469. if (sd->soulball && sd->soulball >= max)
  470. sd->soulball--;
  471. sd->soulball++;
  472. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  473. clif_soulball(sd);
  474. return 0;
  475. }
  476. /**
  477. * Removes number of soulball from player
  478. * @param sd: Player data
  479. * @param count: Amount to remove
  480. * @param type: true = doesn't give client effect
  481. */
  482. int pc_delsoulball(map_session_data *sd, int count, bool type)
  483. {
  484. nullpo_ret(sd);
  485. if (count <= 0)
  486. return 0;
  487. status_change *sc = status_get_sc(&sd->bl);
  488. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  489. sd->soulball = 0;
  490. return 0;
  491. }
  492. sd->soulball -= cap_value(count, 0, sd->soulball);
  493. if (sd->soulball == 0)
  494. status_change_end(&sd->bl, SC_SOULENERGY, INVALID_TIMER);
  495. else
  496. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  497. if (!type)
  498. clif_soulball(sd);
  499. return 0;
  500. }
  501. /**
  502. * Increases a player's fame points and displays a notice to him
  503. * @param sd Player
  504. * @param count Fame point
  505. */
  506. void pc_addfame(struct map_session_data *sd,int count)
  507. {
  508. enum e_rank ranktype;
  509. nullpo_retv(sd);
  510. sd->status.fame += count;
  511. if(sd->status.fame > MAX_FAME)
  512. sd->status.fame = MAX_FAME;
  513. switch(sd->class_&MAPID_UPPERMASK){
  514. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  515. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  516. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  517. default:
  518. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  519. return;
  520. }
  521. clif_update_rankingpoint(sd,ranktype,count);
  522. chrif_updatefamelist(sd);
  523. }
  524. /**
  525. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  526. * @param sd
  527. * @param job Job use enum e_mapid
  528. * @return Rank
  529. */
  530. unsigned char pc_famerank(uint32 char_id, int job)
  531. {
  532. uint8 i;
  533. switch(job){
  534. case MAPID_BLACKSMITH: // Blacksmith
  535. for(i = 0; i < MAX_FAME_LIST; i++){
  536. if(smith_fame_list[i].id == char_id)
  537. return i + 1;
  538. }
  539. break;
  540. case MAPID_ALCHEMIST: // Alchemist
  541. for(i = 0; i < MAX_FAME_LIST; i++){
  542. if(chemist_fame_list[i].id == char_id)
  543. return i + 1;
  544. }
  545. break;
  546. case MAPID_TAEKWON: // Taekwon
  547. for(i = 0; i < MAX_FAME_LIST; i++){
  548. if(taekwon_fame_list[i].id == char_id)
  549. return i + 1;
  550. }
  551. break;
  552. }
  553. return 0;
  554. }
  555. /**
  556. * Restart player's HP & SP value
  557. * @param sd
  558. * @param type Restart type: 1 - Normal Resurection
  559. */
  560. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  561. struct status_data *status, *b_status;
  562. nullpo_retv(sd);
  563. b_status = &sd->base_status;
  564. status = &sd->battle_status;
  565. if (type&1) { //Normal resurrection
  566. status->hp = 1; //Otherwise status_heal may fail if dead.
  567. status_heal(&sd->bl, b_status->hp, 0, 1);
  568. if( status->sp < b_status->sp )
  569. status_set_sp(&sd->bl, b_status->sp, 1);
  570. } else { //Just for saving on the char-server (with values as if respawned)
  571. sd->status.hp = b_status->hp;
  572. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  573. }
  574. }
  575. /*==========================================
  576. Rental System
  577. *------------------------------------------*/
  578. /**
  579. * Ends a rental and removes the item/effect
  580. * @param tid: Tick ID
  581. * @param tick: Timer
  582. * @param id: Timer ID
  583. * @param data: Data
  584. * @return false - failure, true - success
  585. */
  586. TIMER_FUNC(pc_inventory_rental_end){
  587. struct map_session_data *sd = map_id2sd(id);
  588. if( sd == NULL )
  589. return 0;
  590. if( tid != sd->rental_timer ) {
  591. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  592. return 0;
  593. }
  594. pc_inventory_rentals(sd);
  595. return 1;
  596. }
  597. /**
  598. * Removes the rental timer from the player
  599. * @param sd: Player data
  600. */
  601. void pc_inventory_rental_clear(struct map_session_data *sd)
  602. {
  603. if( sd->rental_timer != INVALID_TIMER ) {
  604. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  605. sd->rental_timer = INVALID_TIMER;
  606. }
  607. }
  608. /**
  609. * Check for items in the player's inventory that are rental type
  610. * @param sd: Player data
  611. */
  612. void pc_inventory_rentals(struct map_session_data *sd)
  613. {
  614. int i, c = 0;
  615. unsigned int next_tick = UINT_MAX;
  616. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  617. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  618. continue; // Nothing here
  619. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  620. continue;
  621. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  622. if (sd->inventory_data[i]->unequip_script)
  623. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  624. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  625. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  626. } else {
  627. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  628. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  629. next_tick = umin(expire_tick * 1000U, next_tick);
  630. c++;
  631. }
  632. }
  633. 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
  634. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  635. else
  636. sd->rental_timer = INVALID_TIMER;
  637. }
  638. /**
  639. * Add a rental item to the player and adjusts the rental timer appropriately
  640. * @param sd: Player data
  641. * @param seconds: Rental time
  642. */
  643. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  644. {
  645. t_tick tick = seconds * 1000;
  646. if( sd == NULL )
  647. return;
  648. if( sd->rental_timer != INVALID_TIMER ) {
  649. const struct TimerData * td;
  650. td = get_timer(sd->rental_timer);
  651. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  652. pc_inventory_rental_clear(sd);
  653. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  654. }
  655. } else
  656. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  657. }
  658. /**
  659. * Check if the player can sell the current item
  660. * @param sd: map_session_data of the player
  661. * @param item: struct of the checking item
  662. * @param shoptype: NPC's sub type see enum npc_subtype
  663. * @return bool 'true' is sellable, 'false' otherwise
  664. */
  665. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  666. if (sd == NULL || item == NULL)
  667. return false;
  668. if (item->equip > 0 || item->amount < 0)
  669. return false;
  670. if (battle_config.hide_fav_sell && item->favorite)
  671. return false; //Cannot sell favs (optional config)
  672. if (!battle_config.rental_transaction && item->expire_time)
  673. return false; // Cannot Sell Rental Items
  674. if( item->equipSwitch ){
  675. return false;
  676. }
  677. if (itemdb_ishatched_egg(item))
  678. return false;
  679. switch (shoptype) {
  680. case NPCTYPE_SHOP:
  681. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  682. item->bound != BOUND_GUILD ||
  683. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  684. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  685. ))
  686. return true;
  687. break;
  688. case NPCTYPE_ITEMSHOP:
  689. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  690. item->bound != BOUND_GUILD ||
  691. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  692. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  693. ))
  694. return true;
  695. else if (!item->bound) {
  696. struct item_data *itd = itemdb_search(item->nameid);
  697. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  698. return true;
  699. }
  700. break;
  701. }
  702. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  703. return false;
  704. if (item->bound && !pc_can_give_bounded_items(sd))
  705. return false; // Don't allow sale of bound items
  706. return true;
  707. }
  708. /**
  709. * Determines if player can give / drop / trade / vend items
  710. */
  711. bool pc_can_give_items(struct map_session_data *sd)
  712. {
  713. return pc_has_permission(sd, PC_PERM_TRADE);
  714. }
  715. /**
  716. * Determines if player can give / drop / trade / vend bounded items
  717. */
  718. bool pc_can_give_bounded_items(struct map_session_data *sd)
  719. {
  720. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  721. }
  722. /**
  723. * Determine if an item in a player's inventory is tradeable based on several merits.
  724. * Checks for item_trade, bound, and rental restrictions.
  725. * @param sd: Player data
  726. * @param index: Item inventory index
  727. * @return True if the item can be traded or false otherwise
  728. */
  729. bool pc_can_trade_item(map_session_data *sd, int index) {
  730. if (sd && index >= 0) {
  731. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  732. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  733. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  734. );
  735. }
  736. return false;
  737. }
  738. /*==========================================
  739. * Prepares character for saving.
  740. * @param sd
  741. *------------------------------------------*/
  742. void pc_makesavestatus(struct map_session_data *sd) {
  743. nullpo_retv(sd);
  744. if(!battle_config.save_clothcolor)
  745. sd->status.clothes_color = 0;
  746. if(!battle_config.save_body_style)
  747. sd->status.body = 0;
  748. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  749. //status change load/saving. [Skotlex]
  750. #ifdef NEW_CARTS
  751. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  752. #else
  753. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  754. #endif
  755. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  756. if(pc_isdead(sd)){
  757. pc_setrestartvalue(sd, 0);
  758. } else {
  759. sd->status.hp = sd->battle_status.hp;
  760. sd->status.sp = sd->battle_status.sp;
  761. }
  762. sd->status.last_point.map = sd->mapindex;
  763. sd->status.last_point.x = sd->bl.x;
  764. sd->status.last_point.y = sd->bl.y;
  765. return;
  766. }
  767. if(pc_isdead(sd)) {
  768. pc_setrestartvalue(sd, 0);
  769. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  770. } else {
  771. sd->status.hp = sd->battle_status.hp;
  772. sd->status.sp = sd->battle_status.sp;
  773. sd->status.last_point.map = sd->mapindex;
  774. sd->status.last_point.x = sd->bl.x;
  775. sd->status.last_point.y = sd->bl.y;
  776. }
  777. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  778. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  779. if(mapdata->save.map)
  780. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  781. else
  782. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  783. }
  784. }
  785. /*==========================================
  786. * Off init ? Connection?
  787. *------------------------------------------*/
  788. 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) {
  789. nullpo_retv(sd);
  790. sd->bl.id = account_id;
  791. sd->status.account_id = account_id;
  792. sd->status.char_id = char_id;
  793. sd->status.sex = sex;
  794. sd->login_id1 = login_id1;
  795. sd->login_id2 = 0; // at this point, we can not know the value :(
  796. sd->client_tick = client_tick;
  797. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  798. sd->bl.type = BL_PC;
  799. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  800. sd->canlog_tick = gettick();
  801. //Required to prevent homunculus copuing a base speed of 0.
  802. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  803. }
  804. /**
  805. * Get equip point for an equip
  806. * @param sd
  807. * @param id
  808. */
  809. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  810. int ep = 0;
  811. nullpo_ret(sd);
  812. nullpo_ret(id);
  813. if (!itemdb_isequip2(id))
  814. return 0; //Not equippable by players.
  815. ep = id->equip;
  816. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  817. 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]
  818. if (ep == EQP_WEAPON)
  819. return EQP_ARMS;
  820. if (ep == EQP_SHADOW_WEAPON)
  821. return EQP_SHADOW_ARMS;
  822. }
  823. }
  824. return ep;
  825. }
  826. /**
  827. * Get equip point for an equip
  828. * @param sd
  829. * @param n Equip index in inventory
  830. */
  831. int pc_equippoint(struct map_session_data *sd,int n){
  832. nullpo_ret(sd);
  833. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  834. }
  835. /**
  836. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  837. * @param sd : player session
  838. * @return 0 sucess, 1:invalid sd
  839. */
  840. void pc_setinventorydata(struct map_session_data *sd)
  841. {
  842. uint8 i;
  843. nullpo_retv(sd);
  844. for(i = 0; i < MAX_INVENTORY; i++) {
  845. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  846. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  847. }
  848. }
  849. /**
  850. * 'Calculates' weapon type
  851. * @param sd : Player
  852. */
  853. void pc_calcweapontype(struct map_session_data *sd)
  854. {
  855. nullpo_retv(sd);
  856. // single-hand
  857. if(sd->weapontype2 == W_FIST) {
  858. sd->status.weapon = sd->weapontype1;
  859. return;
  860. }
  861. if(sd->weapontype1 == W_FIST) {
  862. sd->status.weapon = sd->weapontype2;
  863. return;
  864. }
  865. // dual-wield
  866. sd->status.weapon = 0;
  867. switch (sd->weapontype1){
  868. case W_DAGGER:
  869. switch (sd->weapontype2) {
  870. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  871. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  872. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  873. }
  874. break;
  875. case W_1HSWORD:
  876. switch (sd->weapontype2) {
  877. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  878. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  879. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  880. }
  881. break;
  882. case W_1HAXE:
  883. switch (sd->weapontype2) {
  884. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  885. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  886. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  887. }
  888. }
  889. // unknown, default to right hand type
  890. if (!sd->status.weapon)
  891. sd->status.weapon = sd->weapontype1;
  892. }
  893. /**
  894. * Set equip index
  895. * @param sd : Player
  896. */
  897. void pc_setequipindex(struct map_session_data *sd)
  898. {
  899. uint16 i;
  900. nullpo_retv(sd);
  901. for (i = 0; i < EQI_MAX; i++){
  902. sd->equip_index[i] = -1;
  903. sd->equip_switch_index[i] = -1;
  904. }
  905. for (i = 0; i < MAX_INVENTORY; i++) {
  906. if (sd->inventory.u.items_inventory[i].nameid == 0)
  907. continue;
  908. if (sd->inventory.u.items_inventory[i].equip) {
  909. uint8 j;
  910. for (j = 0; j < EQI_MAX; j++)
  911. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  912. sd->equip_index[j] = i;
  913. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  914. if (sd->inventory_data[i])
  915. sd->weapontype1 = sd->inventory_data[i]->look;
  916. else
  917. sd->weapontype1 = 0;
  918. }
  919. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  920. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  921. sd->weapontype2 = sd->inventory_data[i]->look;
  922. else
  923. sd->weapontype2 = 0;
  924. }
  925. }
  926. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  927. for (uint8 j = 0; j < EQI_MAX; j++) {
  928. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  929. sd->equip_switch_index[j] = i;
  930. }
  931. }
  932. }
  933. }
  934. pc_calcweapontype(sd);
  935. }
  936. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  937. //{
  938. // int i;
  939. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  940. // struct item_data *data;
  941. //
  942. // //Crafted/made/hatched items.
  943. // if (itemdb_isspecial(item->card[0]))
  944. // return 1;
  945. //
  946. // /* scan for enchant armor gems */
  947. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  948. // s = MAX_SLOTS - 1;
  949. //
  950. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  951. // return( i < s ) ? 0 : 1;
  952. //}
  953. /**
  954. * Check if an item is equiped by player
  955. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  956. * @param sd : player session
  957. * @param nameid : itemid
  958. * @return 1:yes, 0:no
  959. */
  960. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  961. {
  962. uint8 i;
  963. for( i = 0; i < EQI_MAX; i++ )
  964. {
  965. short index = sd->equip_index[i], j;
  966. if( index < 0 )
  967. continue;
  968. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  969. continue;
  970. if( !sd->inventory_data[index] )
  971. continue;
  972. if( sd->inventory_data[index]->nameid == nameid )
  973. return true;
  974. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  975. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  976. return true;
  977. }
  978. }
  979. return false;
  980. }
  981. /**
  982. * Check adoption rules
  983. * @param p1_sd: Player 1
  984. * @param p2_sd: Player 2
  985. * @param b_sd: Player that will be adopted
  986. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  987. * ADOPT_ALREADY_ADOPTED - Already adopted
  988. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  989. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  990. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  991. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  992. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  993. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  994. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  995. */
  996. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  997. {
  998. if( !p1_sd || !p2_sd || !b_sd )
  999. return ADOPT_CHARACTER_NOT_FOUND;
  1000. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1001. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1002. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1003. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1004. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1005. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1006. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1007. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1008. // Parents need to have their ring equipped
  1009. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1010. return ADOPT_EQUIP_RINGS;
  1011. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1012. return ADOPT_EQUIP_RINGS;
  1013. // Already adopted a baby
  1014. if( p1_sd->status.child || p2_sd->status.child ) {
  1015. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1016. return ADOPT_MORE_CHILDREN;
  1017. }
  1018. // Parents need at least lvl 70 to adopt
  1019. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1020. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1021. return ADOPT_LEVEL_70;
  1022. }
  1023. if( b_sd->status.partner_id ) {
  1024. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1025. return ADOPT_MARRIED;
  1026. }
  1027. 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 ) )
  1028. return ADOPT_NOT_NOVICE;
  1029. return ADOPT_ALLOWED;
  1030. }
  1031. /*==========================================
  1032. * Adoption Process
  1033. *------------------------------------------*/
  1034. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1035. {
  1036. int job, joblevel;
  1037. t_exp jobexp;
  1038. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1039. return false;
  1040. // Preserve current job levels and progress
  1041. joblevel = b_sd->status.job_level;
  1042. jobexp = b_sd->status.job_exp;
  1043. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1044. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1045. { // Success, proceed to configure parents and baby skills
  1046. p1_sd->status.child = b_sd->status.char_id;
  1047. p2_sd->status.child = b_sd->status.char_id;
  1048. b_sd->status.father = p1_sd->status.char_id;
  1049. b_sd->status.mother = p2_sd->status.char_id;
  1050. // Restore progress
  1051. b_sd->status.job_level = joblevel;
  1052. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1053. b_sd->status.job_exp = jobexp;
  1054. clif_updatestatus(b_sd, SP_JOBEXP);
  1055. // Baby Skills
  1056. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1057. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1058. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1059. // Parents Skills
  1060. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1061. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1062. chrif_save(p1_sd, CSAVE_NORMAL);
  1063. chrif_save(p2_sd, CSAVE_NORMAL);
  1064. chrif_save(b_sd, CSAVE_NORMAL);
  1065. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1066. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1067. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1068. return true;
  1069. }
  1070. return false; // Job Change Fail
  1071. }
  1072. /*==========================================
  1073. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1074. Returns:
  1075. false : Cannot use/equip
  1076. true : Can use/equip
  1077. * Credits:
  1078. [Inkfish] for first idea
  1079. [Haru] for third-classes extension
  1080. [Cydh] finishing :D
  1081. *------------------------------------------*/
  1082. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1083. while (1) {
  1084. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1085. break;
  1086. #ifndef RENEWAL
  1087. //allow third classes to use trans. class items
  1088. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1089. break;
  1090. //third-baby classes can use same item too
  1091. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1092. break;
  1093. //don't need to decide specific rules for third-classes?
  1094. //items for third classes can be used for all third classes
  1095. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1096. break;
  1097. #else
  1098. //trans. classes (exl. third-trans.)
  1099. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1100. break;
  1101. //baby classes (exl. third-baby)
  1102. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1103. break;
  1104. //third classes (exl. third-trans. and baby-third)
  1105. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1106. break;
  1107. //trans-third classes
  1108. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1109. break;
  1110. //third-baby classes
  1111. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1112. break;
  1113. #endif
  1114. return false;
  1115. }
  1116. return true;
  1117. }
  1118. /*=================================================
  1119. * Checks if the player can equip the item at index n in inventory.
  1120. * @param sd
  1121. * @param n Item index in inventory
  1122. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1123. *------------------------------------------------*/
  1124. uint8 pc_isequip(struct map_session_data *sd,int n)
  1125. {
  1126. struct item_data *item;
  1127. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1128. item = sd->inventory_data[n];
  1129. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1130. return ITEM_EQUIP_ACK_OK;
  1131. if(item == NULL)
  1132. return ITEM_EQUIP_ACK_FAIL;
  1133. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1134. return ITEM_EQUIP_ACK_FAILLEVEL;
  1135. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1136. return ITEM_EQUIP_ACK_FAILLEVEL;
  1137. if(item->sex != 2 && sd->status.sex != item->sex)
  1138. return ITEM_EQUIP_ACK_FAIL;
  1139. //fail to equip if item is restricted
  1140. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1141. return ITEM_EQUIP_ACK_FAIL;
  1142. if (item->equip&EQP_AMMO) {
  1143. switch (item->look) {
  1144. case AMMO_ARROW:
  1145. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1146. clif_msg(sd, ITEM_NEED_BOW);
  1147. return ITEM_EQUIP_ACK_FAIL;
  1148. }
  1149. break;
  1150. case AMMO_THROWABLE_DAGGER:
  1151. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1152. return ITEM_EQUIP_ACK_FAIL;
  1153. break;
  1154. case AMMO_BULLET:
  1155. case AMMO_SHELL:
  1156. 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
  1157. #ifdef RENEWAL
  1158. && sd->status.weapon != W_GRENADE
  1159. #endif
  1160. ) {
  1161. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1162. return ITEM_EQUIP_ACK_FAIL;
  1163. }
  1164. break;
  1165. #ifndef RENEWAL
  1166. case AMMO_GRENADE:
  1167. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1168. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1169. return ITEM_EQUIP_ACK_FAIL;
  1170. }
  1171. break;
  1172. #endif
  1173. case AMMO_CANNONBALL:
  1174. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1175. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1176. return ITEM_EQUIP_ACK_FAIL;
  1177. }
  1178. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1179. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1180. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1181. return ITEM_EQUIP_ACK_FAIL;
  1182. }
  1183. break;
  1184. }
  1185. }
  1186. if (sd->sc.count) {
  1187. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1188. return ITEM_EQUIP_ACK_FAIL;
  1189. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1190. return ITEM_EQUIP_ACK_FAIL;
  1191. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1192. return ITEM_EQUIP_ACK_FAIL;
  1193. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1194. return ITEM_EQUIP_ACK_FAIL;
  1195. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1196. return ITEM_EQUIP_ACK_FAIL;
  1197. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1198. return ITEM_EQUIP_ACK_FAIL;
  1199. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1200. return ITEM_EQUIP_ACK_FAIL;
  1201. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1202. //Spirit of Super Novice equip bonuses. [Skotlex]
  1203. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1204. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1205. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1206. switch(item->look) { //In weapons, the look determines type of weapon.
  1207. case W_DAGGER: //All level 4 - Daggers
  1208. case W_1HSWORD: //All level 4 - 1H Swords
  1209. case W_1HAXE: //All level 4 - 1H Axes
  1210. case W_MACE: //All level 4 - 1H Maces
  1211. case W_STAFF: //All level 4 - 1H Staves
  1212. case W_2HSTAFF: //All level 4 - 2H Staves
  1213. return ITEM_EQUIP_ACK_OK;
  1214. }
  1215. }
  1216. }
  1217. //Not equipable by class. [Skotlex]
  1218. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1219. return ITEM_EQUIP_ACK_FAIL;
  1220. if (!pc_isItemClass(sd, item))
  1221. return ITEM_EQUIP_ACK_FAIL;
  1222. return ITEM_EQUIP_ACK_OK;
  1223. }
  1224. /*==========================================
  1225. * No problem with the session id
  1226. * set the status that has been sent from char server
  1227. *------------------------------------------*/
  1228. 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)
  1229. {
  1230. int i;
  1231. t_tick tick = gettick();
  1232. uint32 ip = session[sd->fd]->client_addr;
  1233. sd->login_id2 = login_id2;
  1234. sd->group_id = group_id;
  1235. /* load user permissions */
  1236. pc_group_pc_load(sd);
  1237. memcpy(&sd->status, st, sizeof(*st));
  1238. if (st->sex != sd->status.sex) {
  1239. clif_authfail_fd(sd->fd, 0);
  1240. return false;
  1241. }
  1242. //Set the map-server used job id. [Skotlex]
  1243. i = pc_jobid2mapid(sd->status.class_);
  1244. if (i == -1) { //Invalid class?
  1245. 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);
  1246. sd->status.class_ = JOB_NOVICE;
  1247. sd->class_ = MAPID_NOVICE;
  1248. } else
  1249. sd->class_ = i;
  1250. // Checks and fixes to character status data, that are required
  1251. // in case of configuration change or stuff, which cannot be
  1252. // checked on char-server.
  1253. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1254. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1255. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1256. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1257. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1258. sd->state.connect_new = 1;
  1259. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1260. sd->invincible_timer = INVALID_TIMER;
  1261. sd->npc_timer_id = INVALID_TIMER;
  1262. sd->pvp_timer = INVALID_TIMER;
  1263. sd->expiration_tid = INVALID_TIMER;
  1264. sd->autotrade_tid = INVALID_TIMER;
  1265. sd->respawn_tid = INVALID_TIMER;
  1266. sd->tid_queue_active = INVALID_TIMER;
  1267. sd->skill_keep_using.tid = INVALID_TIMER;
  1268. sd->skill_keep_using.skill_id = 0;
  1269. sd->skill_keep_using.level = 0;
  1270. sd->skill_keep_using.target = 0;
  1271. #ifdef SECURE_NPCTIMEOUT
  1272. // Initialize to defaults/expected
  1273. sd->npc_idle_timer = INVALID_TIMER;
  1274. sd->npc_idle_tick = tick;
  1275. sd->npc_idle_type = NPCT_INPUT;
  1276. sd->state.ignoretimeout = false;
  1277. #endif
  1278. sd->canuseitem_tick = tick;
  1279. sd->canusecashfood_tick = tick;
  1280. sd->canequip_tick = tick;
  1281. sd->cantalk_tick = tick;
  1282. sd->canskill_tick = tick;
  1283. sd->cansendmail_tick = tick;
  1284. sd->idletime = last_tick;
  1285. for(i = 0; i < MAX_SPIRITBALL; i++)
  1286. sd->spirit_timer[i] = INVALID_TIMER;
  1287. if (battle_config.item_auto_get)
  1288. sd->state.autoloot = 10000;
  1289. if (battle_config.disp_experience)
  1290. sd->state.showexp = 1;
  1291. if (battle_config.disp_zeny)
  1292. sd->state.showzeny = 1;
  1293. #ifdef VIP_ENABLE
  1294. if (!battle_config.vip_disp_rate)
  1295. sd->vip.disableshowrate = 1;
  1296. #endif
  1297. if (!(battle_config.display_skill_fail&2))
  1298. sd->state.showdelay = 1;
  1299. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1300. memset(&sd->cart, 0, sizeof(struct s_storage));
  1301. memset(&sd->storage, 0, sizeof(struct s_storage));
  1302. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1303. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1304. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1305. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1306. sd->status.option &= ~OPTION_INVISIBLE;
  1307. }
  1308. status_change_init(&sd->bl);
  1309. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1310. unit_dataset(&sd->bl);
  1311. sd->guild_x = -1;
  1312. sd->guild_y = -1;
  1313. sd->delayed_damage = 0;
  1314. // Event Timers
  1315. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1316. sd->eventtimer[i] = INVALID_TIMER;
  1317. // Rental Timer
  1318. sd->rental_timer = INVALID_TIMER;
  1319. for( i = 0; i < 3; i++ )
  1320. sd->hate_mob[i] = -1;
  1321. sd->quest_log = NULL;
  1322. sd->num_quests = 0;
  1323. sd->avail_quests = 0;
  1324. sd->save_quest = false;
  1325. sd->count_rewarp = 0;
  1326. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1327. sd->regs.arrays = NULL;
  1328. sd->vars_dirty = false;
  1329. sd->vars_ok = false;
  1330. sd->vars_received = 0x0;
  1331. sd->qi_display = nullptr;
  1332. sd->qi_count = 0;
  1333. //warp player
  1334. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1335. 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);
  1336. // try warping to a default map instead (church graveyard)
  1337. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1338. // if we fail again
  1339. clif_authfail_fd(sd->fd, 0);
  1340. return false;
  1341. }
  1342. }
  1343. clif_authok(sd);
  1344. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1345. sd->die_counter=-1;
  1346. //display login notice
  1347. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1348. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1349. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1350. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1351. sd->status.name, sd->status.account_id, sd->status.char_id,
  1352. CONVIP(ip), sd->group_id);
  1353. // Send friends list
  1354. clif_friendslist_send(sd);
  1355. if( !changing_mapservers ) {
  1356. if (battle_config.display_version == 1)
  1357. pc_show_version(sd);
  1358. // Message of the Day [Valaris]
  1359. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1360. if (battle_config.motd_type)
  1361. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1362. else
  1363. clif_displaymessage(sd->fd, motd_text[i]);
  1364. }
  1365. if (expiration_time != 0)
  1366. sd->expiration_time = expiration_time;
  1367. /**
  1368. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1369. **/
  1370. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1371. }
  1372. pc_validate_skill(sd);
  1373. /* [Ind] */
  1374. sd->sc_display = NULL;
  1375. sd->sc_display_count = 0;
  1376. // Player has not yet received the CashShop list
  1377. sd->status.cashshop_sent = false;
  1378. sd->last_addeditem_index = -1;
  1379. sd->bonus_script.head = NULL;
  1380. sd->bonus_script.count = 0;
  1381. // Initialize BG queue
  1382. sd->bg_queue_id = 0;
  1383. #if PACKETVER >= 20150513
  1384. sd->hatEffectIDs = NULL;
  1385. sd->hatEffectCount = 0;
  1386. #endif
  1387. sd->catch_target_class = PET_CATCH_FAIL;
  1388. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1389. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1390. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1391. clif_updatestatus(sd, SP_BASEEXP);
  1392. }
  1393. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1394. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1395. clif_updatestatus(sd, SP_JOBEXP);
  1396. }
  1397. // Request all registries (auth is considered completed whence they arrive)
  1398. intif_request_registry(sd,7);
  1399. return true;
  1400. }
  1401. /*==========================================
  1402. * Closes a connection because it failed to be authenticated from the char server.
  1403. *------------------------------------------*/
  1404. void pc_authfail(struct map_session_data *sd)
  1405. {
  1406. clif_authfail_fd(sd->fd, 0);
  1407. return;
  1408. }
  1409. /**
  1410. * Player register a bl as hatred
  1411. * @param sd : player session
  1412. * @param pos : hate position [0;2]
  1413. * @param bl : target bl
  1414. * @return false:failed, true:success
  1415. */
  1416. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1417. {
  1418. int class_;
  1419. if (!sd || !bl || pos < 0 || pos > 2)
  1420. return false;
  1421. if (sd->hate_mob[pos] != -1)
  1422. { //Can't change hate targets.
  1423. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1424. return false;
  1425. }
  1426. class_ = status_get_class(bl);
  1427. if (!pcdb_checkid(class_)) {
  1428. unsigned int max_hp = status_get_max_hp(bl);
  1429. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1430. return false;
  1431. if (pos != status_get_size(bl))
  1432. return false; //Wrong size
  1433. }
  1434. sd->hate_mob[pos] = class_;
  1435. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1436. clif_hate_info(sd, pos, class_, 1);
  1437. return true;
  1438. }
  1439. /*==========================================
  1440. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1441. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1442. * See intif_parse_StorageReceived() for item operations [lighta]
  1443. *------------------------------------------*/
  1444. void pc_reg_received(struct map_session_data *sd)
  1445. {
  1446. uint8 i;
  1447. sd->vars_ok = true;
  1448. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1449. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1450. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1451. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1452. if (msg_checklangtype(sd->langtype,true) < 0)
  1453. sd->langtype = 0; //invalid langtype reset to default
  1454. // Cash shop
  1455. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1456. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1457. // Cooking Exp
  1458. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1459. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1460. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1461. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1462. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1463. }
  1464. if (battle_config.feature_banking)
  1465. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1466. if (battle_config.feature_roulette) {
  1467. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1468. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1469. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1470. }
  1471. sd->roulette.prizeIdx = -1;
  1472. //SG map and mob read [Komurka]
  1473. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1474. uint16 j;
  1475. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1476. sd->feel_map[i].index = j;
  1477. sd->feel_map[i].m = map_mapindex2mapid(j);
  1478. } else {
  1479. sd->feel_map[i].index = 0;
  1480. sd->feel_map[i].m = -1;
  1481. }
  1482. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1483. }
  1484. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1485. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1486. sd->cloneskill_idx = skill_get_index(skid);
  1487. if (sd->cloneskill_idx > 0) {
  1488. sd->status.skill[sd->cloneskill_idx].id = skid;
  1489. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1490. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1491. sd->status.skill[sd->cloneskill_idx].lv = i;
  1492. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1493. }
  1494. }
  1495. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1496. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1497. sd->reproduceskill_idx = skill_get_index(skid);
  1498. if (sd->reproduceskill_idx > 0) {
  1499. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1500. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1501. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1502. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1503. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1504. }
  1505. }
  1506. //Weird... maybe registries were reloaded?
  1507. if (sd->state.active)
  1508. return;
  1509. sd->state.active = 1;
  1510. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1511. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1512. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1513. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1514. // Restore IM_CHAR instance to the player
  1515. for (const auto &instance : instances) {
  1516. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1517. sd->instance_id = instance.first;
  1518. break;
  1519. }
  1520. }
  1521. #if PACKETVER_MAIN_NUM < 20190403 || PACKETVER_RE_NUM < 20190320 || PACKETVER_ZERO_NUM < 20190410
  1522. if (sd->instance_id > 0)
  1523. instance_reqinfo(sd, sd->instance_id);
  1524. if (sd->status.party_id > 0)
  1525. party_member_joined(sd);
  1526. if (sd->status.guild_id > 0)
  1527. guild_member_joined(sd);
  1528. if (sd->status.clan_id > 0)
  1529. clan_member_joined(sd);
  1530. #endif
  1531. // pet
  1532. if (sd->status.pet_id > 0)
  1533. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1534. // Homunculus [albator]
  1535. if( sd->status.hom_id > 0 )
  1536. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1537. if( sd->status.mer_id > 0 )
  1538. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1539. if( sd->status.ele_id > 0 )
  1540. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1541. map_addiddb(&sd->bl);
  1542. map_delnickdb(sd->status.char_id, sd->status.name);
  1543. if (!chrif_auth_finished(sd))
  1544. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1545. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1546. chrif_bsdata_request(sd->status.char_id);
  1547. #ifdef VIP_ENABLE
  1548. sd->vip.time = 0;
  1549. sd->vip.enabled = 0;
  1550. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1551. #endif
  1552. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1553. intif_request_questlog(sd);
  1554. if (battle_config.feature_achievement) {
  1555. sd->achievement_data.total_score = 0;
  1556. sd->achievement_data.level = 0;
  1557. sd->achievement_data.save = false;
  1558. sd->achievement_data.count = 0;
  1559. sd->achievement_data.incompleteCount = 0;
  1560. sd->achievement_data.achievements = NULL;
  1561. intif_request_achievements(sd->status.char_id);
  1562. }
  1563. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1564. sd->state.connect_new = 1;
  1565. clif_parse_LoadEndAck(sd->fd, sd);
  1566. if( pc_isinvisible( sd ) ){
  1567. sd->vd.class_ = JT_INVISIBLE;
  1568. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1569. // decrement the number of pvp players on the map
  1570. map_getmapdata( sd->bl.m )->users_pvp--;
  1571. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1572. // unregister the player for ranking
  1573. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1574. sd->pvp_timer = INVALID_TIMER;
  1575. }
  1576. clif_changeoption( &sd->bl );
  1577. }
  1578. }
  1579. channel_autojoin(sd);
  1580. }
  1581. static int pc_calc_skillpoint(struct map_session_data* sd)
  1582. {
  1583. uint16 i, skill_point = 0;
  1584. nullpo_ret(sd);
  1585. for(i = 1; i < MAX_SKILL; i++) {
  1586. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1587. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1588. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1589. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1590. )
  1591. {
  1592. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1593. skill_point += sd->status.skill[i].lv;
  1594. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1595. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1596. }
  1597. }
  1598. }
  1599. return skill_point;
  1600. }
  1601. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1602. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1603. return false;
  1604. /**
  1605. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1606. * skill tree and since they have no icons they'll give resource errors
  1607. * Get ALL skills except npc/guild ones. [Skotlex]
  1608. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1609. **/
  1610. for (const auto &skill : skill_db) {
  1611. uint16 skill_id = skill.second->nameid;
  1612. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1613. continue;
  1614. switch (skill_id) {
  1615. case SM_SELFPROVOKE:
  1616. case AB_DUPLELIGHT_MELEE:
  1617. case AB_DUPLELIGHT_MAGIC:
  1618. case WL_CHAINLIGHTNING_ATK:
  1619. case WL_TETRAVORTEX_FIRE:
  1620. case WL_TETRAVORTEX_WATER:
  1621. case WL_TETRAVORTEX_WIND:
  1622. case WL_TETRAVORTEX_GROUND:
  1623. case WL_SUMMON_ATK_FIRE:
  1624. case WL_SUMMON_ATK_WIND:
  1625. case WL_SUMMON_ATK_WATER:
  1626. case WL_SUMMON_ATK_GROUND:
  1627. case LG_OVERBRAND_BRANDISH:
  1628. case LG_OVERBRAND_PLUSATK:
  1629. case WM_SEVERE_RAINSTORM_MELEE:
  1630. case RL_R_TRIP_PLUSATK:
  1631. case SG_DEVIL:
  1632. case MO_TRIPLEATTACK:
  1633. case RG_SNATCHER:
  1634. continue;
  1635. default:
  1636. {
  1637. uint8 lv = (uint8)skill_get_max(skill_id);
  1638. if (lv > 0) {
  1639. uint16 idx = skill_get_index(skill_id);
  1640. sd->status.skill[idx].id = skill_id;
  1641. if (addlv)
  1642. sd->status.skill[idx].lv = lv;
  1643. }
  1644. }
  1645. break;
  1646. }
  1647. }
  1648. return true;
  1649. }
  1650. /*==========================================
  1651. * Calculation of skill level.
  1652. * @param sd
  1653. *------------------------------------------*/
  1654. void pc_calc_skilltree(struct map_session_data *sd)
  1655. {
  1656. nullpo_retv(sd);
  1657. int job = pc_calc_skilltree_normalize_job(sd);
  1658. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1659. if( class_ == -1 )
  1660. { //Unable to normalize job??
  1661. 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);
  1662. return;
  1663. }
  1664. class_ = pc_class2idx(class_);
  1665. for (const auto &skill : skill_db) {
  1666. uint16 skill_id = skill.second->nameid;
  1667. uint16 idx = skill_get_index(skill_id);
  1668. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1669. sd->status.skill[idx].id = 0; //First clear skills.
  1670. /* permanent skills that must be re-checked */
  1671. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1672. if (skill_id == 0) {
  1673. sd->status.skill[idx].id = 0;
  1674. sd->status.skill[idx].lv = 0;
  1675. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1676. continue;
  1677. }
  1678. switch (skill_id) {
  1679. case NV_TRICKDEAD:
  1680. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1681. sd->status.skill[idx].id = 0;
  1682. sd->status.skill[idx].lv = 0;
  1683. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1684. }
  1685. break;
  1686. }
  1687. }
  1688. }
  1689. for (const auto &skill : skill_db) {
  1690. uint16 idx = skill_get_index(skill.second->nameid);
  1691. // Restore original level of skills after deleting earned skills.
  1692. 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 ) {
  1693. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1694. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1695. }
  1696. }
  1697. // Removes Taekwon Ranker skill bonus
  1698. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1699. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1700. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1701. uint16 sk_id = skill_tree[c_][i].skill_id;
  1702. uint16 sk_idx = 0;
  1703. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1704. continue;
  1705. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1706. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1707. continue;
  1708. sd->status.skill[sk_idx].id = 0;
  1709. }
  1710. }
  1711. }
  1712. // Grant all skills
  1713. pc_grant_allskills(sd, false);
  1714. int flag;
  1715. do {
  1716. uint16 skid = 0;
  1717. flag = 0;
  1718. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1719. bool fail = false;
  1720. uint16 sk_idx = skill_get_index(skid);
  1721. if (sd->status.skill[sk_idx].id)
  1722. continue; //Skill already known.
  1723. if (!battle_config.skillfree) {
  1724. // Checking required skills
  1725. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1726. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1727. uint16 sk_need_idx = 0;
  1728. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1729. short sk_need = sk_need_id;
  1730. 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)
  1731. sk_need = 0; //Not learned.
  1732. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1733. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1734. else
  1735. sk_need = pc_checkskill(sd,sk_need_id);
  1736. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1737. fail = true;
  1738. break;
  1739. }
  1740. }
  1741. }
  1742. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1743. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1744. c_ = pc_class2idx(c_);
  1745. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1746. fail = true; // base level requirement wasn't satisfied
  1747. }
  1748. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1749. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1750. c_ = pc_class2idx(c_);
  1751. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1752. fail = true; // job level requirement wasn't satisfied
  1753. }
  1754. }
  1755. if (!fail) {
  1756. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1757. if (!sd->status.skill[sk_idx].lv && (
  1758. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1759. skill->inf2[INF2_ISWEDDING] ||
  1760. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1761. ))
  1762. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1763. sd->status.skill[sk_idx].id = skid;
  1764. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1765. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1766. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1767. }
  1768. flag = 1; // skill list has changed, perform another pass
  1769. }
  1770. }
  1771. } while(flag);
  1772. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1773. uint16 skid = 0;
  1774. /* Taekwon Ranker Bonus Skill Tree
  1775. ============================================
  1776. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1777. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1778. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1779. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1780. uint16 sk_idx = skill_get_index(skid);
  1781. if (sk_idx == 0)
  1782. continue;
  1783. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1784. continue; //Do not include Quest/Wedding skills.
  1785. if( sd->status.skill[sk_idx].id == 0 ) {
  1786. sd->status.skill[sk_idx].id = skid;
  1787. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1788. } else if( skid != NV_BASIC )
  1789. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1790. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1791. }
  1792. }
  1793. // Enable Bard/Dancer spirit linked skills.
  1794. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  1795. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  1796. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  1797. { BA_POEMBRAGI, DC_FORTUNEKISS },
  1798. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  1799. for (const auto &skill : linked_skills) {
  1800. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  1801. continue;
  1802. // Tag it as a non-savable, non-uppable, bonus skill
  1803. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  1804. }
  1805. }
  1806. }
  1807. //Checks if you can learn a new skill after having leveled up a skill.
  1808. static void pc_check_skilltree(struct map_session_data *sd)
  1809. {
  1810. int i, flag = 0;
  1811. int c = 0;
  1812. if (battle_config.skillfree)
  1813. return; //Function serves no purpose if this is set
  1814. i = pc_calc_skilltree_normalize_job(sd);
  1815. c = pc_mapid2jobid(i, sd->status.sex);
  1816. if (c == -1) { //Unable to normalize job??
  1817. 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);
  1818. return;
  1819. }
  1820. c = pc_class2idx(c);
  1821. do {
  1822. uint16 skid = 0;
  1823. flag = 0;
  1824. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1825. uint16 sk_idx = skill_get_index(skid);
  1826. bool fail = false;
  1827. uint8 j = 0;
  1828. if (sd->status.skill[sk_idx].id) //Already learned
  1829. continue;
  1830. // Checking required skills
  1831. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1832. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1833. uint16 sk_need_idx = 0;
  1834. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1835. short sk_need = sk_need_id;
  1836. 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)
  1837. sk_need = 0; //Not learned.
  1838. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1839. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1840. else
  1841. sk_need = pc_checkskill(sd,sk_need_id);
  1842. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1843. fail = true;
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. if( fail )
  1849. continue;
  1850. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1851. continue;
  1852. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1853. if( !sd->status.skill[sk_idx].lv && (
  1854. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1855. skill->inf2[INF2_ISWEDDING] ||
  1856. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1857. ) )
  1858. continue; //Cannot be learned via normal means.
  1859. sd->status.skill[sk_idx].id = skid;
  1860. flag = 1;
  1861. }
  1862. } while(flag);
  1863. }
  1864. // Make sure all the skills are in the correct condition
  1865. // before persisting to the backend.. [MouseJstr]
  1866. void pc_clean_skilltree(struct map_session_data *sd)
  1867. {
  1868. uint16 i;
  1869. for (i = 0; i < MAX_SKILL; i++){
  1870. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1871. sd->status.skill[i].id = 0;
  1872. sd->status.skill[i].lv = 0;
  1873. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1874. }
  1875. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1876. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1877. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1878. }
  1879. }
  1880. }
  1881. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1882. {
  1883. int skill_point, novice_skills;
  1884. int c = sd->class_;
  1885. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1886. return c;
  1887. skill_point = pc_calc_skillpoint(sd);
  1888. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1889. // limit 1st class and above to novice job levels
  1890. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1891. {
  1892. c = MAPID_NOVICE;
  1893. }
  1894. // limit 2nd class and above to first class job levels (super novices are exempt)
  1895. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1896. {
  1897. // regenerate change_level_2nd
  1898. if (!sd->change_level_2nd)
  1899. {
  1900. if (sd->class_&JOBL_THIRD)
  1901. {
  1902. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1903. if (!sd->change_level_3rd)
  1904. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1905. else
  1906. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1907. - (sd->status.job_level - 1)
  1908. - (sd->change_level_3rd - 1)
  1909. - novice_skills;
  1910. }
  1911. else
  1912. {
  1913. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1914. - (sd->status.job_level - 1)
  1915. - novice_skills;
  1916. }
  1917. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1918. }
  1919. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1920. {
  1921. c &= MAPID_BASEMASK;
  1922. }
  1923. // limit 3rd class to 2nd class/trans job levels
  1924. else if(sd->class_&JOBL_THIRD)
  1925. {
  1926. // regenerate change_level_3rd
  1927. if (!sd->change_level_3rd)
  1928. {
  1929. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1930. - (sd->status.job_level - 1)
  1931. - (sd->change_level_2nd - 1)
  1932. - novice_skills;
  1933. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1934. }
  1935. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1936. c &= MAPID_UPPERMASK;
  1937. }
  1938. }
  1939. // restore non-limiting flags
  1940. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1941. return c;
  1942. }
  1943. /*==========================================
  1944. * Updates the weight status
  1945. *------------------------------------------
  1946. * 1: overweight 50% for pre-renewal and 70% for renewal
  1947. * 2: overweight 90%
  1948. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1949. */
  1950. void pc_updateweightstatus(struct map_session_data *sd)
  1951. {
  1952. int old_overweight;
  1953. int new_overweight;
  1954. nullpo_retv(sd);
  1955. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1956. #ifdef RENEWAL
  1957. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1958. #else
  1959. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1960. #endif
  1961. if( old_overweight == new_overweight )
  1962. return; // no change
  1963. // stop old status change
  1964. if( old_overweight == 1 )
  1965. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1966. else if( old_overweight == 2 )
  1967. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1968. // start new status change
  1969. if( new_overweight == 1 )
  1970. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1971. else if( new_overweight == 2 )
  1972. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1973. // update overweight status
  1974. sd->regen.state.overweight = new_overweight;
  1975. }
  1976. int pc_disguise(struct map_session_data *sd, int class_)
  1977. {
  1978. if (!class_ && !sd->disguise)
  1979. return 0;
  1980. if (class_ && sd->disguise == class_)
  1981. return 0;
  1982. if(pc_isinvisible(sd))
  1983. { //Character is invisible. Stealth class-change. [Skotlex]
  1984. sd->disguise = class_; //viewdata is set on uncloaking.
  1985. return 2;
  1986. }
  1987. if (sd->bl.prev != NULL) {
  1988. pc_stop_walking(sd, 0);
  1989. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1990. }
  1991. if (!class_) {
  1992. sd->disguise = 0;
  1993. class_ = sd->status.class_;
  1994. } else
  1995. sd->disguise=class_;
  1996. status_set_viewdata(&sd->bl, class_);
  1997. clif_changeoption(&sd->bl);
  1998. if (sd->bl.prev != NULL) {
  1999. clif_spawn(&sd->bl);
  2000. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2001. { //It seems the cart info is lost on undisguise.
  2002. clif_cartlist(sd);
  2003. clif_updatestatus(sd,SP_CARTINFO);
  2004. }
  2005. if (sd->chatID) {
  2006. struct chat_data* cd;
  2007. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2008. clif_dispchat(cd,0);
  2009. }
  2010. }
  2011. return 1;
  2012. }
  2013. /// Show error message
  2014. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2015. /// Check for valid Element, break & show error message if invalid Element
  2016. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2017. /// Check for valid Race, break & show error message if invalid Race
  2018. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2019. /// Check for valid Race2, break & show error message if invalid Race2
  2020. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2021. /// Check for valid Class, break & show error message if invalid Class
  2022. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2023. /// Check for valid Size, break & show error message if invalid Size
  2024. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2025. /// Check for valid SC, break & show error message if invalid SC
  2026. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2027. /**
  2028. * Add auto spell bonus for player while attacking/attacked
  2029. * @param spell: Spell array
  2030. * @param id: Skill to cast
  2031. * @param lv: Skill level
  2032. * @param rate: Success chance
  2033. * @param flag: Battle flag
  2034. * @param card_id: Used to prevent card stacking
  2035. */
  2036. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  2037. {
  2038. if (spell.size() == MAX_PC_BONUS) {
  2039. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2040. return;
  2041. }
  2042. if (!rate)
  2043. return;
  2044. if (!(flag&BF_RANGEMASK))
  2045. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2046. if (!(flag&BF_WEAPONMASK))
  2047. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2048. if (!(flag&BF_SKILLMASK)) {
  2049. if (flag&(BF_MAGIC | BF_MISC))
  2050. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2051. if (flag&BF_WEAPON)
  2052. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2053. }
  2054. for (auto &it : spell) {
  2055. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2056. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2057. return;
  2058. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2059. return;
  2060. }
  2061. }
  2062. struct s_autospell entry = {};
  2063. if (rate < -10000 || rate > 10000)
  2064. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2065. entry.id = id;
  2066. entry.lv = lv;
  2067. entry.rate = cap_value(rate, -10000, 10000);
  2068. entry.flag = flag;
  2069. entry.card_id = card_id;
  2070. spell.push_back(entry);
  2071. }
  2072. /**
  2073. * Add auto spell bonus for player while using skills
  2074. * @param spell: Spell array
  2075. * @param src_skill: Trigger skill
  2076. * @param id: Support or target type
  2077. * @param lv: Skill level
  2078. * @param rate: Success chance
  2079. * @param card_id: Used to prevent card stacking
  2080. */
  2081. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2082. {
  2083. if (spell.size() == MAX_PC_BONUS) {
  2084. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2085. return;
  2086. }
  2087. if (!rate)
  2088. return;
  2089. struct s_autospell entry = {};
  2090. if (rate < -10000 || rate > 10000)
  2091. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2092. entry.flag = src_skill;
  2093. entry.id = id;
  2094. entry.lv = lv;
  2095. entry.rate = cap_value(rate, -10000, 10000);
  2096. entry.card_id = card_id;
  2097. spell.push_back(entry);
  2098. }
  2099. /**
  2100. * Add inflict effect bonus for player while attacking/attacked
  2101. * @param effect: Effect array
  2102. * @param sc: SC/Effect type
  2103. * @param rate: Success chance
  2104. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2105. * @param flag: Target flag
  2106. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2107. */
  2108. 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)
  2109. {
  2110. if (effect.size() == MAX_PC_BONUS) {
  2111. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2112. return;
  2113. }
  2114. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2115. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2116. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2117. flag |= ATF_TARGET; //Default target: enemy.
  2118. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2119. flag |= ATF_WEAPON; //Default type: weapon.
  2120. if (!duration)
  2121. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2122. for (auto &it : effect) {
  2123. if (it.sc == sc && it.flag == flag) {
  2124. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2125. it.arrow_rate += arrow_rate;
  2126. it.duration = umax(it.duration, duration);
  2127. return;
  2128. }
  2129. }
  2130. struct s_addeffect entry = {};
  2131. if (rate < -10000 || rate > 10000)
  2132. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2133. entry.sc = sc;
  2134. entry.rate = cap_value(rate, -10000, 10000);
  2135. entry.arrow_rate = arrow_rate;
  2136. entry.flag = flag;
  2137. entry.duration = duration;
  2138. effect.push_back(entry);
  2139. }
  2140. /**
  2141. * Add inflict effect bonus for player while attacking using skill
  2142. * @param effect: Effect array
  2143. * @param sc: SC/Effect type
  2144. * @param rate: Success chance
  2145. * @param skill_id: Skill to cast
  2146. * @param target: Target type
  2147. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2148. */
  2149. 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)
  2150. {
  2151. if (effect.size() == MAX_PC_BONUS) {
  2152. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2153. return;
  2154. }
  2155. if (!duration)
  2156. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2157. for (auto &it : effect) {
  2158. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2159. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2160. it.duration = umax(it.duration, duration);
  2161. return;
  2162. }
  2163. }
  2164. struct s_addeffectonskill entry = {};
  2165. if (rate < -10000 || rate > 10000)
  2166. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2167. entry.sc = sc;
  2168. entry.rate = cap_value(rate, -10000, 10000);
  2169. entry.skill_id = skill_id;
  2170. entry.target = target;
  2171. entry.duration = duration;
  2172. effect.push_back(entry);
  2173. }
  2174. /**
  2175. * Adjust/add drop rate modifier for player
  2176. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2177. * @param nameid: item id that will be dropped
  2178. * @param group: group id
  2179. * @param class_: target class
  2180. * @param race: target race. if < 0, means monster_id
  2181. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2182. */
  2183. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2184. {
  2185. if (!nameid && !group) {
  2186. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2187. return;
  2188. }
  2189. if (nameid && !itemdb_exists(nameid)) {
  2190. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2191. return;
  2192. }
  2193. if (group && !itemdb_group_exists(group)) {
  2194. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2195. return;
  2196. }
  2197. if (drop.size() == MAX_PC_BONUS) {
  2198. 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);
  2199. return;
  2200. }
  2201. //Apply config rate adjustment settings.
  2202. if (rate >= 0) { //Absolute drop.
  2203. if (battle_config.item_rate_adddrop != 100)
  2204. rate = rate*battle_config.item_rate_adddrop/100;
  2205. if (rate < battle_config.item_drop_adddrop_min)
  2206. rate = battle_config.item_drop_adddrop_min;
  2207. else if (rate > battle_config.item_drop_adddrop_max)
  2208. rate = battle_config.item_drop_adddrop_max;
  2209. } else { //Relative drop, max/min limits are applied at drop time.
  2210. if (battle_config.item_rate_adddrop != 100)
  2211. rate = rate*battle_config.item_rate_adddrop/100;
  2212. if (rate > -1)
  2213. rate = -1;
  2214. }
  2215. for (auto &it : drop) {
  2216. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2217. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2218. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2219. return;
  2220. }
  2221. }
  2222. struct s_add_drop entry = {};
  2223. if (rate < -10000 || rate > 10000)
  2224. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2225. entry.nameid = nameid;
  2226. entry.group = group;
  2227. entry.race = race;
  2228. entry.class_ = class_;
  2229. entry.rate = cap_value(rate, -10000, 10000);
  2230. drop.push_back(entry);
  2231. }
  2232. /**
  2233. * Add autobonus to player when attacking/attacked
  2234. * @param bonus: Bonus array
  2235. * @param script: Script to execute
  2236. * @param rate: Success chance
  2237. * @param dur: Duration
  2238. * @param flag: Battle flag/skill
  2239. * @param other_script: Secondary script to execute
  2240. * @param pos: Item equip position
  2241. * @param onskill: Skill used to trigger autobonus
  2242. * @return True on success or false otherwise
  2243. */
  2244. bool pc_addautobonus(std::vector<s_autobonus> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill)
  2245. {
  2246. if (bonus.size() == MAX_PC_BONUS) {
  2247. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2248. return false;
  2249. }
  2250. if (!onskill) {
  2251. if (!(flag&BF_RANGEMASK))
  2252. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2253. if (!(flag&BF_WEAPONMASK))
  2254. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2255. if (!(flag&BF_SKILLMASK)) {
  2256. if (flag&(BF_MAGIC | BF_MISC))
  2257. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2258. if (flag&BF_WEAPON)
  2259. flag |= BF_NORMAL | BF_SKILL;
  2260. }
  2261. }
  2262. struct s_autobonus entry = {};
  2263. if (rate < -10000 || rate > 10000)
  2264. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2265. entry.rate = cap_value(rate, -10000, 10000);
  2266. entry.duration = dur;
  2267. entry.active = INVALID_TIMER;
  2268. entry.atk_type = flag;
  2269. entry.pos = pos;
  2270. entry.bonus_script = aStrdup(script);
  2271. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2272. bonus.push_back(entry);
  2273. return true;
  2274. }
  2275. /**
  2276. * Remove an autobonus from player
  2277. * @param sd: Player data
  2278. * @param bonus: Autobonus array
  2279. * @param restore: Run script on clearing or not
  2280. */
  2281. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2282. {
  2283. nullpo_retv(sd);
  2284. std::vector<s_autobonus>::iterator it = bonus.begin();
  2285. while( it != bonus.end() ){
  2286. s_autobonus b = *it;
  2287. if (b.active != INVALID_TIMER) {
  2288. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2289. if (b.bonus_script) {
  2290. unsigned int equip_pos_idx = 0;
  2291. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2292. for (uint8 j = 0; j < EQI_MAX; j++) {
  2293. if (sd->equip_index[j] >= 0)
  2294. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2295. }
  2296. if ((equip_pos_idx&b.pos) == b.pos)
  2297. script_run_autobonus(b.bonus_script, sd, b.pos);
  2298. }
  2299. it++;
  2300. continue;
  2301. } else { // Logout / Unequipped an item with an activated bonus
  2302. delete_timer(b.active, pc_endautobonus);
  2303. b.active = INVALID_TIMER;
  2304. }
  2305. }
  2306. if (b.bonus_script)
  2307. aFree(b.bonus_script);
  2308. if (b.other_script)
  2309. aFree(b.other_script);
  2310. it = bonus.erase(it);
  2311. }
  2312. }
  2313. /**
  2314. * Execute autobonus on player
  2315. * @param sd: Player data
  2316. * @param autobonus: Autobonus to run
  2317. */
  2318. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2319. {
  2320. nullpo_retv(sd);
  2321. nullpo_retv(autobonus);
  2322. if (autobonus->active != INVALID_TIMER)
  2323. delete_timer(autobonus->active, pc_endautobonus);
  2324. if( autobonus->other_script )
  2325. {
  2326. int j;
  2327. unsigned int equip_pos_idx = 0;
  2328. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2329. for(j = 0; j < EQI_MAX; j++) {
  2330. if(sd->equip_index[j] >= 0)
  2331. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2332. }
  2333. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2334. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2335. }
  2336. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2337. sd->state.autobonus |= autobonus->pos;
  2338. status_calc_pc(sd,SCO_NONE);
  2339. }
  2340. /**
  2341. * Remove autobonus timer from player
  2342. */
  2343. TIMER_FUNC(pc_endautobonus){
  2344. struct map_session_data *sd = map_id2sd(id);
  2345. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2346. nullpo_ret(sd);
  2347. nullpo_ret(bonus);
  2348. for( struct s_autobonus& autobonus : *bonus ){
  2349. if( autobonus.active == tid ){
  2350. autobonus.active = INVALID_TIMER;
  2351. sd->state.autobonus &= ~autobonus.pos;
  2352. break;
  2353. }
  2354. }
  2355. status_calc_pc(sd,SCO_NONE);
  2356. return 0;
  2357. }
  2358. /**
  2359. * Add element bonus to player when attacking
  2360. * @param sd: Player data
  2361. * @param ele: Element to adjust
  2362. * @param rate: Success chance
  2363. * @param flag: Battle flag
  2364. */
  2365. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2366. {
  2367. nullpo_retv(sd);
  2368. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2369. if (wd->addele2.size() == MAX_PC_BONUS) {
  2370. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2371. return;
  2372. }
  2373. if (!(flag&BF_RANGEMASK))
  2374. flag |= BF_SHORT | BF_LONG;
  2375. if (!(flag&BF_WEAPONMASK))
  2376. flag |= BF_WEAPON;
  2377. if (!(flag&BF_SKILLMASK)) {
  2378. if (flag&(BF_MAGIC | BF_MISC))
  2379. flag |= BF_SKILL;
  2380. if (flag&BF_WEAPON)
  2381. flag |= BF_NORMAL | BF_SKILL;
  2382. }
  2383. for (auto &it : wd->addele2) {
  2384. if (it.ele == ele && it.flag == flag) {
  2385. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2386. return;
  2387. }
  2388. }
  2389. struct s_addele2 entry = {};
  2390. if (rate < -10000 || rate > 10000)
  2391. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2392. entry.ele = ele;
  2393. entry.rate = cap_value(rate, -10000, 10000);
  2394. entry.flag = flag;
  2395. wd->addele2.push_back(entry);
  2396. }
  2397. /**
  2398. * Reduce element bonus to player when attacking
  2399. * @param sd: Player data
  2400. * @param ele: Element to adjust
  2401. * @param rate: Success chance
  2402. * @param flag: Battle flag
  2403. */
  2404. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2405. {
  2406. nullpo_retv(sd);
  2407. if (sd->subele2.size() == MAX_PC_BONUS) {
  2408. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2409. return;
  2410. }
  2411. if (!(flag&BF_RANGEMASK))
  2412. flag |= BF_SHORT | BF_LONG;
  2413. if (!(flag&BF_WEAPONMASK))
  2414. flag |= BF_WEAPON;
  2415. if (!(flag&BF_SKILLMASK)) {
  2416. if (flag&(BF_MAGIC | BF_MISC))
  2417. flag |= BF_SKILL;
  2418. if (flag&BF_WEAPON)
  2419. flag |= BF_NORMAL | BF_SKILL;
  2420. }
  2421. for (auto &it : sd->subele2) {
  2422. if (it.ele == ele && it.flag == flag) {
  2423. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2424. return;
  2425. }
  2426. }
  2427. struct s_addele2 entry = {};
  2428. if (rate < -10000 || rate > 10000)
  2429. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2430. entry.ele = ele;
  2431. entry.rate = cap_value(rate, -10000, 10000);
  2432. entry.flag = flag;
  2433. sd->subele2.push_back(entry);
  2434. }
  2435. /**
  2436. * Adjust race damage to target when attacking
  2437. * @param sd: Player data
  2438. * @param race: Race to adjust
  2439. * @param rate: Success chance
  2440. * @param flag: Battle flag
  2441. */
  2442. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2443. {
  2444. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2445. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2446. return;
  2447. }
  2448. if (!(flag&BF_RANGEMASK))
  2449. flag |= BF_SHORT | BF_LONG;
  2450. if (!(flag&BF_WEAPONMASK))
  2451. flag |= BF_WEAPON;
  2452. if (!(flag&BF_SKILLMASK)) {
  2453. if (flag&(BF_MAGIC | BF_MISC))
  2454. flag |= BF_SKILL;
  2455. if (flag&BF_WEAPON)
  2456. flag |= BF_NORMAL | BF_SKILL;
  2457. }
  2458. for (auto &it : sd->subrace3) {
  2459. if (it.race == race && it.flag == flag) {
  2460. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2461. return;
  2462. }
  2463. }
  2464. struct s_addrace2 entry = {};
  2465. if (rate < -10000 || rate > 10000)
  2466. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2467. entry.race = race;
  2468. entry.rate = cap_value(rate, -10000, 10000);
  2469. entry.flag = flag;
  2470. sd->subrace3.push_back(entry);
  2471. }
  2472. /**
  2473. * General item bonus for player
  2474. * @param bonus: Bonus array
  2475. * @param id: Key
  2476. * @param val: Value
  2477. * @param cap_rate: If Value is a rate value that needs to be capped
  2478. */
  2479. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2480. {
  2481. for (auto &it : bonus) {
  2482. if (it.id == id) {
  2483. if (cap_rate)
  2484. it.val = cap_value(it.val + val, -10000, 10000);
  2485. else
  2486. it.val += val;
  2487. return;
  2488. }
  2489. }
  2490. struct s_item_bonus entry = {};
  2491. if (cap_rate && (val < -10000 || val > 10000)) {
  2492. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2493. val = cap_value(val, -10000, 10000);
  2494. }
  2495. entry.id = id;
  2496. entry.val = val;
  2497. bonus.push_back(entry);
  2498. }
  2499. /**
  2500. * Remove HP/SP to player when attacking
  2501. * @param bonus: Bonus array
  2502. * @param rate: Success chance
  2503. * @param per: Percentage of HP/SP to vanish
  2504. * @param flag: Battle flag
  2505. */
  2506. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2507. if (bonus.size() == MAX_PC_BONUS) {
  2508. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2509. return;
  2510. }
  2511. if (!(flag&BF_RANGEMASK))
  2512. flag |= BF_SHORT | BF_LONG;
  2513. if (!(flag&BF_WEAPONMASK))
  2514. flag |= BF_WEAPON;
  2515. if (!(flag&BF_SKILLMASK)) {
  2516. if (flag&(BF_MAGIC | BF_MISC))
  2517. flag |= BF_SKILL;
  2518. if (flag&BF_WEAPON)
  2519. flag |= BF_NORMAL | BF_SKILL;
  2520. }
  2521. for (auto &it : bonus) {
  2522. if (it.flag == flag) {
  2523. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2524. it.per += per;
  2525. return;
  2526. }
  2527. }
  2528. struct s_vanish_bonus entry = {};
  2529. if (rate < -10000 || rate > 10000)
  2530. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2531. entry.rate = cap_value(rate, -10000, 10000);
  2532. entry.per = per;
  2533. entry.flag = flag;
  2534. bonus.push_back(entry);
  2535. }
  2536. /*==========================================
  2537. * Add a bonus(type) to player sd
  2538. * format: bonus bBonusName,val;
  2539. * @param sd
  2540. * @param type Bonus type used by bBonusName
  2541. * @param val Value that usually for rate or fixed value
  2542. *------------------------------------------*/
  2543. void pc_bonus(struct map_session_data *sd,int type,int val)
  2544. {
  2545. struct status_data *status;
  2546. int bonus;
  2547. nullpo_retv(sd);
  2548. status = &sd->base_status;
  2549. switch(type){
  2550. case SP_STR:
  2551. case SP_AGI:
  2552. case SP_VIT:
  2553. case SP_INT:
  2554. case SP_DEX:
  2555. case SP_LUK:
  2556. if(sd->state.lr_flag != 2)
  2557. sd->param_bonus[type-SP_STR]+=val;
  2558. break;
  2559. case SP_ATK1:
  2560. if(!sd->state.lr_flag) {
  2561. bonus = status->rhw.atk + val;
  2562. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2563. }
  2564. else if(sd->state.lr_flag == 1) {
  2565. bonus = status->lhw.atk + val;
  2566. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2567. }
  2568. break;
  2569. case SP_ATK2:
  2570. if(!sd->state.lr_flag) {
  2571. bonus = status->rhw.atk2 + val;
  2572. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2573. }
  2574. else if(sd->state.lr_flag == 1) {
  2575. bonus = status->lhw.atk2 + val;
  2576. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2577. }
  2578. break;
  2579. case SP_BASE_ATK:
  2580. if(sd->state.lr_flag != 2) {
  2581. #ifdef RENEWAL
  2582. bonus = sd->bonus.eatk + val;
  2583. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2584. #else
  2585. bonus = status->batk + val;
  2586. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2587. #endif
  2588. }
  2589. break;
  2590. case SP_DEF1:
  2591. if(sd->state.lr_flag != 2) {
  2592. bonus = status->def + val;
  2593. #ifdef RENEWAL
  2594. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2595. #else
  2596. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2597. #endif
  2598. }
  2599. break;
  2600. case SP_DEF2:
  2601. if(sd->state.lr_flag != 2) {
  2602. bonus = status->def2 + val;
  2603. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2604. }
  2605. break;
  2606. case SP_MDEF1:
  2607. if(sd->state.lr_flag != 2) {
  2608. bonus = status->mdef + val;
  2609. #ifdef RENEWAL
  2610. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2611. #else
  2612. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2613. #endif
  2614. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2615. sd->bonus.shieldmdef += bonus;
  2616. }
  2617. }
  2618. break;
  2619. case SP_MDEF2:
  2620. if(sd->state.lr_flag != 2) {
  2621. bonus = status->mdef2 + val;
  2622. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2623. }
  2624. break;
  2625. case SP_HIT:
  2626. if(sd->state.lr_flag != 2) {
  2627. bonus = status->hit + val;
  2628. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2629. } else
  2630. sd->bonus.arrow_hit+=val;
  2631. break;
  2632. case SP_FLEE1:
  2633. if(sd->state.lr_flag != 2) {
  2634. bonus = status->flee + val;
  2635. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2636. }
  2637. break;
  2638. case SP_FLEE2:
  2639. if(sd->state.lr_flag != 2) {
  2640. bonus = status->flee2 + val*10;
  2641. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2642. }
  2643. break;
  2644. case SP_CRITICAL:
  2645. if(sd->state.lr_flag != 2) {
  2646. bonus = status->cri + val*10;
  2647. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2648. } else
  2649. sd->bonus.arrow_cri += val*10;
  2650. break;
  2651. case SP_ATKELE:
  2652. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2653. switch (sd->state.lr_flag)
  2654. {
  2655. case 2:
  2656. switch (sd->status.weapon) {
  2657. case W_BOW:
  2658. case W_REVOLVER:
  2659. case W_RIFLE:
  2660. case W_GATLING:
  2661. case W_SHOTGUN:
  2662. case W_GRENADE:
  2663. //Become weapon element.
  2664. status->rhw.ele=val;
  2665. break;
  2666. default: //Become arrow element.
  2667. sd->bonus.arrow_ele=val;
  2668. break;
  2669. }
  2670. break;
  2671. case 1:
  2672. status->lhw.ele=val;
  2673. break;
  2674. default:
  2675. status->rhw.ele=val;
  2676. break;
  2677. }
  2678. break;
  2679. case SP_DEFELE:
  2680. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2681. if(sd->state.lr_flag != 2)
  2682. status->def_ele=val;
  2683. break;
  2684. case SP_MAXHP:
  2685. if(sd->state.lr_flag == 2)
  2686. break;
  2687. sd->bonus.hp += val;
  2688. break;
  2689. case SP_MAXSP:
  2690. if(sd->state.lr_flag == 2)
  2691. break;
  2692. sd->bonus.sp += val;
  2693. break;
  2694. case SP_MAXHPRATE:
  2695. if(sd->state.lr_flag != 2)
  2696. sd->hprate+=val;
  2697. break;
  2698. case SP_MAXSPRATE:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->sprate+=val;
  2701. break;
  2702. case SP_SPRATE:
  2703. if(sd->state.lr_flag != 2)
  2704. sd->dsprate+=val;
  2705. break;
  2706. case SP_ATTACKRANGE:
  2707. switch (sd->state.lr_flag) {
  2708. case 2:
  2709. switch (sd->status.weapon) {
  2710. case W_BOW:
  2711. case W_REVOLVER:
  2712. case W_RIFLE:
  2713. case W_GATLING:
  2714. case W_SHOTGUN:
  2715. case W_GRENADE:
  2716. status->rhw.range += val;
  2717. }
  2718. break;
  2719. case 1:
  2720. status->lhw.range += val;
  2721. break;
  2722. default:
  2723. status->rhw.range += val;
  2724. break;
  2725. }
  2726. break;
  2727. case SP_SPEED_RATE: //Non stackable increase
  2728. if(sd->state.lr_flag != 2)
  2729. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2730. break;
  2731. case SP_SPEED_ADDRATE: //Stackable increase
  2732. if(sd->state.lr_flag != 2)
  2733. sd->bonus.speed_add_rate -= val;
  2734. break;
  2735. case SP_ASPD: //Raw increase
  2736. if(sd->state.lr_flag != 2)
  2737. sd->bonus.aspd_add -= 10*val;
  2738. break;
  2739. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2740. if(sd->state.lr_flag != 2)
  2741. #ifndef RENEWAL_ASPD
  2742. status->aspd_rate -= 10*val;
  2743. #else
  2744. status->aspd_rate2 += val;
  2745. #endif
  2746. break;
  2747. case SP_HP_RECOV_RATE:
  2748. if(sd->state.lr_flag != 2)
  2749. sd->hprecov_rate += val;
  2750. break;
  2751. case SP_SP_RECOV_RATE:
  2752. if(sd->state.lr_flag != 2)
  2753. sd->sprecov_rate += val;
  2754. break;
  2755. case SP_CRITICAL_DEF:
  2756. if(sd->state.lr_flag != 2)
  2757. sd->bonus.critical_def += val;
  2758. break;
  2759. case SP_NEAR_ATK_DEF:
  2760. if(sd->state.lr_flag != 2)
  2761. sd->bonus.near_attack_def_rate += val;
  2762. break;
  2763. case SP_LONG_ATK_DEF:
  2764. if(sd->state.lr_flag != 2)
  2765. sd->bonus.long_attack_def_rate += val;
  2766. break;
  2767. case SP_DOUBLE_RATE:
  2768. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2769. sd->bonus.double_rate = val;
  2770. break;
  2771. case SP_DOUBLE_ADD_RATE:
  2772. if(sd->state.lr_flag == 0)
  2773. sd->bonus.double_add_rate += val;
  2774. break;
  2775. case SP_MATK_RATE:
  2776. if(sd->state.lr_flag != 2)
  2777. sd->matk_rate += val;
  2778. break;
  2779. case SP_IGNORE_DEF_ELE:
  2780. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2781. if(!sd->state.lr_flag)
  2782. sd->right_weapon.ignore_def_ele |= 1<<val;
  2783. else if(sd->state.lr_flag == 1)
  2784. sd->left_weapon.ignore_def_ele |= 1<<val;
  2785. break;
  2786. case SP_IGNORE_DEF_RACE:
  2787. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2788. if(!sd->state.lr_flag)
  2789. sd->right_weapon.ignore_def_race |= 1<<val;
  2790. else if(sd->state.lr_flag == 1)
  2791. sd->left_weapon.ignore_def_race |= 1<<val;
  2792. break;
  2793. case SP_IGNORE_DEF_CLASS:
  2794. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2795. if(!sd->state.lr_flag)
  2796. sd->right_weapon.ignore_def_class |= 1<<val;
  2797. else if(sd->state.lr_flag == 1)
  2798. sd->left_weapon.ignore_def_class |= 1<<val;
  2799. break;
  2800. case SP_ATK_RATE:
  2801. if(sd->state.lr_flag != 2)
  2802. sd->bonus.atk_rate += val;
  2803. break;
  2804. case SP_MAGIC_ATK_DEF:
  2805. if(sd->state.lr_flag != 2)
  2806. sd->bonus.magic_def_rate += val;
  2807. break;
  2808. case SP_MISC_ATK_DEF:
  2809. if(sd->state.lr_flag != 2)
  2810. sd->bonus.misc_def_rate += val;
  2811. break;
  2812. case SP_IGNORE_MDEF_ELE:
  2813. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2814. if(sd->state.lr_flag != 2)
  2815. sd->bonus.ignore_mdef_ele |= 1<<val;
  2816. break;
  2817. case SP_IGNORE_MDEF_RACE:
  2818. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2819. if(sd->state.lr_flag != 2)
  2820. sd->bonus.ignore_mdef_race |= 1<<val;
  2821. break;
  2822. case SP_PERFECT_HIT_RATE:
  2823. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2824. sd->bonus.perfect_hit = val;
  2825. break;
  2826. case SP_PERFECT_HIT_ADD_RATE:
  2827. if(sd->state.lr_flag != 2)
  2828. sd->bonus.perfect_hit_add += val;
  2829. break;
  2830. case SP_CRITICAL_RATE:
  2831. if(sd->state.lr_flag != 2)
  2832. sd->critical_rate+=val;
  2833. break;
  2834. case SP_DEF_RATIO_ATK_ELE:
  2835. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2836. if(!sd->state.lr_flag)
  2837. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2838. else if(sd->state.lr_flag == 1)
  2839. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2840. break;
  2841. case SP_DEF_RATIO_ATK_RACE:
  2842. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2843. if(!sd->state.lr_flag)
  2844. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2845. else if(sd->state.lr_flag == 1)
  2846. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2847. break;
  2848. case SP_DEF_RATIO_ATK_CLASS:
  2849. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2850. if(!sd->state.lr_flag)
  2851. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2852. else if(sd->state.lr_flag == 1)
  2853. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2854. break;
  2855. case SP_HIT_RATE:
  2856. if(sd->state.lr_flag != 2)
  2857. sd->hit_rate += val;
  2858. break;
  2859. case SP_FLEE_RATE:
  2860. if(sd->state.lr_flag != 2)
  2861. sd->flee_rate += val;
  2862. break;
  2863. case SP_FLEE2_RATE:
  2864. if(sd->state.lr_flag != 2)
  2865. sd->flee2_rate += val;
  2866. break;
  2867. case SP_DEF_RATE:
  2868. if(sd->state.lr_flag != 2)
  2869. sd->def_rate += val;
  2870. break;
  2871. case SP_DEF2_RATE:
  2872. if(sd->state.lr_flag != 2)
  2873. sd->def2_rate += val;
  2874. break;
  2875. case SP_MDEF_RATE:
  2876. if(sd->state.lr_flag != 2)
  2877. sd->mdef_rate += val;
  2878. break;
  2879. case SP_MDEF2_RATE:
  2880. if(sd->state.lr_flag != 2)
  2881. sd->mdef2_rate += val;
  2882. break;
  2883. case SP_RESTART_FULL_RECOVER:
  2884. if(sd->state.lr_flag != 2)
  2885. sd->special_state.restart_full_recover = 1;
  2886. break;
  2887. case SP_NO_CASTCANCEL:
  2888. if(sd->state.lr_flag != 2)
  2889. sd->special_state.no_castcancel = 1;
  2890. break;
  2891. case SP_NO_CASTCANCEL2:
  2892. if(sd->state.lr_flag != 2)
  2893. sd->special_state.no_castcancel2 = 1;
  2894. break;
  2895. case SP_NO_SIZEFIX:
  2896. if(sd->state.lr_flag != 2)
  2897. sd->special_state.no_sizefix = 1;
  2898. break;
  2899. case SP_NO_MAGIC_DAMAGE:
  2900. if(sd->state.lr_flag == 2)
  2901. break;
  2902. val+= sd->special_state.no_magic_damage;
  2903. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2904. break;
  2905. case SP_NO_WEAPON_DAMAGE:
  2906. if(sd->state.lr_flag == 2)
  2907. break;
  2908. val+= sd->special_state.no_weapon_damage;
  2909. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2910. break;
  2911. case SP_NO_MISC_DAMAGE:
  2912. if(sd->state.lr_flag == 2)
  2913. break;
  2914. val+= sd->special_state.no_misc_damage;
  2915. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2916. break;
  2917. case SP_NO_GEMSTONE:
  2918. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2919. sd->special_state.no_gemstone = 1;
  2920. break;
  2921. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2922. if(sd->state.lr_flag != 2) {
  2923. sd->special_state.intravision = 1;
  2924. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2925. }
  2926. break;
  2927. case SP_NO_KNOCKBACK:
  2928. if(sd->state.lr_flag != 2)
  2929. sd->special_state.no_knockback = 1;
  2930. break;
  2931. case SP_SPLASH_RANGE:
  2932. if(sd->bonus.splash_range < val)
  2933. sd->bonus.splash_range = val;
  2934. break;
  2935. case SP_SPLASH_ADD_RANGE:
  2936. sd->bonus.splash_add_range += val;
  2937. break;
  2938. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2939. if(sd->state.lr_flag != 2)
  2940. sd->bonus.short_weapon_damage_return += val;
  2941. break;
  2942. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2943. if(sd->state.lr_flag != 2)
  2944. sd->bonus.long_weapon_damage_return += val;
  2945. break;
  2946. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2947. if(sd->state.lr_flag != 2)
  2948. sd->bonus.magic_damage_return += val;
  2949. break;
  2950. case SP_ALL_STATS: // [Valaris]
  2951. if(sd->state.lr_flag!=2) {
  2952. sd->param_bonus[SP_STR-SP_STR]+=val;
  2953. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2954. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2955. sd->param_bonus[SP_INT-SP_STR]+=val;
  2956. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2957. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2958. }
  2959. break;
  2960. case SP_AGI_VIT: // [Valaris]
  2961. if(sd->state.lr_flag!=2) {
  2962. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2963. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2964. }
  2965. break;
  2966. case SP_AGI_DEX_STR: // [Valaris]
  2967. if(sd->state.lr_flag!=2) {
  2968. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2969. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2970. sd->param_bonus[SP_STR-SP_STR]+=val;
  2971. }
  2972. break;
  2973. case SP_PERFECT_HIDE: // [Valaris]
  2974. if(sd->state.lr_flag!=2)
  2975. sd->special_state.perfect_hiding=1;
  2976. break;
  2977. case SP_UNBREAKABLE:
  2978. if(sd->state.lr_flag!=2)
  2979. sd->bonus.unbreakable += val;
  2980. break;
  2981. case SP_UNBREAKABLE_WEAPON:
  2982. if(sd->state.lr_flag != 2)
  2983. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2984. break;
  2985. case SP_UNBREAKABLE_ARMOR:
  2986. if(sd->state.lr_flag != 2)
  2987. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2988. break;
  2989. case SP_UNBREAKABLE_HELM:
  2990. if(sd->state.lr_flag != 2)
  2991. sd->bonus.unbreakable_equip |= EQP_HELM;
  2992. break;
  2993. case SP_UNBREAKABLE_SHIELD:
  2994. if(sd->state.lr_flag != 2)
  2995. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2996. break;
  2997. case SP_UNBREAKABLE_GARMENT:
  2998. if(sd->state.lr_flag != 2)
  2999. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3000. break;
  3001. case SP_UNBREAKABLE_SHOES:
  3002. if(sd->state.lr_flag != 2)
  3003. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3004. break;
  3005. case SP_CLASSCHANGE: // [Valaris]
  3006. if(sd->state.lr_flag !=2)
  3007. sd->bonus.classchange=val;
  3008. break;
  3009. case SP_SHORT_ATK_RATE:
  3010. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3011. sd->bonus.short_attack_atk_rate+=val;
  3012. break;
  3013. case SP_LONG_ATK_RATE:
  3014. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3015. sd->bonus.long_attack_atk_rate+=val;
  3016. break;
  3017. case SP_BREAK_WEAPON_RATE:
  3018. if(sd->state.lr_flag != 2)
  3019. sd->bonus.break_weapon_rate+=val;
  3020. break;
  3021. case SP_BREAK_ARMOR_RATE:
  3022. if(sd->state.lr_flag != 2)
  3023. sd->bonus.break_armor_rate+=val;
  3024. break;
  3025. case SP_ADD_STEAL_RATE:
  3026. if(sd->state.lr_flag != 2)
  3027. sd->bonus.add_steal_rate+=val;
  3028. break;
  3029. case SP_DELAYRATE:
  3030. if(sd->state.lr_flag != 2)
  3031. sd->delayrate+=val;
  3032. break;
  3033. case SP_CRIT_ATK_RATE:
  3034. if(sd->state.lr_flag != 2)
  3035. sd->bonus.crit_atk_rate += val;
  3036. break;
  3037. case SP_CRIT_DEF_RATE:
  3038. if(sd->state.lr_flag != 2)
  3039. sd->bonus.crit_def_rate += val;
  3040. break;
  3041. case SP_NO_REGEN:
  3042. if(sd->state.lr_flag != 2)
  3043. sd->regen.state.block|=val;
  3044. break;
  3045. case SP_UNSTRIPABLE_WEAPON:
  3046. if(sd->state.lr_flag != 2)
  3047. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3048. break;
  3049. case SP_UNSTRIPABLE:
  3050. case SP_UNSTRIPABLE_ARMOR:
  3051. if(sd->state.lr_flag != 2)
  3052. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3053. break;
  3054. case SP_UNSTRIPABLE_HELM:
  3055. if(sd->state.lr_flag != 2)
  3056. sd->bonus.unstripable_equip |= EQP_HELM;
  3057. break;
  3058. case SP_UNSTRIPABLE_SHIELD:
  3059. if(sd->state.lr_flag != 2)
  3060. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3061. break;
  3062. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3063. if(!sd->state.lr_flag) {
  3064. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3065. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3066. } else if(sd->state.lr_flag == 1) {
  3067. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3068. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3069. }
  3070. break;
  3071. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3072. if(!sd->state.lr_flag) {
  3073. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3074. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3075. } else if(sd->state.lr_flag == 1) {
  3076. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3077. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3078. }
  3079. break;
  3080. case SP_SP_GAIN_VALUE:
  3081. if(!sd->state.lr_flag)
  3082. sd->bonus.sp_gain_value += val;
  3083. break;
  3084. case SP_HP_GAIN_VALUE:
  3085. if(!sd->state.lr_flag)
  3086. sd->bonus.hp_gain_value += val;
  3087. break;
  3088. case SP_LONG_SP_GAIN_VALUE:
  3089. if(!sd->state.lr_flag)
  3090. sd->bonus.long_sp_gain_value += val;
  3091. case SP_LONG_HP_GAIN_VALUE:
  3092. if(!sd->state.lr_flag)
  3093. sd->bonus.long_hp_gain_value += val;
  3094. break;
  3095. case SP_MAGIC_SP_GAIN_VALUE:
  3096. if(!sd->state.lr_flag)
  3097. sd->bonus.magic_sp_gain_value += val;
  3098. break;
  3099. case SP_MAGIC_HP_GAIN_VALUE:
  3100. if(!sd->state.lr_flag)
  3101. sd->bonus.magic_hp_gain_value += val;
  3102. break;
  3103. case SP_ADD_HEAL_RATE:
  3104. if(sd->state.lr_flag != 2)
  3105. sd->bonus.add_heal_rate += val;
  3106. break;
  3107. case SP_ADD_HEAL2_RATE:
  3108. if(sd->state.lr_flag != 2)
  3109. sd->bonus.add_heal2_rate += val;
  3110. break;
  3111. case SP_ADD_ITEM_HEAL_RATE:
  3112. if(sd->state.lr_flag != 2)
  3113. sd->bonus.itemhealrate2 += val;
  3114. break;
  3115. case SP_EMATK:
  3116. if(sd->state.lr_flag != 2)
  3117. sd->bonus.ematk += val;
  3118. break;
  3119. case SP_ADD_VARIABLECAST:
  3120. if (sd->state.lr_flag != 2)
  3121. sd->bonus.add_varcast += val;
  3122. break;
  3123. #ifdef RENEWAL_CAST
  3124. case SP_FIXCASTRATE:
  3125. if(sd->state.lr_flag != 2)
  3126. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3127. break;
  3128. case SP_ADD_FIXEDCAST:
  3129. if(sd->state.lr_flag != 2)
  3130. sd->bonus.add_fixcast += val;
  3131. break;
  3132. case SP_CASTRATE:
  3133. case SP_VARCASTRATE:
  3134. if(sd->state.lr_flag != 2)
  3135. sd->bonus.varcastrate -= val;
  3136. break;
  3137. #else
  3138. case SP_ADD_FIXEDCAST:
  3139. case SP_FIXCASTRATE:
  3140. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3141. break;
  3142. case SP_VARCASTRATE:
  3143. case SP_CASTRATE:
  3144. if(sd->state.lr_flag != 2)
  3145. sd->castrate += val;
  3146. break;
  3147. #endif
  3148. case SP_ADDMAXWEIGHT:
  3149. if (sd->state.lr_flag != 2)
  3150. sd->add_max_weight += val;
  3151. break;
  3152. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3153. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3154. break;
  3155. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3156. if (sd->state.lr_flag != 2)
  3157. sd->bonus.critical_rangeatk += val*10;
  3158. else
  3159. sd->bonus.arrow_cri += val*10;
  3160. break;
  3161. case SP_WEAPON_ATK_RATE:
  3162. if (sd->state.lr_flag != 2)
  3163. sd->bonus.weapon_atk_rate += val;
  3164. break;
  3165. case SP_WEAPON_MATK_RATE:
  3166. if (sd->state.lr_flag != 2)
  3167. sd->bonus.weapon_matk_rate += val;
  3168. break;
  3169. case SP_NO_MADO_FUEL:
  3170. if (sd->state.lr_flag != 2)
  3171. sd->special_state.no_mado_fuel = 1;
  3172. break;
  3173. case SP_NO_WALK_DELAY:
  3174. if (sd->state.lr_flag != 2)
  3175. sd->special_state.no_walk_delay = 1;
  3176. break;
  3177. default:
  3178. if (current_equip_combo_pos > 0) {
  3179. 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);
  3180. }
  3181. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3182. 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);
  3183. }
  3184. else {
  3185. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3186. }
  3187. break;
  3188. }
  3189. }
  3190. /*==========================================
  3191. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3192. * format: bonus2 bBonusName,type2,val;
  3193. * @param sd
  3194. * @param type Bonus type used by bBonusName
  3195. * @param type2
  3196. * @param val Value that usually for rate or fixed value
  3197. *------------------------------------------*/
  3198. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3199. {
  3200. nullpo_retv(sd);
  3201. switch(type){
  3202. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3203. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3204. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3205. sd->right_weapon.addele[type2]+=val;
  3206. else if(sd->state.lr_flag == 1)
  3207. sd->left_weapon.addele[type2]+=val;
  3208. else if(sd->state.lr_flag == 2)
  3209. sd->arrow_addele[type2]+=val;
  3210. break;
  3211. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3212. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3213. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3214. sd->right_weapon.addrace[type2]+=val;
  3215. else if(sd->state.lr_flag == 1)
  3216. sd->left_weapon.addrace[type2]+=val;
  3217. else if(sd->state.lr_flag == 2)
  3218. sd->arrow_addrace[type2]+=val;
  3219. break;
  3220. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3221. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3222. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3223. sd->right_weapon.addclass[type2]+=val;
  3224. else if(sd->state.lr_flag == 1)
  3225. sd->left_weapon.addclass[type2]+=val;
  3226. else if(sd->state.lr_flag == 2)
  3227. sd->arrow_addclass[type2]+=val;
  3228. break;
  3229. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3230. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3231. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3232. sd->right_weapon.addsize[type2]+=val;
  3233. else if(sd->state.lr_flag == 1)
  3234. sd->left_weapon.addsize[type2]+=val;
  3235. else if(sd->state.lr_flag == 2)
  3236. sd->arrow_addsize[type2]+=val;
  3237. break;
  3238. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3239. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3240. if(sd->state.lr_flag != 2)
  3241. sd->subele_script[type2] += val;
  3242. break;
  3243. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3244. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3245. if(sd->state.lr_flag != 2)
  3246. sd->subrace[type2]+=val;
  3247. break;
  3248. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3249. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3250. if(sd->state.lr_flag != 2)
  3251. sd->subclass[type2]+=val;
  3252. break;
  3253. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3254. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3255. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3256. break;
  3257. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3258. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3259. 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);
  3260. break;
  3261. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3262. if(sd->state.lr_flag == 2)
  3263. break;
  3264. if (sd->reseff.size() == MAX_PC_BONUS) {
  3265. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3266. break;
  3267. }
  3268. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3269. break;
  3270. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3271. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3272. if(sd->state.lr_flag != 2)
  3273. sd->magic_addele_script[type2] += val;
  3274. break;
  3275. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3276. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3277. if(sd->state.lr_flag != 2)
  3278. sd->magic_addrace[type2]+=val;
  3279. break;
  3280. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3281. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3282. if(sd->state.lr_flag != 2)
  3283. sd->magic_addclass[type2]+=val;
  3284. break;
  3285. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3286. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3287. if(sd->state.lr_flag != 2)
  3288. sd->magic_addsize[type2]+=val;
  3289. break;
  3290. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3291. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3292. if(sd->state.lr_flag != 2)
  3293. sd->magic_atk_ele[type2]+=val;
  3294. break;
  3295. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3296. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3297. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3298. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3299. break;
  3300. }
  3301. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3302. }
  3303. break;
  3304. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3305. if(sd->state.lr_flag == 2)
  3306. break;
  3307. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3308. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3309. break;
  3310. }
  3311. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3312. break;
  3313. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3314. if(sd->state.lr_flag == 2)
  3315. break;
  3316. if (sd->add_def.size() == MAX_PC_BONUS) {
  3317. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3318. break;
  3319. }
  3320. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3321. break;
  3322. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3323. if(sd->state.lr_flag == 2)
  3324. break;
  3325. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3326. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3327. break;
  3328. }
  3329. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3330. break;
  3331. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3332. if(!sd->state.lr_flag) {
  3333. sd->right_weapon.hp_drain_rate.rate += type2;
  3334. sd->right_weapon.hp_drain_rate.per += val;
  3335. }
  3336. else if(sd->state.lr_flag == 1) {
  3337. sd->left_weapon.hp_drain_rate.rate += type2;
  3338. sd->left_weapon.hp_drain_rate.per += val;
  3339. }
  3340. break;
  3341. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3342. if(!sd->state.lr_flag) {
  3343. sd->right_weapon.sp_drain_rate.rate += type2;
  3344. sd->right_weapon.sp_drain_rate.per += val;
  3345. }
  3346. else if(sd->state.lr_flag == 1) {
  3347. sd->left_weapon.sp_drain_rate.rate += type2;
  3348. sd->left_weapon.sp_drain_rate.per += val;
  3349. }
  3350. break;
  3351. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3352. if(sd->state.lr_flag != 2) {
  3353. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3354. }
  3355. break;
  3356. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3357. if(sd->state.lr_flag != 2) {
  3358. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3359. }
  3360. break;
  3361. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3362. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3363. sd->bonus.get_zeny_rate = val;
  3364. sd->bonus.get_zeny_num = type2;
  3365. }
  3366. break;
  3367. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3368. if(sd->state.lr_flag != 2) {
  3369. sd->bonus.get_zeny_rate += val;
  3370. sd->bonus.get_zeny_num += type2;
  3371. }
  3372. break;
  3373. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3374. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3375. if(sd->state.lr_flag == 2)
  3376. break;
  3377. sd->weapon_coma_ele[type2] += val;
  3378. sd->special_state.bonus_coma = 1;
  3379. break;
  3380. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3381. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3382. if(sd->state.lr_flag == 2)
  3383. break;
  3384. sd->weapon_coma_race[type2] += val;
  3385. sd->special_state.bonus_coma = 1;
  3386. break;
  3387. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3388. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3389. if(sd->state.lr_flag == 2)
  3390. break;
  3391. sd->weapon_coma_class[type2] += val;
  3392. sd->special_state.bonus_coma = 1;
  3393. break;
  3394. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3395. if(sd->state.lr_flag != 2)
  3396. sd->weapon_atk[type2]+=val;
  3397. break;
  3398. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3399. if(sd->state.lr_flag != 2)
  3400. sd->weapon_damage_rate[type2]+=val;
  3401. break;
  3402. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3403. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3404. if(sd->state.lr_flag != 2)
  3405. sd->critaddrace[type2] += val*10;
  3406. break;
  3407. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3408. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3409. if(sd->state.lr_flag != 2)
  3410. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3411. break;
  3412. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3413. if(sd->state.lr_flag == 2)
  3414. break;
  3415. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3416. 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);
  3417. break;
  3418. }
  3419. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3420. break;
  3421. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3422. if(sd->state.lr_flag == 2)
  3423. break;
  3424. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3425. 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);
  3426. break;
  3427. }
  3428. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3429. break;
  3430. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3431. if(sd->state.lr_flag == 2)
  3432. break;
  3433. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3434. 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);
  3435. break;
  3436. }
  3437. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3438. break;
  3439. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3440. if(sd->state.lr_flag == 2)
  3441. break;
  3442. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3443. 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);
  3444. break;
  3445. }
  3446. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3447. break;
  3448. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3449. if(sd->state.lr_flag != 2) {
  3450. sd->hp_loss.value = type2;
  3451. sd->hp_loss.rate = val;
  3452. }
  3453. break;
  3454. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3455. if(sd->state.lr_flag != 2) {
  3456. sd->hp_regen.value = type2;
  3457. sd->hp_regen.rate = val;
  3458. }
  3459. break;
  3460. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3461. if (sd->state.lr_flag != 2) {
  3462. sd->percent_hp_regen.value = type2;
  3463. sd->percent_hp_regen.rate = val;
  3464. }
  3465. break;
  3466. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3467. if (sd->state.lr_flag != 2) {
  3468. sd->percent_sp_regen.value = type2;
  3469. sd->percent_sp_regen.rate = val;
  3470. }
  3471. break;
  3472. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3473. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3474. if(sd->state.lr_flag != 2)
  3475. sd->right_weapon.addrace2[type2] += val;
  3476. else
  3477. sd->left_weapon.addrace2[type2] += val;
  3478. break;
  3479. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3480. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3481. if(sd->state.lr_flag != 2)
  3482. sd->subsize[type2]+=val;
  3483. break;
  3484. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3485. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3486. if(sd->state.lr_flag != 2)
  3487. sd->magic_subsize[type2]+=val;
  3488. break;
  3489. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3490. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3491. if(sd->state.lr_flag != 2)
  3492. sd->subrace2[type2]+=val;
  3493. break;
  3494. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3495. if(sd->state.lr_flag == 2)
  3496. break;
  3497. if (!itemdb_exists(type2)) {
  3498. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3499. break;
  3500. }
  3501. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3502. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3503. break;
  3504. }
  3505. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3506. break;
  3507. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3508. if (sd->state.lr_flag == 2)
  3509. break;
  3510. if (!type2 || !itemdb_group_exists(type2)) {
  3511. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3512. break;
  3513. }
  3514. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3515. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3516. break;
  3517. }
  3518. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3519. break;
  3520. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3521. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3522. if(sd->state.lr_flag != 2)
  3523. sd->expaddrace[type2]+=val;
  3524. break;
  3525. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3526. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3527. if(sd->state.lr_flag != 2)
  3528. sd->expaddclass[type2]+=val;
  3529. break;
  3530. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3531. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3532. if(sd->state.lr_flag != 2)
  3533. sd->sp_gain_race[type2]+=val;
  3534. break;
  3535. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3536. if (sd->state.lr_flag != 2)
  3537. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3538. break;
  3539. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3540. if (sd->state.lr_flag != 2)
  3541. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3542. break;
  3543. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3544. if(sd->state.lr_flag != 2) {
  3545. sd->sp_loss.value = type2;
  3546. sd->sp_loss.rate = val;
  3547. }
  3548. break;
  3549. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3550. if(sd->state.lr_flag != 2) {
  3551. sd->sp_regen.value = type2;
  3552. sd->sp_regen.rate = val;
  3553. }
  3554. break;
  3555. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3556. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3557. if(!sd->state.lr_flag) {
  3558. sd->right_weapon.hp_drain_race[type2] += val;
  3559. }
  3560. else if(sd->state.lr_flag == 1) {
  3561. sd->left_weapon.hp_drain_race[type2] += val;
  3562. }
  3563. break;
  3564. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3565. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3566. if(!sd->state.lr_flag) {
  3567. sd->right_weapon.sp_drain_race[type2] += val;
  3568. }
  3569. else if(sd->state.lr_flag == 1) {
  3570. sd->left_weapon.sp_drain_race[type2] += val;
  3571. }
  3572. break;
  3573. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3574. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3575. if(!sd->state.lr_flag) {
  3576. sd->right_weapon.hp_drain_class[type2] += val;
  3577. }
  3578. else if(sd->state.lr_flag == 1) {
  3579. sd->left_weapon.hp_drain_class[type2] += val;
  3580. }
  3581. break;
  3582. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3583. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3584. if(!sd->state.lr_flag) {
  3585. sd->right_weapon.sp_drain_class[type2] += val;
  3586. }
  3587. else if(sd->state.lr_flag == 1) {
  3588. sd->left_weapon.sp_drain_class[type2] += val;
  3589. }
  3590. break;
  3591. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3592. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3593. if(sd->state.lr_flag != 2)
  3594. sd->ignore_mdef_by_race[type2] += val;
  3595. break;
  3596. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3597. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3598. if(sd->state.lr_flag != 2)
  3599. sd->ignore_mdef_by_class[type2] += val;
  3600. break;
  3601. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3602. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3603. if(sd->state.lr_flag != 2)
  3604. sd->ignore_def_by_race[type2] += val;
  3605. break;
  3606. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3607. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3608. if (sd->state.lr_flag != 2)
  3609. sd->ignore_def_by_class[type2] += val;
  3610. break;
  3611. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3612. if(sd->state.lr_flag == 2)
  3613. break;
  3614. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3615. 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);
  3616. break;
  3617. }
  3618. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3619. break;
  3620. case SP_SKILL_DELAY:
  3621. if(sd->state.lr_flag == 2)
  3622. break;
  3623. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3624. 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);
  3625. break;
  3626. }
  3627. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3628. break;
  3629. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3630. if(sd->state.lr_flag == 2)
  3631. break;
  3632. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3633. 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);
  3634. break;
  3635. }
  3636. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3637. break;
  3638. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3639. if (sd->state.lr_flag == 2)
  3640. break;
  3641. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3642. 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);
  3643. break;
  3644. }
  3645. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3646. break;
  3647. #ifdef RENEWAL_CAST
  3648. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3649. if(sd->state.lr_flag == 2)
  3650. break;
  3651. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3652. 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);
  3653. break;
  3654. }
  3655. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3656. break;
  3657. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3658. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3659. if(sd->state.lr_flag == 2)
  3660. break;
  3661. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3662. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3663. break;
  3664. }
  3665. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3666. break;
  3667. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3668. if(sd->state.lr_flag == 2)
  3669. break;
  3670. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3671. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3672. break;
  3673. }
  3674. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3675. break;
  3676. #else
  3677. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3678. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3679. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3680. break;
  3681. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3682. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3683. if(sd->state.lr_flag == 2)
  3684. break;
  3685. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3686. 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);
  3687. break;
  3688. }
  3689. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3690. break;
  3691. #endif
  3692. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3693. if(sd->state.lr_flag == 2)
  3694. break;
  3695. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3696. 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);
  3697. break;
  3698. }
  3699. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3700. break;
  3701. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3702. if (sd->subskill.size() == MAX_PC_BONUS) {
  3703. 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);
  3704. break;
  3705. }
  3706. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3707. break;
  3708. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3709. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3710. sd->subdefele[type2] += val;
  3711. break;
  3712. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3713. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3714. sd->coma_class[type2] += val;
  3715. sd->special_state.bonus_coma = 1;
  3716. break;
  3717. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3718. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3719. sd->coma_race[type2] += val;
  3720. sd->special_state.bonus_coma = 1;
  3721. break;
  3722. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3723. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3724. if(sd->state.lr_flag != 2)
  3725. sd->magic_addrace2[type2] += val;
  3726. break;
  3727. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3728. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3729. if (sd->state.lr_flag != 2)
  3730. sd->ignore_mdef_by_race2[type2] += val;
  3731. break;
  3732. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3733. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3734. if (sd->state.lr_flag != 2)
  3735. sd->dropaddrace[type2] += val;
  3736. break;
  3737. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3738. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3739. if (sd->state.lr_flag != 2)
  3740. sd->dropaddclass[type2] += val;
  3741. break;
  3742. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  3743. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  3744. sd->magic_subdefele[type2] += val;
  3745. break;
  3746. default:
  3747. if (current_equip_combo_pos > 0) {
  3748. 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);
  3749. }
  3750. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3751. 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);
  3752. }
  3753. else {
  3754. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3755. }
  3756. break;
  3757. }
  3758. }
  3759. /**
  3760. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3761. * @param sd
  3762. * @param type Bonus type used by bBonusName
  3763. * @param type2
  3764. * @param type3
  3765. * @param val Value that usually for rate or fixed value
  3766. */
  3767. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3768. {
  3769. nullpo_retv(sd);
  3770. switch(type){
  3771. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3772. if(sd->state.lr_flag != 2)
  3773. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3774. break;
  3775. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3776. if(sd->state.lr_flag != 2)
  3777. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3778. break;
  3779. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3780. if(sd->state.lr_flag != 2)
  3781. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3782. break;
  3783. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3784. if(sd->state.lr_flag != 2)
  3785. {
  3786. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3787. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3788. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3789. }
  3790. break;
  3791. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3792. if(sd->state.lr_flag != 2)
  3793. {
  3794. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3795. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3796. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3797. }
  3798. break;
  3799. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3800. if (sd->state.lr_flag != 2)
  3801. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3802. break;
  3803. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3804. if (sd->state.lr_flag != 2)
  3805. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3806. break;
  3807. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3808. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3809. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3810. break;
  3811. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3812. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3813. if(sd->state.lr_flag != 2)
  3814. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3815. break;
  3816. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3817. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3818. if( sd->state.lr_flag != 2 )
  3819. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3820. break;
  3821. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3822. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3823. if (sd->state.lr_flag != 2)
  3824. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3825. break;
  3826. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3827. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3828. if (sd->state.lr_flag != 2)
  3829. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3830. break;
  3831. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  3832. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  3833. if (sd->state.lr_flag != 2)
  3834. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  3835. break;
  3836. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3837. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3838. if(sd->state.lr_flag != 2) {
  3839. sd->sp_vanish_race[type2].rate += type3;
  3840. sd->sp_vanish_race[type2].per += val;
  3841. }
  3842. break;
  3843. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3844. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3845. if(sd->state.lr_flag != 2) {
  3846. sd->hp_vanish_race[type2].rate += type3;
  3847. sd->hp_vanish_race[type2].per += val;
  3848. }
  3849. break;
  3850. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3851. if(sd->state.lr_flag != 2) {
  3852. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3853. }
  3854. break;
  3855. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3856. if(sd->state.lr_flag != 2) {
  3857. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3858. }
  3859. break;
  3860. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3861. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3862. if (sd->state.lr_flag == 2)
  3863. break;
  3864. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3865. //if (type3 > sd->norecover_state_race[type2].rate) {
  3866. // sd->norecover_state_race[type2].rate = type3;
  3867. // sd->norecover_state_race[type2].tick = val;
  3868. // break;
  3869. //}
  3870. sd->norecover_state_race[type2].rate = type3;
  3871. sd->norecover_state_race[type2].tick = val;
  3872. break;
  3873. default:
  3874. if (current_equip_combo_pos > 0) {
  3875. 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);
  3876. }
  3877. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3878. 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);
  3879. }
  3880. else {
  3881. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3882. }
  3883. break;
  3884. }
  3885. }
  3886. /**
  3887. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3888. * @param sd
  3889. * @param type Bonus type used by bBonusName
  3890. * @param type2
  3891. * @param type3
  3892. * @param type4
  3893. * @param val Value that usually for rate or fixed value
  3894. */
  3895. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3896. {
  3897. nullpo_retv(sd);
  3898. switch(type){
  3899. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3900. if(sd->state.lr_flag != 2)
  3901. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3902. break;
  3903. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3904. if(sd->state.lr_flag != 2)
  3905. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3906. break;
  3907. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3908. if(sd->state.lr_flag != 2)
  3909. {
  3910. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3911. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3912. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3913. }
  3914. break;
  3915. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3916. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3917. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3918. break;
  3919. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3920. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3921. if (sd->state.lr_flag != 2)
  3922. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3923. break;
  3924. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3925. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3926. if( sd->state.lr_flag != 2 )
  3927. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3928. break;
  3929. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3930. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3931. if(sd->state.lr_flag == 2)
  3932. break;
  3933. sd->def_set_race[type2].rate = type3;
  3934. sd->def_set_race[type2].tick = type4;
  3935. sd->def_set_race[type2].value = val;
  3936. break;
  3937. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3938. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3939. if(sd->state.lr_flag == 2)
  3940. break;
  3941. sd->mdef_set_race[type2].rate = type3;
  3942. sd->mdef_set_race[type2].tick = type4;
  3943. sd->mdef_set_race[type2].value = val;
  3944. break;
  3945. default:
  3946. if (current_equip_combo_pos > 0) {
  3947. 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);
  3948. }
  3949. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3950. 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);
  3951. }
  3952. else {
  3953. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3954. }
  3955. break;
  3956. }
  3957. }
  3958. /**
  3959. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3960. * @param sd
  3961. * @param type Bonus type used by bBonusName
  3962. * @param type2
  3963. * @param type3
  3964. * @param type4
  3965. * @param val Value that usually for rate or fixed value
  3966. */
  3967. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3968. {
  3969. nullpo_retv(sd);
  3970. switch(type){
  3971. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3972. if(sd->state.lr_flag != 2)
  3973. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3974. break;
  3975. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3976. if(sd->state.lr_flag != 2)
  3977. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3978. break;
  3979. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3980. if(sd->state.lr_flag != 2)
  3981. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3982. break;
  3983. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3984. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3985. if( sd->state.lr_flag != 2 )
  3986. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3987. break;
  3988. default:
  3989. if (current_equip_combo_pos > 0) {
  3990. 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);
  3991. }
  3992. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3993. 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);
  3994. }
  3995. else {
  3996. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3997. }
  3998. break;
  3999. }
  4000. }
  4001. /*==========================================
  4002. * Grants a player a given skill. Flag values are:
  4003. * 0 - Grant permanent skill to be bound to skill tree
  4004. * 1 - Grant an item skill (temporary)
  4005. * 2 - Like 1, except the level granted can stack with previously learned level.
  4006. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4007. *------------------------------------------*/
  4008. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4009. uint16 idx = 0;
  4010. nullpo_ret(sd);
  4011. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4012. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4013. return false;
  4014. }
  4015. if (level > MAX_SKILL_LEVEL) {
  4016. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4017. return false;
  4018. }
  4019. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4020. 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);
  4021. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4022. }
  4023. switch (type) {
  4024. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4025. sd->status.skill[idx].id = skill_id;
  4026. sd->status.skill[idx].lv = level;
  4027. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4028. if (level == 0) { //Remove skill.
  4029. sd->status.skill[idx].id = 0;
  4030. clif_deleteskill(sd,skill_id);
  4031. } else
  4032. clif_addskill(sd,skill_id);
  4033. 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.
  4034. status_calc_pc(sd, SCO_NONE);
  4035. break;
  4036. case ADDSKILL_TEMP: //Item bonus skill.
  4037. if (sd->status.skill[idx].id != 0) {
  4038. if (sd->status.skill[idx].lv >= level)
  4039. return true;
  4040. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4041. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4042. } else {
  4043. sd->status.skill[idx].id = skill_id;
  4044. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4045. }
  4046. sd->status.skill[idx].lv = level;
  4047. break;
  4048. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4049. if (sd->status.skill[idx].id != 0) {
  4050. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4051. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4052. } else {
  4053. sd->status.skill[idx].id = skill_id;
  4054. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4055. }
  4056. sd->status.skill[idx].lv += level;
  4057. break;
  4058. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4059. sd->status.skill[idx].id = skill_id;
  4060. sd->status.skill[idx].lv = level;
  4061. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4062. if (level == 0) { //Remove skill.
  4063. sd->status.skill[idx].id = 0;
  4064. clif_deleteskill(sd,skill_id);
  4065. } else
  4066. clif_addskill(sd,skill_id);
  4067. 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.
  4068. status_calc_pc(sd, SCO_NONE);
  4069. break;
  4070. default:
  4071. return false;
  4072. }
  4073. return true;
  4074. }
  4075. /*==========================================
  4076. * Append a card to an item ?
  4077. *------------------------------------------*/
  4078. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4079. {
  4080. nullpo_ret(sd);
  4081. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4082. return 0;
  4083. }
  4084. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4085. return 0;
  4086. }
  4087. int i;
  4088. t_itemid nameid;
  4089. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4090. struct item_data* item_card = sd->inventory_data[idx_card];
  4091. if(item_eq == nullptr)
  4092. return 0; //Invalid item index.
  4093. if(item_card == nullptr)
  4094. return 0; //Invalid card index.
  4095. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4096. return 0; // target item missing
  4097. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4098. return 0; // target card missing
  4099. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4100. return 0; // only weapons and armor are allowed
  4101. if( item_card->type != IT_CARD )
  4102. return 0; // must be a card
  4103. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4104. return 0; // target must be identified
  4105. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4106. return 0; // card slots reserved for other purposes
  4107. if( (item_eq->equip & item_card->equip) == 0 )
  4108. return 0; // card cannot be compounded on this item type
  4109. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4110. return 0; // attempted to place shield card on left-hand weapon.
  4111. 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)) )
  4112. return 0; // specific accessory-card can only be inserted to specific accessory.
  4113. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4114. return 0; // item must be unequipped
  4115. ARR_FIND( 0, item_eq->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4116. if( i == item_eq->slot )
  4117. return 0; // no free slots
  4118. // remember the card id to insert
  4119. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4120. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4121. {// failed
  4122. clif_insert_card(sd,idx_equip,idx_card,1);
  4123. }
  4124. else
  4125. {// success
  4126. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4127. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4128. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4129. clif_insert_card(sd,idx_equip,idx_card,0);
  4130. }
  4131. return 0;
  4132. }
  4133. /**
  4134. * Returns the count of unidentified items with the option to identify too.
  4135. * @param sd: Player data
  4136. * @param identify_item: Whether or not to identify any unidentified items
  4137. * @return Unidentified items count
  4138. */
  4139. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4140. {
  4141. int unidentified_count = 0;
  4142. for (int i = 0; i < MAX_INVENTORY; i++) {
  4143. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4144. if (identify_item == true) {
  4145. sd->inventory.u.items_inventory[i].identify = 1;
  4146. clif_item_identified(sd,i,0);
  4147. }
  4148. unidentified_count++;
  4149. }
  4150. }
  4151. return unidentified_count;
  4152. }
  4153. //
  4154. // Items
  4155. //
  4156. /*==========================================
  4157. * Update buying value by skills
  4158. *------------------------------------------*/
  4159. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4160. {
  4161. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4162. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4163. rate1 = 5+skill*2-((skill==10)? 1:0);
  4164. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4165. rate2 = 5+skill*4;
  4166. if(rate1 < rate2) rate1 = rate2;
  4167. if(rate1)
  4168. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4169. if(val < battle_config.min_shop_buy)
  4170. val = battle_config.min_shop_buy;
  4171. return val;
  4172. }
  4173. /*==========================================
  4174. * Update selling value by skills
  4175. *------------------------------------------*/
  4176. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4177. {
  4178. int skill,val = orig_value,rate = 0;
  4179. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4180. rate = 5+skill*2-((skill==10)? 1:0);
  4181. if(rate)
  4182. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4183. if (val < battle_config.min_shop_sell)
  4184. val = battle_config.min_shop_sell;
  4185. return val;
  4186. }
  4187. /*==========================================
  4188. * Checking if we have enough place on inventory for new item
  4189. * Make sure to take 30k as limit (for client I guess)
  4190. * @param sd
  4191. * @param nameid
  4192. * @param amount
  4193. * @return e_chkitem_result
  4194. *------------------------------------------*/
  4195. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4196. {
  4197. int i;
  4198. struct item_data* data;
  4199. nullpo_ret(sd);
  4200. if(amount > MAX_AMOUNT)
  4201. return CHKADDITEM_OVERAMOUNT;
  4202. data = itemdb_search(nameid);
  4203. if(!itemdb_isstackable2(data))
  4204. return CHKADDITEM_NEW;
  4205. if( data->stack.inventory && amount > data->stack.amount )
  4206. return CHKADDITEM_OVERAMOUNT;
  4207. if (data->flag.guid)
  4208. return CHKADDITEM_NEW;
  4209. for(i=0;i<MAX_INVENTORY;i++){
  4210. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4211. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4212. 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 ) )
  4213. return CHKADDITEM_OVERAMOUNT;
  4214. return CHKADDITEM_EXIST;
  4215. }
  4216. }
  4217. return CHKADDITEM_NEW;
  4218. }
  4219. /*==========================================
  4220. * Return number of available place in inventory
  4221. * Each non stackable item will reduce place by 1
  4222. * @param sd
  4223. * @return Number of empty slots
  4224. *------------------------------------------*/
  4225. uint8 pc_inventoryblank(struct map_session_data *sd)
  4226. {
  4227. uint8 i, b;
  4228. nullpo_ret(sd);
  4229. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4230. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4231. b++;
  4232. }
  4233. return b;
  4234. }
  4235. /**
  4236. * Attempts to remove zeny from player
  4237. * @param sd: Player
  4238. * @param zeny: Zeny removed
  4239. * @param type: Log type
  4240. * @param tsd: (optional) From who to log (if null take sd)
  4241. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4242. */
  4243. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4244. {
  4245. nullpo_retr(2,sd);
  4246. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4247. if( zeny < 0 )
  4248. {
  4249. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4250. return 1;
  4251. }
  4252. if( sd->status.zeny < zeny )
  4253. return 1; //Not enough.
  4254. sd->status.zeny -= zeny;
  4255. clif_updatestatus(sd,SP_ZENY);
  4256. if(!tsd) tsd = sd;
  4257. log_zeny(sd, type, tsd, -zeny);
  4258. if( zeny > 0 && sd->state.showzeny ) {
  4259. char output[255];
  4260. sprintf(output, "Removed %dz.", zeny);
  4261. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4262. }
  4263. return 0;
  4264. }
  4265. /**
  4266. * Attempts to give zeny to player
  4267. * @param sd: Player
  4268. * @param type: Log type
  4269. * @param tsd: (optional) From who to log (if null take sd)
  4270. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4271. */
  4272. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4273. {
  4274. nullpo_retr(-1,sd);
  4275. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4276. if( zeny < 0 )
  4277. {
  4278. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4279. return 1;
  4280. }
  4281. if( zeny > MAX_ZENY - sd->status.zeny )
  4282. zeny = MAX_ZENY - sd->status.zeny;
  4283. sd->status.zeny += zeny;
  4284. clif_updatestatus(sd,SP_ZENY);
  4285. if(!tsd) tsd = sd;
  4286. log_zeny(sd, type, tsd, zeny);
  4287. if( zeny > 0 && sd->state.showzeny ) {
  4288. char output[255];
  4289. sprintf(output, "Gained %dz.", zeny);
  4290. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4291. }
  4292. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4293. return 0;
  4294. }
  4295. /**
  4296. * Attempts to remove Cash Points from player
  4297. * @param sd: Player
  4298. * @param price: Total points (cash + kafra) the player has to pay
  4299. * @param points: Kafra points the player has to pay
  4300. * @param type: Log type
  4301. * @return -1: Not enough points, otherwise success (cash+points)
  4302. */
  4303. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4304. {
  4305. int cash;
  4306. nullpo_retr(-1,sd);
  4307. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4308. cash = price-points;
  4309. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4310. {
  4311. 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);
  4312. return -1;
  4313. }
  4314. if( cash ){
  4315. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4316. sd->cashPoints -= cash;
  4317. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4318. }
  4319. if( points ){
  4320. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4321. sd->kafraPoints -= points;
  4322. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4323. }
  4324. if( battle_config.cashshop_show_points )
  4325. {
  4326. char output[CHAT_SIZE_MAX];
  4327. 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.
  4328. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4329. }
  4330. return cash+points;
  4331. }
  4332. /**
  4333. * Attempts to give Cash Points to player
  4334. * @param sd: Player
  4335. * @param cash: Cash points the player gets
  4336. * @param points: Kafra points the player gets
  4337. * @param type: Log type
  4338. * @return -1: Error, otherwise success (cash or points)
  4339. */
  4340. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4341. {
  4342. char output[CHAT_SIZE_MAX];
  4343. nullpo_retr(-1,sd);
  4344. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4345. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4346. if( cash > 0 )
  4347. {
  4348. if( cash > MAX_ZENY-sd->cashPoints )
  4349. {
  4350. 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);
  4351. cash = MAX_ZENY-sd->cashPoints;
  4352. }
  4353. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4354. sd->cashPoints += cash;
  4355. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4356. if( battle_config.cashshop_show_points )
  4357. {
  4358. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4359. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4360. }
  4361. return cash;
  4362. }
  4363. if( points > 0 )
  4364. {
  4365. if( points > MAX_ZENY-sd->kafraPoints )
  4366. {
  4367. 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);
  4368. points = MAX_ZENY-sd->kafraPoints;
  4369. }
  4370. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4371. sd->kafraPoints += points;
  4372. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4373. if( battle_config.cashshop_show_points )
  4374. {
  4375. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4376. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4377. }
  4378. return points;
  4379. }
  4380. return -1; //shouldn't happen but just in case
  4381. }
  4382. /**
  4383. * Searching a specified itemid in inventory and return his stored index
  4384. * @param sd Player
  4385. * @param nameid Find this Item!
  4386. * @return Stored index in inventory, or -1 if not found.
  4387. **/
  4388. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4389. short i;
  4390. nullpo_retr(-1, sd);
  4391. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4392. return ( i < MAX_INVENTORY ) ? i : -1;
  4393. }
  4394. /** Attempt to add a new item to player inventory
  4395. * @param sd
  4396. * @param item_data
  4397. * @param amount
  4398. * @param log_type
  4399. * @return
  4400. * 0 = success
  4401. * 1 = invalid itemid not found or negative amount
  4402. * 2 = overweight
  4403. * 3 = ?
  4404. * 4 = no free place found
  4405. * 5 = max amount reached
  4406. * 6 = ?
  4407. * 7 = stack limitation
  4408. */
  4409. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4410. struct item_data *id;
  4411. int16 i;
  4412. unsigned int w;
  4413. nullpo_retr(ADDITEM_INVALID, sd);
  4414. nullpo_retr(ADDITEM_INVALID, item);
  4415. if( item->nameid == 0 || amount <= 0 )
  4416. return ADDITEM_INVALID;
  4417. if( amount > MAX_AMOUNT )
  4418. return ADDITEM_OVERAMOUNT;
  4419. id = itemdb_search(item->nameid);
  4420. if( id->stack.inventory && amount > id->stack.amount )
  4421. {// item stack limitation
  4422. return ADDITEM_STACKLIMIT;
  4423. }
  4424. w = id->weight*amount;
  4425. if(sd->weight + w > sd->max_weight)
  4426. return ADDITEM_OVERWEIGHT;
  4427. i = MAX_INVENTORY;
  4428. if (id->flag.guid && !item->unique_id)
  4429. item->unique_id = pc_generate_unique_id(sd);
  4430. // Stackable | Non Rental
  4431. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4432. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4433. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4434. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4435. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4436. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4437. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4438. 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 ) )
  4439. return ADDITEM_OVERAMOUNT;
  4440. sd->inventory.u.items_inventory[i].amount += amount;
  4441. clif_additem(sd,i,amount,0);
  4442. break;
  4443. }
  4444. }
  4445. }
  4446. if (i >= MAX_INVENTORY) {
  4447. i = pc_search_inventory(sd,0);
  4448. if( i < 0 )
  4449. return ADDITEM_OVERITEM;
  4450. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4451. // clear equip and favorite fields first, just in case
  4452. if( item->equip )
  4453. sd->inventory.u.items_inventory[i].equip = 0;
  4454. if( item->favorite )
  4455. sd->inventory.u.items_inventory[i].favorite = 0;
  4456. if( item->equipSwitch )
  4457. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4458. sd->inventory.u.items_inventory[i].amount = amount;
  4459. sd->inventory_data[i] = id;
  4460. sd->last_addeditem_index = i;
  4461. if (!itemdb_isstackable2(id) || id->flag.guid)
  4462. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4463. clif_additem(sd,i,amount,0);
  4464. }
  4465. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4466. sd->weight += w;
  4467. clif_updatestatus(sd,SP_WEIGHT);
  4468. //Auto-equip
  4469. if(id->flag.autoequip)
  4470. pc_equipitem(sd, i, id->equip);
  4471. /* rental item check */
  4472. if( item->expire_time ) {
  4473. if( time(NULL) > item->expire_time ) {
  4474. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4475. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4476. } else {
  4477. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4478. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4479. pc_inventory_rental_add(sd, seconds);
  4480. }
  4481. }
  4482. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4483. pc_show_questinfo(sd);
  4484. return ADDITEM_SUCCESS;
  4485. }
  4486. /*==========================================
  4487. * Remove an item at index n from inventory by amount.
  4488. * @param sd
  4489. * @param n Item index in inventory
  4490. * @param amount
  4491. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4492. * @param reason Delete reason
  4493. * @param log_type e_log_pick_type
  4494. * @return 1 - invalid itemid or negative amount; 0 - Success
  4495. *------------------------------------------*/
  4496. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4497. {
  4498. nullpo_retr(1, sd);
  4499. 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)
  4500. return 1;
  4501. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4502. sd->inventory.u.items_inventory[n].amount -= amount;
  4503. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4504. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4505. if(sd->inventory.u.items_inventory[n].equip)
  4506. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4507. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4508. sd->inventory_data[n] = NULL;
  4509. }
  4510. if(!(type&1))
  4511. clif_delitem(sd,n,amount,reason);
  4512. if(!(type&2))
  4513. clif_updatestatus(sd,SP_WEIGHT);
  4514. pc_show_questinfo(sd);
  4515. return 0;
  4516. }
  4517. /*==========================================
  4518. * Attempt to drop an item.
  4519. * @param sd
  4520. * @param n Item index in inventory
  4521. * @param amount Amount of item
  4522. * @return False = fail; True = success
  4523. *------------------------------------------*/
  4524. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4525. {
  4526. nullpo_retr(1, sd);
  4527. if(n < 0 || n >= MAX_INVENTORY)
  4528. return false;
  4529. if(amount <= 0)
  4530. return false;
  4531. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  4532. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4533. sd->inventory.u.items_inventory[n].amount < amount ||
  4534. sd->state.trading || sd->state.vending ||
  4535. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4536. )
  4537. return false;
  4538. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4539. return false;
  4540. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4541. {
  4542. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4543. return false; //Can't drop items in nodrop mapflag maps.
  4544. }
  4545. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4546. {
  4547. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4548. return false;
  4549. }
  4550. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4551. return false;
  4552. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4553. clif_dropitem(sd, n, amount);
  4554. return true;
  4555. }
  4556. /*==========================================
  4557. * Attempt to pick up an item.
  4558. * @param sd
  4559. * @param fitem Item that will be picked
  4560. * @return False = fail; True = success
  4561. *------------------------------------------*/
  4562. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4563. {
  4564. int flag = 0;
  4565. t_tick tick = gettick();
  4566. struct party_data *p = NULL;
  4567. nullpo_ret(sd);
  4568. nullpo_ret(fitem);
  4569. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4570. return false; // Distance is too far
  4571. if (sd->sc.cant.pickup)
  4572. return false;
  4573. if (sd->status.party_id)
  4574. p = party_search(sd->status.party_id);
  4575. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4576. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4577. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4578. if (!(p && p->party.item&1 &&
  4579. first_sd && first_sd->status.party_id == sd->status.party_id
  4580. ))
  4581. return false;
  4582. }
  4583. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4584. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4585. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4586. if (!(p && p->party.item&1 &&
  4587. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4588. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4589. ))
  4590. return false;
  4591. }
  4592. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4593. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4594. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4595. if(!(p && p->party.item&1 &&
  4596. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4597. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4598. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4599. ))
  4600. return false;
  4601. }
  4602. }
  4603. }
  4604. }
  4605. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4606. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4607. clif_additem(sd,0,0,flag);
  4608. return true;
  4609. }
  4610. //Display pickup animation.
  4611. pc_stop_attack(sd);
  4612. clif_takeitem(&sd->bl,&fitem->bl);
  4613. if (fitem->mob_id &&
  4614. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4615. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4616. )
  4617. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4618. map_clearflooritem(&fitem->bl);
  4619. return true;
  4620. }
  4621. /*==========================================
  4622. * Check if item is usable.
  4623. * Return:
  4624. * false = no
  4625. * true = yes
  4626. *------------------------------------------*/
  4627. bool pc_isUseitem(struct map_session_data *sd,int n)
  4628. {
  4629. struct item_data *item;
  4630. t_itemid nameid;
  4631. nullpo_ret(sd);
  4632. item = sd->inventory_data[n];
  4633. nameid = sd->inventory.u.items_inventory[n].nameid;
  4634. if( item == NULL )
  4635. return false;
  4636. //Not consumable item
  4637. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4638. return false;
  4639. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4640. return true;
  4641. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4642. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4643. return false;
  4644. //Prevent mass item usage. [Skotlex]
  4645. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4646. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4647. )
  4648. return false;
  4649. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4650. clif_msg(sd,ITEM_NOUSE_SITTING);
  4651. return false; // You cannot use this item while sitting.
  4652. }
  4653. if (sd->state.storage_flag && item->type != IT_CASH) {
  4654. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4655. return false; // You cannot use this item while storage is open.
  4656. }
  4657. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4658. return false;
  4659. switch( nameid ) {
  4660. case ITEMID_WING_OF_FLY:
  4661. case ITEMID_GIANT_FLY_WING:
  4662. case ITEMID_N_FLY_WING:
  4663. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4664. clif_skill_teleportmessage(sd,0);
  4665. return false;
  4666. }
  4667. if (nameid == ITEMID_GIANT_FLY_WING) {
  4668. struct party_data *pd = party_search(sd->status.party_id);
  4669. if (pd) {
  4670. int i;
  4671. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4672. if (i == MAX_PARTY) { // User is not party leader
  4673. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4674. break;
  4675. }
  4676. 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));
  4677. if (i == MAX_PARTY) { // No party members found on same map
  4678. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4679. break;
  4680. }
  4681. } else {
  4682. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4683. break;
  4684. }
  4685. }
  4686. // Fall through
  4687. case ITEMID_WING_OF_BUTTERFLY:
  4688. case ITEMID_N_BUTTERFLY_WING:
  4689. case ITEMID_DUN_TELE_SCROLL1:
  4690. case ITEMID_DUN_TELE_SCROLL2:
  4691. case ITEMID_DUN_TELE_SCROLL3:
  4692. case ITEMID_WOB_RUNE:
  4693. case ITEMID_WOB_SCHWALTZ:
  4694. case ITEMID_WOB_RACHEL:
  4695. case ITEMID_WOB_LOCAL:
  4696. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4697. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4698. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4699. return false;
  4700. }
  4701. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4702. return false;
  4703. break;
  4704. case ITEMID_MERCENARY_RED_POTION:
  4705. case ITEMID_MERCENARY_BLUE_POTION:
  4706. case ITEMID_M_CENTER_POTION:
  4707. case ITEMID_M_AWAKENING_POTION:
  4708. case ITEMID_M_BERSERK_POTION:
  4709. if( sd->md == NULL || sd->md->db == NULL )
  4710. return false;
  4711. if( sd->md->sc.data[SC_BERSERK] )
  4712. return false;
  4713. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4714. return false;
  4715. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4716. return false;
  4717. break;
  4718. case ITEMID_NEURALIZER:
  4719. if( !mapdata->flag[MF_RESET] )
  4720. return false;
  4721. break;
  4722. }
  4723. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4724. return false; // Mercenary Scrolls
  4725. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4726. if( pc_is90overweight(sd) ) {
  4727. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4728. return false;
  4729. }
  4730. if( !pc_inventoryblank(sd) ) {
  4731. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4732. return false;
  4733. }
  4734. }
  4735. //Gender check
  4736. if(item->sex != 2 && sd->status.sex != item->sex)
  4737. return false;
  4738. //Required level check
  4739. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4740. return false;
  4741. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4742. return false;
  4743. //Not equipable by class. [Skotlex]
  4744. if (!(
  4745. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4746. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4747. ))
  4748. return false;
  4749. if (sd->sc.count && (
  4750. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4751. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4752. sd->sc.data[SC_TRICKDEAD] ||
  4753. sd->sc.data[SC_HIDING] ||
  4754. sd->sc.data[SC__SHADOWFORM] ||
  4755. sd->sc.data[SC__INVISIBILITY] ||
  4756. sd->sc.data[SC__MANHOLE] ||
  4757. sd->sc.data[SC_DEEPSLEEP] ||
  4758. sd->sc.data[SC_CRYSTALIZE] ||
  4759. sd->sc.data[SC_KAGEHUMI] ||
  4760. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4761. sd->sc.data[SC_KINGS_GRACE] ||
  4762. sd->sc.data[SC_SUHIDE]))
  4763. return false;
  4764. if (!pc_isItemClass(sd,item))
  4765. return false;
  4766. //Dead Branch items
  4767. if( item->flag.dead_branch )
  4768. log_branch(sd);
  4769. return true;
  4770. }
  4771. /*==========================================
  4772. * Last checks to use an item.
  4773. * Return:
  4774. * 0 = fail
  4775. * 1 = success
  4776. *------------------------------------------*/
  4777. int pc_useitem(struct map_session_data *sd,int n)
  4778. {
  4779. t_tick tick = gettick();
  4780. int amount;
  4781. t_itemid nameid;
  4782. struct script_code *script;
  4783. struct item item;
  4784. struct item_data *id;
  4785. nullpo_ret(sd);
  4786. if (sd->state.mail_writing)
  4787. return 0;
  4788. if (sd->npc_id) {
  4789. if (sd->progressbar.npc_id) {
  4790. clif_progressbar_abort(sd);
  4791. return 0; // First item use attempt cancels the progress bar
  4792. }
  4793. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4794. #ifdef RENEWAL
  4795. clif_msg( sd, WORK_IN_PROGRESS );
  4796. #endif
  4797. return 0;
  4798. }
  4799. }
  4800. item = sd->inventory.u.items_inventory[n];
  4801. id = sd->inventory_data[n];
  4802. if (item.nameid == 0 || item.amount <= 0)
  4803. return 0;
  4804. if( !pc_isUseitem(sd,n) )
  4805. return 0;
  4806. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4807. nameid = id->nameid;
  4808. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4809. return 0;
  4810. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4811. if( id->flag.delay_consume ) {
  4812. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4813. return 0;
  4814. else if( pc_issit(sd) )
  4815. return 0;
  4816. }
  4817. //Since most delay-consume items involve using a "skill-type" target cursor,
  4818. //perform a skill-use check before going through. [Skotlex]
  4819. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4820. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4821. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4822. return 0;
  4823. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4824. return 0;
  4825. /* on restricted maps the item is consumed but the effect is not used */
  4826. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4827. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4828. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4829. clif_useitemack(sd,n,item.amount-1,true);
  4830. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4831. }
  4832. return 0;/* regardless, effect is not run */
  4833. }
  4834. sd->itemid = item.nameid;
  4835. sd->itemindex = n;
  4836. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4837. sd->catch_target_class = PET_CATCH_FAIL;
  4838. amount = item.amount;
  4839. script = id->script;
  4840. //Check if the item is to be consumed immediately [Skotlex]
  4841. if (id->flag.delay_consume)
  4842. clif_useitemack(sd, n, amount, true);
  4843. else
  4844. {
  4845. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4846. {
  4847. clif_useitemack(sd, n, amount - 1, true);
  4848. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4849. }
  4850. else
  4851. clif_useitemack(sd, n, 0, false);
  4852. }
  4853. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4854. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4855. //Update item use time.
  4856. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4857. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4858. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4859. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4860. potion_flag = 0;
  4861. return 1;
  4862. }
  4863. /**
  4864. * Add item on cart for given index.
  4865. * @param sd
  4866. * @param item
  4867. * @param amount
  4868. * @param log_type
  4869. * @return See pc.hpp::e_additem_result
  4870. */
  4871. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4872. {
  4873. struct item_data *data;
  4874. int i,w;
  4875. nullpo_retr(ADDITEM_INVALID, sd);
  4876. nullpo_retr(ADDITEM_INVALID, item);
  4877. if(item->nameid == 0 || amount <= 0)
  4878. return ADDITEM_INVALID;
  4879. if (itemdb_ishatched_egg(item))
  4880. return ADDITEM_INVALID;
  4881. data = itemdb_search(item->nameid);
  4882. if( data->stack.cart && amount > data->stack.amount )
  4883. {// item stack limitation
  4884. return ADDITEM_STACKLIMIT;
  4885. }
  4886. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4887. { // Check item trade restrictions [Skotlex]
  4888. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4889. return ADDITEM_INVALID;
  4890. }
  4891. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4892. return ADDITEM_OVERWEIGHT;
  4893. i = MAX_CART;
  4894. if( itemdb_isstackable2(data) && !item->expire_time )
  4895. {
  4896. for (i = 0; i < MAX_CART; i++) {
  4897. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4898. && sd->cart.u.items_cart[i].bound == item->bound
  4899. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4900. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4901. )
  4902. break;
  4903. }
  4904. }
  4905. if( i < MAX_CART )
  4906. {// item already in cart, stack it
  4907. 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 ) )
  4908. return ADDITEM_OVERAMOUNT; // no slot
  4909. sd->cart.u.items_cart[i].amount += amount;
  4910. clif_cart_additem(sd,i,amount);
  4911. }
  4912. else
  4913. {// item not stackable or not present, add it
  4914. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4915. if( i == MAX_CART )
  4916. return ADDITEM_OVERAMOUNT; // no slot
  4917. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4918. sd->cart.u.items_cart[i].id = 0;
  4919. sd->cart.u.items_cart[i].amount = amount;
  4920. sd->cart_num++;
  4921. clif_cart_additem(sd,i,amount);
  4922. }
  4923. sd->cart.u.items_cart[i].favorite = 0; // clear
  4924. sd->cart.u.items_cart[i].equipSwitch = 0;
  4925. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4926. sd->cart_weight += w;
  4927. clif_updatestatus(sd,SP_CARTINFO);
  4928. return ADDITEM_SUCCESS;
  4929. }
  4930. /*==========================================
  4931. * Delete item on cart for given index.
  4932. *------------------------------------------*/
  4933. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4934. {
  4935. nullpo_retv(sd);
  4936. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4937. sd->cart.u.items_cart[n].amount < amount)
  4938. return;
  4939. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4940. sd->cart.u.items_cart[n].amount -= amount;
  4941. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4942. if(sd->cart.u.items_cart[n].amount <= 0) {
  4943. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4944. sd->cart_num--;
  4945. }
  4946. if(!type) {
  4947. clif_cart_delitem(sd,n,amount);
  4948. clif_updatestatus(sd,SP_CARTINFO);
  4949. }
  4950. }
  4951. /*==========================================
  4952. * Transfer item from inventory to cart.
  4953. *------------------------------------------*/
  4954. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4955. {
  4956. nullpo_retv(sd);
  4957. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4958. return;
  4959. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4960. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4961. return;
  4962. if( item_data->equipSwitch ){
  4963. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4964. return;
  4965. }
  4966. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4967. if (flag == ADDITEM_SUCCESS)
  4968. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4969. else {
  4970. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4971. clif_additem(sd, idx, amount, 0);
  4972. clif_delitem(sd, idx, amount, 0);
  4973. }
  4974. }
  4975. /*==========================================
  4976. * Get number of item in cart.
  4977. * Return:
  4978. -1 = itemid not found or no amount found
  4979. x = remaining itemid on cart after get
  4980. *------------------------------------------*/
  4981. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4982. {
  4983. struct item* item_data;
  4984. nullpo_retr(-1, sd);
  4985. item_data = &sd->cart.u.items_cart[idx];
  4986. if( item_data->nameid == 0 || item_data->amount == 0 )
  4987. return -1;
  4988. return item_data->amount - amount;
  4989. }
  4990. /*==========================================
  4991. * Retrieve an item at index idx from cart.
  4992. *------------------------------------------*/
  4993. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4994. {
  4995. nullpo_retv(sd);
  4996. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4997. return;
  4998. struct item *item_data=&sd->cart.u.items_cart[idx];
  4999. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5000. return;
  5001. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5002. if (flag == ADDITEM_SUCCESS)
  5003. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5004. else {
  5005. clif_cart_delitem(sd, idx, amount);
  5006. clif_additem(sd, idx, amount, flag);
  5007. clif_cart_additem(sd, idx, amount);
  5008. }
  5009. }
  5010. /*==========================================
  5011. * Bound Item Check
  5012. * Type:
  5013. * 1 Account Bound
  5014. * 2 Guild Bound
  5015. * 3 Party Bound
  5016. * 4 Character Bound
  5017. *------------------------------------------*/
  5018. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5019. {
  5020. int i = 0, j = 0;
  5021. for(i = 0; i < MAX_INVENTORY; i++) {
  5022. 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) {
  5023. idxlist[j] = i;
  5024. j++;
  5025. }
  5026. }
  5027. return j;
  5028. }
  5029. /*==========================================
  5030. * Display item stolen msg to player sd
  5031. *------------------------------------------*/
  5032. int pc_show_steal(struct block_list *bl,va_list ap)
  5033. {
  5034. struct map_session_data *sd;
  5035. t_itemid itemid;
  5036. struct item_data *item=NULL;
  5037. char output[100];
  5038. sd=va_arg(ap,struct map_session_data *);
  5039. itemid=va_arg(ap,int);
  5040. if((item=itemdb_exists(itemid))==NULL)
  5041. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5042. else
  5043. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  5044. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5045. return 0;
  5046. }
  5047. /**
  5048. * Steal an item from bl (mob).
  5049. * @param sd: Player data
  5050. * @param bl: Object to steal from
  5051. * @param skill_lv: Level of skill used
  5052. * @return True on success or false otherwise
  5053. */
  5054. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5055. {
  5056. int i;
  5057. t_itemid itemid;
  5058. double rate;
  5059. unsigned char flag = 0;
  5060. struct status_data *sd_status, *md_status;
  5061. struct mob_data *md;
  5062. struct item tmp_item;
  5063. if(!sd || !bl || bl->type!=BL_MOB)
  5064. return false;
  5065. md = (TBL_MOB *)bl;
  5066. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5067. return false;
  5068. sd_status= status_get_status_data(&sd->bl);
  5069. md_status= status_get_status_data(bl);
  5070. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  5071. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5072. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5073. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5074. ) { //Can't steal from
  5075. md->state.steal_flag = UCHAR_MAX;
  5076. return false;
  5077. }
  5078. // base skill success chance (percentual)
  5079. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5080. rate += sd->bonus.add_steal_rate;
  5081. if( rate < 1
  5082. #ifdef RENEWAL
  5083. || rnd()%100 >= rate
  5084. #endif
  5085. )
  5086. return false;
  5087. // Try dropping one item, in the order from first to last possible slot.
  5088. // Droprate is affected by the skill success rate.
  5089. for( i = 0; i < MAX_STEAL_DROP; i++ )
  5090. 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].p
  5091. #ifndef RENEWAL
  5092. * rate/100.
  5093. #endif
  5094. )
  5095. break;
  5096. if( i == MAX_STEAL_DROP )
  5097. return false;
  5098. itemid = md->db->dropitem[i].nameid;
  5099. memset(&tmp_item,0,sizeof(tmp_item));
  5100. tmp_item.nameid = itemid;
  5101. tmp_item.amount = 1;
  5102. tmp_item.identify = itemdb_isidentified(itemid);
  5103. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5104. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5105. //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?
  5106. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5107. if(flag) { //Failed to steal due to overweight
  5108. clif_additem(sd,0,0,flag);
  5109. return false;
  5110. }
  5111. if(battle_config.show_steal_in_same_party)
  5112. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5113. //Logs items, Stolen from mobs [Lupus]
  5114. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5115. //A Rare Steal Global Announce by Lupus
  5116. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  5117. struct item_data *i_data;
  5118. char message[128];
  5119. i_data = itemdb_search(itemid);
  5120. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  5121. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5122. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5123. }
  5124. return true;
  5125. }
  5126. /*==========================================
  5127. * Stole zeny from bl (mob)
  5128. * return
  5129. * 0 = fail
  5130. * 1 = success
  5131. *------------------------------------------*/
  5132. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5133. {
  5134. int rate, target_lv;
  5135. struct mob_data *md;
  5136. if(!sd || !target || target->type != BL_MOB)
  5137. return 0;
  5138. md = (TBL_MOB*)target;
  5139. target_lv = status_get_lv(target);
  5140. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  5141. return 0;
  5142. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5143. if(rnd()%1000 < rate)
  5144. {
  5145. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5146. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5147. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5148. md->state.steal_coin_flag = 1;
  5149. return 1;
  5150. }
  5151. return 0;
  5152. }
  5153. /*==========================================
  5154. * Set's a player position.
  5155. * @param sd
  5156. * @param mapindex
  5157. * @param x
  5158. * @param y
  5159. * @param clrtype
  5160. * @return SETPOS_OK Success
  5161. * SETPOS_MAPINDEX Invalid map index
  5162. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5163. * SETPOS_AUTOTRADE Player is in autotrade state
  5164. *------------------------------------------*/
  5165. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5166. {
  5167. nullpo_retr(SETPOS_OK,sd);
  5168. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5169. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5170. return SETPOS_MAPINDEX;
  5171. }
  5172. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5173. return SETPOS_AUTOTRADE;
  5174. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5175. pc_setstand(sd, true);
  5176. pc_setrestartvalue(sd,1);
  5177. }
  5178. int16 m = map_mapindex2mapid(mapindex);
  5179. struct map_data *mapdata = map_getmapdata(m);
  5180. status_change *sc = status_get_sc(&sd->bl);
  5181. sd->state.changemap = (sd->mapindex != mapindex);
  5182. sd->state.warping = 1;
  5183. sd->state.workinprogress = WIP_DISABLE_NONE;
  5184. sd->state.mail_writing = false;
  5185. if( sd->state.changemap ) { // Misc map-changing settings
  5186. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5187. if (curr_map_instance_id != new_map_instance_id) {
  5188. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5189. instance_delusers(curr_map_instance_id);
  5190. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5191. }
  5192. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5193. instance_addusers(new_map_instance_id);
  5194. }
  5195. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5196. bg_team_leave(sd, false, true);
  5197. sd->state.pmap = sd->bl.m;
  5198. if (sc && sc->count) { // Cancel some map related stuff.
  5199. if (sc->data[SC_JAILED])
  5200. return SETPOS_MAPINDEX; //You may not get out!
  5201. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5202. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5203. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5204. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5205. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5206. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5207. if (sc->data[SC_KNOWLEDGE]) {
  5208. struct status_change_entry *sce = sc->data[SC_KNOWLEDGE];
  5209. if (sce->timer != INVALID_TIMER)
  5210. delete_timer(sce->timer, status_change_timer);
  5211. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5212. }
  5213. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5214. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5215. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5216. status_change_end(&sd->bl, SC_SUHIDE, INVALID_TIMER);
  5217. }
  5218. for(int i = 0; i < EQI_MAX; i++ ) {
  5219. if( sd->equip_index[i] >= 0 )
  5220. if( pc_isequip(sd,sd->equip_index[i]) )
  5221. pc_unequipitem(sd,sd->equip_index[i],2);
  5222. }
  5223. if (battle_config.clear_unit_onwarp&BL_PC)
  5224. skill_clear_unitgroup(&sd->bl);
  5225. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5226. guild_send_dot_remove(sd);
  5227. bg_send_dot_remove(sd);
  5228. if (sd->regen.state.gc)
  5229. sd->regen.state.gc = 0;
  5230. // make sure vending is allowed here
  5231. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5232. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5233. vending_closevending(sd);
  5234. }
  5235. channel_pcquit(sd,4); //quit map chan
  5236. // Remove Cloaked NPC if changing to another map
  5237. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5238. block_list *npc_bl = map_id2bl(*it);
  5239. if (npc_bl && npc_bl->m != m) {
  5240. sd->cloaked_npc.erase(it);
  5241. break;
  5242. }
  5243. }
  5244. }
  5245. if( m < 0 )
  5246. {
  5247. uint32 ip;
  5248. uint16 port;
  5249. struct script_state *st;
  5250. //if can't find any map-servers, just abort setting position.
  5251. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5252. return SETPOS_NO_MAPSERVER;
  5253. if (sd->npc_id){
  5254. npc_event_dequeue(sd,false);
  5255. st = sd->st;
  5256. }else{
  5257. st = nullptr;
  5258. }
  5259. npc_script_event(sd, NPCE_LOGOUT);
  5260. //remove from map, THEN change x/y coordinates
  5261. unit_remove_map_pc(sd,clrtype);
  5262. sd->mapindex = mapindex;
  5263. sd->bl.x=x;
  5264. sd->bl.y=y;
  5265. pc_clean_skilltree(sd);
  5266. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5267. chrif_changemapserver(sd, ip, (short)port);
  5268. //Free session data from this map server [Kevin]
  5269. unit_free_pc(sd);
  5270. if( st ){
  5271. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5272. st->state = END;
  5273. }
  5274. return SETPOS_OK;
  5275. }
  5276. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5277. {
  5278. 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);
  5279. x = y = 0; // make it random
  5280. }
  5281. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5282. int c=0;
  5283. do {
  5284. x = rnd()%(mapdata->xs-2)+1;
  5285. y = rnd()%(mapdata->ys-2)+1;
  5286. c++;
  5287. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5288. 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));
  5289. return SETPOS_OK; //preventing warp
  5290. //break; //allow warp anyway
  5291. }
  5292. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5293. }
  5294. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5295. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5296. vending_closevending(sd);
  5297. }
  5298. if(sd->bl.prev != NULL){
  5299. unit_remove_map_pc(sd,clrtype);
  5300. clif_changemap(sd,m,x,y); // [MouseJstr]
  5301. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5302. sd->state.rewarp = 1;
  5303. sd->mapindex = mapindex;
  5304. sd->bl.m = m;
  5305. sd->bl.x = sd->ud.to_x = x;
  5306. sd->bl.y = sd->ud.to_y = y;
  5307. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5308. { // Increased guild castle regen [Valaris]
  5309. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5310. if(gc && gc->guild_id == sd->status.guild_id)
  5311. sd->regen.state.gc = 1;
  5312. }
  5313. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5314. {
  5315. sd->pd->bl.m = m;
  5316. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5317. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5318. sd->pd->ud.dir = sd->ud.dir;
  5319. }
  5320. if( hom_is_active(sd->hd) )
  5321. {
  5322. sd->hd->bl.m = m;
  5323. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5324. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5325. sd->hd->ud.dir = sd->ud.dir;
  5326. }
  5327. if( sd->md )
  5328. {
  5329. sd->md->bl.m = m;
  5330. sd->md->bl.x = sd->md->ud.to_x = x;
  5331. sd->md->bl.y = sd->md->ud.to_y = y;
  5332. sd->md->ud.dir = sd->ud.dir;
  5333. }
  5334. if( sd->ed ) {
  5335. sd->ed->bl.m = m;
  5336. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5337. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5338. sd->ed->ud.dir = sd->ud.dir;
  5339. }
  5340. pc_cell_basilica(sd);
  5341. //check if we gonna be rewarped [lighta]
  5342. if(npc_check_areanpc(1,m,x,y,0)){
  5343. sd->count_rewarp++;
  5344. }
  5345. else
  5346. sd->count_rewarp = 0;
  5347. return SETPOS_OK;
  5348. }
  5349. /*==========================================
  5350. * Warp player sd to random location on current map.
  5351. * May fail if no walkable cell found (1000 attempts).
  5352. * Return:
  5353. * 0 = Success
  5354. * 1,2,3 = Fail
  5355. *------------------------------------------*/
  5356. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5357. {
  5358. int x,y,i=0;
  5359. nullpo_ret(sd);
  5360. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5361. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5362. return 3;
  5363. do {
  5364. x = rnd()%(mapdata->xs-2)+1;
  5365. y = rnd()%(mapdata->ys-2)+1;
  5366. } 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);
  5367. if (i < 1000)
  5368. return pc_setpos(sd,mapdata->index,x,y,type);
  5369. return 3;
  5370. }
  5371. /*==========================================
  5372. * Records a memo point at sd's current position
  5373. * pos - entry to replace, (-1: shift oldest entry out)
  5374. *------------------------------------------*/
  5375. bool pc_memo(struct map_session_data* sd, int pos)
  5376. {
  5377. int skill;
  5378. nullpo_ret(sd);
  5379. // check mapflags
  5380. 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) ) {
  5381. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5382. return false;
  5383. }
  5384. // check inputs
  5385. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5386. return false; // invalid input
  5387. // check required skill level
  5388. skill = pc_checkskill(sd, AL_WARP);
  5389. if( skill < 1 ) {
  5390. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5391. return false;
  5392. }
  5393. if( skill < 2 || skill - 2 < pos ) {
  5394. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5395. return false;
  5396. }
  5397. if( pos == -1 )
  5398. {
  5399. uint8 i;
  5400. // prevent memo-ing the same map multiple times
  5401. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5402. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5403. pos = 0;
  5404. }
  5405. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5406. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5407. return false;
  5408. }
  5409. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5410. sd->status.memo_point[pos].x = sd->bl.x;
  5411. sd->status.memo_point[pos].y = sd->bl.y;
  5412. clif_skill_memomessage(sd, 0);
  5413. return true;
  5414. }
  5415. //
  5416. // Skills
  5417. //
  5418. /**
  5419. * Get the skill current cooldown for player.
  5420. * (get the db base cooldown for skill + player specific cooldown)
  5421. * @param sd : player pointer
  5422. * @param id : skill id
  5423. * @param lv : skill lv
  5424. * @return player skill cooldown
  5425. */
  5426. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5427. if (skill_id == SJ_NOVAEXPLOSING) {
  5428. struct status_change *sc = status_get_sc(&sd->bl);
  5429. if (sc && sc->data[SC_DIMENSION])
  5430. return 0;
  5431. }
  5432. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5433. if (cooldown == 0)
  5434. return 0;
  5435. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5436. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5437. for (auto &it : sd->skillcooldown) {
  5438. if (it.id == skill_id) {
  5439. cooldown += it.val;
  5440. cooldown = max(0, cooldown);
  5441. break;
  5442. }
  5443. }
  5444. return cooldown;
  5445. }
  5446. /*==========================================
  5447. * Return player sd skill_lv learned for given skill
  5448. *------------------------------------------*/
  5449. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5450. {
  5451. uint16 idx = 0;
  5452. if (sd == NULL)
  5453. return 0;
  5454. if ((idx = skill_get_index(skill_id)) == 0) {
  5455. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5456. return 0;
  5457. }
  5458. if (SKILL_CHK_GUILD(skill_id) ) {
  5459. struct guild *g;
  5460. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5461. return guild_checkskill(g,skill_id);
  5462. return 0;
  5463. }
  5464. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5465. }
  5466. /**
  5467. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5468. * @param sd: Player data
  5469. * @param type: Summoner Power Type
  5470. * @return Skill points invested
  5471. */
  5472. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5473. if (sd == nullptr)
  5474. return 0;
  5475. uint8 count = 0;
  5476. switch (type) {
  5477. case SUMMONER_POWER_SEA:
  5478. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5479. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  5480. break;
  5481. case SUMMONER_POWER_LAND:
  5482. 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) +
  5483. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  5484. break;
  5485. case SUMMONER_POWER_LIFE:
  5486. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  5487. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  5488. break;
  5489. }
  5490. return count;
  5491. }
  5492. /**
  5493. * Check if we still have the correct weapon to continue the skill (actually status)
  5494. * If not ending it
  5495. * @param sd
  5496. * @return 0:error, 1:check done
  5497. */
  5498. static void pc_checkallowskill(struct map_session_data *sd)
  5499. {
  5500. const enum sc_type scw_list[] = {
  5501. SC_TWOHANDQUICKEN,
  5502. SC_ONEHAND,
  5503. SC_AURABLADE,
  5504. SC_PARRYING,
  5505. SC_SPEARQUICKEN,
  5506. SC_ADRENALINE,
  5507. SC_ADRENALINE2,
  5508. SC_DANCING,
  5509. SC_GATLINGFEVER,
  5510. };
  5511. uint8 i;
  5512. nullpo_retv(sd);
  5513. if(!sd->sc.count)
  5514. return;
  5515. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5516. { // Skills requiring specific weapon types
  5517. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5518. continue;
  5519. if(sd->sc.data[scw_list[i]] &&
  5520. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5521. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5522. }
  5523. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5524. // Spurt requires bare hands (feet, in fact xD)
  5525. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5526. if(sd->status.shield <= 0) { // Skills requiring a shield
  5527. const enum sc_type scs_list[] = {
  5528. SC_AUTOGUARD,
  5529. SC_DEFENDER,
  5530. SC_REFLECTSHIELD,
  5531. SC_REFLECTDAMAGE
  5532. };
  5533. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5534. if(sd->sc.data[scs_list[i]])
  5535. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5536. }
  5537. }
  5538. /*==========================================
  5539. * Return equipped index of item on player sd at pos
  5540. * Return
  5541. * -1 : Nothing equipped
  5542. * idx : (this index could be used in inventory to found item_data)
  5543. *------------------------------------------*/
  5544. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5545. {
  5546. uint8 i;
  5547. nullpo_retr(-1, sd);
  5548. for(i=0;i<EQI_MAX;i++){
  5549. if(pos & equip_bitmask[i]){
  5550. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5551. // Check all if any match is found
  5552. continue;
  5553. }
  5554. return sd->equip_index[i];
  5555. }
  5556. }
  5557. return -1;
  5558. }
  5559. /*==========================================
  5560. * Check if sd has nameid equipped somewhere
  5561. * @sd : the player session
  5562. * @nameid : id of the item to check
  5563. * @min : : see pc.hpp enum equip_index from ? to @max
  5564. * @max : see pc.hpp enum equip_index for @min to ?
  5565. * -return true,false
  5566. *------------------------------------------*/
  5567. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  5568. {
  5569. int i;
  5570. for(i = min; i < max; i++) {
  5571. if(equip_bitmask[i]) {
  5572. int idx = sd->equip_index[i];
  5573. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5574. return true;
  5575. }
  5576. }
  5577. return false;
  5578. }
  5579. /*==========================================
  5580. * Convert's from the client's lame Job ID system
  5581. * to the map server's 'makes sense' system. [Skotlex]
  5582. *------------------------------------------*/
  5583. int pc_jobid2mapid(unsigned short b_class)
  5584. {
  5585. switch(b_class)
  5586. {
  5587. //Novice And 1-1 Jobs
  5588. case JOB_NOVICE: return MAPID_NOVICE;
  5589. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5590. case JOB_MAGE: return MAPID_MAGE;
  5591. case JOB_ARCHER: return MAPID_ARCHER;
  5592. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5593. case JOB_MERCHANT: return MAPID_MERCHANT;
  5594. case JOB_THIEF: return MAPID_THIEF;
  5595. case JOB_TAEKWON: return MAPID_TAEKWON;
  5596. case JOB_WEDDING: return MAPID_WEDDING;
  5597. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5598. case JOB_NINJA: return MAPID_NINJA;
  5599. case JOB_XMAS: return MAPID_XMAS;
  5600. case JOB_SUMMER: return MAPID_SUMMER;
  5601. case JOB_HANBOK: return MAPID_HANBOK;
  5602. case JOB_GANGSI: return MAPID_GANGSI;
  5603. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5604. case JOB_SUMMER2: return MAPID_SUMMER2;
  5605. //2-1 Jobs
  5606. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5607. case JOB_KNIGHT: return MAPID_KNIGHT;
  5608. case JOB_WIZARD: return MAPID_WIZARD;
  5609. case JOB_HUNTER: return MAPID_HUNTER;
  5610. case JOB_PRIEST: return MAPID_PRIEST;
  5611. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5612. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5613. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5614. case JOB_KAGEROU:
  5615. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5616. case JOB_REBELLION: return MAPID_REBELLION;
  5617. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5618. //2-2 Jobs
  5619. case JOB_CRUSADER: return MAPID_CRUSADER;
  5620. case JOB_SAGE: return MAPID_SAGE;
  5621. case JOB_BARD:
  5622. case JOB_DANCER: return MAPID_BARDDANCER;
  5623. case JOB_MONK: return MAPID_MONK;
  5624. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5625. case JOB_ROGUE: return MAPID_ROGUE;
  5626. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5627. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5628. //Trans Novice And Trans 1-1 Jobs
  5629. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5630. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5631. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5632. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5633. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5634. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5635. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5636. //Trans 2-1 Jobs
  5637. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5638. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5639. case JOB_SNIPER: return MAPID_SNIPER;
  5640. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5641. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5642. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5643. //Trans 2-2 Jobs
  5644. case JOB_PALADIN: return MAPID_PALADIN;
  5645. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5646. case JOB_CLOWN:
  5647. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5648. case JOB_CHAMPION: return MAPID_CHAMPION;
  5649. case JOB_CREATOR: return MAPID_CREATOR;
  5650. case JOB_STALKER: return MAPID_STALKER;
  5651. //Baby Novice And Baby 1-1 Jobs
  5652. case JOB_BABY: return MAPID_BABY;
  5653. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5654. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5655. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5656. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5657. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5658. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5659. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5660. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5661. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5662. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5663. //Baby 2-1 Jobs
  5664. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5665. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5666. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5667. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5668. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5669. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5670. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5671. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5672. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5673. case JOB_BABY_KAGEROU:
  5674. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5675. //Baby 2-2 Jobs
  5676. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5677. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5678. case JOB_BABY_BARD:
  5679. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5680. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5681. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5682. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5683. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5684. //3-1 Jobs
  5685. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5686. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5687. case JOB_WARLOCK: return MAPID_WARLOCK;
  5688. case JOB_RANGER: return MAPID_RANGER;
  5689. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5690. case JOB_MECHANIC: return MAPID_MECHANIC;
  5691. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5692. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5693. //3-2 Jobs
  5694. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5695. case JOB_SORCERER: return MAPID_SORCERER;
  5696. case JOB_MINSTREL:
  5697. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5698. case JOB_SURA: return MAPID_SURA;
  5699. case JOB_GENETIC: return MAPID_GENETIC;
  5700. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5701. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5702. //Trans 3-1 Jobs
  5703. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5704. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5705. case JOB_RANGER_T: return MAPID_RANGER_T;
  5706. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5707. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5708. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5709. //Trans 3-2 Jobs
  5710. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5711. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5712. case JOB_MINSTREL_T:
  5713. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5714. case JOB_SURA_T: return MAPID_SURA_T;
  5715. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5716. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5717. //Baby 3-1 Jobs
  5718. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5719. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5720. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5721. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5722. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5723. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5724. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5725. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5726. //Baby 3-2 Jobs
  5727. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5728. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5729. case JOB_BABY_MINSTREL:
  5730. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5731. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5732. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5733. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5734. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5735. //Doram Jobs
  5736. case JOB_SUMMONER: return MAPID_SUMMONER;
  5737. default:
  5738. return -1;
  5739. }
  5740. }
  5741. //Reverts the map-style class id to the client-style one.
  5742. int pc_mapid2jobid(unsigned short class_, int sex)
  5743. {
  5744. switch(class_) {
  5745. //Novice And 1-1 Jobs
  5746. case MAPID_NOVICE: return JOB_NOVICE;
  5747. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5748. case MAPID_MAGE: return JOB_MAGE;
  5749. case MAPID_ARCHER: return JOB_ARCHER;
  5750. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5751. case MAPID_MERCHANT: return JOB_MERCHANT;
  5752. case MAPID_THIEF: return JOB_THIEF;
  5753. case MAPID_TAEKWON: return JOB_TAEKWON;
  5754. case MAPID_WEDDING: return JOB_WEDDING;
  5755. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5756. case MAPID_NINJA: return JOB_NINJA;
  5757. case MAPID_XMAS: return JOB_XMAS;
  5758. case MAPID_SUMMER: return JOB_SUMMER;
  5759. case MAPID_HANBOK: return JOB_HANBOK;
  5760. case MAPID_GANGSI: return JOB_GANGSI;
  5761. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5762. case MAPID_SUMMER2: return JOB_SUMMER2;
  5763. //2-1 Jobs
  5764. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5765. case MAPID_KNIGHT: return JOB_KNIGHT;
  5766. case MAPID_WIZARD: return JOB_WIZARD;
  5767. case MAPID_HUNTER: return JOB_HUNTER;
  5768. case MAPID_PRIEST: return JOB_PRIEST;
  5769. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5770. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5771. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5772. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5773. case MAPID_REBELLION: return JOB_REBELLION;
  5774. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5775. //2-2 Jobs
  5776. case MAPID_CRUSADER: return JOB_CRUSADER;
  5777. case MAPID_SAGE: return JOB_SAGE;
  5778. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5779. case MAPID_MONK: return JOB_MONK;
  5780. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5781. case MAPID_ROGUE: return JOB_ROGUE;
  5782. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5783. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5784. //Trans Novice And Trans 2-1 Jobs
  5785. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5786. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5787. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5788. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5789. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5790. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5791. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5792. //Trans 2-1 Jobs
  5793. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5794. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5795. case MAPID_SNIPER: return JOB_SNIPER;
  5796. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5797. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5798. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5799. //Trans 2-2 Jobs
  5800. case MAPID_PALADIN: return JOB_PALADIN;
  5801. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5802. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5803. case MAPID_CHAMPION: return JOB_CHAMPION;
  5804. case MAPID_CREATOR: return JOB_CREATOR;
  5805. case MAPID_STALKER: return JOB_STALKER;
  5806. //Baby Novice And Baby 1-1 Jobs
  5807. case MAPID_BABY: return JOB_BABY;
  5808. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5809. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5810. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5811. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5812. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5813. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5814. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5815. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5816. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5817. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5818. //Baby 2-1 Jobs
  5819. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5820. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5821. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5822. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5823. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5824. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5825. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5826. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5827. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5828. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5829. //Baby 2-2 Jobs
  5830. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5831. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5832. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5833. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5834. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5835. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5836. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5837. //3-1 Jobs
  5838. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5839. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5840. case MAPID_WARLOCK: return JOB_WARLOCK;
  5841. case MAPID_RANGER: return JOB_RANGER;
  5842. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5843. case MAPID_MECHANIC: return JOB_MECHANIC;
  5844. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5845. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5846. //3-2 Jobs
  5847. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5848. case MAPID_SORCERER: return JOB_SORCERER;
  5849. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5850. case MAPID_SURA: return JOB_SURA;
  5851. case MAPID_GENETIC: return JOB_GENETIC;
  5852. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5853. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5854. //Trans 3-1 Jobs
  5855. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5856. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5857. case MAPID_RANGER_T: return JOB_RANGER_T;
  5858. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5859. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5860. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5861. //Trans 3-2 Jobs
  5862. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5863. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5864. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5865. case MAPID_SURA_T: return JOB_SURA_T;
  5866. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5867. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5868. //Baby 3-1 Jobs
  5869. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5870. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5871. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5872. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5873. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5874. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5875. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5876. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5877. //Baby 3-2 Jobs
  5878. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5879. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5880. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5881. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5882. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5883. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5884. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5885. //Doram Jobs
  5886. case MAPID_SUMMONER: return JOB_SUMMONER;
  5887. default:
  5888. return -1;
  5889. }
  5890. }
  5891. /*====================================================
  5892. * This function return the name of the job (by [Yor])
  5893. *----------------------------------------------------*/
  5894. const char* job_name(int class_)
  5895. {
  5896. switch (class_) {
  5897. case JOB_NOVICE:
  5898. case JOB_SWORDMAN:
  5899. case JOB_MAGE:
  5900. case JOB_ARCHER:
  5901. case JOB_ACOLYTE:
  5902. case JOB_MERCHANT:
  5903. case JOB_THIEF:
  5904. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5905. case JOB_KNIGHT:
  5906. case JOB_PRIEST:
  5907. case JOB_WIZARD:
  5908. case JOB_BLACKSMITH:
  5909. case JOB_HUNTER:
  5910. case JOB_ASSASSIN:
  5911. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5912. case JOB_KNIGHT2:
  5913. return msg_txt(NULL,557);
  5914. case JOB_CRUSADER:
  5915. case JOB_MONK:
  5916. case JOB_SAGE:
  5917. case JOB_ROGUE:
  5918. case JOB_ALCHEMIST:
  5919. case JOB_BARD:
  5920. case JOB_DANCER:
  5921. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5922. case JOB_CRUSADER2:
  5923. return msg_txt(NULL,563);
  5924. case JOB_WEDDING:
  5925. case JOB_SUPER_NOVICE:
  5926. case JOB_GUNSLINGER:
  5927. case JOB_NINJA:
  5928. case JOB_XMAS:
  5929. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5930. case JOB_SUMMER:
  5931. case JOB_SUMMER2:
  5932. return msg_txt(NULL,621);
  5933. case JOB_HANBOK:
  5934. return msg_txt(NULL,694);
  5935. case JOB_OKTOBERFEST:
  5936. return msg_txt(NULL,696);
  5937. case JOB_NOVICE_HIGH:
  5938. case JOB_SWORDMAN_HIGH:
  5939. case JOB_MAGE_HIGH:
  5940. case JOB_ARCHER_HIGH:
  5941. case JOB_ACOLYTE_HIGH:
  5942. case JOB_MERCHANT_HIGH:
  5943. case JOB_THIEF_HIGH:
  5944. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5945. case JOB_LORD_KNIGHT:
  5946. case JOB_HIGH_PRIEST:
  5947. case JOB_HIGH_WIZARD:
  5948. case JOB_WHITESMITH:
  5949. case JOB_SNIPER:
  5950. case JOB_ASSASSIN_CROSS:
  5951. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5952. case JOB_LORD_KNIGHT2:
  5953. return msg_txt(NULL,582);
  5954. case JOB_PALADIN:
  5955. case JOB_CHAMPION:
  5956. case JOB_PROFESSOR:
  5957. case JOB_STALKER:
  5958. case JOB_CREATOR:
  5959. case JOB_CLOWN:
  5960. case JOB_GYPSY:
  5961. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5962. case JOB_PALADIN2:
  5963. return msg_txt(NULL,588);
  5964. case JOB_BABY:
  5965. case JOB_BABY_SWORDMAN:
  5966. case JOB_BABY_MAGE:
  5967. case JOB_BABY_ARCHER:
  5968. case JOB_BABY_ACOLYTE:
  5969. case JOB_BABY_MERCHANT:
  5970. case JOB_BABY_THIEF:
  5971. return msg_txt(NULL,595 - JOB_BABY + class_);
  5972. case JOB_BABY_KNIGHT:
  5973. case JOB_BABY_PRIEST:
  5974. case JOB_BABY_WIZARD:
  5975. case JOB_BABY_BLACKSMITH:
  5976. case JOB_BABY_HUNTER:
  5977. case JOB_BABY_ASSASSIN:
  5978. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5979. case JOB_BABY_KNIGHT2:
  5980. return msg_txt(NULL,602);
  5981. case JOB_BABY_CRUSADER:
  5982. case JOB_BABY_MONK:
  5983. case JOB_BABY_SAGE:
  5984. case JOB_BABY_ROGUE:
  5985. case JOB_BABY_ALCHEMIST:
  5986. case JOB_BABY_BARD:
  5987. case JOB_BABY_DANCER:
  5988. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5989. case JOB_BABY_CRUSADER2:
  5990. return msg_txt(NULL,608);
  5991. case JOB_SUPER_BABY:
  5992. return msg_txt(NULL,615);
  5993. case JOB_TAEKWON:
  5994. return msg_txt(NULL,616);
  5995. case JOB_STAR_GLADIATOR:
  5996. case JOB_STAR_GLADIATOR2:
  5997. return msg_txt(NULL,617);
  5998. case JOB_SOUL_LINKER:
  5999. return msg_txt(NULL,618);
  6000. case JOB_GANGSI:
  6001. case JOB_DEATH_KNIGHT:
  6002. case JOB_DARK_COLLECTOR:
  6003. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6004. case JOB_RUNE_KNIGHT:
  6005. case JOB_WARLOCK:
  6006. case JOB_RANGER:
  6007. case JOB_ARCH_BISHOP:
  6008. case JOB_MECHANIC:
  6009. case JOB_GUILLOTINE_CROSS:
  6010. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6011. case JOB_RUNE_KNIGHT_T:
  6012. case JOB_WARLOCK_T:
  6013. case JOB_RANGER_T:
  6014. case JOB_ARCH_BISHOP_T:
  6015. case JOB_MECHANIC_T:
  6016. case JOB_GUILLOTINE_CROSS_T:
  6017. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6018. case JOB_ROYAL_GUARD:
  6019. case JOB_SORCERER:
  6020. case JOB_MINSTREL:
  6021. case JOB_WANDERER:
  6022. case JOB_SURA:
  6023. case JOB_GENETIC:
  6024. case JOB_SHADOW_CHASER:
  6025. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6026. case JOB_ROYAL_GUARD_T:
  6027. case JOB_SORCERER_T:
  6028. case JOB_MINSTREL_T:
  6029. case JOB_WANDERER_T:
  6030. case JOB_SURA_T:
  6031. case JOB_GENETIC_T:
  6032. case JOB_SHADOW_CHASER_T:
  6033. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6034. case JOB_RUNE_KNIGHT2:
  6035. case JOB_RUNE_KNIGHT_T2:
  6036. return msg_txt(NULL,625);
  6037. case JOB_ROYAL_GUARD2:
  6038. case JOB_ROYAL_GUARD_T2:
  6039. return msg_txt(NULL,631);
  6040. case JOB_RANGER2:
  6041. case JOB_RANGER_T2:
  6042. return msg_txt(NULL,627);
  6043. case JOB_MECHANIC2:
  6044. case JOB_MECHANIC_T2:
  6045. return msg_txt(NULL,629);
  6046. case JOB_BABY_RUNE_KNIGHT:
  6047. case JOB_BABY_WARLOCK:
  6048. case JOB_BABY_RANGER:
  6049. case JOB_BABY_ARCH_BISHOP:
  6050. case JOB_BABY_MECHANIC:
  6051. case JOB_BABY_GUILLOTINE_CROSS:
  6052. case JOB_BABY_ROYAL_GUARD:
  6053. case JOB_BABY_SORCERER:
  6054. case JOB_BABY_MINSTREL:
  6055. case JOB_BABY_WANDERER:
  6056. case JOB_BABY_SURA:
  6057. case JOB_BABY_GENETIC:
  6058. case JOB_BABY_SHADOW_CHASER:
  6059. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6060. case JOB_BABY_RUNE_KNIGHT2:
  6061. return msg_txt(NULL,638);
  6062. case JOB_BABY_ROYAL_GUARD2:
  6063. return msg_txt(NULL,644);
  6064. case JOB_BABY_RANGER2:
  6065. return msg_txt(NULL,640);
  6066. case JOB_BABY_MECHANIC2:
  6067. return msg_txt(NULL,642);
  6068. case JOB_SUPER_NOVICE_E:
  6069. case JOB_SUPER_BABY_E:
  6070. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6071. case JOB_KAGEROU:
  6072. case JOB_OBORO:
  6073. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6074. case JOB_REBELLION:
  6075. return msg_txt(NULL,695);
  6076. case JOB_SUMMONER:
  6077. return msg_txt(NULL,697);
  6078. case JOB_BABY_SUMMONER:
  6079. return msg_txt(NULL,698);
  6080. case JOB_BABY_NINJA:
  6081. return msg_txt(NULL,699);
  6082. case JOB_BABY_KAGEROU:
  6083. case JOB_BABY_OBORO:
  6084. case JOB_BABY_TAEKWON:
  6085. case JOB_BABY_STAR_GLADIATOR:
  6086. case JOB_BABY_SOUL_LINKER:
  6087. case JOB_BABY_GUNSLINGER:
  6088. case JOB_BABY_REBELLION:
  6089. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6090. case JOB_BABY_STAR_GLADIATOR2:
  6091. return msg_txt(NULL,756);
  6092. case JOB_STAR_EMPEROR:
  6093. case JOB_SOUL_REAPER:
  6094. case JOB_BABY_STAR_EMPEROR:
  6095. case JOB_BABY_SOUL_REAPER:
  6096. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6097. case JOB_STAR_EMPEROR2:
  6098. return msg_txt(NULL,782);
  6099. case JOB_BABY_STAR_EMPEROR2:
  6100. return msg_txt(NULL,784);
  6101. default:
  6102. return msg_txt(NULL,655);
  6103. }
  6104. }
  6105. /*====================================================
  6106. * Timered function to make id follow a target.
  6107. * @id = bl.id (player only atm)
  6108. * target is define in sd->followtarget (bl.id)
  6109. * used by pc_follow
  6110. *----------------------------------------------------*/
  6111. TIMER_FUNC(pc_follow_timer){
  6112. struct map_session_data *sd;
  6113. struct block_list *tbl;
  6114. sd = map_id2sd(id);
  6115. nullpo_ret(sd);
  6116. if (sd->followtimer != tid){
  6117. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6118. sd->followtimer = INVALID_TIMER;
  6119. return 0;
  6120. }
  6121. sd->followtimer = INVALID_TIMER;
  6122. tbl = map_id2bl(sd->followtarget);
  6123. if (tbl == NULL || pc_isdead(sd))
  6124. {
  6125. pc_stop_following(sd);
  6126. return 0;
  6127. }
  6128. // either player or target is currently detached from map blocks (could be teleporting),
  6129. // but still connected to this map, so we'll just increment the timer and check back later
  6130. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6131. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6132. {
  6133. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6134. if (!check_distance_bl(&sd->bl, tbl, 5))
  6135. unit_walktobl(&sd->bl, tbl, 5, 0);
  6136. } else
  6137. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6138. }
  6139. sd->followtimer = add_timer(
  6140. tick + 1000, // increase time a bit to loosen up map's load
  6141. pc_follow_timer, sd->bl.id, 0);
  6142. return 0;
  6143. }
  6144. int pc_stop_following (struct map_session_data *sd)
  6145. {
  6146. nullpo_ret(sd);
  6147. if (sd->followtimer != INVALID_TIMER) {
  6148. delete_timer(sd->followtimer,pc_follow_timer);
  6149. sd->followtimer = INVALID_TIMER;
  6150. }
  6151. sd->followtarget = -1;
  6152. sd->ud.target_to = 0;
  6153. unit_stop_walking(&sd->bl, 1);
  6154. return 0;
  6155. }
  6156. int pc_follow(struct map_session_data *sd,int target_id)
  6157. {
  6158. struct block_list *bl = map_id2bl(target_id);
  6159. if (bl == NULL /*|| bl->type != BL_PC*/)
  6160. return 1;
  6161. if (sd->followtimer != INVALID_TIMER)
  6162. pc_stop_following(sd);
  6163. sd->followtarget = target_id;
  6164. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6165. return 0;
  6166. }
  6167. int pc_checkbaselevelup(struct map_session_data *sd) {
  6168. t_exp next = pc_nextbaseexp(sd);
  6169. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6170. return 0;
  6171. uint32 base_level = sd->status.base_level;
  6172. do {
  6173. sd->status.base_exp -= next;
  6174. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6175. 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 )
  6176. sd->status.base_exp = next-1;
  6177. sd->status.status_point += pc_gets_status_point(sd->status.base_level++);
  6178. if( pc_is_maxbaselv(sd) ){
  6179. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6180. break;
  6181. }
  6182. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6183. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6184. status_calc_pet(sd->pd,SCO_NONE);
  6185. clif_updatestatus(sd,SP_STATUSPOINT);
  6186. clif_updatestatus(sd,SP_BASELEVEL);
  6187. clif_updatestatus(sd,SP_BASEEXP);
  6188. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6189. status_calc_pc(sd,SCO_FORCE);
  6190. status_percent_heal(&sd->bl,100,100);
  6191. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6192. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6193. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6194. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6195. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6196. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6197. if (sd->state.snovice_dead_flag)
  6198. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6199. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6200. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6201. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6202. }
  6203. clif_misceffect(&sd->bl,0);
  6204. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6205. if(sd->status.party_id)
  6206. party_send_levelup(sd);
  6207. pc_baselevelchanged(sd);
  6208. for (; base_level <= sd->status.base_level; base_level++) {
  6209. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6210. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6211. }
  6212. return 1;
  6213. }
  6214. void pc_baselevelchanged(struct map_session_data *sd) {
  6215. uint8 i;
  6216. for( i = 0; i < EQI_MAX; i++ ) {
  6217. if( sd->equip_index[i] >= 0 ) {
  6218. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6219. pc_unequipitem(sd, sd->equip_index[i], 3);
  6220. }
  6221. }
  6222. pc_show_questinfo(sd);
  6223. }
  6224. int pc_checkjoblevelup(struct map_session_data *sd)
  6225. {
  6226. t_exp next = pc_nextjobexp(sd);
  6227. nullpo_ret(sd);
  6228. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6229. return 0;
  6230. uint32 job_level = sd->status.job_level;
  6231. do {
  6232. sd->status.job_exp -= next;
  6233. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6234. 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 )
  6235. sd->status.job_exp = next-1;
  6236. sd->status.job_level ++;
  6237. sd->status.skill_point ++;
  6238. if( pc_is_maxjoblv(sd) ){
  6239. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6240. break;
  6241. }
  6242. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6243. clif_updatestatus(sd,SP_JOBLEVEL);
  6244. clif_updatestatus(sd,SP_JOBEXP);
  6245. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6246. clif_updatestatus(sd,SP_SKILLPOINT);
  6247. status_calc_pc(sd,SCO_FORCE);
  6248. clif_misceffect(&sd->bl,1);
  6249. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6250. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6251. npc_script_event(sd, NPCE_JOBLVUP);
  6252. for (; job_level <= sd->status.job_level; job_level++)
  6253. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6254. pc_show_questinfo(sd);
  6255. return 1;
  6256. }
  6257. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6258. * @param sd Player
  6259. * @param base_exp Base EXP before peronal bonuses
  6260. * @param job_exp Job EXP before peronal bonuses
  6261. * @param src Block list that affecting the exp calculation
  6262. */
  6263. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6264. {
  6265. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6266. if (src) {
  6267. struct status_data *status = status_get_status_data(src);
  6268. if( sd->expaddrace[status->race] )
  6269. bonus += sd->expaddrace[status->race];
  6270. if( sd->expaddrace[RC_ALL] )
  6271. bonus += sd->expaddrace[RC_ALL];
  6272. if( sd->expaddclass[status->class_] )
  6273. bonus += sd->expaddclass[status->class_];
  6274. if( sd->expaddclass[CLASS_ALL] )
  6275. bonus += sd->expaddclass[CLASS_ALL];
  6276. if (battle_config.pk_mode &&
  6277. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6278. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6279. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6280. vip_bonus_base = battle_config.vip_base_exp_increase;
  6281. vip_bonus_job = battle_config.vip_job_exp_increase;
  6282. }
  6283. }
  6284. // Give EXPBOOST for quests even if src is NULL.
  6285. if (sd->sc.data[SC_EXPBOOST]) {
  6286. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6287. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6288. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6289. }
  6290. if (*base_exp) {
  6291. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6292. *base_exp = cap_value(exp, 1, MAX_EXP);
  6293. }
  6294. // Give JEXPBOOST for quests even if src is NULL.
  6295. if (sd->sc.data[SC_JEXPBOOST])
  6296. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6297. if (*job_exp) {
  6298. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6299. *job_exp = cap_value(exp, 1, MAX_EXP);
  6300. }
  6301. return;
  6302. }
  6303. /**
  6304. * Show EXP gained by player in percentage by @showexp
  6305. * @param sd Player
  6306. * @param base_exp Base EXP gained/loss
  6307. * @param next_base_exp Base EXP needed for next base level
  6308. * @param job_exp Job EXP gained/loss
  6309. * @param next_job_exp Job EXP needed for next job level
  6310. * @param lost True:EXP penalty, lose EXP
  6311. **/
  6312. 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) {
  6313. char output[CHAT_SIZE_MAX];
  6314. nullpo_retv(sd);
  6315. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6316. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6317. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6318. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6319. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6320. }
  6321. /**
  6322. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6323. * @param sd Player
  6324. * @param src EXP source
  6325. * @param base_exp Base EXP gained
  6326. * @param base_exp Job EXP gained
  6327. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6328. * @return
  6329. **/
  6330. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6331. {
  6332. t_exp nextb = 0, nextj = 0;
  6333. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6334. nullpo_retv(sd);
  6335. if(sd->bl.prev == NULL || pc_isdead(sd))
  6336. return;
  6337. if (!(exp_flag&2)) {
  6338. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6339. return; // no exp on pvp maps
  6340. if (sd->status.guild_id>0)
  6341. base_exp -= guild_payexp(sd,base_exp);
  6342. }
  6343. flag = ((base_exp) ? 1 : 0) |
  6344. ((job_exp) ? 2 : 0) |
  6345. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6346. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6347. if (!(exp_flag&2))
  6348. pc_calcexp(sd, &base_exp, &job_exp, src);
  6349. nextb = pc_nextbaseexp(sd);
  6350. nextj = pc_nextjobexp(sd);
  6351. if (flag&4){
  6352. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6353. base_exp = 0;
  6354. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6355. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6356. }
  6357. if (flag&8){
  6358. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6359. job_exp = 0;
  6360. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6361. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6362. }
  6363. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6364. //Note that this value should never be greater than the original
  6365. //therefore no overflow checks are needed. [Skotlex]
  6366. if (nextb > 0) {
  6367. float nextbp = (float) base_exp / (float) nextb;
  6368. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6369. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6370. }
  6371. if (nextj > 0) {
  6372. float nextjp = (float) job_exp / (float) nextj;
  6373. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6374. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6375. }
  6376. }
  6377. // Give EXP for Base Level
  6378. if (base_exp) {
  6379. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6380. if (!pc_checkbaselevelup(sd))
  6381. clif_updatestatus(sd,SP_BASEEXP);
  6382. }
  6383. // Give EXP for Job Level
  6384. if (job_exp) {
  6385. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6386. if (!pc_checkjoblevelup(sd))
  6387. clif_updatestatus(sd,SP_JOBEXP);
  6388. }
  6389. if (flag&1)
  6390. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6391. if (flag&2)
  6392. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6393. if (sd->state.showexp && (base_exp || job_exp))
  6394. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6395. }
  6396. /**
  6397. * Lost Base/Job EXP from a player
  6398. * @param sd Player
  6399. * @param base_exp Base EXP lost
  6400. * @param job_exp Job EXP lost
  6401. **/
  6402. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  6403. nullpo_retv(sd);
  6404. if (base_exp) {
  6405. base_exp = u64min(sd->status.base_exp, base_exp);
  6406. sd->status.base_exp -= base_exp;
  6407. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6408. clif_updatestatus(sd, SP_BASEEXP);
  6409. }
  6410. if (job_exp) {
  6411. job_exp = u64min(sd->status.job_exp, job_exp);
  6412. sd->status.job_exp -= job_exp;
  6413. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6414. clif_updatestatus(sd, SP_JOBEXP);
  6415. }
  6416. if (sd->state.showexp && (base_exp || job_exp))
  6417. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6418. }
  6419. /**
  6420. * Returns max base level for this character's class.
  6421. * @param class_: Player's class
  6422. * @return Max Base Level
  6423. */
  6424. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6425. return job_info[pc_class2idx(class_)].max_level[0];
  6426. }
  6427. /**
  6428. * Returns max base level for this character.
  6429. * @param sd Player
  6430. * @return Max Base Level
  6431. **/
  6432. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6433. return pc_class_maxbaselv(sd->status.class_);
  6434. }
  6435. /**
  6436. * Returns max job level for this character's class.
  6437. * @param class_: Player's class
  6438. * @return Max Job Level
  6439. */
  6440. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6441. return job_info[pc_class2idx(class_)].max_level[1];
  6442. }
  6443. /**
  6444. * Returns max job level for this character.
  6445. * @param sd Player
  6446. * @return Max Job Level
  6447. **/
  6448. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6449. return pc_class_maxjoblv(sd->status.class_);
  6450. }
  6451. /**
  6452. * Check if player is reached max base level
  6453. * @param sd
  6454. * @return True if reached max level
  6455. **/
  6456. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6457. nullpo_retr(false, sd);
  6458. return (sd->status.base_level >= pc_maxbaselv(sd));
  6459. }
  6460. /**
  6461. * Check if player is reached max base level
  6462. * @param sd
  6463. * @return True if reached max level
  6464. **/
  6465. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6466. nullpo_retr(false, sd);
  6467. return (sd->status.job_level >= pc_maxjoblv(sd));
  6468. }
  6469. /**
  6470. * Base exp needed for player to level up.
  6471. * @param sd
  6472. * @return Base EXP needed for next base level
  6473. **/
  6474. t_exp pc_nextbaseexp(struct map_session_data *sd){
  6475. nullpo_ret(sd);
  6476. if (sd->status.base_level == 0) // Is this something that possible?
  6477. return 0;
  6478. if (pc_is_maxbaselv(sd))
  6479. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6480. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6481. }
  6482. /**
  6483. * Job exp needed for player to level up.
  6484. * @param sd
  6485. * @return Job EXP needed for next job level
  6486. **/
  6487. t_exp pc_nextjobexp(struct map_session_data *sd){
  6488. nullpo_ret(sd);
  6489. if (sd->status.job_level == 0) // Is this something that possible?
  6490. return 0;
  6491. if (pc_is_maxjoblv(sd))
  6492. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6493. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6494. }
  6495. /// Returns the value of the specified stat.
  6496. static int pc_getstat(struct map_session_data* sd, int type)
  6497. {
  6498. nullpo_retr(-1, sd);
  6499. switch( type ) {
  6500. case SP_STR: return sd->status.str;
  6501. case SP_AGI: return sd->status.agi;
  6502. case SP_VIT: return sd->status.vit;
  6503. case SP_INT: return sd->status.int_;
  6504. case SP_DEX: return sd->status.dex;
  6505. case SP_LUK: return sd->status.luk;
  6506. default:
  6507. return -1;
  6508. }
  6509. }
  6510. /// Sets the specified stat to the specified value.
  6511. /// Returns the new value.
  6512. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6513. {
  6514. nullpo_retr(-1, sd);
  6515. switch( type ) {
  6516. case SP_STR: sd->status.str = val; break;
  6517. case SP_AGI: sd->status.agi = val; break;
  6518. case SP_VIT: sd->status.vit = val; break;
  6519. case SP_INT: sd->status.int_ = val; break;
  6520. case SP_DEX: sd->status.dex = val; break;
  6521. case SP_LUK: sd->status.luk = val; break;
  6522. default:
  6523. return -1;
  6524. }
  6525. return val;
  6526. }
  6527. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6528. int pc_gets_status_point(int level)
  6529. {
  6530. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6531. return (statp[level+1] - statp[level]);
  6532. else //Default increase
  6533. return ((level+15) / 5);
  6534. }
  6535. #ifdef RENEWAL_STAT
  6536. /// Renewal status point cost formula
  6537. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6538. #else
  6539. /// Pre-Renewal status point cost formula
  6540. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6541. #endif
  6542. /// Returns the number of stat points needed to change the specified stat by val.
  6543. /// If val is negative, returns the number of stat points that would be needed to
  6544. /// raise the specified stat from (current value - val) to current value.
  6545. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6546. {
  6547. int low, high, sp = 0, max = 0;
  6548. if ( val == 0 )
  6549. return 0;
  6550. low = pc_getstat(sd,type);
  6551. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6552. if ( low >= max && val > 0 )
  6553. return 0; // Official servers show '0' when max is reached
  6554. high = low + val;
  6555. if ( val < 0 )
  6556. SWAP(low, high);
  6557. for ( ; low < high; low++ )
  6558. sp += PC_STATUS_POINT_COST(low);
  6559. return sp;
  6560. }
  6561. /**
  6562. * Returns the value the specified stat can be increased by with the current
  6563. * amount of available status points for the current character's class.
  6564. *
  6565. * @param sd The target character.
  6566. * @param type Stat to verify.
  6567. * @return Maximum value the stat could grow by.
  6568. */
  6569. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6570. {
  6571. int base, final_val, status_points, max_param;
  6572. nullpo_ret(sd);
  6573. base = final_val = pc_getstat(sd, type);
  6574. status_points = sd->status.status_point;
  6575. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6576. while (final_val <= max_param && status_points >= 0) {
  6577. status_points -= PC_STATUS_POINT_COST(final_val);
  6578. final_val++;
  6579. }
  6580. final_val--;
  6581. return (final_val > base ? final_val-base : 0);
  6582. }
  6583. /**
  6584. * Raises a stat by the specified amount.
  6585. *
  6586. * Obeys max_parameter limits.
  6587. * Subtracts status points according to the cost of the increased stat points.
  6588. *
  6589. * @param sd The target character.
  6590. * @param type The stat to change (see enum _sp)
  6591. * @param increase The stat increase (strictly positive) amount.
  6592. * @retval true if the stat was increased by any amount.
  6593. * @retval false if there were no changes.
  6594. */
  6595. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6596. {
  6597. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6598. nullpo_ret(sd);
  6599. // check conditions
  6600. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6601. clif_statusupack(sd, type, 0, 0);
  6602. return false;
  6603. }
  6604. // check limits
  6605. current = pc_getstat(sd, type);
  6606. max_increase = pc_maxparameterincrease(sd, type);
  6607. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6608. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6609. clif_statusupack(sd, type, 0, 0);
  6610. return false;
  6611. }
  6612. // check status points
  6613. needed_points = pc_need_status_point(sd, type, increase);
  6614. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6615. clif_statusupack(sd, type, 0, 0);
  6616. return false;
  6617. }
  6618. // set new values
  6619. final_value = pc_setstat(sd, type, current + increase);
  6620. sd->status.status_point -= needed_points;
  6621. status_calc_pc(sd,SCO_NONE);
  6622. // update increase cost indicator
  6623. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6624. // update statpoint count
  6625. clif_updatestatus(sd, SP_STATUSPOINT);
  6626. // update stat value
  6627. clif_statusupack(sd, type, 1, final_value); // required
  6628. if( final_value > 255 )
  6629. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6630. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6631. return true;
  6632. }
  6633. /**
  6634. * Raises a stat by the specified amount.
  6635. *
  6636. * Obeys max_parameter limits.
  6637. * Does not subtract status points for the cost of the modified stat points.
  6638. *
  6639. * @param sd The target character.
  6640. * @param type The stat to change (see enum _sp)
  6641. * @param val The stat increase (or decrease) amount.
  6642. * @return the stat increase amount.
  6643. * @retval 0 if no changes were made.
  6644. */
  6645. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6646. {
  6647. int max, need;
  6648. nullpo_ret(sd);
  6649. if( type < SP_STR || type > SP_LUK )
  6650. {
  6651. clif_statusupack(sd,type,0,0);
  6652. return 0;
  6653. }
  6654. need = pc_need_status_point(sd,type,1);
  6655. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6656. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6657. status_calc_pc(sd,SCO_NONE);
  6658. // update increase cost indicator
  6659. if( need != pc_need_status_point(sd,type,1) )
  6660. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6661. // update stat value
  6662. clif_statusupack(sd,type,1,val); // required
  6663. if( val > 255 )
  6664. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6665. return val;
  6666. }
  6667. /*==========================================
  6668. * Update skill_lv for player sd
  6669. * Skill point allocation
  6670. *------------------------------------------*/
  6671. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6672. {
  6673. uint16 idx = skill_get_index(skill_id);
  6674. nullpo_retv(sd);
  6675. if (!idx) {
  6676. if (skill_id)
  6677. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6678. return;
  6679. }
  6680. // Level up guild skill
  6681. if (SKILL_CHK_GUILD(skill_id)) {
  6682. guild_skillup(sd, skill_id);
  6683. return;
  6684. }
  6685. // Level up homunculus skill
  6686. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6687. hom_skillup(sd->hd, skill_id);
  6688. return;
  6689. }
  6690. else {
  6691. if( sd->status.skill_point > 0 &&
  6692. sd->status.skill[idx].id &&
  6693. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6694. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6695. {
  6696. int lv, range, upgradable;
  6697. sd->status.skill[idx].lv++;
  6698. sd->status.skill_point--;
  6699. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  6700. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6701. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6702. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6703. else
  6704. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6705. lv = sd->status.skill[idx].lv;
  6706. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6707. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6708. clif_skillup(sd,skill_id,lv,range,upgradable);
  6709. clif_updatestatus(sd,SP_SKILLPOINT);
  6710. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6711. clif_updatestatus(sd,SP_CARTINFO);
  6712. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  6713. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6714. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6715. clif_skillinfoblock(sd);
  6716. }
  6717. //else
  6718. // 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);
  6719. }
  6720. }
  6721. /*==========================================
  6722. * /allskill
  6723. *------------------------------------------*/
  6724. int pc_allskillup(struct map_session_data *sd)
  6725. {
  6726. int i;
  6727. nullpo_ret(sd);
  6728. for (i = 0; i < MAX_SKILL; i++) {
  6729. 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) {
  6730. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6731. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6732. if (sd->status.skill[i].lv == 0)
  6733. sd->status.skill[i].id = 0;
  6734. }
  6735. }
  6736. if (!pc_grant_allskills(sd, true)) {
  6737. uint16 sk_id;
  6738. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6739. uint16 sk_idx = skill_get_index(sk_id);
  6740. if (sk_id == 0 || sk_idx == 0)
  6741. continue;
  6742. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6743. if (
  6744. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6745. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6746. sk_id == SG_DEVIL
  6747. )
  6748. continue; //Cannot be learned normally.
  6749. sd->status.skill[sk_idx].id = sk_id;
  6750. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6751. }
  6752. }
  6753. status_calc_pc(sd,SCO_NONE);
  6754. //Required because if you could level up all skills previously,
  6755. //the update will not be sent as only the lv variable changes.
  6756. clif_skillinfoblock(sd);
  6757. return 0;
  6758. }
  6759. /*==========================================
  6760. * /resetlvl
  6761. *------------------------------------------*/
  6762. int pc_resetlvl(struct map_session_data* sd,int type)
  6763. {
  6764. int i;
  6765. nullpo_ret(sd);
  6766. if (type != 3) //Also reset skills
  6767. pc_resetskill(sd, 0);
  6768. if(type == 1){
  6769. sd->status.skill_point=0;
  6770. sd->status.base_level=1;
  6771. sd->status.job_level=1;
  6772. sd->status.base_exp=0;
  6773. sd->status.job_exp=0;
  6774. if(sd->sc.option !=0)
  6775. sd->sc.option = 0;
  6776. sd->status.str=1;
  6777. sd->status.agi=1;
  6778. sd->status.vit=1;
  6779. sd->status.int_=1;
  6780. sd->status.dex=1;
  6781. sd->status.luk=1;
  6782. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6783. sd->status.status_point=100; // not 88 [celest]
  6784. // give platinum skills upon changing
  6785. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6786. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6787. }
  6788. }
  6789. if(type == 2){
  6790. sd->status.skill_point=0;
  6791. sd->status.base_level=1;
  6792. sd->status.job_level=1;
  6793. sd->status.base_exp=0;
  6794. sd->status.job_exp=0;
  6795. }
  6796. if(type == 3){
  6797. sd->status.base_level=1;
  6798. sd->status.base_exp=0;
  6799. }
  6800. if(type == 4){
  6801. sd->status.job_level=1;
  6802. sd->status.job_exp=0;
  6803. }
  6804. clif_updatestatus(sd,SP_STATUSPOINT);
  6805. clif_updatestatus(sd,SP_STR);
  6806. clif_updatestatus(sd,SP_AGI);
  6807. clif_updatestatus(sd,SP_VIT);
  6808. clif_updatestatus(sd,SP_INT);
  6809. clif_updatestatus(sd,SP_DEX);
  6810. clif_updatestatus(sd,SP_LUK);
  6811. clif_updatestatus(sd,SP_BASELEVEL);
  6812. clif_updatestatus(sd,SP_JOBLEVEL);
  6813. clif_updatestatus(sd,SP_STATUSPOINT);
  6814. clif_updatestatus(sd,SP_BASEEXP);
  6815. clif_updatestatus(sd,SP_JOBEXP);
  6816. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6817. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6818. clif_updatestatus(sd,SP_SKILLPOINT);
  6819. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6820. clif_updatestatus(sd,SP_UAGI);
  6821. clif_updatestatus(sd,SP_UVIT);
  6822. clif_updatestatus(sd,SP_UINT);
  6823. clif_updatestatus(sd,SP_UDEX);
  6824. clif_updatestatus(sd,SP_ULUK); // End Addition
  6825. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6826. if(sd->equip_index[i] >= 0)
  6827. if(pc_isequip(sd,sd->equip_index[i]))
  6828. pc_unequipitem(sd,sd->equip_index[i],2);
  6829. }
  6830. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6831. party_send_levelup(sd);
  6832. status_calc_pc(sd, SCO_FORCE);
  6833. clif_skillinfoblock(sd);
  6834. return 0;
  6835. }
  6836. /*==========================================
  6837. * /resetstate
  6838. *------------------------------------------*/
  6839. int pc_resetstate(struct map_session_data* sd)
  6840. {
  6841. nullpo_ret(sd);
  6842. if (battle_config.use_statpoint_table)
  6843. { // New statpoint table used here - Dexity
  6844. if (sd->status.base_level > MAX_LEVEL)
  6845. { //statp[] goes out of bounds, can't reset!
  6846. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6847. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6848. return 0;
  6849. }
  6850. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6851. }
  6852. else
  6853. {
  6854. int add=0;
  6855. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6856. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6857. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6858. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6859. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6860. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6861. sd->status.status_point+=add;
  6862. }
  6863. pc_setstat(sd, SP_STR, 1);
  6864. pc_setstat(sd, SP_AGI, 1);
  6865. pc_setstat(sd, SP_VIT, 1);
  6866. pc_setstat(sd, SP_INT, 1);
  6867. pc_setstat(sd, SP_DEX, 1);
  6868. pc_setstat(sd, SP_LUK, 1);
  6869. clif_updatestatus(sd,SP_STR);
  6870. clif_updatestatus(sd,SP_AGI);
  6871. clif_updatestatus(sd,SP_VIT);
  6872. clif_updatestatus(sd,SP_INT);
  6873. clif_updatestatus(sd,SP_DEX);
  6874. clif_updatestatus(sd,SP_LUK);
  6875. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6876. clif_updatestatus(sd,SP_UAGI);
  6877. clif_updatestatus(sd,SP_UVIT);
  6878. clif_updatestatus(sd,SP_UINT);
  6879. clif_updatestatus(sd,SP_UDEX);
  6880. clif_updatestatus(sd,SP_ULUK); // End Addition
  6881. clif_updatestatus(sd,SP_STATUSPOINT);
  6882. if( sd->mission_mobid ) { //bugreport:2200
  6883. sd->mission_mobid = 0;
  6884. sd->mission_count = 0;
  6885. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6886. }
  6887. status_calc_pc(sd, SCO_NONE);
  6888. return 1;
  6889. }
  6890. /*==========================================
  6891. * /resetskill
  6892. * if flag&1, perform block resync and status_calc call.
  6893. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6894. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6895. *------------------------------------------*/
  6896. int pc_resetskill(struct map_session_data* sd, int flag)
  6897. {
  6898. int i, skill_point=0;
  6899. nullpo_ret(sd);
  6900. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6901. return 0;
  6902. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6903. /**
  6904. * 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)
  6905. **/
  6906. if( pc_is_taekwon_ranker(sd) )
  6907. return 0;
  6908. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6909. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6910. i = sd->sc.option;
  6911. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6912. i &= ~OPTION_RIDING;
  6913. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6914. i &= ~OPTION_FALCON;
  6915. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6916. i &= ~OPTION_DRAGON;
  6917. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6918. i &= ~OPTION_WUG;
  6919. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6920. i &= ~OPTION_WUGRIDER;
  6921. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6922. i &= ~OPTION_MADOGEAR;
  6923. #ifndef NEW_CARTS
  6924. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6925. i &= ~OPTION_CART;
  6926. #else
  6927. if( sd->sc.data[SC_PUSH_CART] )
  6928. pc_setcart(sd, 0);
  6929. #endif
  6930. if( i != sd->sc.option )
  6931. pc_setoption(sd, i);
  6932. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6933. hom_vaporize(sd, HOM_ST_ACTIVE);
  6934. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6935. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6936. }
  6937. for (const auto &skill : skill_db) {
  6938. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6939. uint8 lv = sd->status.skill[idx].lv;
  6940. if (lv == 0 || skill_id == 0)
  6941. continue;
  6942. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6943. continue;
  6944. // Don't reset trick dead if not a novice/baby
  6945. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6946. {
  6947. sd->status.skill[idx].lv = 0;
  6948. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6949. continue;
  6950. }
  6951. // do not reset basic skill
  6952. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6953. continue;
  6954. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6955. continue;
  6956. if( flag&4 && !skill_ischangesex(skill_id) )
  6957. continue;
  6958. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6959. { //Only handle quest skills in a special way when you can't learn them manually
  6960. if( battle_config.quest_skill_reset && !(flag&2) )
  6961. { //Wipe them
  6962. sd->status.skill[idx].lv = 0;
  6963. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6964. }
  6965. continue;
  6966. }
  6967. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6968. skill_point += lv;
  6969. else
  6970. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6971. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6972. if( !(flag&2) )
  6973. {// reset
  6974. sd->status.skill[idx].lv = 0;
  6975. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6976. }
  6977. }
  6978. if( flag&2 || !skill_point ) return skill_point;
  6979. sd->status.skill_point += skill_point;
  6980. if (flag&1) {
  6981. clif_updatestatus(sd,SP_SKILLPOINT);
  6982. clif_skillinfoblock(sd);
  6983. status_calc_pc(sd, SCO_FORCE);
  6984. }
  6985. return skill_point;
  6986. }
  6987. /*==========================================
  6988. * /resetfeel [Komurka]
  6989. *------------------------------------------*/
  6990. int pc_resetfeel(struct map_session_data* sd)
  6991. {
  6992. int i;
  6993. nullpo_ret(sd);
  6994. for (i=0; i<MAX_PC_FEELHATE; i++)
  6995. {
  6996. sd->feel_map[i].m = -1;
  6997. sd->feel_map[i].index = 0;
  6998. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6999. }
  7000. return 0;
  7001. }
  7002. int pc_resethate(struct map_session_data* sd)
  7003. {
  7004. int i;
  7005. nullpo_ret(sd);
  7006. for (i=0; i<MAX_PC_FEELHATE; i++)
  7007. {
  7008. sd->hate_mob[i] = -1;
  7009. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7010. }
  7011. return 0;
  7012. }
  7013. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7014. {
  7015. int bonus = 0;
  7016. nullpo_ret(sd);
  7017. skill_id = skill_dummy2skill_id(skill_id);
  7018. for (auto &it : sd->skillatk) {
  7019. if (it.id == skill_id) {
  7020. bonus += it.val;
  7021. break;
  7022. }
  7023. }
  7024. return bonus;
  7025. }
  7026. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7027. {
  7028. int bonus = 0;
  7029. nullpo_ret(sd);
  7030. skill_id = skill_dummy2skill_id(skill_id);
  7031. for (auto &it : sd->subskill) {
  7032. if (it.id == skill_id) {
  7033. bonus += it.val;
  7034. break;
  7035. }
  7036. }
  7037. return bonus;
  7038. }
  7039. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7040. int bonus = sd->bonus.add_heal_rate;
  7041. nullpo_ret(sd);
  7042. skill_id = skill_dummy2skill_id(skill_id);
  7043. if( bonus ) {
  7044. switch( skill_id ) {
  7045. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7046. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7047. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7048. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7049. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7050. }
  7051. }
  7052. for (auto &it : sd->skillheal) {
  7053. if (it.id == skill_id) {
  7054. bonus += it.val;
  7055. break;
  7056. }
  7057. }
  7058. return bonus;
  7059. }
  7060. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7061. int bonus = sd->bonus.add_heal2_rate;
  7062. skill_id = skill_dummy2skill_id(skill_id);
  7063. for (auto &it : sd->skillheal2) {
  7064. if (it.id == skill_id) {
  7065. bonus += it.val;
  7066. break;
  7067. }
  7068. }
  7069. return bonus;
  7070. }
  7071. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7072. {
  7073. if( !pc_isdead(sd) )
  7074. return; // not applicable
  7075. if( sd->bg_id && bg_member_respawn(sd) )
  7076. return; // member revived by battleground
  7077. pc_setstand(sd, true);
  7078. pc_setrestartvalue(sd,3);
  7079. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7080. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7081. }
  7082. static TIMER_FUNC(pc_respawn_timer){
  7083. struct map_session_data *sd = map_id2sd(id);
  7084. if( sd != NULL )
  7085. {
  7086. sd->pvp_point=0;
  7087. sd->respawn_tid = INVALID_TIMER;
  7088. pc_respawn(sd,CLR_OUTSIGHT);
  7089. }
  7090. return 0;
  7091. }
  7092. /*==========================================
  7093. * Invoked when a player has received damage
  7094. *------------------------------------------*/
  7095. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  7096. {
  7097. if (sp) clif_updatestatus(sd,SP_SP);
  7098. if (hp) clif_updatestatus(sd,SP_HP);
  7099. else return;
  7100. if (!src)
  7101. return;
  7102. if( pc_issit(sd) ) {
  7103. pc_setstand(sd, true);
  7104. skill_sit(sd,0);
  7105. }
  7106. if (sd->progressbar.npc_id)
  7107. clif_progressbar_abort(sd);
  7108. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7109. pet_target_check(sd->pd,src,1);
  7110. if( sd->status.ele_id > 0 )
  7111. elemental_set_target(sd,src);
  7112. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7113. sd->canlog_tick = gettick();
  7114. }
  7115. TIMER_FUNC(pc_close_npc_timer){
  7116. TBL_PC *sd = map_id2sd(id);
  7117. if(sd) pc_close_npc(sd,data);
  7118. return 0;
  7119. }
  7120. /**
  7121. * Method to properly close a NPC for player and clear anything related.
  7122. * @param sd: Player attached
  7123. * @param flag: Method of closure
  7124. * 1: Produce a close button and end the NPC
  7125. * 2: End the NPC (best for no dialog windows)
  7126. */
  7127. void pc_close_npc(struct map_session_data *sd,int flag)
  7128. {
  7129. nullpo_retv(sd);
  7130. if (sd->npc_id || sd->npc_shopid) {
  7131. if (sd->state.using_fake_npc) {
  7132. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7133. sd->state.using_fake_npc = 0;
  7134. }
  7135. if (sd->st) {
  7136. if(sd->st->state == RUN){ //wait ending code execution
  7137. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7138. return;
  7139. }
  7140. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7141. sd->st->mes_active = 0;
  7142. }
  7143. sd->state.menu_or_input = 0;
  7144. sd->npc_menu = 0;
  7145. sd->npc_shopid = 0;
  7146. #ifdef SECURE_NPCTIMEOUT
  7147. if( sd->npc_idle_timer != INVALID_TIMER ){
  7148. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  7149. sd->npc_idle_timer = INVALID_TIMER;
  7150. }
  7151. #endif
  7152. if (sd->st) {
  7153. if (sd->st->state == CLOSE) {
  7154. clif_scriptclose(sd, sd->npc_id);
  7155. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7156. sd->st->state = END; // Force to end now
  7157. }
  7158. if (sd->st->state == END) { // free attached scripts that are waiting
  7159. script_free_state(sd->st);
  7160. sd->st = NULL;
  7161. sd->npc_id = 0;
  7162. }
  7163. }
  7164. }
  7165. }
  7166. /*==========================================
  7167. * Invoked when a player has negative current hp
  7168. *------------------------------------------*/
  7169. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7170. {
  7171. int i=0,k=0;
  7172. t_tick tick = gettick();
  7173. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7174. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7175. // Super Novices have no kill or die functions attached when saved by their angel
  7176. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7177. t_exp exp = pc_nextbaseexp(sd);
  7178. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  7179. sd->state.snovice_dead_flag = 1;
  7180. pc_setrestartvalue(sd,1);
  7181. status_percent_heal(&sd->bl, 100, 100);
  7182. clif_resurrection(&sd->bl, 1);
  7183. if(battle_config.pc_invincible_time)
  7184. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7185. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7186. if(mapdata_flag_gvg2(mapdata))
  7187. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7188. return 0;
  7189. }
  7190. }
  7191. for(k = 0; k < MAX_DEVOTION; k++) {
  7192. if (sd->devotion[k]){
  7193. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7194. if (devsd)
  7195. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7196. sd->devotion[k] = 0;
  7197. }
  7198. }
  7199. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7200. if (sd->stellar_mark[k]) {
  7201. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7202. if (smarksd)
  7203. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7204. sd->stellar_mark[k] = 0;
  7205. }
  7206. }
  7207. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7208. if (sd->united_soul[k]) {
  7209. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7210. if (usoulsd)
  7211. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7212. sd->united_soul[k] = 0;
  7213. }
  7214. }
  7215. if(sd->shadowform_id) { //if we were target of shadowform
  7216. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7217. sd->shadowform_id = 0; //should be remove on status end anyway
  7218. }
  7219. if(sd->status.pet_id > 0 && sd->pd) {
  7220. struct pet_data *pd = sd->pd;
  7221. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7222. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7223. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7224. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7225. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7226. }
  7227. if( sd->pd->target_id ) // Unlock all targets...
  7228. pet_unlocktarget(sd->pd);
  7229. }
  7230. 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)
  7231. hom_vaporize(sd, HOM_ST_ACTIVE);
  7232. if( sd->md )
  7233. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7234. if( sd->ed )
  7235. elemental_delete(sd->ed);
  7236. // Leave duel if you die [LuzZza]
  7237. if(battle_config.duel_autoleave_when_die) {
  7238. if(sd->duel_group > 0)
  7239. duel_leave(sd->duel_group, sd);
  7240. if(sd->duel_invite > 0)
  7241. duel_reject(sd->duel_invite, sd);
  7242. }
  7243. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  7244. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  7245. sd->skill_keep_using.tid = INVALID_TIMER;
  7246. }
  7247. pc_close_npc(sd,2); //close npc if we were using one
  7248. /* e.g. not killed thru pc_damage */
  7249. if( pc_issit(sd) ) {
  7250. clif_status_load(&sd->bl,EFST_SIT,0);
  7251. }
  7252. pc_setdead(sd);
  7253. clif_party_dead( sd );
  7254. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  7255. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7256. //Reset menu skills/item skills
  7257. if ((sd->skillitem) != 0)
  7258. sd->skillitem = sd->skillitemlv = 0;
  7259. if ((sd->menuskill_id) != 0)
  7260. sd->menuskill_id = sd->menuskill_val = 0;
  7261. //Reset ticks.
  7262. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7263. if ( sd->spiritball !=0 )
  7264. pc_delspiritball(sd,sd->spiritball,0);
  7265. if (sd->soulball != 0)
  7266. pc_delsoulball(sd, sd->soulball, false);
  7267. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7268. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7269. if (src)
  7270. switch (src->type) {
  7271. case BL_MOB:
  7272. {
  7273. struct mob_data *md=(struct mob_data *)src;
  7274. if(md->target_id==sd->bl.id)
  7275. mob_unlocktarget(md,tick);
  7276. if(battle_config.mobs_level_up && md->status.hp &&
  7277. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7278. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7279. ) { // monster level up [Valaris]
  7280. clif_misceffect(&md->bl,0);
  7281. md->level++;
  7282. status_calc_mob(md, SCO_NONE);
  7283. status_percent_heal(src,10,0);
  7284. if( battle_config.show_mob_info&4 )
  7285. {// update name with new level
  7286. clif_name_area(&md->bl);
  7287. }
  7288. }
  7289. src = battle_get_master(src); // Maybe Player Summon
  7290. }
  7291. break;
  7292. case BL_PET: //Pass on to master...
  7293. case BL_HOM:
  7294. case BL_MER:
  7295. src = battle_get_master(src);
  7296. break;
  7297. }
  7298. if (src && src->type == BL_PC) {
  7299. struct map_session_data *ssd = (struct map_session_data *)src;
  7300. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7301. npc_script_event(ssd, NPCE_KILLPC);
  7302. if (battle_config.pk_mode&2) {
  7303. ssd->status.manner -= 5;
  7304. if(ssd->status.manner < 0)
  7305. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7306. #if 0
  7307. // PK/Karma system code (not enabled yet) [celest]
  7308. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7309. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7310. // karma going down = more 'good' / more honourable.
  7311. // The Karma System way...
  7312. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7313. sd->status.karma--;
  7314. ssd->status.karma--;
  7315. }
  7316. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7317. ssd->status.karma++;
  7318. // or the PK System way...
  7319. if (sd->status.karma > 0) // player killed is dishonourable?
  7320. ssd->status.karma--; // honour points earned
  7321. sd->status.karma++; // honour points lost
  7322. // To-do: Receive exp on certain occasions
  7323. #endif
  7324. }
  7325. }
  7326. if(battle_config.bone_drop==2
  7327. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7328. {
  7329. struct item item_tmp;
  7330. memset(&item_tmp,0,sizeof(item_tmp));
  7331. item_tmp.nameid=ITEMID_SKULL_;
  7332. item_tmp.identify=1;
  7333. item_tmp.card[0]=CARD0_CREATE;
  7334. item_tmp.card[1]=0;
  7335. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7336. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7337. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7338. }
  7339. //Remove bonus_script when dead
  7340. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7341. // changed penalty options, added death by player if pk_mode [Valaris]
  7342. if(battle_config.death_penalty_type
  7343. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7344. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7345. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7346. {
  7347. t_exp base_penalty = 0;
  7348. t_exp job_penalty = 0;
  7349. uint32 zeny_penalty = 0;
  7350. if (pc_isvip(sd)) { // EXP penalty for VIP
  7351. base_penalty = battle_config.vip_exp_penalty_base;
  7352. job_penalty = battle_config.vip_exp_penalty_job;
  7353. zeny_penalty = battle_config.vip_zeny_penalty;
  7354. } else {
  7355. base_penalty = battle_config.death_penalty_base;
  7356. job_penalty = battle_config.death_penalty_job;
  7357. zeny_penalty = battle_config.zeny_penalty;
  7358. }
  7359. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7360. switch (battle_config.death_penalty_type) {
  7361. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7362. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7363. }
  7364. if (base_penalty){ //recheck after altering to speedup
  7365. if (battle_config.pk_mode && src && src->type==BL_PC)
  7366. base_penalty *= 2;
  7367. base_penalty = u64min(sd->status.base_exp, base_penalty);
  7368. }
  7369. }
  7370. else
  7371. base_penalty = 0;
  7372. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7373. switch (battle_config.death_penalty_type) {
  7374. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7375. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7376. }
  7377. if (job_penalty) {
  7378. if (battle_config.pk_mode && src && src->type==BL_PC)
  7379. job_penalty *= 2;
  7380. job_penalty = u64min(sd->status.job_exp, job_penalty);
  7381. }
  7382. }
  7383. else
  7384. job_penalty = 0;
  7385. if (base_penalty || job_penalty)
  7386. pc_lostexp(sd, base_penalty, job_penalty);
  7387. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7388. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7389. if(zeny_penalty)
  7390. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7391. }
  7392. }
  7393. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7394. for (const auto &it : mapdata->drop_list) {
  7395. int id = it.drop_id, per = it.drop_per;
  7396. enum e_nightmare_drop_type type = it.drop_type;
  7397. if(id == 0)
  7398. continue;
  7399. if(id == -1){
  7400. int eq_num=0,eq_n[MAX_INVENTORY];
  7401. memset(eq_n,0,sizeof(eq_n));
  7402. for(i=0;i<MAX_INVENTORY;i++) {
  7403. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7404. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7405. || type&NMDT_ALL)
  7406. {
  7407. int l;
  7408. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7409. if( l < MAX_INVENTORY )
  7410. eq_n[l] = i;
  7411. eq_num++;
  7412. }
  7413. }
  7414. if(eq_num > 0){
  7415. int n = eq_n[rnd()%eq_num];
  7416. if(rnd()%10000 < per) {
  7417. if(sd->inventory.u.items_inventory[n].equip)
  7418. pc_unequipitem(sd,n,3);
  7419. pc_dropitem(sd,n,1);
  7420. }
  7421. }
  7422. }
  7423. else if(id > 0) {
  7424. for(i=0;i<MAX_INVENTORY;i++){
  7425. if(sd->inventory.u.items_inventory[i].nameid == id
  7426. && rnd()%10000 < per
  7427. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7428. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7429. || type&NMDT_ALL) ){
  7430. if(sd->inventory.u.items_inventory[i].equip)
  7431. pc_unequipitem(sd,i,3);
  7432. pc_dropitem(sd,i,1);
  7433. break;
  7434. }
  7435. }
  7436. }
  7437. }
  7438. }
  7439. // pvp
  7440. // disable certain pvp functions on pk_mode [Valaris]
  7441. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7442. sd->pvp_point -= 5;
  7443. sd->pvp_lost++;
  7444. if( src && src->type == BL_PC ) {
  7445. struct map_session_data *ssd = (struct map_session_data *)src;
  7446. ssd->pvp_point++;
  7447. ssd->pvp_won++;
  7448. }
  7449. if( sd->pvp_point < 0 ) {
  7450. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7451. return 1|8;
  7452. }
  7453. }
  7454. //GvG
  7455. if( mapdata_flag_gvg2(mapdata) ) {
  7456. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7457. return 1|8;
  7458. }
  7459. else if( sd->bg_id ) {
  7460. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7461. if (bg) {
  7462. if (bg->cemetery.map > 0) { // Respawn by BG
  7463. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7464. return 1|8;
  7465. }
  7466. }
  7467. }
  7468. //Reset "can log out" tick.
  7469. if( battle_config.prevent_logout )
  7470. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7471. return 1;
  7472. }
  7473. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7474. if(hp) clif_updatestatus(sd,SP_HP);
  7475. if(sp) clif_updatestatus(sd,SP_SP);
  7476. pc_setstand(sd, true);
  7477. if(battle_config.pc_invincible_time > 0)
  7478. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7479. if( sd->state.gmaster_flag ) {
  7480. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7481. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7482. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7483. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7484. }
  7485. }
  7486. bool pc_revive_item(struct map_session_data *sd) {
  7487. nullpo_retr(false, sd);
  7488. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7489. return false;
  7490. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7491. return false;
  7492. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7493. uint8 hp = 100, sp = 100;
  7494. if (item_position < 0) {
  7495. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7496. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7497. sp = 0;
  7498. }
  7499. else
  7500. return false;
  7501. }
  7502. if (!status_revive(&sd->bl, hp, sp))
  7503. return false;
  7504. if (item_position < 0)
  7505. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7506. else
  7507. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7508. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7509. return true;
  7510. }
  7511. // script
  7512. //
  7513. /*==========================================
  7514. * script reading pc status registry
  7515. *------------------------------------------*/
  7516. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7517. {
  7518. int64 val = 0;
  7519. nullpo_ret(sd);
  7520. switch(type) {
  7521. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7522. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7523. case SP_ZENY: val = sd->status.zeny; break;
  7524. case SP_BASELEVEL: val = sd->status.base_level; break;
  7525. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7526. case SP_CLASS: val = sd->status.class_; break;
  7527. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7528. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7529. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7530. case SP_SEX: val = sd->status.sex; break;
  7531. case SP_WEIGHT: val = sd->weight; break;
  7532. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7533. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  7534. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  7535. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  7536. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  7537. case SP_HP: val = sd->battle_status.hp; break;
  7538. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7539. case SP_SP: val = sd->battle_status.sp; break;
  7540. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7541. case SP_STR: val = sd->status.str; break;
  7542. case SP_AGI: val = sd->status.agi; break;
  7543. case SP_VIT: val = sd->status.vit; break;
  7544. case SP_INT: val = sd->status.int_; break;
  7545. case SP_DEX: val = sd->status.dex; break;
  7546. case SP_LUK: val = sd->status.luk; break;
  7547. case SP_KARMA: val = sd->status.karma; break;
  7548. case SP_MANNER: val = sd->status.manner; break;
  7549. case SP_FAME: val = sd->status.fame; break;
  7550. case SP_KILLERRID: val = sd->killerrid; break;
  7551. case SP_KILLEDRID: val = sd->killedrid; break;
  7552. case SP_KILLEDGID: val = sd->killedgid; break;
  7553. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7554. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7555. case SP_CHARRENAME: val = sd->status.rename; break;
  7556. case SP_CHARFONT: val = sd->status.font; break;
  7557. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7558. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7559. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7560. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7561. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7562. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7563. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7564. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7565. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7566. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7567. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7568. case SP_BASE_ATK:
  7569. #ifdef RENEWAL
  7570. val = sd->bonus.eatk;
  7571. #else
  7572. val = sd->battle_status.batk;
  7573. #endif
  7574. break;
  7575. case SP_DEF1: val = sd->battle_status.def; break;
  7576. case SP_DEF2: val = sd->battle_status.def2; break;
  7577. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7578. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7579. case SP_HIT: val = sd->battle_status.hit; break;
  7580. case SP_FLEE1: val = sd->battle_status.flee; break;
  7581. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7582. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7583. case SP_MAXHPRATE: val = sd->hprate; break;
  7584. case SP_MAXSPRATE: val = sd->sprate; break;
  7585. case SP_SPRATE: val = sd->dsprate; break;
  7586. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7587. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7588. case SP_ASPD_RATE:
  7589. #ifndef RENEWAL_ASPD
  7590. val = sd->battle_status.aspd_rate;
  7591. #else
  7592. val = sd->battle_status.aspd_rate2;
  7593. #endif
  7594. break;
  7595. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7596. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7597. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7598. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7599. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7600. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7601. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7602. case SP_MATK_RATE: val = sd->matk_rate; break;
  7603. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7604. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7605. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7606. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7607. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7608. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7609. case SP_HIT_RATE: val = sd->hit_rate; break;
  7610. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7611. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7612. case SP_DEF_RATE: val = sd->def_rate; break;
  7613. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7614. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7615. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7616. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7617. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7618. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7619. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7620. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7621. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7622. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7623. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7624. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7625. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7626. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7627. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7628. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7629. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7630. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7631. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7632. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7633. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7634. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7635. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7636. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7637. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7638. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7639. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7640. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7641. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7642. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  7643. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7644. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7645. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7646. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7647. case SP_DELAYRATE: val = sd->delayrate; break;
  7648. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7649. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7650. case SP_UNSTRIPABLE:
  7651. case SP_UNSTRIPABLE_ARMOR:
  7652. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7653. break;
  7654. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7655. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7656. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7657. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7658. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7659. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7660. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7661. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7662. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7663. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7664. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7665. case SP_EMATK: val = sd->bonus.ematk; break;
  7666. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7667. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7668. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7669. case SP_CASTRATE:
  7670. case SP_VARCASTRATE:
  7671. #ifdef RENEWAL_CAST
  7672. val = sd->bonus.varcastrate; break;
  7673. #else
  7674. val = sd->castrate; break;
  7675. #endif
  7676. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  7677. default:
  7678. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7679. return -1;
  7680. }
  7681. return val;
  7682. }
  7683. /*==========================================
  7684. * script set pc status registry
  7685. *------------------------------------------*/
  7686. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7687. {
  7688. nullpo_retr(false,sd);
  7689. int val = static_cast<unsigned int>(val_tmp);
  7690. switch(type){
  7691. case SP_BASELEVEL:
  7692. if (val > pc_maxbaselv(sd)) //Capping to max
  7693. val = pc_maxbaselv(sd);
  7694. if (val > sd->status.base_level) {
  7695. int i = 0;
  7696. int stat=0;
  7697. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7698. stat += pc_gets_status_point(sd->status.base_level + i);
  7699. sd->status.status_point += stat;
  7700. }
  7701. sd->status.base_level = val;
  7702. sd->status.base_exp = 0;
  7703. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7704. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7705. clif_updatestatus(sd, SP_STATUSPOINT);
  7706. clif_updatestatus(sd, SP_BASEEXP);
  7707. status_calc_pc(sd, SCO_FORCE);
  7708. if(sd->status.party_id)
  7709. party_send_levelup(sd);
  7710. break;
  7711. case SP_JOBLEVEL:
  7712. if (val >= sd->status.job_level) {
  7713. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7714. sd->status.skill_point += val - sd->status.job_level;
  7715. clif_updatestatus(sd, SP_SKILLPOINT);
  7716. }
  7717. sd->status.job_level = val;
  7718. sd->status.job_exp = 0;
  7719. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7720. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7721. clif_updatestatus(sd, SP_JOBEXP);
  7722. status_calc_pc(sd, SCO_FORCE);
  7723. break;
  7724. case SP_SKILLPOINT:
  7725. sd->status.skill_point = val;
  7726. break;
  7727. case SP_STATUSPOINT:
  7728. sd->status.status_point = val;
  7729. break;
  7730. case SP_ZENY:
  7731. if( val < 0 )
  7732. return false;// can't set negative zeny
  7733. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7734. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7735. break;
  7736. case SP_BASEEXP:
  7737. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  7738. if (val_tmp < sd->status.base_exp) // Lost
  7739. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  7740. else // Gained
  7741. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  7742. return true;
  7743. case SP_JOBEXP:
  7744. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  7745. if (val_tmp < sd->status.job_exp) // Lost
  7746. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  7747. else // Gained
  7748. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  7749. return true;
  7750. case SP_SEX:
  7751. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7752. break;
  7753. case SP_WEIGHT:
  7754. sd->weight = val;
  7755. break;
  7756. case SP_MAXWEIGHT:
  7757. sd->max_weight = val;
  7758. break;
  7759. case SP_HP:
  7760. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7761. break;
  7762. case SP_MAXHP:
  7763. if (sd->status.base_level < 100)
  7764. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7765. else if (sd->status.base_level < 151)
  7766. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7767. else
  7768. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7769. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7770. {
  7771. sd->battle_status.hp = sd->battle_status.max_hp;
  7772. clif_updatestatus(sd, SP_HP);
  7773. }
  7774. break;
  7775. case SP_SP:
  7776. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7777. break;
  7778. case SP_MAXSP:
  7779. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7780. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7781. {
  7782. sd->battle_status.sp = sd->battle_status.max_sp;
  7783. clif_updatestatus(sd, SP_SP);
  7784. }
  7785. break;
  7786. case SP_STR:
  7787. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7788. break;
  7789. case SP_AGI:
  7790. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7791. break;
  7792. case SP_VIT:
  7793. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7794. break;
  7795. case SP_INT:
  7796. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7797. break;
  7798. case SP_DEX:
  7799. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7800. break;
  7801. case SP_LUK:
  7802. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7803. break;
  7804. case SP_KARMA:
  7805. sd->status.karma = val;
  7806. break;
  7807. case SP_MANNER:
  7808. sd->status.manner = val;
  7809. if( val < 0 )
  7810. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7811. else {
  7812. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7813. clif_manner_message(sd, 5);
  7814. }
  7815. return true; // status_change_start/status_change_end already sends packets warning the client
  7816. case SP_FAME:
  7817. sd->status.fame = val;
  7818. break;
  7819. case SP_KILLERRID:
  7820. sd->killerrid = val;
  7821. return true;
  7822. case SP_KILLEDRID:
  7823. sd->killedrid = val;
  7824. return true;
  7825. case SP_KILLEDGID:
  7826. sd->killedgid = val;
  7827. return true;
  7828. case SP_CHARMOVE:
  7829. sd->status.character_moves = val;
  7830. return true;
  7831. case SP_CHARRENAME:
  7832. sd->status.rename = val;
  7833. return true;
  7834. case SP_CHARFONT:
  7835. sd->status.font = val;
  7836. clif_font(sd);
  7837. return true;
  7838. case SP_BANK_VAULT:
  7839. if (val < 0)
  7840. return false;
  7841. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7842. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7843. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7844. return true;
  7845. case SP_ROULETTE_BRONZE:
  7846. sd->roulette_point.bronze = val;
  7847. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7848. return true;
  7849. case SP_ROULETTE_SILVER:
  7850. sd->roulette_point.silver = val;
  7851. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7852. return true;
  7853. case SP_ROULETTE_GOLD:
  7854. sd->roulette_point.gold = val;
  7855. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7856. return true;
  7857. case SP_CASHPOINTS:
  7858. if (val < 0)
  7859. return false;
  7860. if (!sd->state.connect_new)
  7861. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7862. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7863. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7864. return true;
  7865. case SP_KAFRAPOINTS:
  7866. if (val < 0)
  7867. return false;
  7868. if (!sd->state.connect_new)
  7869. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7870. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7871. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7872. return true;
  7873. case SP_PCDIECOUNTER:
  7874. if (val < 0)
  7875. return false;
  7876. if (sd->die_counter == val)
  7877. return true;
  7878. sd->die_counter = val;
  7879. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7880. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7881. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7882. return true;
  7883. case SP_COOKMASTERY:
  7884. if (val < 0)
  7885. return false;
  7886. if (sd->cook_mastery == val)
  7887. return true;
  7888. val = cap_value(val, 0, 1999);
  7889. sd->cook_mastery = val;
  7890. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7891. return true;
  7892. default:
  7893. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7894. return false;
  7895. }
  7896. clif_updatestatus(sd,static_cast<int>(type));
  7897. return true;
  7898. }
  7899. /*==========================================
  7900. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7901. *------------------------------------------*/
  7902. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7903. {
  7904. if (type&2) {
  7905. if (hp || type&4)
  7906. clif_heal(sd->fd,SP_HP,hp);
  7907. if (sp)
  7908. clif_heal(sd->fd,SP_SP,sp);
  7909. } else {
  7910. if(hp)
  7911. clif_updatestatus(sd,SP_HP);
  7912. if(sp)
  7913. clif_updatestatus(sd,SP_SP);
  7914. }
  7915. return;
  7916. }
  7917. /**
  7918. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7919. * @param sd: Player data
  7920. * @param itemid: Item ID
  7921. * @param hp: HP to heal
  7922. * @param sp: SP to heal
  7923. * @return Amount healed to an object
  7924. */
  7925. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  7926. {
  7927. int bonus, tmp, penalty = 0;
  7928. if (hp) {
  7929. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7930. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7931. if (potion_flag == 2) {
  7932. bonus += bonus * 50 / 100;
  7933. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7934. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  7935. }
  7936. //All item bonuses.
  7937. bonus += sd->bonus.itemhealrate2;
  7938. //Item Group bonuses
  7939. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid) / 100;
  7940. //Individual item bonuses.
  7941. for(const auto &it : sd->itemhealrate) {
  7942. if (it.id == itemid) {
  7943. bonus += bonus * it.val / 100;
  7944. break;
  7945. }
  7946. }
  7947. // Recovery Potion
  7948. if (sd->sc.data[SC_INCHEALRATE])
  7949. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  7950. // 2014 Halloween Event : Pumpkin Bonus
  7951. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7952. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  7953. tmp = hp * bonus / 100; // Overflow check
  7954. if (bonus != 100 && tmp > hp)
  7955. hp = tmp;
  7956. }
  7957. if (sp) {
  7958. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7959. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7960. if (potion_flag == 2)
  7961. bonus += bonus * 50 / 100;
  7962. tmp = sp * bonus / 100; // Overflow check
  7963. if (bonus != 100 && tmp > sp)
  7964. sp = tmp;
  7965. }
  7966. if (sd->sc.count) {
  7967. // Critical Wound and Death Hurt stack
  7968. if (sd->sc.data[SC_CRITICALWOUND])
  7969. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7970. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  7971. penalty += 20;
  7972. if (sd->sc.data[SC_NORECOVER_STATE])
  7973. penalty = 100;
  7974. if (sd->sc.data[SC_VITALITYACTIVATION])
  7975. hp += hp / 2; // 1.5 times
  7976. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7977. hp += hp / 10;
  7978. sp += sp / 10;
  7979. }
  7980. #ifdef RENEWAL
  7981. if (sd->sc.data[SC_APPLEIDUN])
  7982. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  7983. #endif
  7984. if (penalty > 0) {
  7985. hp -= hp * penalty / 100;
  7986. sp -= sp * penalty / 100;
  7987. }
  7988. #ifdef RENEWAL
  7989. if (sd->sc.data[SC_EXTREMITYFIST2])
  7990. sp = 0;
  7991. #endif
  7992. if (sd->sc.data[SC_BITESCAR])
  7993. hp = 0;
  7994. }
  7995. return status_heal(&sd->bl, hp, sp, 1);
  7996. }
  7997. /*==========================================
  7998. * HP/SP Recovery
  7999. * Heal player hp nad/or sp by rate
  8000. *------------------------------------------*/
  8001. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8002. {
  8003. nullpo_ret(sd);
  8004. if (hp > 100) hp = 100;
  8005. else if (hp <-100) hp = -100;
  8006. if (sp > 100) sp = 100;
  8007. else if (sp <-100) sp = -100;
  8008. if(hp >= 0 && sp >= 0) //Heal
  8009. return status_percent_heal(&sd->bl, hp, sp);
  8010. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8011. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8012. //Crossed signs
  8013. if(hp) {
  8014. if(hp > 0)
  8015. status_percent_heal(&sd->bl, hp, 0);
  8016. else
  8017. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8018. }
  8019. if(sp) {
  8020. if(sp > 0)
  8021. status_percent_heal(&sd->bl, 0, sp);
  8022. else
  8023. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8024. }
  8025. return 0;
  8026. }
  8027. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8028. {
  8029. struct mob_data *md;
  8030. int flag;
  8031. md = (struct mob_data *)bl;
  8032. nullpo_ret(md);
  8033. flag = va_arg(ap, int);
  8034. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8035. status_kill(&md->bl);
  8036. return 1;
  8037. }
  8038. /**
  8039. * Called when player changes job
  8040. * Rewrote to make it tidider [Celest]
  8041. * @param sd
  8042. * @param job JOB ID. See enum e_job
  8043. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8044. * @return True if success, false if failed
  8045. **/
  8046. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8047. {
  8048. int i, fame_flag = 0;
  8049. int b_class;
  8050. nullpo_retr(false,sd);
  8051. if (job < 0)
  8052. return false;
  8053. //Normalize job.
  8054. b_class = pc_jobid2mapid(job);
  8055. if (b_class == -1)
  8056. return false;
  8057. switch (upper) {
  8058. case 1:
  8059. b_class|= JOBL_UPPER;
  8060. break;
  8061. case 2:
  8062. b_class|= JOBL_BABY;
  8063. break;
  8064. }
  8065. //This will automatically adjust bard/dancer classes to the correct gender
  8066. //That is, if you try to jobchange into dancer, it will turn you to bard.
  8067. job = pc_mapid2jobid(b_class, sd->status.sex);
  8068. if (job == -1)
  8069. return false;
  8070. if ((unsigned short)b_class == sd->class_)
  8071. return false; //Nothing to change.
  8072. // changing from 1st to 2nd job
  8073. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  8074. sd->change_level_2nd = sd->status.job_level;
  8075. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  8076. }
  8077. // changing from 2nd to 3rd job
  8078. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  8079. sd->change_level_3rd = sd->status.job_level;
  8080. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  8081. }
  8082. if(sd->cloneskill_idx > 0) {
  8083. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8084. sd->status.skill[sd->cloneskill_idx].id = 0;
  8085. sd->status.skill[sd->cloneskill_idx].lv = 0;
  8086. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  8087. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  8088. }
  8089. sd->cloneskill_idx = 0;
  8090. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  8091. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  8092. }
  8093. if(sd->reproduceskill_idx > 0) {
  8094. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8095. sd->status.skill[sd->reproduceskill_idx].id = 0;
  8096. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  8097. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  8098. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  8099. }
  8100. sd->reproduceskill_idx = 0;
  8101. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8102. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8103. }
  8104. // Give or reduce transcendent status points
  8105. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8106. sd->status.status_point += battle_config.transcendent_status_points;
  8107. clif_updatestatus(sd,SP_STATUSPOINT);
  8108. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8109. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8110. // The player already used his bonus points, so we have to reset his status points
  8111. pc_resetstate(sd);
  8112. }
  8113. sd->status.status_point -= battle_config.transcendent_status_points;
  8114. clif_updatestatus(sd,SP_STATUSPOINT);
  8115. }
  8116. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8117. const int class_ = pc_class2idx(sd->status.class_);
  8118. uint16 skill_id;
  8119. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  8120. //Remove status specific to your current tree skills.
  8121. enum sc_type sc = status_skill2sc(skill_id);
  8122. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  8123. status_change_end(&sd->bl, sc, INVALID_TIMER);
  8124. }
  8125. }
  8126. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8127. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  8128. pc_resetfeel(sd);
  8129. pc_resethate(sd);
  8130. }
  8131. // Reset body style to 0 before changing job to avoid
  8132. // errors since not every job has a alternate outfit.
  8133. sd->status.body = 0;
  8134. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8135. sd->status.class_ = job;
  8136. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8137. sd->class_ = (unsigned short)b_class;
  8138. sd->status.job_level=1;
  8139. sd->status.job_exp=0;
  8140. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8141. sd->status.base_level = pc_maxbaselv(sd);
  8142. sd->status.base_exp=0;
  8143. pc_resetstate(sd);
  8144. clif_updatestatus(sd,SP_STATUSPOINT);
  8145. clif_updatestatus(sd,SP_BASELEVEL);
  8146. clif_updatestatus(sd,SP_BASEEXP);
  8147. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8148. }
  8149. clif_updatestatus(sd,SP_JOBLEVEL);
  8150. clif_updatestatus(sd,SP_JOBEXP);
  8151. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8152. for(i=0;i<EQI_MAX;i++) {
  8153. if(sd->equip_index[i] >= 0)
  8154. if(pc_isequip(sd,sd->equip_index[i]))
  8155. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8156. }
  8157. //Change look, if disguised, you need to undisguise
  8158. //to correctly calculate new job sprite without
  8159. if (sd->disguise)
  8160. pc_disguise(sd, 0);
  8161. status_set_viewdata(&sd->bl, job);
  8162. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8163. #if PACKETVER >= 20151001
  8164. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8165. #endif
  8166. if(sd->vd.cloth_color)
  8167. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8168. /*
  8169. if(sd->vd.body_style)
  8170. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8171. */
  8172. //Update skill tree.
  8173. pc_calc_skilltree(sd);
  8174. clif_skillinfoblock(sd);
  8175. if (sd->ed)
  8176. elemental_delete(sd->ed);
  8177. if (sd->state.vending)
  8178. vending_closevending(sd);
  8179. if (sd->state.buyingstore)
  8180. buyingstore_close(sd);
  8181. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8182. //Remove peco/cart/falcon
  8183. i = sd->sc.option;
  8184. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8185. i&=~OPTION_RIDING;
  8186. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8187. i&=~OPTION_FALCON;
  8188. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8189. i&=~OPTION_DRAGON;
  8190. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8191. i&=~OPTION_WUGRIDER;
  8192. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8193. i&=~OPTION_WUG;
  8194. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8195. i&=~OPTION_MADOGEAR;
  8196. #ifndef NEW_CARTS
  8197. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8198. i&=~OPTION_CART;
  8199. #else
  8200. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8201. pc_setcart(sd, 0);
  8202. #endif
  8203. if(i != sd->sc.option)
  8204. pc_setoption(sd, i);
  8205. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8206. hom_vaporize(sd, HOM_ST_ACTIVE);
  8207. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8208. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8209. if(sd->status.manner < 0)
  8210. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8211. status_calc_pc(sd,SCO_FORCE);
  8212. pc_checkallowskill(sd);
  8213. pc_equiplookall(sd);
  8214. pc_show_questinfo(sd);
  8215. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8216. if( sd->status.party_id ){
  8217. struct party_data* p;
  8218. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8219. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8220. if( i < MAX_PARTY ){
  8221. p->party.member[i].class_ = sd->status.class_;
  8222. clif_party_job_and_level(sd);
  8223. }
  8224. }
  8225. }
  8226. chrif_save(sd, CSAVE_NORMAL);
  8227. //if you were previously famous, not anymore.
  8228. if (fame_flag)
  8229. chrif_buildfamelist();
  8230. else if (sd->status.fame > 0) {
  8231. //It may be that now they are famous?
  8232. switch (sd->class_&MAPID_UPPERMASK) {
  8233. case MAPID_BLACKSMITH:
  8234. case MAPID_ALCHEMIST:
  8235. case MAPID_TAEKWON:
  8236. chrif_buildfamelist();
  8237. break;
  8238. }
  8239. }
  8240. return true;
  8241. }
  8242. /*==========================================
  8243. * Tell client player sd has change equipement
  8244. *------------------------------------------*/
  8245. void pc_equiplookall(struct map_session_data *sd)
  8246. {
  8247. nullpo_retv(sd);
  8248. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8249. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8250. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8251. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8252. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8253. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8254. }
  8255. /*==========================================
  8256. * Tell client player sd has change look (hair,equip...)
  8257. *------------------------------------------*/
  8258. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8259. nullpo_retv(sd);
  8260. switch(type) {
  8261. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8262. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8263. if (sd->status.hair != val) {
  8264. sd->status.hair = val;
  8265. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8266. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8267. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8268. }
  8269. break;
  8270. case LOOK_WEAPON:
  8271. sd->status.weapon = val;
  8272. break;
  8273. case LOOK_HEAD_BOTTOM:
  8274. sd->status.head_bottom = val;
  8275. sd->setlook_head_bottom = val;
  8276. break;
  8277. case LOOK_HEAD_TOP:
  8278. sd->status.head_top = val;
  8279. sd->setlook_head_top = val;
  8280. break;
  8281. case LOOK_HEAD_MID:
  8282. sd->status.head_mid = val;
  8283. sd->setlook_head_mid = val;
  8284. break;
  8285. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8286. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8287. if (sd->status.hair_color != val) {
  8288. sd->status.hair_color = val;
  8289. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8290. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8291. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8292. }
  8293. break;
  8294. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8295. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8296. sd->status.clothes_color = val;
  8297. break;
  8298. case LOOK_SHIELD:
  8299. sd->status.shield = val;
  8300. break;
  8301. case LOOK_SHOES:
  8302. break;
  8303. case LOOK_ROBE:
  8304. sd->status.robe = val;
  8305. sd->setlook_robe = val;
  8306. break;
  8307. case LOOK_BODY2:
  8308. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8309. sd->status.body = val;
  8310. break;
  8311. }
  8312. clif_changelook(&sd->bl, type, val);
  8313. }
  8314. /*==========================================
  8315. * Give an option (type) to player (sd) and display it to client
  8316. *------------------------------------------*/
  8317. void pc_setoption(struct map_session_data *sd,int type)
  8318. {
  8319. int p_type, new_look=0;
  8320. nullpo_retv(sd);
  8321. p_type = sd->sc.option;
  8322. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8323. sd->sc.option=type;
  8324. clif_changeoption(&sd->bl);
  8325. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8326. { // Mounting
  8327. clif_status_load(&sd->bl,EFST_RIDING,1);
  8328. status_calc_pc(sd,SCO_NONE);
  8329. }
  8330. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8331. { // Dismount
  8332. clif_status_load(&sd->bl,EFST_RIDING,0);
  8333. status_calc_pc(sd,SCO_NONE);
  8334. }
  8335. #ifndef NEW_CARTS
  8336. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8337. clif_cartlist(sd);
  8338. clif_updatestatus(sd, SP_CARTINFO);
  8339. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8340. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8341. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8342. clif_clearcart(sd->fd);
  8343. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8344. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8345. }
  8346. #endif
  8347. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8348. clif_status_load(&sd->bl,EFST_FALCON,1);
  8349. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8350. clif_status_load(&sd->bl,EFST_FALCON,0);
  8351. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8352. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8353. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8354. status_calc_pc(sd,SCO_NONE);
  8355. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8356. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8357. status_calc_pc(sd,SCO_NONE);
  8358. }
  8359. }
  8360. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8361. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8362. status_calc_pc(sd,SCO_NONE);
  8363. for (const auto &sc : mado_statuses) {
  8364. uint16 skill_id = status_sc2skill(sc);
  8365. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8366. status_change_end(&sd->bl,sc,INVALID_TIMER);
  8367. }
  8368. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8369. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106
  8370. clif_status_load( &sd->bl, EFST_MADOGEAR, 1 );
  8371. #endif
  8372. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8373. status_calc_pc(sd,SCO_NONE);
  8374. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8375. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8376. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8377. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8378. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8379. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8380. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8381. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8382. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8383. #if PACKETVER_MAIN_NUM >= 20191120 || PACKETVER_RE_NUM >= 20191106
  8384. clif_status_load( &sd->bl, EFST_MADOGEAR, 0 );
  8385. #endif
  8386. }
  8387. }
  8388. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8389. new_look = JOB_STAR_GLADIATOR2;
  8390. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8391. new_look = -1;
  8392. if (sd->disguise || !new_look)
  8393. return; //Disguises break sprite changes
  8394. if (new_look < 0) { //Restore normal look.
  8395. status_set_viewdata(&sd->bl, sd->status.class_);
  8396. new_look = sd->vd.class_;
  8397. }
  8398. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8399. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8400. if (sd->vd.cloth_color)
  8401. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8402. if( sd->vd.body_style )
  8403. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8404. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8405. }
  8406. /**
  8407. * Give player a cart
  8408. * @param sd Player
  8409. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8410. **/
  8411. bool pc_setcart(struct map_session_data *sd,int type) {
  8412. #ifndef NEW_CARTS
  8413. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8414. int option;
  8415. #endif
  8416. nullpo_retr(false,sd);
  8417. if( type < 0 || type > MAX_CARTS )
  8418. return false;// Never trust the values sent by the client! [Skotlex]
  8419. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8420. return false;// Push cart is required
  8421. #ifdef NEW_CARTS
  8422. switch( type ) {
  8423. case 0:
  8424. if( !sd->sc.data[SC_PUSH_CART] )
  8425. return 0;
  8426. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8427. clif_clearcart(sd->fd);
  8428. break;
  8429. default:/* everything else is an allowed ID so we can move on */
  8430. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8431. clif_cartlist(sd);
  8432. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8433. }
  8434. clif_updatestatus(sd, SP_CARTINFO);
  8435. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8436. break;
  8437. }
  8438. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8439. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8440. #else
  8441. // Update option
  8442. option = sd->sc.option;
  8443. option &= ~OPTION_CART;// clear cart bits
  8444. option |= cart[type]; // set cart
  8445. pc_setoption(sd, option);
  8446. #endif
  8447. return true;
  8448. }
  8449. /*==========================================
  8450. * Give player a falcon
  8451. *------------------------------------------*/
  8452. void pc_setfalcon(struct map_session_data* sd, int flag)
  8453. {
  8454. if( flag ){
  8455. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8456. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8457. } else if( pc_isfalcon(sd) ){
  8458. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8459. }
  8460. }
  8461. /*==========================================
  8462. * Set player riding
  8463. *------------------------------------------*/
  8464. void pc_setriding(struct map_session_data* sd, int flag)
  8465. {
  8466. if( sd->sc.data[SC_ALL_RIDING] )
  8467. return;
  8468. if( flag ){
  8469. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8470. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8471. } else if( pc_isriding(sd) ){
  8472. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8473. }
  8474. }
  8475. /*==========================================
  8476. * Give player a mado
  8477. *------------------------------------------*/
  8478. void pc_setmadogear(struct map_session_data* sd, int flag)
  8479. {
  8480. if( flag ){
  8481. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8482. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8483. } else if( pc_ismadogear(sd) ){
  8484. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8485. }
  8486. }
  8487. /*==========================================
  8488. * Check if player can drop an item
  8489. *------------------------------------------*/
  8490. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8491. {
  8492. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8493. return false;
  8494. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8495. return false;
  8496. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8497. }
  8498. /**
  8499. * Determines whether a player can attack based on status changes
  8500. * Why not use status_check_skilluse?
  8501. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8502. * Even ground-based attacks should be blocked by these statuses
  8503. * Called from unit_attack and unit_attack_timer_sub
  8504. * @retval true Can attack
  8505. **/
  8506. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8507. nullpo_retr(false, sd);
  8508. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8509. return false;
  8510. if (sd->state.block_action & PCBLOCK_ATTACK)
  8511. return false;
  8512. if(
  8513. #ifdef RENEWAL
  8514. sd->sc.data[SC_BASILICA_CELL] ||
  8515. #else
  8516. sd->sc.data[SC_BASILICA] ||
  8517. #endif
  8518. sd->sc.data[SC__SHADOWFORM] ||
  8519. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8520. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8521. sd->sc.data[SC_CRYSTALIZE] ||
  8522. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8523. sd->sc.data[SC_TRICKDEAD] ||
  8524. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8525. sd->sc.data[SC_BLADESTOP] ||
  8526. sd->sc.data[SC_DEEPSLEEP] ||
  8527. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8528. sd->sc.data[SC_KINGS_GRACE] )
  8529. return false;
  8530. return true;
  8531. }
  8532. /*==========================================
  8533. * Read '@type' variables (temporary numeric char reg)
  8534. *------------------------------------------*/
  8535. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8536. {
  8537. return i64db_i64get(sd->regs.vars, reg);
  8538. }
  8539. /*==========================================
  8540. * Set '@type' variables (temporary numeric char reg)
  8541. *------------------------------------------*/
  8542. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8543. {
  8544. uint32 index = script_getvaridx(reg);
  8545. nullpo_retr(false, sd);
  8546. if( val ) {
  8547. i64db_i64put(sd->regs.vars, reg, val);
  8548. if( index )
  8549. script_array_update(&sd->regs, reg, false);
  8550. } else {
  8551. i64db_remove(sd->regs.vars, reg);
  8552. if( index )
  8553. script_array_update(&sd->regs, reg, true);
  8554. }
  8555. return true;
  8556. }
  8557. /*==========================================
  8558. * Read '@type$' variables (temporary string char reg)
  8559. *------------------------------------------*/
  8560. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8561. {
  8562. struct script_reg_str *p = NULL;
  8563. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8564. return p ? p->value : NULL;
  8565. }
  8566. /*==========================================
  8567. * Set '@type$' variables (temporary string char reg)
  8568. *------------------------------------------*/
  8569. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8570. {
  8571. struct script_reg_str *p = NULL;
  8572. unsigned int index = script_getvaridx(reg);
  8573. DBData prev;
  8574. nullpo_retr(false, sd);
  8575. if( str[0] ) {
  8576. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8577. p->value = aStrdup(str);
  8578. p->flag.type = 1;
  8579. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8580. p = (struct script_reg_str *)db_data2ptr(&prev);
  8581. if( p->value )
  8582. aFree(p->value);
  8583. ers_free(str_reg_ers, p);
  8584. } else {
  8585. if( index )
  8586. script_array_update(&sd->regs, reg, false);
  8587. }
  8588. } else {
  8589. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8590. p = (struct script_reg_str *)db_data2ptr(&prev);
  8591. if( p->value )
  8592. aFree(p->value);
  8593. ers_free(str_reg_ers, p);
  8594. if( index )
  8595. script_array_update(&sd->regs, reg, true);
  8596. }
  8597. }
  8598. return true;
  8599. }
  8600. /**
  8601. * Serves the following variable types:
  8602. * - 'type' (permanent numeric char reg)
  8603. * - '#type' (permanent numeric account reg)
  8604. * - '##type' (permanent numeric account reg2)
  8605. **/
  8606. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8607. {
  8608. struct script_reg_num *p = NULL;
  8609. if (!sd->vars_ok) {
  8610. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8611. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8612. //intif->request_registry(sd,type==3?4:type);
  8613. set_eof(sd->fd);
  8614. return 0;
  8615. }
  8616. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8617. return p ? p->value : 0;
  8618. }
  8619. /**
  8620. * Serves the following variable types:
  8621. * - 'type$' (permanent str char reg)
  8622. * - '#type$' (permanent str account reg)
  8623. * - '##type$' (permanent str account reg2)
  8624. **/
  8625. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8626. {
  8627. struct script_reg_str *p = NULL;
  8628. if (!sd->vars_ok) {
  8629. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8630. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8631. //intif->request_registry(sd,type==3?4:type);
  8632. set_eof(sd->fd);
  8633. return NULL;
  8634. }
  8635. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8636. return p ? p->value : NULL;
  8637. }
  8638. /**
  8639. * Serves the following variable types:
  8640. * - 'type' (permanent numeric char reg)
  8641. * - '#type' (permanent numeric account reg)
  8642. * - '##type' (permanent numeric account reg2)
  8643. **/
  8644. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8645. {
  8646. struct script_reg_num *p = NULL;
  8647. const char *regname = get_str(script_getvarid(reg));
  8648. uint32 index = script_getvaridx(reg);
  8649. if ( !reg_load && !sd->vars_ok ) {
  8650. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8651. return false;
  8652. }
  8653. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8654. if( val ) {
  8655. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8656. script_array_update(&sd->regs, reg, false);
  8657. p->value = val;
  8658. } else {
  8659. p->value = 0;
  8660. if( index )
  8661. script_array_update(&sd->regs, reg, true);
  8662. }
  8663. if (!reg_load)
  8664. p->flag.update = 1;/* either way, it will require either delete or replace */
  8665. } else if( val ) {
  8666. DBData prev;
  8667. if( index )
  8668. script_array_update(&sd->regs, reg, false);
  8669. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8670. p->value = val;
  8671. if (!reg_load)
  8672. p->flag.update = 1;
  8673. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8674. p = (struct script_reg_num *)db_data2ptr(&prev);
  8675. ers_free(num_reg_ers, p);
  8676. }
  8677. }
  8678. if (!reg_load && p)
  8679. sd->vars_dirty = true;
  8680. return true;
  8681. }
  8682. /**
  8683. * Serves the following variable types:
  8684. * - 'type$' (permanent str char reg)
  8685. * - '#type$' (permanent str account reg)
  8686. * - '##type$' (permanent str account reg2)
  8687. **/
  8688. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8689. {
  8690. struct script_reg_str *p = NULL;
  8691. const char *regname = get_str(script_getvarid(reg));
  8692. unsigned int index = script_getvaridx(reg);
  8693. size_t vlen = 0;
  8694. if (!reg_load && !sd->vars_ok) {
  8695. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8696. return false;
  8697. }
  8698. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8699. {
  8700. 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);
  8701. return false;
  8702. }
  8703. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8704. if( val[0] ) {
  8705. if( p->value )
  8706. aFree(p->value);
  8707. else if ( index ) // an entry that was deleted, so we reset
  8708. script_array_update(&sd->regs, reg, false);
  8709. p->value = aStrdup(val);
  8710. } else {
  8711. if (p->value)
  8712. aFree(p->value);
  8713. p->value = NULL;
  8714. if( index )
  8715. script_array_update(&sd->regs, reg, true);
  8716. }
  8717. if( !reg_load )
  8718. p->flag.update = 1; // either way, it will require either delete or replace
  8719. } else if( val[0] ) {
  8720. DBData prev;
  8721. if( index )
  8722. script_array_update(&sd->regs, reg, false);
  8723. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8724. p->value = aStrdup(val);
  8725. if( !reg_load )
  8726. p->flag.update = 1;
  8727. p->flag.type = 1;
  8728. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8729. p = (struct script_reg_str *)db_data2ptr(&prev);
  8730. if( p->value )
  8731. aFree(p->value);
  8732. ers_free(str_reg_ers, p);
  8733. }
  8734. }
  8735. if( !reg_load && p )
  8736. sd->vars_dirty = true;
  8737. return true;
  8738. }
  8739. /**
  8740. * Set value of player variable
  8741. * @param sd Player
  8742. * @param reg Variable name
  8743. * @param value
  8744. * @return True if success, false if failed.
  8745. **/
  8746. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8747. char prefix = reg[0];
  8748. nullpo_retr(false, sd);
  8749. if (reg[strlen(reg)-1] == '$') {
  8750. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8751. return false;
  8752. }
  8753. val = cap_value(val, INT_MIN, INT_MAX);
  8754. switch (prefix) {
  8755. case '.':
  8756. case '\'':
  8757. case '$':
  8758. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8759. return false;
  8760. case '@':
  8761. return pc_setreg(sd, add_str(reg), val);
  8762. case '#':
  8763. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8764. default:
  8765. return pc_setglobalreg(sd, add_str(reg), val);
  8766. }
  8767. return false;
  8768. }
  8769. /**
  8770. * Get value of player variable
  8771. * @param sd Player
  8772. * @param reg Variable name
  8773. * @return Variable value or 0 if failed.
  8774. **/
  8775. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8776. char prefix = reg[0];
  8777. nullpo_ret(sd);
  8778. if (reg[strlen(reg)-1] == '$') {
  8779. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8780. return 0;
  8781. }
  8782. switch (prefix) {
  8783. case '.':
  8784. case '\'':
  8785. case '$':
  8786. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8787. return 0;
  8788. case '@':
  8789. return pc_readreg(sd, add_str(reg));
  8790. case '#':
  8791. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8792. default:
  8793. return pc_readglobalreg(sd, add_str(reg));
  8794. }
  8795. return 0;
  8796. }
  8797. /*==========================================
  8798. * Exec eventtimer for player sd (retrieved from map_session (id))
  8799. *------------------------------------------*/
  8800. static TIMER_FUNC(pc_eventtimer){
  8801. struct map_session_data *sd=map_id2sd(id);
  8802. char *p = (char *)data;
  8803. int i;
  8804. if(sd==NULL)
  8805. return 0;
  8806. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8807. if( i < MAX_EVENTTIMER )
  8808. {
  8809. sd->eventtimer[i] = INVALID_TIMER;
  8810. sd->eventcount--;
  8811. npc_event(sd,p,0);
  8812. }
  8813. else
  8814. ShowError("pc_eventtimer: no such event timer\n");
  8815. if (p) aFree(p);
  8816. return 0;
  8817. }
  8818. /*==========================================
  8819. * Add eventtimer for player sd ?
  8820. *------------------------------------------*/
  8821. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8822. {
  8823. int i;
  8824. nullpo_retr(false,sd);
  8825. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8826. if( i == MAX_EVENTTIMER )
  8827. return false;
  8828. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8829. sd->eventcount++;
  8830. return true;
  8831. }
  8832. /*==========================================
  8833. * Del eventtimer for player sd ?
  8834. *------------------------------------------*/
  8835. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8836. {
  8837. char* p = NULL;
  8838. int i;
  8839. nullpo_retr(false,sd);
  8840. if (sd->eventcount == 0)
  8841. return false;
  8842. // find the named event timer
  8843. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8844. sd->eventtimer[i] != INVALID_TIMER &&
  8845. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8846. strcmp(p, name) == 0
  8847. );
  8848. if( i == MAX_EVENTTIMER )
  8849. return false; // not found
  8850. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8851. sd->eventtimer[i] = INVALID_TIMER;
  8852. if(sd->eventcount > 0)
  8853. sd->eventcount--;
  8854. aFree(p);
  8855. return true;
  8856. }
  8857. /*==========================================
  8858. * Update eventtimer count for player sd
  8859. *------------------------------------------*/
  8860. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8861. {
  8862. int i;
  8863. nullpo_retv(sd);
  8864. for(i=0;i<MAX_EVENTTIMER;i++)
  8865. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8866. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8867. addt_tickimer(sd->eventtimer[i],tick);
  8868. break;
  8869. }
  8870. }
  8871. /*==========================================
  8872. * Remove all eventtimer for player sd
  8873. *------------------------------------------*/
  8874. void pc_cleareventtimer(struct map_session_data *sd)
  8875. {
  8876. int i;
  8877. nullpo_retv(sd);
  8878. if (sd->eventcount == 0)
  8879. return;
  8880. for(i=0;i<MAX_EVENTTIMER;i++){
  8881. if( sd->eventtimer[i] != INVALID_TIMER ){
  8882. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8883. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8884. sd->eventtimer[i] = INVALID_TIMER;
  8885. if(sd->eventcount > 0) //avoid looping to max val
  8886. sd->eventcount--;
  8887. if (p) aFree(p);
  8888. }
  8889. }
  8890. }
  8891. /**
  8892. * Called when an item with combo is worn
  8893. * @param *sd
  8894. * @param *data struct item_data
  8895. * @return success numbers of succeed combo
  8896. */
  8897. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8898. uint16 i;
  8899. int success = 0;
  8900. for( i = 0; i < data->combos_count; i++ ) {
  8901. struct itemchk {
  8902. int idx;
  8903. t_itemid nameid;
  8904. t_itemid card[MAX_SLOTS];
  8905. } *combo_idx;
  8906. int idx, j;
  8907. int nb_itemCombo;
  8908. unsigned int pos = 0;
  8909. /* ensure this isn't a duplicate combo */
  8910. if( sd->combos.bonus != NULL ) {
  8911. int x;
  8912. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8913. /* found a match, skip this combo */
  8914. if( x < sd->combos.count )
  8915. continue;
  8916. }
  8917. nb_itemCombo = data->combos[i]->count;
  8918. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8919. continue;
  8920. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8921. for(j=0; j < nb_itemCombo; j++){
  8922. combo_idx[j].idx=-1;
  8923. combo_idx[j].nameid=-1;
  8924. for( int k = 0; k < MAX_SLOTS; k++ ){
  8925. combo_idx[j].card[k] = -1;
  8926. }
  8927. }
  8928. for( j = 0; j < nb_itemCombo; j++ ) {
  8929. t_itemid id = data->combos[i]->nameid[j];
  8930. uint16 k;
  8931. bool found = false;
  8932. for( k = 0; k < EQI_MAX; k++ ) {
  8933. short index = sd->equip_index[k];
  8934. if( index < 0 )
  8935. continue;
  8936. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8937. continue;
  8938. if (!sd->inventory_data[index] )
  8939. continue;
  8940. if ( itemdb_type(id) != IT_CARD ) {
  8941. if ( sd->inventory_data[index]->nameid != id )
  8942. continue;
  8943. if(j>0){ //check if this item not already used
  8944. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8945. uint8 z;
  8946. for (z = 0; z < nb_itemCombo-1; z++)
  8947. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8948. do_continue=true;
  8949. if(do_continue)
  8950. continue;
  8951. }
  8952. combo_idx[j].nameid = id;
  8953. combo_idx[j].idx = index;
  8954. pos |= sd->inventory.u.items_inventory[index].equip;
  8955. found = true;
  8956. break;
  8957. } else { //Cards and enchants
  8958. uint16 z;
  8959. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8960. continue;
  8961. for (z = 0; z < MAX_SLOTS; z++) {
  8962. bool do_continue=false;
  8963. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8964. continue;
  8965. if(j>0){
  8966. int c1, c2;
  8967. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8968. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8969. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8970. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8971. do_continue = true;
  8972. break;
  8973. }
  8974. }
  8975. }
  8976. }
  8977. }
  8978. if(do_continue)
  8979. continue;
  8980. combo_idx[j].nameid = id;
  8981. combo_idx[j].idx = index;
  8982. combo_idx[j].card[z] = id;
  8983. pos |= sd->inventory.u.items_inventory[index].equip;
  8984. found = true;
  8985. break;
  8986. }
  8987. }
  8988. }
  8989. if( !found )
  8990. break;/* we haven't found all the ids for this combo, so we can return */
  8991. }
  8992. aFree(combo_idx);
  8993. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8994. if( j < nb_itemCombo )
  8995. continue;
  8996. /* we got here, means all items in the combo are matching */
  8997. idx = sd->combos.count;
  8998. if( sd->combos.bonus == NULL ) {
  8999. CREATE(sd->combos.bonus, struct script_code *, 1);
  9000. CREATE(sd->combos.id, unsigned short, 1);
  9001. CREATE(sd->combos.pos, unsigned int, 1);
  9002. sd->combos.count = 1;
  9003. } else {
  9004. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  9005. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  9006. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  9007. }
  9008. /* we simply copy the pointer */
  9009. sd->combos.bonus[idx] = data->combos[i]->script;
  9010. /* save this combo's id */
  9011. sd->combos.id[idx] = data->combos[i]->id;
  9012. /* save pos of combo*/
  9013. sd->combos.pos[idx] = pos;
  9014. success++;
  9015. }
  9016. return success;
  9017. }
  9018. /**
  9019. * Called when an item with combo is removed
  9020. * @param *sd
  9021. * @param *data struct item_data
  9022. * @return retval numbers of removed combo
  9023. */
  9024. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  9025. int i, retval = 0;
  9026. if( sd->combos.bonus == NULL )
  9027. return 0;/* nothing to do here, player has no combos */
  9028. for( i = 0; i < data->combos_count; i++ ) {
  9029. /* check if this combo exists in this user */
  9030. int x = 0, cursor = 0, j;
  9031. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  9032. /* no match, skip this combo */
  9033. if(x >= sd->combos.count)
  9034. continue;
  9035. sd->combos.bonus[x] = NULL;
  9036. sd->combos.id[x] = 0;
  9037. sd->combos.pos[x] = 0;
  9038. retval++;
  9039. /* check if combo requirements still fit */
  9040. if( pc_checkcombo( sd, data ) )
  9041. continue;
  9042. /* move next value to empty slot */
  9043. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  9044. if( sd->combos.bonus[j] == NULL )
  9045. continue;
  9046. if( cursor != j ) {
  9047. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  9048. sd->combos.id[cursor] = sd->combos.id[j];
  9049. sd->combos.pos[cursor] = sd->combos.pos[j];
  9050. }
  9051. cursor++;
  9052. }
  9053. /* it's empty, we can clear all the memory */
  9054. if( (sd->combos.count = cursor) == 0 ) {
  9055. aFree(sd->combos.bonus);
  9056. aFree(sd->combos.id);
  9057. aFree(sd->combos.pos);
  9058. sd->combos.bonus = NULL;
  9059. sd->combos.id = NULL;
  9060. sd->combos.pos = NULL;
  9061. return retval; /* we also can return at this point for we have no more combos to check */
  9062. }
  9063. }
  9064. return retval;
  9065. }
  9066. /**
  9067. * Load combo data(s) of player
  9068. * @param *sd
  9069. * @return ret numbers of succeed combo
  9070. */
  9071. int pc_load_combo(struct map_session_data *sd) {
  9072. int i, ret = 0;
  9073. for( i = 0; i < EQI_MAX; i++ ) {
  9074. struct item_data *id = NULL;
  9075. short idx = sd->equip_index[i];
  9076. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  9077. continue;
  9078. if( id->combos_count )
  9079. ret += pc_checkcombo(sd,id);
  9080. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9081. struct item_data *data;
  9082. int j;
  9083. for( j = 0; j < MAX_SLOTS; j++ ) {
  9084. if (!sd->inventory.u.items_inventory[idx].card[j])
  9085. continue;
  9086. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  9087. if( data->combos_count )
  9088. ret += pc_checkcombo(sd,data);
  9089. }
  9090. }
  9091. }
  9092. }
  9093. return ret;
  9094. }
  9095. /*==========================================
  9096. * Equip item on player sd at req_pos from inventory index n
  9097. * return: false - fail; true - success
  9098. *------------------------------------------*/
  9099. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9100. {
  9101. int i, pos, flag = 0, iflag;
  9102. struct item_data *id;
  9103. uint8 res = ITEM_EQUIP_ACK_OK;
  9104. short* equip_index;
  9105. nullpo_retr(false,sd);
  9106. if( n < 0 || n >= MAX_INVENTORY ) {
  9107. if( equipswitch ){
  9108. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9109. }else{
  9110. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9111. }
  9112. return false;
  9113. }
  9114. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9115. if( equipswitch ){
  9116. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9117. }else{
  9118. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9119. }
  9120. return false;
  9121. }
  9122. if (!(id = sd->inventory_data[n]))
  9123. return false;
  9124. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9125. if(battle_config.battle_log && !equipswitch)
  9126. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9127. if((res = pc_isequip(sd,n))) {
  9128. if( equipswitch ){
  9129. clif_equipswitch_add( sd, n, req_pos, res );
  9130. }else{
  9131. clif_equipitemack(sd,n,0,res); // fail
  9132. }
  9133. return false;
  9134. }
  9135. if( equipswitch && id->type == IT_AMMO ){
  9136. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9137. return false;
  9138. }
  9139. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9140. if( equipswitch ){
  9141. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9142. }else{
  9143. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9144. }
  9145. return false;
  9146. }
  9147. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9148. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9149. if( equipswitch ){
  9150. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9151. }else{
  9152. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9153. }
  9154. return false;
  9155. }
  9156. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9157. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9158. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9159. clif_notify_bindOnEquip(sd,n);
  9160. }
  9161. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9162. pos = req_pos&EQP_ACC;
  9163. if (pos == EQP_ACC) //User specified both slots.
  9164. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9165. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  9166. if (!sd->inventory.u.items_inventory[n].card[i])
  9167. continue;
  9168. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9169. if (card_data) {
  9170. int card_pos = card_data->equip;
  9171. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9172. pos = card_pos; // Use the card's equip position
  9173. break;
  9174. }
  9175. }
  9176. }
  9177. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9178. pos = (req_pos&EQP_ARMS);
  9179. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9180. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9181. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9182. pos = req_pos&EQP_SHADOW_ACC;
  9183. if (pos == EQP_SHADOW_ACC)
  9184. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9185. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9186. pos = (req_pos&EQP_SHADOW_ARMS);
  9187. if( pos == EQP_SHADOW_ARMS )
  9188. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9189. }
  9190. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9191. //Update skill-block range database when weapon range changes. [Skotlex]
  9192. i = equip_index[EQI_HAND_R];
  9193. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9194. flag = 1;
  9195. else
  9196. flag = id->range != sd->inventory_data[i]->range;
  9197. }
  9198. if( equipswitch ){
  9199. for( i = 0; i < EQI_MAX; i++ ){
  9200. if( pos&equip_bitmask[i] ){
  9201. // If there was already an item assigned to this slot
  9202. if( sd->equip_switch_index[i] >= 0 ){
  9203. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9204. }
  9205. // Assign the new index to it
  9206. sd->equip_switch_index[i] = n;
  9207. }
  9208. }
  9209. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9210. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9211. return true;
  9212. }else{
  9213. for(i=0;i<EQI_MAX;i++) {
  9214. if(pos & equip_bitmask[i]) {
  9215. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9216. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9217. sd->equip_index[i] = n;
  9218. }
  9219. }
  9220. pc_equipswitch_remove(sd, n);
  9221. if(pos==EQP_AMMO) {
  9222. clif_arrowequip(sd,n);
  9223. clif_arrow_fail(sd,3);
  9224. }
  9225. else
  9226. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9227. sd->inventory.u.items_inventory[n].equip = pos;
  9228. }
  9229. if(pos & EQP_HAND_R) {
  9230. if(id)
  9231. sd->weapontype1 = id->look;
  9232. else
  9233. sd->weapontype1 = 0;
  9234. pc_calcweapontype(sd);
  9235. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9236. }
  9237. if(pos & EQP_HAND_L) {
  9238. if(id) {
  9239. if(id->type == IT_WEAPON) {
  9240. sd->status.shield = 0;
  9241. sd->weapontype2 = id->look;
  9242. }
  9243. else
  9244. if(id->type == IT_ARMOR) {
  9245. sd->status.shield = id->look;
  9246. sd->weapontype2 = 0;
  9247. }
  9248. }
  9249. else
  9250. sd->status.shield = sd->weapontype2 = 0;
  9251. pc_calcweapontype(sd);
  9252. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9253. }
  9254. if(pos & EQP_SHOES)
  9255. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9256. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9257. short idx = sd->equip_index[EQI_AMMO];
  9258. if (idx >= 0) {
  9259. switch (sd->inventory_data[idx]->look) {
  9260. case AMMO_ARROW:
  9261. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9262. pc_unequipitem(sd, idx, 2 | 4);
  9263. break;
  9264. case AMMO_BULLET:
  9265. case AMMO_SHELL:
  9266. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9267. #ifdef RENEWAL
  9268. && id->look != W_GRENADE
  9269. #endif
  9270. )
  9271. pc_unequipitem(sd, idx, 2 | 4);
  9272. break;
  9273. #ifndef RENEWAL
  9274. case AMMO_GRENADE:
  9275. if (id->look != W_GRENADE)
  9276. pc_unequipitem(sd, idx, 2 | 4);
  9277. break;
  9278. #endif
  9279. }
  9280. }
  9281. }
  9282. pc_set_costume_view(sd);
  9283. pc_checkallowskill(sd); //Check if status changes should be halted.
  9284. iflag = sd->npc_item_flag;
  9285. /* check for combos (MUST be before status_calc_pc) */
  9286. if( id->combos_count )
  9287. pc_checkcombo(sd,id);
  9288. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9289. ; //No cards
  9290. else {
  9291. for( i = 0; i < MAX_SLOTS; i++ ) {
  9292. struct item_data *data;
  9293. if (!sd->inventory.u.items_inventory[n].card[i])
  9294. continue;
  9295. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9296. if( data->combos_count )
  9297. pc_checkcombo(sd,data);
  9298. }
  9299. }
  9300. }
  9301. status_calc_pc(sd,SCO_NONE);
  9302. if (flag) //Update skill data
  9303. clif_skillinfoblock(sd);
  9304. //OnEquip script [Skotlex]
  9305. if (id) {
  9306. //only run the script if item isn't restricted
  9307. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9308. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9309. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9310. ; //No cards
  9311. else {
  9312. for( i = 0; i < MAX_SLOTS; i++ ) {
  9313. struct item_data *data;
  9314. if (!sd->inventory.u.items_inventory[n].card[i])
  9315. continue;
  9316. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9317. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9318. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9319. }
  9320. }
  9321. }
  9322. }
  9323. sd->npc_item_flag = iflag;
  9324. return true;
  9325. }
  9326. /**
  9327. * Recalculate player status on unequip
  9328. * @param sd: Player data
  9329. * @param n: Item inventory index
  9330. * @param flag: Whether to recalculate a player's status or not
  9331. * @return True on success or false on failure
  9332. */
  9333. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9334. int i, iflag;
  9335. bool status_calc = false;
  9336. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9337. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9338. sd->inventory.u.items_inventory[n].equip = 0;
  9339. if (!(flag & 4))
  9340. pc_checkallowskill(sd);
  9341. iflag = sd->npc_item_flag;
  9342. /* check for combos (MUST be before status_calc_pc) */
  9343. if (sd->inventory_data[n]) {
  9344. if (sd->inventory_data[n]->combos_count) {
  9345. if (pc_removecombo(sd, sd->inventory_data[n]))
  9346. status_calc = true;
  9347. }
  9348. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9349. ; //No cards
  9350. else {
  9351. for (i = 0; i < MAX_SLOTS; i++) {
  9352. struct item_data *data;
  9353. if (!sd->inventory.u.items_inventory[n].card[i])
  9354. continue;
  9355. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9356. if (data->combos_count) {
  9357. if (pc_removecombo(sd, data))
  9358. status_calc = true;
  9359. }
  9360. }
  9361. }
  9362. }
  9363. }
  9364. if (flag & 1 || status_calc) {
  9365. pc_checkallowskill(sd);
  9366. status_calc_pc(sd, SCO_NONE);
  9367. }
  9368. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9369. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9370. //OnUnEquip script [Skotlex]
  9371. if (sd->inventory_data[n]) {
  9372. if (sd->inventory_data[n]->unequip_script)
  9373. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9374. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9375. ; //No cards
  9376. else {
  9377. for (i = 0; i < MAX_SLOTS; i++) {
  9378. struct item_data *data;
  9379. if (!sd->inventory.u.items_inventory[n].card[i])
  9380. continue;
  9381. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9382. if (data->unequip_script)
  9383. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9384. }
  9385. }
  9386. }
  9387. }
  9388. sd->npc_item_flag = iflag;
  9389. }
  9390. /**
  9391. * Called when attempting to unequip an item from a player
  9392. * @param sd: Player data
  9393. * @param n: Item inventory index
  9394. * @param flag: Type of unequip
  9395. * 0 - only unequip
  9396. * 1 - calculate status after unequipping
  9397. * 2 - force unequip
  9398. * 4 - unequip by switching equipment
  9399. * @return True on success or false on failure
  9400. */
  9401. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9402. int i, pos;
  9403. nullpo_retr(false,sd);
  9404. if (n < 0 || n >= MAX_INVENTORY) {
  9405. clif_unequipitemack(sd,0,0,0);
  9406. return false;
  9407. }
  9408. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9409. clif_unequipitemack(sd,n,0,0);
  9410. return false; //Nothing to unequip
  9411. }
  9412. // status change that makes player cannot unequip equipment
  9413. if (!(flag&2) && sd->sc.count &&
  9414. (sd->sc.data[SC_BERSERK] ||
  9415. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9416. sd->sc.data[SC__BLOODYLUST] ||
  9417. sd->sc.data[SC_KYOUGAKU] ||
  9418. (sd->sc.data[SC_PYROCLASTIC] &&
  9419. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9420. {
  9421. clif_unequipitemack(sd,n,0,0);
  9422. return false;
  9423. }
  9424. if (battle_config.battle_log)
  9425. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9426. for(i = 0; i < EQI_MAX; i++) {
  9427. if (pos & equip_bitmask[i])
  9428. sd->equip_index[i] = -1;
  9429. }
  9430. if(pos & EQP_HAND_R) {
  9431. sd->weapontype1 = 0;
  9432. sd->status.weapon = sd->weapontype2;
  9433. pc_calcweapontype(sd);
  9434. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9435. if( !battle_config.dancing_weaponswitch_fix )
  9436. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9437. #ifdef RENEWAL
  9438. if (battle_config.switch_remove_edp&2) {
  9439. #else
  9440. if (battle_config.switch_remove_edp&1) {
  9441. #endif
  9442. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9443. }
  9444. }
  9445. if(pos & EQP_HAND_L) {
  9446. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9447. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9448. sd->status.shield = sd->weapontype2 = 0;
  9449. pc_calcweapontype(sd);
  9450. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9451. }
  9452. if(pos & EQP_SHOES)
  9453. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9454. clif_unequipitemack(sd,n,pos,1);
  9455. pc_set_costume_view(sd);
  9456. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9457. // On weapon change (right and left hand)
  9458. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9459. if (battle_config.ammo_unequip && !(flag & 4)) {
  9460. switch (sd->inventory_data[n]->look) {
  9461. case W_BOW:
  9462. case W_MUSICAL:
  9463. case W_WHIP:
  9464. case W_REVOLVER:
  9465. case W_RIFLE:
  9466. case W_GATLING:
  9467. case W_SHOTGUN:
  9468. case W_GRENADE: {
  9469. short idx = sd->equip_index[EQI_AMMO];
  9470. if (idx >= 0) {
  9471. sd->equip_index[EQI_AMMO] = -1;
  9472. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9473. pc_unequipitem_sub(sd, idx, 0);
  9474. }
  9475. }
  9476. break;
  9477. }
  9478. }
  9479. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9480. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9481. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9482. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9483. }
  9484. // On armor change
  9485. if (pos & EQP_ARMOR) {
  9486. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9487. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9488. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9489. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9490. }
  9491. // On equipment change
  9492. #ifndef RENEWAL
  9493. if (!(flag&4))
  9494. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9495. #endif
  9496. // On ammo change
  9497. 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_))
  9498. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9499. pc_unequipitem_sub(sd, n, flag);
  9500. return true;
  9501. }
  9502. int pc_equipswitch( struct map_session_data* sd, int index ){
  9503. // Get the target equip mask
  9504. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9505. // Get the currently equipped item
  9506. short equippedItem = pc_checkequip( sd, position, true );
  9507. // No item equipped at the target
  9508. if( equippedItem == -1 ){
  9509. // Remove it from the equip switch
  9510. pc_equipswitch_remove( sd, index );
  9511. pc_equipitem( sd, index, position );
  9512. return position;
  9513. }else{
  9514. std::map<int, int> unequipped;
  9515. int unequipped_position = 0;
  9516. // Unequip all items that interfere
  9517. for( int i = 0; i < EQI_MAX; i++ ){
  9518. int unequip_index = sd->equip_index[i];
  9519. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9520. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9521. // Store the unequipped index and position mask for later
  9522. unequipped[unequip_index] = unequip_item->equip;
  9523. // Keep the position for later
  9524. unequipped_position |= unequip_item->equip;
  9525. // Unequip the item
  9526. pc_unequipitem( sd, unequip_index, 0 );
  9527. }
  9528. }
  9529. int all_position = position | unequipped_position;
  9530. // Equip everything that is hit by the mask
  9531. for( int i = 0; i < EQI_MAX; i++ ){
  9532. int exchange_index = sd->equip_switch_index[i];
  9533. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9534. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9535. // Store the target position
  9536. int exchange_position = exchange_item->equipSwitch;
  9537. // Remove the item from equip switch
  9538. pc_equipswitch_remove( sd, exchange_index );
  9539. // Equip the item at the destinated position
  9540. pc_equipitem( sd, exchange_index, exchange_position );
  9541. }
  9542. }
  9543. // Place all unequipped items into the equip switch window
  9544. for( std::pair<int, int> pair : unequipped ){
  9545. int unequipped_index = pair.first;
  9546. int unequipped_position = pair.second;
  9547. // Rebuild the index cache
  9548. for( int i = 0; i < EQI_MAX; i++ ){
  9549. if( unequipped_position & equip_bitmask[i] ){
  9550. sd->equip_switch_index[i] = unequipped_index;
  9551. }
  9552. }
  9553. // Set the correct position mask
  9554. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9555. // Notify the client
  9556. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9557. }
  9558. return all_position;
  9559. }
  9560. }
  9561. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9562. struct item* item = &sd->inventory.u.items_inventory[index];
  9563. if( !item->equipSwitch ){
  9564. return;
  9565. }
  9566. for( int i = 0; i < EQI_MAX; i++ ){
  9567. // If a match is found
  9568. if( sd->equip_switch_index[i] == index ){
  9569. // Remove it from the slot
  9570. sd->equip_switch_index[i] = -1;
  9571. }
  9572. }
  9573. // Send out one packet for all slots using the current item's mask
  9574. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9575. item->equipSwitch = 0;
  9576. }
  9577. /*==========================================
  9578. * Checking if player (sd) has an invalid item
  9579. * and is unequiped on map load (item_noequip)
  9580. *------------------------------------------*/
  9581. void pc_checkitem(struct map_session_data *sd) {
  9582. int i, calc_flag = 0;
  9583. struct item* it;
  9584. nullpo_retv(sd);
  9585. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9586. return;
  9587. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9588. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9589. it = &sd->inventory.u.items_inventory[i];
  9590. if( it->nameid == 0 )
  9591. continue;
  9592. if( !it->equip )
  9593. continue;
  9594. if( it->equip&~pc_equippoint(sd,i) ) {
  9595. pc_unequipitem(sd, i, 2);
  9596. calc_flag = 1;
  9597. continue;
  9598. }
  9599. 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) ) {
  9600. pc_unequipitem(sd, i, 2);
  9601. calc_flag = 1;
  9602. continue;
  9603. }
  9604. }
  9605. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9606. it = &sd->inventory.u.items_inventory[i];
  9607. if( it->nameid == 0 )
  9608. continue;
  9609. if( !it->equipSwitch )
  9610. continue;
  9611. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9612. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9613. for( int j = 0; j < EQI_MAX; j++ ){
  9614. if( sd->equip_switch_index[j] == i ){
  9615. sd->equip_switch_index[j] = -1;
  9616. }
  9617. }
  9618. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9619. continue;
  9620. }
  9621. }
  9622. if( calc_flag && sd->state.active ) {
  9623. pc_checkallowskill(sd);
  9624. status_calc_pc(sd,SCO_NONE);
  9625. }
  9626. }
  9627. /*==========================================
  9628. * Checks for unavailable items and removes them.
  9629. * @param sd: Player data
  9630. * @param type Forced check:
  9631. * 1 - Inventory
  9632. * 2 - Cart
  9633. * 4 - Storage
  9634. *------------------------------------------*/
  9635. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9636. {
  9637. int i;
  9638. t_itemid nameid;
  9639. char output[256];
  9640. nullpo_retv(sd);
  9641. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9642. for(i = 0; i < MAX_INVENTORY; i++) {
  9643. nameid = sd->inventory.u.items_inventory[i].nameid;
  9644. if (!nameid)
  9645. continue;
  9646. if (!itemdb_available(nameid)) {
  9647. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  9648. clif_displaymessage(sd->fd, output);
  9649. 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);
  9650. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9651. continue;
  9652. }
  9653. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9654. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9655. }
  9656. }
  9657. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9658. for(i = 0; i < MAX_CART; i++) {
  9659. nameid = sd->cart.u.items_cart[i].nameid;
  9660. if (!nameid)
  9661. continue;
  9662. if (!itemdb_available(nameid)) {
  9663. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  9664. clif_displaymessage(sd->fd, output);
  9665. 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);
  9666. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9667. continue;
  9668. }
  9669. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9670. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9671. }
  9672. }
  9673. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9674. for(i = 0; i < sd->storage.max_amount; i++) {
  9675. nameid = sd->storage.u.items_storage[i].nameid;
  9676. if (!nameid)
  9677. continue;
  9678. if (!itemdb_available(nameid)) {
  9679. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  9680. clif_displaymessage(sd->fd, output);
  9681. 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);
  9682. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9683. continue;
  9684. }
  9685. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9686. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9687. }
  9688. }
  9689. }
  9690. /*==========================================
  9691. * Update PVP rank for sd1 in cmp to sd2
  9692. *------------------------------------------*/
  9693. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9694. {
  9695. struct map_session_data *sd1,*sd2;
  9696. sd1=(struct map_session_data *)bl;
  9697. sd2=va_arg(ap,struct map_session_data *);
  9698. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9699. {// cannot register pvp rank for hidden GMs
  9700. return 0;
  9701. }
  9702. if( sd1->pvp_point > sd2->pvp_point )
  9703. sd2->pvp_rank++;
  9704. return 0;
  9705. }
  9706. /*==========================================
  9707. * Calculate new rank beetween all present players (map_foreachinallarea)
  9708. * and display result
  9709. *------------------------------------------*/
  9710. int pc_calc_pvprank(struct map_session_data *sd)
  9711. {
  9712. int old = sd->pvp_rank;
  9713. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9714. sd->pvp_rank=1;
  9715. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9716. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9717. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9718. return sd->pvp_rank;
  9719. }
  9720. /*==========================================
  9721. * Calculate next sd ranking calculation from config
  9722. *------------------------------------------*/
  9723. TIMER_FUNC(pc_calc_pvprank_timer){
  9724. struct map_session_data *sd;
  9725. sd=map_id2sd(id);
  9726. if(sd==NULL)
  9727. return 0;
  9728. sd->pvp_timer = INVALID_TIMER;
  9729. if( pc_isinvisible(sd) )
  9730. {// do not calculate the pvp rank for a hidden GM
  9731. return 0;
  9732. }
  9733. if( pc_calc_pvprank(sd) > 0 )
  9734. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9735. return 0;
  9736. }
  9737. /*==========================================
  9738. * Checking if sd is married
  9739. * Return:
  9740. * partner_id = yes
  9741. * 0 = no
  9742. *------------------------------------------*/
  9743. int pc_ismarried(struct map_session_data *sd)
  9744. {
  9745. if(sd == NULL)
  9746. return -1;
  9747. if(sd->status.partner_id > 0)
  9748. return sd->status.partner_id;
  9749. else
  9750. return 0;
  9751. }
  9752. /*==========================================
  9753. * Marry player sd to player dstsd
  9754. * Return:
  9755. * false = fail
  9756. * true = success
  9757. *------------------------------------------*/
  9758. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9759. {
  9760. if(sd == NULL || dstsd == NULL ||
  9761. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9762. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9763. return false;
  9764. sd->status.partner_id = dstsd->status.char_id;
  9765. dstsd->status.partner_id = sd->status.char_id;
  9766. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9767. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9768. return true;
  9769. }
  9770. /*==========================================
  9771. * Divorce sd from its partner
  9772. * Return:
  9773. * false = fail
  9774. * true = success
  9775. *------------------------------------------*/
  9776. bool pc_divorce(struct map_session_data *sd)
  9777. {
  9778. struct map_session_data *p_sd;
  9779. int i;
  9780. if( sd == NULL || !pc_ismarried(sd) )
  9781. return false;
  9782. if( !sd->status.partner_id )
  9783. return false; // Char is not married
  9784. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9785. { // Lets char server do the divorce
  9786. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9787. return false; // No char server connected
  9788. return true;
  9789. }
  9790. // Both players online, lets do the divorce manually
  9791. sd->status.partner_id = 0;
  9792. p_sd->status.partner_id = 0;
  9793. for( i = 0; i < MAX_INVENTORY; i++ )
  9794. {
  9795. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9796. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9797. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9798. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9799. }
  9800. clif_divorced(sd, p_sd->status.name);
  9801. clif_divorced(p_sd, sd->status.name);
  9802. return true;
  9803. }
  9804. /**
  9805. * Get the partner map_session_data of a player
  9806. * @param sd : the husband|wife session
  9807. * @return partner session or NULL
  9808. */
  9809. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9810. if (!sd || !pc_ismarried(sd))
  9811. return NULL;
  9812. return map_charid2sd(sd->status.partner_id);
  9813. }
  9814. /**
  9815. * Get the father map_session_data of a player
  9816. * @param sd : the baby session
  9817. * @return father session or NULL
  9818. */
  9819. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9820. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9821. return NULL;
  9822. return map_charid2sd(sd->status.father);
  9823. }
  9824. /**
  9825. * Get the mother map_session_data of a player
  9826. * @param sd : the baby session
  9827. * @return mother session or NULL
  9828. */
  9829. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9830. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9831. return NULL;
  9832. return map_charid2sd(sd->status.mother);
  9833. }
  9834. /*==========================================
  9835. * Get sd children charid. (Need to be married)
  9836. *------------------------------------------*/
  9837. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9838. {
  9839. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9840. // charid2sd returns NULL if not found
  9841. return NULL;
  9842. return map_charid2sd(sd->status.child);
  9843. }
  9844. /*==========================================
  9845. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9846. *------------------------------------------*/
  9847. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9848. {
  9849. int hp = 0, sp = 0;
  9850. if( pc_isdead(sd) )
  9851. return;
  9852. if (sd->hp_loss.value) {
  9853. sd->hp_loss.tick += diff_tick;
  9854. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9855. hp += sd->hp_loss.value;
  9856. sd->hp_loss.tick -= sd->hp_loss.rate;
  9857. }
  9858. if(hp >= sd->battle_status.hp)
  9859. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9860. }
  9861. if (sd->sp_loss.value) {
  9862. sd->sp_loss.tick += diff_tick;
  9863. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9864. sp += sd->sp_loss.value;
  9865. sd->sp_loss.tick -= sd->sp_loss.rate;
  9866. }
  9867. }
  9868. if (hp > 0 || sp > 0)
  9869. status_zap(&sd->bl, hp, sp);
  9870. }
  9871. //Character regen. Flag is used to know which types of regen can take place.
  9872. //&1: HP regen
  9873. //&2: SP regen
  9874. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9875. {
  9876. int hp = 0, sp = 0;
  9877. if (sd->hp_regen.value) {
  9878. sd->hp_regen.tick += diff_tick;
  9879. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9880. hp += sd->hp_regen.value;
  9881. sd->hp_regen.tick -= sd->hp_regen.rate;
  9882. }
  9883. }
  9884. if (sd->sp_regen.value) {
  9885. sd->sp_regen.tick += diff_tick;
  9886. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9887. sp += sd->sp_regen.value;
  9888. sd->sp_regen.tick -= sd->sp_regen.rate;
  9889. }
  9890. }
  9891. if (sd->percent_hp_regen.value) {
  9892. sd->percent_hp_regen.tick += diff_tick;
  9893. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9894. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9895. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9896. }
  9897. }
  9898. if (sd->percent_sp_regen.value) {
  9899. sd->percent_sp_regen.tick += diff_tick;
  9900. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9901. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9902. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9903. }
  9904. }
  9905. if (hp > 0 || sp > 0)
  9906. status_heal(&sd->bl, hp, sp, 0);
  9907. }
  9908. /*==========================================
  9909. * Memo player sd savepoint. (map,x,y)
  9910. *------------------------------------------*/
  9911. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9912. {
  9913. nullpo_retv(sd);
  9914. sd->status.save_point.map = mapindex;
  9915. sd->status.save_point.x = x;
  9916. sd->status.save_point.y = y;
  9917. }
  9918. /*==========================================
  9919. * Save 1 player data at autosave interval
  9920. *------------------------------------------*/
  9921. static TIMER_FUNC(pc_autosave){
  9922. int interval;
  9923. struct s_mapiterator* iter;
  9924. struct map_session_data* sd;
  9925. static int last_save_id = 0, save_flag = 0;
  9926. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9927. save_flag = 0;
  9928. else
  9929. save_flag = 1; //Noone was saved, so save first found char.
  9930. iter = mapit_getallusers();
  9931. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9932. {
  9933. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9934. continue;
  9935. if(sd->bl.id == last_save_id && save_flag != 1) {
  9936. save_flag = 1;
  9937. continue;
  9938. }
  9939. if(save_flag != 1) //Not our turn to save yet.
  9940. continue;
  9941. //Save char.
  9942. last_save_id = sd->bl.id;
  9943. save_flag = 2;
  9944. if (pc_isvip(sd)) // Check if we're still VIP
  9945. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9946. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9947. break;
  9948. }
  9949. mapit_free(iter);
  9950. interval = autosave_interval/(map_usercount()+1);
  9951. if(interval < minsave_interval)
  9952. interval = minsave_interval;
  9953. add_timer(gettick()+interval,pc_autosave,0,0);
  9954. return 0;
  9955. }
  9956. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9957. {
  9958. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9959. { //Night/day state does not match.
  9960. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9961. sd->state.night = night_flag;
  9962. return 1;
  9963. }
  9964. return 0;
  9965. }
  9966. /*================================================
  9967. * timer to do the day [Yor]
  9968. * data: 0 = called by timer, 1 = gmcommand/script
  9969. *------------------------------------------------*/
  9970. TIMER_FUNC(map_day_timer){
  9971. char tmp_soutput[1024];
  9972. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9973. return 0;
  9974. if (!night_flag)
  9975. return 0; //Already day.
  9976. night_flag = 0; // 0=day, 1=night [Yor]
  9977. map_foreachpc(pc_daynight_timer_sub);
  9978. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9979. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9980. return 0;
  9981. }
  9982. /*================================================
  9983. * timer to do the night [Yor]
  9984. * data: 0 = called by timer, 1 = gmcommand/script
  9985. *------------------------------------------------*/
  9986. TIMER_FUNC(map_night_timer){
  9987. char tmp_soutput[1024];
  9988. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9989. return 0;
  9990. if (night_flag)
  9991. return 0; //Already nigth.
  9992. night_flag = 1; // 0=day, 1=night [Yor]
  9993. map_foreachpc(pc_daynight_timer_sub);
  9994. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9995. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9996. return 0;
  9997. }
  9998. /**
  9999. * Attempt to stand up a player
  10000. * @param sd
  10001. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  10002. * @return True if success, Fals if failed
  10003. */
  10004. bool pc_setstand(struct map_session_data *sd, bool force){
  10005. nullpo_ret(sd);
  10006. // Cannot stand yet
  10007. // TODO: Move to SCS_NOSTAND [Cydh]
  10008. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  10009. return false;
  10010. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  10011. clif_status_load(&sd->bl,EFST_SIT,0);
  10012. clif_standing(&sd->bl); //Inform area PC is standing
  10013. //Reset sitting tick.
  10014. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10015. if( pc_isdead( sd ) ){
  10016. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10017. clif_party_dead( sd );
  10018. }else{
  10019. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10020. }
  10021. return true;
  10022. }
  10023. /**
  10024. * Calculate Overheat value
  10025. * @param sd: Player data
  10026. * @param heat: Amount of Heat to adjust
  10027. **/
  10028. void pc_overheat(struct map_session_data *sd, int16 heat) {
  10029. struct status_change_entry *sce = NULL;
  10030. int16 limit[] = { 150, 200, 280, 360, 450 };
  10031. uint16 skill_lv;
  10032. nullpo_retv(sd);
  10033. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  10034. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  10035. sce->val1 += heat;
  10036. sce->val1 = cap_value(sce->val1, 0, 1000);
  10037. if (sd->sc.data[SC_OVERHEAT])
  10038. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  10039. if (sce->val1 > limit[skill_lv])
  10040. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  10041. } else if (heat > 0)
  10042. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10043. }
  10044. /**
  10045. * Check if player is autolooting given itemID.
  10046. */
  10047. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10048. {
  10049. uint8 i = 0;
  10050. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10051. return true;
  10052. if (!sd->state.autolooting)
  10053. return false;
  10054. if (sd->state.autolooting)
  10055. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10056. return (i != AUTOLOOTITEM_SIZE);
  10057. }
  10058. /**
  10059. * Checks if player can use @/#command
  10060. * @param sd Player map session data
  10061. * @param command Command name without @/# and params
  10062. * @param type is it atcommand or charcommand
  10063. */
  10064. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  10065. {
  10066. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  10067. }
  10068. /**
  10069. * Checks if commands used by a player should be logged
  10070. * according to their group setting.
  10071. * @param sd Player map session data
  10072. */
  10073. bool pc_should_log_commands(struct map_session_data *sd)
  10074. {
  10075. return pc_group_should_log_commands(pc_get_group_id(sd));
  10076. }
  10077. /**
  10078. * Spirit Charm expiration timer.
  10079. * @see TimerFunc
  10080. */
  10081. static TIMER_FUNC(pc_spiritcharm_timer){
  10082. struct map_session_data *sd;
  10083. int i;
  10084. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10085. return 1;
  10086. if (sd->spiritcharm <= 0) {
  10087. 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);
  10088. sd->spiritcharm = 0;
  10089. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10090. return 0;
  10091. }
  10092. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10093. if (i == sd->spiritcharm) {
  10094. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10095. return 0;
  10096. }
  10097. sd->spiritcharm--;
  10098. if (i != sd->spiritcharm)
  10099. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10100. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10101. if (sd->spiritcharm <= 0)
  10102. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10103. clif_spiritcharm(sd);
  10104. return 0;
  10105. }
  10106. /**
  10107. * Adds a spirit charm.
  10108. * @param sd: Target character
  10109. * @param interval: Duration
  10110. * @param max: Maximum amount of charms to add
  10111. * @param type: Charm type (@see spirit_charm_types)
  10112. */
  10113. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10114. {
  10115. int tid, i;
  10116. nullpo_retv(sd);
  10117. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10118. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10119. if (max > MAX_SPIRITCHARM)
  10120. max = MAX_SPIRITCHARM;
  10121. if (sd->spiritcharm < 0)
  10122. sd->spiritcharm = 0;
  10123. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10124. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10125. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10126. sd->spiritcharm--;
  10127. if (sd->spiritcharm != 0)
  10128. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10129. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10130. }
  10131. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10132. 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);
  10133. if (i != sd->spiritcharm)
  10134. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10135. sd->spiritcharm_timer[i] = tid;
  10136. sd->spiritcharm++;
  10137. sd->spiritcharm_type = type;
  10138. clif_spiritcharm(sd);
  10139. }
  10140. /**
  10141. * Removes one or more spirit charms.
  10142. * @param sd: The target character
  10143. * @param count: Amount of charms to remove
  10144. * @param type: Type of charm to remove
  10145. */
  10146. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10147. {
  10148. int i;
  10149. nullpo_retv(sd);
  10150. if (sd->spiritcharm_type != type)
  10151. return;
  10152. if (sd->spiritcharm <= 0) {
  10153. sd->spiritcharm = 0;
  10154. return;
  10155. }
  10156. if (count <= 0)
  10157. return;
  10158. if (count > sd->spiritcharm)
  10159. count = sd->spiritcharm;
  10160. sd->spiritcharm -= count;
  10161. if (count > MAX_SPIRITCHARM)
  10162. count = MAX_SPIRITCHARM;
  10163. for (i = 0; i < count; i++) {
  10164. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10165. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10166. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10167. }
  10168. }
  10169. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10170. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10171. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10172. }
  10173. if (sd->spiritcharm <= 0)
  10174. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10175. clif_spiritcharm(sd);
  10176. }
  10177. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10178. /**
  10179. * Renewal EXP/Item Drop rate modifier based on level penalty
  10180. * @param level_diff: Monster and Player level difference
  10181. * @param mob_class: Monster class
  10182. * @param mode: Monster mode
  10183. * @param type: 1 - EXP, 2 - Item Drop
  10184. * @return Penalty rate
  10185. */
  10186. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  10187. {
  10188. int rate = 100;
  10189. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  10190. return rate;
  10191. if (level_diff < 0)
  10192. level_diff = MAX_LEVEL + (~level_diff + 1);
  10193. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  10194. return rate;
  10195. return 100; // Penalty not found, return default
  10196. }
  10197. #endif
  10198. int pc_split_str(char *str,char **val,int num)
  10199. {
  10200. int i;
  10201. for (i=0; i<num && str; i++){
  10202. val[i] = str;
  10203. str = strchr(str,',');
  10204. if (str && i<num-1) //Do not remove a trailing comma.
  10205. *str++=0;
  10206. }
  10207. return i;
  10208. }
  10209. int pc_split_atoi(char* str, int* val, char sep, int max)
  10210. {
  10211. int i,j;
  10212. for (i=0; i<max; i++) {
  10213. if (!str) break;
  10214. val[i] = atoi(str);
  10215. str = strchr(str,sep);
  10216. if (str)
  10217. *str++=0;
  10218. }
  10219. //Zero up the remaining.
  10220. for(j=i; j < max; j++)
  10221. val[j] = 0;
  10222. return i;
  10223. }
  10224. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10225. {
  10226. static int warning=0;
  10227. int i,j;
  10228. for (i=0; i<max; i++) {
  10229. double f;
  10230. if (!str) break;
  10231. f = atof(str);
  10232. if (f < 0)
  10233. val[i] = 0;
  10234. else if (f > UINT_MAX) {
  10235. val[i] = UINT_MAX;
  10236. if (!warning) {
  10237. warning = 1;
  10238. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10239. }
  10240. } else
  10241. val[i] = (unsigned int)f;
  10242. str = strchr(str,sep);
  10243. if (str)
  10244. *str++=0;
  10245. }
  10246. //Zero up the remaining.
  10247. for(j=i; j < max; j++)
  10248. val[j] = 0;
  10249. return i;
  10250. }
  10251. /*==========================================
  10252. * sub DB reading.
  10253. * Function used to read skill_tree.txt
  10254. *------------------------------------------*/
  10255. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10256. {
  10257. uint32 baselv, joblv, baselv_max, joblv_max;
  10258. uint16 skill_id, skill_lv, skill_lv_max;
  10259. int idx, class_;
  10260. unsigned int i, offset, skill_idx;
  10261. class_ = atoi(fields[0]);
  10262. skill_id = (uint16)atoi(fields[1]);
  10263. skill_lv = (uint16)atoi(fields[2]);
  10264. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10265. baselv = (uint32)atoi(fields[3]);
  10266. joblv = (uint32)atoi(fields[4]);
  10267. offset = 5;
  10268. }
  10269. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10270. baselv = joblv = 0;
  10271. offset = 3;
  10272. }
  10273. else {
  10274. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10275. return false;
  10276. }
  10277. if(!pcdb_checkid(class_))
  10278. {
  10279. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10280. return false;
  10281. }
  10282. idx = pc_class2idx(class_);
  10283. if (!skill_get_index(skill_id)) {
  10284. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10285. return false;
  10286. }
  10287. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10288. 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);
  10289. skill_lv = skill_lv_max;
  10290. }
  10291. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10292. 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);
  10293. baselv = baselv_max;
  10294. }
  10295. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10296. 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);
  10297. joblv = joblv_max;
  10298. }
  10299. //This is to avoid adding two lines for the same skill. [Skotlex]
  10300. 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 );
  10301. if( skill_idx == MAX_SKILL_TREE )
  10302. {
  10303. 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_);
  10304. return false;
  10305. }
  10306. else if(skill_tree[idx][skill_idx].skill_id)
  10307. {
  10308. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10309. }
  10310. skill_tree[idx][skill_idx].skill_id = skill_id;
  10311. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10312. skill_tree[idx][skill_idx].baselv = baselv;
  10313. skill_tree[idx][skill_idx].joblv = joblv;
  10314. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10315. {
  10316. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10317. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10318. if (skill_id == 0) {
  10319. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10320. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10321. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10322. }
  10323. continue;
  10324. }
  10325. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10326. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10327. return false;
  10328. }
  10329. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10330. 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);
  10331. skill_lv = skill_lv_max;
  10332. }
  10333. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10334. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10335. }
  10336. return true;
  10337. }
  10338. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10339. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10340. {
  10341. int type, class_, diff;
  10342. type = atoi(fields[0]); //1=experience, 2=item drop
  10343. class_ = atoi(fields[1]);
  10344. diff = atoi(fields[2]);
  10345. if( type != 1 && type != 2 ){
  10346. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10347. return false;
  10348. }
  10349. if( !CHK_CLASS(class_) ){
  10350. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10351. return false;
  10352. }
  10353. diff = min(diff, MAX_LEVEL);
  10354. if( diff < 0 )
  10355. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10356. level_penalty[type][class_][diff] = atoi(fields[3]);
  10357. return true;
  10358. }
  10359. #endif
  10360. /** [Cydh]
  10361. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10362. * @param level Base level of player
  10363. * @param class_ Job ID @see enum e_job
  10364. * @return base_hp
  10365. */
  10366. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10367. double base_hp;
  10368. uint16 i, idx = pc_class2idx(class_);
  10369. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10370. #ifndef RENEWAL
  10371. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10372. #endif
  10373. for (i = 2; i <= level; i++)
  10374. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10375. if (class_ == JOB_SUMMONER)
  10376. base_hp += floor((base_hp / 2) + 0.5);
  10377. return (unsigned int)base_hp;
  10378. }
  10379. /** [Playtester]
  10380. * Calculates base sp of player.
  10381. * @param level Base level of player
  10382. * @param class_ Job ID @see enum e_job
  10383. * @return base_sp
  10384. */
  10385. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10386. double base_sp;
  10387. uint16 idx = pc_class2idx(class_);
  10388. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10389. switch (class_) {
  10390. case JOB_NINJA:
  10391. if (level >= 10)
  10392. base_sp -= 22;
  10393. else
  10394. base_sp = 11 + 3*level;
  10395. break;
  10396. case JOB_GUNSLINGER:
  10397. if (level > 10)
  10398. base_sp -= 18;
  10399. else
  10400. base_sp = 9 + 3*level;
  10401. break;
  10402. case JOB_SUMMONER:
  10403. base_sp -= floor(base_sp / 2);
  10404. break;
  10405. }
  10406. return (unsigned int)base_sp;
  10407. }
  10408. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10409. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10410. int idx, class_;
  10411. unsigned int i;
  10412. class_ = atoi(fields[0]);
  10413. if (!pcdb_checkid(class_)) {
  10414. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10415. return false;
  10416. }
  10417. idx = pc_class2idx(class_);
  10418. job_info[idx].max_weight_base = atoi(fields[1]);
  10419. job_info[idx].hp_factor = atoi(fields[2]);
  10420. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10421. job_info[idx].sp_factor = atoi(fields[4]);
  10422. #ifdef RENEWAL_ASPD
  10423. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10424. #else
  10425. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10426. #endif
  10427. {
  10428. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10429. }
  10430. return true;
  10431. }
  10432. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10433. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10434. {
  10435. int idx, class_, i;
  10436. class_ = atoi(fields[0]);
  10437. if(!pcdb_checkid(class_))
  10438. {
  10439. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10440. return false;
  10441. }
  10442. idx = pc_class2idx(class_);
  10443. for(i = 1; i < columns; i++)
  10444. {
  10445. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10446. }
  10447. return true;
  10448. }
  10449. //Reading job_exp.txt line
  10450. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10451. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10452. {
  10453. int idx, i, type;
  10454. int job_id,job_count,jobs[CLASS_COUNT];
  10455. unsigned int ui, maxlvl;
  10456. maxlvl = atoi(fields[0]);
  10457. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10458. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10459. return false;
  10460. }
  10461. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10462. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10463. return false;
  10464. }
  10465. type = atoi(fields[2]);
  10466. if(type < 0 || type > 1){
  10467. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10468. return false;
  10469. }
  10470. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10471. if (job_count < 1)
  10472. return false;
  10473. job_id = jobs[0];
  10474. if(!pcdb_checkid(job_id)){
  10475. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10476. return false;
  10477. }
  10478. idx = pc_class2idx(job_id);
  10479. job_info[idx].max_level[type] = maxlvl;
  10480. for(i=0; i<maxlvl; i++){
  10481. t_exp exp = strtoull( fields[3 + i], nullptr, 10 );
  10482. if( exp == 0 ){
  10483. ShowWarning( "pc_readdb_job_exp: No value defined for level %d in line %d. Defaulting to MAX_EXP...\n", i + 1, current );
  10484. exp = MAX_EXP;
  10485. }else if( exp > MAX_EXP ){
  10486. ShowWarning( "pc_readdb_job_exp: Value %" PRIu64 " is too high, capping to %" PRIu64 "...\n", exp, MAX_EXP );
  10487. exp = MAX_EXP;
  10488. }
  10489. job_info[idx].exp_table[type][i] = exp;
  10490. }
  10491. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10492. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10493. //should look like this:
  10494. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10495. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10496. job_info[idx].max_level[type]--;
  10497. if (job_info[idx].max_level[type] < maxlvl) {
  10498. 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]);
  10499. ShowInfo("Filling the missing values with the last exp entry.\n");
  10500. //Fill the requested values with the last entry.
  10501. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10502. for (; ui+2 < maxlvl; ui++)
  10503. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10504. job_info[idx].max_level[type] = maxlvl;
  10505. }
  10506. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10507. for (i = 1; i < job_count; i++) {
  10508. job_id = jobs[i];
  10509. if (!pcdb_checkid(job_id)) {
  10510. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10511. continue;
  10512. }
  10513. idx = pc_class2idx(job_id);
  10514. 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]));
  10515. job_info[idx].max_level[type] = maxlvl;
  10516. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10517. }
  10518. return true;
  10519. }
  10520. /**
  10521. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10522. * startlvl,endlvl,class,type,values...
  10523. */
  10524. #ifdef HP_SP_TABLES
  10525. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10526. {
  10527. int i, startlvl, endlvl;
  10528. int job_count,jobs[CLASS_COUNT];
  10529. short type;
  10530. startlvl = atoi(fields[0]);
  10531. if(startlvl<1){
  10532. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10533. return false;
  10534. }
  10535. endlvl = atoi(fields[1]);
  10536. if(endlvl<1 || endlvl<startlvl){
  10537. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10538. return false;
  10539. }
  10540. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10541. 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);
  10542. return false;
  10543. }
  10544. type = atoi(fields[3]);
  10545. if(type < 0 || type > 1){
  10546. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10547. return false;
  10548. }
  10549. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10550. if (job_count < 1)
  10551. return false;
  10552. for (i = 0; i < job_count; i++) {
  10553. int idx, job_id = jobs[i], use_endlvl;
  10554. if (!pcdb_checkid(job_id)) {
  10555. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10556. return false;
  10557. }
  10558. idx = pc_class2idx(job_id);
  10559. if (startlvl > job_info[idx].max_level[0]) {
  10560. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10561. return false;
  10562. }
  10563. //Just read until available max level for this job, don't use MAX_LEVEL!
  10564. use_endlvl = endlvl;
  10565. if (use_endlvl > job_info[idx].max_level[0])
  10566. use_endlvl = job_info[idx].max_level[0];
  10567. if(type == 0) { //hp type
  10568. uint16 j;
  10569. for(j = 0; j < use_endlvl; j++) {
  10570. if (atoi(fields[j+4])) {
  10571. uint16 lvl_idx = startlvl-1+j;
  10572. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10573. //Tells if this HP is lower than previous level (but not for 99->100)
  10574. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10575. 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",
  10576. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10577. }
  10578. }
  10579. }
  10580. else { //sp type
  10581. uint16 j;
  10582. for(j = 0; j < use_endlvl; j++) {
  10583. if (atoi(fields[j+4])) {
  10584. uint16 lvl_idx = startlvl-1+j;
  10585. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10586. //Tells if this SP is lower than previous level (but not for 99->100)
  10587. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10588. 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",
  10589. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10590. }
  10591. }
  10592. }
  10593. }
  10594. return true;
  10595. }
  10596. #endif
  10597. /** [Cydh]
  10598. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10599. */
  10600. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10601. {
  10602. int64 class_tmp;
  10603. int idx, class_;
  10604. uint16 str, agi, vit, int_, dex, luk;
  10605. script_get_constant(trim(fields[0]),&class_tmp);
  10606. class_ = static_cast<int>(class_tmp);
  10607. if ((idx = pc_class2idx(class_)) < 0) {
  10608. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10609. return false;
  10610. }
  10611. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10612. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10613. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10614. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10615. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10616. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10617. job_info[idx].max_param.str = str;
  10618. job_info[idx].max_param.agi = agi;
  10619. job_info[idx].max_param.vit = vit;
  10620. job_info[idx].max_param.int_ = int_;
  10621. job_info[idx].max_param.dex = dex;
  10622. job_info[idx].max_param.luk = luk;
  10623. return true;
  10624. }
  10625. /**
  10626. * Read job_noenter_map.txt
  10627. **/
  10628. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10629. int idx, class_ = -1;
  10630. int64 class_tmp;
  10631. if (ISDIGIT(str[0][0])) {
  10632. class_ = atoi(str[0]);
  10633. } else {
  10634. if (!script_get_constant(str[0], &class_tmp)) {
  10635. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10636. return false;
  10637. }
  10638. class_ = static_cast<int>(class_tmp);
  10639. }
  10640. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10641. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10642. return false;
  10643. }
  10644. job_info[idx].noenter_map.zone = atoi(str[1]);
  10645. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10646. return true;
  10647. }
  10648. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10649. int i=1;
  10650. char line[24000]; //FIXME this seem too big
  10651. FILE *fp;
  10652. sprintf(line, "%s/statpoint.txt", basedir);
  10653. fp=fopen(line,"r");
  10654. if(fp == NULL){
  10655. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10656. return max(last_s,i);
  10657. } else {
  10658. int entries=0;
  10659. while(fgets(line, sizeof(line), fp))
  10660. {
  10661. int stat;
  10662. trim(line);
  10663. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10664. continue;
  10665. if ((stat=strtoul(line,NULL,10))<0)
  10666. stat=0;
  10667. if (i > MAX_LEVEL)
  10668. break;
  10669. statp[i]=stat;
  10670. i++;
  10671. entries++;
  10672. }
  10673. fclose(fp);
  10674. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10675. }
  10676. return max(last_s,i);
  10677. }
  10678. /*==========================================
  10679. * pc DB reading.
  10680. * job_exp.txt - required experience values
  10681. * skill_tree.txt - skill tree for every class
  10682. * attr_fix.txt - elemental adjustment table
  10683. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10684. * job_db2.txt - job,stats bonuses/lvl
  10685. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10686. *------------------------------------------*/
  10687. void pc_readdb(void) {
  10688. int i, k, s = 1;
  10689. const char* dbsubpath[] = {
  10690. "",
  10691. "/" DBIMPORT,
  10692. //add other path here
  10693. };
  10694. //reset
  10695. memset(job_info,0,sizeof(job_info)); // job_info table
  10696. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10697. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10698. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10699. for( k=1; k < 3; k++ ){ // fill in the blanks
  10700. int j;
  10701. for( j = 0; j < CLASS_ALL; j++ ){
  10702. int tmp = 0;
  10703. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10704. if( i == MAX_LEVEL+1 )
  10705. tmp = level_penalty[k][j][0];// reset
  10706. if( level_penalty[k][j][i] > 0 )
  10707. tmp = level_penalty[k][j][i];
  10708. else
  10709. level_penalty[k][j][i] = tmp;
  10710. }
  10711. }
  10712. }
  10713. #endif
  10714. // reset then read statspoint
  10715. memset(statp,0,sizeof(statp));
  10716. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10717. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10718. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10719. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10720. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10721. if(i==0) {
  10722. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10723. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10724. }
  10725. else {
  10726. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10727. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10728. }
  10729. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10730. if (i == 0)
  10731. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10732. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10733. #else
  10734. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10735. #endif
  10736. else
  10737. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10738. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10739. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10740. #ifdef HP_SP_TABLES
  10741. 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!
  10742. #endif
  10743. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10744. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10745. aFree(dbsubpath1);
  10746. aFree(dbsubpath2);
  10747. }
  10748. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10749. memset(skill_tree, 0, sizeof(skill_tree));
  10750. 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);
  10751. 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);
  10752. // generate the remaining parts of the db if necessary
  10753. k = battle_config.use_statpoint_table; //save setting
  10754. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10755. statp[0] = 45; // seed value
  10756. for (; s <= MAX_LEVEL; s++)
  10757. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10758. battle_config.use_statpoint_table = k; //restore setting
  10759. //Checking if all class have their data
  10760. for (i = 0; i < JOB_MAX; i++) {
  10761. int idx;
  10762. uint16 j;
  10763. if (!pcdb_checkid(i))
  10764. continue;
  10765. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10766. continue; //Classes that do not need exp tables.
  10767. idx = pc_class2idx(i);
  10768. if (!job_info[idx].max_level[0])
  10769. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10770. if (!job_info[idx].max_level[1])
  10771. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10772. //Init and checking the empty value of Base HP/SP [Cydh]
  10773. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10774. if (job_info[idx].base_hp[j] == 0)
  10775. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10776. if (job_info[idx].base_sp[j] == 0)
  10777. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10778. }
  10779. }
  10780. }
  10781. // Read MOTD on startup. [Valaris]
  10782. int pc_read_motd(void)
  10783. {
  10784. FILE* fp;
  10785. // clear old MOTD
  10786. memset(motd_text, 0, sizeof(motd_text));
  10787. // read current MOTD
  10788. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10789. {
  10790. unsigned int entries = 0;
  10791. char buf[CHAT_SIZE_MAX];
  10792. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10793. {
  10794. unsigned int lines = 0;
  10795. size_t len;
  10796. lines++;
  10797. if( buf[0] == '/' && buf[1] == '/' )
  10798. continue;
  10799. len = strlen(buf);
  10800. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10801. len--;
  10802. if( len ) {
  10803. char * ptr;
  10804. buf[len] = 0;
  10805. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10806. 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);
  10807. }
  10808. else {// empty line
  10809. buf[0] = ' ';
  10810. buf[1] = 0;
  10811. }
  10812. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10813. entries++;
  10814. }
  10815. fclose(fp);
  10816. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10817. }
  10818. else
  10819. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10820. return 0;
  10821. }
  10822. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10823. int i,cursor = 0;
  10824. struct item_cd* cd = NULL;
  10825. if( load ) {
  10826. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10827. // no item cooldown is associated with this character
  10828. return;
  10829. }
  10830. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10831. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10832. sd->item_delay[cursor].tick = cd->tick[i];
  10833. sd->item_delay[cursor].nameid = cd->nameid[i];
  10834. cursor++;
  10835. }
  10836. }
  10837. idb_remove(itemcd_db,sd->status.char_id);
  10838. } else {
  10839. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10840. // create a new skill cooldown object for map storage
  10841. CREATE( cd, struct item_cd, 1 );
  10842. idb_put( itemcd_db, sd->status.char_id, cd );
  10843. }
  10844. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10845. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10846. cd->tick[cursor] = sd->item_delay[i].tick;
  10847. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10848. cursor++;
  10849. }
  10850. }
  10851. }
  10852. return;
  10853. }
  10854. /**
  10855. * Add item delay to player's item delay data
  10856. * @param sd Player
  10857. * @param id Item data
  10858. * @param tick Current tick
  10859. * @param n Item index in inventory
  10860. * @return 0: No delay, can consume item.
  10861. * 1: Has delay, cancel consumption.
  10862. **/
  10863. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10864. int i;
  10865. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10866. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10867. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10868. if( i < MAX_ITEMDELAYS ) {
  10869. if( sd->item_delay[i].nameid ) {// found
  10870. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10871. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10872. char e_msg[CHAT_SIZE_MAX];
  10873. if( e_tick > 99 )
  10874. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10875. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10876. else
  10877. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10878. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10879. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10880. return 1; // Delay has not expired yet
  10881. }
  10882. } else {// not yet used item (all slots are initially empty)
  10883. sd->item_delay[i].nameid = id->nameid;
  10884. }
  10885. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10886. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10887. } else {// should not happen
  10888. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  10889. }
  10890. //clean up used delays so we can give room for more
  10891. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10892. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10893. sd->item_delay[i].tick = 0;
  10894. sd->item_delay[i].nameid = 0;
  10895. }
  10896. }
  10897. return 0;
  10898. }
  10899. /**
  10900. * Check if player has delay to reuse item
  10901. * @param sd Player
  10902. * @param id Item data
  10903. * @param tick Current tick
  10904. * @param n Item index in inventory
  10905. * @return 0: No delay, can consume item.
  10906. * 1: Has delay, cancel consumption.
  10907. **/
  10908. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10909. struct status_change *sc = NULL;
  10910. nullpo_retr(0, sd);
  10911. nullpo_retr(0, id);
  10912. // Do normal delay assignment
  10913. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10914. return pc_itemcd_add(sd, id, tick, n);
  10915. // Send reply of delay remains
  10916. if (sc->data[id->delay_sc]) {
  10917. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10918. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10919. return 1;
  10920. }
  10921. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10922. return 0;
  10923. }
  10924. /**
  10925. * Clear the dmglog data from player
  10926. * @param sd
  10927. * @param md
  10928. **/
  10929. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10930. {
  10931. uint8 i;
  10932. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10933. if (i < DAMAGELOG_SIZE) {
  10934. md->dmglog[i].id = 0;
  10935. md->dmglog[i].dmg = 0;
  10936. md->dmglog[i].flag = 0;
  10937. }
  10938. }
  10939. /**
  10940. * Add log to player's dmglog
  10941. * @param sd
  10942. * @param id Monster's GID
  10943. **/
  10944. void pc_damage_log_add(struct map_session_data *sd, int id)
  10945. {
  10946. uint8 i = 0;
  10947. if (!sd || !id)
  10948. return;
  10949. //Only store new data, don't need to renew the old one with same id
  10950. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10951. if (i < DAMAGELOG_SIZE_PC)
  10952. return;
  10953. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10954. if (sd->dmglog[i] == 0) {
  10955. sd->dmglog[i] = id;
  10956. return;
  10957. }
  10958. }
  10959. }
  10960. /**
  10961. * Clear dmglog data from player
  10962. * @param sd
  10963. * @param id Monster's id
  10964. **/
  10965. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10966. {
  10967. uint8 i;
  10968. struct mob_data *md = NULL;
  10969. if (!sd)
  10970. return;
  10971. if (!id) {
  10972. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10973. if( !sd->dmglog[i] ) //skip the empty value
  10974. continue;
  10975. if ((md = map_id2md(sd->dmglog[i])))
  10976. pc_clear_log_damage_sub(sd->status.char_id,md);
  10977. sd->dmglog[i] = 0;
  10978. }
  10979. }
  10980. else {
  10981. if ((md = map_id2md(id)))
  10982. pc_clear_log_damage_sub(sd->status.char_id,md);
  10983. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10984. if (i < DAMAGELOG_SIZE_PC)
  10985. sd->dmglog[i] = 0;
  10986. }
  10987. }
  10988. /**
  10989. * Status change data arrived from char-server
  10990. * @param sd: Player data
  10991. */
  10992. void pc_scdata_received(struct map_session_data *sd) {
  10993. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10994. clif_weight_limit( sd );
  10995. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10996. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10997. }
  10998. sd->state.pc_loaded = true;
  10999. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  11000. sd->state.connect_new = 1;
  11001. clif_parse_LoadEndAck(sd->fd, sd);
  11002. }
  11003. if (pc_iscarton(sd)) {
  11004. sd->cart_weight_max = 0; // Force a client refesh
  11005. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  11006. }
  11007. if (sd->sc.data[SC_SOULENERGY])
  11008. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  11009. }
  11010. /**
  11011. * Check player account expiration time and rental item expirations
  11012. * @param sd: Player data
  11013. */
  11014. void pc_check_expiration(struct map_session_data *sd) {
  11015. #ifndef ENABLE_SC_SAVING
  11016. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  11017. #endif
  11018. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  11019. time_t exp_time = sd->expiration_time;
  11020. char tmpstr[1024];
  11021. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  11022. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  11023. pc_expire_check(sd);
  11024. }
  11025. }
  11026. TIMER_FUNC(pc_expiration_timer){
  11027. struct map_session_data *sd = map_id2sd(id);
  11028. if( !sd ) return 0;
  11029. sd->expiration_tid = INVALID_TIMER;
  11030. if( sd->fd )
  11031. clif_authfail_fd(sd->fd,10);
  11032. map_quit(sd);
  11033. return 0;
  11034. }
  11035. TIMER_FUNC(pc_autotrade_timer){
  11036. struct map_session_data *sd = map_id2sd(id);
  11037. if (!sd)
  11038. return 0;
  11039. sd->autotrade_tid = INVALID_TIMER;
  11040. if (sd->state.autotrade&2)
  11041. vending_reopen(sd);
  11042. if (sd->state.autotrade&4)
  11043. buyingstore_reopen(sd);
  11044. if (!sd->vender_id && !sd->buyer_id) {
  11045. sd->state.autotrade = 0;
  11046. map_quit(sd);
  11047. }
  11048. return 0;
  11049. }
  11050. /* this timer exists only when a character with a expire timer > 24h is online */
  11051. /* it loops thru online players once an hour to check whether a new < 24h is available */
  11052. TIMER_FUNC(pc_global_expiration_timer){
  11053. struct s_mapiterator* iter;
  11054. struct map_session_data* sd;
  11055. iter = mapit_getallusers();
  11056. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11057. if( sd->expiration_time )
  11058. pc_expire_check(sd);
  11059. mapit_free(iter);
  11060. return 0;
  11061. }
  11062. void pc_expire_check(struct map_session_data *sd) {
  11063. /* ongoing timer */
  11064. if( sd->expiration_tid != INVALID_TIMER )
  11065. return;
  11066. /* not within the next 24h, enable the global check */
  11067. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  11068. /* global check not running, enable */
  11069. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  11070. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  11071. return;
  11072. }
  11073. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  11074. }
  11075. /**
  11076. * Deposit some money to bank
  11077. * @param sd
  11078. * @param money Amount of money to deposit
  11079. **/
  11080. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  11081. unsigned int limit_check = money + sd->bank_vault;
  11082. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  11083. return BDA_OVERFLOW;
  11084. } else if ( money > sd->status.zeny ) {
  11085. return BDA_NO_MONEY;
  11086. }
  11087. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11088. return BDA_NO_MONEY;
  11089. sd->bank_vault += money;
  11090. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11091. if( save_settings&CHARSAVE_BANK )
  11092. chrif_save(sd, CSAVE_NORMAL);
  11093. return BDA_SUCCESS;
  11094. }
  11095. /**
  11096. * Withdraw money from bank
  11097. * @param sd
  11098. * @param money Amount of money that will be withdrawn
  11099. **/
  11100. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  11101. unsigned int limit_check = money + sd->status.zeny;
  11102. if( money <= 0 ) {
  11103. return BWA_UNKNOWN_ERROR;
  11104. } else if ( money > sd->bank_vault ) {
  11105. return BWA_NO_MONEY;
  11106. } else if ( limit_check > MAX_ZENY ) {
  11107. /* no official response for this scenario exists. */
  11108. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  11109. return BWA_UNKNOWN_ERROR;
  11110. }
  11111. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  11112. return BWA_NO_MONEY;
  11113. sd->bank_vault -= money;
  11114. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  11115. if( save_settings&CHARSAVE_BANK )
  11116. chrif_save(sd, CSAVE_NORMAL);
  11117. return BWA_SUCCESS;
  11118. }
  11119. /**
  11120. * Clear Crimson Marker data from caster
  11121. * @param sd: Player
  11122. **/
  11123. void pc_crimson_marker_clear(struct map_session_data *sd) {
  11124. uint8 i;
  11125. if (!sd)
  11126. return;
  11127. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  11128. struct block_list *bl = NULL;
  11129. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  11130. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  11131. sd->c_marker[i] = 0;
  11132. }
  11133. }
  11134. /**
  11135. * Show version to player
  11136. * @param sd: Player
  11137. **/
  11138. void pc_show_version(struct map_session_data *sd) {
  11139. const char* svn = get_svn_revision();
  11140. char buf[CHAT_SIZE_MAX];
  11141. if( svn[0] != UNKNOWN_VERSION )
  11142. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  11143. else {
  11144. const char* git = get_git_hash();
  11145. if( git[0] != UNKNOWN_VERSION )
  11146. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  11147. else
  11148. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  11149. }
  11150. clif_displaymessage(sd->fd,buf);
  11151. }
  11152. /**
  11153. * Run bonus_script on player
  11154. * @param sd
  11155. * @author [Cydh]
  11156. **/
  11157. void pc_bonus_script(struct map_session_data *sd) {
  11158. t_tick now = gettick();
  11159. struct linkdb_node *node = NULL, *next = NULL;
  11160. if (!sd || !(node = sd->bonus_script.head))
  11161. return;
  11162. while (node) {
  11163. struct s_bonus_script_entry *entry = NULL;
  11164. next = node->next;
  11165. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  11166. // Only start timer for new bonus_script
  11167. if (entry->tid == INVALID_TIMER) {
  11168. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  11169. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  11170. entry->tick += now;
  11171. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  11172. }
  11173. if (entry->script)
  11174. run_script(entry->script, 0, sd->bl.id, 0);
  11175. else
  11176. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  11177. }
  11178. node = next;
  11179. }
  11180. }
  11181. /**
  11182. * Add bonus_script to player
  11183. * @param sd Player
  11184. * @param script_str Script string
  11185. * @param dur Duration in ms
  11186. * @param icon EFST
  11187. * @param flag Flags @see enum e_bonus_script_flags
  11188. * @param type 0 - None, 1 - Buff, 2 - Debuff
  11189. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  11190. * @author [Cydh]
  11191. **/
  11192. 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) {
  11193. struct script_code *script = NULL;
  11194. struct linkdb_node *node = NULL;
  11195. struct s_bonus_script_entry *entry = NULL;
  11196. if (!sd)
  11197. return NULL;
  11198. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11199. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11200. return NULL;
  11201. }
  11202. // Duplication checks
  11203. if ((node = sd->bonus_script.head)) {
  11204. while (node) {
  11205. entry = (struct s_bonus_script_entry *)node->data;
  11206. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11207. t_tick newdur = gettick() + dur;
  11208. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11209. sett_tickimer(entry->tid, newdur);
  11210. script_free_code(script);
  11211. return NULL;
  11212. }
  11213. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11214. break;
  11215. else { // No duplicate bonus
  11216. script_free_code(script);
  11217. return NULL;
  11218. }
  11219. }
  11220. node = node->next;
  11221. }
  11222. }
  11223. CREATE(entry, struct s_bonus_script_entry, 1);
  11224. entry->script_buf = StringBuf_Malloc();
  11225. StringBuf_AppendStr(entry->script_buf, script_str);
  11226. entry->tid = INVALID_TIMER;
  11227. entry->flag = flag;
  11228. entry->icon = icon;
  11229. entry->tick = dur; // Use duration first, on run change to expire time
  11230. entry->type = type;
  11231. entry->script = script;
  11232. sd->bonus_script.count++;
  11233. return entry;
  11234. }
  11235. /**
  11236. * Remove bonus_script data from player
  11237. * @param sd: Target player
  11238. * @param list: Bonus script entry from player
  11239. * @author [Cydh]
  11240. **/
  11241. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11242. if (entry->tid != INVALID_TIMER)
  11243. delete_timer(entry->tid, pc_bonus_script_timer);
  11244. if (entry->script)
  11245. script_free_code(entry->script);
  11246. if (entry->script_buf)
  11247. StringBuf_Free(entry->script_buf);
  11248. if (sd) {
  11249. if (entry->icon != EFST_BLANK)
  11250. clif_status_load(&sd->bl, entry->icon, 0);
  11251. if (sd->bonus_script.count > 0)
  11252. sd->bonus_script.count--;
  11253. }
  11254. aFree(entry);
  11255. }
  11256. /**
  11257. * Do final process if no entry left
  11258. * @param sd
  11259. **/
  11260. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11261. if (sd->bonus_script.count == 0) {
  11262. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11263. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11264. linkdb_final(&sd->bonus_script.head);
  11265. }
  11266. }
  11267. /**
  11268. * Timer for bonus_script
  11269. * @param tid
  11270. * @param tick
  11271. * @param id
  11272. * @param data
  11273. * @author [Cydh]
  11274. **/
  11275. TIMER_FUNC(pc_bonus_script_timer){
  11276. struct map_session_data *sd;
  11277. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11278. sd = map_id2sd(id);
  11279. if (!sd) {
  11280. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11281. return 0;
  11282. }
  11283. if (tid == INVALID_TIMER)
  11284. return 0;
  11285. if (!sd->bonus_script.head || entry == NULL) {
  11286. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11287. return 0;
  11288. }
  11289. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11290. pc_bonus_script_free_entry(sd, entry);
  11291. pc_bonus_script_check_final(sd);
  11292. status_calc_pc(sd,SCO_NONE);
  11293. return 0;
  11294. }
  11295. /**
  11296. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11297. * @param sd: Target player
  11298. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11299. * @author [Cydh]
  11300. **/
  11301. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11302. struct linkdb_node *node = NULL;
  11303. uint16 count = 0;
  11304. if (!sd || !(node = sd->bonus_script.head))
  11305. return;
  11306. while (node) {
  11307. struct linkdb_node *next = node->next;
  11308. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11309. if (entry && (
  11310. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11311. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11312. (flag&entry->flag) || // Matched flag
  11313. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11314. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11315. )
  11316. )
  11317. {
  11318. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11319. pc_bonus_script_free_entry(sd, entry);
  11320. count++;
  11321. }
  11322. node = next;
  11323. }
  11324. pc_bonus_script_check_final(sd);
  11325. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11326. status_calc_pc(sd,SCO_NONE);
  11327. }
  11328. /** [Cydh]
  11329. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11330. * @param sd: Target player
  11331. */
  11332. void pc_cell_basilica(struct map_session_data *sd) {
  11333. nullpo_retv(sd);
  11334. #ifdef RENEWAL
  11335. enum sc_type type = SC_BASILICA_CELL;
  11336. #else
  11337. enum sc_type type = SC_BASILICA;
  11338. #endif
  11339. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11340. if (sd->sc.data[type])
  11341. status_change_end(&sd->bl, type,INVALID_TIMER);
  11342. }
  11343. else if (!sd->sc.data[type])
  11344. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11345. }
  11346. /** [Cydh]
  11347. * Get maximum specified parameter for specified class
  11348. * @param class_: sd->class
  11349. * @param sex: sd->status.sex
  11350. * @param flag: parameter will be checked
  11351. * @return max_param
  11352. */
  11353. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11354. int idx = -1, class_ = sd->class_;
  11355. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11356. short max_param = 0;
  11357. switch (param) {
  11358. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11359. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11360. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11361. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11362. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11363. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11364. }
  11365. if (max_param > 0)
  11366. return max_param;
  11367. }
  11368. // Always check babies first
  11369. if( class_ & JOBL_BABY ){
  11370. if( class_ & JOBL_THIRD ){
  11371. return battle_config.max_baby_third_parameter;
  11372. }else{
  11373. return battle_config.max_baby_parameter;
  11374. }
  11375. }
  11376. // Summoner
  11377. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11378. return battle_config.max_summoner_parameter;
  11379. }
  11380. // Extended classes
  11381. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11382. return battle_config.max_extended_parameter;
  11383. }
  11384. // 3rd class
  11385. if( class_ & JOBL_THIRD ){
  11386. // Transcendent
  11387. if( class_ & JOBL_UPPER ){
  11388. return battle_config.max_third_trans_parameter;
  11389. }else{
  11390. return battle_config.max_third_parameter;
  11391. }
  11392. }
  11393. // Transcendent
  11394. if( class_ & JOBL_UPPER ){
  11395. return battle_config.max_trans_parameter;
  11396. }
  11397. return battle_config.max_parameter;
  11398. }
  11399. /**
  11400. * Get max ASPD for player based on Class
  11401. * @param sd Player
  11402. * @return ASPD
  11403. */
  11404. short pc_maxaspd(struct map_session_data *sd) {
  11405. nullpo_ret(sd);
  11406. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11407. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11408. battle_config.max_aspd ));
  11409. }
  11410. /**
  11411. * Calculates total item-group related bonuses for the given item
  11412. * @param sd Player
  11413. * @param nameid Item ID
  11414. * @return Heal rate
  11415. **/
  11416. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid) {
  11417. if (sd->itemgrouphealrate.empty())
  11418. return 0;
  11419. short bonus = 0;
  11420. for (const auto &it : sd->itemgrouphealrate) {
  11421. uint16 group_id = it.id, i;
  11422. struct s_item_group_db *group = NULL;
  11423. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11424. continue;
  11425. for (i = 0; i < group->random[0].data_qty; i++) {
  11426. if (group->random[0].data[i].nameid == nameid) {
  11427. bonus += it.val;
  11428. break;
  11429. }
  11430. }
  11431. }
  11432. return bonus;
  11433. }
  11434. /**
  11435. * Calculates total item-group related bonuses for the given item group
  11436. * @param sd Player
  11437. * @param group_id Item Group ID
  11438. * @return Heal rate
  11439. **/
  11440. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11441. if (sd->itemgrouphealrate.empty())
  11442. return 0;
  11443. for (const auto &it : sd->itemgrouphealrate) {
  11444. if (it.id == group_id)
  11445. return it.val;
  11446. }
  11447. return 0;
  11448. }
  11449. /**
  11450. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11451. * @param eqi Item EQI of enum equip_index
  11452. * @param *equip_index Player's equip_index[]
  11453. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11454. * @return True if item in same inventory index, False if doesn't
  11455. */
  11456. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11457. if (index < 0 || index >= MAX_INVENTORY)
  11458. return true;
  11459. // Dual weapon checks
  11460. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11461. return true;
  11462. // Headgear with Mid & Low location
  11463. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11464. return true;
  11465. // Headgear with Top & Mid or Low location
  11466. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11467. return true;
  11468. // Headgear with Mid & Low location
  11469. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11470. return true;
  11471. // Headgear with Top & Mid or Low location
  11472. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11473. return true;
  11474. return false;
  11475. }
  11476. /**
  11477. * Generate Unique item ID for player
  11478. * @param sd : Player
  11479. * @return A generated Unique item ID
  11480. */
  11481. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11482. nullpo_ret(sd);
  11483. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11484. }
  11485. /**
  11486. * Validating skill from player after logged on
  11487. * @param sd
  11488. **/
  11489. void pc_validate_skill(struct map_session_data *sd) {
  11490. if (sd) {
  11491. uint16 i = 0, count = 0;
  11492. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11493. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11494. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11495. for (i = 0; i < MAX_SKILL; i++) {
  11496. uint16 idx = 0;
  11497. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11498. continue;
  11499. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11500. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11501. count++;
  11502. }
  11503. else
  11504. 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);
  11505. }
  11506. }
  11507. }
  11508. /**
  11509. * Toggle to remember if the questinfo is displayed yet or not.
  11510. * @param qi_display Display flag
  11511. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11512. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11513. **/
  11514. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11515. if (show) {
  11516. // Check if need to be displayed
  11517. if ((*qi_display) != 1) {
  11518. (*qi_display) = 1;
  11519. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11520. }
  11521. }
  11522. else {
  11523. // Check if need to be hide
  11524. if ((*qi_display) != 0) {
  11525. (*qi_display) = 0;
  11526. #if PACKETVER >= 20120410
  11527. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11528. #else
  11529. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11530. #endif
  11531. }
  11532. }
  11533. }
  11534. /**
  11535. * Show available NPC Quest / Event Icon Check [Kisuka]
  11536. * @param sd Player
  11537. **/
  11538. void pc_show_questinfo(struct map_session_data *sd) {
  11539. #if PACKETVER >= 20090218
  11540. nullpo_retv(sd);
  11541. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11542. return;
  11543. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11544. nullpo_retv(mapdata);
  11545. if (mapdata->qi_data.empty())
  11546. return;
  11547. if (mapdata->qi_data.size() != sd->qi_count)
  11548. return; // init was not called yet
  11549. struct s_questinfo *qi = nullptr;
  11550. bool show;
  11551. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11552. qi = &mapdata->qi_data[i];
  11553. if (!qi)
  11554. continue;
  11555. if (!qi->condition)
  11556. show = true;
  11557. else {
  11558. if (achievement_check_condition(qi->condition, sd))
  11559. show = true;
  11560. else
  11561. show = false;
  11562. }
  11563. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11564. }
  11565. #endif
  11566. }
  11567. /**
  11568. * Reinit the questinfo for player when changing map
  11569. * @param sd Player
  11570. **/
  11571. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11572. #if PACKETVER >= 20090218
  11573. nullpo_retv(sd);
  11574. if (sd->qi_display) {
  11575. aFree(sd->qi_display);
  11576. sd->qi_display = nullptr;
  11577. }
  11578. sd->qi_count = 0;
  11579. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11580. return;
  11581. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11582. nullpo_retv(mapdata);
  11583. if (mapdata->qi_data.empty())
  11584. return;
  11585. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11586. #endif
  11587. }
  11588. /**
  11589. * Check if a job is allowed to enter the map
  11590. * @param jobid Job ID see enum e_job or sd->status.class_
  11591. * @param m ID -an index- for direct indexing map[] array
  11592. * @return 1 if job is allowed, 0 otherwise
  11593. **/
  11594. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11595. uint16 idx = 0;
  11596. // Map is other map server.
  11597. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11598. if (m < 0)
  11599. return true;
  11600. struct map_data *mapdata = map_getmapdata(m);
  11601. if (!mapdata->cell)
  11602. return false;
  11603. if (!pcdb_checkid(jobid))
  11604. return false;
  11605. idx = pc_class2idx(jobid);
  11606. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11607. return true;
  11608. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11609. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11610. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11611. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11612. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11613. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11614. )
  11615. return false;
  11616. return true;
  11617. }
  11618. /**
  11619. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11620. * @param sd
  11621. **/
  11622. void pc_set_costume_view(struct map_session_data *sd) {
  11623. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11624. struct item_data *id = NULL;
  11625. nullpo_retv(sd);
  11626. head_low = sd->status.head_bottom;
  11627. head_mid = sd->status.head_mid;
  11628. head_top = sd->status.head_top;
  11629. robe = sd->status.robe;
  11630. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11631. //Added check to prevent sending the same look on multiple slots ->
  11632. //causes client to redraw item on top of itself. (suggested by Lupus)
  11633. // Normal headgear checks
  11634. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11635. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11636. sd->status.head_bottom = id->look;
  11637. else
  11638. sd->status.head_bottom = 0;
  11639. }
  11640. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11641. if (!(id->equip&(EQP_HEAD_TOP)))
  11642. sd->status.head_mid = id->look;
  11643. else
  11644. sd->status.head_mid = 0;
  11645. }
  11646. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11647. sd->status.head_top = id->look;
  11648. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11649. sd->status.robe = id->look;
  11650. // Costumes check
  11651. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11652. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11653. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11654. sd->status.head_bottom = id->look;
  11655. else
  11656. sd->status.head_bottom = 0;
  11657. }
  11658. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11659. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11660. sd->status.head_mid = id->look;
  11661. else
  11662. sd->status.head_mid = 0;
  11663. }
  11664. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11665. sd->status.head_top = id->look;
  11666. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11667. sd->status.robe = id->look;
  11668. }
  11669. if (sd->setlook_head_bottom)
  11670. sd->status.head_bottom = sd->setlook_head_bottom;
  11671. if (sd->setlook_head_mid)
  11672. sd->status.head_mid = sd->setlook_head_mid;
  11673. if (sd->setlook_head_top)
  11674. sd->status.head_top = sd->setlook_head_top;
  11675. if (sd->setlook_robe)
  11676. sd->status.robe = sd->setlook_robe;
  11677. if (head_low != sd->status.head_bottom)
  11678. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11679. if (head_mid != sd->status.head_mid)
  11680. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11681. if (head_top != sd->status.head_top)
  11682. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11683. if (robe != sd->status.robe)
  11684. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11685. }
  11686. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11687. uint32 date = date_get(DT_YYYYMMDD);
  11688. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11689. std::shared_ptr<s_attendance_period> period = pair.second;
  11690. if( period->start <= date && period->end >= date ){
  11691. return period;
  11692. }
  11693. }
  11694. return nullptr;
  11695. }
  11696. bool pc_attendance_enabled(){
  11697. // Check if the attendance feature is disabled
  11698. if( !battle_config.feature_attendance ){
  11699. return false;
  11700. }
  11701. // Check if there is a running attendance period
  11702. return pc_attendance_period() != nullptr;
  11703. }
  11704. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11705. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11706. }
  11707. int32 pc_attendance_counter( struct map_session_data* sd ){
  11708. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11709. // No running attendance period
  11710. if( period == nullptr ){
  11711. return 0;
  11712. }
  11713. // Get the counter for the current period
  11714. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11715. // Check if we have a remaining counter from a previous period
  11716. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11717. // Reset the counter to zero
  11718. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11719. return 0;
  11720. }
  11721. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11722. }
  11723. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11724. // If the user's group does not have the permission
  11725. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11726. return;
  11727. }
  11728. // Check if the attendance feature is disabled
  11729. if( !pc_attendance_enabled() ){
  11730. return;
  11731. }
  11732. // Check if the user already got his reward today
  11733. if( pc_attendance_rewarded_today( sd ) ){
  11734. return;
  11735. }
  11736. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11737. attendance_counter += 1;
  11738. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11739. if( period == nullptr ){
  11740. return;
  11741. }
  11742. if( period->rewards.size() < attendance_counter ){
  11743. return;
  11744. }
  11745. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11746. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11747. if( save_settings&CHARSAVE_ATTENDANCE )
  11748. chrif_save(sd, CSAVE_NORMAL);
  11749. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11750. struct mail_message msg;
  11751. memset( &msg, 0, sizeof( struct mail_message ) );
  11752. msg.dest_id = sd->status.char_id;
  11753. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11754. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11755. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11756. msg.item[0].nameid = reward->item_id;
  11757. msg.item[0].amount = reward->amount;
  11758. msg.item[0].identify = 1;
  11759. msg.status = MAIL_NEW;
  11760. msg.type = MAIL_INBOX_NORMAL;
  11761. msg.timestamp = time(NULL);
  11762. intif_Mail_send(0, &msg);
  11763. clif_attendence_response( sd, attendance_counter );
  11764. }
  11765. /*==========================================
  11766. * pc Init/Terminate
  11767. *------------------------------------------*/
  11768. void do_final_pc(void) {
  11769. db_destroy(itemcd_db);
  11770. do_final_pc_groups();
  11771. ers_destroy(pc_sc_display_ers);
  11772. ers_destroy(num_reg_ers);
  11773. ers_destroy(str_reg_ers);
  11774. attendance_db.clear();
  11775. }
  11776. void do_init_pc(void) {
  11777. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11778. pc_readdb();
  11779. pc_read_motd(); // Read MOTD [Valaris]
  11780. attendance_db.load();
  11781. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11782. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11783. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11784. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11785. add_timer_func_list(pc_autosave, "pc_autosave");
  11786. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11787. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11788. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11789. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11790. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11791. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11792. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11793. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  11794. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11795. // 0=day, 1=night [Yor]
  11796. night_flag = battle_config.night_at_start ? 1 : 0;
  11797. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11798. int day_duration = battle_config.day_duration;
  11799. int night_duration = battle_config.night_duration;
  11800. // add night/day timer [Yor]
  11801. add_timer_func_list(map_day_timer, "map_day_timer");
  11802. add_timer_func_list(map_night_timer, "map_night_timer");
  11803. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11804. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11805. }
  11806. do_init_pc_groups();
  11807. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11808. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11809. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11810. ers_chunk_size(pc_sc_display_ers, 150);
  11811. ers_chunk_size(num_reg_ers, 300);
  11812. ers_chunk_size(str_reg_ers, 50);
  11813. }