pc.cpp 395 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868
  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/ers.hpp" // ers_destroy
  11. #include "../common/malloc.hpp"
  12. #include "../common/mmo.hpp" //NAME_LENGTH
  13. #include "../common/nullpo.hpp"
  14. #include "../common/random.hpp"
  15. #include "../common/showmsg.hpp"
  16. #include "../common/socket.hpp" // session[]
  17. #include "../common/strlib.hpp" // safestrncpy()
  18. #include "../common/timer.hpp"
  19. #include "../common/utilities.hpp"
  20. #include "../common/utils.hpp"
  21. #include "achievement.hpp"
  22. #include "atcommand.hpp" // get_atcommand_level()
  23. #include "battle.hpp" // battle_config
  24. #include "battleground.hpp"
  25. #include "buyingstore.hpp" // struct s_buyingstore
  26. #include "channel.hpp"
  27. #include "chat.hpp"
  28. #include "chrif.hpp"
  29. #include "clan.hpp"
  30. #include "clif.hpp"
  31. #include "date.hpp" // is_day_of_*()
  32. #include "duel.hpp"
  33. #include "elemental.hpp"
  34. #include "guild.hpp"
  35. #include "homunculus.hpp"
  36. #include "instance.hpp"
  37. #include "intif.hpp"
  38. #include "itemdb.hpp" // MAX_ITEMGROUP
  39. #include "log.hpp"
  40. #include "map.hpp"
  41. #include "mercenary.hpp"
  42. #include "mob.hpp"
  43. #include "npc.hpp"
  44. #include "party.hpp" // party_search()
  45. #include "pc_groups.hpp"
  46. #include "pet.hpp" // pet_unlocktarget()
  47. #include "quest.hpp"
  48. #include "script.hpp" // struct script_reg, struct script_regstr
  49. #include "searchstore.hpp" // struct s_search_store_info
  50. #include "status.hpp" // OPTION_*, struct weapon_atk
  51. #include "storage.hpp"
  52. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  53. #include "vending.hpp" // struct s_vending
  54. using namespace rathena;
  55. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  56. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  57. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  58. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  59. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  60. static unsigned int statp[MAX_LEVEL+1];
  61. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  62. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  63. #endif
  64. // h-files are for declarations, not for implementations... [Shinomori]
  65. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  66. // timer for night.day implementation
  67. int day_timer_tid = INVALID_TIMER;
  68. int night_timer_tid = INVALID_TIMER;
  69. struct eri *pc_sc_display_ers = NULL;
  70. struct eri *num_reg_ers;
  71. struct eri *str_reg_ers;
  72. int pc_expiration_tid = INVALID_TIMER;
  73. struct fame_list smith_fame_list[MAX_FAME_LIST];
  74. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  75. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  76. struct s_job_info job_info[CLASS_COUNT];
  77. struct s_attendance_reward{
  78. uint16 item_id;
  79. uint16 amount;
  80. };
  81. struct s_attendance_period{
  82. uint32 start;
  83. uint32 end;
  84. std::map<int,struct s_attendance_reward> rewards;
  85. };
  86. std::vector<struct s_attendance_period> attendance_periods;
  87. #define MOTD_LINE_SIZE 128
  88. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  89. bool reg_load;
  90. /**
  91. * Translation table from athena equip index to aegis bitmask
  92. */
  93. unsigned int equip_bitmask[EQI_MAX] = {
  94. EQP_ACC_L, // EQI_ACC_L
  95. EQP_ACC_R, // EQI_ACC_R
  96. EQP_SHOES, // EQI_SHOES
  97. EQP_GARMENT, // EQI_GARMENT
  98. EQP_HEAD_LOW, // EQI_HEAD_LOW
  99. EQP_HEAD_MID, // EQI_HEAD_MID
  100. EQP_HEAD_TOP, // EQI_HEAD_TOP
  101. EQP_ARMOR, // EQI_ARMOR
  102. EQP_HAND_L, // EQI_HAND_L
  103. EQP_HAND_R, // EQI_HAND_R
  104. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  105. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  106. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  107. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  108. EQP_AMMO, // EQI_AMMO
  109. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  110. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  111. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  112. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  113. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  114. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  115. };
  116. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  117. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  118. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  119. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  120. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  121. };
  122. void pc_set_reg_load( bool val ){
  123. reg_load = val;
  124. }
  125. /**
  126. * Item Cool Down Delay Saving
  127. * Struct item_cd is not a member of struct map_session_data
  128. * to keep cooldowns in memory between player log-ins.
  129. * All cooldowns are reset when server is restarted.
  130. **/
  131. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  132. struct item_cd {
  133. unsigned int tick[MAX_ITEMDELAYS]; //tick
  134. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  135. };
  136. /**
  137. * Converts a class to its array index for CLASS_COUNT defined arrays.
  138. * Note that it does not do a validity check for speed purposes, where parsing
  139. * player input make sure to use a pcdb_checkid first!
  140. * @param class_ Job ID see enum e_job
  141. * @return Class Index
  142. */
  143. int pc_class2idx(int class_) {
  144. if (class_ >= JOB_NOVICE_HIGH)
  145. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  146. return class_;
  147. }
  148. /**
  149. * Get player's group ID
  150. * @param sd
  151. * @return Group ID
  152. */
  153. int pc_get_group_id(struct map_session_data *sd) {
  154. return sd->group_id;
  155. }
  156. /** Get player's group Level
  157. * @param sd
  158. * @return Group Level
  159. */
  160. int pc_get_group_level(struct map_session_data *sd) {
  161. return sd->group_level;
  162. }
  163. static TIMER_FUNC(pc_invincible_timer){
  164. struct map_session_data *sd;
  165. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  166. return 1;
  167. if(sd->invincible_timer != tid){
  168. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  169. return 0;
  170. }
  171. sd->invincible_timer = INVALID_TIMER;
  172. skill_unit_move(&sd->bl,tick,1);
  173. return 0;
  174. }
  175. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  176. nullpo_retv(sd);
  177. if( sd->invincible_timer != INVALID_TIMER )
  178. delete_timer(sd->invincible_timer,pc_invincible_timer);
  179. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  180. }
  181. void pc_delinvincibletimer(struct map_session_data* sd)
  182. {
  183. nullpo_retv(sd);
  184. if( sd->invincible_timer != INVALID_TIMER )
  185. {
  186. delete_timer(sd->invincible_timer,pc_invincible_timer);
  187. sd->invincible_timer = INVALID_TIMER;
  188. skill_unit_move(&sd->bl,gettick(),1);
  189. }
  190. }
  191. static TIMER_FUNC(pc_spiritball_timer){
  192. struct map_session_data *sd;
  193. int i;
  194. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  195. return 1;
  196. if( sd->spiritball <= 0 )
  197. {
  198. 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);
  199. sd->spiritball = 0;
  200. return 0;
  201. }
  202. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  203. if( i == sd->spiritball )
  204. {
  205. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  206. return 0;
  207. }
  208. sd->spiritball--;
  209. if( i != sd->spiritball )
  210. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  211. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  212. clif_spiritball(&sd->bl);
  213. return 0;
  214. }
  215. /**
  216. * Adds a spiritball to player for 'interval' ms
  217. * @param sd
  218. * @param interval
  219. * @param max
  220. */
  221. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  222. {
  223. int tid;
  224. uint8 i;
  225. nullpo_retv(sd);
  226. if(max > MAX_SPIRITBALL)
  227. max = MAX_SPIRITBALL;
  228. if(sd->spiritball < 0)
  229. sd->spiritball = 0;
  230. if( sd->spiritball && sd->spiritball >= max )
  231. {
  232. if(sd->spirit_timer[0] != INVALID_TIMER)
  233. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  234. sd->spiritball--;
  235. if( sd->spiritball != 0 )
  236. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  237. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  238. }
  239. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  240. 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);
  241. if( i != sd->spiritball )
  242. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  243. sd->spirit_timer[i] = tid;
  244. sd->spiritball++;
  245. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  246. clif_millenniumshield(&sd->bl,sd->spiritball);
  247. else
  248. clif_spiritball(&sd->bl);
  249. }
  250. /**
  251. * Removes number of spiritball from player
  252. * @param sd
  253. * @param count
  254. * @param type 1 = doesn't give client effect
  255. */
  256. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  257. {
  258. uint8 i;
  259. nullpo_retv(sd);
  260. if(sd->spiritball <= 0) {
  261. sd->spiritball = 0;
  262. return;
  263. }
  264. if(count == 0)
  265. return;
  266. if(count > sd->spiritball)
  267. count = sd->spiritball;
  268. sd->spiritball -= count;
  269. if(count > MAX_SPIRITBALL)
  270. count = MAX_SPIRITBALL;
  271. for(i=0;i<count;i++) {
  272. if(sd->spirit_timer[i] != INVALID_TIMER) {
  273. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  274. sd->spirit_timer[i] = INVALID_TIMER;
  275. }
  276. }
  277. for(i=count;i<MAX_SPIRITBALL;i++) {
  278. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  279. sd->spirit_timer[i] = INVALID_TIMER;
  280. }
  281. if(!type) {
  282. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  283. clif_millenniumshield(&sd->bl,sd->spiritball);
  284. else
  285. clif_spiritball(&sd->bl);
  286. }
  287. }
  288. /**
  289. * Increases a player's fame points and displays a notice to him
  290. * @param sd Player
  291. * @param count Fame point
  292. */
  293. void pc_addfame(struct map_session_data *sd,int count)
  294. {
  295. enum e_rank ranktype;
  296. nullpo_retv(sd);
  297. sd->status.fame += count;
  298. if(sd->status.fame > MAX_FAME)
  299. sd->status.fame = MAX_FAME;
  300. switch(sd->class_&MAPID_UPPERMASK){
  301. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  302. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  303. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  304. default:
  305. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  306. return;
  307. }
  308. clif_update_rankingpoint(sd,ranktype,count);
  309. chrif_updatefamelist(sd);
  310. }
  311. /**
  312. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  313. * @param sd
  314. * @param job Job use enum e_mapid
  315. * @return Rank
  316. */
  317. unsigned char pc_famerank(uint32 char_id, int job)
  318. {
  319. uint8 i;
  320. switch(job){
  321. case MAPID_BLACKSMITH: // Blacksmith
  322. for(i = 0; i < MAX_FAME_LIST; i++){
  323. if(smith_fame_list[i].id == char_id)
  324. return i + 1;
  325. }
  326. break;
  327. case MAPID_ALCHEMIST: // Alchemist
  328. for(i = 0; i < MAX_FAME_LIST; i++){
  329. if(chemist_fame_list[i].id == char_id)
  330. return i + 1;
  331. }
  332. break;
  333. case MAPID_TAEKWON: // Taekwon
  334. for(i = 0; i < MAX_FAME_LIST; i++){
  335. if(taekwon_fame_list[i].id == char_id)
  336. return i + 1;
  337. }
  338. break;
  339. }
  340. return 0;
  341. }
  342. /**
  343. * Restart player's HP & SP value
  344. * @param sd
  345. * @param type Restart type: 1 - Normal Resurection
  346. */
  347. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  348. struct status_data *status, *b_status;
  349. nullpo_retv(sd);
  350. b_status = &sd->base_status;
  351. status = &sd->battle_status;
  352. if (type&1) { //Normal resurrection
  353. status->hp = 1; //Otherwise status_heal may fail if dead.
  354. status_heal(&sd->bl, b_status->hp, 0, 1);
  355. if( status->sp < b_status->sp )
  356. status_set_sp(&sd->bl, b_status->sp, 1);
  357. } else { //Just for saving on the char-server (with values as if respawned)
  358. sd->status.hp = b_status->hp;
  359. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  360. }
  361. }
  362. /*==========================================
  363. Rental System
  364. *------------------------------------------*/
  365. /**
  366. * Ends a rental and removes the item/effect
  367. * @param tid: Tick ID
  368. * @param tick: Timer
  369. * @param id: Timer ID
  370. * @param data: Data
  371. * @return false - failure, true - success
  372. */
  373. TIMER_FUNC(pc_inventory_rental_end){
  374. struct map_session_data *sd = map_id2sd(id);
  375. if( sd == NULL )
  376. return 0;
  377. if( tid != sd->rental_timer ) {
  378. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  379. return 0;
  380. }
  381. pc_inventory_rentals(sd);
  382. return 1;
  383. }
  384. /**
  385. * Removes the rental timer from the player
  386. * @param sd: Player data
  387. */
  388. void pc_inventory_rental_clear(struct map_session_data *sd)
  389. {
  390. if( sd->rental_timer != INVALID_TIMER ) {
  391. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  392. sd->rental_timer = INVALID_TIMER;
  393. }
  394. }
  395. /**
  396. * Check for items in the player's inventory that are rental type
  397. * @param sd: Player data
  398. */
  399. void pc_inventory_rentals(struct map_session_data *sd)
  400. {
  401. int i, c = 0;
  402. unsigned int next_tick = UINT_MAX;
  403. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  404. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  405. continue; // Nothing here
  406. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  407. continue;
  408. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  409. if (sd->inventory_data[i]->unequip_script)
  410. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  411. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  412. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  413. } else {
  414. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  415. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  416. next_tick = umin(expire_tick * 1000U, next_tick);
  417. c++;
  418. }
  419. }
  420. 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
  421. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  422. else
  423. sd->rental_timer = INVALID_TIMER;
  424. }
  425. /**
  426. * Add a rental item to the player and adjusts the rental timer appropriately
  427. * @param sd: Player data
  428. * @param seconds: Rental time
  429. */
  430. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  431. {
  432. unsigned int tick = seconds * 1000;
  433. if( sd == NULL )
  434. return;
  435. if( sd->rental_timer != INVALID_TIMER ) {
  436. const struct TimerData * td;
  437. td = get_timer(sd->rental_timer);
  438. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  439. pc_inventory_rental_clear(sd);
  440. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  441. }
  442. } else
  443. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  444. }
  445. /**
  446. * Check if the player can sell the current item
  447. * @param sd: map_session_data of the player
  448. * @param item: struct of the checking item
  449. * @param shoptype: NPC's sub type see enum npc_subtype
  450. * @return bool 'true' is sellable, 'false' otherwise
  451. */
  452. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  453. if (sd == NULL || item == NULL)
  454. return false;
  455. if (item->equip > 0 || item->amount < 0)
  456. return false;
  457. if (battle_config.hide_fav_sell && item->favorite)
  458. return false; //Cannot sell favs (optional config)
  459. if (!battle_config.rental_transaction && item->expire_time)
  460. return false; // Cannot Sell Rental Items
  461. switch (shoptype) {
  462. case NPCTYPE_SHOP:
  463. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  464. item->bound != BOUND_GUILD ||
  465. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  466. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  467. ))
  468. return true;
  469. break;
  470. case NPCTYPE_ITEMSHOP:
  471. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  472. item->bound != BOUND_GUILD ||
  473. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  474. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  475. ))
  476. return true;
  477. else if (!item->bound) {
  478. struct item_data *itd = itemdb_search(item->nameid);
  479. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  480. return true;
  481. }
  482. break;
  483. }
  484. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  485. return false;
  486. if (item->bound && !pc_can_give_bounded_items(sd))
  487. return false; // Don't allow sale of bound items
  488. return true;
  489. }
  490. /**
  491. * Determines if player can give / drop / trade / vend items
  492. */
  493. bool pc_can_give_items(struct map_session_data *sd)
  494. {
  495. return pc_has_permission(sd, PC_PERM_TRADE);
  496. }
  497. /**
  498. * Determines if player can give / drop / trade / vend bounded items
  499. */
  500. bool pc_can_give_bounded_items(struct map_session_data *sd)
  501. {
  502. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  503. }
  504. /*==========================================
  505. * Prepares character for saving.
  506. * @param sd
  507. *------------------------------------------*/
  508. void pc_makesavestatus(struct map_session_data *sd) {
  509. nullpo_retv(sd);
  510. if(!battle_config.save_clothcolor)
  511. sd->status.clothes_color = 0;
  512. // Since this is currently not officially released,
  513. // its best to have a forced option to not save body styles.
  514. if(!battle_config.save_body_style)
  515. sd->status.body = 0;
  516. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  517. //status change load/saving. [Skotlex]
  518. #ifdef NEW_CARTS
  519. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  520. #else
  521. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  522. #endif
  523. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  524. if(pc_isdead(sd)){
  525. pc_setrestartvalue(sd, 0);
  526. } else {
  527. sd->status.hp = sd->battle_status.hp;
  528. sd->status.sp = sd->battle_status.sp;
  529. }
  530. sd->status.last_point.map = sd->mapindex;
  531. sd->status.last_point.x = sd->bl.x;
  532. sd->status.last_point.y = sd->bl.y;
  533. return;
  534. }
  535. if(pc_isdead(sd)) {
  536. pc_setrestartvalue(sd, 0);
  537. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  538. } else {
  539. sd->status.hp = sd->battle_status.hp;
  540. sd->status.sp = sd->battle_status.sp;
  541. sd->status.last_point.map = sd->mapindex;
  542. sd->status.last_point.x = sd->bl.x;
  543. sd->status.last_point.y = sd->bl.y;
  544. }
  545. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  546. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  547. if(mapdata->save.map)
  548. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  549. else
  550. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  551. }
  552. }
  553. /*==========================================
  554. * Off init ? Connection?
  555. *------------------------------------------*/
  556. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
  557. nullpo_retv(sd);
  558. sd->bl.id = account_id;
  559. sd->status.account_id = account_id;
  560. sd->status.char_id = char_id;
  561. sd->status.sex = sex;
  562. sd->login_id1 = login_id1;
  563. sd->login_id2 = 0; // at this point, we can not know the value :(
  564. sd->client_tick = client_tick;
  565. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  566. sd->bl.type = BL_PC;
  567. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  568. sd->canlog_tick = gettick();
  569. //Required to prevent homunculus copuing a base speed of 0.
  570. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  571. }
  572. /**
  573. * Get equip point for an equip
  574. * @param sd
  575. * @param id
  576. */
  577. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  578. int ep = 0;
  579. nullpo_ret(sd);
  580. nullpo_ret(id);
  581. if (!itemdb_isequip2(id))
  582. return 0; //Not equippable by players.
  583. ep = id->equip;
  584. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  585. 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]
  586. if (ep == EQP_WEAPON)
  587. return EQP_ARMS;
  588. if (ep == EQP_SHADOW_WEAPON)
  589. return EQP_SHADOW_ARMS;
  590. }
  591. }
  592. return ep;
  593. }
  594. /**
  595. * Get equip point for an equip
  596. * @param sd
  597. * @param n Equip index in inventory
  598. */
  599. int pc_equippoint(struct map_session_data *sd,int n){
  600. nullpo_ret(sd);
  601. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  602. }
  603. /**
  604. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  605. * @param sd : player session
  606. * @return 0 sucess, 1:invalid sd
  607. */
  608. void pc_setinventorydata(struct map_session_data *sd)
  609. {
  610. uint8 i;
  611. nullpo_retv(sd);
  612. for(i = 0; i < MAX_INVENTORY; i++) {
  613. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  614. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  615. }
  616. }
  617. /**
  618. * 'Calculates' weapon type
  619. * @param sd : Player
  620. */
  621. void pc_calcweapontype(struct map_session_data *sd)
  622. {
  623. nullpo_retv(sd);
  624. // single-hand
  625. if(sd->weapontype2 == W_FIST) {
  626. sd->status.weapon = sd->weapontype1;
  627. return;
  628. }
  629. if(sd->weapontype1 == W_FIST) {
  630. sd->status.weapon = sd->weapontype2;
  631. return;
  632. }
  633. // dual-wield
  634. sd->status.weapon = 0;
  635. switch (sd->weapontype1){
  636. case W_DAGGER:
  637. switch (sd->weapontype2) {
  638. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  639. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  640. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  641. }
  642. break;
  643. case W_1HSWORD:
  644. switch (sd->weapontype2) {
  645. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  646. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  647. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  648. }
  649. break;
  650. case W_1HAXE:
  651. switch (sd->weapontype2) {
  652. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  653. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  654. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  655. }
  656. }
  657. // unknown, default to right hand type
  658. if (!sd->status.weapon)
  659. sd->status.weapon = sd->weapontype1;
  660. }
  661. /**
  662. * Set equip index
  663. * @param sd : Player
  664. */
  665. void pc_setequipindex(struct map_session_data *sd)
  666. {
  667. uint16 i;
  668. nullpo_retv(sd);
  669. for (i = 0; i < EQI_MAX; i++)
  670. sd->equip_index[i] = -1;
  671. for (i = 0; i < MAX_INVENTORY; i++) {
  672. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  673. continue;
  674. if (sd->inventory.u.items_inventory[i].equip) {
  675. uint8 j;
  676. for (j = 0; j < EQI_MAX; j++)
  677. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  678. sd->equip_index[j] = i;
  679. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  680. if (sd->inventory_data[i])
  681. sd->weapontype1 = sd->inventory_data[i]->look;
  682. else
  683. sd->weapontype1 = 0;
  684. }
  685. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  686. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  687. sd->weapontype2 = sd->inventory_data[i]->look;
  688. else
  689. sd->weapontype2 = 0;
  690. }
  691. }
  692. }
  693. pc_calcweapontype(sd);
  694. }
  695. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  696. //{
  697. // int i;
  698. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  699. // struct item_data *data;
  700. //
  701. // //Crafted/made/hatched items.
  702. // if (itemdb_isspecial(item->card[0]))
  703. // return 1;
  704. //
  705. // /* scan for enchant armor gems */
  706. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  707. // s = MAX_SLOTS - 1;
  708. //
  709. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  710. // return( i < s ) ? 0 : 1;
  711. //}
  712. /**
  713. * Check if an item is equiped by player
  714. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  715. * @param sd : player session
  716. * @param nameid : itemid
  717. * @return 1:yes, 0:no
  718. */
  719. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  720. {
  721. uint8 i;
  722. for( i = 0; i < EQI_MAX; i++ )
  723. {
  724. short index = sd->equip_index[i], j;
  725. if( index < 0 )
  726. continue;
  727. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  728. continue;
  729. if( !sd->inventory_data[index] )
  730. continue;
  731. if( sd->inventory_data[index]->nameid == nameid )
  732. return true;
  733. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  734. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  735. return true;
  736. }
  737. }
  738. return false;
  739. }
  740. /**
  741. * Check adoption rules
  742. * @param p1_sd: Player 1
  743. * @param p2_sd: Player 2
  744. * @param b_sd: Player that will be adopted
  745. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  746. * ADOPT_ALREADY_ADOPTED - Already adopted
  747. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  748. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  749. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  750. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  751. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  752. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  753. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  754. */
  755. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  756. {
  757. if( !p1_sd || !p2_sd || !b_sd )
  758. return ADOPT_CHARACTER_NOT_FOUND;
  759. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  760. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  761. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  762. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  763. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  764. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  765. if( p2_sd->status.party_id != p1_sd->status.party_id )
  766. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  767. // Parents need to have their ring equipped
  768. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  769. return ADOPT_EQUIP_RINGS;
  770. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  771. return ADOPT_EQUIP_RINGS;
  772. // Already adopted a baby
  773. if( p1_sd->status.child || p2_sd->status.child ) {
  774. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  775. return ADOPT_MORE_CHILDREN;
  776. }
  777. // Parents need at least lvl 70 to adopt
  778. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  779. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  780. return ADOPT_LEVEL_70;
  781. }
  782. if( b_sd->status.partner_id ) {
  783. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  784. return ADOPT_MARRIED;
  785. }
  786. 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 ) )
  787. return ADOPT_NOT_NOVICE;
  788. return ADOPT_ALLOWED;
  789. }
  790. /*==========================================
  791. * Adoption Process
  792. *------------------------------------------*/
  793. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  794. {
  795. int job, joblevel;
  796. unsigned int jobexp;
  797. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  798. return false;
  799. // Preserve current job levels and progress
  800. joblevel = b_sd->status.job_level;
  801. jobexp = b_sd->status.job_exp;
  802. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  803. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  804. { // Success, proceed to configure parents and baby skills
  805. p1_sd->status.child = b_sd->status.char_id;
  806. p2_sd->status.child = b_sd->status.char_id;
  807. b_sd->status.father = p1_sd->status.char_id;
  808. b_sd->status.mother = p2_sd->status.char_id;
  809. // Restore progress
  810. b_sd->status.job_level = joblevel;
  811. clif_updatestatus(b_sd, SP_JOBLEVEL);
  812. b_sd->status.job_exp = jobexp;
  813. clif_updatestatus(b_sd, SP_JOBEXP);
  814. // Baby Skills
  815. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  816. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  817. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  818. // Parents Skills
  819. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  820. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  821. chrif_save(p1_sd, CSAVE_NORMAL);
  822. chrif_save(p2_sd, CSAVE_NORMAL);
  823. chrif_save(b_sd, CSAVE_NORMAL);
  824. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  825. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  826. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  827. return true;
  828. }
  829. return false; // Job Change Fail
  830. }
  831. /*==========================================
  832. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  833. Returns:
  834. false : Cannot use/equip
  835. true : Can use/equip
  836. * Credits:
  837. [Inkfish] for first idea
  838. [Haru] for third-classes extension
  839. [Cydh] finishing :D
  840. *------------------------------------------*/
  841. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  842. while (1) {
  843. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  844. break;
  845. #ifndef RENEWAL
  846. //allow third classes to use trans. class items
  847. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  848. break;
  849. //third-baby classes can use same item too
  850. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  851. break;
  852. //don't need to decide specific rules for third-classes?
  853. //items for third classes can be used for all third classes
  854. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  855. break;
  856. #else
  857. //trans. classes (exl. third-trans.)
  858. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  859. break;
  860. //baby classes (exl. third-baby)
  861. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  862. break;
  863. //third classes (exl. third-trans. and baby-third)
  864. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  865. break;
  866. //trans-third classes
  867. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  868. break;
  869. //third-baby classes
  870. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  871. break;
  872. #endif
  873. return false;
  874. }
  875. return true;
  876. }
  877. /*=================================================
  878. * Checks if the player can equip the item at index n in inventory.
  879. * @param sd
  880. * @param n Item index in inventory
  881. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  882. *------------------------------------------------*/
  883. uint8 pc_isequip(struct map_session_data *sd,int n)
  884. {
  885. struct item_data *item;
  886. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  887. item = sd->inventory_data[n];
  888. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  889. return ITEM_EQUIP_ACK_OK;
  890. if(item == NULL)
  891. return ITEM_EQUIP_ACK_FAIL;
  892. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  893. return ITEM_EQUIP_ACK_FAILLEVEL;
  894. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  895. return ITEM_EQUIP_ACK_FAILLEVEL;
  896. if(item->sex != 2 && sd->status.sex != item->sex)
  897. return ITEM_EQUIP_ACK_FAIL;
  898. //fail to equip if item is restricted
  899. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  900. return ITEM_EQUIP_ACK_FAIL;
  901. if (item->equip&EQP_AMMO) {
  902. switch (item->look) {
  903. case AMMO_ARROW:
  904. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  905. clif_msg(sd, ITEM_NEED_BOW);
  906. return ITEM_EQUIP_ACK_FAIL;
  907. }
  908. break;
  909. case AMMO_THROWABLE_DAGGER:
  910. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  911. return ITEM_EQUIP_ACK_FAIL;
  912. break;
  913. case AMMO_BULLET:
  914. case AMMO_SHELL:
  915. 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
  916. #ifdef RENEWAL
  917. && sd->status.weapon != W_GRENADE
  918. #endif
  919. ) {
  920. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  921. return ITEM_EQUIP_ACK_FAIL;
  922. }
  923. break;
  924. #ifndef RENEWAL
  925. case AMMO_GRENADE:
  926. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  927. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  928. return ITEM_EQUIP_ACK_FAIL;
  929. }
  930. break;
  931. #endif
  932. case AMMO_CANNONBALL:
  933. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  934. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  935. return ITEM_EQUIP_ACK_FAIL;
  936. }
  937. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  938. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  939. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  940. return ITEM_EQUIP_ACK_FAIL;
  941. }
  942. break;
  943. }
  944. }
  945. if (sd->sc.count) {
  946. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  947. return ITEM_EQUIP_ACK_FAIL;
  948. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  949. return ITEM_EQUIP_ACK_FAIL;
  950. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  951. return ITEM_EQUIP_ACK_FAIL;
  952. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  953. return ITEM_EQUIP_ACK_FAIL;
  954. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  955. return ITEM_EQUIP_ACK_FAIL;
  956. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  957. return ITEM_EQUIP_ACK_FAIL;
  958. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  959. return ITEM_EQUIP_ACK_FAIL;
  960. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  961. //Spirit of Super Novice equip bonuses. [Skotlex]
  962. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  963. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  964. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  965. switch(item->look) { //In weapons, the look determines type of weapon.
  966. case W_DAGGER: //All level 4 - Daggers
  967. case W_1HSWORD: //All level 4 - 1H Swords
  968. case W_1HAXE: //All level 4 - 1H Axes
  969. case W_MACE: //All level 4 - 1H Maces
  970. case W_STAFF: //All level 4 - 1H Staves
  971. case W_2HSTAFF: //All level 4 - 2H Staves
  972. return ITEM_EQUIP_ACK_OK;
  973. }
  974. }
  975. }
  976. //Not equipable by class. [Skotlex]
  977. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  978. return ITEM_EQUIP_ACK_FAIL;
  979. if (!pc_isItemClass(sd, item))
  980. return ITEM_EQUIP_ACK_FAIL;
  981. return ITEM_EQUIP_ACK_OK;
  982. }
  983. /*==========================================
  984. * No problem with the session id
  985. * set the status that has been sent from char server
  986. *------------------------------------------*/
  987. 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)
  988. {
  989. int i;
  990. unsigned long tick = gettick();
  991. uint32 ip = session[sd->fd]->client_addr;
  992. sd->login_id2 = login_id2;
  993. sd->group_id = group_id;
  994. /* load user permissions */
  995. pc_group_pc_load(sd);
  996. memcpy(&sd->status, st, sizeof(*st));
  997. if (st->sex != sd->status.sex) {
  998. clif_authfail_fd(sd->fd, 0);
  999. return false;
  1000. }
  1001. //Set the map-server used job id. [Skotlex]
  1002. i = pc_jobid2mapid(sd->status.class_);
  1003. if (i == -1) { //Invalid class?
  1004. 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);
  1005. sd->status.class_ = JOB_NOVICE;
  1006. sd->class_ = MAPID_NOVICE;
  1007. } else
  1008. sd->class_ = i;
  1009. // Checks and fixes to character status data, that are required
  1010. // in case of configuration change or stuff, which cannot be
  1011. // checked on char-server.
  1012. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1013. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1014. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1015. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1016. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1017. sd->state.connect_new = 1;
  1018. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1019. sd->invincible_timer = INVALID_TIMER;
  1020. sd->npc_timer_id = INVALID_TIMER;
  1021. sd->pvp_timer = INVALID_TIMER;
  1022. sd->expiration_tid = INVALID_TIMER;
  1023. sd->autotrade_tid = INVALID_TIMER;
  1024. #ifdef SECURE_NPCTIMEOUT
  1025. // Initialize to defaults/expected
  1026. sd->npc_idle_timer = INVALID_TIMER;
  1027. sd->npc_idle_tick = tick;
  1028. sd->npc_idle_type = NPCT_INPUT;
  1029. sd->state.ignoretimeout = false;
  1030. #endif
  1031. sd->canuseitem_tick = tick;
  1032. sd->canusecashfood_tick = tick;
  1033. sd->canequip_tick = tick;
  1034. sd->cantalk_tick = tick;
  1035. sd->canskill_tick = tick;
  1036. sd->cansendmail_tick = tick;
  1037. sd->idletime = last_tick;
  1038. for(i = 0; i < MAX_SPIRITBALL; i++)
  1039. sd->spirit_timer[i] = INVALID_TIMER;
  1040. if (battle_config.item_auto_get)
  1041. sd->state.autoloot = 10000;
  1042. if (battle_config.disp_experience)
  1043. sd->state.showexp = 1;
  1044. if (battle_config.disp_zeny)
  1045. sd->state.showzeny = 1;
  1046. #ifdef VIP_ENABLE
  1047. if (!battle_config.vip_disp_rate)
  1048. sd->vip.disableshowrate = 1;
  1049. #endif
  1050. if (!(battle_config.display_skill_fail&2))
  1051. sd->state.showdelay = 1;
  1052. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1053. memset(&sd->cart, 0, sizeof(struct s_storage));
  1054. memset(&sd->storage, 0, sizeof(struct s_storage));
  1055. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1056. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1057. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1058. sd->status.option &= ~OPTION_INVISIBLE;
  1059. }
  1060. status_change_init(&sd->bl);
  1061. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1062. unit_dataset(&sd->bl);
  1063. sd->guild_x = -1;
  1064. sd->guild_y = -1;
  1065. sd->delayed_damage = 0;
  1066. // Event Timers
  1067. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1068. sd->eventtimer[i] = INVALID_TIMER;
  1069. // Rental Timer
  1070. sd->rental_timer = INVALID_TIMER;
  1071. for( i = 0; i < 3; i++ )
  1072. sd->hate_mob[i] = -1;
  1073. sd->quest_log = NULL;
  1074. sd->num_quests = 0;
  1075. sd->avail_quests = 0;
  1076. sd->save_quest = false;
  1077. sd->count_rewarp = 0;
  1078. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1079. sd->regs.arrays = NULL;
  1080. sd->vars_dirty = false;
  1081. sd->vars_ok = false;
  1082. sd->vars_received = 0x0;
  1083. sd->qi_display = NULL;
  1084. sd->qi_count = 0;
  1085. //warp player
  1086. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1087. 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);
  1088. // try warping to a default map instead (church graveyard)
  1089. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1090. // if we fail again
  1091. clif_authfail_fd(sd->fd, 0);
  1092. return false;
  1093. }
  1094. }
  1095. clif_authok(sd);
  1096. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1097. sd->die_counter=-1;
  1098. //display login notice
  1099. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1100. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1101. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1102. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1103. sd->status.name, sd->status.account_id, sd->status.char_id,
  1104. CONVIP(ip), sd->group_id);
  1105. // Send friends list
  1106. clif_friendslist_send(sd);
  1107. if( !changing_mapservers ) {
  1108. if (battle_config.display_version == 1)
  1109. pc_show_version(sd);
  1110. // Message of the Day [Valaris]
  1111. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1112. if (battle_config.motd_type)
  1113. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1114. else
  1115. clif_displaymessage(sd->fd, motd_text[i]);
  1116. }
  1117. if (expiration_time != 0)
  1118. sd->expiration_time = expiration_time;
  1119. /**
  1120. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1121. **/
  1122. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1123. }
  1124. pc_validate_skill(sd);
  1125. /* [Ind] */
  1126. sd->sc_display = NULL;
  1127. sd->sc_display_count = 0;
  1128. // Player has not yet received the CashShop list
  1129. sd->status.cashshop_sent = false;
  1130. sd->last_addeditem_index = -1;
  1131. sd->bonus_script.head = NULL;
  1132. sd->bonus_script.count = 0;
  1133. #if PACKETVER >= 20150513
  1134. sd->hatEffectIDs = NULL;
  1135. sd->hatEffectCount = 0;
  1136. #endif
  1137. sd->catch_target_class = PET_CATCH_FAIL;
  1138. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1139. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1140. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1141. clif_updatestatus(sd, SP_BASEEXP);
  1142. }
  1143. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1144. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1145. clif_updatestatus(sd, SP_JOBEXP);
  1146. }
  1147. // Request all registries (auth is considered completed whence they arrive)
  1148. intif_request_registry(sd,7);
  1149. return true;
  1150. }
  1151. /*==========================================
  1152. * Closes a connection because it failed to be authenticated from the char server.
  1153. *------------------------------------------*/
  1154. void pc_authfail(struct map_session_data *sd)
  1155. {
  1156. clif_authfail_fd(sd->fd, 0);
  1157. return;
  1158. }
  1159. /**
  1160. * Player register a bl as hatred
  1161. * @param sd : player session
  1162. * @param pos : hate position [0;2]
  1163. * @param bl : target bl
  1164. * @return false:failed, true:success
  1165. */
  1166. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1167. {
  1168. int class_;
  1169. if (!sd || !bl || pos < 0 || pos > 2)
  1170. return false;
  1171. if (sd->hate_mob[pos] != -1)
  1172. { //Can't change hate targets.
  1173. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1174. return false;
  1175. }
  1176. class_ = status_get_class(bl);
  1177. if (!pcdb_checkid(class_)) {
  1178. unsigned int max_hp = status_get_max_hp(bl);
  1179. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1180. return false;
  1181. if (pos != status_get_size(bl))
  1182. return false; //Wrong size
  1183. }
  1184. sd->hate_mob[pos] = class_;
  1185. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1186. clif_hate_info(sd, pos, class_, 1);
  1187. return true;
  1188. }
  1189. /*==========================================
  1190. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1191. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1192. * See intif_parse_StorageReceived() for item operations [lighta]
  1193. *------------------------------------------*/
  1194. void pc_reg_received(struct map_session_data *sd)
  1195. {
  1196. uint8 i;
  1197. sd->vars_ok = true;
  1198. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1199. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1200. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1201. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1202. if (msg_checklangtype(sd->langtype,true) < 0)
  1203. sd->langtype = 0; //invalid langtype reset to default
  1204. // Cash shop
  1205. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1206. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1207. // Cooking Exp
  1208. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1209. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1210. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1211. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1212. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1213. }
  1214. if (battle_config.feature_banking)
  1215. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1216. if (battle_config.feature_roulette) {
  1217. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1218. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1219. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1220. }
  1221. sd->roulette.prizeIdx = -1;
  1222. //SG map and mob read [Komurka]
  1223. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1224. uint16 j;
  1225. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1226. sd->feel_map[i].index = j;
  1227. sd->feel_map[i].m = map_mapindex2mapid(j);
  1228. } else {
  1229. sd->feel_map[i].index = 0;
  1230. sd->feel_map[i].m = -1;
  1231. }
  1232. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1233. }
  1234. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1235. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1236. sd->cloneskill_idx = skill_get_index(skid);
  1237. if (sd->cloneskill_idx > 0) {
  1238. sd->status.skill[sd->cloneskill_idx].id = skid;
  1239. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1240. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1241. sd->status.skill[sd->cloneskill_idx].lv = i;
  1242. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1243. }
  1244. }
  1245. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1246. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1247. sd->reproduceskill_idx = skill_get_index(skid);
  1248. if (sd->reproduceskill_idx > 0) {
  1249. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1250. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1251. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1252. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1253. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1254. }
  1255. }
  1256. //Weird... maybe registries were reloaded?
  1257. if (sd->state.active)
  1258. return;
  1259. sd->state.active = 1;
  1260. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1261. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1262. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1263. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1264. if (sd->status.party_id)
  1265. party_member_joined(sd);
  1266. if (sd->status.guild_id)
  1267. guild_member_joined(sd);
  1268. if( sd->status.clan_id )
  1269. clan_member_joined(sd);
  1270. // pet
  1271. if (sd->status.pet_id > 0)
  1272. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1273. // Homunculus [albator]
  1274. if( sd->status.hom_id > 0 )
  1275. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1276. if( sd->status.mer_id > 0 )
  1277. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1278. if( sd->status.ele_id > 0 )
  1279. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1280. map_addiddb(&sd->bl);
  1281. map_delnickdb(sd->status.char_id, sd->status.name);
  1282. if (!chrif_auth_finished(sd))
  1283. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1284. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1285. chrif_bsdata_request(sd->status.char_id);
  1286. #ifdef VIP_ENABLE
  1287. sd->vip.time = 0;
  1288. sd->vip.enabled = 0;
  1289. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1290. #endif
  1291. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1292. intif_request_questlog(sd);
  1293. if (battle_config.feature_achievement) {
  1294. sd->achievement_data.total_score = 0;
  1295. sd->achievement_data.level = 0;
  1296. sd->achievement_data.save = false;
  1297. sd->achievement_data.count = 0;
  1298. sd->achievement_data.incompleteCount = 0;
  1299. sd->achievement_data.achievements = NULL;
  1300. intif_request_achievements(sd->status.char_id);
  1301. }
  1302. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1303. sd->state.connect_new = 1;
  1304. clif_parse_LoadEndAck(sd->fd, sd);
  1305. }
  1306. if( pc_isinvisible(sd) ) {
  1307. sd->vd.class_ = JT_INVISIBLE;
  1308. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1309. // decrement the number of pvp players on the map
  1310. map_getmapdata(sd->bl.m)->users_pvp--;
  1311. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1312. // unregister the player for ranking
  1313. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1314. sd->pvp_timer = INVALID_TIMER;
  1315. }
  1316. clif_changeoption( &sd->bl );
  1317. }
  1318. channel_autojoin(sd);
  1319. }
  1320. static int pc_calc_skillpoint(struct map_session_data* sd)
  1321. {
  1322. uint16 i, skill_point = 0;
  1323. nullpo_ret(sd);
  1324. for(i = 1; i < MAX_SKILL; i++) {
  1325. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1326. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1327. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1328. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1329. )
  1330. {
  1331. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1332. skill_point += sd->status.skill[i].lv;
  1333. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1334. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1335. }
  1336. }
  1337. }
  1338. return skill_point;
  1339. }
  1340. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1341. uint16 i = 0;
  1342. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1343. return false;
  1344. /**
  1345. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1346. * skill tree and since they have no icons they'll give resource errors
  1347. * Get ALL skills except npc/guild ones. [Skotlex]
  1348. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1349. **/
  1350. for( i = 0; i < MAX_SKILL; i++ ) {
  1351. uint16 skill_id = skill_idx2id(i);
  1352. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1353. continue;
  1354. switch (skill_id) {
  1355. case SM_SELFPROVOKE:
  1356. case AB_DUPLELIGHT_MELEE:
  1357. case AB_DUPLELIGHT_MAGIC:
  1358. case WL_CHAINLIGHTNING_ATK:
  1359. case WL_TETRAVORTEX_FIRE:
  1360. case WL_TETRAVORTEX_WATER:
  1361. case WL_TETRAVORTEX_WIND:
  1362. case WL_TETRAVORTEX_GROUND:
  1363. case WL_SUMMON_ATK_FIRE:
  1364. case WL_SUMMON_ATK_WIND:
  1365. case WL_SUMMON_ATK_WATER:
  1366. case WL_SUMMON_ATK_GROUND:
  1367. case LG_OVERBRAND_BRANDISH:
  1368. case LG_OVERBRAND_PLUSATK:
  1369. case WM_SEVERE_RAINSTORM_MELEE:
  1370. case RL_R_TRIP_PLUSATK:
  1371. case SG_DEVIL:
  1372. case MO_TRIPLEATTACK:
  1373. case RG_SNATCHER:
  1374. continue;
  1375. default:
  1376. {
  1377. uint8 lv = (uint8)skill_get_max(skill_id);
  1378. if (lv > 0) {
  1379. sd->status.skill[i].id = skill_id;
  1380. if (addlv)
  1381. sd->status.skill[i].lv = lv;
  1382. }
  1383. }
  1384. break;
  1385. }
  1386. }
  1387. return true;
  1388. }
  1389. /*==========================================
  1390. * Calculation of skill level.
  1391. * @param sd
  1392. *------------------------------------------*/
  1393. void pc_calc_skilltree(struct map_session_data *sd)
  1394. {
  1395. int i, flag;
  1396. int c = 0;
  1397. nullpo_retv(sd);
  1398. i = pc_calc_skilltree_normalize_job(sd);
  1399. c = pc_mapid2jobid(i, sd->status.sex);
  1400. if( c == -1 )
  1401. { //Unable to normalize job??
  1402. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1403. return;
  1404. }
  1405. c = pc_class2idx(c);
  1406. for( i = 0; i < MAX_SKILL; i++ ) {
  1407. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1408. sd->status.skill[i].id = 0; //First clear skills.
  1409. /* permanent skills that must be re-checked */
  1410. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1411. uint16 sk_id = skill_idx2id(i);
  1412. if (!sk_id) {
  1413. sd->status.skill[i].id = 0;
  1414. sd->status.skill[i].lv = 0;
  1415. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1416. continue;
  1417. }
  1418. switch (sk_id) {
  1419. case NV_TRICKDEAD:
  1420. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1421. sd->status.skill[i].id = 0;
  1422. sd->status.skill[i].lv = 0;
  1423. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1424. }
  1425. break;
  1426. }
  1427. }
  1428. }
  1429. for( i = 0; i < MAX_SKILL; i++ ) {
  1430. uint16 skill_id = 0;
  1431. // Restore original level of skills after deleting earned skills.
  1432. 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 ) {
  1433. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1434. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1435. }
  1436. //Enable Bard/Dancer spirit linked skills.
  1437. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1438. continue;
  1439. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1440. //Link Dancer skills to bard.
  1441. if( sd->status.sex ) {
  1442. if( sd->status.skill[i-8].lv < 10 )
  1443. continue;
  1444. sd->status.skill[i].id = skill_id;
  1445. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1446. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1447. }
  1448. //Link Bard skills to dancer.
  1449. else {
  1450. if( sd->status.skill[i].lv < 10 )
  1451. continue;
  1452. sd->status.skill[i-8].id = skill_id - 8;
  1453. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1454. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1455. }
  1456. }
  1457. }
  1458. // Removes Taekwon Ranker skill bonus
  1459. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1460. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1461. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1462. uint16 sk_id = skill_tree[c_][i].skill_id;
  1463. uint16 sk_idx = 0;
  1464. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1465. continue;
  1466. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1467. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1468. continue;
  1469. sd->status.skill[sk_idx].id = 0;
  1470. }
  1471. }
  1472. }
  1473. // Grant all skills
  1474. pc_grant_allskills(sd, false);
  1475. do {
  1476. uint16 skid = 0;
  1477. flag = 0;
  1478. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1479. bool fail = false;
  1480. uint16 sk_idx = skill_get_index(skid);
  1481. if (sd->status.skill[sk_idx].id)
  1482. continue; //Skill already known.
  1483. if (!battle_config.skillfree) {
  1484. uint8 j;
  1485. // Checking required skills
  1486. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1487. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1488. uint16 sk_need_idx = 0;
  1489. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1490. short sk_need = sk_need_id;
  1491. 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)
  1492. sk_need = 0; //Not learned.
  1493. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1494. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1495. else
  1496. sk_need = pc_checkskill(sd,sk_need_id);
  1497. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1498. fail = true;
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1504. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1505. class_ = pc_class2idx(class_);
  1506. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1507. fail = true; // base level requirement wasn't satisfied
  1508. }
  1509. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1510. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1511. class_ = pc_class2idx(class_);
  1512. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1513. fail = true; // job level requirement wasn't satisfied
  1514. }
  1515. }
  1516. if (!fail) {
  1517. int inf2 = skill_get_inf2(skid);
  1518. if (!sd->status.skill[sk_idx].lv && (
  1519. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1520. inf2&INF2_WEDDING_SKILL ||
  1521. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1522. ))
  1523. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1524. sd->status.skill[sk_idx].id = skid;
  1525. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1526. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1527. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1528. }
  1529. flag = 1; // skill list has changed, perform another pass
  1530. }
  1531. }
  1532. } while(flag);
  1533. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1534. unsigned short skid = 0;
  1535. /* Taekwon Ranker Bonus Skill Tree
  1536. ============================================
  1537. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1538. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1539. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1540. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1541. uint16 sk_idx = 0;
  1542. if (!(sk_idx = skill_get_index(skid)))
  1543. continue;
  1544. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1545. continue; //Do not include Quest/Wedding skills.
  1546. if( sd->status.skill[sk_idx].id == 0 ) {
  1547. sd->status.skill[sk_idx].id = skid;
  1548. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1549. } else if( skid != NV_BASIC )
  1550. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1551. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1552. }
  1553. }
  1554. }
  1555. //Checks if you can learn a new skill after having leveled up a skill.
  1556. static void pc_check_skilltree(struct map_session_data *sd)
  1557. {
  1558. int i, flag = 0;
  1559. int c = 0;
  1560. if (battle_config.skillfree)
  1561. return; //Function serves no purpose if this is set
  1562. i = pc_calc_skilltree_normalize_job(sd);
  1563. c = pc_mapid2jobid(i, sd->status.sex);
  1564. if (c == -1) { //Unable to normalize job??
  1565. 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);
  1566. return;
  1567. }
  1568. c = pc_class2idx(c);
  1569. do {
  1570. uint16 skid = 0;
  1571. flag = 0;
  1572. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1573. uint16 sk_idx = skill_get_index(skid);
  1574. bool fail = false;
  1575. uint8 j = 0;
  1576. if (sd->status.skill[sk_idx].id) //Already learned
  1577. continue;
  1578. // Checking required skills
  1579. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1580. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1581. uint16 sk_need_idx = 0;
  1582. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1583. short sk_need = sk_need_id;
  1584. 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)
  1585. sk_need = 0; //Not learned.
  1586. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1587. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1588. else
  1589. sk_need = pc_checkskill(sd,sk_need_id);
  1590. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1591. fail = true;
  1592. break;
  1593. }
  1594. }
  1595. }
  1596. if( fail )
  1597. continue;
  1598. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1599. continue;
  1600. j = skill_get_inf2(skid);
  1601. if( !sd->status.skill[sk_idx].lv && (
  1602. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1603. j&INF2_WEDDING_SKILL ||
  1604. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1605. ) )
  1606. continue; //Cannot be learned via normal means.
  1607. sd->status.skill[sk_idx].id = skid;
  1608. flag = 1;
  1609. }
  1610. } while(flag);
  1611. }
  1612. // Make sure all the skills are in the correct condition
  1613. // before persisting to the backend.. [MouseJstr]
  1614. void pc_clean_skilltree(struct map_session_data *sd)
  1615. {
  1616. uint16 i;
  1617. for (i = 0; i < MAX_SKILL; i++){
  1618. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1619. sd->status.skill[i].id = 0;
  1620. sd->status.skill[i].lv = 0;
  1621. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1622. }
  1623. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1624. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1625. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1626. }
  1627. }
  1628. }
  1629. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1630. {
  1631. int skill_point, novice_skills;
  1632. int c = sd->class_;
  1633. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1634. return c;
  1635. skill_point = pc_calc_skillpoint(sd);
  1636. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1637. // limit 1st class and above to novice job levels
  1638. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1639. {
  1640. c = MAPID_NOVICE;
  1641. }
  1642. // limit 2nd class and above to first class job levels (super novices are exempt)
  1643. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1644. {
  1645. // regenerate change_level_2nd
  1646. if (!sd->change_level_2nd)
  1647. {
  1648. if (sd->class_&JOBL_THIRD)
  1649. {
  1650. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1651. if (!sd->change_level_3rd)
  1652. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1653. else
  1654. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1655. - (sd->status.job_level - 1)
  1656. - (sd->change_level_3rd - 1)
  1657. - novice_skills;
  1658. }
  1659. else
  1660. {
  1661. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1662. - (sd->status.job_level - 1)
  1663. - novice_skills;
  1664. }
  1665. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1666. }
  1667. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1668. {
  1669. c &= MAPID_BASEMASK;
  1670. }
  1671. // limit 3rd class to 2nd class/trans job levels
  1672. else if(sd->class_&JOBL_THIRD)
  1673. {
  1674. // regenerate change_level_3rd
  1675. if (!sd->change_level_3rd)
  1676. {
  1677. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1678. - (sd->status.job_level - 1)
  1679. - (sd->change_level_2nd - 1)
  1680. - novice_skills;
  1681. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1682. }
  1683. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1684. c &= MAPID_UPPERMASK;
  1685. }
  1686. }
  1687. // restore non-limiting flags
  1688. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1689. return c;
  1690. }
  1691. /*==========================================
  1692. * Updates the weight status
  1693. *------------------------------------------
  1694. * 1: overweight 50% for pre-renewal and 70% for renewal
  1695. * 2: overweight 90%
  1696. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1697. */
  1698. void pc_updateweightstatus(struct map_session_data *sd)
  1699. {
  1700. int old_overweight;
  1701. int new_overweight;
  1702. nullpo_retv(sd);
  1703. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1704. #ifdef RENEWAL
  1705. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1706. #else
  1707. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1708. #endif
  1709. if( old_overweight == new_overweight )
  1710. return; // no change
  1711. // stop old status change
  1712. if( old_overweight == 1 )
  1713. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1714. else if( old_overweight == 2 )
  1715. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1716. // start new status change
  1717. if( new_overweight == 1 )
  1718. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1719. else if( new_overweight == 2 )
  1720. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1721. // update overweight status
  1722. sd->regen.state.overweight = new_overweight;
  1723. }
  1724. int pc_disguise(struct map_session_data *sd, int class_)
  1725. {
  1726. if (!class_ && !sd->disguise)
  1727. return 0;
  1728. if (class_ && sd->disguise == class_)
  1729. return 0;
  1730. if(pc_isinvisible(sd))
  1731. { //Character is invisible. Stealth class-change. [Skotlex]
  1732. sd->disguise = class_; //viewdata is set on uncloaking.
  1733. return 2;
  1734. }
  1735. if (sd->bl.prev != NULL) {
  1736. pc_stop_walking(sd, 0);
  1737. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1738. }
  1739. if (!class_) {
  1740. sd->disguise = 0;
  1741. class_ = sd->status.class_;
  1742. } else
  1743. sd->disguise=class_;
  1744. status_set_viewdata(&sd->bl, class_);
  1745. clif_changeoption(&sd->bl);
  1746. if (sd->bl.prev != NULL) {
  1747. clif_spawn(&sd->bl);
  1748. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1749. { //It seems the cart info is lost on undisguise.
  1750. clif_cartlist(sd);
  1751. clif_updatestatus(sd,SP_CARTINFO);
  1752. }
  1753. if (sd->chatID) {
  1754. struct chat_data* cd;
  1755. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1756. clif_dispchat(cd,0);
  1757. }
  1758. }
  1759. return 1;
  1760. }
  1761. /// Show error message
  1762. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1763. /// Check for valid Element, break & show error message if invalid Element
  1764. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1765. /// Check for valid Race, break & show error message if invalid Race
  1766. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1767. /// Check for valid Race2, break & show error message if invalid Race2
  1768. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1769. /// Check for valid Class, break & show error message if invalid Class
  1770. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1771. /// Check for valid Size, break & show error message if invalid Size
  1772. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1773. /// Check for valid SC, break & show error message if invalid SC
  1774. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1775. /**
  1776. * Add auto spell bonus for player while attacking/attacked
  1777. * @param spell: Spell array
  1778. * @param id: Skill to cast
  1779. * @param lv: Skill level
  1780. * @param rate: Success chance
  1781. * @param flag: Battle flag
  1782. * @param card_id: Used to prevent card stacking
  1783. */
  1784. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1785. {
  1786. if (spell.size() == MAX_PC_BONUS) {
  1787. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1788. return;
  1789. }
  1790. if (!rate)
  1791. return;
  1792. if (!(flag&BF_RANGEMASK))
  1793. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1794. if (!(flag&BF_WEAPONMASK))
  1795. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1796. if (!(flag&BF_SKILLMASK)) {
  1797. if (flag&(BF_MAGIC | BF_MISC))
  1798. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1799. if (flag&BF_WEAPON)
  1800. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1801. }
  1802. if (!battle_config.autospell_stacking && rate > 0) // Stacking disabled, make a new entry
  1803. ;
  1804. else {
  1805. for (auto &it : spell) {
  1806. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  1807. it.rate += rate;
  1808. return;
  1809. }
  1810. }
  1811. }
  1812. struct s_autospell entry;
  1813. entry.id = id;
  1814. entry.lv = lv;
  1815. entry.rate = rate;
  1816. entry.flag = flag;
  1817. entry.card_id = card_id;
  1818. spell.push_back(entry);
  1819. }
  1820. /**
  1821. * Add auto spell bonus for player while using skills
  1822. * @param spell: Spell array
  1823. * @param src_skill: Trigger skill
  1824. * @param id: Support or target type
  1825. * @param lv: Skill level
  1826. * @param rate: Success chance
  1827. * @param card_id: Used to prevent card stacking
  1828. */
  1829. static void pc_bonus_autospell_onskill(std::vector<s_autospell> spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1830. {
  1831. if (spell.size() == MAX_PC_BONUS) {
  1832. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1833. return;
  1834. }
  1835. if (!rate)
  1836. return;
  1837. struct s_autospell entry;
  1838. entry.flag = src_skill;
  1839. entry.id = id;
  1840. entry.lv = lv;
  1841. entry.rate = rate;
  1842. entry.card_id = card_id;
  1843. spell.push_back(entry);
  1844. }
  1845. /**
  1846. * Add inflict effect bonus for player while attacking/attacked
  1847. * @param effect: Effect array
  1848. * @param sc: SC/Effect type
  1849. * @param rate: Success chance
  1850. * @param arrow_rate: success chance if bonus comes from arrow-type item
  1851. * @param flag: Target flag
  1852. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  1853. */
  1854. 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)
  1855. {
  1856. if (effect.size() == MAX_PC_BONUS) {
  1857. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  1858. return;
  1859. }
  1860. if (!(flag&(ATF_SHORT | ATF_LONG)))
  1861. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  1862. if (!(flag&(ATF_TARGET | ATF_SELF)))
  1863. flag |= ATF_TARGET; //Default target: enemy.
  1864. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  1865. flag |= ATF_WEAPON; //Default type: weapon.
  1866. if (!duration)
  1867. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  1868. for (auto &it : effect) {
  1869. if (it.sc == sc && it.flag == flag) {
  1870. it.rate += rate;
  1871. it.arrow_rate += arrow_rate;
  1872. it.duration = umax(it.duration, duration);
  1873. return;
  1874. }
  1875. }
  1876. struct s_addeffect entry;
  1877. entry.sc = sc;
  1878. entry.rate = rate;
  1879. entry.arrow_rate = arrow_rate;
  1880. entry.flag = flag;
  1881. entry.duration = duration;
  1882. effect.push_back(entry);
  1883. }
  1884. /**
  1885. * Add inflict effect bonus for player while attacking using skill
  1886. * @param effect: Effect array
  1887. * @param sc: SC/Effect type
  1888. * @param rate: Success chance
  1889. * @param skill_id: Skill to cast
  1890. * @param target: Target type
  1891. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  1892. */
  1893. 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)
  1894. {
  1895. if (effect.size() == MAX_PC_BONUS) {
  1896. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  1897. return;
  1898. }
  1899. if (!duration)
  1900. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  1901. for (auto &it : effect) {
  1902. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  1903. it.rate += rate;
  1904. it.duration = umax(it.duration, duration);
  1905. return;
  1906. }
  1907. }
  1908. struct s_addeffectonskill entry;
  1909. entry.sc = sc;
  1910. entry.rate = rate;
  1911. entry.skill_id = skill_id;
  1912. entry.target = target;
  1913. entry.duration = duration;
  1914. effect.push_back(entry);
  1915. }
  1916. /**
  1917. * Adjust/add drop rate modifier for player
  1918. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1919. * @param nameid: item id that will be dropped
  1920. * @param group: group id
  1921. * @param class_: target class
  1922. * @param race: target race. if < 0, means monster_id
  1923. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1924. */
  1925. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  1926. {
  1927. if (!nameid && !group) {
  1928. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1929. return;
  1930. }
  1931. if (nameid && !itemdb_exists(nameid)) {
  1932. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1933. return;
  1934. }
  1935. if (group && !itemdb_group_exists(group)) {
  1936. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1937. return;
  1938. }
  1939. if (drop.size() == MAX_PC_BONUS) {
  1940. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %hu group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  1941. return;
  1942. }
  1943. //Apply config rate adjustment settings.
  1944. if (rate >= 0) { //Absolute drop.
  1945. if (battle_config.item_rate_adddrop != 100)
  1946. rate = rate*battle_config.item_rate_adddrop/100;
  1947. if (rate < battle_config.item_drop_adddrop_min)
  1948. rate = battle_config.item_drop_adddrop_min;
  1949. else if (rate > battle_config.item_drop_adddrop_max)
  1950. rate = battle_config.item_drop_adddrop_max;
  1951. } else { //Relative drop, max/min limits are applied at drop time.
  1952. if (battle_config.item_rate_adddrop != 100)
  1953. rate = rate*battle_config.item_rate_adddrop/100;
  1954. if (rate > -1)
  1955. rate = -1;
  1956. }
  1957. for (auto &it : drop) {
  1958. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  1959. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  1960. it.rate += rate;
  1961. return;
  1962. }
  1963. }
  1964. struct s_add_drop entry;
  1965. entry.nameid = nameid;
  1966. entry.group = group;
  1967. entry.race = race;
  1968. entry.class_ = class_;
  1969. entry.rate = rate;
  1970. drop.push_back(entry);
  1971. }
  1972. /**
  1973. * Add autobonus to player when attacking/attacked
  1974. * @param bonus: Bonus array
  1975. * @param script: Script to execute
  1976. * @param rate: Success chance
  1977. * @param dur: Duration
  1978. * @param flag: Battle flag
  1979. * @param other_script: Secondary script to execute
  1980. * @param pos: Item equip position
  1981. * @param onskill: Skill used to trigger autobonus
  1982. * @return True on success or false otherwise
  1983. */
  1984. bool pc_addautobonus(std::vector<s_autobonus> &bonus, const char *script, short rate, unsigned int dur, short flag, const char *other_script, unsigned int pos, bool onskill)
  1985. {
  1986. if (bonus.size() == MAX_PC_BONUS) {
  1987. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  1988. return false;
  1989. }
  1990. if (!onskill) {
  1991. if (!(flag&BF_RANGEMASK))
  1992. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1993. if (!(flag&BF_WEAPONMASK))
  1994. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1995. if (!(flag&BF_SKILLMASK)) {
  1996. if (flag&(BF_MAGIC | BF_MISC))
  1997. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1998. if (flag&BF_WEAPON)
  1999. flag |= BF_NORMAL | BF_SKILL;
  2000. }
  2001. }
  2002. struct s_autobonus entry;
  2003. entry.rate = rate;
  2004. entry.duration = dur;
  2005. entry.active = INVALID_TIMER;
  2006. entry.atk_type = flag;
  2007. entry.pos = pos;
  2008. entry.bonus_script = aStrdup(script);
  2009. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2010. bonus.push_back(entry);
  2011. return true;
  2012. }
  2013. /**
  2014. * Remove an autobonus from player
  2015. * @param sd: Player data
  2016. * @param bonus: Autobonus array
  2017. * @param restore: Run script on clearing or not
  2018. */
  2019. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2020. {
  2021. if (!sd)
  2022. return;
  2023. for (uint8 i = 0; i < bonus.size(); i++) {
  2024. if (bonus[i].active != INVALID_TIMER) {
  2025. if (restore && (sd->state.autobonus&bonus[i].pos) == bonus[i].pos) {
  2026. if (bonus[i].bonus_script) {
  2027. unsigned int equip_pos_idx = 0;
  2028. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2029. for (uint8 j = 0; j < EQI_MAX; j++) {
  2030. if (sd->equip_index[j] >= 0)
  2031. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2032. }
  2033. if ((equip_pos_idx&bonus[i].pos) == bonus[i].pos)
  2034. script_run_autobonus(bonus[i].bonus_script, sd, bonus[i].pos);
  2035. }
  2036. continue;
  2037. } else { // Logout / Unequipped an item with an activated bonus
  2038. delete_timer(bonus[i].active, pc_endautobonus);
  2039. bonus[i].active = INVALID_TIMER;
  2040. }
  2041. }
  2042. if (bonus[i].bonus_script)
  2043. aFree(bonus[i].bonus_script);
  2044. if (bonus[i].other_script)
  2045. aFree(bonus[i].other_script);
  2046. }
  2047. bonus.clear();
  2048. }
  2049. /**
  2050. * Execute autobonus on player
  2051. * @param sd: Player data
  2052. * @param autobonus: Autobonus to run
  2053. */
  2054. void pc_exeautobonus(struct map_session_data *sd, struct s_autobonus *autobonus)
  2055. {
  2056. if (!sd || !autobonus)
  2057. return;
  2058. if (autobonus->active != INVALID_TIMER)
  2059. delete_timer(autobonus->active, pc_endautobonus);
  2060. if( autobonus->other_script )
  2061. {
  2062. int j;
  2063. unsigned int equip_pos_idx = 0;
  2064. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2065. for(j = 0; j < EQI_MAX; j++) {
  2066. if(sd->equip_index[j] >= 0)
  2067. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2068. }
  2069. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2070. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2071. }
  2072. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  2073. sd->state.autobonus |= autobonus->pos;
  2074. status_calc_pc(sd,SCO_FORCE);
  2075. }
  2076. /**
  2077. * Remove autobonus timer from player
  2078. */
  2079. TIMER_FUNC(pc_endautobonus){
  2080. struct map_session_data *sd = map_id2sd(id);
  2081. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  2082. nullpo_ret(sd);
  2083. nullpo_ret(autobonus);
  2084. autobonus->active = INVALID_TIMER;
  2085. sd->state.autobonus &= ~autobonus->pos;
  2086. status_calc_pc(sd,SCO_FORCE);
  2087. return 0;
  2088. }
  2089. /**
  2090. * Add element bonus to player when attacking
  2091. * @param sd: Player data
  2092. * @param ele: Element to adjust
  2093. * @param rate: Success chance
  2094. * @param flag: Battle flag
  2095. */
  2096. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2097. {
  2098. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2099. struct s_addele2 entry;
  2100. if (wd->addele2.size() == MAX_PC_BONUS) {
  2101. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2102. return;
  2103. }
  2104. if (!(flag&BF_RANGEMASK))
  2105. flag |= BF_SHORT | BF_LONG;
  2106. if (!(flag&BF_WEAPONMASK))
  2107. flag |= BF_WEAPON;
  2108. if (!(flag&BF_SKILLMASK)) {
  2109. if (flag&(BF_MAGIC | BF_MISC))
  2110. flag |= BF_SKILL;
  2111. if (flag&BF_WEAPON)
  2112. flag |= BF_NORMAL | BF_SKILL;
  2113. }
  2114. for (auto &it : wd->addele2) {
  2115. if (it.ele == ele) {
  2116. it.rate += rate;
  2117. it.flag = flag;
  2118. return;
  2119. }
  2120. }
  2121. entry.ele = ele;
  2122. entry.rate = rate;
  2123. entry.flag = flag;
  2124. wd->addele2.push_back(entry);
  2125. }
  2126. /**
  2127. * Reduce element bonus to player when attacking
  2128. * @param sd: Player data
  2129. * @param ele: Element to adjust
  2130. * @param rate: Success chance
  2131. * @param flag: Battle flag
  2132. */
  2133. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2134. {
  2135. struct s_addele2 entry;
  2136. if (sd->subele2.size() == MAX_PC_BONUS) {
  2137. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2138. return;
  2139. }
  2140. if (!(flag&BF_RANGEMASK))
  2141. flag |= BF_SHORT | BF_LONG;
  2142. if (!(flag&BF_WEAPONMASK))
  2143. flag |= BF_WEAPON;
  2144. if (!(flag&BF_SKILLMASK)) {
  2145. if (flag&(BF_MAGIC | BF_MISC))
  2146. flag |= BF_SKILL;
  2147. if (flag&BF_WEAPON)
  2148. flag |= BF_NORMAL | BF_SKILL;
  2149. }
  2150. for (auto &it : sd->subele2) {
  2151. if (it.ele == ele) {
  2152. it.rate += rate;
  2153. it.flag = flag;
  2154. return;
  2155. }
  2156. }
  2157. entry.ele = ele;
  2158. entry.rate = rate;
  2159. entry.flag = flag;
  2160. sd->subele2.push_back(entry);
  2161. }
  2162. /**
  2163. * General item bonus for player
  2164. * @param bonus: Bonus array
  2165. * @param id: Key
  2166. * @param val: Value
  2167. */
  2168. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val)
  2169. {
  2170. struct s_item_bonus entry;
  2171. for (auto &it : bonus) {
  2172. if (it.id == id) {
  2173. it.val += val;
  2174. return;
  2175. }
  2176. }
  2177. entry.id = id;
  2178. entry.val = val;
  2179. bonus.push_back(entry);
  2180. }
  2181. /*==========================================
  2182. * Add a bonus(type) to player sd
  2183. * format: bonus bBonusName,val;
  2184. * @param sd
  2185. * @param type Bonus type used by bBonusName
  2186. * @param val Value that usually for rate or fixed value
  2187. *------------------------------------------*/
  2188. void pc_bonus(struct map_session_data *sd,int type,int val)
  2189. {
  2190. struct status_data *status;
  2191. int bonus;
  2192. nullpo_retv(sd);
  2193. status = &sd->base_status;
  2194. switch(type){
  2195. case SP_STR:
  2196. case SP_AGI:
  2197. case SP_VIT:
  2198. case SP_INT:
  2199. case SP_DEX:
  2200. case SP_LUK:
  2201. if(sd->state.lr_flag != 2)
  2202. sd->param_bonus[type-SP_STR]+=val;
  2203. break;
  2204. case SP_ATK1:
  2205. if(!sd->state.lr_flag) {
  2206. bonus = status->rhw.atk + val;
  2207. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2208. }
  2209. else if(sd->state.lr_flag == 1) {
  2210. bonus = status->lhw.atk + val;
  2211. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2212. }
  2213. break;
  2214. case SP_ATK2:
  2215. if(!sd->state.lr_flag) {
  2216. bonus = status->rhw.atk2 + val;
  2217. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2218. }
  2219. else if(sd->state.lr_flag == 1) {
  2220. bonus = status->lhw.atk2 + val;
  2221. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2222. }
  2223. break;
  2224. case SP_BASE_ATK:
  2225. if(sd->state.lr_flag != 2) {
  2226. #ifdef RENEWAL
  2227. bonus = sd->bonus.eatk + val;
  2228. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2229. #else
  2230. bonus = status->batk + val;
  2231. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2232. #endif
  2233. }
  2234. break;
  2235. case SP_DEF1:
  2236. if(sd->state.lr_flag != 2) {
  2237. bonus = status->def + val;
  2238. #ifdef RENEWAL
  2239. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2240. #else
  2241. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2242. #endif
  2243. }
  2244. break;
  2245. case SP_DEF2:
  2246. if(sd->state.lr_flag != 2) {
  2247. bonus = status->def2 + val;
  2248. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2249. }
  2250. break;
  2251. case SP_MDEF1:
  2252. if(sd->state.lr_flag != 2) {
  2253. bonus = status->mdef + val;
  2254. #ifdef RENEWAL
  2255. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2256. #else
  2257. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2258. #endif
  2259. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2260. sd->bonus.shieldmdef += bonus;
  2261. }
  2262. }
  2263. break;
  2264. case SP_MDEF2:
  2265. if(sd->state.lr_flag != 2) {
  2266. bonus = status->mdef2 + val;
  2267. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2268. }
  2269. break;
  2270. case SP_HIT:
  2271. if(sd->state.lr_flag != 2) {
  2272. bonus = status->hit + val;
  2273. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2274. } else
  2275. sd->bonus.arrow_hit+=val;
  2276. break;
  2277. case SP_FLEE1:
  2278. if(sd->state.lr_flag != 2) {
  2279. bonus = status->flee + val;
  2280. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2281. }
  2282. break;
  2283. case SP_FLEE2:
  2284. if(sd->state.lr_flag != 2) {
  2285. bonus = status->flee2 + val*10;
  2286. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2287. }
  2288. break;
  2289. case SP_CRITICAL:
  2290. if(sd->state.lr_flag != 2) {
  2291. bonus = status->cri + val*10;
  2292. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2293. } else
  2294. sd->bonus.arrow_cri += val*10;
  2295. break;
  2296. case SP_ATKELE:
  2297. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2298. switch (sd->state.lr_flag)
  2299. {
  2300. case 2:
  2301. switch (sd->status.weapon) {
  2302. case W_BOW:
  2303. case W_REVOLVER:
  2304. case W_RIFLE:
  2305. case W_GATLING:
  2306. case W_SHOTGUN:
  2307. case W_GRENADE:
  2308. //Become weapon element.
  2309. status->rhw.ele=val;
  2310. break;
  2311. default: //Become arrow element.
  2312. sd->bonus.arrow_ele=val;
  2313. break;
  2314. }
  2315. break;
  2316. case 1:
  2317. status->lhw.ele=val;
  2318. break;
  2319. default:
  2320. status->rhw.ele=val;
  2321. break;
  2322. }
  2323. break;
  2324. case SP_DEFELE:
  2325. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2326. if(sd->state.lr_flag != 2)
  2327. status->def_ele=val;
  2328. break;
  2329. case SP_MAXHP:
  2330. if(sd->state.lr_flag == 2)
  2331. break;
  2332. sd->bonus.hp += val;
  2333. break;
  2334. case SP_MAXSP:
  2335. if(sd->state.lr_flag == 2)
  2336. break;
  2337. sd->bonus.sp += val;
  2338. break;
  2339. case SP_MAXHPRATE:
  2340. if(sd->state.lr_flag != 2)
  2341. sd->hprate+=val;
  2342. break;
  2343. case SP_MAXSPRATE:
  2344. if(sd->state.lr_flag != 2)
  2345. sd->sprate+=val;
  2346. break;
  2347. case SP_SPRATE:
  2348. if(sd->state.lr_flag != 2)
  2349. sd->dsprate+=val;
  2350. break;
  2351. case SP_ATTACKRANGE:
  2352. switch (sd->state.lr_flag) {
  2353. case 2:
  2354. switch (sd->status.weapon) {
  2355. case W_BOW:
  2356. case W_REVOLVER:
  2357. case W_RIFLE:
  2358. case W_GATLING:
  2359. case W_SHOTGUN:
  2360. case W_GRENADE:
  2361. status->rhw.range += val;
  2362. }
  2363. break;
  2364. case 1:
  2365. status->lhw.range += val;
  2366. break;
  2367. default:
  2368. status->rhw.range += val;
  2369. break;
  2370. }
  2371. break;
  2372. case SP_SPEED_RATE: //Non stackable increase
  2373. if(sd->state.lr_flag != 2)
  2374. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2375. break;
  2376. case SP_SPEED_ADDRATE: //Stackable increase
  2377. if(sd->state.lr_flag != 2)
  2378. sd->bonus.speed_add_rate -= val;
  2379. break;
  2380. case SP_ASPD: //Raw increase
  2381. if(sd->state.lr_flag != 2)
  2382. sd->bonus.aspd_add -= 10*val;
  2383. break;
  2384. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2385. if(sd->state.lr_flag != 2)
  2386. #ifndef RENEWAL_ASPD
  2387. status->aspd_rate -= 10*val;
  2388. #else
  2389. status->aspd_rate2 += val;
  2390. #endif
  2391. break;
  2392. case SP_HP_RECOV_RATE:
  2393. if(sd->state.lr_flag != 2)
  2394. sd->hprecov_rate += val;
  2395. break;
  2396. case SP_SP_RECOV_RATE:
  2397. if(sd->state.lr_flag != 2)
  2398. sd->sprecov_rate += val;
  2399. break;
  2400. case SP_CRITICAL_DEF:
  2401. if(sd->state.lr_flag != 2)
  2402. sd->bonus.critical_def += val;
  2403. break;
  2404. case SP_NEAR_ATK_DEF:
  2405. if(sd->state.lr_flag != 2)
  2406. sd->bonus.near_attack_def_rate += val;
  2407. break;
  2408. case SP_LONG_ATK_DEF:
  2409. if(sd->state.lr_flag != 2)
  2410. sd->bonus.long_attack_def_rate += val;
  2411. break;
  2412. case SP_DOUBLE_RATE:
  2413. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2414. sd->bonus.double_rate = val;
  2415. break;
  2416. case SP_DOUBLE_ADD_RATE:
  2417. if(sd->state.lr_flag == 0)
  2418. sd->bonus.double_add_rate += val;
  2419. break;
  2420. case SP_MATK_RATE:
  2421. if(sd->state.lr_flag != 2)
  2422. sd->matk_rate += val;
  2423. break;
  2424. case SP_IGNORE_DEF_ELE:
  2425. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2426. if(!sd->state.lr_flag)
  2427. sd->right_weapon.ignore_def_ele |= 1<<val;
  2428. else if(sd->state.lr_flag == 1)
  2429. sd->left_weapon.ignore_def_ele |= 1<<val;
  2430. break;
  2431. case SP_IGNORE_DEF_RACE:
  2432. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2433. if(!sd->state.lr_flag)
  2434. sd->right_weapon.ignore_def_race |= 1<<val;
  2435. else if(sd->state.lr_flag == 1)
  2436. sd->left_weapon.ignore_def_race |= 1<<val;
  2437. break;
  2438. case SP_IGNORE_DEF_CLASS:
  2439. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2440. if(!sd->state.lr_flag)
  2441. sd->right_weapon.ignore_def_class |= 1<<val;
  2442. else if(sd->state.lr_flag == 1)
  2443. sd->left_weapon.ignore_def_class |= 1<<val;
  2444. break;
  2445. case SP_ATK_RATE:
  2446. if(sd->state.lr_flag != 2)
  2447. sd->bonus.atk_rate += val;
  2448. break;
  2449. case SP_MAGIC_ATK_DEF:
  2450. if(sd->state.lr_flag != 2)
  2451. sd->bonus.magic_def_rate += val;
  2452. break;
  2453. case SP_MISC_ATK_DEF:
  2454. if(sd->state.lr_flag != 2)
  2455. sd->bonus.misc_def_rate += val;
  2456. break;
  2457. case SP_IGNORE_MDEF_ELE:
  2458. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2459. if(sd->state.lr_flag != 2)
  2460. sd->bonus.ignore_mdef_ele |= 1<<val;
  2461. break;
  2462. case SP_IGNORE_MDEF_RACE:
  2463. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2464. if(sd->state.lr_flag != 2)
  2465. sd->bonus.ignore_mdef_race |= 1<<val;
  2466. break;
  2467. case SP_PERFECT_HIT_RATE:
  2468. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2469. sd->bonus.perfect_hit = val;
  2470. break;
  2471. case SP_PERFECT_HIT_ADD_RATE:
  2472. if(sd->state.lr_flag != 2)
  2473. sd->bonus.perfect_hit_add += val;
  2474. break;
  2475. case SP_CRITICAL_RATE:
  2476. if(sd->state.lr_flag != 2)
  2477. sd->critical_rate+=val;
  2478. break;
  2479. case SP_DEF_RATIO_ATK_ELE:
  2480. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2481. if(!sd->state.lr_flag)
  2482. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2483. else if(sd->state.lr_flag == 1)
  2484. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2485. break;
  2486. case SP_DEF_RATIO_ATK_RACE:
  2487. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2488. if(!sd->state.lr_flag)
  2489. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2490. else if(sd->state.lr_flag == 1)
  2491. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2492. break;
  2493. case SP_DEF_RATIO_ATK_CLASS:
  2494. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2495. if(!sd->state.lr_flag)
  2496. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2497. else if(sd->state.lr_flag == 1)
  2498. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2499. break;
  2500. case SP_HIT_RATE:
  2501. if(sd->state.lr_flag != 2)
  2502. sd->hit_rate += val;
  2503. break;
  2504. case SP_FLEE_RATE:
  2505. if(sd->state.lr_flag != 2)
  2506. sd->flee_rate += val;
  2507. break;
  2508. case SP_FLEE2_RATE:
  2509. if(sd->state.lr_flag != 2)
  2510. sd->flee2_rate += val;
  2511. break;
  2512. case SP_DEF_RATE:
  2513. if(sd->state.lr_flag != 2)
  2514. sd->def_rate += val;
  2515. break;
  2516. case SP_DEF2_RATE:
  2517. if(sd->state.lr_flag != 2)
  2518. sd->def2_rate += val;
  2519. break;
  2520. case SP_MDEF_RATE:
  2521. if(sd->state.lr_flag != 2)
  2522. sd->mdef_rate += val;
  2523. break;
  2524. case SP_MDEF2_RATE:
  2525. if(sd->state.lr_flag != 2)
  2526. sd->mdef2_rate += val;
  2527. break;
  2528. case SP_RESTART_FULL_RECOVER:
  2529. if(sd->state.lr_flag != 2)
  2530. sd->special_state.restart_full_recover = 1;
  2531. break;
  2532. case SP_NO_CASTCANCEL:
  2533. if(sd->state.lr_flag != 2)
  2534. sd->special_state.no_castcancel = 1;
  2535. break;
  2536. case SP_NO_CASTCANCEL2:
  2537. if(sd->state.lr_flag != 2)
  2538. sd->special_state.no_castcancel2 = 1;
  2539. break;
  2540. case SP_NO_SIZEFIX:
  2541. if(sd->state.lr_flag != 2)
  2542. sd->special_state.no_sizefix = 1;
  2543. break;
  2544. case SP_NO_MAGIC_DAMAGE:
  2545. if(sd->state.lr_flag == 2)
  2546. break;
  2547. val+= sd->special_state.no_magic_damage;
  2548. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2549. break;
  2550. case SP_NO_WEAPON_DAMAGE:
  2551. if(sd->state.lr_flag == 2)
  2552. break;
  2553. val+= sd->special_state.no_weapon_damage;
  2554. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2555. break;
  2556. case SP_NO_MISC_DAMAGE:
  2557. if(sd->state.lr_flag == 2)
  2558. break;
  2559. val+= sd->special_state.no_misc_damage;
  2560. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2561. break;
  2562. case SP_NO_GEMSTONE:
  2563. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2564. sd->special_state.no_gemstone = 1;
  2565. break;
  2566. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2567. if(sd->state.lr_flag != 2) {
  2568. sd->special_state.intravision = 1;
  2569. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2570. }
  2571. break;
  2572. case SP_NO_KNOCKBACK:
  2573. if(sd->state.lr_flag != 2)
  2574. sd->special_state.no_knockback = 1;
  2575. break;
  2576. case SP_SPLASH_RANGE:
  2577. if(sd->bonus.splash_range < val)
  2578. sd->bonus.splash_range = val;
  2579. break;
  2580. case SP_SPLASH_ADD_RANGE:
  2581. sd->bonus.splash_add_range += val;
  2582. break;
  2583. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2584. if(sd->state.lr_flag != 2)
  2585. sd->bonus.short_weapon_damage_return += val;
  2586. break;
  2587. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2588. if(sd->state.lr_flag != 2)
  2589. sd->bonus.long_weapon_damage_return += val;
  2590. break;
  2591. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2592. if(sd->state.lr_flag != 2)
  2593. sd->bonus.magic_damage_return += val;
  2594. break;
  2595. case SP_ALL_STATS: // [Valaris]
  2596. if(sd->state.lr_flag!=2) {
  2597. sd->param_bonus[SP_STR-SP_STR]+=val;
  2598. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2599. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2600. sd->param_bonus[SP_INT-SP_STR]+=val;
  2601. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2602. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2603. }
  2604. break;
  2605. case SP_AGI_VIT: // [Valaris]
  2606. if(sd->state.lr_flag!=2) {
  2607. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2608. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2609. }
  2610. break;
  2611. case SP_AGI_DEX_STR: // [Valaris]
  2612. if(sd->state.lr_flag!=2) {
  2613. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2614. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2615. sd->param_bonus[SP_STR-SP_STR]+=val;
  2616. }
  2617. break;
  2618. case SP_PERFECT_HIDE: // [Valaris]
  2619. if(sd->state.lr_flag!=2)
  2620. sd->special_state.perfect_hiding=1;
  2621. break;
  2622. case SP_UNBREAKABLE:
  2623. if(sd->state.lr_flag!=2)
  2624. sd->bonus.unbreakable += val;
  2625. break;
  2626. case SP_UNBREAKABLE_WEAPON:
  2627. if(sd->state.lr_flag != 2)
  2628. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2629. break;
  2630. case SP_UNBREAKABLE_ARMOR:
  2631. if(sd->state.lr_flag != 2)
  2632. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2633. break;
  2634. case SP_UNBREAKABLE_HELM:
  2635. if(sd->state.lr_flag != 2)
  2636. sd->bonus.unbreakable_equip |= EQP_HELM;
  2637. break;
  2638. case SP_UNBREAKABLE_SHIELD:
  2639. if(sd->state.lr_flag != 2)
  2640. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2641. break;
  2642. case SP_UNBREAKABLE_GARMENT:
  2643. if(sd->state.lr_flag != 2)
  2644. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2645. break;
  2646. case SP_UNBREAKABLE_SHOES:
  2647. if(sd->state.lr_flag != 2)
  2648. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2649. break;
  2650. case SP_CLASSCHANGE: // [Valaris]
  2651. if(sd->state.lr_flag !=2)
  2652. sd->bonus.classchange=val;
  2653. break;
  2654. case SP_LONG_ATK_RATE:
  2655. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2656. sd->bonus.long_attack_atk_rate+=val;
  2657. break;
  2658. case SP_BREAK_WEAPON_RATE:
  2659. if(sd->state.lr_flag != 2)
  2660. sd->bonus.break_weapon_rate+=val;
  2661. break;
  2662. case SP_BREAK_ARMOR_RATE:
  2663. if(sd->state.lr_flag != 2)
  2664. sd->bonus.break_armor_rate+=val;
  2665. break;
  2666. case SP_ADD_STEAL_RATE:
  2667. if(sd->state.lr_flag != 2)
  2668. sd->bonus.add_steal_rate+=val;
  2669. break;
  2670. case SP_DELAYRATE:
  2671. if(sd->state.lr_flag != 2)
  2672. sd->delayrate+=val;
  2673. break;
  2674. case SP_CRIT_ATK_RATE:
  2675. if(sd->state.lr_flag != 2)
  2676. sd->bonus.crit_atk_rate += val;
  2677. break;
  2678. case SP_NO_REGEN:
  2679. if(sd->state.lr_flag != 2)
  2680. sd->regen.state.block|=val;
  2681. break;
  2682. case SP_UNSTRIPABLE_WEAPON:
  2683. if(sd->state.lr_flag != 2)
  2684. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2685. break;
  2686. case SP_UNSTRIPABLE:
  2687. case SP_UNSTRIPABLE_ARMOR:
  2688. if(sd->state.lr_flag != 2)
  2689. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2690. break;
  2691. case SP_UNSTRIPABLE_HELM:
  2692. if(sd->state.lr_flag != 2)
  2693. sd->bonus.unstripable_equip |= EQP_HELM;
  2694. break;
  2695. case SP_UNSTRIPABLE_SHIELD:
  2696. if(sd->state.lr_flag != 2)
  2697. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2698. break;
  2699. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2700. if(!sd->state.lr_flag) {
  2701. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2702. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2703. } else if(sd->state.lr_flag == 1) {
  2704. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2705. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2706. }
  2707. break;
  2708. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2709. if(!sd->state.lr_flag) {
  2710. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2711. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2712. } else if(sd->state.lr_flag == 1) {
  2713. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2714. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2715. }
  2716. break;
  2717. case SP_SP_GAIN_VALUE:
  2718. if(!sd->state.lr_flag)
  2719. sd->bonus.sp_gain_value += val;
  2720. break;
  2721. case SP_HP_GAIN_VALUE:
  2722. if(!sd->state.lr_flag)
  2723. sd->bonus.hp_gain_value += val;
  2724. break;
  2725. case SP_MAGIC_SP_GAIN_VALUE:
  2726. if(!sd->state.lr_flag)
  2727. sd->bonus.magic_sp_gain_value += val;
  2728. break;
  2729. case SP_MAGIC_HP_GAIN_VALUE:
  2730. if(!sd->state.lr_flag)
  2731. sd->bonus.magic_hp_gain_value += val;
  2732. break;
  2733. case SP_ADD_HEAL_RATE:
  2734. if(sd->state.lr_flag != 2)
  2735. sd->bonus.add_heal_rate += val;
  2736. break;
  2737. case SP_ADD_HEAL2_RATE:
  2738. if(sd->state.lr_flag != 2)
  2739. sd->bonus.add_heal2_rate += val;
  2740. break;
  2741. case SP_ADD_ITEM_HEAL_RATE:
  2742. if(sd->state.lr_flag != 2)
  2743. sd->bonus.itemhealrate2 += val;
  2744. break;
  2745. case SP_EMATK:
  2746. if(sd->state.lr_flag != 2)
  2747. sd->bonus.ematk += val;
  2748. break;
  2749. #ifdef RENEWAL_CAST
  2750. case SP_FIXCASTRATE:
  2751. if(sd->state.lr_flag != 2)
  2752. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2753. break;
  2754. case SP_ADD_FIXEDCAST:
  2755. if(sd->state.lr_flag != 2)
  2756. sd->bonus.add_fixcast += val;
  2757. break;
  2758. case SP_CASTRATE:
  2759. case SP_VARCASTRATE:
  2760. if(sd->state.lr_flag != 2)
  2761. sd->bonus.varcastrate -= val;
  2762. break;
  2763. case SP_ADD_VARIABLECAST:
  2764. if(sd->state.lr_flag != 2)
  2765. sd->bonus.add_varcast += val;
  2766. break;
  2767. #else
  2768. case SP_ADD_FIXEDCAST:
  2769. case SP_FIXCASTRATE:
  2770. case SP_ADD_VARIABLECAST:
  2771. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2772. break;
  2773. case SP_VARCASTRATE:
  2774. case SP_CASTRATE:
  2775. if(sd->state.lr_flag != 2)
  2776. sd->castrate += val;
  2777. break;
  2778. #endif
  2779. case SP_ADDMAXWEIGHT:
  2780. if (sd->state.lr_flag != 2)
  2781. sd->add_max_weight += val;
  2782. break;
  2783. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2784. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2785. break;
  2786. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2787. if (sd->state.lr_flag != 2)
  2788. sd->bonus.critical_rangeatk += val*10;
  2789. else
  2790. sd->bonus.arrow_cri += val*10;
  2791. break;
  2792. case SP_WEAPON_ATK_RATE:
  2793. if (sd->state.lr_flag != 2)
  2794. sd->bonus.weapon_atk_rate += val;
  2795. break;
  2796. case SP_WEAPON_MATK_RATE:
  2797. if (sd->state.lr_flag != 2)
  2798. sd->bonus.weapon_matk_rate += val;
  2799. break;
  2800. case SP_NO_MADO_FUEL:
  2801. if (sd->state.lr_flag != 2)
  2802. sd->special_state.no_mado_fuel = 1;
  2803. break;
  2804. default:
  2805. if (running_npc_stat_calc_event) {
  2806. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2807. }
  2808. else if (current_equip_combo_pos > 0) {
  2809. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  2810. }
  2811. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  2812. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  2813. }
  2814. else {
  2815. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  2816. }
  2817. break;
  2818. }
  2819. }
  2820. /*==========================================
  2821. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2822. * format: bonus2 bBonusName,type2,val;
  2823. * @param sd
  2824. * @param type Bonus type used by bBonusName
  2825. * @param type2
  2826. * @param val Value that usually for rate or fixed value
  2827. *------------------------------------------*/
  2828. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2829. {
  2830. nullpo_retv(sd);
  2831. switch(type){
  2832. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2833. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2834. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2835. sd->right_weapon.addele[type2]+=val;
  2836. else if(sd->state.lr_flag == 1)
  2837. sd->left_weapon.addele[type2]+=val;
  2838. else if(sd->state.lr_flag == 2)
  2839. sd->arrow_addele[type2]+=val;
  2840. break;
  2841. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2842. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2843. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2844. sd->right_weapon.addrace[type2]+=val;
  2845. else if(sd->state.lr_flag == 1)
  2846. sd->left_weapon.addrace[type2]+=val;
  2847. else if(sd->state.lr_flag == 2)
  2848. sd->arrow_addrace[type2]+=val;
  2849. break;
  2850. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2851. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2852. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2853. sd->right_weapon.addclass[type2]+=val;
  2854. else if(sd->state.lr_flag == 1)
  2855. sd->left_weapon.addclass[type2]+=val;
  2856. else if(sd->state.lr_flag == 2)
  2857. sd->arrow_addclass[type2]+=val;
  2858. break;
  2859. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2860. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2861. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  2862. sd->right_weapon.addsize[type2]+=val;
  2863. else if(sd->state.lr_flag == 1)
  2864. sd->left_weapon.addsize[type2]+=val;
  2865. else if(sd->state.lr_flag == 2)
  2866. sd->arrow_addsize[type2]+=val;
  2867. break;
  2868. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2869. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2870. if(sd->state.lr_flag != 2)
  2871. sd->subele_script[type2] += val;
  2872. break;
  2873. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2874. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2875. if(sd->state.lr_flag != 2)
  2876. sd->subrace[type2]+=val;
  2877. break;
  2878. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2879. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2880. if(sd->state.lr_flag != 2)
  2881. sd->subclass[type2]+=val;
  2882. break;
  2883. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2884. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2885. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2886. break;
  2887. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2888. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2889. 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);
  2890. break;
  2891. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2892. if(sd->state.lr_flag == 2)
  2893. break;
  2894. if (sd->reseff.size() == MAX_PC_BONUS) {
  2895. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  2896. break;
  2897. }
  2898. val = cap_value(val, -10000, 10000);
  2899. pc_bonus_itembonus(sd->reseff, type2, val);
  2900. break;
  2901. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2902. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2903. if(sd->state.lr_flag != 2)
  2904. sd->magic_addele_script[type2] += val;
  2905. break;
  2906. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2907. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2908. if(sd->state.lr_flag != 2)
  2909. sd->magic_addrace[type2]+=val;
  2910. break;
  2911. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2912. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2913. if(sd->state.lr_flag != 2)
  2914. sd->magic_addclass[type2]+=val;
  2915. break;
  2916. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2917. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2918. if(sd->state.lr_flag != 2)
  2919. sd->magic_addsize[type2]+=val;
  2920. break;
  2921. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2922. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2923. if(sd->state.lr_flag != 2)
  2924. sd->magic_atk_ele[type2]+=val;
  2925. break;
  2926. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  2927. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2928. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  2929. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  2930. break;
  2931. }
  2932. pc_bonus_itembonus(wd->add_dmg, type2, val);
  2933. }
  2934. break;
  2935. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2936. if(sd->state.lr_flag == 2)
  2937. break;
  2938. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  2939. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  2940. break;
  2941. }
  2942. pc_bonus_itembonus(sd->add_mdmg, type2, val);
  2943. break;
  2944. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2945. if(sd->state.lr_flag == 2)
  2946. break;
  2947. if (sd->add_def.size() == MAX_PC_BONUS) {
  2948. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  2949. break;
  2950. }
  2951. pc_bonus_itembonus(sd->add_def, type2, val);
  2952. break;
  2953. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2954. if(sd->state.lr_flag == 2)
  2955. break;
  2956. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  2957. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  2958. break;
  2959. }
  2960. pc_bonus_itembonus(sd->add_mdef, type2, val);
  2961. break;
  2962. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2963. if(!sd->state.lr_flag) {
  2964. sd->right_weapon.hp_drain_rate.rate += type2;
  2965. sd->right_weapon.hp_drain_rate.per += val;
  2966. }
  2967. else if(sd->state.lr_flag == 1) {
  2968. sd->left_weapon.hp_drain_rate.rate += type2;
  2969. sd->left_weapon.hp_drain_rate.per += val;
  2970. }
  2971. break;
  2972. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2973. if(!sd->state.lr_flag) {
  2974. sd->right_weapon.sp_drain_rate.rate += type2;
  2975. sd->right_weapon.sp_drain_rate.per += val;
  2976. }
  2977. else if(sd->state.lr_flag == 1) {
  2978. sd->left_weapon.sp_drain_rate.rate += type2;
  2979. sd->left_weapon.sp_drain_rate.per += val;
  2980. }
  2981. break;
  2982. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2983. if(sd->state.lr_flag != 2) {
  2984. sd->bonus.sp_vanish_rate += type2;
  2985. sd->bonus.sp_vanish_per += val;
  2986. }
  2987. break;
  2988. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2989. if(sd->state.lr_flag != 2) {
  2990. sd->bonus.hp_vanish_rate += type2;
  2991. sd->bonus.hp_vanish_per += val;
  2992. }
  2993. break;
  2994. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2995. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2996. sd->bonus.get_zeny_rate = val;
  2997. sd->bonus.get_zeny_num = type2;
  2998. }
  2999. break;
  3000. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3001. if(sd->state.lr_flag != 2) {
  3002. sd->bonus.get_zeny_rate += val;
  3003. sd->bonus.get_zeny_num += type2;
  3004. }
  3005. break;
  3006. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3007. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3008. if(sd->state.lr_flag == 2)
  3009. break;
  3010. sd->weapon_coma_ele[type2] += val;
  3011. sd->special_state.bonus_coma = 1;
  3012. break;
  3013. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3014. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3015. if(sd->state.lr_flag == 2)
  3016. break;
  3017. sd->weapon_coma_race[type2] += val;
  3018. sd->special_state.bonus_coma = 1;
  3019. break;
  3020. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3021. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3022. if(sd->state.lr_flag == 2)
  3023. break;
  3024. sd->weapon_coma_class[type2] += val;
  3025. sd->special_state.bonus_coma = 1;
  3026. break;
  3027. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3028. if(sd->state.lr_flag != 2)
  3029. sd->weapon_atk[type2]+=val;
  3030. break;
  3031. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3032. if(sd->state.lr_flag != 2)
  3033. sd->weapon_damage_rate[type2]+=val;
  3034. break;
  3035. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3036. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3037. if(sd->state.lr_flag != 2)
  3038. sd->critaddrace[type2] += val*10;
  3039. break;
  3040. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3041. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3042. if(sd->state.lr_flag != 2)
  3043. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3044. break;
  3045. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3046. if(sd->state.lr_flag == 2)
  3047. break;
  3048. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3049. 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);
  3050. break;
  3051. }
  3052. pc_bonus_itembonus(sd->skillatk, type2, val);
  3053. break;
  3054. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3055. if(sd->state.lr_flag == 2)
  3056. break;
  3057. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3058. 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);
  3059. break;
  3060. }
  3061. pc_bonus_itembonus(sd->skillheal, type2, val);
  3062. break;
  3063. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3064. if(sd->state.lr_flag == 2)
  3065. break;
  3066. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3067. 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);
  3068. break;
  3069. }
  3070. pc_bonus_itembonus(sd->skillheal2, type2, val);
  3071. break;
  3072. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3073. if(sd->state.lr_flag == 2)
  3074. break;
  3075. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3076. 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);
  3077. break;
  3078. }
  3079. pc_bonus_itembonus(sd->skillblown, type2, val);
  3080. break;
  3081. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3082. if(sd->state.lr_flag != 2) {
  3083. sd->hp_loss.value = type2;
  3084. sd->hp_loss.rate = val;
  3085. }
  3086. break;
  3087. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3088. if(sd->state.lr_flag != 2) {
  3089. sd->hp_regen.value = type2;
  3090. sd->hp_regen.rate = val;
  3091. }
  3092. break;
  3093. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3094. if (sd->state.lr_flag != 2) {
  3095. sd->percent_hp_regen.value = type2;
  3096. sd->percent_hp_regen.rate = val;
  3097. }
  3098. break;
  3099. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3100. if (sd->state.lr_flag != 2) {
  3101. sd->percent_sp_regen.value = type2;
  3102. sd->percent_sp_regen.rate = val;
  3103. }
  3104. break;
  3105. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3106. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3107. if(sd->state.lr_flag != 2)
  3108. sd->right_weapon.addrace2[type2] += val;
  3109. else
  3110. sd->left_weapon.addrace2[type2] += val;
  3111. break;
  3112. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3113. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3114. if(sd->state.lr_flag != 2)
  3115. sd->subsize[type2]+=val;
  3116. break;
  3117. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3118. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3119. if(sd->state.lr_flag != 2)
  3120. sd->subrace2[type2]+=val;
  3121. break;
  3122. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3123. if(sd->state.lr_flag == 2)
  3124. break;
  3125. if (!itemdb_exists(type2)) {
  3126. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3127. break;
  3128. }
  3129. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3130. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3131. break;
  3132. }
  3133. pc_bonus_itembonus(sd->itemhealrate, type2, val);
  3134. break;
  3135. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3136. if (sd->state.lr_flag == 2)
  3137. break;
  3138. if (!type2 || !itemdb_group_exists(type2)) {
  3139. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3140. break;
  3141. }
  3142. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3143. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3144. break;
  3145. }
  3146. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val);
  3147. break;
  3148. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3149. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3150. if(sd->state.lr_flag != 2)
  3151. sd->expaddrace[type2]+=val;
  3152. break;
  3153. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3154. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3155. if(sd->state.lr_flag != 2)
  3156. sd->expaddclass[type2]+=val;
  3157. break;
  3158. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3159. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3160. if(sd->state.lr_flag != 2)
  3161. sd->sp_gain_race[type2]+=val;
  3162. break;
  3163. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3164. if (sd->state.lr_flag != 2)
  3165. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3166. break;
  3167. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3168. if (sd->state.lr_flag != 2)
  3169. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3170. break;
  3171. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3172. if(sd->state.lr_flag != 2) {
  3173. sd->sp_loss.value = type2;
  3174. sd->sp_loss.rate = val;
  3175. }
  3176. break;
  3177. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3178. if(sd->state.lr_flag != 2) {
  3179. sd->sp_regen.value = type2;
  3180. sd->sp_regen.rate = val;
  3181. }
  3182. break;
  3183. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3184. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3185. if(!sd->state.lr_flag) {
  3186. sd->right_weapon.hp_drain_race[type2] += val;
  3187. }
  3188. else if(sd->state.lr_flag == 1) {
  3189. sd->left_weapon.hp_drain_race[type2] += val;
  3190. }
  3191. break;
  3192. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3193. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3194. if(!sd->state.lr_flag) {
  3195. sd->right_weapon.sp_drain_race[type2] += val;
  3196. }
  3197. else if(sd->state.lr_flag == 1) {
  3198. sd->left_weapon.sp_drain_race[type2] += val;
  3199. }
  3200. break;
  3201. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3202. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3203. if(!sd->state.lr_flag) {
  3204. sd->right_weapon.hp_drain_class[type2] += val;
  3205. }
  3206. else if(sd->state.lr_flag == 1) {
  3207. sd->left_weapon.hp_drain_class[type2] += val;
  3208. }
  3209. break;
  3210. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3211. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3212. if(!sd->state.lr_flag) {
  3213. sd->right_weapon.sp_drain_class[type2] += val;
  3214. }
  3215. else if(sd->state.lr_flag == 1) {
  3216. sd->left_weapon.sp_drain_class[type2] += val;
  3217. }
  3218. break;
  3219. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3220. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3221. if(sd->state.lr_flag != 2)
  3222. sd->ignore_mdef_by_race[type2] += val;
  3223. break;
  3224. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3225. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3226. if(sd->state.lr_flag != 2)
  3227. sd->ignore_mdef_by_class[type2] += val;
  3228. break;
  3229. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3230. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3231. if(sd->state.lr_flag != 2)
  3232. sd->ignore_def_by_race[type2] += val;
  3233. break;
  3234. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3235. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3236. if (sd->state.lr_flag != 2)
  3237. sd->ignore_def_by_class[type2] += val;
  3238. break;
  3239. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3240. if(sd->state.lr_flag == 2)
  3241. break;
  3242. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3243. 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);
  3244. break;
  3245. }
  3246. pc_bonus_itembonus(sd->skillusesprate, type2, val);
  3247. break;
  3248. case SP_SKILL_DELAY:
  3249. if(sd->state.lr_flag == 2)
  3250. break;
  3251. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3252. 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);
  3253. break;
  3254. }
  3255. pc_bonus_itembonus(sd->skilldelay, type2, val);
  3256. break;
  3257. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3258. if(sd->state.lr_flag == 2)
  3259. break;
  3260. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3261. 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);
  3262. break;
  3263. }
  3264. pc_bonus_itembonus(sd->skillcooldown, type2, val);
  3265. break;
  3266. #ifdef RENEWAL_CAST
  3267. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3268. if(sd->state.lr_flag == 2)
  3269. break;
  3270. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3271. 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);
  3272. break;
  3273. }
  3274. pc_bonus_itembonus(sd->skillfixcast, type2, val);
  3275. break;
  3276. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3277. if(sd->state.lr_flag == 2)
  3278. break;
  3279. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3280. 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);
  3281. break;
  3282. }
  3283. pc_bonus_itembonus(sd->skillvarcast, type2, val);
  3284. break;
  3285. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3286. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3287. if(sd->state.lr_flag == 2)
  3288. break;
  3289. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3290. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3291. break;
  3292. }
  3293. pc_bonus_itembonus(sd->skillcastrate, type2, -val); // Send inversed value here
  3294. break;
  3295. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3296. if(sd->state.lr_flag == 2)
  3297. break;
  3298. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3299. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3300. break;
  3301. }
  3302. pc_bonus_itembonus(sd->skillcastrate, type2, -val); // Send inversed value here
  3303. break;
  3304. #else
  3305. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3306. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3307. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3308. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3309. break;
  3310. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3311. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3312. if(sd->state.lr_flag == 2)
  3313. break;
  3314. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3315. 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);
  3316. break;
  3317. }
  3318. pc_bonus_itembonus(sd->skillcastrate, type2, val);
  3319. break;
  3320. #endif
  3321. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3322. if(sd->state.lr_flag == 2)
  3323. break;
  3324. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3325. 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);
  3326. break;
  3327. }
  3328. pc_bonus_itembonus(sd->skillusesp, type2, val);
  3329. break;
  3330. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3331. if (sd->subskill.size() == MAX_PC_BONUS) {
  3332. 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);
  3333. break;
  3334. }
  3335. pc_bonus_itembonus(sd->subskill, type2, val);
  3336. break;
  3337. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3338. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3339. sd->subdefele[type2] += val;
  3340. break;
  3341. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3342. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3343. sd->coma_class[type2] += val;
  3344. sd->special_state.bonus_coma = 1;
  3345. break;
  3346. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3347. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3348. sd->coma_race[type2] += val;
  3349. sd->special_state.bonus_coma = 1;
  3350. break;
  3351. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3352. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3353. if(sd->state.lr_flag != 2)
  3354. sd->magic_addrace2[type2] += val;
  3355. break;
  3356. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3357. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3358. if (sd->state.lr_flag != 2)
  3359. sd->ignore_mdef_by_race2[type2] += val;
  3360. break;
  3361. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3362. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3363. if (sd->state.lr_flag != 2)
  3364. sd->dropaddrace[type2] += val;
  3365. break;
  3366. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3367. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3368. if (sd->state.lr_flag != 2)
  3369. sd->dropaddclass[type2] += val;
  3370. break;
  3371. default:
  3372. if (running_npc_stat_calc_event) {
  3373. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3374. }
  3375. else if (current_equip_combo_pos > 0) {
  3376. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3377. }
  3378. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3379. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3380. }
  3381. else {
  3382. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3383. }
  3384. break;
  3385. }
  3386. }
  3387. /**
  3388. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3389. * @param sd
  3390. * @param type Bonus type used by bBonusName
  3391. * @param type2
  3392. * @param val Value that usually for rate or fixed value
  3393. */
  3394. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3395. {
  3396. nullpo_retv(sd);
  3397. switch(type){
  3398. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3399. if(sd->state.lr_flag != 2)
  3400. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3401. break;
  3402. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3403. if(sd->state.lr_flag != 2)
  3404. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3405. break;
  3406. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3407. if(sd->state.lr_flag != 2)
  3408. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3409. break;
  3410. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3411. if(sd->state.lr_flag != 2)
  3412. {
  3413. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3414. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3415. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3416. }
  3417. break;
  3418. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3419. if(sd->state.lr_flag != 2)
  3420. {
  3421. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3422. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3423. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3424. }
  3425. break;
  3426. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3427. if (sd->state.lr_flag != 2)
  3428. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3429. break;
  3430. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3431. if (sd->state.lr_flag != 2)
  3432. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3433. break;
  3434. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3435. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3436. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3437. break;
  3438. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3439. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3440. if(sd->state.lr_flag != 2)
  3441. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3442. break;
  3443. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3444. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3445. if( sd->state.lr_flag != 2 )
  3446. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3447. break;
  3448. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3449. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3450. if (sd->state.lr_flag != 2)
  3451. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3452. break;
  3453. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3454. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3455. if (sd->state.lr_flag != 2)
  3456. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3457. break;
  3458. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3459. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3460. if(sd->state.lr_flag != 2) {
  3461. sd->sp_vanish_race[type2].rate += type3;
  3462. sd->sp_vanish_race[type2].per += val;
  3463. }
  3464. break;
  3465. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3466. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3467. if(sd->state.lr_flag != 2) {
  3468. sd->hp_vanish_race[type2].rate += type3;
  3469. sd->hp_vanish_race[type2].per += val;
  3470. }
  3471. break;
  3472. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3473. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3474. if (sd->state.lr_flag == 2)
  3475. break;
  3476. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3477. //if (type3 > sd->norecover_state_race[type2].rate) {
  3478. // sd->norecover_state_race[type2].rate = type3;
  3479. // sd->norecover_state_race[type2].tick = val;
  3480. // break;
  3481. //}
  3482. sd->norecover_state_race[type2].rate = type3;
  3483. sd->norecover_state_race[type2].tick = val;
  3484. break;
  3485. default:
  3486. if (running_npc_stat_calc_event) {
  3487. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3488. }
  3489. else if (current_equip_combo_pos > 0) {
  3490. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3491. }
  3492. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3493. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3494. }
  3495. else {
  3496. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3497. }
  3498. break;
  3499. }
  3500. }
  3501. /**
  3502. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3503. * @param sd
  3504. * @param type Bonus type used by bBonusName
  3505. * @param type2
  3506. * @param type3
  3507. * @param val Value that usually for rate or fixed value
  3508. */
  3509. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3510. {
  3511. nullpo_retv(sd);
  3512. switch(type){
  3513. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3514. if(sd->state.lr_flag != 2)
  3515. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3516. break;
  3517. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3518. if(sd->state.lr_flag != 2)
  3519. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3520. break;
  3521. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3522. if(sd->state.lr_flag != 2)
  3523. {
  3524. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3525. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3526. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3527. }
  3528. break;
  3529. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3530. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3531. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3532. break;
  3533. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3534. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3535. if (sd->state.lr_flag != 2)
  3536. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3537. break;
  3538. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3539. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3540. if( sd->state.lr_flag != 2 )
  3541. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3542. break;
  3543. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3544. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3545. if(sd->state.lr_flag == 2)
  3546. break;
  3547. sd->def_set_race[type2].rate = type3;
  3548. sd->def_set_race[type2].tick = type4;
  3549. sd->def_set_race[type2].value = val;
  3550. break;
  3551. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3552. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3553. if(sd->state.lr_flag == 2)
  3554. break;
  3555. sd->mdef_set_race[type2].rate = type3;
  3556. sd->mdef_set_race[type2].tick = type4;
  3557. sd->mdef_set_race[type2].value = val;
  3558. break;
  3559. default:
  3560. if (running_npc_stat_calc_event) {
  3561. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3562. }
  3563. else if (current_equip_combo_pos > 0) {
  3564. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3565. }
  3566. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3567. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3568. }
  3569. else {
  3570. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3571. }
  3572. break;
  3573. }
  3574. }
  3575. /**
  3576. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3577. * @param sd
  3578. * @param type Bonus type used by bBonusName
  3579. * @param type2
  3580. * @param type3
  3581. * @param type4
  3582. * @param val Value that usually for rate or fixed value
  3583. */
  3584. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3585. {
  3586. nullpo_retv(sd);
  3587. switch(type){
  3588. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3589. if(sd->state.lr_flag != 2)
  3590. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3591. break;
  3592. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3593. if(sd->state.lr_flag != 2)
  3594. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3595. break;
  3596. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3597. if(sd->state.lr_flag != 2)
  3598. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3599. break;
  3600. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3601. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3602. if( sd->state.lr_flag != 2 )
  3603. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3604. break;
  3605. default:
  3606. if (running_npc_stat_calc_event) {
  3607. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3608. }
  3609. else if (current_equip_combo_pos > 0) {
  3610. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3611. }
  3612. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3613. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3614. }
  3615. else {
  3616. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3617. }
  3618. break;
  3619. }
  3620. }
  3621. /*==========================================
  3622. * Grants a player a given skill. Flag values are:
  3623. * 0 - Grant permanent skill to be bound to skill tree
  3624. * 1 - Grant an item skill (temporary)
  3625. * 2 - Like 1, except the level granted can stack with previously learned level.
  3626. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3627. *------------------------------------------*/
  3628. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3629. uint16 idx = 0;
  3630. nullpo_ret(sd);
  3631. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3632. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3633. return false;
  3634. }
  3635. if (level > MAX_SKILL_LEVEL) {
  3636. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3637. return false;
  3638. }
  3639. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3640. 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);
  3641. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3642. }
  3643. switch (type) {
  3644. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3645. sd->status.skill[idx].id = skill_id;
  3646. sd->status.skill[idx].lv = level;
  3647. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3648. if (level == 0) { //Remove skill.
  3649. sd->status.skill[idx].id = 0;
  3650. clif_deleteskill(sd,skill_id);
  3651. } else
  3652. clif_addskill(sd,skill_id);
  3653. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3654. status_calc_pc(sd, SCO_NONE);
  3655. break;
  3656. case ADDSKILL_TEMP: //Item bonus skill.
  3657. if (sd->status.skill[idx].id != 0) {
  3658. if (sd->status.skill[idx].lv >= level)
  3659. return true;
  3660. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3661. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3662. } else {
  3663. sd->status.skill[idx].id = skill_id;
  3664. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3665. }
  3666. sd->status.skill[idx].lv = level;
  3667. break;
  3668. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3669. if (sd->status.skill[idx].id != 0) {
  3670. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3671. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3672. } else {
  3673. sd->status.skill[idx].id = skill_id;
  3674. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3675. }
  3676. sd->status.skill[idx].lv += level;
  3677. break;
  3678. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3679. sd->status.skill[idx].id = skill_id;
  3680. sd->status.skill[idx].lv = level;
  3681. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3682. if (level == 0) { //Remove skill.
  3683. sd->status.skill[idx].id = 0;
  3684. clif_deleteskill(sd,skill_id);
  3685. } else
  3686. clif_addskill(sd,skill_id);
  3687. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3688. status_calc_pc(sd, SCO_NONE);
  3689. break;
  3690. default:
  3691. return false;
  3692. }
  3693. return true;
  3694. }
  3695. /*==========================================
  3696. * Append a card to an item ?
  3697. *------------------------------------------*/
  3698. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3699. {
  3700. int i;
  3701. unsigned short nameid;
  3702. nullpo_ret(sd);
  3703. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3704. return 0; //Invalid item index.
  3705. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3706. return 0; //Invalid card index.
  3707. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3708. return 0; // target item missing
  3709. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3710. return 0; // target card missing
  3711. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3712. return 0; // only weapons and armor are allowed
  3713. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3714. return 0; // must be a card
  3715. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3716. return 0; // target must be identified
  3717. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3718. return 0; // card slots reserved for other purposes
  3719. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3720. return 0; // card cannot be compounded on this item type
  3721. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3722. return 0; // attempted to place shield card on left-hand weapon.
  3723. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3724. return 0; // item must be unequipped
  3725. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3726. if( i == sd->inventory_data[idx_equip]->slot )
  3727. return 0; // no free slots
  3728. // remember the card id to insert
  3729. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3730. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3731. {// failed
  3732. clif_insert_card(sd,idx_equip,idx_card,1);
  3733. }
  3734. else
  3735. {// success
  3736. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3737. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3738. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3739. clif_insert_card(sd,idx_equip,idx_card,0);
  3740. }
  3741. return 0;
  3742. }
  3743. /**
  3744. * Returns the count of unidentified items with the option to identify too.
  3745. * @param sd: Player data
  3746. * @param identify_item: Whether or not to identify any unidentified items
  3747. * @return Unidentified items count
  3748. */
  3749. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3750. {
  3751. int unidentified_count = 0;
  3752. for (int i = 0; i < MAX_INVENTORY; i++) {
  3753. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  3754. if (identify_item == true) {
  3755. sd->inventory.u.items_inventory[i].identify = 1;
  3756. clif_item_identified(sd,i,0);
  3757. }
  3758. unidentified_count++;
  3759. }
  3760. }
  3761. return unidentified_count;
  3762. }
  3763. //
  3764. // Items
  3765. //
  3766. /*==========================================
  3767. * Update buying value by skills
  3768. *------------------------------------------*/
  3769. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3770. {
  3771. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3772. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3773. rate1 = 5+skill*2-((skill==10)? 1:0);
  3774. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3775. rate2 = 5+skill*4;
  3776. if(rate1 < rate2) rate1 = rate2;
  3777. if(rate1)
  3778. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3779. if(val < battle_config.min_shop_buy)
  3780. val = battle_config.min_shop_buy;
  3781. return val;
  3782. }
  3783. /*==========================================
  3784. * Update selling value by skills
  3785. *------------------------------------------*/
  3786. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3787. {
  3788. int skill,val = orig_value,rate = 0;
  3789. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3790. rate = 5+skill*2-((skill==10)? 1:0);
  3791. if(rate)
  3792. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3793. if (val < battle_config.min_shop_sell)
  3794. val = battle_config.min_shop_sell;
  3795. return val;
  3796. }
  3797. /*==========================================
  3798. * Checking if we have enough place on inventory for new item
  3799. * Make sure to take 30k as limit (for client I guess)
  3800. * @param sd
  3801. * @param nameid
  3802. * @param amount
  3803. * @return e_chkitem_result
  3804. *------------------------------------------*/
  3805. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  3806. {
  3807. int i;
  3808. struct item_data* data;
  3809. nullpo_ret(sd);
  3810. if(amount > MAX_AMOUNT)
  3811. return CHKADDITEM_OVERAMOUNT;
  3812. data = itemdb_search(nameid);
  3813. if(!itemdb_isstackable2(data))
  3814. return CHKADDITEM_NEW;
  3815. if( data->stack.inventory && amount > data->stack.amount )
  3816. return CHKADDITEM_OVERAMOUNT;
  3817. for(i=0;i<MAX_INVENTORY;i++){
  3818. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3819. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  3820. 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 ) )
  3821. return CHKADDITEM_OVERAMOUNT;
  3822. return CHKADDITEM_EXIST;
  3823. }
  3824. }
  3825. return CHKADDITEM_NEW;
  3826. }
  3827. /*==========================================
  3828. * Return number of available place in inventory
  3829. * Each non stackable item will reduce place by 1
  3830. * @param sd
  3831. * @return Number of empty slots
  3832. *------------------------------------------*/
  3833. uint8 pc_inventoryblank(struct map_session_data *sd)
  3834. {
  3835. uint8 i, b;
  3836. nullpo_ret(sd);
  3837. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  3838. if(sd->inventory.u.items_inventory[i].nameid == 0)
  3839. b++;
  3840. }
  3841. return b;
  3842. }
  3843. /**
  3844. * Attempts to remove zeny from player
  3845. * @param sd: Player
  3846. * @param zeny: Zeny removed
  3847. * @param type: Log type
  3848. * @param tsd: (optional) From who to log (if null take sd)
  3849. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  3850. */
  3851. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3852. {
  3853. nullpo_retr(2,sd);
  3854. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3855. if( zeny < 0 )
  3856. {
  3857. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3858. return 1;
  3859. }
  3860. if( sd->status.zeny < zeny )
  3861. return 1; //Not enough.
  3862. sd->status.zeny -= zeny;
  3863. clif_updatestatus(sd,SP_ZENY);
  3864. if(!tsd) tsd = sd;
  3865. log_zeny(sd, type, tsd, -zeny);
  3866. if( zeny > 0 && sd->state.showzeny ) {
  3867. char output[255];
  3868. sprintf(output, "Removed %dz.", zeny);
  3869. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3870. }
  3871. return 0;
  3872. }
  3873. /**
  3874. * Attempts to give zeny to player
  3875. * @param sd: Player
  3876. * @param type: Log type
  3877. * @param tsd: (optional) From who to log (if null take sd)
  3878. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  3879. */
  3880. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3881. {
  3882. nullpo_retr(-1,sd);
  3883. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3884. if( zeny < 0 )
  3885. {
  3886. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3887. return 1;
  3888. }
  3889. if( zeny > MAX_ZENY - sd->status.zeny )
  3890. zeny = MAX_ZENY - sd->status.zeny;
  3891. sd->status.zeny += zeny;
  3892. clif_updatestatus(sd,SP_ZENY);
  3893. if(!tsd) tsd = sd;
  3894. log_zeny(sd, type, tsd, zeny);
  3895. if( zeny > 0 && sd->state.showzeny ) {
  3896. char output[255];
  3897. sprintf(output, "Gained %dz.", zeny);
  3898. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3899. }
  3900. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  3901. return 0;
  3902. }
  3903. /**
  3904. * Attempts to remove Cash Points from player
  3905. * @param sd: Player
  3906. * @param price: Points player has to pay
  3907. * @param points: Points player has
  3908. * @param type: Log type
  3909. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3910. */
  3911. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3912. {
  3913. int cash;
  3914. nullpo_retr(-1,sd);
  3915. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3916. if( price < 0 || points < 0 )
  3917. {
  3918. ShowError("pc_paycash: Paying negative points (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  3919. return -2;
  3920. }
  3921. if( points > price )
  3922. {
  3923. ShowWarning("pc_paycash: More kafra points provided than needed (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
  3924. points = price;
  3925. }
  3926. cash = price-points;
  3927. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3928. {
  3929. 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);
  3930. return -1;
  3931. }
  3932. if( cash ){
  3933. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  3934. sd->cashPoints -= cash;
  3935. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3936. }
  3937. if( points ){
  3938. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  3939. sd->kafraPoints -= points;
  3940. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3941. }
  3942. if( battle_config.cashshop_show_points )
  3943. {
  3944. char output[CHAT_SIZE_MAX];
  3945. 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.
  3946. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3947. }
  3948. return cash+points;
  3949. }
  3950. /**
  3951. * Attempts to give Cash Points to player
  3952. * @param sd: Player
  3953. * @param cash: Cash player gets
  3954. * @param points: Points player has
  3955. * @param type: Log type
  3956. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  3957. */
  3958. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  3959. {
  3960. char output[CHAT_SIZE_MAX];
  3961. nullpo_retr(-1,sd);
  3962. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3963. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3964. if( cash > 0 )
  3965. {
  3966. if( cash > MAX_ZENY-sd->cashPoints )
  3967. {
  3968. 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);
  3969. cash = MAX_ZENY-sd->cashPoints;
  3970. }
  3971. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  3972. sd->cashPoints += cash;
  3973. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3974. if( battle_config.cashshop_show_points )
  3975. {
  3976. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3977. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  3978. }
  3979. return cash;
  3980. }
  3981. else if( cash < 0 )
  3982. {
  3983. ShowError("pc_getcash: Obtaining negative cash points (cash=%d, account_id=%d, char_id=%d).\n", cash, sd->status.account_id, sd->status.char_id);
  3984. return -1;
  3985. }
  3986. if( points > 0 )
  3987. {
  3988. if( points > MAX_ZENY-sd->kafraPoints )
  3989. {
  3990. 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);
  3991. points = MAX_ZENY-sd->kafraPoints;
  3992. }
  3993. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  3994. sd->kafraPoints += points;
  3995. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3996. if( battle_config.cashshop_show_points )
  3997. {
  3998. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3999. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4000. }
  4001. return points;
  4002. }
  4003. else if( points < 0 )
  4004. {
  4005. ShowError("pc_getcash: Obtaining negative kafra points (points=%d, account_id=%d, char_id=%d).\n", points, sd->status.account_id, sd->status.char_id);
  4006. return -1;
  4007. }
  4008. return -2; //shouldn't happen but just in case
  4009. }
  4010. /**
  4011. * Searching a specified itemid in inventory and return his stored index
  4012. * @param sd Player
  4013. * @param nameid Find this Item!
  4014. * @return Stored index in inventory, or -1 if not found.
  4015. **/
  4016. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4017. short i;
  4018. nullpo_retr(-1, sd);
  4019. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4020. return ( i < MAX_INVENTORY ) ? i : -1;
  4021. }
  4022. /** Attempt to add a new item to player inventory
  4023. * @param sd
  4024. * @param item_data
  4025. * @param amount
  4026. * @param log_type
  4027. * @return
  4028. * 0 = success
  4029. * 1 = invalid itemid not found or negative amount
  4030. * 2 = overweight
  4031. * 3 = ?
  4032. * 4 = no free place found
  4033. * 5 = max amount reached
  4034. * 6 = ?
  4035. * 7 = stack limitation
  4036. */
  4037. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4038. struct item_data *id;
  4039. int16 i;
  4040. unsigned int w;
  4041. nullpo_retr(1, sd);
  4042. nullpo_retr(1, item);
  4043. if( item->nameid == 0 || amount <= 0 )
  4044. return ADDITEM_INVALID;
  4045. if( amount > MAX_AMOUNT )
  4046. return ADDITEM_OVERAMOUNT;
  4047. id = itemdb_search(item->nameid);
  4048. if( id->stack.inventory && amount > id->stack.amount )
  4049. {// item stack limitation
  4050. return ADDITEM_STACKLIMIT;
  4051. }
  4052. w = id->weight*amount;
  4053. if(sd->weight + w > sd->max_weight)
  4054. return ADDITEM_OVERWEIGHT;
  4055. i = MAX_INVENTORY;
  4056. if (id->flag.guid && !item->unique_id)
  4057. item->unique_id = pc_generate_unique_id(sd);
  4058. // Stackable | Non Rental
  4059. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4060. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4061. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4062. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4063. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4064. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4065. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4066. 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 ) )
  4067. return ADDITEM_OVERAMOUNT;
  4068. sd->inventory.u.items_inventory[i].amount += amount;
  4069. clif_additem(sd,i,amount,0);
  4070. break;
  4071. }
  4072. }
  4073. }
  4074. if (i >= MAX_INVENTORY) {
  4075. i = pc_search_inventory(sd,0);
  4076. if( i < 0 )
  4077. return ADDITEM_OVERITEM;
  4078. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4079. // clear equip and favorite fields first, just in case
  4080. if( item->equip )
  4081. sd->inventory.u.items_inventory[i].equip = 0;
  4082. if( item->favorite )
  4083. sd->inventory.u.items_inventory[i].favorite = 0;
  4084. sd->inventory.u.items_inventory[i].amount = amount;
  4085. sd->inventory_data[i] = id;
  4086. sd->last_addeditem_index = i;
  4087. if (!itemdb_isstackable2(id) || id->flag.guid)
  4088. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4089. clif_additem(sd,i,amount,0);
  4090. }
  4091. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4092. sd->weight += w;
  4093. clif_updatestatus(sd,SP_WEIGHT);
  4094. //Auto-equip
  4095. if(id->flag.autoequip)
  4096. pc_equipitem(sd, i, id->equip);
  4097. /* rental item check */
  4098. if( item->expire_time ) {
  4099. if( time(NULL) > item->expire_time ) {
  4100. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4101. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4102. } else {
  4103. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4104. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4105. pc_inventory_rental_add(sd, seconds);
  4106. }
  4107. }
  4108. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4109. return ADDITEM_SUCCESS;
  4110. }
  4111. /*==========================================
  4112. * Remove an item at index n from inventory by amount.
  4113. * @param sd
  4114. * @param n Item index in inventory
  4115. * @param amount
  4116. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4117. * @param reason Delete reason
  4118. * @param log_type e_log_pick_type
  4119. * @return 1 - invalid itemid or negative amount; 0 - Success
  4120. *------------------------------------------*/
  4121. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4122. {
  4123. nullpo_retr(1, sd);
  4124. 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)
  4125. return 1;
  4126. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4127. sd->inventory.u.items_inventory[n].amount -= amount;
  4128. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4129. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4130. if(sd->inventory.u.items_inventory[n].equip)
  4131. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4132. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4133. sd->inventory_data[n] = NULL;
  4134. }
  4135. if(!(type&1))
  4136. clif_delitem(sd,n,amount,reason);
  4137. if(!(type&2))
  4138. clif_updatestatus(sd,SP_WEIGHT);
  4139. return 0;
  4140. }
  4141. /*==========================================
  4142. * Attempt to drop an item.
  4143. * @param sd
  4144. * @param n Item index in inventory
  4145. * @param amount Amount of item
  4146. * @return False = fail; True = success
  4147. *------------------------------------------*/
  4148. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4149. {
  4150. nullpo_retr(1, sd);
  4151. if(n < 0 || n >= MAX_INVENTORY)
  4152. return false;
  4153. if(amount <= 0)
  4154. return false;
  4155. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4156. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4157. sd->inventory.u.items_inventory[n].amount < amount ||
  4158. sd->state.trading || sd->state.vending ||
  4159. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4160. )
  4161. return false;
  4162. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4163. {
  4164. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4165. return false; //Can't drop items in nodrop mapflag maps.
  4166. }
  4167. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4168. {
  4169. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4170. return false;
  4171. }
  4172. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4173. return false;
  4174. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4175. clif_dropitem(sd, n, amount);
  4176. return true;
  4177. }
  4178. /*==========================================
  4179. * Attempt to pick up an item.
  4180. * @param sd
  4181. * @param fitem Item that will be picked
  4182. * @return False = fail; True = success
  4183. *------------------------------------------*/
  4184. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4185. {
  4186. int flag = 0;
  4187. unsigned int tick = gettick();
  4188. struct party_data *p = NULL;
  4189. nullpo_ret(sd);
  4190. nullpo_ret(fitem);
  4191. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4192. return false; // Distance is too far
  4193. if (sd->sc.cant.pickup)
  4194. return false;
  4195. if (sd->status.party_id)
  4196. p = party_search(sd->status.party_id);
  4197. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4198. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4199. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4200. if (!(p && p->party.item&1 &&
  4201. first_sd && first_sd->status.party_id == sd->status.party_id
  4202. ))
  4203. return false;
  4204. }
  4205. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4206. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4207. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4208. if (!(p && p->party.item&1 &&
  4209. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4210. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4211. ))
  4212. return false;
  4213. }
  4214. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4215. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4216. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4217. if(!(p && p->party.item&1 &&
  4218. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4219. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4220. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4221. ))
  4222. return false;
  4223. }
  4224. }
  4225. }
  4226. }
  4227. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4228. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4229. clif_additem(sd,0,0,flag);
  4230. return true;
  4231. }
  4232. //Display pickup animation.
  4233. pc_stop_attack(sd);
  4234. clif_takeitem(&sd->bl,&fitem->bl);
  4235. if (fitem->mob_id &&
  4236. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4237. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4238. )
  4239. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4240. map_clearflooritem(&fitem->bl);
  4241. return true;
  4242. }
  4243. /*==========================================
  4244. * Check if item is usable.
  4245. * Return:
  4246. * false = no
  4247. * true = yes
  4248. *------------------------------------------*/
  4249. bool pc_isUseitem(struct map_session_data *sd,int n)
  4250. {
  4251. struct item_data *item;
  4252. unsigned short nameid;
  4253. nullpo_ret(sd);
  4254. item = sd->inventory_data[n];
  4255. nameid = sd->inventory.u.items_inventory[n].nameid;
  4256. if( item == NULL )
  4257. return false;
  4258. //Not consumable item
  4259. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4260. return false;
  4261. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4262. return true;
  4263. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4264. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4265. return false;
  4266. //Prevent mass item usage. [Skotlex]
  4267. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4268. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4269. )
  4270. return false;
  4271. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4272. clif_msg(sd,ITEM_NOUSE_SITTING);
  4273. return false; // You cannot use this item while sitting.
  4274. }
  4275. if (sd->state.storage_flag && item->type != IT_CASH) {
  4276. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4277. return false; // You cannot use this item while storage is open.
  4278. }
  4279. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4280. return false;
  4281. switch( nameid ) {
  4282. case ITEMID_WING_OF_FLY:
  4283. case ITEMID_GIANT_FLY_WING:
  4284. case ITEMID_N_FLY_WING:
  4285. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4286. clif_skill_teleportmessage(sd,0);
  4287. return false;
  4288. }
  4289. if (nameid == ITEMID_GIANT_FLY_WING) {
  4290. struct party_data *pd = party_search(sd->status.party_id);
  4291. if (pd) {
  4292. int i;
  4293. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4294. if (i == MAX_PARTY) { // User is not party leader
  4295. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4296. break;
  4297. }
  4298. 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));
  4299. if (i == MAX_PARTY) { // No party members found on same map
  4300. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4301. break;
  4302. }
  4303. } else {
  4304. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4305. break;
  4306. }
  4307. }
  4308. // Fall through
  4309. case ITEMID_WING_OF_BUTTERFLY:
  4310. case ITEMID_N_BUTTERFLY_WING:
  4311. case ITEMID_DUN_TELE_SCROLL1:
  4312. case ITEMID_DUN_TELE_SCROLL2:
  4313. case ITEMID_DUN_TELE_SCROLL3:
  4314. case ITEMID_WOB_RUNE:
  4315. case ITEMID_WOB_SCHWALTZ:
  4316. case ITEMID_WOB_RACHEL:
  4317. case ITEMID_WOB_LOCAL:
  4318. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4319. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4320. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4321. return false;
  4322. }
  4323. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4324. return false;
  4325. break;
  4326. case ITEMID_MERCENARY_RED_POTION:
  4327. case ITEMID_MERCENARY_BLUE_POTION:
  4328. case ITEMID_M_CENTER_POTION:
  4329. case ITEMID_M_AWAKENING_POTION:
  4330. case ITEMID_M_BERSERK_POTION:
  4331. if( sd->md == NULL || sd->md->db == NULL )
  4332. return false;
  4333. if( sd->md->sc.data[SC_BERSERK] )
  4334. return false;
  4335. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4336. return false;
  4337. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4338. return false;
  4339. break;
  4340. case ITEMID_NEURALIZER:
  4341. if( !mapdata->flag[MF_RESET] )
  4342. return false;
  4343. break;
  4344. }
  4345. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4346. return false; // Mercenary Scrolls
  4347. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4348. if( pc_is90overweight(sd) ) {
  4349. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4350. return false;
  4351. }
  4352. if( !pc_inventoryblank(sd) ) {
  4353. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4354. return false;
  4355. }
  4356. }
  4357. //Gender check
  4358. if(item->sex != 2 && sd->status.sex != item->sex)
  4359. return false;
  4360. //Required level check
  4361. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4362. return false;
  4363. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4364. return false;
  4365. //Not equipable by class. [Skotlex]
  4366. if (!(
  4367. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4368. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4369. ))
  4370. return false;
  4371. if (sd->sc.count && (
  4372. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4373. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4374. sd->sc.data[SC_TRICKDEAD] ||
  4375. sd->sc.data[SC_HIDING] ||
  4376. sd->sc.data[SC__SHADOWFORM] ||
  4377. sd->sc.data[SC__INVISIBILITY] ||
  4378. sd->sc.data[SC__MANHOLE] ||
  4379. sd->sc.data[SC_DEEPSLEEP] ||
  4380. sd->sc.data[SC_CRYSTALIZE] ||
  4381. sd->sc.data[SC_KAGEHUMI] ||
  4382. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4383. sd->sc.data[SC_KINGS_GRACE] ||
  4384. sd->sc.data[SC_SUHIDE]))
  4385. return false;
  4386. if (!pc_isItemClass(sd,item))
  4387. return false;
  4388. //Dead Branch items
  4389. if( item->flag.dead_branch )
  4390. log_branch(sd);
  4391. return true;
  4392. }
  4393. /*==========================================
  4394. * Last checks to use an item.
  4395. * Return:
  4396. * 0 = fail
  4397. * 1 = success
  4398. *------------------------------------------*/
  4399. int pc_useitem(struct map_session_data *sd,int n)
  4400. {
  4401. unsigned int tick = gettick();
  4402. int amount;
  4403. unsigned short nameid;
  4404. struct script_code *script;
  4405. struct item item;
  4406. struct item_data *id;
  4407. nullpo_ret(sd);
  4408. if (sd->npc_id) {
  4409. if (sd->progressbar.npc_id) {
  4410. clif_progressbar_abort(sd);
  4411. return 0; // First item use attempt cancels the progress bar
  4412. }
  4413. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4414. #ifdef RENEWAL
  4415. clif_msg( sd, WORK_IN_PROGRESS );
  4416. #endif
  4417. return 0;
  4418. }
  4419. }
  4420. item = sd->inventory.u.items_inventory[n];
  4421. id = sd->inventory_data[n];
  4422. if (item.nameid == 0 || item.amount <= 0)
  4423. return 0;
  4424. if( !pc_isUseitem(sd,n) )
  4425. return 0;
  4426. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4427. nameid = id->nameid;
  4428. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4429. return 0;
  4430. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4431. if( id->flag.delay_consume ) {
  4432. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4433. return 0;
  4434. else if( pc_issit(sd) )
  4435. return 0;
  4436. }
  4437. //Since most delay-consume items involve using a "skill-type" target cursor,
  4438. //perform a skill-use check before going through. [Skotlex]
  4439. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4440. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4441. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4442. return 0;
  4443. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4444. return 0;
  4445. /* on restricted maps the item is consumed but the effect is not used */
  4446. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4447. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4448. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4449. clif_useitemack(sd,n,item.amount-1,true);
  4450. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4451. }
  4452. return 0;/* regardless, effect is not run */
  4453. }
  4454. sd->itemid = item.nameid;
  4455. sd->itemindex = n;
  4456. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4457. sd->catch_target_class = PET_CATCH_FAIL;
  4458. amount = item.amount;
  4459. script = id->script;
  4460. //Check if the item is to be consumed immediately [Skotlex]
  4461. if (id->flag.delay_consume)
  4462. clif_useitemack(sd, n, amount, true);
  4463. else
  4464. {
  4465. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4466. {
  4467. clif_useitemack(sd, n, amount - 1, true);
  4468. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4469. }
  4470. else
  4471. clif_useitemack(sd, n, 0, false);
  4472. }
  4473. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4474. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4475. //Update item use time.
  4476. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4477. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4478. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4479. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4480. potion_flag = 0;
  4481. return 1;
  4482. }
  4483. /**
  4484. * Add item on cart for given index.
  4485. * @param sd
  4486. * @param item
  4487. * @param amount
  4488. * @param log_type
  4489. * @return 0 = success; 1 = fail; 2 = no slot
  4490. */
  4491. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4492. {
  4493. struct item_data *data;
  4494. int i,w;
  4495. nullpo_retr(1, sd);
  4496. nullpo_retr(1, item);
  4497. if(item->nameid == 0 || amount <= 0)
  4498. return 1;
  4499. data = itemdb_search(item->nameid);
  4500. if( data->stack.cart && amount > data->stack.amount )
  4501. {// item stack limitation
  4502. return 1;
  4503. }
  4504. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4505. { // Check item trade restrictions [Skotlex]
  4506. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4507. return 1;
  4508. }
  4509. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4510. return 1;
  4511. i = MAX_CART;
  4512. if( itemdb_isstackable2(data) && !item->expire_time )
  4513. {
  4514. for (i = 0; i < MAX_CART; i++) {
  4515. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4516. && sd->cart.u.items_cart[i].bound == item->bound
  4517. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4518. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4519. )
  4520. break;
  4521. }
  4522. }
  4523. if( i < MAX_CART )
  4524. {// item already in cart, stack it
  4525. 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 ) )
  4526. return 2; // no slot
  4527. sd->cart.u.items_cart[i].amount += amount;
  4528. clif_cart_additem(sd,i,amount,0);
  4529. }
  4530. else
  4531. {// item not stackable or not present, add it
  4532. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4533. if( i == MAX_CART )
  4534. return 2; // no slot
  4535. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4536. sd->cart.u.items_cart[i].id = 0;
  4537. sd->cart.u.items_cart[i].amount = amount;
  4538. sd->cart_num++;
  4539. clif_cart_additem(sd,i,amount,0);
  4540. }
  4541. sd->cart.u.items_cart[i].favorite = 0; // clear
  4542. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4543. sd->cart_weight += w;
  4544. clif_updatestatus(sd,SP_CARTINFO);
  4545. return 0;
  4546. }
  4547. /*==========================================
  4548. * Delete item on cart for given index.
  4549. *------------------------------------------*/
  4550. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4551. {
  4552. nullpo_retv(sd);
  4553. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4554. sd->cart.u.items_cart[n].amount < amount)
  4555. return;
  4556. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4557. sd->cart.u.items_cart[n].amount -= amount;
  4558. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4559. if(sd->cart.u.items_cart[n].amount <= 0) {
  4560. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4561. sd->cart_num--;
  4562. }
  4563. if(!type) {
  4564. clif_cart_delitem(sd,n,amount);
  4565. clif_updatestatus(sd,SP_CARTINFO);
  4566. }
  4567. }
  4568. /*==========================================
  4569. * Transfer item from inventory to cart.
  4570. *------------------------------------------*/
  4571. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4572. {
  4573. struct item *item_data;
  4574. char flag;
  4575. nullpo_retv(sd);
  4576. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4577. return;
  4578. item_data = &sd->inventory.u.items_inventory[idx];
  4579. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4580. return;
  4581. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4582. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4583. else {
  4584. clif_dropitem(sd,idx,0);
  4585. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4586. }
  4587. }
  4588. /*==========================================
  4589. * Get number of item in cart.
  4590. * Return:
  4591. -1 = itemid not found or no amount found
  4592. x = remaining itemid on cart after get
  4593. *------------------------------------------*/
  4594. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4595. {
  4596. struct item* item_data;
  4597. nullpo_retr(-1, sd);
  4598. item_data = &sd->cart.u.items_cart[idx];
  4599. if( item_data->nameid == 0 || item_data->amount == 0 )
  4600. return -1;
  4601. return item_data->amount - amount;
  4602. }
  4603. /*==========================================
  4604. * Retrieve an item at index idx from cart.
  4605. *------------------------------------------*/
  4606. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4607. {
  4608. struct item *item_data;
  4609. unsigned char flag = 0;
  4610. nullpo_retv(sd);
  4611. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4612. return;
  4613. item_data=&sd->cart.u.items_cart[idx];
  4614. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4615. return;
  4616. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4617. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4618. else {
  4619. clif_dropitem(sd,idx,0);
  4620. clif_additem(sd,0,0,flag);
  4621. }
  4622. }
  4623. /*==========================================
  4624. * Bound Item Check
  4625. * Type:
  4626. * 1 Account Bound
  4627. * 2 Guild Bound
  4628. * 3 Party Bound
  4629. * 4 Character Bound
  4630. *------------------------------------------*/
  4631. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4632. {
  4633. int i = 0, j = 0;
  4634. for(i = 0; i < MAX_INVENTORY; i++) {
  4635. 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) {
  4636. idxlist[j] = i;
  4637. j++;
  4638. }
  4639. }
  4640. return j;
  4641. }
  4642. /*==========================================
  4643. * Display item stolen msg to player sd
  4644. *------------------------------------------*/
  4645. int pc_show_steal(struct block_list *bl,va_list ap)
  4646. {
  4647. struct map_session_data *sd;
  4648. int itemid;
  4649. struct item_data *item=NULL;
  4650. char output[100];
  4651. sd=va_arg(ap,struct map_session_data *);
  4652. itemid=va_arg(ap,int);
  4653. if((item=itemdb_exists(itemid))==NULL)
  4654. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4655. else
  4656. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4657. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4658. return 0;
  4659. }
  4660. /**
  4661. * Steal an item from bl (mob).
  4662. * @param sd: Player data
  4663. * @param bl: Object to steal from
  4664. * @param skill_lv: Level of skill used
  4665. * @return True on success or false otherwise
  4666. */
  4667. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4668. {
  4669. int i,itemid;
  4670. double rate;
  4671. unsigned char flag = 0;
  4672. struct status_data *sd_status, *md_status;
  4673. struct mob_data *md;
  4674. struct item tmp_item;
  4675. if(!sd || !bl || bl->type!=BL_MOB)
  4676. return false;
  4677. md = (TBL_MOB *)bl;
  4678. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4679. return false;
  4680. sd_status= status_get_status_data(&sd->bl);
  4681. md_status= status_get_status_data(bl);
  4682. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4683. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4684. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4685. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4686. ) { //Can't steal from
  4687. md->state.steal_flag = UCHAR_MAX;
  4688. return false;
  4689. }
  4690. // base skill success chance (percentual)
  4691. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4692. rate += sd->bonus.add_steal_rate;
  4693. if( rate < 1
  4694. #ifdef RENEWAL
  4695. || rnd()%100 >= rate
  4696. #endif
  4697. )
  4698. return false;
  4699. // Try dropping one item, in the order from first to last possible slot.
  4700. // Droprate is affected by the skill success rate.
  4701. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4702. 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
  4703. #ifndef RENEWAL
  4704. * rate/100.
  4705. #endif
  4706. )
  4707. break;
  4708. if( i == MAX_STEAL_DROP )
  4709. return false;
  4710. itemid = md->db->dropitem[i].nameid;
  4711. memset(&tmp_item,0,sizeof(tmp_item));
  4712. tmp_item.nameid = itemid;
  4713. tmp_item.amount = 1;
  4714. tmp_item.identify = itemdb_isidentified(itemid);
  4715. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4716. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4717. //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?
  4718. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4719. if(flag) { //Failed to steal due to overweight
  4720. clif_additem(sd,0,0,flag);
  4721. return false;
  4722. }
  4723. if(battle_config.show_steal_in_same_party)
  4724. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4725. //Logs items, Stolen from mobs [Lupus]
  4726. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4727. //A Rare Steal Global Announce by Lupus
  4728. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4729. struct item_data *i_data;
  4730. char message[128];
  4731. i_data = itemdb_search(itemid);
  4732. 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);
  4733. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4734. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4735. }
  4736. return true;
  4737. }
  4738. /*==========================================
  4739. * Stole zeny from bl (mob)
  4740. * return
  4741. * 0 = fail
  4742. * 1 = success
  4743. *------------------------------------------*/
  4744. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4745. {
  4746. int rate, target_lv;
  4747. struct mob_data *md;
  4748. if(!sd || !target || target->type != BL_MOB)
  4749. return 0;
  4750. md = (TBL_MOB*)target;
  4751. target_lv = status_get_lv(target);
  4752. 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)
  4753. return 0;
  4754. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4755. if(rnd()%1000 < rate)
  4756. {
  4757. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4758. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4759. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4760. md->state.steal_coin_flag = 1;
  4761. return 1;
  4762. }
  4763. return 0;
  4764. }
  4765. /*==========================================
  4766. * Set's a player position.
  4767. * @param sd
  4768. * @param mapindex
  4769. * @param x
  4770. * @param y
  4771. * @param clrtype
  4772. * @return SETPOS_OK Success
  4773. * SETPOS_MAPINDEX Invalid map index
  4774. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4775. * SETPOS_AUTOTRADE Player is in autotrade state
  4776. *------------------------------------------*/
  4777. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4778. {
  4779. nullpo_retr(SETPOS_OK,sd);
  4780. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4781. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4782. return SETPOS_MAPINDEX;
  4783. }
  4784. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4785. return SETPOS_AUTOTRADE;
  4786. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4787. pc_setstand(sd, true);
  4788. pc_setrestartvalue(sd,1);
  4789. }
  4790. int16 m = map_mapindex2mapid(mapindex);
  4791. struct map_data *mapdata = map_getmapdata(m);
  4792. sd->state.changemap = (sd->mapindex != mapindex);
  4793. sd->state.warping = 1;
  4794. sd->state.workinprogress = WIP_DISABLE_NONE;
  4795. if( sd->state.changemap ) { // Misc map-changing settings
  4796. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  4797. if (curr_map_instance_id != new_map_instance_id) {
  4798. if (curr_map_instance_id) // Update instance timer for the map on leave
  4799. instance_delusers(curr_map_instance_id);
  4800. if (new_map_instance_id) // Update instance timer for the map on enter
  4801. instance_addusers(new_map_instance_id);
  4802. }
  4803. sd->state.pmap = sd->bl.m;
  4804. if (sd->sc.count) { // Cancel some map related stuff.
  4805. if (sd->sc.data[SC_JAILED])
  4806. return SETPOS_MAPINDEX; //You may not get out!
  4807. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4808. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4809. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4810. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4811. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4812. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4813. if (sd->sc.data[SC_KNOWLEDGE]) {
  4814. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4815. if (sce->timer != INVALID_TIMER)
  4816. delete_timer(sce->timer, status_change_timer);
  4817. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4818. }
  4819. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4820. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4821. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4822. }
  4823. for(int i = 0; i < EQI_MAX; i++ ) {
  4824. if( sd->equip_index[i] >= 0 )
  4825. if( pc_isequip(sd,sd->equip_index[i]) )
  4826. pc_unequipitem(sd,sd->equip_index[i],2);
  4827. }
  4828. if (battle_config.clear_unit_onwarp&BL_PC)
  4829. skill_clear_unitgroup(&sd->bl);
  4830. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4831. guild_send_dot_remove(sd);
  4832. bg_send_dot_remove(sd);
  4833. if (sd->regen.state.gc)
  4834. sd->regen.state.gc = 0;
  4835. // make sure vending is allowed here
  4836. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  4837. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4838. vending_closevending(sd);
  4839. }
  4840. channel_pcquit(sd,4); //quit map chan
  4841. }
  4842. if( m < 0 )
  4843. {
  4844. uint32 ip;
  4845. uint16 port;
  4846. struct script_state *st;
  4847. //if can't find any map-servers, just abort setting position.
  4848. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4849. return SETPOS_NO_MAPSERVER;
  4850. if (sd->npc_id){
  4851. npc_event_dequeue(sd,false);
  4852. st = sd->st;
  4853. }else{
  4854. st = nullptr;
  4855. }
  4856. npc_script_event(sd, NPCE_LOGOUT);
  4857. //remove from map, THEN change x/y coordinates
  4858. unit_remove_map_pc(sd,clrtype);
  4859. sd->mapindex = mapindex;
  4860. sd->bl.x=x;
  4861. sd->bl.y=y;
  4862. pc_clean_skilltree(sd);
  4863. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  4864. chrif_changemapserver(sd, ip, (short)port);
  4865. //Free session data from this map server [Kevin]
  4866. unit_free_pc(sd);
  4867. if( st ){
  4868. // Has to be done here, because otherwise unit_free_pc will free the stack already
  4869. st->state = END;
  4870. }
  4871. return SETPOS_OK;
  4872. }
  4873. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  4874. {
  4875. 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);
  4876. x = y = 0; // make it random
  4877. }
  4878. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4879. int c=0;
  4880. do {
  4881. x = rnd()%(mapdata->xs-2)+1;
  4882. y = rnd()%(mapdata->ys-2)+1;
  4883. c++;
  4884. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  4885. 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));
  4886. return SETPOS_OK; //preventing warp
  4887. //break; //allow warp anyway
  4888. }
  4889. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4890. }
  4891. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4892. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4893. vending_closevending(sd);
  4894. }
  4895. if(sd->bl.prev != NULL){
  4896. unit_remove_map_pc(sd,clrtype);
  4897. clif_changemap(sd,m,x,y); // [MouseJstr]
  4898. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4899. sd->state.rewarp = 1;
  4900. sd->mapindex = mapindex;
  4901. sd->bl.m = m;
  4902. sd->bl.x = sd->ud.to_x = x;
  4903. sd->bl.y = sd->ud.to_y = y;
  4904. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  4905. { // Increased guild castle regen [Valaris]
  4906. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4907. if(gc && gc->guild_id == sd->status.guild_id)
  4908. sd->regen.state.gc = 1;
  4909. }
  4910. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4911. {
  4912. sd->pd->bl.m = m;
  4913. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4914. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4915. sd->pd->ud.dir = sd->ud.dir;
  4916. }
  4917. if( hom_is_active(sd->hd) )
  4918. {
  4919. sd->hd->bl.m = m;
  4920. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4921. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4922. sd->hd->ud.dir = sd->ud.dir;
  4923. }
  4924. if( sd->md )
  4925. {
  4926. sd->md->bl.m = m;
  4927. sd->md->bl.x = sd->md->ud.to_x = x;
  4928. sd->md->bl.y = sd->md->ud.to_y = y;
  4929. sd->md->ud.dir = sd->ud.dir;
  4930. }
  4931. if( sd->ed ) {
  4932. sd->ed->bl.m = m;
  4933. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4934. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4935. sd->ed->ud.dir = sd->ud.dir;
  4936. }
  4937. pc_cell_basilica(sd);
  4938. //check if we gonna be rewarped [lighta]
  4939. if(npc_check_areanpc(1,m,x,y,0)){
  4940. sd->count_rewarp++;
  4941. }
  4942. else
  4943. sd->count_rewarp = 0;
  4944. return SETPOS_OK;
  4945. }
  4946. /*==========================================
  4947. * Warp player sd to random location on current map.
  4948. * May fail if no walkable cell found (1000 attempts).
  4949. * Return:
  4950. * 0 = Success
  4951. * 1,2,3 = Fail
  4952. *------------------------------------------*/
  4953. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4954. {
  4955. int x,y,i=0;
  4956. nullpo_ret(sd);
  4957. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4958. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  4959. return 3;
  4960. do {
  4961. x = rnd()%(mapdata->xs-2)+1;
  4962. y = rnd()%(mapdata->ys-2)+1;
  4963. } 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);
  4964. if (i < 1000)
  4965. return pc_setpos(sd,mapdata->index,x,y,type);
  4966. return 3;
  4967. }
  4968. /*==========================================
  4969. * Records a memo point at sd's current position
  4970. * pos - entry to replace, (-1: shift oldest entry out)
  4971. *------------------------------------------*/
  4972. bool pc_memo(struct map_session_data* sd, int pos)
  4973. {
  4974. int skill;
  4975. nullpo_ret(sd);
  4976. // check mapflags
  4977. 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) ) {
  4978. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4979. return false;
  4980. }
  4981. // check inputs
  4982. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4983. return false; // invalid input
  4984. // check required skill level
  4985. skill = pc_checkskill(sd, AL_WARP);
  4986. if( skill < 1 ) {
  4987. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4988. return false;
  4989. }
  4990. if( skill < 2 || skill - 2 < pos ) {
  4991. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4992. return false;
  4993. }
  4994. if( pos == -1 )
  4995. {
  4996. uint8 i;
  4997. // prevent memo-ing the same map multiple times
  4998. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4999. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5000. pos = 0;
  5001. }
  5002. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5003. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5004. return false;
  5005. }
  5006. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5007. sd->status.memo_point[pos].x = sd->bl.x;
  5008. sd->status.memo_point[pos].y = sd->bl.y;
  5009. clif_skill_memomessage(sd, 0);
  5010. return true;
  5011. }
  5012. //
  5013. // Skills
  5014. //
  5015. /**
  5016. * Get the skill current cooldown for player.
  5017. * (get the db base cooldown for skill + player specific cooldown)
  5018. * @param sd : player pointer
  5019. * @param id : skill id
  5020. * @param lv : skill lv
  5021. * @return player skill cooldown
  5022. */
  5023. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5024. uint16 idx = skill_get_index(skill_id);
  5025. int cooldown = 0;
  5026. if (!idx) return 0;
  5027. if (skill_db[idx]->cooldown[skill_lv - 1])
  5028. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5029. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5030. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5031. for (auto &it : sd->skillcooldown) {
  5032. if (it.id == skill_id) {
  5033. cooldown += it.val;
  5034. cooldown = max(0, cooldown);
  5035. break;
  5036. }
  5037. }
  5038. return cooldown;
  5039. }
  5040. /*==========================================
  5041. * Return player sd skill_lv learned for given skill
  5042. *------------------------------------------*/
  5043. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5044. {
  5045. uint16 idx = 0;
  5046. if (sd == NULL)
  5047. return 0;
  5048. if ((idx = skill_get_index(skill_id)) == 0) {
  5049. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5050. return 0;
  5051. }
  5052. if (SKILL_CHK_GUILD(skill_id) ) {
  5053. struct guild *g;
  5054. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5055. return guild_checkskill(g,skill_id);
  5056. return 0;
  5057. }
  5058. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5059. }
  5060. /**
  5061. * Check if we still have the correct weapon to continue the skill (actually status)
  5062. * If not ending it
  5063. * @param sd
  5064. * @return 0:error, 1:check done
  5065. */
  5066. static void pc_checkallowskill(struct map_session_data *sd)
  5067. {
  5068. const enum sc_type scw_list[] = {
  5069. SC_TWOHANDQUICKEN,
  5070. SC_ONEHAND,
  5071. SC_AURABLADE,
  5072. SC_PARRYING,
  5073. SC_SPEARQUICKEN,
  5074. SC_ADRENALINE,
  5075. SC_ADRENALINE2,
  5076. SC_DANCING,
  5077. SC_GATLINGFEVER,
  5078. };
  5079. uint8 i;
  5080. nullpo_retv(sd);
  5081. if(!sd->sc.count)
  5082. return;
  5083. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5084. { // Skills requiring specific weapon types
  5085. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5086. continue;
  5087. if(sd->sc.data[scw_list[i]] &&
  5088. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5089. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5090. }
  5091. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5092. // Spurt requires bare hands (feet, in fact xD)
  5093. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5094. if(sd->status.shield <= 0) { // Skills requiring a shield
  5095. const enum sc_type scs_list[] = {
  5096. SC_AUTOGUARD,
  5097. SC_DEFENDER,
  5098. SC_REFLECTSHIELD,
  5099. SC_REFLECTDAMAGE
  5100. };
  5101. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5102. if(sd->sc.data[scs_list[i]])
  5103. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5104. }
  5105. }
  5106. /*==========================================
  5107. * Return equipped index of item on player sd at pos
  5108. * Return
  5109. * -1 : Nothing equipped
  5110. * idx : (this index could be used in inventory to found item_data)
  5111. *------------------------------------------*/
  5112. short pc_checkequip(struct map_session_data *sd,int pos)
  5113. {
  5114. uint8 i;
  5115. nullpo_retr(-1, sd);
  5116. for(i=0;i<EQI_MAX;i++){
  5117. if(pos & equip_bitmask[i])
  5118. return sd->equip_index[i];
  5119. }
  5120. return -1;
  5121. }
  5122. /*==========================================
  5123. * Check if sd has nameid equipped somewhere
  5124. * @sd : the player session
  5125. * @nameid : id of the item to check
  5126. * @min : : see pc.hpp enum equip_index from ? to @max
  5127. * @max : see pc.hpp enum equip_index for @min to ?
  5128. * -return true,false
  5129. *------------------------------------------*/
  5130. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5131. {
  5132. int i;
  5133. for(i = min; i < max; i++) {
  5134. if(equip_bitmask[i]) {
  5135. int idx = sd->equip_index[i];
  5136. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5137. return true;
  5138. }
  5139. }
  5140. return false;
  5141. }
  5142. /*==========================================
  5143. * Convert's from the client's lame Job ID system
  5144. * to the map server's 'makes sense' system. [Skotlex]
  5145. *------------------------------------------*/
  5146. int pc_jobid2mapid(unsigned short b_class)
  5147. {
  5148. switch(b_class)
  5149. {
  5150. //Novice And 1-1 Jobs
  5151. case JOB_NOVICE: return MAPID_NOVICE;
  5152. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5153. case JOB_MAGE: return MAPID_MAGE;
  5154. case JOB_ARCHER: return MAPID_ARCHER;
  5155. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5156. case JOB_MERCHANT: return MAPID_MERCHANT;
  5157. case JOB_THIEF: return MAPID_THIEF;
  5158. case JOB_TAEKWON: return MAPID_TAEKWON;
  5159. case JOB_WEDDING: return MAPID_WEDDING;
  5160. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5161. case JOB_NINJA: return MAPID_NINJA;
  5162. case JOB_XMAS: return MAPID_XMAS;
  5163. case JOB_SUMMER: return MAPID_SUMMER;
  5164. case JOB_HANBOK: return MAPID_HANBOK;
  5165. case JOB_GANGSI: return MAPID_GANGSI;
  5166. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5167. case JOB_SUMMER2: return MAPID_SUMMER2;
  5168. //2-1 Jobs
  5169. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5170. case JOB_KNIGHT: return MAPID_KNIGHT;
  5171. case JOB_WIZARD: return MAPID_WIZARD;
  5172. case JOB_HUNTER: return MAPID_HUNTER;
  5173. case JOB_PRIEST: return MAPID_PRIEST;
  5174. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5175. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5176. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5177. case JOB_KAGEROU:
  5178. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5179. case JOB_REBELLION: return MAPID_REBELLION;
  5180. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5181. //2-2 Jobs
  5182. case JOB_CRUSADER: return MAPID_CRUSADER;
  5183. case JOB_SAGE: return MAPID_SAGE;
  5184. case JOB_BARD:
  5185. case JOB_DANCER: return MAPID_BARDDANCER;
  5186. case JOB_MONK: return MAPID_MONK;
  5187. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5188. case JOB_ROGUE: return MAPID_ROGUE;
  5189. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5190. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5191. //Trans Novice And Trans 1-1 Jobs
  5192. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5193. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5194. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5195. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5196. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5197. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5198. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5199. //Trans 2-1 Jobs
  5200. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5201. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5202. case JOB_SNIPER: return MAPID_SNIPER;
  5203. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5204. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5205. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5206. //Trans 2-2 Jobs
  5207. case JOB_PALADIN: return MAPID_PALADIN;
  5208. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5209. case JOB_CLOWN:
  5210. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5211. case JOB_CHAMPION: return MAPID_CHAMPION;
  5212. case JOB_CREATOR: return MAPID_CREATOR;
  5213. case JOB_STALKER: return MAPID_STALKER;
  5214. //Baby Novice And Baby 1-1 Jobs
  5215. case JOB_BABY: return MAPID_BABY;
  5216. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5217. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5218. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5219. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5220. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5221. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5222. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5223. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5224. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5225. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5226. //Baby 2-1 Jobs
  5227. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5228. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5229. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5230. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5231. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5232. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5233. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5234. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5235. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5236. case JOB_BABY_KAGEROU:
  5237. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5238. //Baby 2-2 Jobs
  5239. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5240. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5241. case JOB_BABY_BARD:
  5242. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5243. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5244. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5245. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5246. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5247. //3-1 Jobs
  5248. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5249. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5250. case JOB_WARLOCK: return MAPID_WARLOCK;
  5251. case JOB_RANGER: return MAPID_RANGER;
  5252. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5253. case JOB_MECHANIC: return MAPID_MECHANIC;
  5254. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5255. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5256. //3-2 Jobs
  5257. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5258. case JOB_SORCERER: return MAPID_SORCERER;
  5259. case JOB_MINSTREL:
  5260. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5261. case JOB_SURA: return MAPID_SURA;
  5262. case JOB_GENETIC: return MAPID_GENETIC;
  5263. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5264. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5265. //Trans 3-1 Jobs
  5266. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5267. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5268. case JOB_RANGER_T: return MAPID_RANGER_T;
  5269. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5270. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5271. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5272. //Trans 3-2 Jobs
  5273. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5274. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5275. case JOB_MINSTREL_T:
  5276. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5277. case JOB_SURA_T: return MAPID_SURA_T;
  5278. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5279. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5280. //Baby 3-1 Jobs
  5281. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5282. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5283. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5284. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5285. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5286. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5287. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5288. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5289. //Baby 3-2 Jobs
  5290. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5291. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5292. case JOB_BABY_MINSTREL:
  5293. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5294. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5295. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5296. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5297. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5298. //Doram Jobs
  5299. case JOB_SUMMONER: return MAPID_SUMMONER;
  5300. default:
  5301. return -1;
  5302. }
  5303. }
  5304. //Reverts the map-style class id to the client-style one.
  5305. int pc_mapid2jobid(unsigned short class_, int sex)
  5306. {
  5307. switch(class_) {
  5308. //Novice And 1-1 Jobs
  5309. case MAPID_NOVICE: return JOB_NOVICE;
  5310. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5311. case MAPID_MAGE: return JOB_MAGE;
  5312. case MAPID_ARCHER: return JOB_ARCHER;
  5313. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5314. case MAPID_MERCHANT: return JOB_MERCHANT;
  5315. case MAPID_THIEF: return JOB_THIEF;
  5316. case MAPID_TAEKWON: return JOB_TAEKWON;
  5317. case MAPID_WEDDING: return JOB_WEDDING;
  5318. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5319. case MAPID_NINJA: return JOB_NINJA;
  5320. case MAPID_XMAS: return JOB_XMAS;
  5321. case MAPID_SUMMER: return JOB_SUMMER;
  5322. case MAPID_HANBOK: return JOB_HANBOK;
  5323. case MAPID_GANGSI: return JOB_GANGSI;
  5324. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5325. case MAPID_SUMMER2: return JOB_SUMMER2;
  5326. //2-1 Jobs
  5327. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5328. case MAPID_KNIGHT: return JOB_KNIGHT;
  5329. case MAPID_WIZARD: return JOB_WIZARD;
  5330. case MAPID_HUNTER: return JOB_HUNTER;
  5331. case MAPID_PRIEST: return JOB_PRIEST;
  5332. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5333. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5334. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5335. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5336. case MAPID_REBELLION: return JOB_REBELLION;
  5337. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5338. //2-2 Jobs
  5339. case MAPID_CRUSADER: return JOB_CRUSADER;
  5340. case MAPID_SAGE: return JOB_SAGE;
  5341. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5342. case MAPID_MONK: return JOB_MONK;
  5343. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5344. case MAPID_ROGUE: return JOB_ROGUE;
  5345. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5346. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5347. //Trans Novice And Trans 2-1 Jobs
  5348. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5349. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5350. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5351. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5352. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5353. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5354. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5355. //Trans 2-1 Jobs
  5356. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5357. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5358. case MAPID_SNIPER: return JOB_SNIPER;
  5359. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5360. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5361. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5362. //Trans 2-2 Jobs
  5363. case MAPID_PALADIN: return JOB_PALADIN;
  5364. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5365. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5366. case MAPID_CHAMPION: return JOB_CHAMPION;
  5367. case MAPID_CREATOR: return JOB_CREATOR;
  5368. case MAPID_STALKER: return JOB_STALKER;
  5369. //Baby Novice And Baby 1-1 Jobs
  5370. case MAPID_BABY: return JOB_BABY;
  5371. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5372. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5373. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5374. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5375. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5376. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5377. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5378. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5379. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5380. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5381. //Baby 2-1 Jobs
  5382. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5383. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5384. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5385. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5386. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5387. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5388. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5389. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5390. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5391. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5392. //Baby 2-2 Jobs
  5393. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5394. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5395. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5396. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5397. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5398. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5399. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5400. //3-1 Jobs
  5401. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5402. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5403. case MAPID_WARLOCK: return JOB_WARLOCK;
  5404. case MAPID_RANGER: return JOB_RANGER;
  5405. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5406. case MAPID_MECHANIC: return JOB_MECHANIC;
  5407. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5408. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5409. //3-2 Jobs
  5410. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5411. case MAPID_SORCERER: return JOB_SORCERER;
  5412. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5413. case MAPID_SURA: return JOB_SURA;
  5414. case MAPID_GENETIC: return JOB_GENETIC;
  5415. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5416. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5417. //Trans 3-1 Jobs
  5418. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5419. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5420. case MAPID_RANGER_T: return JOB_RANGER_T;
  5421. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5422. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5423. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5424. //Trans 3-2 Jobs
  5425. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5426. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5427. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5428. case MAPID_SURA_T: return JOB_SURA_T;
  5429. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5430. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5431. //Baby 3-1 Jobs
  5432. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5433. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5434. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5435. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5436. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5437. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5438. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5439. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5440. //Baby 3-2 Jobs
  5441. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5442. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5443. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5444. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5445. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5446. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5447. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5448. //Doram Jobs
  5449. case MAPID_SUMMONER: return JOB_SUMMONER;
  5450. default:
  5451. return -1;
  5452. }
  5453. }
  5454. /*====================================================
  5455. * This function return the name of the job (by [Yor])
  5456. *----------------------------------------------------*/
  5457. const char* job_name(int class_)
  5458. {
  5459. switch (class_) {
  5460. case JOB_NOVICE:
  5461. case JOB_SWORDMAN:
  5462. case JOB_MAGE:
  5463. case JOB_ARCHER:
  5464. case JOB_ACOLYTE:
  5465. case JOB_MERCHANT:
  5466. case JOB_THIEF:
  5467. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5468. case JOB_KNIGHT:
  5469. case JOB_PRIEST:
  5470. case JOB_WIZARD:
  5471. case JOB_BLACKSMITH:
  5472. case JOB_HUNTER:
  5473. case JOB_ASSASSIN:
  5474. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5475. case JOB_KNIGHT2:
  5476. return msg_txt(NULL,557);
  5477. case JOB_CRUSADER:
  5478. case JOB_MONK:
  5479. case JOB_SAGE:
  5480. case JOB_ROGUE:
  5481. case JOB_ALCHEMIST:
  5482. case JOB_BARD:
  5483. case JOB_DANCER:
  5484. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5485. case JOB_CRUSADER2:
  5486. return msg_txt(NULL,563);
  5487. case JOB_WEDDING:
  5488. case JOB_SUPER_NOVICE:
  5489. case JOB_GUNSLINGER:
  5490. case JOB_NINJA:
  5491. case JOB_XMAS:
  5492. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5493. case JOB_SUMMER:
  5494. case JOB_SUMMER2:
  5495. return msg_txt(NULL,621);
  5496. case JOB_HANBOK:
  5497. return msg_txt(NULL,694);
  5498. case JOB_OKTOBERFEST:
  5499. return msg_txt(NULL,696);
  5500. case JOB_NOVICE_HIGH:
  5501. case JOB_SWORDMAN_HIGH:
  5502. case JOB_MAGE_HIGH:
  5503. case JOB_ARCHER_HIGH:
  5504. case JOB_ACOLYTE_HIGH:
  5505. case JOB_MERCHANT_HIGH:
  5506. case JOB_THIEF_HIGH:
  5507. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5508. case JOB_LORD_KNIGHT:
  5509. case JOB_HIGH_PRIEST:
  5510. case JOB_HIGH_WIZARD:
  5511. case JOB_WHITESMITH:
  5512. case JOB_SNIPER:
  5513. case JOB_ASSASSIN_CROSS:
  5514. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5515. case JOB_LORD_KNIGHT2:
  5516. return msg_txt(NULL,582);
  5517. case JOB_PALADIN:
  5518. case JOB_CHAMPION:
  5519. case JOB_PROFESSOR:
  5520. case JOB_STALKER:
  5521. case JOB_CREATOR:
  5522. case JOB_CLOWN:
  5523. case JOB_GYPSY:
  5524. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5525. case JOB_PALADIN2:
  5526. return msg_txt(NULL,588);
  5527. case JOB_BABY:
  5528. case JOB_BABY_SWORDMAN:
  5529. case JOB_BABY_MAGE:
  5530. case JOB_BABY_ARCHER:
  5531. case JOB_BABY_ACOLYTE:
  5532. case JOB_BABY_MERCHANT:
  5533. case JOB_BABY_THIEF:
  5534. return msg_txt(NULL,595 - JOB_BABY + class_);
  5535. case JOB_BABY_KNIGHT:
  5536. case JOB_BABY_PRIEST:
  5537. case JOB_BABY_WIZARD:
  5538. case JOB_BABY_BLACKSMITH:
  5539. case JOB_BABY_HUNTER:
  5540. case JOB_BABY_ASSASSIN:
  5541. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5542. case JOB_BABY_KNIGHT2:
  5543. return msg_txt(NULL,602);
  5544. case JOB_BABY_CRUSADER:
  5545. case JOB_BABY_MONK:
  5546. case JOB_BABY_SAGE:
  5547. case JOB_BABY_ROGUE:
  5548. case JOB_BABY_ALCHEMIST:
  5549. case JOB_BABY_BARD:
  5550. case JOB_BABY_DANCER:
  5551. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5552. case JOB_BABY_CRUSADER2:
  5553. return msg_txt(NULL,608);
  5554. case JOB_SUPER_BABY:
  5555. return msg_txt(NULL,615);
  5556. case JOB_TAEKWON:
  5557. return msg_txt(NULL,616);
  5558. case JOB_STAR_GLADIATOR:
  5559. case JOB_STAR_GLADIATOR2:
  5560. return msg_txt(NULL,617);
  5561. case JOB_SOUL_LINKER:
  5562. return msg_txt(NULL,618);
  5563. case JOB_GANGSI:
  5564. case JOB_DEATH_KNIGHT:
  5565. case JOB_DARK_COLLECTOR:
  5566. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5567. case JOB_RUNE_KNIGHT:
  5568. case JOB_WARLOCK:
  5569. case JOB_RANGER:
  5570. case JOB_ARCH_BISHOP:
  5571. case JOB_MECHANIC:
  5572. case JOB_GUILLOTINE_CROSS:
  5573. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5574. case JOB_RUNE_KNIGHT_T:
  5575. case JOB_WARLOCK_T:
  5576. case JOB_RANGER_T:
  5577. case JOB_ARCH_BISHOP_T:
  5578. case JOB_MECHANIC_T:
  5579. case JOB_GUILLOTINE_CROSS_T:
  5580. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5581. case JOB_ROYAL_GUARD:
  5582. case JOB_SORCERER:
  5583. case JOB_MINSTREL:
  5584. case JOB_WANDERER:
  5585. case JOB_SURA:
  5586. case JOB_GENETIC:
  5587. case JOB_SHADOW_CHASER:
  5588. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5589. case JOB_ROYAL_GUARD_T:
  5590. case JOB_SORCERER_T:
  5591. case JOB_MINSTREL_T:
  5592. case JOB_WANDERER_T:
  5593. case JOB_SURA_T:
  5594. case JOB_GENETIC_T:
  5595. case JOB_SHADOW_CHASER_T:
  5596. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5597. case JOB_RUNE_KNIGHT2:
  5598. case JOB_RUNE_KNIGHT_T2:
  5599. return msg_txt(NULL,625);
  5600. case JOB_ROYAL_GUARD2:
  5601. case JOB_ROYAL_GUARD_T2:
  5602. return msg_txt(NULL,631);
  5603. case JOB_RANGER2:
  5604. case JOB_RANGER_T2:
  5605. return msg_txt(NULL,627);
  5606. case JOB_MECHANIC2:
  5607. case JOB_MECHANIC_T2:
  5608. return msg_txt(NULL,629);
  5609. case JOB_BABY_RUNE:
  5610. case JOB_BABY_WARLOCK:
  5611. case JOB_BABY_RANGER:
  5612. case JOB_BABY_BISHOP:
  5613. case JOB_BABY_MECHANIC:
  5614. case JOB_BABY_CROSS:
  5615. case JOB_BABY_GUARD:
  5616. case JOB_BABY_SORCERER:
  5617. case JOB_BABY_MINSTREL:
  5618. case JOB_BABY_WANDERER:
  5619. case JOB_BABY_SURA:
  5620. case JOB_BABY_GENETIC:
  5621. case JOB_BABY_CHASER:
  5622. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5623. case JOB_BABY_RUNE2:
  5624. return msg_txt(NULL,638);
  5625. case JOB_BABY_GUARD2:
  5626. return msg_txt(NULL,644);
  5627. case JOB_BABY_RANGER2:
  5628. return msg_txt(NULL,640);
  5629. case JOB_BABY_MECHANIC2:
  5630. return msg_txt(NULL,642);
  5631. case JOB_SUPER_NOVICE_E:
  5632. case JOB_SUPER_BABY_E:
  5633. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5634. case JOB_KAGEROU:
  5635. case JOB_OBORO:
  5636. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5637. case JOB_REBELLION:
  5638. return msg_txt(NULL,695);
  5639. case JOB_SUMMONER:
  5640. return msg_txt(NULL,697);
  5641. case JOB_BABY_SUMMONER:
  5642. return msg_txt(NULL,698);
  5643. case JOB_BABY_NINJA:
  5644. return msg_txt(NULL,699);
  5645. case JOB_BABY_KAGEROU:
  5646. case JOB_BABY_OBORO:
  5647. case JOB_BABY_TAEKWON:
  5648. case JOB_BABY_STAR_GLADIATOR:
  5649. case JOB_BABY_SOUL_LINKER:
  5650. case JOB_BABY_GUNSLINGER:
  5651. case JOB_BABY_REBELLION:
  5652. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5653. case JOB_BABY_STAR_GLADIATOR2:
  5654. return msg_txt(NULL,756);
  5655. case JOB_STAR_EMPEROR:
  5656. case JOB_SOUL_REAPER:
  5657. case JOB_BABY_STAR_EMPEROR:
  5658. case JOB_BABY_SOUL_REAPER:
  5659. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5660. case JOB_STAR_EMPEROR2:
  5661. return msg_txt(NULL,782);
  5662. case JOB_BABY_STAR_EMPEROR2:
  5663. return msg_txt(NULL,784);
  5664. default:
  5665. return msg_txt(NULL,655);
  5666. }
  5667. }
  5668. /*====================================================
  5669. * Timered function to make id follow a target.
  5670. * @id = bl.id (player only atm)
  5671. * target is define in sd->followtarget (bl.id)
  5672. * used by pc_follow
  5673. *----------------------------------------------------*/
  5674. TIMER_FUNC(pc_follow_timer){
  5675. struct map_session_data *sd;
  5676. struct block_list *tbl;
  5677. sd = map_id2sd(id);
  5678. nullpo_ret(sd);
  5679. if (sd->followtimer != tid){
  5680. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5681. sd->followtimer = INVALID_TIMER;
  5682. return 0;
  5683. }
  5684. sd->followtimer = INVALID_TIMER;
  5685. tbl = map_id2bl(sd->followtarget);
  5686. if (tbl == NULL || pc_isdead(sd))
  5687. {
  5688. pc_stop_following(sd);
  5689. return 0;
  5690. }
  5691. // either player or target is currently detached from map blocks (could be teleporting),
  5692. // but still connected to this map, so we'll just increment the timer and check back later
  5693. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5694. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5695. {
  5696. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5697. if (!check_distance_bl(&sd->bl, tbl, 5))
  5698. unit_walktobl(&sd->bl, tbl, 5, 0);
  5699. } else
  5700. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5701. }
  5702. sd->followtimer = add_timer(
  5703. tick + 1000, // increase time a bit to loosen up map's load
  5704. pc_follow_timer, sd->bl.id, 0);
  5705. return 0;
  5706. }
  5707. int pc_stop_following (struct map_session_data *sd)
  5708. {
  5709. nullpo_ret(sd);
  5710. if (sd->followtimer != INVALID_TIMER) {
  5711. delete_timer(sd->followtimer,pc_follow_timer);
  5712. sd->followtimer = INVALID_TIMER;
  5713. }
  5714. sd->followtarget = -1;
  5715. sd->ud.target_to = 0;
  5716. unit_stop_walking(&sd->bl, 1);
  5717. return 0;
  5718. }
  5719. int pc_follow(struct map_session_data *sd,int target_id)
  5720. {
  5721. struct block_list *bl = map_id2bl(target_id);
  5722. if (bl == NULL /*|| bl->type != BL_PC*/)
  5723. return 1;
  5724. if (sd->followtimer != INVALID_TIMER)
  5725. pc_stop_following(sd);
  5726. sd->followtarget = target_id;
  5727. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5728. return 0;
  5729. }
  5730. int pc_checkbaselevelup(struct map_session_data *sd) {
  5731. unsigned int next = pc_nextbaseexp(sd);
  5732. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5733. return 0;
  5734. do {
  5735. sd->status.base_exp -= next;
  5736. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5737. 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 )
  5738. sd->status.base_exp = next-1;
  5739. next = pc_gets_status_point(sd->status.base_level);
  5740. sd->status.base_level++;
  5741. sd->status.status_point += next;
  5742. if( pc_is_maxbaselv(sd) ){
  5743. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5744. break;
  5745. }
  5746. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5747. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5748. status_calc_pet(sd->pd,SCO_NONE);
  5749. clif_updatestatus(sd,SP_STATUSPOINT);
  5750. clif_updatestatus(sd,SP_BASELEVEL);
  5751. clif_updatestatus(sd,SP_BASEEXP);
  5752. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5753. status_calc_pc(sd,SCO_FORCE);
  5754. status_percent_heal(&sd->bl,100,100);
  5755. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5756. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5757. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5758. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5759. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5760. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5761. if (sd->state.snovice_dead_flag)
  5762. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5763. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5764. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5765. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5766. }
  5767. clif_misceffect(&sd->bl,0);
  5768. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5769. if(sd->status.party_id)
  5770. party_send_levelup(sd);
  5771. pc_baselevelchanged(sd);
  5772. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  5773. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  5774. return 1;
  5775. }
  5776. void pc_baselevelchanged(struct map_session_data *sd) {
  5777. uint8 i;
  5778. for( i = 0; i < EQI_MAX; i++ ) {
  5779. if( sd->equip_index[i] >= 0 ) {
  5780. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5781. pc_unequipitem(sd, sd->equip_index[i], 3);
  5782. }
  5783. }
  5784. pc_show_questinfo(sd);
  5785. }
  5786. int pc_checkjoblevelup(struct map_session_data *sd)
  5787. {
  5788. unsigned int next = pc_nextjobexp(sd);
  5789. nullpo_ret(sd);
  5790. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5791. return 0;
  5792. do {
  5793. sd->status.job_exp -= next;
  5794. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5795. 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 )
  5796. sd->status.job_exp = next-1;
  5797. sd->status.job_level ++;
  5798. sd->status.skill_point ++;
  5799. if( pc_is_maxjoblv(sd) ){
  5800. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5801. break;
  5802. }
  5803. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5804. clif_updatestatus(sd,SP_JOBLEVEL);
  5805. clif_updatestatus(sd,SP_JOBEXP);
  5806. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5807. clif_updatestatus(sd,SP_SKILLPOINT);
  5808. status_calc_pc(sd,SCO_FORCE);
  5809. clif_misceffect(&sd->bl,1);
  5810. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5811. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5812. npc_script_event(sd, NPCE_JOBLVUP);
  5813. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  5814. return 1;
  5815. }
  5816. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5817. * @param sd Player
  5818. * @param base_exp Base EXP before peronal bonuses
  5819. * @param job_exp Job EXP before peronal bonuses
  5820. * @param src Block list that affecting the exp calculation
  5821. */
  5822. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5823. {
  5824. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5825. if (src) {
  5826. struct status_data *status = status_get_status_data(src);
  5827. if( sd->expaddrace[status->race] )
  5828. bonus += sd->expaddrace[status->race];
  5829. if( sd->expaddrace[RC_ALL] )
  5830. bonus += sd->expaddrace[RC_ALL];
  5831. if( sd->expaddclass[status->class_] )
  5832. bonus += sd->expaddclass[status->class_];
  5833. if( sd->expaddclass[CLASS_ALL] )
  5834. bonus += sd->expaddclass[CLASS_ALL];
  5835. if (battle_config.pk_mode &&
  5836. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5837. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5838. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5839. vip_bonus_base = battle_config.vip_base_exp_increase;
  5840. vip_bonus_job = battle_config.vip_job_exp_increase;
  5841. }
  5842. }
  5843. // Give EXPBOOST for quests even if src is NULL.
  5844. if (sd->sc.data[SC_EXPBOOST]) {
  5845. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5846. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5847. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5848. }
  5849. if (*base_exp) {
  5850. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5851. *base_exp = cap_value(exp, 1, UINT_MAX);
  5852. }
  5853. // Give JEXPBOOST for quests even if src is NULL.
  5854. if (sd->sc.data[SC_JEXPBOOST])
  5855. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5856. if (*job_exp) {
  5857. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5858. *job_exp = cap_value(exp, 1, UINT_MAX);
  5859. }
  5860. return;
  5861. }
  5862. /**
  5863. * Show EXP gained by player in percentage by @showexp
  5864. * @param sd Player
  5865. * @param base_exp Base EXP gained/loss
  5866. * @param next_base_exp Base EXP needed for next base level
  5867. * @param job_exp Job EXP gained/loss
  5868. * @param next_job_exp Job EXP needed for next job level
  5869. * @param lost True:EXP penalty, lose EXP
  5870. **/
  5871. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  5872. char output[CHAT_SIZE_MAX];
  5873. nullpo_retv(sd);
  5874. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5875. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5876. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5877. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5878. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5879. }
  5880. /**
  5881. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5882. * @param sd Player
  5883. * @param src EXP source
  5884. * @param base_exp Base EXP gained
  5885. * @param base_exp Job EXP gained
  5886. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5887. * @return
  5888. **/
  5889. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5890. {
  5891. unsigned int nextb = 0, nextj = 0;
  5892. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5893. nullpo_retv(sd);
  5894. if(sd->bl.prev == NULL || pc_isdead(sd))
  5895. return;
  5896. if (!(exp_flag&2)) {
  5897. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  5898. return; // no exp on pvp maps
  5899. if (sd->status.guild_id>0)
  5900. base_exp -= guild_payexp(sd,base_exp);
  5901. }
  5902. flag = ((base_exp) ? 1 : 0) |
  5903. ((job_exp) ? 2 : 0) |
  5904. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5905. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5906. if (!(exp_flag&2))
  5907. pc_calcexp(sd, &base_exp, &job_exp, src);
  5908. nextb = pc_nextbaseexp(sd);
  5909. nextj = pc_nextjobexp(sd);
  5910. if (flag&4){
  5911. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5912. base_exp = 0;
  5913. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5914. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5915. }
  5916. if (flag&8){
  5917. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5918. job_exp = 0;
  5919. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5920. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5921. }
  5922. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5923. //Note that this value should never be greater than the original
  5924. //therefore no overflow checks are needed. [Skotlex]
  5925. if (nextb > 0) {
  5926. float nextbp = (float) base_exp / (float) nextb;
  5927. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5928. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5929. }
  5930. if (nextj > 0) {
  5931. float nextjp = (float) job_exp / (float) nextj;
  5932. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5933. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5934. }
  5935. }
  5936. // Give EXP for Base Level
  5937. if (base_exp) {
  5938. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5939. sd->status.base_exp = UINT32_MAX;
  5940. else
  5941. sd->status.base_exp += base_exp;
  5942. if (!pc_checkbaselevelup(sd))
  5943. clif_updatestatus(sd,SP_BASEEXP);
  5944. }
  5945. // Give EXP for Job Level
  5946. if (job_exp) {
  5947. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5948. sd->status.job_exp = UINT32_MAX;
  5949. else
  5950. sd->status.job_exp += job_exp;
  5951. if (!pc_checkjoblevelup(sd))
  5952. clif_updatestatus(sd,SP_JOBEXP);
  5953. }
  5954. if (flag&1)
  5955. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5956. if (flag&2)
  5957. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5958. if (sd->state.showexp && (base_exp || job_exp))
  5959. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5960. }
  5961. /**
  5962. * Lost Base/Job EXP from a player
  5963. * @param sd Player
  5964. * @param base_exp Base EXP lost
  5965. * @param job_exp Job EXP lost
  5966. **/
  5967. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  5968. nullpo_retv(sd);
  5969. if (base_exp) {
  5970. base_exp = u32min(sd->status.base_exp, base_exp);
  5971. sd->status.base_exp -= base_exp;
  5972. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  5973. clif_updatestatus(sd, SP_BASEEXP);
  5974. }
  5975. if (job_exp) {
  5976. job_exp = u32min(sd->status.job_exp, job_exp);
  5977. sd->status.job_exp -= job_exp;
  5978. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  5979. clif_updatestatus(sd, SP_JOBEXP);
  5980. }
  5981. if (sd->state.showexp && (base_exp || job_exp))
  5982. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  5983. }
  5984. /**
  5985. * Returns max base level for this character's class.
  5986. * @param class_: Player's class
  5987. * @return Max Base Level
  5988. */
  5989. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  5990. return job_info[pc_class2idx(class_)].max_level[0];
  5991. }
  5992. /**
  5993. * Returns max base level for this character.
  5994. * @param sd Player
  5995. * @return Max Base Level
  5996. **/
  5997. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5998. return pc_class_maxbaselv(sd->status.class_);
  5999. }
  6000. /**
  6001. * Returns max job level for this character's class.
  6002. * @param class_: Player's class
  6003. * @return Max Job Level
  6004. */
  6005. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6006. return job_info[pc_class2idx(class_)].max_level[1];
  6007. }
  6008. /**
  6009. * Returns max job level for this character.
  6010. * @param sd Player
  6011. * @return Max Job Level
  6012. **/
  6013. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6014. return pc_class_maxjoblv(sd->status.class_);
  6015. }
  6016. /**
  6017. * Check if player is reached max base level
  6018. * @param sd
  6019. * @return True if reached max level
  6020. **/
  6021. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6022. nullpo_retr(false, sd);
  6023. return (sd->status.base_level >= pc_maxbaselv(sd));
  6024. }
  6025. /**
  6026. * Check if player is reached max base level
  6027. * @param sd
  6028. * @return True if reached max level
  6029. **/
  6030. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6031. nullpo_retr(false, sd);
  6032. return (sd->status.job_level >= pc_maxjoblv(sd));
  6033. }
  6034. /**
  6035. * Base exp needed for player to level up.
  6036. * @param sd
  6037. * @return Base EXP needed for next base level
  6038. **/
  6039. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6040. nullpo_ret(sd);
  6041. if (sd->status.base_level == 0) // Is this something that possible?
  6042. return 0;
  6043. if (pc_is_maxbaselv(sd))
  6044. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6045. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6046. }
  6047. /**
  6048. * Job exp needed for player to level up.
  6049. * @param sd
  6050. * @return Job EXP needed for next job level
  6051. **/
  6052. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6053. nullpo_ret(sd);
  6054. if (sd->status.job_level == 0) // Is this something that possible?
  6055. return 0;
  6056. if (pc_is_maxjoblv(sd))
  6057. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6058. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6059. }
  6060. /// Returns the value of the specified stat.
  6061. static int pc_getstat(struct map_session_data* sd, int type)
  6062. {
  6063. nullpo_retr(-1, sd);
  6064. switch( type ) {
  6065. case SP_STR: return sd->status.str;
  6066. case SP_AGI: return sd->status.agi;
  6067. case SP_VIT: return sd->status.vit;
  6068. case SP_INT: return sd->status.int_;
  6069. case SP_DEX: return sd->status.dex;
  6070. case SP_LUK: return sd->status.luk;
  6071. default:
  6072. return -1;
  6073. }
  6074. }
  6075. /// Sets the specified stat to the specified value.
  6076. /// Returns the new value.
  6077. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6078. {
  6079. nullpo_retr(-1, sd);
  6080. switch( type ) {
  6081. case SP_STR: sd->status.str = val; break;
  6082. case SP_AGI: sd->status.agi = val; break;
  6083. case SP_VIT: sd->status.vit = val; break;
  6084. case SP_INT: sd->status.int_ = val; break;
  6085. case SP_DEX: sd->status.dex = val; break;
  6086. case SP_LUK: sd->status.luk = val; break;
  6087. default:
  6088. return -1;
  6089. }
  6090. return val;
  6091. }
  6092. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6093. int pc_gets_status_point(int level)
  6094. {
  6095. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6096. return (statp[level+1] - statp[level]);
  6097. else //Default increase
  6098. return ((level+15) / 5);
  6099. }
  6100. #ifdef RENEWAL_STAT
  6101. /// Renewal status point cost formula
  6102. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6103. #else
  6104. /// Pre-Renewal status point cost formula
  6105. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6106. #endif
  6107. /// Returns the number of stat points needed to change the specified stat by val.
  6108. /// If val is negative, returns the number of stat points that would be needed to
  6109. /// raise the specified stat from (current value - val) to current value.
  6110. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6111. {
  6112. int low, high, sp = 0, max = 0;
  6113. if ( val == 0 )
  6114. return 0;
  6115. low = pc_getstat(sd,type);
  6116. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6117. if ( low >= max && val > 0 )
  6118. return 0; // Official servers show '0' when max is reached
  6119. high = low + val;
  6120. if ( val < 0 )
  6121. SWAP(low, high);
  6122. for ( ; low < high; low++ )
  6123. sp += PC_STATUS_POINT_COST(low);
  6124. return sp;
  6125. }
  6126. /**
  6127. * Returns the value the specified stat can be increased by with the current
  6128. * amount of available status points for the current character's class.
  6129. *
  6130. * @param sd The target character.
  6131. * @param type Stat to verify.
  6132. * @return Maximum value the stat could grow by.
  6133. */
  6134. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6135. {
  6136. int base, final_val, status_points, max_param;
  6137. nullpo_ret(sd);
  6138. base = final_val = pc_getstat(sd, type);
  6139. status_points = sd->status.status_point;
  6140. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6141. while (final_val <= max_param && status_points >= 0) {
  6142. status_points -= PC_STATUS_POINT_COST(final_val);
  6143. final_val++;
  6144. }
  6145. final_val--;
  6146. return (final_val > base ? final_val-base : 0);
  6147. }
  6148. /**
  6149. * Raises a stat by the specified amount.
  6150. *
  6151. * Obeys max_parameter limits.
  6152. * Subtracts status points according to the cost of the increased stat points.
  6153. *
  6154. * @param sd The target character.
  6155. * @param type The stat to change (see enum _sp)
  6156. * @param increase The stat increase (strictly positive) amount.
  6157. * @retval true if the stat was increased by any amount.
  6158. * @retval false if there were no changes.
  6159. */
  6160. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6161. {
  6162. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6163. nullpo_ret(sd);
  6164. // check conditions
  6165. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6166. clif_statusupack(sd, type, 0, 0);
  6167. return false;
  6168. }
  6169. // check limits
  6170. current = pc_getstat(sd, type);
  6171. max_increase = pc_maxparameterincrease(sd, type);
  6172. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6173. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6174. clif_statusupack(sd, type, 0, 0);
  6175. return false;
  6176. }
  6177. // check status points
  6178. needed_points = pc_need_status_point(sd, type, increase);
  6179. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6180. clif_statusupack(sd, type, 0, 0);
  6181. return false;
  6182. }
  6183. // set new values
  6184. final_value = pc_setstat(sd, type, current + increase);
  6185. sd->status.status_point -= needed_points;
  6186. status_calc_pc(sd,SCO_NONE);
  6187. // update increase cost indicator
  6188. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6189. // update statpoint count
  6190. clif_updatestatus(sd, SP_STATUSPOINT);
  6191. // update stat value
  6192. clif_statusupack(sd, type, 1, final_value); // required
  6193. if( final_value > 255 )
  6194. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6195. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6196. return true;
  6197. }
  6198. /**
  6199. * Raises a stat by the specified amount.
  6200. *
  6201. * Obeys max_parameter limits.
  6202. * Does not subtract status points for the cost of the modified stat points.
  6203. *
  6204. * @param sd The target character.
  6205. * @param type The stat to change (see enum _sp)
  6206. * @param val The stat increase (or decrease) amount.
  6207. * @return the stat increase amount.
  6208. * @retval 0 if no changes were made.
  6209. */
  6210. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6211. {
  6212. int max, need;
  6213. nullpo_ret(sd);
  6214. if( type < SP_STR || type > SP_LUK )
  6215. {
  6216. clif_statusupack(sd,type,0,0);
  6217. return 0;
  6218. }
  6219. need = pc_need_status_point(sd,type,1);
  6220. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6221. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6222. status_calc_pc(sd,SCO_NONE);
  6223. // update increase cost indicator
  6224. if( need != pc_need_status_point(sd,type,1) )
  6225. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6226. // update stat value
  6227. clif_statusupack(sd,type,1,val); // required
  6228. if( val > 255 )
  6229. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6230. return val;
  6231. }
  6232. /*==========================================
  6233. * Update skill_lv for player sd
  6234. * Skill point allocation
  6235. *------------------------------------------*/
  6236. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6237. {
  6238. uint16 idx = skill_get_index(skill_id);
  6239. nullpo_retv(sd);
  6240. if (!idx) {
  6241. if (skill_id)
  6242. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6243. return;
  6244. }
  6245. // Level up guild skill
  6246. if (SKILL_CHK_GUILD(skill_id)) {
  6247. guild_skillup(sd, skill_id);
  6248. return;
  6249. }
  6250. // Level up homunculus skill
  6251. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6252. hom_skillup(sd->hd, skill_id);
  6253. return;
  6254. }
  6255. else {
  6256. if( sd->status.skill_point > 0 &&
  6257. sd->status.skill[idx].id &&
  6258. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6259. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6260. {
  6261. int lv, range, upgradable;
  6262. sd->status.skill[idx].lv++;
  6263. sd->status.skill_point--;
  6264. if( !skill_get_inf(skill_id) )
  6265. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6266. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6267. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6268. else
  6269. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6270. lv = sd->status.skill[idx].lv;
  6271. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6272. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6273. clif_skillup(sd,skill_id,lv,range,upgradable);
  6274. clif_updatestatus(sd,SP_SKILLPOINT);
  6275. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6276. clif_updatestatus(sd,SP_CARTINFO);
  6277. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6278. clif_skillinfoblock(sd);
  6279. }
  6280. //else
  6281. // 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);
  6282. }
  6283. }
  6284. /*==========================================
  6285. * /allskill
  6286. *------------------------------------------*/
  6287. int pc_allskillup(struct map_session_data *sd)
  6288. {
  6289. int i;
  6290. nullpo_ret(sd);
  6291. for (i = 0; i < MAX_SKILL; i++) {
  6292. 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) {
  6293. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6294. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6295. if (sd->status.skill[i].lv == 0)
  6296. sd->status.skill[i].id = 0;
  6297. }
  6298. }
  6299. if (!pc_grant_allskills(sd, true)) {
  6300. uint16 sk_id;
  6301. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6302. int inf2 = 0;
  6303. uint16 sk_idx = 0;
  6304. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6305. continue;
  6306. inf2 = skill_get_inf2(sk_id);
  6307. if (
  6308. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6309. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6310. sk_id == SG_DEVIL
  6311. )
  6312. continue; //Cannot be learned normally.
  6313. sd->status.skill[sk_idx].id = sk_id;
  6314. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6315. }
  6316. }
  6317. status_calc_pc(sd,SCO_NONE);
  6318. //Required because if you could level up all skills previously,
  6319. //the update will not be sent as only the lv variable changes.
  6320. clif_skillinfoblock(sd);
  6321. return 0;
  6322. }
  6323. /*==========================================
  6324. * /resetlvl
  6325. *------------------------------------------*/
  6326. int pc_resetlvl(struct map_session_data* sd,int type)
  6327. {
  6328. int i;
  6329. nullpo_ret(sd);
  6330. if (type != 3) //Also reset skills
  6331. pc_resetskill(sd, 0);
  6332. if(type == 1){
  6333. sd->status.skill_point=0;
  6334. sd->status.base_level=1;
  6335. sd->status.job_level=1;
  6336. sd->status.base_exp=0;
  6337. sd->status.job_exp=0;
  6338. if(sd->sc.option !=0)
  6339. sd->sc.option = 0;
  6340. sd->status.str=1;
  6341. sd->status.agi=1;
  6342. sd->status.vit=1;
  6343. sd->status.int_=1;
  6344. sd->status.dex=1;
  6345. sd->status.luk=1;
  6346. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6347. sd->status.status_point=100; // not 88 [celest]
  6348. // give platinum skills upon changing
  6349. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6350. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6351. }
  6352. }
  6353. if(type == 2){
  6354. sd->status.skill_point=0;
  6355. sd->status.base_level=1;
  6356. sd->status.job_level=1;
  6357. sd->status.base_exp=0;
  6358. sd->status.job_exp=0;
  6359. }
  6360. if(type == 3){
  6361. sd->status.base_level=1;
  6362. sd->status.base_exp=0;
  6363. }
  6364. if(type == 4){
  6365. sd->status.job_level=1;
  6366. sd->status.job_exp=0;
  6367. }
  6368. clif_updatestatus(sd,SP_STATUSPOINT);
  6369. clif_updatestatus(sd,SP_STR);
  6370. clif_updatestatus(sd,SP_AGI);
  6371. clif_updatestatus(sd,SP_VIT);
  6372. clif_updatestatus(sd,SP_INT);
  6373. clif_updatestatus(sd,SP_DEX);
  6374. clif_updatestatus(sd,SP_LUK);
  6375. clif_updatestatus(sd,SP_BASELEVEL);
  6376. clif_updatestatus(sd,SP_JOBLEVEL);
  6377. clif_updatestatus(sd,SP_STATUSPOINT);
  6378. clif_updatestatus(sd,SP_BASEEXP);
  6379. clif_updatestatus(sd,SP_JOBEXP);
  6380. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6381. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6382. clif_updatestatus(sd,SP_SKILLPOINT);
  6383. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6384. clif_updatestatus(sd,SP_UAGI);
  6385. clif_updatestatus(sd,SP_UVIT);
  6386. clif_updatestatus(sd,SP_UINT);
  6387. clif_updatestatus(sd,SP_UDEX);
  6388. clif_updatestatus(sd,SP_ULUK); // End Addition
  6389. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6390. if(sd->equip_index[i] >= 0)
  6391. if(pc_isequip(sd,sd->equip_index[i]))
  6392. pc_unequipitem(sd,sd->equip_index[i],2);
  6393. }
  6394. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6395. party_send_levelup(sd);
  6396. status_calc_pc(sd, SCO_FORCE);
  6397. clif_skillinfoblock(sd);
  6398. return 0;
  6399. }
  6400. /*==========================================
  6401. * /resetstate
  6402. *------------------------------------------*/
  6403. int pc_resetstate(struct map_session_data* sd)
  6404. {
  6405. nullpo_ret(sd);
  6406. if (battle_config.use_statpoint_table)
  6407. { // New statpoint table used here - Dexity
  6408. if (sd->status.base_level > MAX_LEVEL)
  6409. { //statp[] goes out of bounds, can't reset!
  6410. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6411. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6412. return 0;
  6413. }
  6414. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6415. }
  6416. else
  6417. {
  6418. int add=0;
  6419. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6420. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6421. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6422. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6423. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6424. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6425. sd->status.status_point+=add;
  6426. }
  6427. pc_setstat(sd, SP_STR, 1);
  6428. pc_setstat(sd, SP_AGI, 1);
  6429. pc_setstat(sd, SP_VIT, 1);
  6430. pc_setstat(sd, SP_INT, 1);
  6431. pc_setstat(sd, SP_DEX, 1);
  6432. pc_setstat(sd, SP_LUK, 1);
  6433. clif_updatestatus(sd,SP_STR);
  6434. clif_updatestatus(sd,SP_AGI);
  6435. clif_updatestatus(sd,SP_VIT);
  6436. clif_updatestatus(sd,SP_INT);
  6437. clif_updatestatus(sd,SP_DEX);
  6438. clif_updatestatus(sd,SP_LUK);
  6439. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6440. clif_updatestatus(sd,SP_UAGI);
  6441. clif_updatestatus(sd,SP_UVIT);
  6442. clif_updatestatus(sd,SP_UINT);
  6443. clif_updatestatus(sd,SP_UDEX);
  6444. clif_updatestatus(sd,SP_ULUK); // End Addition
  6445. clif_updatestatus(sd,SP_STATUSPOINT);
  6446. if( sd->mission_mobid ) { //bugreport:2200
  6447. sd->mission_mobid = 0;
  6448. sd->mission_count = 0;
  6449. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6450. }
  6451. status_calc_pc(sd, SCO_NONE);
  6452. return 1;
  6453. }
  6454. /*==========================================
  6455. * /resetskill
  6456. * if flag&1, perform block resync and status_calc call.
  6457. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6458. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6459. *------------------------------------------*/
  6460. int pc_resetskill(struct map_session_data* sd, int flag)
  6461. {
  6462. int i, skill_point=0;
  6463. nullpo_ret(sd);
  6464. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6465. return 0;
  6466. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6467. /**
  6468. * 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)
  6469. **/
  6470. if( pc_is_taekwon_ranker(sd) )
  6471. return 0;
  6472. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6473. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6474. i = sd->sc.option;
  6475. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6476. i &= ~OPTION_RIDING;
  6477. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6478. i &= ~OPTION_FALCON;
  6479. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6480. i &= ~OPTION_DRAGON;
  6481. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6482. i &= ~OPTION_WUG;
  6483. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6484. i &= ~OPTION_WUGRIDER;
  6485. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6486. i &= ~OPTION_MADOGEAR;
  6487. #ifndef NEW_CARTS
  6488. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6489. i &= ~OPTION_CART;
  6490. #else
  6491. if( sd->sc.data[SC_PUSH_CART] )
  6492. pc_setcart(sd, 0);
  6493. #endif
  6494. if( i != sd->sc.option )
  6495. pc_setoption(sd, i);
  6496. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6497. hom_vaporize(sd, HOM_ST_ACTIVE);
  6498. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6499. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6500. }
  6501. for( i = 1; i < MAX_SKILL; i++ )
  6502. {
  6503. uint8 lv = sd->status.skill[i].lv;
  6504. int inf2;
  6505. uint16 skill_id = skill_idx2id(i);
  6506. if (lv == 0 || skill_id == 0)
  6507. continue;
  6508. inf2 = skill_get_inf2(skill_id);
  6509. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6510. continue;
  6511. // Don't reset trick dead if not a novice/baby
  6512. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6513. {
  6514. sd->status.skill[i].lv = 0;
  6515. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6516. continue;
  6517. }
  6518. // do not reset basic skill
  6519. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6520. continue;
  6521. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6522. continue;
  6523. if( flag&4 && !skill_ischangesex(skill_id) )
  6524. continue;
  6525. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6526. { //Only handle quest skills in a special way when you can't learn them manually
  6527. if( battle_config.quest_skill_reset && !(flag&2) )
  6528. { //Wipe them
  6529. sd->status.skill[i].lv = 0;
  6530. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6531. }
  6532. continue;
  6533. }
  6534. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6535. skill_point += lv;
  6536. else
  6537. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6538. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6539. if( !(flag&2) )
  6540. {// reset
  6541. sd->status.skill[i].lv = 0;
  6542. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6543. }
  6544. }
  6545. if( flag&2 || !skill_point ) return skill_point;
  6546. sd->status.skill_point += skill_point;
  6547. if (flag&1) {
  6548. clif_updatestatus(sd,SP_SKILLPOINT);
  6549. clif_skillinfoblock(sd);
  6550. status_calc_pc(sd, SCO_FORCE);
  6551. }
  6552. return skill_point;
  6553. }
  6554. /*==========================================
  6555. * /resetfeel [Komurka]
  6556. *------------------------------------------*/
  6557. int pc_resetfeel(struct map_session_data* sd)
  6558. {
  6559. int i;
  6560. nullpo_ret(sd);
  6561. for (i=0; i<MAX_PC_FEELHATE; i++)
  6562. {
  6563. sd->feel_map[i].m = -1;
  6564. sd->feel_map[i].index = 0;
  6565. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6566. }
  6567. return 0;
  6568. }
  6569. int pc_resethate(struct map_session_data* sd)
  6570. {
  6571. int i;
  6572. nullpo_ret(sd);
  6573. for (i=0; i<3; i++)
  6574. {
  6575. sd->hate_mob[i] = -1;
  6576. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6577. }
  6578. return 0;
  6579. }
  6580. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6581. {
  6582. int bonus = 0;
  6583. nullpo_ret(sd);
  6584. skill_id = skill_dummy2skill_id(skill_id);
  6585. for (auto &it : sd->skillatk) {
  6586. if (it.id == skill_id) {
  6587. bonus += it.val;
  6588. break;
  6589. }
  6590. }
  6591. return bonus;
  6592. }
  6593. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6594. {
  6595. int bonus = 0;
  6596. nullpo_ret(sd);
  6597. skill_id = skill_dummy2skill_id(skill_id);
  6598. for (auto &it : sd->subskill) {
  6599. if (it.id == skill_id) {
  6600. bonus += it.val;
  6601. break;
  6602. }
  6603. }
  6604. return bonus;
  6605. }
  6606. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6607. int bonus = sd->bonus.add_heal_rate;
  6608. nullpo_ret(sd);
  6609. skill_id = skill_dummy2skill_id(skill_id);
  6610. if( bonus ) {
  6611. switch( skill_id ) {
  6612. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6613. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6614. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6615. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6616. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6617. }
  6618. }
  6619. for (auto &it : sd->skillheal) {
  6620. if (it.id == skill_id) {
  6621. bonus += it.val;
  6622. break;
  6623. }
  6624. }
  6625. return bonus;
  6626. }
  6627. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6628. int bonus = sd->bonus.add_heal2_rate;
  6629. skill_id = skill_dummy2skill_id(skill_id);
  6630. for (auto &it : sd->skillheal2) {
  6631. if (it.id == skill_id) {
  6632. bonus += it.val;
  6633. break;
  6634. }
  6635. }
  6636. return bonus;
  6637. }
  6638. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6639. {
  6640. if( !pc_isdead(sd) )
  6641. return; // not applicable
  6642. if( sd->bg_id && bg_member_respawn(sd) )
  6643. return; // member revived by battleground
  6644. pc_setstand(sd, true);
  6645. pc_setrestartvalue(sd,3);
  6646. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6647. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6648. }
  6649. static TIMER_FUNC(pc_respawn_timer){
  6650. struct map_session_data *sd = map_id2sd(id);
  6651. if( sd != NULL )
  6652. {
  6653. sd->pvp_point=0;
  6654. pc_respawn(sd,CLR_OUTSIGHT);
  6655. }
  6656. return 0;
  6657. }
  6658. /*==========================================
  6659. * Invoked when a player has received damage
  6660. *------------------------------------------*/
  6661. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6662. {
  6663. if (sp) clif_updatestatus(sd,SP_SP);
  6664. if (hp) clif_updatestatus(sd,SP_HP);
  6665. else return;
  6666. if (!src)
  6667. return;
  6668. if( pc_issit(sd) ) {
  6669. pc_setstand(sd, true);
  6670. skill_sit(sd,0);
  6671. }
  6672. if (sd->progressbar.npc_id)
  6673. clif_progressbar_abort(sd);
  6674. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6675. pet_target_check(sd->pd,src,1);
  6676. if( sd->status.ele_id > 0 )
  6677. elemental_set_target(sd,src);
  6678. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6679. sd->canlog_tick = gettick();
  6680. }
  6681. TIMER_FUNC(pc_close_npc_timer){
  6682. TBL_PC *sd = map_id2sd(id);
  6683. if(sd) pc_close_npc(sd,data);
  6684. return 0;
  6685. }
  6686. /**
  6687. * Method to properly close a NPC for player and clear anything related.
  6688. * @param sd: Player attached
  6689. * @param flag: Method of closure
  6690. * 1: Produce a close button and end the NPC
  6691. * 2: End the NPC (best for no dialog windows)
  6692. */
  6693. void pc_close_npc(struct map_session_data *sd,int flag)
  6694. {
  6695. nullpo_retv(sd);
  6696. if (sd->npc_id || sd->npc_shopid) {
  6697. if (sd->state.using_fake_npc) {
  6698. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6699. sd->state.using_fake_npc = 0;
  6700. }
  6701. if (sd->st) {
  6702. if(sd->st->state == RUN){ //wait ending code execution
  6703. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6704. return;
  6705. }
  6706. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6707. sd->st->mes_active = 0;
  6708. }
  6709. sd->state.menu_or_input = 0;
  6710. sd->npc_menu = 0;
  6711. sd->npc_shopid = 0;
  6712. #ifdef SECURE_NPCTIMEOUT
  6713. sd->npc_idle_timer = INVALID_TIMER;
  6714. #endif
  6715. if (sd->st) {
  6716. if (sd->st->state == CLOSE) {
  6717. clif_scriptclose(sd, sd->npc_id);
  6718. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6719. sd->st->state = END; // Force to end now
  6720. }
  6721. if (sd->st->state == END) { // free attached scripts that are waiting
  6722. script_free_state(sd->st);
  6723. sd->st = NULL;
  6724. sd->npc_id = 0;
  6725. }
  6726. }
  6727. }
  6728. }
  6729. /*==========================================
  6730. * Invoked when a player has negative current hp
  6731. *------------------------------------------*/
  6732. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6733. {
  6734. int i=0,k=0;
  6735. unsigned int tick = gettick();
  6736. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6737. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6738. // Super Novices have no kill or die functions attached when saved by their angel
  6739. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6740. unsigned int exp = pc_nextbaseexp(sd);
  6741. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6742. sd->state.snovice_dead_flag = 1;
  6743. pc_setrestartvalue(sd,1);
  6744. status_percent_heal(&sd->bl, 100, 100);
  6745. clif_resurrection(&sd->bl, 1);
  6746. if(battle_config.pc_invincible_time)
  6747. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6748. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6749. if(mapdata_flag_gvg2(mapdata))
  6750. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6751. return 0;
  6752. }
  6753. }
  6754. for(k = 0; k < MAX_DEVOTION; k++) {
  6755. if (sd->devotion[k]){
  6756. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6757. if (devsd)
  6758. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6759. sd->devotion[k] = 0;
  6760. }
  6761. }
  6762. if(sd->shadowform_id) { //if we were target of shadowform
  6763. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6764. sd->shadowform_id = 0; //should be remove on status end anyway
  6765. }
  6766. if(sd->status.pet_id > 0 && sd->pd) {
  6767. struct pet_data *pd = sd->pd;
  6768. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  6769. pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
  6770. if( pd->pet.intimate < 0 )
  6771. pd->pet.intimate = 0;
  6772. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6773. }
  6774. if( sd->pd->target_id ) // Unlock all targets...
  6775. pet_unlocktarget(sd->pd);
  6776. }
  6777. 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)
  6778. hom_vaporize(sd, HOM_ST_ACTIVE);
  6779. if( sd->md )
  6780. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6781. if( sd->ed )
  6782. elemental_delete(sd->ed);
  6783. // Leave duel if you die [LuzZza]
  6784. if(battle_config.duel_autoleave_when_die) {
  6785. if(sd->duel_group > 0)
  6786. duel_leave(sd->duel_group, sd);
  6787. if(sd->duel_invite > 0)
  6788. duel_reject(sd->duel_invite, sd);
  6789. }
  6790. pc_close_npc(sd,2); //close npc if we were using one
  6791. /* e.g. not killed thru pc_damage */
  6792. if( pc_issit(sd) ) {
  6793. clif_status_load(&sd->bl,EFST_SIT,0);
  6794. }
  6795. pc_setdead(sd);
  6796. clif_party_dead( sd );
  6797. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  6798. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6799. //Reset menu skills/item skills
  6800. if ((sd->skillitem) != 0)
  6801. sd->skillitem = sd->skillitemlv = 0;
  6802. if ((sd->menuskill_id) != 0)
  6803. sd->menuskill_id = sd->menuskill_val = 0;
  6804. //Reset ticks.
  6805. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6806. if ( sd->spiritball !=0 )
  6807. pc_delspiritball(sd,sd->spiritball,0);
  6808. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6809. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6810. if (src)
  6811. switch (src->type) {
  6812. case BL_MOB:
  6813. {
  6814. struct mob_data *md=(struct mob_data *)src;
  6815. if(md->target_id==sd->bl.id)
  6816. mob_unlocktarget(md,tick);
  6817. if(battle_config.mobs_level_up && md->status.hp &&
  6818. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6819. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6820. ) { // monster level up [Valaris]
  6821. clif_misceffect(&md->bl,0);
  6822. md->level++;
  6823. status_calc_mob(md, SCO_NONE);
  6824. status_percent_heal(src,10,0);
  6825. if( battle_config.show_mob_info&4 )
  6826. {// update name with new level
  6827. clif_name_area(&md->bl);
  6828. }
  6829. }
  6830. src = battle_get_master(src); // Maybe Player Summon
  6831. }
  6832. break;
  6833. case BL_PET: //Pass on to master...
  6834. case BL_HOM:
  6835. case BL_MER:
  6836. src = battle_get_master(src);
  6837. break;
  6838. }
  6839. if (src && src->type == BL_PC) {
  6840. struct map_session_data *ssd = (struct map_session_data *)src;
  6841. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6842. npc_script_event(ssd, NPCE_KILLPC);
  6843. if (battle_config.pk_mode&2) {
  6844. ssd->status.manner -= 5;
  6845. if(ssd->status.manner < 0)
  6846. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6847. #if 0
  6848. // PK/Karma system code (not enabled yet) [celest]
  6849. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6850. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6851. // karma going down = more 'good' / more honourable.
  6852. // The Karma System way...
  6853. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6854. sd->status.karma--;
  6855. ssd->status.karma--;
  6856. }
  6857. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6858. ssd->status.karma++;
  6859. // or the PK System way...
  6860. if (sd->status.karma > 0) // player killed is dishonourable?
  6861. ssd->status.karma--; // honour points earned
  6862. sd->status.karma++; // honour points lost
  6863. // To-do: Receive exp on certain occasions
  6864. #endif
  6865. }
  6866. }
  6867. if(battle_config.bone_drop==2
  6868. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  6869. {
  6870. struct item item_tmp;
  6871. memset(&item_tmp,0,sizeof(item_tmp));
  6872. item_tmp.nameid=ITEMID_SKULL_;
  6873. item_tmp.identify=1;
  6874. item_tmp.card[0]=CARD0_CREATE;
  6875. item_tmp.card[1]=0;
  6876. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6877. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6878. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6879. }
  6880. //Remove bonus_script when dead
  6881. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6882. // changed penalty options, added death by player if pk_mode [Valaris]
  6883. if(battle_config.death_penalty_type
  6884. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6885. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  6886. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  6887. {
  6888. uint32 base_penalty = 0;
  6889. uint32 job_penalty = 0;
  6890. uint32 zeny_penalty = 0;
  6891. if (pc_isvip(sd)) { // EXP penalty for VIP
  6892. base_penalty = battle_config.vip_exp_penalty_base;
  6893. job_penalty = battle_config.vip_exp_penalty_job;
  6894. zeny_penalty = battle_config.vip_zeny_penalty;
  6895. } else {
  6896. base_penalty = battle_config.death_penalty_base;
  6897. job_penalty = battle_config.death_penalty_job;
  6898. zeny_penalty = battle_config.zeny_penalty;
  6899. }
  6900. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6901. switch (battle_config.death_penalty_type) {
  6902. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6903. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6904. }
  6905. if (base_penalty){ //recheck after altering to speedup
  6906. if (battle_config.pk_mode && src && src->type==BL_PC)
  6907. base_penalty *= 2;
  6908. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6909. }
  6910. }
  6911. else
  6912. base_penalty = 0;
  6913. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6914. switch (battle_config.death_penalty_type) {
  6915. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6916. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6917. }
  6918. if (job_penalty) {
  6919. if (battle_config.pk_mode && src && src->type==BL_PC)
  6920. job_penalty *= 2;
  6921. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6922. }
  6923. }
  6924. else
  6925. job_penalty = 0;
  6926. if (base_penalty || job_penalty)
  6927. pc_lostexp(sd, base_penalty, job_penalty);
  6928. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  6929. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6930. if(zeny_penalty)
  6931. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6932. }
  6933. }
  6934. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6935. for (const auto &it : mapdata->drop_list) {
  6936. int id = it.drop_id, per = it.drop_per;
  6937. enum e_nightmare_drop_type type = it.drop_type;
  6938. if(id == 0)
  6939. continue;
  6940. if(id == -1){
  6941. int eq_num=0,eq_n[MAX_INVENTORY];
  6942. memset(eq_n,0,sizeof(eq_n));
  6943. for(i=0;i<MAX_INVENTORY;i++) {
  6944. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  6945. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  6946. || type&NMDT_ALL)
  6947. {
  6948. int l;
  6949. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6950. if( l < MAX_INVENTORY )
  6951. eq_n[l] = i;
  6952. eq_num++;
  6953. }
  6954. }
  6955. if(eq_num > 0){
  6956. int n = eq_n[rnd()%eq_num];
  6957. if(rnd()%10000 < per) {
  6958. if(sd->inventory.u.items_inventory[n].equip)
  6959. pc_unequipitem(sd,n,3);
  6960. pc_dropitem(sd,n,1);
  6961. }
  6962. }
  6963. }
  6964. else if(id > 0) {
  6965. for(i=0;i<MAX_INVENTORY;i++){
  6966. if(sd->inventory.u.items_inventory[i].nameid == id
  6967. && rnd()%10000 < per
  6968. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  6969. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  6970. || type&NMDT_ALL) ){
  6971. if(sd->inventory.u.items_inventory[i].equip)
  6972. pc_unequipitem(sd,i,3);
  6973. pc_dropitem(sd,i,1);
  6974. break;
  6975. }
  6976. }
  6977. }
  6978. }
  6979. }
  6980. // pvp
  6981. // disable certain pvp functions on pk_mode [Valaris]
  6982. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  6983. sd->pvp_point -= 5;
  6984. sd->pvp_lost++;
  6985. if( src && src->type == BL_PC ) {
  6986. struct map_session_data *ssd = (struct map_session_data *)src;
  6987. ssd->pvp_point++;
  6988. ssd->pvp_won++;
  6989. }
  6990. if( sd->pvp_point < 0 ) {
  6991. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6992. return 1|8;
  6993. }
  6994. }
  6995. //GvG
  6996. if( mapdata_flag_gvg2(mapdata) ) {
  6997. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6998. return 1|8;
  6999. }
  7000. else if( sd->bg_id ) {
  7001. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7002. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7003. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7004. return 1|8;
  7005. }
  7006. }
  7007. //Reset "can log out" tick.
  7008. if( battle_config.prevent_logout )
  7009. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7010. return 1;
  7011. }
  7012. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7013. if(hp) clif_updatestatus(sd,SP_HP);
  7014. if(sp) clif_updatestatus(sd,SP_SP);
  7015. pc_setstand(sd, true);
  7016. if(battle_config.pc_invincible_time > 0)
  7017. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7018. if( sd->state.gmaster_flag ) {
  7019. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7020. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7021. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7022. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7023. }
  7024. }
  7025. // script
  7026. //
  7027. /*==========================================
  7028. * script reading pc status registry
  7029. *------------------------------------------*/
  7030. int pc_readparam(struct map_session_data* sd,int type)
  7031. {
  7032. int val = 0;
  7033. nullpo_ret(sd);
  7034. switch(type) {
  7035. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7036. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7037. case SP_ZENY: val = sd->status.zeny; break;
  7038. case SP_BASELEVEL: val = sd->status.base_level; break;
  7039. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7040. case SP_CLASS: val = sd->status.class_; break;
  7041. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7042. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7043. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7044. case SP_SEX: val = sd->status.sex; break;
  7045. case SP_WEIGHT: val = sd->weight; break;
  7046. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7047. case SP_BASEEXP: val = sd->status.base_exp; break;
  7048. case SP_JOBEXP: val = sd->status.job_exp; break;
  7049. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7050. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7051. case SP_HP: val = sd->battle_status.hp; break;
  7052. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7053. case SP_SP: val = sd->battle_status.sp; break;
  7054. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7055. case SP_STR: val = sd->status.str; break;
  7056. case SP_AGI: val = sd->status.agi; break;
  7057. case SP_VIT: val = sd->status.vit; break;
  7058. case SP_INT: val = sd->status.int_; break;
  7059. case SP_DEX: val = sd->status.dex; break;
  7060. case SP_LUK: val = sd->status.luk; break;
  7061. case SP_KARMA: val = sd->status.karma; break;
  7062. case SP_MANNER: val = sd->status.manner; break;
  7063. case SP_FAME: val = sd->status.fame; break;
  7064. case SP_KILLERRID: val = sd->killerrid; break;
  7065. case SP_KILLEDRID: val = sd->killedrid; break;
  7066. case SP_KILLEDGID: val = sd->killedgid; break;
  7067. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7068. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7069. case SP_CHARRENAME: val = sd->status.rename; break;
  7070. case SP_CHARFONT: val = sd->status.font; break;
  7071. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7072. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7073. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7074. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7075. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7076. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7077. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7078. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7079. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7080. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7081. case SP_BASE_ATK:
  7082. #ifdef RENEWAL
  7083. val = sd->bonus.eatk;
  7084. #else
  7085. val = sd->battle_status.batk;
  7086. #endif
  7087. break;
  7088. case SP_DEF1: val = sd->battle_status.def; break;
  7089. case SP_DEF2: val = sd->battle_status.def2; break;
  7090. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7091. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7092. case SP_HIT: val = sd->battle_status.hit; break;
  7093. case SP_FLEE1: val = sd->battle_status.flee; break;
  7094. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7095. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7096. case SP_MAXHPRATE: val = sd->hprate; break;
  7097. case SP_MAXSPRATE: val = sd->sprate; break;
  7098. case SP_SPRATE: val = sd->dsprate; break;
  7099. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7100. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7101. case SP_ASPD_RATE:
  7102. #ifndef RENEWAL_ASPD
  7103. val = sd->battle_status.aspd_rate;
  7104. #else
  7105. val = sd->battle_status.aspd_rate2;
  7106. #endif
  7107. break;
  7108. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7109. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7110. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7111. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7112. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7113. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7114. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7115. case SP_MATK_RATE: val = sd->matk_rate; break;
  7116. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7117. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7118. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7119. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7120. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7121. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7122. case SP_HIT_RATE: val = sd->hit_rate; break;
  7123. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7124. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7125. case SP_DEF_RATE: val = sd->def_rate; break;
  7126. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7127. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7128. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7129. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7130. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7131. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7132. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7133. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7134. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7135. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7136. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7137. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7138. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7139. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7140. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7141. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7142. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7143. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7144. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7145. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7146. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7147. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7148. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7149. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7150. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7151. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7152. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7153. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7154. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7155. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7156. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7157. case SP_DELAYRATE: val = sd->delayrate; break;
  7158. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7159. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7160. case SP_UNSTRIPABLE:
  7161. case SP_UNSTRIPABLE_ARMOR:
  7162. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7163. break;
  7164. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7165. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7166. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7167. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7168. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7169. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7170. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7171. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7172. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7173. case SP_EMATK: val = sd->bonus.ematk; break;
  7174. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7175. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7176. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7177. case SP_CASTRATE:
  7178. case SP_VARCASTRATE:
  7179. #ifdef RENEWAL_CAST
  7180. val = sd->bonus.varcastrate; break;
  7181. #else
  7182. val = sd->castrate; break;
  7183. #endif
  7184. default:
  7185. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7186. return -1;
  7187. }
  7188. return val;
  7189. }
  7190. /*==========================================
  7191. * script set pc status registry
  7192. *------------------------------------------*/
  7193. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7194. {
  7195. nullpo_retr(false,sd);
  7196. switch(type){
  7197. case SP_BASELEVEL:
  7198. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7199. val = pc_maxbaselv(sd);
  7200. if ((unsigned int)val > sd->status.base_level) {
  7201. int i = 0;
  7202. int stat=0;
  7203. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7204. stat += pc_gets_status_point(sd->status.base_level + i);
  7205. sd->status.status_point += stat;
  7206. }
  7207. sd->status.base_level = (unsigned int)val;
  7208. sd->status.base_exp = 0;
  7209. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7210. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7211. clif_updatestatus(sd, SP_STATUSPOINT);
  7212. clif_updatestatus(sd, SP_BASEEXP);
  7213. status_calc_pc(sd, SCO_FORCE);
  7214. if(sd->status.party_id)
  7215. party_send_levelup(sd);
  7216. break;
  7217. case SP_JOBLEVEL:
  7218. if ((unsigned int)val >= sd->status.job_level) {
  7219. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7220. sd->status.skill_point += val - sd->status.job_level;
  7221. clif_updatestatus(sd, SP_SKILLPOINT);
  7222. }
  7223. sd->status.job_level = (unsigned int)val;
  7224. sd->status.job_exp = 0;
  7225. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7226. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7227. clif_updatestatus(sd, SP_JOBEXP);
  7228. status_calc_pc(sd, SCO_FORCE);
  7229. break;
  7230. case SP_SKILLPOINT:
  7231. sd->status.skill_point = val;
  7232. break;
  7233. case SP_STATUSPOINT:
  7234. sd->status.status_point = val;
  7235. break;
  7236. case SP_ZENY:
  7237. if( val < 0 )
  7238. return false;// can't set negative zeny
  7239. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7240. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7241. break;
  7242. case SP_BASEEXP:
  7243. {
  7244. val = cap_value(val, 0, INT_MAX);
  7245. if ((unsigned int)val < sd->status.base_exp) // Lost
  7246. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7247. else // Gained
  7248. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7249. }
  7250. return true;
  7251. case SP_JOBEXP:
  7252. {
  7253. val = cap_value(val, 0, INT_MAX);
  7254. if ((unsigned int)val < sd->status.job_exp) // Lost
  7255. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7256. else // Gained
  7257. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7258. }
  7259. return true;
  7260. case SP_SEX:
  7261. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7262. break;
  7263. case SP_WEIGHT:
  7264. sd->weight = val;
  7265. break;
  7266. case SP_MAXWEIGHT:
  7267. sd->max_weight = val;
  7268. break;
  7269. case SP_HP:
  7270. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7271. break;
  7272. case SP_MAXHP:
  7273. if (sd->status.base_level < 100)
  7274. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7275. else if (sd->status.base_level < 151)
  7276. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7277. else
  7278. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7279. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7280. {
  7281. sd->battle_status.hp = sd->battle_status.max_hp;
  7282. clif_updatestatus(sd, SP_HP);
  7283. }
  7284. break;
  7285. case SP_SP:
  7286. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7287. break;
  7288. case SP_MAXSP:
  7289. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7290. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7291. {
  7292. sd->battle_status.sp = sd->battle_status.max_sp;
  7293. clif_updatestatus(sd, SP_SP);
  7294. }
  7295. break;
  7296. case SP_STR:
  7297. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7298. break;
  7299. case SP_AGI:
  7300. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7301. break;
  7302. case SP_VIT:
  7303. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7304. break;
  7305. case SP_INT:
  7306. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7307. break;
  7308. case SP_DEX:
  7309. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7310. break;
  7311. case SP_LUK:
  7312. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7313. break;
  7314. case SP_KARMA:
  7315. sd->status.karma = val;
  7316. break;
  7317. case SP_MANNER:
  7318. sd->status.manner = val;
  7319. if( val < 0 )
  7320. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7321. else {
  7322. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7323. clif_manner_message(sd, 5);
  7324. }
  7325. return true; // status_change_start/status_change_end already sends packets warning the client
  7326. case SP_FAME:
  7327. sd->status.fame = val;
  7328. break;
  7329. case SP_KILLERRID:
  7330. sd->killerrid = val;
  7331. return true;
  7332. case SP_KILLEDRID:
  7333. sd->killedrid = val;
  7334. return true;
  7335. case SP_KILLEDGID:
  7336. sd->killedgid = val;
  7337. return true;
  7338. case SP_CHARMOVE:
  7339. sd->status.character_moves = val;
  7340. return true;
  7341. case SP_CHARRENAME:
  7342. sd->status.rename = val;
  7343. return true;
  7344. case SP_CHARFONT:
  7345. sd->status.font = val;
  7346. clif_font(sd);
  7347. return true;
  7348. case SP_BANK_VAULT:
  7349. if (val < 0)
  7350. return false;
  7351. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7352. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7353. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7354. return true;
  7355. case SP_ROULETTE_BRONZE:
  7356. sd->roulette_point.bronze = val;
  7357. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7358. return true;
  7359. case SP_ROULETTE_SILVER:
  7360. sd->roulette_point.silver = val;
  7361. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7362. return true;
  7363. case SP_ROULETTE_GOLD:
  7364. sd->roulette_point.gold = val;
  7365. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7366. return true;
  7367. case SP_CASHPOINTS:
  7368. if (val < 0)
  7369. return false;
  7370. if (!sd->state.connect_new)
  7371. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7372. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7373. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7374. return true;
  7375. case SP_KAFRAPOINTS:
  7376. if (val < 0)
  7377. return false;
  7378. if (!sd->state.connect_new)
  7379. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7380. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7381. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7382. return true;
  7383. case SP_PCDIECOUNTER:
  7384. if (val < 0)
  7385. return false;
  7386. if (sd->die_counter == val)
  7387. return true;
  7388. sd->die_counter = val;
  7389. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7390. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7391. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7392. return true;
  7393. case SP_COOKMASTERY:
  7394. if (val < 0)
  7395. return false;
  7396. if (sd->cook_mastery == val)
  7397. return true;
  7398. val = cap_value(val, 0, 1999);
  7399. sd->cook_mastery = val;
  7400. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7401. return true;
  7402. default:
  7403. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7404. return false;
  7405. }
  7406. clif_updatestatus(sd,type);
  7407. return true;
  7408. }
  7409. /*==========================================
  7410. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7411. *------------------------------------------*/
  7412. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7413. {
  7414. if (type&2) {
  7415. if (hp || type&4)
  7416. clif_heal(sd->fd,SP_HP,hp);
  7417. if (sp)
  7418. clif_heal(sd->fd,SP_SP,sp);
  7419. } else {
  7420. if(hp)
  7421. clif_updatestatus(sd,SP_HP);
  7422. if(sp)
  7423. clif_updatestatus(sd,SP_SP);
  7424. }
  7425. return;
  7426. }
  7427. /**
  7428. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7429. * @param sd: Player data
  7430. * @param itemid: Item ID
  7431. * @param hp: HP to heal
  7432. * @param sp: SP to heal
  7433. * @return Amount healed to an object
  7434. */
  7435. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7436. {
  7437. int bonus, tmp, penalty = 0;
  7438. if (hp) {
  7439. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7440. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7441. if (potion_flag == 2) {
  7442. bonus += 50;
  7443. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7444. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7445. }
  7446. //All item bonuses.
  7447. bonus += sd->bonus.itemhealrate2;
  7448. //Item Group bonuses
  7449. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7450. //Individual item bonuses.
  7451. for(const auto &it : sd->itemhealrate) {
  7452. if (it.id == itemid) {
  7453. bonus += it.val;
  7454. break;
  7455. }
  7456. }
  7457. // Recovery Potion
  7458. if (sd->sc.data[SC_INCHEALRATE])
  7459. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7460. // 2014 Halloween Event : Pumpkin Bonus
  7461. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7462. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7463. tmp = hp * bonus / 100; // Overflow check
  7464. if (bonus != 100 && tmp > hp)
  7465. hp = tmp;
  7466. }
  7467. if (sp) {
  7468. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7469. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7470. if (potion_flag == 2)
  7471. bonus += 50;
  7472. tmp = sp * bonus / 100; // Overflow check
  7473. if (bonus != 100 && tmp > sp)
  7474. sp = tmp;
  7475. }
  7476. if (sd->sc.count) {
  7477. // Critical Wound and Death Hurt stack
  7478. if (sd->sc.data[SC_CRITICALWOUND])
  7479. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7480. if (sd->sc.data[SC_DEATHHURT])
  7481. penalty += 20;
  7482. if (sd->sc.data[SC_NORECOVER_STATE])
  7483. penalty = 100;
  7484. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7485. hp += hp / 2; // 1.5 times
  7486. sp -= sp / 2;
  7487. }
  7488. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7489. hp += hp / 10;
  7490. sp += sp / 10;
  7491. }
  7492. if (penalty > 0) {
  7493. hp -= hp * penalty / 100;
  7494. sp -= sp * penalty / 100;
  7495. }
  7496. #ifdef RENEWAL
  7497. if (sd->sc.data[SC_EXTREMITYFIST2])
  7498. sp = 0;
  7499. #endif
  7500. if (sd->sc.data[SC_BITESCAR])
  7501. hp = 0;
  7502. }
  7503. return status_heal(&sd->bl, hp, sp, 1);
  7504. }
  7505. /*==========================================
  7506. * HP/SP Recovery
  7507. * Heal player hp nad/or sp by rate
  7508. *------------------------------------------*/
  7509. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7510. {
  7511. nullpo_ret(sd);
  7512. if (hp > 100) hp = 100;
  7513. else if (hp <-100) hp = -100;
  7514. if (sp > 100) sp = 100;
  7515. else if (sp <-100) sp = -100;
  7516. if(hp >= 0 && sp >= 0) //Heal
  7517. return status_percent_heal(&sd->bl, hp, sp);
  7518. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7519. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7520. //Crossed signs
  7521. if(hp) {
  7522. if(hp > 0)
  7523. status_percent_heal(&sd->bl, hp, 0);
  7524. else
  7525. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7526. }
  7527. if(sp) {
  7528. if(sp > 0)
  7529. status_percent_heal(&sd->bl, 0, sp);
  7530. else
  7531. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7532. }
  7533. return 0;
  7534. }
  7535. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7536. {
  7537. struct mob_data *md;
  7538. int flag;
  7539. md = (struct mob_data *)bl;
  7540. nullpo_ret(md);
  7541. flag = va_arg(ap, int);
  7542. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7543. status_kill(&md->bl);
  7544. return 1;
  7545. }
  7546. /**
  7547. * Called when player changes job
  7548. * Rewrote to make it tidider [Celest]
  7549. * @param sd
  7550. * @param job JOB ID. See enum e_job
  7551. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7552. * @return True if success, false if failed
  7553. **/
  7554. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7555. {
  7556. int i, fame_flag = 0;
  7557. int b_class;
  7558. nullpo_retr(false,sd);
  7559. if (job < 0)
  7560. return false;
  7561. //Normalize job.
  7562. b_class = pc_jobid2mapid(job);
  7563. if (b_class == -1)
  7564. return false;
  7565. switch (upper) {
  7566. case 1:
  7567. b_class|= JOBL_UPPER;
  7568. break;
  7569. case 2:
  7570. b_class|= JOBL_BABY;
  7571. break;
  7572. }
  7573. //This will automatically adjust bard/dancer classes to the correct gender
  7574. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7575. job = pc_mapid2jobid(b_class, sd->status.sex);
  7576. if (job == -1)
  7577. return false;
  7578. if ((unsigned short)b_class == sd->class_)
  7579. return false; //Nothing to change.
  7580. // changing from 1st to 2nd job
  7581. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7582. sd->change_level_2nd = sd->status.job_level;
  7583. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7584. }
  7585. // changing from 2nd to 3rd job
  7586. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7587. sd->change_level_3rd = sd->status.job_level;
  7588. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7589. }
  7590. if(sd->cloneskill_idx > 0) {
  7591. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7592. sd->status.skill[sd->cloneskill_idx].id = 0;
  7593. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7594. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7595. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7596. }
  7597. sd->cloneskill_idx = 0;
  7598. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7599. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7600. }
  7601. if(sd->reproduceskill_idx > 0) {
  7602. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7603. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7604. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7605. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7606. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7607. }
  7608. sd->reproduceskill_idx = 0;
  7609. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7610. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7611. }
  7612. // Give or reduce transcendent status points
  7613. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7614. sd->status.status_point += battle_config.transcendent_status_points;
  7615. clif_updatestatus(sd,SP_STATUSPOINT);
  7616. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7617. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7618. // The player already used his bonus points, so we have to reset his status points
  7619. pc_resetstate(sd);
  7620. }
  7621. sd->status.status_point -= battle_config.transcendent_status_points;
  7622. clif_updatestatus(sd,SP_STATUSPOINT);
  7623. }
  7624. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7625. const int class_ = pc_class2idx(sd->status.class_);
  7626. uint16 skill_id;
  7627. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7628. //Remove status specific to your current tree skills.
  7629. enum sc_type sc = status_skill2sc(skill_id);
  7630. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7631. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7632. }
  7633. }
  7634. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7635. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7636. pc_resetfeel(sd);
  7637. }
  7638. // Reset body style to 0 before changing job to avoid
  7639. // errors since not every job has a alternate outfit.
  7640. sd->status.body = 0;
  7641. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7642. sd->status.class_ = job;
  7643. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7644. sd->class_ = (unsigned short)b_class;
  7645. sd->status.job_level=1;
  7646. sd->status.job_exp=0;
  7647. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7648. sd->status.base_level = pc_maxbaselv(sd);
  7649. sd->status.base_exp=0;
  7650. pc_resetstate(sd);
  7651. clif_updatestatus(sd,SP_STATUSPOINT);
  7652. clif_updatestatus(sd,SP_BASELEVEL);
  7653. clif_updatestatus(sd,SP_BASEEXP);
  7654. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7655. }
  7656. clif_updatestatus(sd,SP_JOBLEVEL);
  7657. clif_updatestatus(sd,SP_JOBEXP);
  7658. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7659. for(i=0;i<EQI_MAX;i++) {
  7660. if(sd->equip_index[i] >= 0)
  7661. if(pc_isequip(sd,sd->equip_index[i]))
  7662. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7663. }
  7664. //Change look, if disguised, you need to undisguise
  7665. //to correctly calculate new job sprite without
  7666. if (sd->disguise)
  7667. pc_disguise(sd, 0);
  7668. status_set_viewdata(&sd->bl, job);
  7669. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7670. #if PACKETVER >= 20151001
  7671. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7672. #endif
  7673. if(sd->vd.cloth_color)
  7674. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7675. /*
  7676. if(sd->vd.body_style)
  7677. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7678. */
  7679. //Update skill tree.
  7680. pc_calc_skilltree(sd);
  7681. clif_skillinfoblock(sd);
  7682. if (sd->ed)
  7683. elemental_delete(sd->ed);
  7684. if (sd->state.vending)
  7685. vending_closevending(sd);
  7686. if (sd->state.buyingstore)
  7687. buyingstore_close(sd);
  7688. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7689. //Remove peco/cart/falcon
  7690. i = sd->sc.option;
  7691. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7692. i&=~OPTION_RIDING;
  7693. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7694. i&=~OPTION_FALCON;
  7695. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7696. i&=~OPTION_DRAGON;
  7697. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7698. i&=~OPTION_WUGRIDER;
  7699. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7700. i&=~OPTION_WUG;
  7701. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7702. i&=~OPTION_MADOGEAR;
  7703. #ifndef NEW_CARTS
  7704. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7705. i&=~OPTION_CART;
  7706. #else
  7707. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7708. pc_setcart(sd, 0);
  7709. #endif
  7710. if(i != sd->sc.option)
  7711. pc_setoption(sd, i);
  7712. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7713. hom_vaporize(sd, HOM_ST_ACTIVE);
  7714. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7715. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7716. if(sd->status.manner < 0)
  7717. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7718. status_calc_pc(sd,SCO_FORCE);
  7719. pc_checkallowskill(sd);
  7720. pc_equiplookall(sd);
  7721. pc_show_questinfo(sd);
  7722. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7723. if( sd->status.party_id ){
  7724. struct party_data* p;
  7725. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7726. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7727. if( i < MAX_PARTY ){
  7728. p->party.member[i].class_ = sd->status.class_;
  7729. clif_party_job_and_level(sd);
  7730. }
  7731. }
  7732. }
  7733. chrif_save(sd, CSAVE_NORMAL);
  7734. //if you were previously famous, not anymore.
  7735. if (fame_flag)
  7736. chrif_buildfamelist();
  7737. else if (sd->status.fame > 0) {
  7738. //It may be that now they are famous?
  7739. switch (sd->class_&MAPID_UPPERMASK) {
  7740. case MAPID_BLACKSMITH:
  7741. case MAPID_ALCHEMIST:
  7742. case MAPID_TAEKWON:
  7743. chrif_buildfamelist();
  7744. break;
  7745. }
  7746. }
  7747. return true;
  7748. }
  7749. /*==========================================
  7750. * Tell client player sd has change equipement
  7751. *------------------------------------------*/
  7752. void pc_equiplookall(struct map_session_data *sd)
  7753. {
  7754. nullpo_retv(sd);
  7755. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7756. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7757. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7758. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7759. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7760. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7761. }
  7762. /*==========================================
  7763. * Tell client player sd has change look (hair,equip...)
  7764. *------------------------------------------*/
  7765. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7766. nullpo_retv(sd);
  7767. switch(type) {
  7768. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7769. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7770. if (sd->status.hair != val) {
  7771. sd->status.hair = val;
  7772. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7773. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7774. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7775. }
  7776. break;
  7777. case LOOK_WEAPON:
  7778. sd->status.weapon = val;
  7779. break;
  7780. case LOOK_HEAD_BOTTOM:
  7781. sd->status.head_bottom = val;
  7782. sd->setlook_head_bottom = val;
  7783. break;
  7784. case LOOK_HEAD_TOP:
  7785. sd->status.head_top = val;
  7786. sd->setlook_head_top = val;
  7787. break;
  7788. case LOOK_HEAD_MID:
  7789. sd->status.head_mid = val;
  7790. sd->setlook_head_mid = val;
  7791. break;
  7792. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7793. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7794. if (sd->status.hair_color != val) {
  7795. sd->status.hair_color = val;
  7796. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7797. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7798. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7799. }
  7800. break;
  7801. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7802. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7803. sd->status.clothes_color = val;
  7804. break;
  7805. case LOOK_SHIELD:
  7806. sd->status.shield = val;
  7807. break;
  7808. case LOOK_SHOES:
  7809. break;
  7810. case LOOK_ROBE:
  7811. sd->status.robe = val;
  7812. sd->setlook_robe = val;
  7813. break;
  7814. case LOOK_BODY2:
  7815. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7816. sd->status.body = val;
  7817. break;
  7818. }
  7819. clif_changelook(&sd->bl, type, val);
  7820. }
  7821. /*==========================================
  7822. * Give an option (type) to player (sd) and display it to client
  7823. *------------------------------------------*/
  7824. void pc_setoption(struct map_session_data *sd,int type)
  7825. {
  7826. int p_type, new_look=0;
  7827. nullpo_retv(sd);
  7828. p_type = sd->sc.option;
  7829. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7830. sd->sc.option=type;
  7831. clif_changeoption(&sd->bl);
  7832. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7833. { // Mounting
  7834. clif_status_load(&sd->bl,EFST_RIDING,1);
  7835. status_calc_pc(sd,SCO_NONE);
  7836. }
  7837. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7838. { // Dismount
  7839. clif_status_load(&sd->bl,EFST_RIDING,0);
  7840. status_calc_pc(sd,SCO_NONE);
  7841. }
  7842. #ifndef NEW_CARTS
  7843. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7844. clif_cartlist(sd);
  7845. clif_updatestatus(sd, SP_CARTINFO);
  7846. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7847. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7848. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7849. clif_clearcart(sd->fd);
  7850. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7851. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7852. }
  7853. #endif
  7854. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7855. clif_status_load(&sd->bl,EFST_FALCON,1);
  7856. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7857. clif_status_load(&sd->bl,EFST_FALCON,0);
  7858. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7859. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7860. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  7861. status_calc_pc(sd,SCO_NONE);
  7862. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7863. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  7864. status_calc_pc(sd,SCO_NONE);
  7865. }
  7866. }
  7867. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7868. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7869. status_calc_pc(sd,SCO_NONE);
  7870. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7871. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7872. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7873. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7874. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7875. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7876. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7877. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7878. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7879. status_calc_pc(sd,SCO_NONE);
  7880. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7881. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7882. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7883. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7884. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7885. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  7886. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  7887. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  7888. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7889. }
  7890. }
  7891. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7892. new_look = JOB_STAR_GLADIATOR2;
  7893. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7894. new_look = -1;
  7895. if (sd->disguise || !new_look)
  7896. return; //Disguises break sprite changes
  7897. if (new_look < 0) { //Restore normal look.
  7898. status_set_viewdata(&sd->bl, sd->status.class_);
  7899. new_look = sd->vd.class_;
  7900. }
  7901. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7902. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7903. if (sd->vd.cloth_color)
  7904. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7905. if( sd->vd.body_style )
  7906. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7907. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7908. }
  7909. /**
  7910. * Give player a cart
  7911. * @param sd Player
  7912. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  7913. **/
  7914. bool pc_setcart(struct map_session_data *sd,int type) {
  7915. #ifndef NEW_CARTS
  7916. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7917. int option;
  7918. #endif
  7919. nullpo_retr(false,sd);
  7920. if( type < 0 || type > MAX_CARTS )
  7921. return false;// Never trust the values sent by the client! [Skotlex]
  7922. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7923. return false;// Push cart is required
  7924. #ifdef NEW_CARTS
  7925. switch( type ) {
  7926. case 0:
  7927. if( !sd->sc.data[SC_PUSH_CART] )
  7928. return 0;
  7929. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7930. clif_clearcart(sd->fd);
  7931. break;
  7932. default:/* everything else is an allowed ID so we can move on */
  7933. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  7934. clif_cartlist(sd);
  7935. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  7936. }
  7937. clif_updatestatus(sd, SP_CARTINFO);
  7938. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  7939. break;
  7940. }
  7941. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7942. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7943. #else
  7944. // Update option
  7945. option = sd->sc.option;
  7946. option &= ~OPTION_CART;// clear cart bits
  7947. option |= cart[type]; // set cart
  7948. pc_setoption(sd, option);
  7949. #endif
  7950. return true;
  7951. }
  7952. /*==========================================
  7953. * Give player a falcon
  7954. *------------------------------------------*/
  7955. void pc_setfalcon(struct map_session_data* sd, int flag)
  7956. {
  7957. if( flag ){
  7958. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7959. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7960. } else if( pc_isfalcon(sd) ){
  7961. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7962. }
  7963. }
  7964. /*==========================================
  7965. * Set player riding
  7966. *------------------------------------------*/
  7967. void pc_setriding(struct map_session_data* sd, int flag)
  7968. {
  7969. if( sd->sc.data[SC_ALL_RIDING] )
  7970. return;
  7971. if( flag ){
  7972. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7973. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7974. } else if( pc_isriding(sd) ){
  7975. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7976. }
  7977. }
  7978. /*==========================================
  7979. * Give player a mado
  7980. *------------------------------------------*/
  7981. void pc_setmadogear(struct map_session_data* sd, int flag)
  7982. {
  7983. if( flag ){
  7984. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7985. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7986. } else if( pc_ismadogear(sd) ){
  7987. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7988. }
  7989. }
  7990. /*==========================================
  7991. * Check if player can drop an item
  7992. *------------------------------------------*/
  7993. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7994. {
  7995. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7996. return false;
  7997. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7998. return false;
  7999. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8000. }
  8001. /**
  8002. * Determines whether a player can attack based on status changes
  8003. * Why not use status_check_skilluse?
  8004. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8005. * Even ground-based attacks should be blocked by these statuses
  8006. * Called from unit_attack and unit_attack_timer_sub
  8007. * @retval true Can attack
  8008. **/
  8009. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8010. nullpo_retr(false, sd);
  8011. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8012. return false;
  8013. if( sd->sc.data[SC_BASILICA] ||
  8014. sd->sc.data[SC__SHADOWFORM] ||
  8015. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8016. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8017. sd->sc.data[SC_CRYSTALIZE] ||
  8018. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8019. sd->sc.data[SC_TRICKDEAD] ||
  8020. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8021. sd->sc.data[SC_BLADESTOP] ||
  8022. sd->sc.data[SC_DEEPSLEEP] ||
  8023. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8024. sd->sc.data[SC_KINGS_GRACE] )
  8025. return false;
  8026. return true;
  8027. }
  8028. /*==========================================
  8029. * Read '@type' variables (temporary numeric char reg)
  8030. *------------------------------------------*/
  8031. int pc_readreg(struct map_session_data* sd, int64 reg)
  8032. {
  8033. return i64db_iget(sd->regs.vars, reg);
  8034. }
  8035. /*==========================================
  8036. * Set '@type' variables (temporary numeric char reg)
  8037. *------------------------------------------*/
  8038. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8039. {
  8040. unsigned int index = script_getvaridx(reg);
  8041. nullpo_retr(false, sd);
  8042. if( val ) {
  8043. i64db_iput(sd->regs.vars, reg, val);
  8044. if( index )
  8045. script_array_update(&sd->regs, reg, false);
  8046. } else {
  8047. i64db_remove(sd->regs.vars, reg);
  8048. if( index )
  8049. script_array_update(&sd->regs, reg, true);
  8050. }
  8051. return true;
  8052. }
  8053. /*==========================================
  8054. * Read '@type$' variables (temporary string char reg)
  8055. *------------------------------------------*/
  8056. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8057. {
  8058. struct script_reg_str *p = NULL;
  8059. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8060. return p ? p->value : NULL;
  8061. }
  8062. /*==========================================
  8063. * Set '@type$' variables (temporary string char reg)
  8064. *------------------------------------------*/
  8065. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8066. {
  8067. struct script_reg_str *p = NULL;
  8068. unsigned int index = script_getvaridx(reg);
  8069. DBData prev;
  8070. nullpo_retr(false, sd);
  8071. if( str[0] ) {
  8072. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8073. p->value = aStrdup(str);
  8074. p->flag.type = 1;
  8075. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8076. p = (struct script_reg_str *)db_data2ptr(&prev);
  8077. if( p->value )
  8078. aFree(p->value);
  8079. ers_free(str_reg_ers, p);
  8080. } else {
  8081. if( index )
  8082. script_array_update(&sd->regs, reg, false);
  8083. }
  8084. } else {
  8085. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8086. p = (struct script_reg_str *)db_data2ptr(&prev);
  8087. if( p->value )
  8088. aFree(p->value);
  8089. ers_free(str_reg_ers, p);
  8090. if( index )
  8091. script_array_update(&sd->regs, reg, true);
  8092. }
  8093. }
  8094. return true;
  8095. }
  8096. /**
  8097. * Serves the following variable types:
  8098. * - 'type' (permanent numeric char reg)
  8099. * - '#type' (permanent numeric account reg)
  8100. * - '##type' (permanent numeric account reg2)
  8101. **/
  8102. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8103. {
  8104. struct script_reg_num *p = NULL;
  8105. if (!sd->vars_ok) {
  8106. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8107. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8108. //intif->request_registry(sd,type==3?4:type);
  8109. set_eof(sd->fd);
  8110. return 0;
  8111. }
  8112. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8113. return p ? p->value : 0;
  8114. }
  8115. /**
  8116. * Serves the following variable types:
  8117. * - 'type$' (permanent str char reg)
  8118. * - '#type$' (permanent str account reg)
  8119. * - '##type$' (permanent str account reg2)
  8120. **/
  8121. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8122. {
  8123. struct script_reg_str *p = NULL;
  8124. if (!sd->vars_ok) {
  8125. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8126. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8127. //intif->request_registry(sd,type==3?4:type);
  8128. set_eof(sd->fd);
  8129. return NULL;
  8130. }
  8131. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8132. return p ? p->value : NULL;
  8133. }
  8134. /**
  8135. * Serves the following variable types:
  8136. * - 'type' (permanent numeric char reg)
  8137. * - '#type' (permanent numeric account reg)
  8138. * - '##type' (permanent numeric account reg2)
  8139. **/
  8140. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8141. {
  8142. struct script_reg_num *p = NULL;
  8143. const char *regname = get_str(script_getvarid(reg));
  8144. unsigned int index = script_getvaridx(reg);
  8145. if ( !reg_load && !sd->vars_ok ) {
  8146. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8147. return 0;
  8148. }
  8149. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8150. if( val ) {
  8151. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8152. script_array_update(&sd->regs, reg, false);
  8153. p->value = val;
  8154. } else {
  8155. p->value = 0;
  8156. if( index )
  8157. script_array_update(&sd->regs, reg, true);
  8158. }
  8159. if (!reg_load)
  8160. p->flag.update = 1;/* either way, it will require either delete or replace */
  8161. } else if( val ) {
  8162. DBData prev;
  8163. if( index )
  8164. script_array_update(&sd->regs, reg, false);
  8165. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8166. p->value = val;
  8167. if (!reg_load)
  8168. p->flag.update = 1;
  8169. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8170. p = (struct script_reg_num *)db_data2ptr(&prev);
  8171. ers_free(num_reg_ers, p);
  8172. }
  8173. }
  8174. if (!reg_load && p)
  8175. sd->vars_dirty = true;
  8176. return 1;
  8177. }
  8178. /**
  8179. * Serves the following variable types:
  8180. * - 'type$' (permanent str char reg)
  8181. * - '#type$' (permanent str account reg)
  8182. * - '##type$' (permanent str account reg2)
  8183. **/
  8184. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8185. {
  8186. struct script_reg_str *p = NULL;
  8187. const char *regname = get_str(script_getvarid(reg));
  8188. unsigned int index = script_getvaridx(reg);
  8189. size_t vlen = 0;
  8190. if (!reg_load && !sd->vars_ok) {
  8191. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8192. return 0;
  8193. }
  8194. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8195. {
  8196. 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);
  8197. return 0;
  8198. }
  8199. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8200. if( val[0] ) {
  8201. if( p->value )
  8202. aFree(p->value);
  8203. else if ( index ) // an entry that was deleted, so we reset
  8204. script_array_update(&sd->regs, reg, false);
  8205. p->value = aStrdup(val);
  8206. } else {
  8207. if (p->value)
  8208. aFree(p->value);
  8209. p->value = NULL;
  8210. if( index )
  8211. script_array_update(&sd->regs, reg, true);
  8212. }
  8213. if( !reg_load )
  8214. p->flag.update = 1; // either way, it will require either delete or replace
  8215. } else if( val[0] ) {
  8216. DBData prev;
  8217. if( index )
  8218. script_array_update(&sd->regs, reg, false);
  8219. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8220. p->value = aStrdup(val);
  8221. if( !reg_load )
  8222. p->flag.update = 1;
  8223. p->flag.type = 1;
  8224. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8225. p = (struct script_reg_str *)db_data2ptr(&prev);
  8226. if( p->value )
  8227. aFree(p->value);
  8228. ers_free(str_reg_ers, p);
  8229. }
  8230. }
  8231. if( !reg_load && p )
  8232. sd->vars_dirty = true;
  8233. return 1;
  8234. }
  8235. /**
  8236. * Set value of player variable
  8237. * @param sd Player
  8238. * @param reg Variable name
  8239. * @param value
  8240. * @return True if success, false if failed.
  8241. **/
  8242. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8243. char prefix = reg[0];
  8244. nullpo_retr(false, sd);
  8245. if (reg[strlen(reg)-1] == '$') {
  8246. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8247. return false;
  8248. }
  8249. val = cap_value(val, INT_MIN, INT_MAX);
  8250. switch (prefix) {
  8251. case '.':
  8252. case '\'':
  8253. case '$':
  8254. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8255. return false;
  8256. case '@':
  8257. return pc_setreg(sd, add_str(reg), val);
  8258. case '#':
  8259. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8260. default:
  8261. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8262. }
  8263. return false;
  8264. }
  8265. /**
  8266. * Get value of player variable
  8267. * @param sd Player
  8268. * @param reg Variable name
  8269. * @return Variable value or 0 if failed.
  8270. **/
  8271. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8272. char prefix = reg[0];
  8273. nullpo_ret(sd);
  8274. if (reg[strlen(reg)-1] == '$') {
  8275. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8276. return 0;
  8277. }
  8278. switch (prefix) {
  8279. case '.':
  8280. case '\'':
  8281. case '$':
  8282. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8283. return 0;
  8284. case '@':
  8285. return pc_readreg(sd, add_str(reg));
  8286. case '#':
  8287. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8288. default:
  8289. return pc_readglobalreg(sd, add_str(reg));
  8290. }
  8291. return 0;
  8292. }
  8293. /*==========================================
  8294. * Exec eventtimer for player sd (retrieved from map_session (id))
  8295. *------------------------------------------*/
  8296. static TIMER_FUNC(pc_eventtimer){
  8297. struct map_session_data *sd=map_id2sd(id);
  8298. char *p = (char *)data;
  8299. int i;
  8300. if(sd==NULL)
  8301. return 0;
  8302. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8303. if( i < MAX_EVENTTIMER )
  8304. {
  8305. sd->eventtimer[i] = INVALID_TIMER;
  8306. sd->eventcount--;
  8307. npc_event(sd,p,0);
  8308. }
  8309. else
  8310. ShowError("pc_eventtimer: no such event timer\n");
  8311. if (p) aFree(p);
  8312. return 0;
  8313. }
  8314. /*==========================================
  8315. * Add eventtimer for player sd ?
  8316. *------------------------------------------*/
  8317. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8318. {
  8319. int i;
  8320. nullpo_retr(false,sd);
  8321. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8322. if( i == MAX_EVENTTIMER )
  8323. return false;
  8324. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8325. sd->eventcount++;
  8326. return true;
  8327. }
  8328. /*==========================================
  8329. * Del eventtimer for player sd ?
  8330. *------------------------------------------*/
  8331. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8332. {
  8333. char* p = NULL;
  8334. int i;
  8335. nullpo_retr(false,sd);
  8336. if (sd->eventcount == 0)
  8337. return false;
  8338. // find the named event timer
  8339. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8340. sd->eventtimer[i] != INVALID_TIMER &&
  8341. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8342. strcmp(p, name) == 0
  8343. );
  8344. if( i == MAX_EVENTTIMER )
  8345. return false; // not found
  8346. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8347. sd->eventtimer[i] = INVALID_TIMER;
  8348. if(sd->eventcount > 0)
  8349. sd->eventcount--;
  8350. aFree(p);
  8351. return true;
  8352. }
  8353. /*==========================================
  8354. * Update eventtimer count for player sd
  8355. *------------------------------------------*/
  8356. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8357. {
  8358. int i;
  8359. nullpo_retv(sd);
  8360. for(i=0;i<MAX_EVENTTIMER;i++)
  8361. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8362. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8363. addtick_timer(sd->eventtimer[i],tick);
  8364. break;
  8365. }
  8366. }
  8367. /*==========================================
  8368. * Remove all eventtimer for player sd
  8369. *------------------------------------------*/
  8370. void pc_cleareventtimer(struct map_session_data *sd)
  8371. {
  8372. int i;
  8373. nullpo_retv(sd);
  8374. if (sd->eventcount == 0)
  8375. return;
  8376. for(i=0;i<MAX_EVENTTIMER;i++){
  8377. if( sd->eventtimer[i] != INVALID_TIMER ){
  8378. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8379. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8380. sd->eventtimer[i] = INVALID_TIMER;
  8381. if(sd->eventcount > 0) //avoid looping to max val
  8382. sd->eventcount--;
  8383. if (p) aFree(p);
  8384. }
  8385. }
  8386. }
  8387. /**
  8388. * Called when an item with combo is worn
  8389. * @param *sd
  8390. * @param *data struct item_data
  8391. * @return success numbers of succeed combo
  8392. */
  8393. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8394. uint16 i;
  8395. int success = 0;
  8396. for( i = 0; i < data->combos_count; i++ ) {
  8397. struct itemchk {
  8398. int idx;
  8399. unsigned short nameid;
  8400. short card[MAX_SLOTS];
  8401. } *combo_idx;
  8402. int idx, j;
  8403. int nb_itemCombo;
  8404. unsigned int pos = 0;
  8405. /* ensure this isn't a duplicate combo */
  8406. if( sd->combos.bonus != NULL ) {
  8407. int x;
  8408. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8409. /* found a match, skip this combo */
  8410. if( x < sd->combos.count )
  8411. continue;
  8412. }
  8413. nb_itemCombo = data->combos[i]->count;
  8414. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8415. continue;
  8416. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8417. for(j=0; j < nb_itemCombo; j++){
  8418. combo_idx[j].idx=-1;
  8419. combo_idx[j].nameid=-1;
  8420. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8421. }
  8422. for( j = 0; j < nb_itemCombo; j++ ) {
  8423. uint16 id = data->combos[i]->nameid[j], k;
  8424. bool found = false;
  8425. for( k = 0; k < EQI_MAX; k++ ) {
  8426. short index = sd->equip_index[k];
  8427. if( index < 0 )
  8428. continue;
  8429. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8430. continue;
  8431. if (!sd->inventory_data[index] )
  8432. continue;
  8433. if ( itemdb_type(id) != IT_CARD ) {
  8434. if ( sd->inventory_data[index]->nameid != id )
  8435. continue;
  8436. if(j>0){ //check if this item not already used
  8437. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8438. uint8 z;
  8439. for (z = 0; z < nb_itemCombo-1; z++)
  8440. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8441. do_continue=true;
  8442. if(do_continue)
  8443. continue;
  8444. }
  8445. combo_idx[j].nameid = id;
  8446. combo_idx[j].idx = index;
  8447. pos |= sd->inventory.u.items_inventory[index].equip;
  8448. found = true;
  8449. break;
  8450. } else { //Cards and enchants
  8451. uint16 z;
  8452. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8453. continue;
  8454. for (z = 0; z < MAX_SLOTS; z++) {
  8455. bool do_continue=false;
  8456. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8457. continue;
  8458. if(j>0){
  8459. int c1, c2;
  8460. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8461. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8462. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8463. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8464. do_continue = true;
  8465. break;
  8466. }
  8467. }
  8468. }
  8469. }
  8470. }
  8471. if(do_continue)
  8472. continue;
  8473. combo_idx[j].nameid = id;
  8474. combo_idx[j].idx = index;
  8475. combo_idx[j].card[z] = id;
  8476. pos |= sd->inventory.u.items_inventory[index].equip;
  8477. found = true;
  8478. break;
  8479. }
  8480. }
  8481. }
  8482. if( !found )
  8483. break;/* we haven't found all the ids for this combo, so we can return */
  8484. }
  8485. aFree(combo_idx);
  8486. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8487. if( j < nb_itemCombo )
  8488. continue;
  8489. /* we got here, means all items in the combo are matching */
  8490. idx = sd->combos.count;
  8491. if( sd->combos.bonus == NULL ) {
  8492. CREATE(sd->combos.bonus, struct script_code *, 1);
  8493. CREATE(sd->combos.id, unsigned short, 1);
  8494. CREATE(sd->combos.pos, unsigned int, 1);
  8495. sd->combos.count = 1;
  8496. } else {
  8497. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8498. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8499. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8500. }
  8501. /* we simply copy the pointer */
  8502. sd->combos.bonus[idx] = data->combos[i]->script;
  8503. /* save this combo's id */
  8504. sd->combos.id[idx] = data->combos[i]->id;
  8505. /* save pos of combo*/
  8506. sd->combos.pos[idx] = pos;
  8507. success++;
  8508. }
  8509. return success;
  8510. }
  8511. /**
  8512. * Called when an item with combo is removed
  8513. * @param *sd
  8514. * @param *data struct item_data
  8515. * @return retval numbers of removed combo
  8516. */
  8517. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8518. int i, retval = 0;
  8519. if( sd->combos.bonus == NULL )
  8520. return 0;/* nothing to do here, player has no combos */
  8521. for( i = 0; i < data->combos_count; i++ ) {
  8522. /* check if this combo exists in this user */
  8523. int x = 0, cursor = 0, j;
  8524. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8525. /* no match, skip this combo */
  8526. if(x >= sd->combos.count)
  8527. continue;
  8528. sd->combos.bonus[x] = NULL;
  8529. sd->combos.id[x] = 0;
  8530. sd->combos.pos[x] = 0;
  8531. retval++;
  8532. /* check if combo requirements still fit */
  8533. if( pc_checkcombo( sd, data ) )
  8534. continue;
  8535. /* move next value to empty slot */
  8536. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8537. if( sd->combos.bonus[j] == NULL )
  8538. continue;
  8539. if( cursor != j ) {
  8540. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8541. sd->combos.id[cursor] = sd->combos.id[j];
  8542. sd->combos.pos[cursor] = sd->combos.pos[j];
  8543. }
  8544. cursor++;
  8545. }
  8546. /* it's empty, we can clear all the memory */
  8547. if( (sd->combos.count = cursor) == 0 ) {
  8548. aFree(sd->combos.bonus);
  8549. aFree(sd->combos.id);
  8550. aFree(sd->combos.pos);
  8551. sd->combos.bonus = NULL;
  8552. sd->combos.id = NULL;
  8553. sd->combos.pos = NULL;
  8554. return retval; /* we also can return at this point for we have no more combos to check */
  8555. }
  8556. }
  8557. return retval;
  8558. }
  8559. /**
  8560. * Load combo data(s) of player
  8561. * @param *sd
  8562. * @return ret numbers of succeed combo
  8563. */
  8564. int pc_load_combo(struct map_session_data *sd) {
  8565. int i, ret = 0;
  8566. for( i = 0; i < EQI_MAX; i++ ) {
  8567. struct item_data *id = NULL;
  8568. short idx = sd->equip_index[i];
  8569. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8570. continue;
  8571. if( id->combos_count )
  8572. ret += pc_checkcombo(sd,id);
  8573. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8574. struct item_data *data;
  8575. int j;
  8576. for( j = 0; j < id->slot; j++ ) {
  8577. if (!sd->inventory.u.items_inventory[idx].card[j])
  8578. continue;
  8579. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8580. if( data->combos_count )
  8581. ret += pc_checkcombo(sd,data);
  8582. }
  8583. }
  8584. }
  8585. }
  8586. return ret;
  8587. }
  8588. /*==========================================
  8589. * Equip item on player sd at req_pos from inventory index n
  8590. * return: false - fail; true - success
  8591. *------------------------------------------*/
  8592. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8593. {
  8594. int i, pos, flag = 0, iflag;
  8595. struct item_data *id;
  8596. uint8 res = ITEM_EQUIP_ACK_OK;
  8597. nullpo_retr(false,sd);
  8598. if( n < 0 || n >= MAX_INVENTORY ) {
  8599. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8600. return false;
  8601. }
  8602. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8603. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8604. return false;
  8605. }
  8606. if (!(id = sd->inventory_data[n]))
  8607. return false;
  8608. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8609. if(battle_config.battle_log)
  8610. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8611. if((res = pc_isequip(sd,n))) {
  8612. clif_equipitemack(sd,n,0,res); // fail
  8613. return false;
  8614. }
  8615. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8616. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8617. return false;
  8618. }
  8619. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8620. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8621. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8622. return false;
  8623. }
  8624. if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8625. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8626. clif_notify_bindOnEquip(sd,n);
  8627. }
  8628. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8629. pos = req_pos&EQP_ACC;
  8630. if (pos == EQP_ACC) //User specified both slots..
  8631. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8632. }
  8633. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8634. pos = (req_pos&EQP_ARMS);
  8635. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8636. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8637. }
  8638. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8639. pos = req_pos&EQP_SHADOW_ACC;
  8640. if (pos == EQP_SHADOW_ACC)
  8641. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8642. }
  8643. if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8644. pos = (req_pos&EQP_SHADOW_ARMS);
  8645. if( pos == EQP_SHADOW_ARMS )
  8646. pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8647. }
  8648. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8649. //Update skill-block range database when weapon range changes. [Skotlex]
  8650. i = sd->equip_index[EQI_HAND_R];
  8651. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8652. flag = 1;
  8653. else
  8654. flag = id->range != sd->inventory_data[i]->range;
  8655. }
  8656. for(i=0;i<EQI_MAX;i++) {
  8657. if(pos & equip_bitmask[i]) {
  8658. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8659. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8660. sd->equip_index[i] = n;
  8661. }
  8662. }
  8663. if(pos==EQP_AMMO) {
  8664. clif_arrowequip(sd,n);
  8665. clif_arrow_fail(sd,3);
  8666. }
  8667. else
  8668. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8669. sd->inventory.u.items_inventory[n].equip = pos;
  8670. if(pos & EQP_HAND_R) {
  8671. if(id)
  8672. sd->weapontype1 = id->look;
  8673. else
  8674. sd->weapontype1 = 0;
  8675. pc_calcweapontype(sd);
  8676. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8677. }
  8678. if(pos & EQP_HAND_L) {
  8679. if(id) {
  8680. if(id->type == IT_WEAPON) {
  8681. sd->status.shield = 0;
  8682. sd->weapontype2 = id->look;
  8683. }
  8684. else
  8685. if(id->type == IT_ARMOR) {
  8686. sd->status.shield = id->look;
  8687. sd->weapontype2 = 0;
  8688. }
  8689. }
  8690. else
  8691. sd->status.shield = sd->weapontype2 = 0;
  8692. pc_calcweapontype(sd);
  8693. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8694. }
  8695. if(pos & EQP_SHOES)
  8696. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8697. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  8698. short idx = sd->equip_index[EQI_AMMO];
  8699. if (idx >= 0) {
  8700. switch (sd->inventory_data[idx]->look) {
  8701. case AMMO_ARROW:
  8702. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  8703. pc_unequipitem(sd, idx, 2 | 4);
  8704. break;
  8705. case AMMO_BULLET:
  8706. case AMMO_SHELL:
  8707. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  8708. #ifdef RENEWAL
  8709. && id->look != W_GRENADE
  8710. #endif
  8711. )
  8712. pc_unequipitem(sd, idx, 2 | 4);
  8713. break;
  8714. #ifndef RENEWAL
  8715. case AMMO_GRENADE:
  8716. if (id->look != W_GRENADE)
  8717. pc_unequipitem(sd, idx, 2 | 4);
  8718. break;
  8719. #endif
  8720. }
  8721. }
  8722. }
  8723. pc_set_costume_view(sd);
  8724. pc_checkallowskill(sd); //Check if status changes should be halted.
  8725. iflag = sd->npc_item_flag;
  8726. /* check for combos (MUST be before status_calc_pc) */
  8727. if( id->combos_count )
  8728. pc_checkcombo(sd,id);
  8729. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8730. ; //No cards
  8731. else {
  8732. for( i = 0; i < MAX_SLOTS; i++ ) {
  8733. struct item_data *data;
  8734. if (!sd->inventory.u.items_inventory[n].card[i])
  8735. continue;
  8736. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8737. if( data->combos_count )
  8738. pc_checkcombo(sd,data);
  8739. }
  8740. }
  8741. }
  8742. status_calc_pc(sd,SCO_NONE);
  8743. if (flag) //Update skill data
  8744. clif_skillinfoblock(sd);
  8745. //OnEquip script [Skotlex]
  8746. if (id) {
  8747. //only run the script if item isn't restricted
  8748. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8749. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8750. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8751. ; //No cards
  8752. else {
  8753. for( i = 0; i < MAX_SLOTS; i++ ) {
  8754. struct item_data *data;
  8755. if (!sd->inventory.u.items_inventory[n].card[i])
  8756. continue;
  8757. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  8758. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8759. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8760. }
  8761. }
  8762. }
  8763. }
  8764. sd->npc_item_flag = iflag;
  8765. return true;
  8766. }
  8767. /**
  8768. * Recalculate player status on unequip
  8769. * @param sd: Player data
  8770. * @param n: Item inventory index
  8771. * @param flag: Whether to recalculate a player's status or not
  8772. * @return True on success or false on failure
  8773. */
  8774. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  8775. int i, iflag;
  8776. bool status_calc = false;
  8777. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  8778. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  8779. sd->inventory.u.items_inventory[n].equip = 0;
  8780. pc_checkallowskill(sd);
  8781. iflag = sd->npc_item_flag;
  8782. /* check for combos (MUST be before status_calc_pc) */
  8783. if (sd->inventory_data[n]) {
  8784. if (sd->inventory_data[n]->combos_count) {
  8785. if (pc_removecombo(sd, sd->inventory_data[n]))
  8786. status_calc = true;
  8787. }
  8788. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8789. ; //No cards
  8790. else {
  8791. for (i = 0; i < MAX_SLOTS; i++) {
  8792. struct item_data *data;
  8793. if (!sd->inventory.u.items_inventory[n].card[i])
  8794. continue;
  8795. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  8796. if (data->combos_count) {
  8797. if (pc_removecombo(sd, data))
  8798. status_calc = true;
  8799. }
  8800. }
  8801. }
  8802. }
  8803. }
  8804. if (status_calc)
  8805. status_calc_pc(sd, SCO_NONE);
  8806. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  8807. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8808. //OnUnEquip script [Skotlex]
  8809. if (sd->inventory_data[n]) {
  8810. if (sd->inventory_data[n]->unequip_script)
  8811. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  8812. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  8813. ; //No cards
  8814. else {
  8815. for (i = 0; i < MAX_SLOTS; i++) {
  8816. struct item_data *data;
  8817. if (!sd->inventory.u.items_inventory[n].card[i])
  8818. continue;
  8819. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  8820. if (data->unequip_script)
  8821. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  8822. }
  8823. }
  8824. }
  8825. }
  8826. if (flag & 1)
  8827. status_calc_pc(sd, SCO_FORCE);
  8828. sd->npc_item_flag = iflag;
  8829. }
  8830. /**
  8831. * Called when attempting to unequip an item from a player
  8832. * @param sd: Player data
  8833. * @param n: Item inventory index
  8834. * @param flag: Type of unequip
  8835. * 0 - only unequip
  8836. * 1 - calculate status after unequipping
  8837. * 2 - force unequip
  8838. * 4 - unequip by switching equipment
  8839. * @return True on success or false on failure
  8840. */
  8841. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8842. int i, pos;
  8843. nullpo_retr(false,sd);
  8844. if (n < 0 || n >= MAX_INVENTORY) {
  8845. clif_unequipitemack(sd,0,0,0);
  8846. return false;
  8847. }
  8848. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  8849. clif_unequipitemack(sd,n,0,0);
  8850. return false; //Nothing to unequip
  8851. }
  8852. // status change that makes player cannot unequip equipment
  8853. if (!(flag&2) && sd->sc.count &&
  8854. (sd->sc.data[SC_BERSERK] ||
  8855. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8856. sd->sc.data[SC__BLOODYLUST] ||
  8857. sd->sc.data[SC_KYOUGAKU] ||
  8858. (sd->sc.data[SC_PYROCLASTIC] &&
  8859. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8860. {
  8861. clif_unequipitemack(sd,n,0,0);
  8862. return false;
  8863. }
  8864. if (battle_config.battle_log)
  8865. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  8866. for(i = 0; i < EQI_MAX; i++) {
  8867. if (pos & equip_bitmask[i])
  8868. sd->equip_index[i] = -1;
  8869. }
  8870. if(pos & EQP_HAND_R) {
  8871. sd->weapontype1 = 0;
  8872. sd->status.weapon = sd->weapontype2;
  8873. pc_calcweapontype(sd);
  8874. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8875. if( !battle_config.dancing_weaponswitch_fix )
  8876. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8877. #ifdef RENEWAL
  8878. if (battle_config.switch_remove_edp&2) {
  8879. #else
  8880. if (battle_config.switch_remove_edp&1) {
  8881. #endif
  8882. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  8883. }
  8884. }
  8885. if(pos & EQP_HAND_L) {
  8886. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  8887. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  8888. sd->status.shield = sd->weapontype2 = 0;
  8889. pc_calcweapontype(sd);
  8890. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8891. }
  8892. if(pos & EQP_SHOES)
  8893. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8894. clif_unequipitemack(sd,n,pos,1);
  8895. pc_set_costume_view(sd);
  8896. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8897. // On weapon change (right and left hand)
  8898. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8899. if (battle_config.ammo_unequip && !(flag & 4)) {
  8900. switch (sd->inventory_data[n]->look) {
  8901. case W_BOW:
  8902. case W_MUSICAL:
  8903. case W_WHIP:
  8904. case W_REVOLVER:
  8905. case W_RIFLE:
  8906. case W_GATLING:
  8907. case W_SHOTGUN:
  8908. case W_GRENADE: {
  8909. short idx = sd->equip_index[EQI_AMMO];
  8910. if (idx >= 0) {
  8911. sd->equip_index[EQI_AMMO] = -1;
  8912. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  8913. pc_unequipitem_sub(sd, idx, 0);
  8914. }
  8915. }
  8916. break;
  8917. }
  8918. }
  8919. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8920. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8921. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8922. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8923. }
  8924. // On armor change
  8925. if (pos & EQP_ARMOR) {
  8926. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8927. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8928. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8929. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8930. }
  8931. // On ammo change
  8932. 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_))
  8933. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8934. pc_unequipitem_sub(sd, n, flag);
  8935. return true;
  8936. }
  8937. /*==========================================
  8938. * Checking if player (sd) has an invalid item
  8939. * and is unequiped on map load (item_noequip)
  8940. *------------------------------------------*/
  8941. void pc_checkitem(struct map_session_data *sd) {
  8942. int i, calc_flag = 0;
  8943. struct item it;
  8944. nullpo_retv(sd);
  8945. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8946. return;
  8947. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  8948. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8949. it = sd->inventory.u.items_inventory[i];
  8950. if( it.nameid == 0 )
  8951. continue;
  8952. if( !it.equip )
  8953. continue;
  8954. if( it.equip&~pc_equippoint(sd,i) ) {
  8955. pc_unequipitem(sd, i, 2);
  8956. calc_flag = 1;
  8957. continue;
  8958. }
  8959. 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) ) {
  8960. pc_unequipitem(sd, i, 2);
  8961. calc_flag = 1;
  8962. continue;
  8963. }
  8964. }
  8965. if( calc_flag && sd->state.active ) {
  8966. pc_checkallowskill(sd);
  8967. status_calc_pc(sd,SCO_NONE);
  8968. }
  8969. }
  8970. /*==========================================
  8971. * Checks for unavailable items and removes them.
  8972. * @param sd: Player data
  8973. * @param type Forced check:
  8974. * 1 - Inventory
  8975. * 2 - Cart
  8976. * 4 - Storage
  8977. *------------------------------------------*/
  8978. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  8979. {
  8980. int i;
  8981. unsigned short nameid;
  8982. char output[256];
  8983. nullpo_retv(sd);
  8984. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  8985. for(i = 0; i < MAX_INVENTORY; i++) {
  8986. nameid = sd->inventory.u.items_inventory[i].nameid;
  8987. if (!nameid)
  8988. continue;
  8989. if (!itemdb_available(nameid)) {
  8990. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8991. clif_displaymessage(sd->fd, output);
  8992. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  8993. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  8994. continue;
  8995. }
  8996. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  8997. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  8998. }
  8999. }
  9000. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9001. for(i = 0; i < MAX_CART; i++) {
  9002. nameid = sd->cart.u.items_cart[i].nameid;
  9003. if (!nameid)
  9004. continue;
  9005. if (!itemdb_available(nameid)) {
  9006. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9007. clif_displaymessage(sd->fd, output);
  9008. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  9009. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9010. continue;
  9011. }
  9012. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9013. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9014. }
  9015. }
  9016. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9017. for(i = 0; i < sd->storage.max_amount; i++) {
  9018. nameid = sd->storage.u.items_storage[i].nameid;
  9019. if (!nameid)
  9020. continue;
  9021. if (!itemdb_available(nameid)) {
  9022. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9023. clif_displaymessage(sd->fd, output);
  9024. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  9025. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9026. continue;
  9027. }
  9028. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9029. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9030. }
  9031. }
  9032. }
  9033. /*==========================================
  9034. * Update PVP rank for sd1 in cmp to sd2
  9035. *------------------------------------------*/
  9036. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9037. {
  9038. struct map_session_data *sd1,*sd2;
  9039. sd1=(struct map_session_data *)bl;
  9040. sd2=va_arg(ap,struct map_session_data *);
  9041. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9042. {// cannot register pvp rank for hidden GMs
  9043. return 0;
  9044. }
  9045. if( sd1->pvp_point > sd2->pvp_point )
  9046. sd2->pvp_rank++;
  9047. return 0;
  9048. }
  9049. /*==========================================
  9050. * Calculate new rank beetween all present players (map_foreachinallarea)
  9051. * and display result
  9052. *------------------------------------------*/
  9053. int pc_calc_pvprank(struct map_session_data *sd)
  9054. {
  9055. int old = sd->pvp_rank;
  9056. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9057. sd->pvp_rank=1;
  9058. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9059. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9060. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9061. return sd->pvp_rank;
  9062. }
  9063. /*==========================================
  9064. * Calculate next sd ranking calculation from config
  9065. *------------------------------------------*/
  9066. TIMER_FUNC(pc_calc_pvprank_timer){
  9067. struct map_session_data *sd;
  9068. sd=map_id2sd(id);
  9069. if(sd==NULL)
  9070. return 0;
  9071. sd->pvp_timer = INVALID_TIMER;
  9072. if( pc_isinvisible(sd) )
  9073. {// do not calculate the pvp rank for a hidden GM
  9074. return 0;
  9075. }
  9076. if( pc_calc_pvprank(sd) > 0 )
  9077. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9078. return 0;
  9079. }
  9080. /*==========================================
  9081. * Checking if sd is married
  9082. * Return:
  9083. * partner_id = yes
  9084. * 0 = no
  9085. *------------------------------------------*/
  9086. int pc_ismarried(struct map_session_data *sd)
  9087. {
  9088. if(sd == NULL)
  9089. return -1;
  9090. if(sd->status.partner_id > 0)
  9091. return sd->status.partner_id;
  9092. else
  9093. return 0;
  9094. }
  9095. /*==========================================
  9096. * Marry player sd to player dstsd
  9097. * Return:
  9098. * false = fail
  9099. * true = success
  9100. *------------------------------------------*/
  9101. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9102. {
  9103. if(sd == NULL || dstsd == NULL ||
  9104. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9105. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9106. return false;
  9107. sd->status.partner_id = dstsd->status.char_id;
  9108. dstsd->status.partner_id = sd->status.char_id;
  9109. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9110. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9111. return true;
  9112. }
  9113. /*==========================================
  9114. * Divorce sd from its partner
  9115. * Return:
  9116. * false = fail
  9117. * true = success
  9118. *------------------------------------------*/
  9119. bool pc_divorce(struct map_session_data *sd)
  9120. {
  9121. struct map_session_data *p_sd;
  9122. int i;
  9123. if( sd == NULL || !pc_ismarried(sd) )
  9124. return false;
  9125. if( !sd->status.partner_id )
  9126. return false; // Char is not married
  9127. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9128. { // Lets char server do the divorce
  9129. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9130. return false; // No char server connected
  9131. return true;
  9132. }
  9133. // Both players online, lets do the divorce manually
  9134. sd->status.partner_id = 0;
  9135. p_sd->status.partner_id = 0;
  9136. for( i = 0; i < MAX_INVENTORY; i++ )
  9137. {
  9138. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9139. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9140. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9141. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9142. }
  9143. clif_divorced(sd, p_sd->status.name);
  9144. clif_divorced(p_sd, sd->status.name);
  9145. return true;
  9146. }
  9147. /**
  9148. * Get the partner map_session_data of a player
  9149. * @param sd : the husband|wife session
  9150. * @return partner session or NULL
  9151. */
  9152. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9153. if (!sd || !pc_ismarried(sd))
  9154. return NULL;
  9155. return map_charid2sd(sd->status.partner_id);
  9156. }
  9157. /**
  9158. * Get the father map_session_data of a player
  9159. * @param sd : the baby session
  9160. * @return father session or NULL
  9161. */
  9162. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9163. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9164. return NULL;
  9165. return map_charid2sd(sd->status.father);
  9166. }
  9167. /**
  9168. * Get the mother map_session_data of a player
  9169. * @param sd : the baby session
  9170. * @return mother session or NULL
  9171. */
  9172. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9173. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9174. return NULL;
  9175. return map_charid2sd(sd->status.mother);
  9176. }
  9177. /*==========================================
  9178. * Get sd children charid. (Need to be married)
  9179. *------------------------------------------*/
  9180. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9181. {
  9182. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9183. // charid2sd returns NULL if not found
  9184. return NULL;
  9185. return map_charid2sd(sd->status.child);
  9186. }
  9187. /*==========================================
  9188. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9189. *------------------------------------------*/
  9190. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9191. {
  9192. int hp = 0, sp = 0;
  9193. if( pc_isdead(sd) )
  9194. return;
  9195. if (sd->hp_loss.value) {
  9196. sd->hp_loss.tick += diff_tick;
  9197. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9198. hp += sd->hp_loss.value;
  9199. sd->hp_loss.tick -= sd->hp_loss.rate;
  9200. }
  9201. if(hp >= sd->battle_status.hp)
  9202. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9203. }
  9204. if (sd->sp_loss.value) {
  9205. sd->sp_loss.tick += diff_tick;
  9206. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9207. sp += sd->sp_loss.value;
  9208. sd->sp_loss.tick -= sd->sp_loss.rate;
  9209. }
  9210. }
  9211. if (hp > 0 || sp > 0)
  9212. status_zap(&sd->bl, hp, sp);
  9213. }
  9214. //Character regen. Flag is used to know which types of regen can take place.
  9215. //&1: HP regen
  9216. //&2: SP regen
  9217. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9218. {
  9219. int hp = 0, sp = 0;
  9220. if (sd->hp_regen.value) {
  9221. sd->hp_regen.tick += diff_tick;
  9222. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9223. hp += sd->hp_regen.value;
  9224. sd->hp_regen.tick -= sd->hp_regen.rate;
  9225. }
  9226. }
  9227. if (sd->sp_regen.value) {
  9228. sd->sp_regen.tick += diff_tick;
  9229. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9230. sp += sd->sp_regen.value;
  9231. sd->sp_regen.tick -= sd->sp_regen.rate;
  9232. }
  9233. }
  9234. if (sd->percent_hp_regen.value) {
  9235. sd->percent_hp_regen.tick += diff_tick;
  9236. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9237. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9238. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9239. }
  9240. }
  9241. if (sd->percent_sp_regen.value) {
  9242. sd->percent_sp_regen.tick += diff_tick;
  9243. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9244. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9245. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9246. }
  9247. }
  9248. if (hp > 0 || sp > 0)
  9249. status_heal(&sd->bl, hp, sp, 0);
  9250. }
  9251. /*==========================================
  9252. * Memo player sd savepoint. (map,x,y)
  9253. *------------------------------------------*/
  9254. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9255. {
  9256. nullpo_retv(sd);
  9257. sd->status.save_point.map = mapindex;
  9258. sd->status.save_point.x = x;
  9259. sd->status.save_point.y = y;
  9260. }
  9261. /*==========================================
  9262. * Save 1 player data at autosave interval
  9263. *------------------------------------------*/
  9264. static TIMER_FUNC(pc_autosave){
  9265. int interval;
  9266. struct s_mapiterator* iter;
  9267. struct map_session_data* sd;
  9268. static int last_save_id = 0, save_flag = 0;
  9269. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9270. save_flag = 0;
  9271. else
  9272. save_flag = 1; //Noone was saved, so save first found char.
  9273. iter = mapit_getallusers();
  9274. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9275. {
  9276. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9277. continue;
  9278. if(sd->bl.id == last_save_id && save_flag != 1) {
  9279. save_flag = 1;
  9280. continue;
  9281. }
  9282. if(save_flag != 1) //Not our turn to save yet.
  9283. continue;
  9284. //Save char.
  9285. last_save_id = sd->bl.id;
  9286. save_flag = 2;
  9287. if (pc_isvip(sd)) // Check if we're still VIP
  9288. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9289. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9290. break;
  9291. }
  9292. mapit_free(iter);
  9293. interval = autosave_interval/(map_usercount()+1);
  9294. if(interval < minsave_interval)
  9295. interval = minsave_interval;
  9296. add_timer(gettick()+interval,pc_autosave,0,0);
  9297. return 0;
  9298. }
  9299. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9300. {
  9301. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9302. { //Night/day state does not match.
  9303. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9304. sd->state.night = night_flag;
  9305. return 1;
  9306. }
  9307. return 0;
  9308. }
  9309. /*================================================
  9310. * timer to do the day [Yor]
  9311. * data: 0 = called by timer, 1 = gmcommand/script
  9312. *------------------------------------------------*/
  9313. TIMER_FUNC(map_day_timer){
  9314. char tmp_soutput[1024];
  9315. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9316. return 0;
  9317. if (!night_flag)
  9318. return 0; //Already day.
  9319. night_flag = 0; // 0=day, 1=night [Yor]
  9320. map_foreachpc(pc_daynight_timer_sub);
  9321. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9322. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9323. return 0;
  9324. }
  9325. /*================================================
  9326. * timer to do the night [Yor]
  9327. * data: 0 = called by timer, 1 = gmcommand/script
  9328. *------------------------------------------------*/
  9329. TIMER_FUNC(map_night_timer){
  9330. char tmp_soutput[1024];
  9331. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9332. return 0;
  9333. if (night_flag)
  9334. return 0; //Already nigth.
  9335. night_flag = 1; // 0=day, 1=night [Yor]
  9336. map_foreachpc(pc_daynight_timer_sub);
  9337. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9338. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9339. return 0;
  9340. }
  9341. /**
  9342. * Attempt to stand up a player
  9343. * @param sd
  9344. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9345. * @return True if success, Fals if failed
  9346. */
  9347. bool pc_setstand(struct map_session_data *sd, bool force){
  9348. nullpo_ret(sd);
  9349. // Cannot stand yet
  9350. // TODO: Move to SCS_NOSTAND [Cydh]
  9351. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9352. return false;
  9353. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9354. clif_status_load(&sd->bl,EFST_SIT,0);
  9355. clif_standing(&sd->bl); //Inform area PC is standing
  9356. //Reset sitting tick.
  9357. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9358. if( pc_isdead( sd ) ){
  9359. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9360. clif_party_dead( sd );
  9361. }else{
  9362. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9363. }
  9364. return true;
  9365. }
  9366. /**
  9367. * Calculate Overheat value
  9368. * @param sd: Player data
  9369. * @param heat: Amount of Heat to adjust
  9370. **/
  9371. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9372. struct status_change_entry *sce = NULL;
  9373. int16 limit[] = { 150, 200, 280, 360, 450 };
  9374. uint16 skill_lv;
  9375. nullpo_retv(sd);
  9376. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9377. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9378. sce->val1 += heat;
  9379. sce->val1 = cap_value(sce->val1, 0, 1000);
  9380. if (sd->sc.data[SC_OVERHEAT])
  9381. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9382. if (sce->val1 > limit[skill_lv])
  9383. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9384. } else if (heat > 0)
  9385. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9386. }
  9387. /**
  9388. * Check if player is autolooting given itemID.
  9389. */
  9390. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9391. {
  9392. uint8 i = 0;
  9393. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9394. return true;
  9395. if (!sd->state.autolooting)
  9396. return false;
  9397. if (sd->state.autolooting)
  9398. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9399. return (i != AUTOLOOTITEM_SIZE);
  9400. }
  9401. /**
  9402. * Checks if player can use @/#command
  9403. * @param sd Player map session data
  9404. * @param command Command name without @/# and params
  9405. * @param type is it atcommand or charcommand
  9406. */
  9407. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9408. {
  9409. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9410. }
  9411. /**
  9412. * Checks if commands used by a player should be logged
  9413. * according to their group setting.
  9414. * @param sd Player map session data
  9415. */
  9416. bool pc_should_log_commands(struct map_session_data *sd)
  9417. {
  9418. return pc_group_should_log_commands(pc_get_group_id(sd));
  9419. }
  9420. /**
  9421. * Spirit Charm expiration timer.
  9422. * @see TimerFunc
  9423. */
  9424. static TIMER_FUNC(pc_spiritcharm_timer){
  9425. struct map_session_data *sd;
  9426. int i;
  9427. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9428. return 1;
  9429. if (sd->spiritcharm <= 0) {
  9430. 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);
  9431. sd->spiritcharm = 0;
  9432. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9433. return 0;
  9434. }
  9435. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9436. if (i == sd->spiritcharm) {
  9437. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9438. return 0;
  9439. }
  9440. sd->spiritcharm--;
  9441. if (i != sd->spiritcharm)
  9442. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9443. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9444. if (sd->spiritcharm <= 0)
  9445. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9446. clif_spiritcharm(sd);
  9447. return 0;
  9448. }
  9449. /**
  9450. * Adds a spirit charm.
  9451. * @param sd: Target character
  9452. * @param interval: Duration
  9453. * @param max: Maximum amount of charms to add
  9454. * @param type: Charm type (@see spirit_charm_types)
  9455. */
  9456. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9457. {
  9458. int tid, i;
  9459. nullpo_retv(sd);
  9460. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9461. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9462. if (max > MAX_SPIRITCHARM)
  9463. max = MAX_SPIRITCHARM;
  9464. if (sd->spiritcharm < 0)
  9465. sd->spiritcharm = 0;
  9466. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9467. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9468. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9469. sd->spiritcharm--;
  9470. if (sd->spiritcharm != 0)
  9471. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9472. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9473. }
  9474. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9475. 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);
  9476. if (i != sd->spiritcharm)
  9477. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9478. sd->spiritcharm_timer[i] = tid;
  9479. sd->spiritcharm++;
  9480. sd->spiritcharm_type = type;
  9481. clif_spiritcharm(sd);
  9482. }
  9483. /**
  9484. * Removes one or more spirit charms.
  9485. * @param sd: The target character
  9486. * @param count: Amount of charms to remove
  9487. * @param type: Type of charm to remove
  9488. */
  9489. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9490. {
  9491. int i;
  9492. nullpo_retv(sd);
  9493. if (sd->spiritcharm_type != type)
  9494. return;
  9495. if (sd->spiritcharm <= 0) {
  9496. sd->spiritcharm = 0;
  9497. return;
  9498. }
  9499. if (count <= 0)
  9500. return;
  9501. if (count > sd->spiritcharm)
  9502. count = sd->spiritcharm;
  9503. sd->spiritcharm -= count;
  9504. if (count > MAX_SPIRITCHARM)
  9505. count = MAX_SPIRITCHARM;
  9506. for (i = 0; i < count; i++) {
  9507. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9508. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9509. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9510. }
  9511. }
  9512. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9513. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9514. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9515. }
  9516. if (sd->spiritcharm <= 0)
  9517. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9518. clif_spiritcharm(sd);
  9519. }
  9520. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9521. /**
  9522. * Renewal EXP/Item Drop rate modifier based on level penalty
  9523. * @param level_diff: Monster and Player level difference
  9524. * @param mob_class: Monster class
  9525. * @param mode: Monster mode
  9526. * @param type: 1 - EXP, 2 - Item Drop
  9527. * @return Penalty rate
  9528. */
  9529. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9530. {
  9531. int rate = 100;
  9532. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9533. return rate;
  9534. if (level_diff < 0)
  9535. level_diff = MAX_LEVEL + (~level_diff + 1);
  9536. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9537. return rate;
  9538. return 100; // Penalty not found, return default
  9539. }
  9540. #endif
  9541. int pc_split_str(char *str,char **val,int num)
  9542. {
  9543. int i;
  9544. for (i=0; i<num && str; i++){
  9545. val[i] = str;
  9546. str = strchr(str,',');
  9547. if (str && i<num-1) //Do not remove a trailing comma.
  9548. *str++=0;
  9549. }
  9550. return i;
  9551. }
  9552. int pc_split_atoi(char* str, int* val, char sep, int max)
  9553. {
  9554. int i,j;
  9555. for (i=0; i<max; i++) {
  9556. if (!str) break;
  9557. val[i] = atoi(str);
  9558. str = strchr(str,sep);
  9559. if (str)
  9560. *str++=0;
  9561. }
  9562. //Zero up the remaining.
  9563. for(j=i; j < max; j++)
  9564. val[j] = 0;
  9565. return i;
  9566. }
  9567. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9568. {
  9569. static int warning=0;
  9570. int i,j;
  9571. for (i=0; i<max; i++) {
  9572. double f;
  9573. if (!str) break;
  9574. f = atof(str);
  9575. if (f < 0)
  9576. val[i] = 0;
  9577. else if (f > UINT_MAX) {
  9578. val[i] = UINT_MAX;
  9579. if (!warning) {
  9580. warning = 1;
  9581. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9582. }
  9583. } else
  9584. val[i] = (unsigned int)f;
  9585. str = strchr(str,sep);
  9586. if (str)
  9587. *str++=0;
  9588. }
  9589. //Zero up the remaining.
  9590. for(j=i; j < max; j++)
  9591. val[j] = 0;
  9592. return i;
  9593. }
  9594. /*==========================================
  9595. * sub DB reading.
  9596. * Function used to read skill_tree.txt
  9597. *------------------------------------------*/
  9598. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9599. {
  9600. uint32 baselv, joblv, baselv_max, joblv_max;
  9601. uint16 skill_id, skill_lv, skill_lv_max;
  9602. int idx, class_;
  9603. unsigned int i, offset, skill_idx;
  9604. class_ = atoi(fields[0]);
  9605. skill_id = (uint16)atoi(fields[1]);
  9606. skill_lv = (uint16)atoi(fields[2]);
  9607. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9608. baselv = (uint32)atoi(fields[3]);
  9609. joblv = (uint32)atoi(fields[4]);
  9610. offset = 5;
  9611. }
  9612. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9613. baselv = joblv = 0;
  9614. offset = 3;
  9615. }
  9616. else {
  9617. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9618. return false;
  9619. }
  9620. if(!pcdb_checkid(class_))
  9621. {
  9622. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9623. return false;
  9624. }
  9625. idx = pc_class2idx(class_);
  9626. if (!skill_get_index(skill_id)) {
  9627. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9628. return false;
  9629. }
  9630. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9631. 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);
  9632. skill_lv = skill_lv_max;
  9633. }
  9634. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9635. 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);
  9636. baselv = baselv_max;
  9637. }
  9638. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9639. 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);
  9640. joblv = joblv_max;
  9641. }
  9642. //This is to avoid adding two lines for the same skill. [Skotlex]
  9643. 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 );
  9644. if( skill_idx == MAX_SKILL_TREE )
  9645. {
  9646. 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_);
  9647. return false;
  9648. }
  9649. else if(skill_tree[idx][skill_idx].skill_id)
  9650. {
  9651. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9652. }
  9653. skill_tree[idx][skill_idx].skill_id = skill_id;
  9654. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9655. skill_tree[idx][skill_idx].baselv = baselv;
  9656. skill_tree[idx][skill_idx].joblv = joblv;
  9657. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9658. {
  9659. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9660. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9661. if (skill_id == 0) {
  9662. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  9663. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  9664. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  9665. }
  9666. continue;
  9667. }
  9668. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9669. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9670. return false;
  9671. }
  9672. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9673. 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);
  9674. skill_lv = skill_lv_max;
  9675. }
  9676. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9677. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9678. }
  9679. return true;
  9680. }
  9681. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9682. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9683. {
  9684. int type, class_, diff;
  9685. type = atoi(fields[0]); //1=experience, 2=item drop
  9686. class_ = atoi(fields[1]);
  9687. diff = atoi(fields[2]);
  9688. if( type != 1 && type != 2 ){
  9689. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9690. return false;
  9691. }
  9692. if( !CHK_CLASS(class_) ){
  9693. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9694. return false;
  9695. }
  9696. diff = min(diff, MAX_LEVEL);
  9697. if( diff < 0 )
  9698. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9699. level_penalty[type][class_][diff] = atoi(fields[3]);
  9700. return true;
  9701. }
  9702. #endif
  9703. /** [Cydh]
  9704. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9705. * @param level Base level of player
  9706. * @param class_ Job ID @see enum e_job
  9707. * @return base_hp
  9708. */
  9709. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9710. double base_hp;
  9711. uint16 i, idx = pc_class2idx(class_);
  9712. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9713. #ifndef RENEWAL
  9714. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9715. #endif
  9716. for (i = 2; i <= level; i++)
  9717. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9718. if (class_ == JOB_SUMMONER)
  9719. base_hp += floor((base_hp / 2) + 0.5);
  9720. return (unsigned int)base_hp;
  9721. }
  9722. /** [Playtester]
  9723. * Calculates base sp of player.
  9724. * @param level Base level of player
  9725. * @param class_ Job ID @see enum e_job
  9726. * @return base_sp
  9727. */
  9728. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9729. double base_sp;
  9730. uint16 idx = pc_class2idx(class_);
  9731. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9732. switch (class_) {
  9733. case JOB_NINJA:
  9734. if (level >= 10)
  9735. base_sp -= 22;
  9736. else
  9737. base_sp = 11 + 3*level;
  9738. break;
  9739. case JOB_GUNSLINGER:
  9740. if (level > 10)
  9741. base_sp -= 18;
  9742. else
  9743. base_sp = 9 + 3*level;
  9744. break;
  9745. case JOB_SUMMONER:
  9746. base_sp -= floor(base_sp / 2);
  9747. break;
  9748. }
  9749. return (unsigned int)base_sp;
  9750. }
  9751. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9752. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9753. int idx, class_;
  9754. unsigned int i;
  9755. class_ = atoi(fields[0]);
  9756. if (!pcdb_checkid(class_)) {
  9757. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9758. return false;
  9759. }
  9760. idx = pc_class2idx(class_);
  9761. job_info[idx].max_weight_base = atoi(fields[1]);
  9762. job_info[idx].hp_factor = atoi(fields[2]);
  9763. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9764. job_info[idx].sp_factor = atoi(fields[4]);
  9765. #ifdef RENEWAL_ASPD
  9766. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9767. #else
  9768. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9769. #endif
  9770. {
  9771. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9772. }
  9773. return true;
  9774. }
  9775. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9776. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9777. {
  9778. int idx, class_, i;
  9779. class_ = atoi(fields[0]);
  9780. if(!pcdb_checkid(class_))
  9781. {
  9782. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9783. return false;
  9784. }
  9785. idx = pc_class2idx(class_);
  9786. for(i = 1; i < columns; i++)
  9787. {
  9788. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9789. }
  9790. return true;
  9791. }
  9792. //Reading job_exp.txt line
  9793. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9794. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9795. {
  9796. int idx, i, type;
  9797. int job_id,job_count,jobs[CLASS_COUNT];
  9798. unsigned int ui, maxlvl;
  9799. maxlvl = atoi(fields[0]);
  9800. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9801. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9802. return false;
  9803. }
  9804. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9805. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9806. return false;
  9807. }
  9808. type = atoi(fields[2]);
  9809. if(type < 0 || type > 1){
  9810. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9811. return false;
  9812. }
  9813. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9814. if (job_count < 1)
  9815. return false;
  9816. job_id = jobs[0];
  9817. if(!pcdb_checkid(job_id)){
  9818. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9819. return false;
  9820. }
  9821. idx = pc_class2idx(job_id);
  9822. job_info[idx].max_level[type] = maxlvl;
  9823. for(i=0; i<maxlvl; i++)
  9824. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9825. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9826. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9827. //should look like this:
  9828. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9829. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9830. job_info[idx].max_level[type]--;
  9831. if (job_info[idx].max_level[type] < maxlvl) {
  9832. 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]);
  9833. ShowInfo("Filling the missing values with the last exp entry.\n");
  9834. //Fill the requested values with the last entry.
  9835. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9836. for (; ui+2 < maxlvl; ui++)
  9837. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9838. job_info[idx].max_level[type] = maxlvl;
  9839. }
  9840. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9841. for (i = 1; i < job_count; i++) {
  9842. job_id = jobs[i];
  9843. if (!pcdb_checkid(job_id)) {
  9844. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9845. continue;
  9846. }
  9847. idx = pc_class2idx(job_id);
  9848. 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]));
  9849. job_info[idx].max_level[type] = maxlvl;
  9850. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9851. }
  9852. return true;
  9853. }
  9854. /**
  9855. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9856. * startlvl,endlvl,class,type,values...
  9857. */
  9858. #ifdef HP_SP_TABLES
  9859. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9860. {
  9861. int i, startlvl, endlvl;
  9862. int job_count,jobs[CLASS_COUNT];
  9863. short type;
  9864. startlvl = atoi(fields[0]);
  9865. if(startlvl<1){
  9866. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9867. return false;
  9868. }
  9869. endlvl = atoi(fields[1]);
  9870. if(endlvl<1 || endlvl<startlvl){
  9871. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9872. return false;
  9873. }
  9874. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9875. 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);
  9876. return false;
  9877. }
  9878. type = atoi(fields[3]);
  9879. if(type < 0 || type > 1){
  9880. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9881. return false;
  9882. }
  9883. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9884. if (job_count < 1)
  9885. return false;
  9886. for (i = 0; i < job_count; i++) {
  9887. int idx, job_id = jobs[i], use_endlvl;
  9888. if (!pcdb_checkid(job_id)) {
  9889. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9890. return false;
  9891. }
  9892. idx = pc_class2idx(job_id);
  9893. if (startlvl > job_info[idx].max_level[0]) {
  9894. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9895. return false;
  9896. }
  9897. //Just read until available max level for this job, don't use MAX_LEVEL!
  9898. use_endlvl = endlvl;
  9899. if (use_endlvl > job_info[idx].max_level[0])
  9900. use_endlvl = job_info[idx].max_level[0];
  9901. if(type == 0) { //hp type
  9902. uint16 j;
  9903. for(j = 0; j < use_endlvl; j++) {
  9904. if (atoi(fields[j+4])) {
  9905. uint16 lvl_idx = startlvl-1+j;
  9906. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9907. //Tells if this HP is lower than previous level (but not for 99->100)
  9908. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9909. 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",
  9910. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9911. }
  9912. }
  9913. }
  9914. else { //sp type
  9915. uint16 j;
  9916. for(j = 0; j < use_endlvl; j++) {
  9917. if (atoi(fields[j+4])) {
  9918. uint16 lvl_idx = startlvl-1+j;
  9919. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9920. //Tells if this SP is lower than previous level (but not for 99->100)
  9921. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9922. 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",
  9923. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9924. }
  9925. }
  9926. }
  9927. }
  9928. return true;
  9929. }
  9930. #endif
  9931. /** [Cydh]
  9932. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9933. */
  9934. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9935. {
  9936. int idx, class_;
  9937. uint16 str, agi, vit, int_, dex, luk;
  9938. script_get_constant(trim(fields[0]),&class_);
  9939. if ((idx = pc_class2idx(class_)) < 0) {
  9940. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9941. return false;
  9942. }
  9943. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9944. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9945. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9946. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9947. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9948. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9949. job_info[idx].max_param.str = str;
  9950. job_info[idx].max_param.agi = agi;
  9951. job_info[idx].max_param.vit = vit;
  9952. job_info[idx].max_param.int_ = int_;
  9953. job_info[idx].max_param.dex = dex;
  9954. job_info[idx].max_param.luk = luk;
  9955. return true;
  9956. }
  9957. /**
  9958. * Read job_noenter_map.txt
  9959. **/
  9960. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  9961. int idx, class_ = -1;
  9962. if (ISDIGIT(str[0][0])) {
  9963. class_ = atoi(str[0]);
  9964. } else {
  9965. if (!script_get_constant(str[0], &class_)) {
  9966. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  9967. return false;
  9968. }
  9969. }
  9970. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  9971. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  9972. return false;
  9973. }
  9974. job_info[idx].noenter_map.zone = atoi(str[1]);
  9975. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  9976. return true;
  9977. }
  9978. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9979. int i=1;
  9980. char line[24000]; //FIXME this seem too big
  9981. FILE *fp;
  9982. sprintf(line, "%s/statpoint.txt", basedir);
  9983. fp=fopen(line,"r");
  9984. if(fp == NULL){
  9985. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  9986. return max(last_s,i);
  9987. } else {
  9988. int entries=0;
  9989. while(fgets(line, sizeof(line), fp))
  9990. {
  9991. int stat;
  9992. trim(line);
  9993. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9994. continue;
  9995. if ((stat=strtoul(line,NULL,10))<0)
  9996. stat=0;
  9997. if (i > MAX_LEVEL)
  9998. break;
  9999. statp[i]=stat;
  10000. i++;
  10001. entries++;
  10002. }
  10003. fclose(fp);
  10004. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10005. }
  10006. return max(last_s,i);
  10007. }
  10008. /*==========================================
  10009. * pc DB reading.
  10010. * job_exp.txt - required experience values
  10011. * skill_tree.txt - skill tree for every class
  10012. * attr_fix.txt - elemental adjustment table
  10013. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10014. * job_db2.txt - job,stats bonuses/lvl
  10015. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10016. *------------------------------------------*/
  10017. void pc_readdb(void) {
  10018. int i, k, s = 1;
  10019. const char* dbsubpath[] = {
  10020. "",
  10021. "/" DBIMPORT,
  10022. //add other path here
  10023. };
  10024. //reset
  10025. memset(job_info,0,sizeof(job_info)); // job_info table
  10026. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10027. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10028. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10029. for( k=1; k < 3; k++ ){ // fill in the blanks
  10030. int j;
  10031. for( j = 0; j < CLASS_ALL; j++ ){
  10032. int tmp = 0;
  10033. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10034. if( i == MAX_LEVEL+1 )
  10035. tmp = level_penalty[k][j][0];// reset
  10036. if( level_penalty[k][j][i] > 0 )
  10037. tmp = level_penalty[k][j][i];
  10038. else
  10039. level_penalty[k][j][i] = tmp;
  10040. }
  10041. }
  10042. }
  10043. #endif
  10044. // reset then read statspoint
  10045. memset(statp,0,sizeof(statp));
  10046. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10047. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10048. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10049. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10050. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10051. if(i==0) {
  10052. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10053. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10054. }
  10055. else {
  10056. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10057. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10058. }
  10059. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10060. if (i == 0)
  10061. #ifdef RENEWAL_ASPD
  10062. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10063. #else
  10064. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10065. #endif
  10066. else
  10067. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10068. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10069. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10070. #ifdef HP_SP_TABLES
  10071. 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!
  10072. #endif
  10073. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10074. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10075. aFree(dbsubpath1);
  10076. aFree(dbsubpath2);
  10077. }
  10078. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10079. memset(skill_tree, 0, sizeof(skill_tree));
  10080. 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);
  10081. 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);
  10082. // generate the remaining parts of the db if necessary
  10083. k = battle_config.use_statpoint_table; //save setting
  10084. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10085. statp[0] = 45; // seed value
  10086. for (; s <= MAX_LEVEL; s++)
  10087. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10088. battle_config.use_statpoint_table = k; //restore setting
  10089. //Checking if all class have their data
  10090. for (i = 0; i < JOB_MAX; i++) {
  10091. int idx;
  10092. uint16 j;
  10093. if (!pcdb_checkid(i))
  10094. continue;
  10095. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10096. continue; //Classes that do not need exp tables.
  10097. idx = pc_class2idx(i);
  10098. if (!job_info[idx].max_level[0])
  10099. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10100. if (!job_info[idx].max_level[1])
  10101. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10102. //Init and checking the empty value of Base HP/SP [Cydh]
  10103. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10104. if (job_info[idx].base_hp[j] == 0)
  10105. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10106. if (job_info[idx].base_sp[j] == 0)
  10107. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10108. }
  10109. }
  10110. }
  10111. // Read MOTD on startup. [Valaris]
  10112. int pc_read_motd(void)
  10113. {
  10114. FILE* fp;
  10115. // clear old MOTD
  10116. memset(motd_text, 0, sizeof(motd_text));
  10117. // read current MOTD
  10118. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10119. {
  10120. unsigned int entries = 0;
  10121. char buf[CHAT_SIZE_MAX];
  10122. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10123. {
  10124. unsigned int lines = 0;
  10125. size_t len;
  10126. lines++;
  10127. if( buf[0] == '/' && buf[1] == '/' )
  10128. continue;
  10129. len = strlen(buf);
  10130. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10131. len--;
  10132. if( len ) {
  10133. char * ptr;
  10134. buf[len] = 0;
  10135. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10136. 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);
  10137. }
  10138. else {// empty line
  10139. buf[0] = ' ';
  10140. buf[1] = 0;
  10141. }
  10142. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10143. entries++;
  10144. }
  10145. fclose(fp);
  10146. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10147. }
  10148. else
  10149. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10150. return 0;
  10151. }
  10152. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10153. int i,cursor = 0;
  10154. struct item_cd* cd = NULL;
  10155. if( load ) {
  10156. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10157. // no item cooldown is associated with this character
  10158. return;
  10159. }
  10160. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10161. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10162. sd->item_delay[cursor].tick = cd->tick[i];
  10163. sd->item_delay[cursor].nameid = cd->nameid[i];
  10164. cursor++;
  10165. }
  10166. }
  10167. idb_remove(itemcd_db,sd->status.char_id);
  10168. } else {
  10169. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10170. // create a new skill cooldown object for map storage
  10171. CREATE( cd, struct item_cd, 1 );
  10172. idb_put( itemcd_db, sd->status.char_id, cd );
  10173. }
  10174. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10175. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10176. cd->tick[cursor] = sd->item_delay[i].tick;
  10177. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10178. cursor++;
  10179. }
  10180. }
  10181. }
  10182. return;
  10183. }
  10184. /**
  10185. * Add item delay to player's item delay data
  10186. * @param sd Player
  10187. * @param id Item data
  10188. * @param tick Current tick
  10189. * @param n Item index in inventory
  10190. * @return 0: No delay, can consume item.
  10191. * 1: Has delay, cancel consumption.
  10192. **/
  10193. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10194. int i;
  10195. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10196. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10197. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10198. if( i < MAX_ITEMDELAYS ) {
  10199. if( sd->item_delay[i].nameid ) {// found
  10200. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10201. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10202. char e_msg[CHAT_SIZE_MAX];
  10203. if( e_tick > 99 )
  10204. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10205. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10206. else
  10207. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10208. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10209. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10210. return 1; // Delay has not expired yet
  10211. }
  10212. } else {// not yet used item (all slots are initially empty)
  10213. sd->item_delay[i].nameid = id->nameid;
  10214. }
  10215. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10216. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10217. } else {// should not happen
  10218. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10219. }
  10220. //clean up used delays so we can give room for more
  10221. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10222. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10223. sd->item_delay[i].tick = 0;
  10224. sd->item_delay[i].nameid = 0;
  10225. }
  10226. }
  10227. return 0;
  10228. }
  10229. /**
  10230. * Check if player has delay to reuse item
  10231. * @param sd Player
  10232. * @param id Item data
  10233. * @param tick Current tick
  10234. * @param n Item index in inventory
  10235. * @return 0: No delay, can consume item.
  10236. * 1: Has delay, cancel consumption.
  10237. **/
  10238. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10239. struct status_change *sc = NULL;
  10240. nullpo_retr(0, sd);
  10241. nullpo_retr(0, id);
  10242. // Do normal delay assignment
  10243. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10244. return pc_itemcd_add(sd, id, tick, n);
  10245. // Send reply of delay remains
  10246. if (sc->data[id->delay_sc]) {
  10247. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10248. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10249. return 1;
  10250. }
  10251. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10252. return 0;
  10253. }
  10254. /**
  10255. * Clear the dmglog data from player
  10256. * @param sd
  10257. * @param md
  10258. **/
  10259. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10260. {
  10261. uint8 i;
  10262. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10263. if (i < DAMAGELOG_SIZE) {
  10264. md->dmglog[i].id = 0;
  10265. md->dmglog[i].dmg = 0;
  10266. md->dmglog[i].flag = 0;
  10267. }
  10268. }
  10269. /**
  10270. * Add log to player's dmglog
  10271. * @param sd
  10272. * @param id Monster's GID
  10273. **/
  10274. void pc_damage_log_add(struct map_session_data *sd, int id)
  10275. {
  10276. uint8 i = 0;
  10277. if (!sd || !id)
  10278. return;
  10279. //Only store new data, don't need to renew the old one with same id
  10280. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10281. if (i < DAMAGELOG_SIZE_PC)
  10282. return;
  10283. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10284. if (sd->dmglog[i] == 0) {
  10285. sd->dmglog[i] = id;
  10286. return;
  10287. }
  10288. }
  10289. }
  10290. /**
  10291. * Clear dmglog data from player
  10292. * @param sd
  10293. * @param id Monster's id
  10294. **/
  10295. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10296. {
  10297. uint8 i;
  10298. struct mob_data *md = NULL;
  10299. if (!sd)
  10300. return;
  10301. if (!id) {
  10302. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10303. if( !sd->dmglog[i] ) //skip the empty value
  10304. continue;
  10305. if ((md = map_id2md(sd->dmglog[i])))
  10306. pc_clear_log_damage_sub(sd->status.char_id,md);
  10307. sd->dmglog[i] = 0;
  10308. }
  10309. }
  10310. else {
  10311. if ((md = map_id2md(id)))
  10312. pc_clear_log_damage_sub(sd->status.char_id,md);
  10313. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10314. if (i < DAMAGELOG_SIZE_PC)
  10315. sd->dmglog[i] = 0;
  10316. }
  10317. }
  10318. /**
  10319. * Status change data arrived from char-server
  10320. * @param sd: Player data
  10321. */
  10322. void pc_scdata_received(struct map_session_data *sd) {
  10323. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10324. clif_weight_limit( sd );
  10325. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10326. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10327. }
  10328. sd->state.pc_loaded = true;
  10329. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10330. sd->state.connect_new = 1;
  10331. clif_parse_LoadEndAck(sd->fd, sd);
  10332. }
  10333. if (pc_iscarton(sd)) {
  10334. sd->cart_weight_max = 0; // Force a client refesh
  10335. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10336. }
  10337. }
  10338. /**
  10339. * Check player account expiration time and rental item expirations
  10340. * @param sd: Player data
  10341. */
  10342. void pc_check_expiration(struct map_session_data *sd) {
  10343. #ifndef ENABLE_SC_SAVING
  10344. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10345. #endif
  10346. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10347. time_t exp_time = sd->expiration_time;
  10348. char tmpstr[1024];
  10349. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10350. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10351. pc_expire_check(sd);
  10352. }
  10353. }
  10354. TIMER_FUNC(pc_expiration_timer){
  10355. struct map_session_data *sd = map_id2sd(id);
  10356. if( !sd ) return 0;
  10357. sd->expiration_tid = INVALID_TIMER;
  10358. if( sd->fd )
  10359. clif_authfail_fd(sd->fd,10);
  10360. map_quit(sd);
  10361. return 0;
  10362. }
  10363. TIMER_FUNC(pc_autotrade_timer){
  10364. struct map_session_data *sd = map_id2sd(id);
  10365. if (!sd)
  10366. return 0;
  10367. sd->autotrade_tid = INVALID_TIMER;
  10368. if (sd->state.autotrade&2)
  10369. vending_reopen(sd);
  10370. if (sd->state.autotrade&4)
  10371. buyingstore_reopen(sd);
  10372. if (!sd->vender_id && !sd->buyer_id) {
  10373. sd->state.autotrade = 0;
  10374. map_quit(sd);
  10375. }
  10376. return 0;
  10377. }
  10378. /* this timer exists only when a character with a expire timer > 24h is online */
  10379. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10380. TIMER_FUNC(pc_global_expiration_timer){
  10381. struct s_mapiterator* iter;
  10382. struct map_session_data* sd;
  10383. iter = mapit_getallusers();
  10384. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10385. if( sd->expiration_time )
  10386. pc_expire_check(sd);
  10387. mapit_free(iter);
  10388. return 0;
  10389. }
  10390. void pc_expire_check(struct map_session_data *sd) {
  10391. /* ongoing timer */
  10392. if( sd->expiration_tid != INVALID_TIMER )
  10393. return;
  10394. /* not within the next 24h, enable the global check */
  10395. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10396. /* global check not running, enable */
  10397. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10398. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10399. return;
  10400. }
  10401. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10402. }
  10403. /**
  10404. * Deposit some money to bank
  10405. * @param sd
  10406. * @param money Amount of money to deposit
  10407. **/
  10408. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10409. unsigned int limit_check = money + sd->bank_vault;
  10410. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10411. return BDA_OVERFLOW;
  10412. } else if ( money > sd->status.zeny ) {
  10413. return BDA_NO_MONEY;
  10414. }
  10415. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10416. return BDA_NO_MONEY;
  10417. sd->bank_vault += money;
  10418. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10419. if( save_settings&CHARSAVE_BANK )
  10420. chrif_save(sd, CSAVE_NORMAL);
  10421. return BDA_SUCCESS;
  10422. }
  10423. /**
  10424. * Withdraw money from bank
  10425. * @param sd
  10426. * @param money Amount of money that will be withdrawn
  10427. **/
  10428. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10429. unsigned int limit_check = money + sd->status.zeny;
  10430. if( money <= 0 ) {
  10431. return BWA_UNKNOWN_ERROR;
  10432. } else if ( money > sd->bank_vault ) {
  10433. return BWA_NO_MONEY;
  10434. } else if ( limit_check > MAX_ZENY ) {
  10435. /* no official response for this scenario exists. */
  10436. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10437. return BWA_UNKNOWN_ERROR;
  10438. }
  10439. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10440. return BWA_NO_MONEY;
  10441. sd->bank_vault -= money;
  10442. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10443. if( save_settings&CHARSAVE_BANK )
  10444. chrif_save(sd, CSAVE_NORMAL);
  10445. return BWA_SUCCESS;
  10446. }
  10447. /**
  10448. * Clear Crimson Marker data from caster
  10449. * @param sd: Player
  10450. **/
  10451. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10452. uint8 i;
  10453. if (!sd)
  10454. return;
  10455. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10456. struct block_list *bl = NULL;
  10457. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10458. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10459. sd->c_marker[i] = 0;
  10460. }
  10461. }
  10462. /**
  10463. * Show version to player
  10464. * @param sd: Player
  10465. **/
  10466. void pc_show_version(struct map_session_data *sd) {
  10467. const char* svn = get_svn_revision();
  10468. char buf[CHAT_SIZE_MAX];
  10469. if( svn[0] != UNKNOWN_VERSION )
  10470. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10471. else {
  10472. const char* git = get_git_hash();
  10473. if( git[0] != UNKNOWN_VERSION )
  10474. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10475. else
  10476. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10477. }
  10478. clif_displaymessage(sd->fd,buf);
  10479. }
  10480. /**
  10481. * Run bonus_script on player
  10482. * @param sd
  10483. * @author [Cydh]
  10484. **/
  10485. void pc_bonus_script(struct map_session_data *sd) {
  10486. int now = gettick();
  10487. struct linkdb_node *node = NULL, *next = NULL;
  10488. if (!sd || !(node = sd->bonus_script.head))
  10489. return;
  10490. while (node) {
  10491. struct s_bonus_script_entry *entry = NULL;
  10492. next = node->next;
  10493. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10494. // Only start timer for new bonus_script
  10495. if (entry->tid == INVALID_TIMER) {
  10496. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10497. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10498. entry->tick += now;
  10499. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10500. }
  10501. if (entry->script)
  10502. run_script(entry->script, 0, sd->bl.id, 0);
  10503. else
  10504. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10505. }
  10506. node = next;
  10507. }
  10508. }
  10509. /**
  10510. * Add bonus_script to player
  10511. * @param sd Player
  10512. * @param script_str Script string
  10513. * @param dur Duration in ms
  10514. * @param icon EFST
  10515. * @param flag Flags @see enum e_bonus_script_flags
  10516. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10517. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10518. * @author [Cydh]
  10519. **/
  10520. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum efst_types icon, uint16 flag, uint8 type) {
  10521. struct script_code *script = NULL;
  10522. struct linkdb_node *node = NULL;
  10523. struct s_bonus_script_entry *entry = NULL;
  10524. if (!sd)
  10525. return NULL;
  10526. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10527. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10528. return NULL;
  10529. }
  10530. // Duplication checks
  10531. if ((node = sd->bonus_script.head)) {
  10532. while (node) {
  10533. entry = (struct s_bonus_script_entry *)node->data;
  10534. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10535. int newdur = gettick() + dur;
  10536. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10537. settick_timer(entry->tid, newdur);
  10538. script_free_code(script);
  10539. return NULL;
  10540. }
  10541. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10542. break;
  10543. else { // No duplicate bonus
  10544. script_free_code(script);
  10545. return NULL;
  10546. }
  10547. }
  10548. node = node->next;
  10549. }
  10550. }
  10551. CREATE(entry, struct s_bonus_script_entry, 1);
  10552. entry->script_buf = StringBuf_Malloc();
  10553. StringBuf_AppendStr(entry->script_buf, script_str);
  10554. entry->tid = INVALID_TIMER;
  10555. entry->flag = flag;
  10556. entry->icon = icon;
  10557. entry->tick = dur; // Use duration first, on run change to expire time
  10558. entry->type = type;
  10559. entry->script = script;
  10560. sd->bonus_script.count++;
  10561. return entry;
  10562. }
  10563. /**
  10564. * Remove bonus_script data from player
  10565. * @param sd: Target player
  10566. * @param list: Bonus script entry from player
  10567. * @author [Cydh]
  10568. **/
  10569. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10570. if (entry->tid != INVALID_TIMER)
  10571. delete_timer(entry->tid, pc_bonus_script_timer);
  10572. if (entry->script)
  10573. script_free_code(entry->script);
  10574. if (entry->script_buf)
  10575. StringBuf_Free(entry->script_buf);
  10576. if (sd) {
  10577. if (entry->icon != EFST_BLANK)
  10578. clif_status_load(&sd->bl, entry->icon, 0);
  10579. if (sd->bonus_script.count > 0)
  10580. sd->bonus_script.count--;
  10581. }
  10582. aFree(entry);
  10583. }
  10584. /**
  10585. * Do final process if no entry left
  10586. * @param sd
  10587. **/
  10588. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10589. if (sd->bonus_script.count == 0) {
  10590. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10591. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10592. linkdb_final(&sd->bonus_script.head);
  10593. }
  10594. }
  10595. /**
  10596. * Timer for bonus_script
  10597. * @param tid
  10598. * @param tick
  10599. * @param id
  10600. * @param data
  10601. * @author [Cydh]
  10602. **/
  10603. TIMER_FUNC(pc_bonus_script_timer){
  10604. struct map_session_data *sd;
  10605. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10606. sd = map_id2sd(id);
  10607. if (!sd) {
  10608. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10609. return 0;
  10610. }
  10611. if (tid == INVALID_TIMER)
  10612. return 0;
  10613. if (!sd->bonus_script.head || entry == NULL) {
  10614. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  10615. return 0;
  10616. }
  10617. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10618. pc_bonus_script_free_entry(sd, entry);
  10619. pc_bonus_script_check_final(sd);
  10620. status_calc_pc(sd,SCO_NONE);
  10621. return 0;
  10622. }
  10623. /**
  10624. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10625. * @param sd: Target player
  10626. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10627. * @author [Cydh]
  10628. **/
  10629. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10630. struct linkdb_node *node = NULL;
  10631. uint16 count = 0;
  10632. if (!sd || !(node = sd->bonus_script.head))
  10633. return;
  10634. while (node) {
  10635. struct linkdb_node *next = node->next;
  10636. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10637. if (entry && (
  10638. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10639. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10640. (flag&entry->flag) || // Matched flag
  10641. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10642. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10643. )
  10644. )
  10645. {
  10646. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10647. pc_bonus_script_free_entry(sd, entry);
  10648. count++;
  10649. }
  10650. node = next;
  10651. }
  10652. pc_bonus_script_check_final(sd);
  10653. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10654. status_calc_pc(sd,SCO_NONE);
  10655. }
  10656. /** [Cydh]
  10657. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10658. * @param sd: Target player
  10659. */
  10660. void pc_cell_basilica(struct map_session_data *sd) {
  10661. nullpo_retv(sd);
  10662. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10663. if (sd->sc.data[SC_BASILICA])
  10664. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10665. }
  10666. else if (!sd->sc.data[SC_BASILICA])
  10667. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,INFINITE_TICK);
  10668. }
  10669. /** [Cydh]
  10670. * Get maximum specified parameter for specified class
  10671. * @param class_: sd->class
  10672. * @param sex: sd->status.sex
  10673. * @param flag: parameter will be checked
  10674. * @return max_param
  10675. */
  10676. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10677. int idx = -1, class_ = sd->class_;
  10678. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10679. short max_param = 0;
  10680. switch (param) {
  10681. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10682. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10683. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10684. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10685. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10686. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10687. }
  10688. if (max_param > 0)
  10689. return max_param;
  10690. }
  10691. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10692. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10693. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  10694. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10695. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10696. }
  10697. /**
  10698. * Get max ASPD for player based on Class
  10699. * @param sd Player
  10700. * @return ASPD
  10701. */
  10702. short pc_maxaspd(struct map_session_data *sd) {
  10703. nullpo_ret(sd);
  10704. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10705. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10706. battle_config.max_aspd ));
  10707. }
  10708. /**
  10709. * Calculates total item-group related bonuses for the given item
  10710. * @param sd Player
  10711. * @param nameid Item ID
  10712. * @return Heal rate
  10713. **/
  10714. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10715. if (sd->itemgrouphealrate.empty())
  10716. return 0;
  10717. short bonus = 0;
  10718. for (const auto &it : sd->itemgrouphealrate) {
  10719. uint16 group_id = it.id, i;
  10720. struct s_item_group_db *group = NULL;
  10721. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10722. continue;
  10723. for (i = 0; i < group->random[0].data_qty; i++) {
  10724. if (group->random[0].data[i].nameid == nameid) {
  10725. bonus += it.val;
  10726. break;
  10727. }
  10728. }
  10729. }
  10730. return bonus;
  10731. }
  10732. /**
  10733. * Calculates total item-group related bonuses for the given item group
  10734. * @param sd Player
  10735. * @param group_id Item Group ID
  10736. * @return Heal rate
  10737. **/
  10738. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10739. if (sd->itemgrouphealrate.empty())
  10740. return 0;
  10741. for (const auto &it : sd->itemgrouphealrate) {
  10742. if (it.id == group_id)
  10743. return it.val;
  10744. }
  10745. return 0;
  10746. }
  10747. /**
  10748. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10749. * @param eqi Item EQI of enum equip_index
  10750. * @param *equip_index Player's equip_index[]
  10751. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10752. * @return True if item in same inventory index, False if doesn't
  10753. */
  10754. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10755. if (index < 0 || index >= MAX_INVENTORY)
  10756. return true;
  10757. // Dual weapon checks
  10758. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10759. return true;
  10760. // Headgear with Mid & Low location
  10761. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10762. return true;
  10763. // Headgear with Top & Mid or Low location
  10764. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10765. return true;
  10766. // Headgear with Mid & Low location
  10767. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10768. return true;
  10769. // Headgear with Top & Mid or Low location
  10770. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10771. return true;
  10772. return false;
  10773. }
  10774. /**
  10775. * Generate Unique item ID for player
  10776. * @param sd : Player
  10777. * @return A generated Unique item ID
  10778. */
  10779. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10780. nullpo_ret(sd);
  10781. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10782. }
  10783. /**
  10784. * Validating skill from player after logged on
  10785. * @param sd
  10786. **/
  10787. void pc_validate_skill(struct map_session_data *sd) {
  10788. if (sd) {
  10789. uint16 i = 0, count = 0;
  10790. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10791. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10792. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10793. for (i = 0; i < MAX_SKILL; i++) {
  10794. uint16 idx = 0;
  10795. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10796. continue;
  10797. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10798. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10799. count++;
  10800. }
  10801. else
  10802. 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);
  10803. }
  10804. }
  10805. }
  10806. /**
  10807. * Toggle to remember if the questinfo is displayed yet or not.
  10808. * @param qi_display Display flag
  10809. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10810. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10811. **/
  10812. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10813. if (show) {
  10814. // Check if need to be displayed
  10815. if ((*qi_display) != 1) {
  10816. (*qi_display) = 1;
  10817. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10818. }
  10819. }
  10820. else {
  10821. // Check if need to be hide
  10822. if ((*qi_display) != 0) {
  10823. (*qi_display) = 0;
  10824. #if PACKETVER >= 20120410
  10825. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10826. #else
  10827. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10828. #endif
  10829. }
  10830. }
  10831. }
  10832. /**
  10833. * Show available NPC Quest / Event Icon Check [Kisuka]
  10834. * @param sd Player
  10835. **/
  10836. void pc_show_questinfo(struct map_session_data *sd) {
  10837. #if PACKETVER >= 20090218
  10838. struct questinfo *qi = NULL;
  10839. unsigned short i;
  10840. uint8 j;
  10841. int8 mystate = 0;
  10842. bool failed = false;
  10843. nullpo_retv(sd);
  10844. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10845. return;
  10846. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10847. if (!mapdata->qi_count || !mapdata->qi_data)
  10848. return;
  10849. if (mapdata->qi_count != sd->qi_count)
  10850. return; // init was not called yet
  10851. for(i = 0; i < mapdata->qi_count; i++) {
  10852. qi = &mapdata->qi_data[i];
  10853. if (!qi)
  10854. continue;
  10855. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10856. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10857. continue;
  10858. }
  10859. // Level range checks
  10860. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10861. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10862. continue;
  10863. }
  10864. // Quest requirements
  10865. if (qi->req_count) {
  10866. failed = false;
  10867. for (j = 0; j < qi->req_count; j++) {
  10868. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10869. mystate = mystate + (mystate < 1);
  10870. if (mystate != qi->req[j].state) {
  10871. failed = true;
  10872. break;
  10873. }
  10874. }
  10875. if (failed) {
  10876. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10877. continue;
  10878. }
  10879. }
  10880. // Job requirements
  10881. if (qi->jobid_count) {
  10882. failed = true;
  10883. for (j = 0; j < qi->jobid_count; j++) {
  10884. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10885. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10886. failed = false;
  10887. break;
  10888. }
  10889. }
  10890. if (!failed)
  10891. continue;
  10892. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10893. }
  10894. else {
  10895. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10896. }
  10897. }
  10898. #endif
  10899. }
  10900. /**
  10901. * Reinit the questinfo for player when changing map
  10902. * @param sd Player
  10903. **/
  10904. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10905. #if PACKETVER >= 20090218
  10906. nullpo_retv(sd);
  10907. if (sd->qi_display) {
  10908. aFree(sd->qi_display);
  10909. sd->qi_display = NULL;
  10910. }
  10911. sd->qi_count = 0;
  10912. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10913. return;
  10914. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10915. if (!mapdata->qi_count || !mapdata->qi_data)
  10916. return;
  10917. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
  10918. #endif
  10919. }
  10920. /**
  10921. * Check if a job is allowed to enter the map
  10922. * @param jobid Job ID see enum e_job or sd->status.class_
  10923. * @param m ID -an index- for direct indexing map[] array
  10924. * @return 1 if job is allowed, 0 otherwise
  10925. **/
  10926. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10927. uint16 idx = 0;
  10928. // Map is other map server.
  10929. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10930. if (m < 0)
  10931. return true;
  10932. struct map_data *mapdata = map_getmapdata(m);
  10933. if (!mapdata->cell)
  10934. return false;
  10935. if (!pcdb_checkid(jobid))
  10936. return false;
  10937. idx = pc_class2idx(jobid);
  10938. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10939. return true;
  10940. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  10941. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  10942. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  10943. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  10944. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  10945. (job_info[idx].noenter_map.zone&(8*mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  10946. )
  10947. return false;
  10948. return true;
  10949. }
  10950. /**
  10951. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  10952. * @param sd
  10953. **/
  10954. void pc_set_costume_view(struct map_session_data *sd) {
  10955. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  10956. struct item_data *id = NULL;
  10957. nullpo_retv(sd);
  10958. head_low = sd->status.head_bottom;
  10959. head_mid = sd->status.head_mid;
  10960. head_top = sd->status.head_top;
  10961. robe = sd->status.robe;
  10962. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  10963. //Added check to prevent sending the same look on multiple slots ->
  10964. //causes client to redraw item on top of itself. (suggested by Lupus)
  10965. // Normal headgear checks
  10966. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10967. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  10968. sd->status.head_bottom = id->look;
  10969. else
  10970. sd->status.head_bottom = 0;
  10971. }
  10972. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10973. if (!(id->equip&(EQP_HEAD_TOP)))
  10974. sd->status.head_mid = id->look;
  10975. else
  10976. sd->status.head_mid = 0;
  10977. }
  10978. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10979. sd->status.head_top = id->look;
  10980. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10981. sd->status.robe = id->look;
  10982. // Costumes check
  10983. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  10984. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  10985. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  10986. sd->status.head_bottom = id->look;
  10987. else
  10988. sd->status.head_bottom = 0;
  10989. }
  10990. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  10991. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  10992. sd->status.head_mid = id->look;
  10993. else
  10994. sd->status.head_mid = 0;
  10995. }
  10996. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  10997. sd->status.head_top = id->look;
  10998. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  10999. sd->status.robe = id->look;
  11000. }
  11001. if (sd->setlook_head_bottom)
  11002. sd->status.head_bottom = sd->setlook_head_bottom;
  11003. if (sd->setlook_head_mid)
  11004. sd->status.head_mid = sd->setlook_head_mid;
  11005. if (sd->setlook_head_top)
  11006. sd->status.head_top = sd->setlook_head_top;
  11007. if (sd->setlook_robe)
  11008. sd->status.robe = sd->setlook_robe;
  11009. if (head_low != sd->status.head_bottom)
  11010. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11011. if (head_mid != sd->status.head_mid)
  11012. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11013. if (head_top != sd->status.head_top)
  11014. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11015. if (robe != sd->status.robe)
  11016. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11017. }
  11018. struct s_attendance_period* pc_attendance_period(){
  11019. uint32 date = date_get(DT_YYYYMMDD);
  11020. for( struct s_attendance_period& period : attendance_periods ){
  11021. if( period.start <= date && period.end >= date ){
  11022. return &period;
  11023. }
  11024. }
  11025. return nullptr;
  11026. }
  11027. bool pc_attendance_enabled(){
  11028. // Check if the attendance feature is disabled
  11029. if( !battle_config.feature_attendance ){
  11030. return false;
  11031. }
  11032. // Check if there is a running attendance period
  11033. return pc_attendance_period() != nullptr;
  11034. }
  11035. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11036. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11037. }
  11038. int32 pc_attendance_counter( struct map_session_data* sd ){
  11039. struct s_attendance_period* period = pc_attendance_period();
  11040. // No running attendance period
  11041. if( period == nullptr ){
  11042. return 0;
  11043. }
  11044. // Get the counter for the current period
  11045. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11046. // Check if we have a remaining counter from a previous period
  11047. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11048. // Reset the counter to zero
  11049. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11050. return 0;
  11051. }
  11052. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11053. }
  11054. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11055. // If the user's group does not have the permission
  11056. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11057. return;
  11058. }
  11059. // Check if the attendance feature is disabled
  11060. if( !pc_attendance_enabled() ){
  11061. return;
  11062. }
  11063. // Check if the user already got his reward today
  11064. if( pc_attendance_rewarded_today( sd ) ){
  11065. return;
  11066. }
  11067. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11068. attendance_counter += 1;
  11069. struct s_attendance_period* period = pc_attendance_period();
  11070. if( period == nullptr ){
  11071. return;
  11072. }
  11073. if( period->rewards.size() < attendance_counter ){
  11074. return;
  11075. }
  11076. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11077. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11078. if( save_settings&CHARSAVE_ATTENDANCE )
  11079. chrif_save(sd, CSAVE_NORMAL);
  11080. struct s_attendance_reward& reward = period->rewards.at( attendance_counter - 1 );
  11081. struct mail_message msg;
  11082. memset( &msg, 0, sizeof( struct mail_message ) );
  11083. msg.dest_id = sd->status.char_id;
  11084. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11085. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11086. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11087. msg.item[0].nameid = reward.item_id;
  11088. msg.item[0].amount = reward.amount;
  11089. msg.item[0].identify = 1;
  11090. msg.status = MAIL_NEW;
  11091. msg.type = MAIL_INBOX_NORMAL;
  11092. msg.timestamp = time(NULL);
  11093. intif_Mail_send(0, &msg);
  11094. clif_attendence_response( sd, attendance_counter );
  11095. }
  11096. void pc_attendance_load( std::string path ){
  11097. YAML::Node root;
  11098. try{
  11099. root = YAML::LoadFile( path );
  11100. }catch( ... ){
  11101. ShowError( "pc_attendance_load: Failed to read attendance configuration file \"%s\".\n", path.c_str() );
  11102. return;
  11103. }
  11104. if( root["Attendance"] ){
  11105. YAML::Node attendance = root["Attendance"];
  11106. for( const auto &periodNode : attendance ){
  11107. if( !periodNode["Start"].IsDefined() ){
  11108. ShowError( "pc_attendance_load: Missing \"Start\" for period in line %d.\n", periodNode.Mark().line );
  11109. continue;
  11110. }
  11111. YAML::Node startNode = periodNode["Start"];
  11112. if( !periodNode["End"].IsDefined() ){
  11113. ShowError( "pc_attendance_load: Missing \"End\" for period in line %d.\n", periodNode.Mark().line );
  11114. continue;
  11115. }
  11116. YAML::Node endNode = periodNode["End"];
  11117. if( !periodNode["Rewards"].IsDefined() ){
  11118. ShowError( "pc_attendance_load: Missing \"Rewards\" for period in line %d.\n", periodNode.Mark().line );
  11119. continue;
  11120. }
  11121. YAML::Node rewardsNode = periodNode["Rewards"];
  11122. uint32 start = startNode.as<uint32>();
  11123. uint32 end = endNode.as<uint32>();
  11124. // If the period is outdated already, we do not even bother parsing
  11125. if( end < date_get( DT_YYYYMMDD ) ){
  11126. continue;
  11127. }
  11128. // Collision detection
  11129. bool collision = false;
  11130. for( struct s_attendance_period& period : attendance_periods ){
  11131. // Check if start is inside another period
  11132. if( period.start <= start && start <= period.end ){
  11133. ShowError( "pc_attendance_load: period start %u intersects with period %u-%u.\n", start, period.start, period.end );
  11134. collision = true;
  11135. break;
  11136. }
  11137. // Check if end is inside another period
  11138. if( period.start <= end && end <= period.end ){
  11139. ShowError( "pc_attendance_load: period end %u intersects with period %u-%u.\n", start, period.start, period.end );
  11140. collision = true;
  11141. break;
  11142. }
  11143. }
  11144. if( collision ){
  11145. continue;
  11146. }
  11147. struct s_attendance_period period;
  11148. period.start = start;
  11149. period.end = end;
  11150. for( const auto& rewardNode : rewardsNode ){
  11151. if( !rewardNode["Day"].IsDefined() ){
  11152. ShowError( "pc_attendance_load: No day defined for node in line %d.\n", rewardNode.Mark().line );
  11153. continue;
  11154. }
  11155. uint32 day = rewardNode["Day"].as<uint32>();
  11156. if( !rewardNode["ItemId"].IsDefined() ){
  11157. ShowError( "pc_attendance_load: No reward defined for day %d.\n", day );
  11158. continue;
  11159. }
  11160. YAML::Node itemNode = rewardNode["ItemId"];
  11161. uint16 item_id = itemNode.as<uint16>();
  11162. if( item_id == 0 || !itemdb_exists( item_id ) ){
  11163. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day );
  11164. continue;
  11165. }
  11166. uint16 amount;
  11167. if( rewardNode["Amount"] ){
  11168. amount = rewardNode["Amount"].as<uint16>();
  11169. if( amount == 0 ){
  11170. ShowError( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day );
  11171. amount = 1;
  11172. }else if( amount > MAX_AMOUNT ){
  11173. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day, MAX_AMOUNT );
  11174. amount = MAX_AMOUNT;
  11175. }
  11176. }else{
  11177. amount = 1;
  11178. }
  11179. struct s_attendance_reward* reward = &period.rewards[day - 1];
  11180. reward->item_id = item_id;
  11181. reward->amount = amount;
  11182. }
  11183. bool missing_day = false;
  11184. for( int day = 0; day < period.rewards.size(); day++ ){
  11185. if( !util::map_exists( period.rewards, day ) ){
  11186. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  11187. missing_day = true;
  11188. break;
  11189. }
  11190. }
  11191. if( missing_day ){
  11192. continue;
  11193. }
  11194. attendance_periods.push_back( period );
  11195. }
  11196. }
  11197. }
  11198. void pc_read_attendance(){
  11199. char path[1024];
  11200. sprintf( path, "%s/%sattendance.yml", db_path, DBPATH );
  11201. pc_attendance_load( path );
  11202. sprintf( path, "%s/%s/attendance.yml", db_path, DBIMPORT );
  11203. pc_attendance_load( path );
  11204. }
  11205. /*==========================================
  11206. * pc Init/Terminate
  11207. *------------------------------------------*/
  11208. void do_final_pc(void) {
  11209. db_destroy(itemcd_db);
  11210. do_final_pc_groups();
  11211. ers_destroy(pc_sc_display_ers);
  11212. ers_destroy(num_reg_ers);
  11213. ers_destroy(str_reg_ers);
  11214. attendance_periods.clear();
  11215. }
  11216. void do_init_pc(void) {
  11217. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11218. pc_readdb();
  11219. pc_read_motd(); // Read MOTD [Valaris]
  11220. pc_read_attendance();
  11221. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11222. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11223. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11224. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11225. add_timer_func_list(pc_autosave, "pc_autosave");
  11226. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11227. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11228. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11229. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11230. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11231. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11232. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11233. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11234. // 0=day, 1=night [Yor]
  11235. night_flag = battle_config.night_at_start ? 1 : 0;
  11236. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11237. int day_duration = battle_config.day_duration;
  11238. int night_duration = battle_config.night_duration;
  11239. // add night/day timer [Yor]
  11240. add_timer_func_list(map_day_timer, "map_day_timer");
  11241. add_timer_func_list(map_night_timer, "map_night_timer");
  11242. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11243. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11244. }
  11245. do_init_pc_groups();
  11246. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11247. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11248. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11249. ers_chunk_size(pc_sc_display_ers, 150);
  11250. ers_chunk_size(num_reg_ers, 300);
  11251. ers_chunk_size(str_reg_ers, 50);
  11252. }