pc.c 380 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h" // get_svn_revision()
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/random.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/socket.h" // session[]
  10. #include "../common/strlib.h" // safestrncpy()
  11. #include "../common/timer.h"
  12. #include "../common/utils.h"
  13. #include "../common/mmo.h" //NAME_LENGTH
  14. #include "atcommand.h" // get_atcommand_level()
  15. #include "map.h"
  16. #include "battle.h" // battle_config
  17. #include "battleground.h"
  18. #include "channel.h"
  19. #include "chat.h"
  20. #include "chrif.h"
  21. #include "date.h" // is_day_of_*()
  22. #include "duel.h"
  23. #include "intif.h"
  24. #include "homunculus.h"
  25. #include "instance.h"
  26. #include "mercenary.h"
  27. #include "elemental.h"
  28. #include "pet.h" // pet_unlocktarget()
  29. #include "party.h" // party_search()
  30. #include "storage.h"
  31. #include "quest.h"
  32. #include <stdlib.h>
  33. #include <math.h>
  34. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  35. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  36. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  37. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  38. static unsigned int statp[MAX_LEVEL+1];
  39. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  40. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  41. #endif
  42. // h-files are for declarations, not for implementations... [Shinomori]
  43. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  44. // timer for night.day implementation
  45. int day_timer_tid = INVALID_TIMER;
  46. int night_timer_tid = INVALID_TIMER;
  47. struct eri *pc_sc_display_ers = NULL;
  48. struct eri *pc_itemgrouphealrate_ers = NULL;
  49. int pc_expiration_tid = INVALID_TIMER;
  50. struct fame_list smith_fame_list[MAX_FAME_LIST];
  51. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  52. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  53. #define MOTD_LINE_SIZE 128
  54. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  55. /**
  56. * Translation table from athena equip index to aegis bitmask
  57. */
  58. unsigned int equip_bitmask[EQI_MAX] = {
  59. EQP_ACC_L, // EQI_ACC_L
  60. EQP_ACC_R, // EQI_ACC_R
  61. EQP_SHOES, // EQI_SHOES
  62. EQP_GARMENT, // EQI_GARMENT
  63. EQP_HEAD_LOW, // EQI_HEAD_LOW
  64. EQP_HEAD_MID, // EQI_HEAD_MID
  65. EQP_HEAD_TOP, // EQI_HEAD_TOP
  66. EQP_ARMOR, // EQI_ARMOR
  67. EQP_HAND_L, // EQI_HAND_L
  68. EQP_HAND_R, // EQI_HAND_R
  69. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  70. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  71. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  72. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  73. EQP_AMMO, // EQI_AMMO
  74. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  75. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  76. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  77. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  78. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  79. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  80. };
  81. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  82. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  83. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  84. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  85. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  86. };
  87. /**
  88. * Item Cool Down Delay Saving
  89. * Struct item_cd is not a member of struct map_session_data
  90. * to keep cooldowns in memory between player log-ins.
  91. * All cooldowns are reset when server is restarted.
  92. **/
  93. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  94. struct item_cd {
  95. unsigned int tick[MAX_ITEMDELAYS]; //tick
  96. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  97. };
  98. /**
  99. * Converts a class to its array index for CLASS_COUNT defined arrays.
  100. * Note that it does not do a validity check for speed purposes, where parsing
  101. * player input make sure to use a pcdb_checkid first!
  102. * @param class_ Job ID see enum e_job
  103. * @return Class Index
  104. */
  105. int pc_class2idx(int class_) {
  106. if (class_ >= JOB_NOVICE_HIGH)
  107. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  108. return class_;
  109. }
  110. /**
  111. * Get player's group ID
  112. * @param sd
  113. * @return Group ID
  114. */
  115. inline int pc_get_group_id(struct map_session_data *sd) {
  116. return sd->group_id;
  117. }
  118. /** Get player's group Level
  119. * @param sd
  120. * @return Group Level
  121. */
  122. inline int pc_get_group_level(struct map_session_data *sd) {
  123. return sd->group_level;
  124. }
  125. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  126. {
  127. struct map_session_data *sd;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if(sd->invincible_timer != tid){
  131. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  132. return 0;
  133. }
  134. sd->invincible_timer = INVALID_TIMER;
  135. skill_unit_move(&sd->bl,tick,1);
  136. return 0;
  137. }
  138. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  139. nullpo_retv(sd);
  140. if( sd->invincible_timer != INVALID_TIMER )
  141. delete_timer(sd->invincible_timer,pc_invincible_timer);
  142. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  143. }
  144. void pc_delinvincibletimer(struct map_session_data* sd)
  145. {
  146. nullpo_retv(sd);
  147. if( sd->invincible_timer != INVALID_TIMER )
  148. {
  149. delete_timer(sd->invincible_timer,pc_invincible_timer);
  150. sd->invincible_timer = INVALID_TIMER;
  151. skill_unit_move(&sd->bl,gettick(),1);
  152. }
  153. }
  154. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  155. {
  156. struct map_session_data *sd;
  157. int i;
  158. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  159. return 1;
  160. if( sd->spiritball <= 0 )
  161. {
  162. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  163. sd->spiritball = 0;
  164. return 0;
  165. }
  166. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  167. if( i == sd->spiritball )
  168. {
  169. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  170. return 0;
  171. }
  172. sd->spiritball--;
  173. if( i != sd->spiritball )
  174. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  175. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  176. clif_spiritball(&sd->bl);
  177. return 0;
  178. }
  179. /**
  180. * Adds a spiritball to player for 'interval' ms
  181. * @param sd
  182. * @param interval
  183. * @param max
  184. */
  185. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  186. {
  187. int tid;
  188. uint8 i;
  189. nullpo_retv(sd);
  190. if(max > MAX_SPIRITBALL)
  191. max = MAX_SPIRITBALL;
  192. if(sd->spiritball < 0)
  193. sd->spiritball = 0;
  194. if( sd->spiritball && sd->spiritball >= max )
  195. {
  196. if(sd->spirit_timer[0] != INVALID_TIMER)
  197. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  198. sd->spiritball--;
  199. if( sd->spiritball != 0 )
  200. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  201. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  202. }
  203. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  204. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  205. if( i != sd->spiritball )
  206. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  207. sd->spirit_timer[i] = tid;
  208. sd->spiritball++;
  209. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  210. clif_millenniumshield(&sd->bl,sd->spiritball);
  211. else
  212. clif_spiritball(&sd->bl);
  213. }
  214. /**
  215. * Removes number of spiritball from player
  216. * @param sd
  217. * @param count
  218. * @param type 1 = doesn't give client effect
  219. */
  220. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  221. {
  222. uint8 i;
  223. nullpo_retv(sd);
  224. if(sd->spiritball <= 0) {
  225. sd->spiritball = 0;
  226. return;
  227. }
  228. if(count == 0)
  229. return;
  230. if(count > sd->spiritball)
  231. count = sd->spiritball;
  232. sd->spiritball -= count;
  233. if(count > MAX_SPIRITBALL)
  234. count = MAX_SPIRITBALL;
  235. for(i=0;i<count;i++) {
  236. if(sd->spirit_timer[i] != INVALID_TIMER) {
  237. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  238. sd->spirit_timer[i] = INVALID_TIMER;
  239. }
  240. }
  241. for(i=count;i<MAX_SPIRITBALL;i++) {
  242. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  243. sd->spirit_timer[i] = INVALID_TIMER;
  244. }
  245. if(!type) {
  246. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  247. clif_millenniumshield(&sd->bl,sd->spiritball);
  248. else
  249. clif_spiritball(&sd->bl);
  250. }
  251. }
  252. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  253. int *c, *b_sd;
  254. struct block_list *src;
  255. struct map_session_data *tsd;
  256. struct status_change *sc;
  257. nullpo_ret(bl);
  258. nullpo_ret(tsd = (struct map_session_data*)bl);
  259. nullpo_ret(src = va_arg(ap,struct block_list *));
  260. c = va_arg(ap,int *);
  261. b_sd = va_arg(ap, int *);
  262. if(pc_isdead(tsd))
  263. return 0;
  264. sc = status_get_sc(bl);
  265. if( sc && sc->data[SC_BANDING] )
  266. {
  267. b_sd[(*c)++] = tsd->bl.id;
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  273. int c;
  274. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  275. int i, j, hp, extra_hp = 0, tmp_qty = 0;
  276. int range = skill_get_splash(LG_BANDING,skill_lv);
  277. nullpo_ret(sd);
  278. c = 0;
  279. memset(b_sd, 0, sizeof(b_sd));
  280. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  281. if( c < 1 ) //just recalc status no need to recalc hp
  282. { // No more Royal Guards in Banding found.
  283. struct status_change *sc;
  284. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  285. {
  286. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  287. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  288. }
  289. return 0;
  290. }
  291. //Add yourself
  292. hp = status_get_hp(&sd->bl);
  293. i++;
  294. // Get total HP of all Royal Guards in party.
  295. for( j = 0; j < i; j++ ){
  296. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  297. if( bsd != NULL )
  298. hp += status_get_hp(&bsd->bl);
  299. }
  300. // Set average HP.
  301. hp = hp / i;
  302. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  303. for( j = 0; j < i; j++ )
  304. {
  305. int tmp_hp=0;
  306. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  307. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 ){
  308. extra_hp += tmp_hp;
  309. tmp_qty++;
  310. }
  311. }
  312. if( extra_hp > 0 && tmp_qty > 0 )
  313. hp += extra_hp / tmp_qty;
  314. for( j = 0; j < i; j++ ){
  315. struct map_session_data *bsd = map_id2sd(b_sd[j]);
  316. if( bsd != NULL )
  317. {
  318. struct status_change *sc;
  319. status_set_hp(&bsd->bl,hp,0); // Set hp
  320. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  321. {
  322. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  323. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  324. }
  325. }
  326. }
  327. return c;
  328. }
  329. /**
  330. * Increases a player's fame points and displays a notice to him
  331. * @param sd Player
  332. * @param count Fame point
  333. */
  334. void pc_addfame(struct map_session_data *sd,int count)
  335. {
  336. int ranktype=-1;
  337. nullpo_retv(sd);
  338. sd->status.fame += count;
  339. if(sd->status.fame > MAX_FAME)
  340. sd->status.fame = MAX_FAME;
  341. switch(sd->class_&MAPID_UPPERMASK){
  342. case MAPID_BLACKSMITH: ranktype = 0; break;
  343. case MAPID_ALCHEMIST: ranktype = 1; break;
  344. case MAPID_TAEKWON: ranktype = 2; break;
  345. }
  346. clif_update_rankingpoint(sd,ranktype,count);
  347. chrif_updatefamelist(sd);
  348. }
  349. /**
  350. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  351. * @param sd
  352. * @param job Job use enum e_mapid
  353. * @return Rank
  354. */
  355. unsigned char pc_famerank(uint32 char_id, int job)
  356. {
  357. uint8 i;
  358. switch(job){
  359. case MAPID_BLACKSMITH: // Blacksmith
  360. for(i = 0; i < MAX_FAME_LIST; i++){
  361. if(smith_fame_list[i].id == char_id)
  362. return i + 1;
  363. }
  364. break;
  365. case MAPID_ALCHEMIST: // Alchemist
  366. for(i = 0; i < MAX_FAME_LIST; i++){
  367. if(chemist_fame_list[i].id == char_id)
  368. return i + 1;
  369. }
  370. break;
  371. case MAPID_TAEKWON: // Taekwon
  372. for(i = 0; i < MAX_FAME_LIST; i++){
  373. if(taekwon_fame_list[i].id == char_id)
  374. return i + 1;
  375. }
  376. break;
  377. }
  378. return 0;
  379. }
  380. /**
  381. * Restart player's HP & SP value
  382. * @param sd
  383. * @param type Restart type: 1 - Normal Resurection
  384. */
  385. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  386. struct status_data *status, *b_status;
  387. nullpo_retv(sd);
  388. b_status = &sd->base_status;
  389. status = &sd->battle_status;
  390. if (type&1) { //Normal resurrection
  391. status->hp = 1; //Otherwise status_heal may fail if dead.
  392. status_heal(&sd->bl, b_status->hp, 0, 1);
  393. if( status->sp < b_status->sp )
  394. status_set_sp(&sd->bl, b_status->sp, 1);
  395. } else { //Just for saving on the char-server (with values as if respawned)
  396. sd->status.hp = b_status->hp;
  397. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  398. }
  399. }
  400. /*==========================================
  401. Rental System
  402. *------------------------------------------*/
  403. /**
  404. * Ends a rental and removes the item/effect
  405. * @param tid: Tick ID
  406. * @param tick: Timer
  407. * @param id: Timer ID
  408. * @param data: Data
  409. * @return false - failure, true - success
  410. */
  411. int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  412. {
  413. struct map_session_data *sd = map_id2sd(id);
  414. if( sd == NULL )
  415. return 0;
  416. if( tid != sd->rental_timer ) {
  417. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  418. return 0;
  419. }
  420. pc_inventory_rentals(sd);
  421. return 1;
  422. }
  423. /**
  424. * Removes the rental timer from the player
  425. * @param sd: Player data
  426. */
  427. void pc_inventory_rental_clear(struct map_session_data *sd)
  428. {
  429. if( sd->rental_timer != INVALID_TIMER ) {
  430. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  431. sd->rental_timer = INVALID_TIMER;
  432. }
  433. }
  434. /**
  435. * Check for items in the player's inventory that are rental type
  436. * @param sd: Player data
  437. */
  438. void pc_inventory_rentals(struct map_session_data *sd)
  439. {
  440. int i, c = 0;
  441. unsigned int expire_tick, next_tick = UINT_MAX;
  442. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  443. if( sd->status.inventory[i].nameid == 0 )
  444. continue; // Nothing here
  445. if( sd->status.inventory[i].expire_time == 0 )
  446. continue;
  447. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  448. if (sd->inventory_data[i]->unequip_script)
  449. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  450. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  451. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  452. } else {
  453. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  454. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  455. next_tick = umin(expire_tick, next_tick);
  456. c++;
  457. }
  458. }
  459. if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
  460. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  461. else
  462. sd->rental_timer = INVALID_TIMER;
  463. }
  464. /**
  465. * Add a rental item to the player and adjusts the rental timer appropriately
  466. * @param sd: Player data
  467. * @param seconds: Rental time
  468. */
  469. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  470. {
  471. unsigned int tick = seconds * 1000;
  472. if( sd == NULL )
  473. return;
  474. if( sd->rental_timer != INVALID_TIMER ) {
  475. const struct TimerData * td;
  476. td = get_timer(sd->rental_timer);
  477. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  478. pc_inventory_rental_clear(sd);
  479. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  480. }
  481. } else
  482. sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  483. }
  484. /**
  485. * Determines if player can give / drop / trade / vend items
  486. */
  487. bool pc_can_give_items(struct map_session_data *sd)
  488. {
  489. return pc_has_permission(sd, PC_PERM_TRADE);
  490. }
  491. /**
  492. * Determines if player can give / drop / trade / vend bounded items
  493. */
  494. bool pc_can_give_bounded_items(struct map_session_data *sd)
  495. {
  496. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  497. }
  498. /*==========================================
  499. * Prepares character for saving.
  500. * @param sd
  501. *------------------------------------------*/
  502. void pc_makesavestatus(struct map_session_data *sd) {
  503. nullpo_retv(sd);
  504. if(!battle_config.save_clothcolor)
  505. sd->status.clothes_color = 0;
  506. // Since this is currently not officially released,
  507. // its best to have a forced option to not save body styles.
  508. if(!battle_config.save_body_style)
  509. sd->status.body = 0;
  510. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  511. //status change load/saving. [Skotlex]
  512. #ifdef NEW_CARTS
  513. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  514. #else
  515. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  516. #endif
  517. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  518. if(pc_isdead(sd)){
  519. pc_setrestartvalue(sd, 0);
  520. } else {
  521. sd->status.hp = sd->battle_status.hp;
  522. sd->status.sp = sd->battle_status.sp;
  523. }
  524. sd->status.last_point.map = sd->mapindex;
  525. sd->status.last_point.x = sd->bl.x;
  526. sd->status.last_point.y = sd->bl.y;
  527. return;
  528. }
  529. if(pc_isdead(sd)) {
  530. pc_setrestartvalue(sd, 0);
  531. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  532. } else {
  533. sd->status.hp = sd->battle_status.hp;
  534. sd->status.sp = sd->battle_status.sp;
  535. sd->status.last_point.map = sd->mapindex;
  536. sd->status.last_point.x = sd->bl.x;
  537. sd->status.last_point.y = sd->bl.y;
  538. }
  539. if(map[sd->bl.m].flag.nosave) {
  540. struct map_data *m=&map[sd->bl.m];
  541. if(m->save.map)
  542. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  543. else
  544. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  545. }
  546. }
  547. /*==========================================
  548. * Off init ? Connection?
  549. *------------------------------------------*/
  550. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
  551. nullpo_retv(sd);
  552. sd->bl.id = account_id;
  553. sd->status.account_id = account_id;
  554. sd->status.char_id = char_id;
  555. sd->status.sex = sex;
  556. sd->login_id1 = login_id1;
  557. sd->login_id2 = 0; // at this point, we can not know the value :(
  558. sd->client_tick = client_tick;
  559. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  560. sd->bl.type = BL_PC;
  561. sd->canlog_tick = gettick();
  562. //Required to prevent homunculus copuing a base speed of 0.
  563. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  564. }
  565. /**
  566. * Get equip point for an equip
  567. * @param sd
  568. * @param n Equip index in inventory
  569. */
  570. int pc_equippoint(struct map_session_data *sd,int n){
  571. int ep = 0;
  572. nullpo_ret(sd);
  573. if(!sd->inventory_data[n])
  574. return 0;
  575. if (!itemdb_isequip2(sd->inventory_data[n]))
  576. return 0; //Not equippable by players.
  577. ep = sd->inventory_data[n]->equip;
  578. if(sd->inventory_data[n]->look == W_DAGGER ||
  579. sd->inventory_data[n]->look == W_1HSWORD ||
  580. sd->inventory_data[n]->look == W_1HAXE) {
  581. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  582. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  583. return EQP_ARMS;
  584. }
  585. return ep;
  586. }
  587. /**
  588. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  589. * @param sd : player session
  590. * @return 0 sucess, 1:invalid sd
  591. */
  592. void pc_setinventorydata(struct map_session_data *sd)
  593. {
  594. uint8 i;
  595. nullpo_retv(sd);
  596. for(i = 0; i < MAX_INVENTORY; i++) {
  597. unsigned short id = sd->status.inventory[i].nameid;
  598. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  599. }
  600. }
  601. /**
  602. * 'Calculates' weapon type
  603. * @param sd : player
  604. */
  605. void pc_calcweapontype(struct map_session_data *sd)
  606. {
  607. nullpo_retv(sd);
  608. // single-hand
  609. if(sd->weapontype2 == W_FIST) {
  610. sd->status.weapon = sd->weapontype1;
  611. return;
  612. }
  613. if(sd->weapontype1 == W_FIST) {
  614. sd->status.weapon = sd->weapontype2;
  615. return;
  616. }
  617. // dual-wield
  618. sd->status.weapon = 0;
  619. switch (sd->weapontype1){
  620. case W_DAGGER:
  621. switch (sd->weapontype2) {
  622. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  623. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  624. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  625. }
  626. break;
  627. case W_1HSWORD:
  628. switch (sd->weapontype2) {
  629. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  630. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  631. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  632. }
  633. break;
  634. case W_1HAXE:
  635. switch (sd->weapontype2) {
  636. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  637. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  638. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  639. }
  640. }
  641. // unknown, default to right hand type
  642. if (!sd->status.weapon)
  643. sd->status.weapon = sd->weapontype1;
  644. }
  645. /**
  646. * Set equip index
  647. * @param sd : Player
  648. */
  649. void pc_setequipindex(struct map_session_data *sd)
  650. {
  651. uint16 i;
  652. nullpo_retv(sd);
  653. for (i = 0; i < EQI_MAX; i++)
  654. sd->equip_index[i] = -1;
  655. for (i = 0; i < MAX_INVENTORY; i++) {
  656. if (sd->status.inventory[i].nameid <= 0)
  657. continue;
  658. if (sd->status.inventory[i].equip) {
  659. uint8 j;
  660. for (j = 0; j < EQI_MAX; j++)
  661. if (sd->status.inventory[i].equip & equip_bitmask[j])
  662. sd->equip_index[j] = i;
  663. if (sd->status.inventory[i].equip & EQP_HAND_R) {
  664. if (sd->inventory_data[i])
  665. sd->weapontype1 = sd->inventory_data[i]->look;
  666. else
  667. sd->weapontype1 = 0;
  668. }
  669. if( sd->status.inventory[i].equip & EQP_HAND_L ) {
  670. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  671. sd->weapontype2 = sd->inventory_data[i]->look;
  672. else
  673. sd->weapontype2 = 0;
  674. }
  675. }
  676. }
  677. pc_calcweapontype(sd);
  678. }
  679. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  680. //{
  681. // int i;
  682. // struct item *item = &sd->status.inventory[eqindex];
  683. // struct item_data *data;
  684. //
  685. // //Crafted/made/hatched items.
  686. // if (itemdb_isspecial(item->card[0]))
  687. // return 1;
  688. //
  689. // /* scan for enchant armor gems */
  690. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  691. // s = MAX_SLOTS - 1;
  692. //
  693. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  694. // return( i < s ) ? 0 : 1;
  695. //}
  696. /**
  697. * Check if an item is equiped by player
  698. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  699. * @param sd : player session
  700. * @param nameid : itemid
  701. * @return 1:yes, 0:no
  702. */
  703. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  704. {
  705. uint8 i;
  706. for( i = 0; i < EQI_MAX; i++ )
  707. {
  708. short index = sd->equip_index[i], j;
  709. if( index < 0 )
  710. continue;
  711. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  712. continue;
  713. if( !sd->inventory_data[index] )
  714. continue;
  715. if( sd->inventory_data[index]->nameid == nameid )
  716. return true;
  717. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  718. if( sd->status.inventory[index].card[j] == nameid )
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. /**
  725. * Check adoption rules
  726. * @param p1_sd: Player 1
  727. * @param p2_sd: Player 2
  728. * @param b_sd: Player that will be adopted
  729. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  730. * ADOPT_ALREADY_ADOPTED - Already adopted
  731. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  732. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  733. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  734. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  735. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  736. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  737. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  738. */
  739. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  740. {
  741. if( !p1_sd || !p2_sd || !b_sd )
  742. return ADOPT_CHARACTER_NOT_FOUND;
  743. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  744. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  745. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  746. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  747. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  748. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  749. if( p2_sd->status.party_id != p1_sd->status.party_id )
  750. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  751. // Parents need to have their ring equipped
  752. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  753. return ADOPT_EQUIP_RINGS;
  754. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  755. return ADOPT_EQUIP_RINGS;
  756. // Already adopted a baby
  757. if( p1_sd->status.child || p2_sd->status.child ) {
  758. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  759. return ADOPT_MORE_CHILDREN;
  760. }
  761. // Parents need at least lvl 70 to adopt
  762. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  763. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  764. return ADOPT_LEVEL_70;
  765. }
  766. if( b_sd->status.partner_id ) {
  767. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  768. return ADOPT_MARRIED;
  769. }
  770. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
  771. return ADOPT_NOT_NOVICE;
  772. return ADOPT_ALLOWED;
  773. }
  774. /*==========================================
  775. * Adoption Process
  776. *------------------------------------------*/
  777. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  778. {
  779. int job, joblevel;
  780. unsigned int jobexp;
  781. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  782. return false;
  783. // Preserve current job levels and progress
  784. joblevel = b_sd->status.job_level;
  785. jobexp = b_sd->status.job_exp;
  786. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  787. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  788. { // Success, proceed to configure parents and baby skills
  789. p1_sd->status.child = b_sd->status.char_id;
  790. p2_sd->status.child = b_sd->status.char_id;
  791. b_sd->status.father = p1_sd->status.char_id;
  792. b_sd->status.mother = p2_sd->status.char_id;
  793. // Restore progress
  794. b_sd->status.job_level = joblevel;
  795. clif_updatestatus(b_sd, SP_JOBLEVEL);
  796. b_sd->status.job_exp = jobexp;
  797. clif_updatestatus(b_sd, SP_JOBEXP);
  798. // Baby Skills
  799. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  800. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  801. // Parents Skills
  802. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  803. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  804. return true;
  805. }
  806. return false; // Job Change Fail
  807. }
  808. /*==========================================
  809. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  810. Returns:
  811. false : Cannot use/equip
  812. true : Can use/equip
  813. * Credits:
  814. [Inkfish] for first idea
  815. [Haru] for third-classes extension
  816. [Cydh] finishing :D
  817. *------------------------------------------*/
  818. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  819. while (1) {
  820. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  821. break;
  822. #ifndef RENEWAL
  823. //allow third classes to use trans. class items
  824. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  825. break;
  826. //third-baby classes can use same item too
  827. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  828. break;
  829. //don't need to decide specific rules for third-classes?
  830. //items for third classes can be used for all third classes
  831. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  832. break;
  833. #else
  834. //trans. classes (exl. third-trans.)
  835. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  836. break;
  837. //baby classes (exl. third-baby)
  838. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  839. break;
  840. //third classes (exl. third-trans. and baby-third)
  841. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  842. break;
  843. //trans-third classes
  844. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  845. break;
  846. //third-baby classes
  847. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  848. break;
  849. #endif
  850. return false;
  851. }
  852. return true;
  853. }
  854. /*=================================================
  855. * Checks if the player can equip the item at index n in inventory.
  856. * @param sd
  857. * @param n Item index in inventory
  858. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  859. *------------------------------------------------*/
  860. uint8 pc_isequip(struct map_session_data *sd,int n)
  861. {
  862. struct item_data *item;
  863. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  864. item = sd->inventory_data[n];
  865. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  866. return ITEM_EQUIP_ACK_OK;
  867. if(item == NULL)
  868. return ITEM_EQUIP_ACK_FAIL;
  869. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  870. return ITEM_EQUIP_ACK_FAILLEVEL;
  871. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  872. return ITEM_EQUIP_ACK_FAILLEVEL;
  873. if(item->sex != 2 && sd->status.sex != item->sex)
  874. return ITEM_EQUIP_ACK_FAIL;
  875. if (sd->sc.count) {
  876. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  877. return ITEM_EQUIP_ACK_FAIL;
  878. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  879. return ITEM_EQUIP_ACK_FAIL;
  880. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  881. return ITEM_EQUIP_ACK_FAIL;
  882. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  883. return ITEM_EQUIP_ACK_FAIL;
  884. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  885. return ITEM_EQUIP_ACK_FAIL;
  886. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  887. return ITEM_EQUIP_ACK_FAIL;
  888. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  889. //Spirit of Super Novice equip bonuses. [Skotlex]
  890. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  891. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  892. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  893. switch(item->look) { //In weapons, the look determines type of weapon.
  894. case W_DAGGER: //All level 4 - Daggers
  895. case W_1HSWORD: //All level 4 - 1H Swords
  896. case W_1HAXE: //All level 4 - 1H Axes
  897. case W_MACE: //All level 4 - 1H Maces
  898. case W_STAFF: //All level 4 - 1H Staves
  899. case W_2HSTAFF: //All level 4 - 2H Staves
  900. return ITEM_EQUIP_ACK_OK;
  901. }
  902. }
  903. }
  904. //fail to equip if item is restricted
  905. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  906. return ITEM_EQUIP_ACK_FAIL;
  907. //Not equipable by class. [Skotlex]
  908. if (!(1ULL<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  909. return ITEM_EQUIP_ACK_FAIL;
  910. if (!pc_isItemClass(sd,item))
  911. return ITEM_EQUIP_ACK_FAIL;
  912. return ITEM_EQUIP_ACK_OK;
  913. }
  914. /*==========================================
  915. * No problem with the session id
  916. * set the status that has been sent from char server
  917. *------------------------------------------*/
  918. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  919. {
  920. int i;
  921. #ifdef BOUND_ITEMS
  922. int j;
  923. int idxlist[MAX_INVENTORY];
  924. #endif
  925. unsigned long tick = gettick();
  926. uint32 ip = session[sd->fd]->client_addr;
  927. sd->login_id2 = login_id2;
  928. sd->group_id = group_id;
  929. /* load user permissions */
  930. pc_group_pc_load(sd);
  931. memcpy(&sd->status, st, sizeof(*st));
  932. if (st->sex != sd->status.sex) {
  933. clif_authfail_fd(sd->fd, 0);
  934. return false;
  935. }
  936. //Set the map-server used job id. [Skotlex]
  937. i = pc_jobid2mapid(sd->status.class_);
  938. if (i == -1) { //Invalid class?
  939. ShowError("pc_authok: Invalid class %d for player %s (%d:%d). Class was changed to novice.\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  940. sd->status.class_ = JOB_NOVICE;
  941. sd->class_ = MAPID_NOVICE;
  942. } else
  943. sd->class_ = i;
  944. // Checks and fixes to character status data, that are required
  945. // in case of configuration change or stuff, which cannot be
  946. // checked on char-server.
  947. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  948. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  949. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  950. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  951. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  952. if(!sd->status.hp) pc_setdead(sd);
  953. sd->state.connect_new = 1;
  954. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  955. sd->invincible_timer = INVALID_TIMER;
  956. sd->npc_timer_id = INVALID_TIMER;
  957. sd->pvp_timer = INVALID_TIMER;
  958. sd->expiration_tid = INVALID_TIMER;
  959. sd->autotrade_tid = INVALID_TIMER;
  960. #ifdef SECURE_NPCTIMEOUT
  961. // Initialize to defaults/expected
  962. sd->npc_idle_timer = INVALID_TIMER;
  963. sd->npc_idle_tick = tick;
  964. sd->npc_idle_type = NPCT_INPUT;
  965. sd->state.ignoretimeout = false;
  966. #endif
  967. sd->canuseitem_tick = tick;
  968. sd->canusecashfood_tick = tick;
  969. sd->canequip_tick = tick;
  970. sd->cantalk_tick = tick;
  971. sd->canskill_tick = tick;
  972. sd->cansendmail_tick = tick;
  973. sd->idletime = last_tick;
  974. for(i = 0; i < MAX_SPIRITBALL; i++)
  975. sd->spirit_timer[i] = INVALID_TIMER;
  976. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  977. sd->autobonus[i].active = INVALID_TIMER;
  978. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  979. sd->autobonus2[i].active = INVALID_TIMER;
  980. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  981. sd->autobonus3[i].active = INVALID_TIMER;
  982. if (battle_config.item_auto_get)
  983. sd->state.autoloot = 10000;
  984. if (battle_config.disp_experience)
  985. sd->state.showexp = 1;
  986. if (battle_config.disp_zeny)
  987. sd->state.showzeny = 1;
  988. #ifdef VIP_ENABLE
  989. if (!battle_config.vip_disp_rate)
  990. sd->vip.disableshowrate = 1;
  991. #endif
  992. if (!(battle_config.display_skill_fail&2))
  993. sd->state.showdelay = 1;
  994. pc_setinventorydata(sd);
  995. pc_setequipindex(sd);
  996. if( sd->status.option&OPTION_INVISIBLE && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  997. sd->status.option &= ~OPTION_INVISIBLE;
  998. }
  999. status_change_init(&sd->bl);
  1000. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1001. //Set here because we need the inventory data for weapon sprite parsing.
  1002. status_set_viewdata(&sd->bl, sd->status.class_);
  1003. unit_dataset(&sd->bl);
  1004. sd->guild_x = -1;
  1005. sd->guild_y = -1;
  1006. sd->delayed_damage = 0;
  1007. // Event Timers
  1008. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1009. sd->eventtimer[i] = INVALID_TIMER;
  1010. // Rental Timer
  1011. sd->rental_timer = INVALID_TIMER;
  1012. for( i = 0; i < 3; i++ )
  1013. sd->hate_mob[i] = -1;
  1014. sd->quest_log = NULL;
  1015. sd->num_quests = 0;
  1016. sd->avail_quests = 0;
  1017. sd->save_quest = false;
  1018. sd->count_rewarp = 0;
  1019. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1020. sd->regs.arrays = NULL;
  1021. sd->vars_dirty = false;
  1022. sd->vars_ok = false;
  1023. sd->vars_received = 0x0;
  1024. sd->qi_display = NULL;
  1025. sd->qi_count = 0;
  1026. //warp player
  1027. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1028. ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);
  1029. // try warping to a default map instead (church graveyard)
  1030. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1031. // if we fail again
  1032. clif_authfail_fd(sd->fd, 0);
  1033. return false;
  1034. }
  1035. }
  1036. clif_authok(sd);
  1037. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1038. sd->die_counter=-1;
  1039. //display login notice
  1040. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  1041. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  1042. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  1043. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  1044. sd->status.name, sd->status.account_id, sd->status.char_id,
  1045. sd->packet_ver, CONVIP(ip), sd->group_id);
  1046. // Send friends list
  1047. clif_friendslist_send(sd);
  1048. if( !changing_mapservers ) {
  1049. if (battle_config.display_version == 1)
  1050. pc_show_version(sd);
  1051. // Message of the Day [Valaris]
  1052. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1053. if (battle_config.motd_type)
  1054. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  1055. else
  1056. clif_displaymessage(sd->fd, motd_text[i]);
  1057. }
  1058. if (expiration_time != 0)
  1059. sd->expiration_time = expiration_time;
  1060. /**
  1061. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1062. **/
  1063. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1064. }
  1065. /**
  1066. * Check if player have any item cooldowns on
  1067. **/
  1068. pc_itemcd_do(sd,true);
  1069. pc_validate_skill(sd);
  1070. #ifdef BOUND_ITEMS
  1071. // Party bound item check
  1072. if(sd->status.party_id == 0 && (j = pc_bound_chk(sd,BOUND_PARTY,idxlist))) { // Party was deleted while character offline
  1073. for(i=0;i<j;i++)
  1074. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_OTHER);
  1075. }
  1076. #endif
  1077. /* [Ind] */
  1078. sd->sc_display = NULL;
  1079. sd->sc_display_count = 0;
  1080. // Player has not yet received the CashShop list
  1081. sd->status.cashshop_sent = false;
  1082. sd->last_addeditem_index = -1;
  1083. sd->bonus_script.head = NULL;
  1084. sd->bonus_script.count = 0;
  1085. #if PACKETVER >= 20150513
  1086. sd->hatEffectIDs = NULL;
  1087. sd->hatEffectCount = 0;
  1088. #endif
  1089. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1090. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1091. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1092. clif_updatestatus(sd, SP_BASEEXP);
  1093. }
  1094. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1095. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1096. clif_updatestatus(sd, SP_JOBEXP);
  1097. }
  1098. // Request all registries (auth is considered completed whence they arrive)
  1099. intif_request_registry(sd,7);
  1100. return true;
  1101. }
  1102. /*==========================================
  1103. * Closes a connection because it failed to be authenticated from the char server.
  1104. *------------------------------------------*/
  1105. void pc_authfail(struct map_session_data *sd)
  1106. {
  1107. clif_authfail_fd(sd->fd, 0);
  1108. return;
  1109. }
  1110. /**
  1111. * Player register a bl as hatred
  1112. * @param sd : player session
  1113. * @param pos : hate position [0;2]
  1114. * @param bl : target bl
  1115. * @return false:failed, true:success
  1116. */
  1117. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1118. {
  1119. int class_;
  1120. if (!sd || !bl || pos < 0 || pos > 2)
  1121. return false;
  1122. if (sd->hate_mob[pos] != -1)
  1123. { //Can't change hate targets.
  1124. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1125. return false;
  1126. }
  1127. class_ = status_get_class(bl);
  1128. if (!pcdb_checkid(class_)) {
  1129. unsigned int max_hp = status_get_max_hp(bl);
  1130. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1131. return false;
  1132. if (pos != status_get_size(bl))
  1133. return false; //Wrong size
  1134. }
  1135. sd->hate_mob[pos] = class_;
  1136. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1137. clif_hate_info(sd, pos, class_, 1);
  1138. return true;
  1139. }
  1140. /*==========================================
  1141. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1142. *------------------------------------------*/
  1143. void pc_reg_received(struct map_session_data *sd)
  1144. {
  1145. uint8 i;
  1146. sd->vars_ok = true;
  1147. sd->change_level_2nd = pc_readglobalreg(sd, add_str("jobchange_level"));
  1148. sd->change_level_3rd = pc_readglobalreg(sd, add_str("jobchange_level_3rd"));
  1149. sd->die_counter = pc_readglobalreg(sd, add_str("PC_DIE_COUNTER"));
  1150. sd->langtype = pc_readaccountreg(sd, add_str("#langtype"));
  1151. if (msg_checklangtype(sd->langtype,true) < 0)
  1152. sd->langtype = 0; //invalid langtype reset to default
  1153. // Cash shop
  1154. sd->cashPoints = pc_readaccountreg(sd, add_str("#CASHPOINTS"));
  1155. sd->kafraPoints = pc_readaccountreg(sd, add_str("#KAFRAPOINTS"));
  1156. // Cooking Exp
  1157. sd->cook_mastery = pc_readglobalreg(sd, add_str("COOK_MASTERY"));
  1158. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1159. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1160. sd->mission_mobid = pc_readglobalreg(sd, add_str("TK_MISSION_ID"));
  1161. sd->mission_count = pc_readglobalreg(sd, add_str("TK_MISSION_COUNT"));
  1162. }
  1163. if (battle_config.feature_banking)
  1164. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1165. if (battle_config.feature_roulette) {
  1166. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1167. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1168. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1169. }
  1170. sd->roulette.prizeIdx = -1;
  1171. //SG map and mob read [Komurka]
  1172. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1173. uint16 j;
  1174. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1175. sd->feel_map[i].index = j;
  1176. sd->feel_map[i].m = map_mapindex2mapid(j);
  1177. } else {
  1178. sd->feel_map[i].index = 0;
  1179. sd->feel_map[i].m = -1;
  1180. }
  1181. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1182. }
  1183. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1184. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1185. sd->cloneskill_idx = skill_get_index(skid);
  1186. if (sd->cloneskill_idx > 0) {
  1187. sd->status.skill[sd->cloneskill_idx].id = skid;
  1188. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1189. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1190. sd->status.skill[sd->cloneskill_idx].lv = i;
  1191. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1192. }
  1193. }
  1194. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1195. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1196. sd->reproduceskill_idx = skill_get_index(skid);
  1197. if (sd->reproduceskill_idx > 0) {
  1198. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1199. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1200. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1201. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1202. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1203. }
  1204. }
  1205. //Weird... maybe registries were reloaded?
  1206. if (sd->state.active)
  1207. return;
  1208. sd->state.active = 1;
  1209. if (sd->status.party_id)
  1210. party_member_joined(sd);
  1211. if (sd->status.guild_id)
  1212. guild_member_joined(sd);
  1213. // pet
  1214. if (sd->status.pet_id > 0)
  1215. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1216. // Homunculus [albator]
  1217. if( sd->status.hom_id > 0 )
  1218. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1219. if( sd->status.mer_id > 0 )
  1220. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1221. if( sd->status.ele_id > 0 )
  1222. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1223. map_addiddb(&sd->bl);
  1224. map_delnickdb(sd->status.char_id, sd->status.name);
  1225. if (!chrif_auth_finished(sd))
  1226. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1227. pc_load_combo(sd);
  1228. status_calc_pc(sd, (enum e_status_calc_opt)(SCO_FIRST|SCO_FORCE));
  1229. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1230. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1231. chrif_bsdata_request(sd->status.char_id);
  1232. sd->storage_size = MIN_STORAGE; //default to min
  1233. #ifdef VIP_ENABLE
  1234. sd->vip.time = 0;
  1235. sd->vip.enabled = 0;
  1236. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0); // request VIP information
  1237. #endif
  1238. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1239. intif_request_questlog(sd);
  1240. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1241. sd->state.connect_new = 1;
  1242. clif_parse_LoadEndAck(sd->fd, sd);
  1243. }
  1244. if( sd->sc.option&OPTION_INVISIBLE ) {
  1245. sd->vd.class_ = INVISIBLE_CLASS;
  1246. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1247. // decrement the number of pvp players on the map
  1248. map[sd->bl.m].users_pvp--;
  1249. if( map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ){
  1250. // unregister the player for ranking
  1251. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1252. sd->pvp_timer = INVALID_TIMER;
  1253. }
  1254. clif_changeoption( &sd->bl );
  1255. }
  1256. pc_check_expiration(sd);
  1257. if( sd->state.autotrade ) {
  1258. clif_parse_LoadEndAck(sd->fd, sd);
  1259. sd->autotrade_tid = add_timer(gettick() + battle_config.feature_autotrade_open_delay, pc_autotrade_timer, sd->bl.id, 0);
  1260. }
  1261. }
  1262. static int pc_calc_skillpoint(struct map_session_data* sd)
  1263. {
  1264. uint16 i, skill_point = 0;
  1265. nullpo_ret(sd);
  1266. for(i = 1; i < MAX_SKILL; i++) {
  1267. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1268. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1269. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1270. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1271. )
  1272. {
  1273. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1274. skill_point += sd->status.skill[i].lv;
  1275. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1276. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1277. }
  1278. }
  1279. }
  1280. return skill_point;
  1281. }
  1282. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1283. uint16 i = 0;
  1284. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1285. return false;
  1286. /**
  1287. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1288. * skill tree and since they have no icons they'll give resource errors
  1289. * Get ALL skills except npc/guild ones. [Skotlex]
  1290. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1291. **/
  1292. for( i = 0; i < MAX_SKILL; i++ ) {
  1293. uint16 skill_id = skill_idx2id(i);
  1294. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1295. continue;
  1296. switch (skill_id) {
  1297. case SM_SELFPROVOKE:
  1298. case AB_DUPLELIGHT_MELEE:
  1299. case AB_DUPLELIGHT_MAGIC:
  1300. case WL_CHAINLIGHTNING_ATK:
  1301. case WL_TETRAVORTEX_FIRE:
  1302. case WL_TETRAVORTEX_WATER:
  1303. case WL_TETRAVORTEX_WIND:
  1304. case WL_TETRAVORTEX_GROUND:
  1305. case WL_SUMMON_ATK_FIRE:
  1306. case WL_SUMMON_ATK_WIND:
  1307. case WL_SUMMON_ATK_WATER:
  1308. case WL_SUMMON_ATK_GROUND:
  1309. case LG_OVERBRAND_BRANDISH:
  1310. case LG_OVERBRAND_PLUSATK:
  1311. case WM_SEVERE_RAINSTORM_MELEE:
  1312. case RL_R_TRIP_PLUSATK:
  1313. case SG_DEVIL:
  1314. case MO_TRIPLEATTACK:
  1315. case RG_SNATCHER:
  1316. continue;
  1317. default:
  1318. {
  1319. uint8 lv = (uint8)skill_get_max(skill_id);
  1320. if (lv > 0) {
  1321. sd->status.skill[i].id = skill_id;
  1322. if (addlv)
  1323. sd->status.skill[i].lv = lv;
  1324. }
  1325. }
  1326. break;
  1327. }
  1328. }
  1329. return true;
  1330. }
  1331. /*==========================================
  1332. * Calculation of skill level.
  1333. * @param sd
  1334. *------------------------------------------*/
  1335. void pc_calc_skilltree(struct map_session_data *sd)
  1336. {
  1337. int i, flag;
  1338. int c = 0;
  1339. nullpo_retv(sd);
  1340. i = pc_calc_skilltree_normalize_job(sd);
  1341. c = pc_mapid2jobid(i, sd->status.sex);
  1342. if( c == -1 )
  1343. { //Unable to normalize job??
  1344. 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);
  1345. return;
  1346. }
  1347. c = pc_class2idx(c);
  1348. for( i = 0; i < MAX_SKILL; i++ ) {
  1349. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1350. sd->status.skill[i].id = 0; //First clear skills.
  1351. /* permanent skills that must be re-checked */
  1352. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1353. uint16 sk_id = skill_idx2id(i);
  1354. if (!sk_id) {
  1355. sd->status.skill[i].id = 0;
  1356. sd->status.skill[i].lv = 0;
  1357. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1358. continue;
  1359. }
  1360. switch (sk_id) {
  1361. case NV_TRICKDEAD:
  1362. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1363. sd->status.skill[i].id = 0;
  1364. sd->status.skill[i].lv = 0;
  1365. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1366. }
  1367. break;
  1368. }
  1369. }
  1370. }
  1371. for( i = 0; i < MAX_SKILL; i++ ) {
  1372. uint16 skill_id = 0;
  1373. // Restore original level of skills after deleting earned skills.
  1374. 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 ) {
  1375. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1376. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1377. }
  1378. //Enable Bard/Dancer spirit linked skills.
  1379. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1380. continue;
  1381. if( &sd->sc && sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1382. //Link Dancer skills to bard.
  1383. if( sd->status.sex ) {
  1384. if( sd->status.skill[i-8].lv < 10 )
  1385. continue;
  1386. sd->status.skill[i].id = skill_id;
  1387. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1388. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1389. }
  1390. //Link Bard skills to dancer.
  1391. else {
  1392. if( sd->status.skill[i].lv < 10 )
  1393. continue;
  1394. sd->status.skill[i-8].id = skill_id - 8;
  1395. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1396. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1397. }
  1398. }
  1399. }
  1400. // Removes Taekwon Ranker skill bonus
  1401. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1402. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1403. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1404. uint16 sk_id = skill_tree[c_][i].skill_id;
  1405. uint16 sk_idx = 0;
  1406. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1407. continue;
  1408. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1409. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1410. continue;
  1411. sd->status.skill[sk_idx].id = 0;
  1412. }
  1413. }
  1414. }
  1415. // Grant all skills
  1416. pc_grant_allskills(sd, false);
  1417. do {
  1418. uint16 skid = 0;
  1419. flag = 0;
  1420. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1421. bool fail = false;
  1422. uint16 sk_idx = skill_get_index(skid);
  1423. if (sd->status.skill[sk_idx].id)
  1424. continue; //Skill already known.
  1425. if (!battle_config.skillfree) {
  1426. uint8 j;
  1427. // Checking required skills
  1428. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1429. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1430. uint16 sk_need_idx = 0;
  1431. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1432. short sk_need = sk_need_id;
  1433. 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)
  1434. sk_need = 0; //Not learned.
  1435. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1436. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1437. else
  1438. sk_need = pc_checkskill(sd,sk_need_id);
  1439. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1440. fail = true;
  1441. break;
  1442. }
  1443. }
  1444. }
  1445. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1446. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1447. class_ = pc_class2idx(class_);
  1448. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1449. fail = true; // base level requirement wasn't satisfied
  1450. }
  1451. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1452. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1453. class_ = pc_class2idx(class_);
  1454. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1455. fail = true; // job level requirement wasn't satisfied
  1456. }
  1457. }
  1458. if (!fail) {
  1459. int inf2 = skill_get_inf2(skid);
  1460. if (!sd->status.skill[sk_idx].lv && (
  1461. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1462. inf2&INF2_WEDDING_SKILL ||
  1463. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1464. ))
  1465. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1466. sd->status.skill[sk_idx].id = skid;
  1467. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1468. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1469. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1470. }
  1471. flag = 1; // skill list has changed, perform another pass
  1472. }
  1473. }
  1474. } while(flag);
  1475. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1476. unsigned short skid = 0;
  1477. /* Taekwon Ranker Bonus Skill Tree
  1478. ============================================
  1479. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1480. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1481. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1482. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1483. uint16 sk_idx = 0;
  1484. if (!(sk_idx = skill_get_index(skid)))
  1485. continue;
  1486. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1487. continue; //Do not include Quest/Wedding skills.
  1488. if( sd->status.skill[sk_idx].id == 0 ) {
  1489. sd->status.skill[sk_idx].id = skid;
  1490. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1491. } else if( skid != NV_BASIC )
  1492. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1493. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1494. }
  1495. }
  1496. }
  1497. //Checks if you can learn a new skill after having leveled up a skill.
  1498. static void pc_check_skilltree(struct map_session_data *sd)
  1499. {
  1500. int i, flag = 0;
  1501. int c = 0;
  1502. if (battle_config.skillfree)
  1503. return; //Function serves no purpose if this is set
  1504. i = pc_calc_skilltree_normalize_job(sd);
  1505. c = pc_mapid2jobid(i, sd->status.sex);
  1506. if (c == -1) { //Unable to normalize job??
  1507. 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);
  1508. return;
  1509. }
  1510. c = pc_class2idx(c);
  1511. do {
  1512. uint16 skid = 0;
  1513. flag = 0;
  1514. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1515. uint16 sk_idx = skill_get_index(skid);
  1516. bool fail = false;
  1517. uint8 j = 0;
  1518. if (sd->status.skill[sk_idx].id) //Already learned
  1519. continue;
  1520. // Checking required skills
  1521. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1522. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1523. uint16 sk_need_idx = 0;
  1524. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1525. short sk_need = sk_need_id;
  1526. 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)
  1527. sk_need = 0; //Not learned.
  1528. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1529. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1530. else
  1531. sk_need = pc_checkskill(sd,sk_need_id);
  1532. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1533. fail = true;
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. if( fail )
  1539. continue;
  1540. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1541. continue;
  1542. j = skill_get_inf2(skid);
  1543. if( !sd->status.skill[sk_idx].lv && (
  1544. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1545. j&INF2_WEDDING_SKILL ||
  1546. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1547. ) )
  1548. continue; //Cannot be learned via normal means.
  1549. sd->status.skill[sk_idx].id = skid;
  1550. flag = 1;
  1551. }
  1552. } while(flag);
  1553. }
  1554. // Make sure all the skills are in the correct condition
  1555. // before persisting to the backend.. [MouseJstr]
  1556. void pc_clean_skilltree(struct map_session_data *sd)
  1557. {
  1558. uint16 i;
  1559. for (i = 0; i < MAX_SKILL; i++){
  1560. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1561. sd->status.skill[i].id = 0;
  1562. sd->status.skill[i].lv = 0;
  1563. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1564. }
  1565. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1566. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1567. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1568. }
  1569. }
  1570. }
  1571. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1572. {
  1573. int skill_point, novice_skills;
  1574. int c = sd->class_;
  1575. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1576. return c;
  1577. skill_point = pc_calc_skillpoint(sd);
  1578. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1579. // limit 1st class and above to novice job levels
  1580. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1581. {
  1582. c = MAPID_NOVICE;
  1583. }
  1584. // limit 2nd class and above to first class job levels (super novices are exempt)
  1585. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1586. {
  1587. // regenerate change_level_2nd
  1588. if (!sd->change_level_2nd)
  1589. {
  1590. if (sd->class_&JOBL_THIRD)
  1591. {
  1592. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1593. if (!sd->change_level_3rd)
  1594. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1595. else
  1596. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1597. - (sd->status.job_level - 1)
  1598. - (sd->change_level_3rd - 1)
  1599. - novice_skills;
  1600. }
  1601. else
  1602. {
  1603. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1604. - (sd->status.job_level - 1)
  1605. - novice_skills;
  1606. }
  1607. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  1608. }
  1609. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1610. {
  1611. c &= MAPID_BASEMASK;
  1612. }
  1613. // limit 3rd class to 2nd class/trans job levels
  1614. else if(sd->class_&JOBL_THIRD)
  1615. {
  1616. // regenerate change_level_3rd
  1617. if (!sd->change_level_3rd)
  1618. {
  1619. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1620. - (sd->status.job_level - 1)
  1621. - (sd->change_level_2nd - 1)
  1622. - novice_skills;
  1623. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  1624. }
  1625. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1626. c &= MAPID_UPPERMASK;
  1627. }
  1628. }
  1629. // restore non-limiting flags
  1630. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1631. return c;
  1632. }
  1633. /*==========================================
  1634. * Updates the weight status
  1635. *------------------------------------------
  1636. * 1: overweight 50%
  1637. * 2: overweight 90%
  1638. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1639. */
  1640. void pc_updateweightstatus(struct map_session_data *sd)
  1641. {
  1642. int old_overweight;
  1643. int new_overweight;
  1644. nullpo_retv(sd);
  1645. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1646. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1647. if( old_overweight == new_overweight )
  1648. return; // no change
  1649. // stop old status change
  1650. if( old_overweight == 1 )
  1651. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1652. else if( old_overweight == 2 )
  1653. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1654. // start new status change
  1655. if( new_overweight == 1 )
  1656. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1657. else if( new_overweight == 2 )
  1658. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1659. // update overweight status
  1660. sd->regen.state.overweight = new_overweight;
  1661. }
  1662. int pc_disguise(struct map_session_data *sd, int class_)
  1663. {
  1664. if (!class_ && !sd->disguise)
  1665. return 0;
  1666. if (class_ && sd->disguise == class_)
  1667. return 0;
  1668. if(sd->sc.option&OPTION_INVISIBLE)
  1669. { //Character is invisible. Stealth class-change. [Skotlex]
  1670. sd->disguise = class_; //viewdata is set on uncloaking.
  1671. return 2;
  1672. }
  1673. if (sd->bl.prev != NULL) {
  1674. pc_stop_walking(sd, 0);
  1675. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1676. }
  1677. if (!class_) {
  1678. sd->disguise = 0;
  1679. class_ = sd->status.class_;
  1680. } else
  1681. sd->disguise=class_;
  1682. status_set_viewdata(&sd->bl, class_);
  1683. clif_changeoption(&sd->bl);
  1684. if (sd->bl.prev != NULL) {
  1685. clif_spawn(&sd->bl);
  1686. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1687. { //It seems the cart info is lost on undisguise.
  1688. clif_cartlist(sd);
  1689. clif_updatestatus(sd,SP_CARTINFO);
  1690. }
  1691. if (sd->chatID) {
  1692. struct chat_data* cd;
  1693. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1694. clif_dispchat(cd,0);
  1695. }
  1696. }
  1697. return 1;
  1698. }
  1699. /// Show error message
  1700. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1701. /// Check for valid Element, break & show error message if invalid Element
  1702. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1703. /// Check for valid Race, break & show error message if invalid Race
  1704. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1705. /// Check for valid Race2, break & show error message if invalid Race2
  1706. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1707. /// Check for valid Class, break & show error message if invalid Class
  1708. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1709. /// Check for valid Size, break & show error message if invalid Size
  1710. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1711. /// Check for valid SC, break & show error message if invalid SC
  1712. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1713. static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
  1714. {
  1715. uint8 i;
  1716. if( !rate )
  1717. return;
  1718. for( i = 0; i < max && spell[i].id; i++ )
  1719. {
  1720. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1721. {
  1722. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1723. return;
  1724. rate += spell[i].rate;
  1725. break;
  1726. }
  1727. }
  1728. if (i == max) {
  1729. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
  1730. return;
  1731. }
  1732. spell[i].id = id;
  1733. spell[i].lv = lv;
  1734. spell[i].rate = rate;
  1735. //Auto-update flag value.
  1736. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1737. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1738. if (!(flag&BF_SKILLMASK)) {
  1739. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1740. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1741. }
  1742. spell[i].flag|= flag;
  1743. spell[i].card_id = card_id;
  1744. }
  1745. static void pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1746. {
  1747. uint8 i;
  1748. if( !rate )
  1749. return;
  1750. for( i = 0; i < max && spell[i].id; i++ )
  1751. {
  1752. ; // each autospell works independently
  1753. }
  1754. if( i == max )
  1755. {
  1756. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
  1757. return;
  1758. }
  1759. spell[i].flag = src_skill;
  1760. spell[i].id = id;
  1761. spell[i].lv = lv;
  1762. spell[i].rate = rate;
  1763. spell[i].card_id = card_id;
  1764. return;
  1765. }
  1766. /**
  1767. * Add inflict effect bonus for player while attacking/atatcked
  1768. * @param effect Effect array
  1769. * @param pmax Max array
  1770. * @param sc SC/Effect type
  1771. * @param rate Success chance
  1772. * @param arrow_rate success chance if bonus comes from arrow-type item
  1773. * @param flag Target flag
  1774. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1775. **/
  1776. static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  1777. {
  1778. uint16 i;
  1779. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1780. flag |= ATF_SHORT|ATF_LONG; //Default range: both
  1781. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1782. flag |= ATF_TARGET; //Default target: enemy.
  1783. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1784. flag |= ATF_WEAPON; //Default type: weapon.
  1785. if (!duration)
  1786. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1787. for (i = 0; i < pmax && effect[i].flag; i++) {
  1788. if (effect[i].sc == sc && effect[i].flag == flag) {
  1789. effect[i].rate += rate;
  1790. effect[i].arrow_rate += arrow_rate;
  1791. effect[i].duration = umax(effect[i].duration, duration);
  1792. return;
  1793. }
  1794. }
  1795. if (i == pmax) {
  1796. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
  1797. return;
  1798. }
  1799. effect[i].sc = sc;
  1800. effect[i].rate = rate;
  1801. effect[i].arrow_rate = arrow_rate;
  1802. effect[i].flag = flag;
  1803. effect[i].duration = duration;
  1804. }
  1805. /**
  1806. * Add inflict effect bonus for player while attacking using skill
  1807. * @param effect Effect array
  1808. * @param pmax Max array
  1809. * @param sc SC/Effect type
  1810. * @param rate Success chance
  1811. * @param flag Target flag
  1812. * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
  1813. **/
  1814. static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  1815. {
  1816. uint8 i;
  1817. if (!duration)
  1818. duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
  1819. for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
  1820. if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
  1821. effect[i].rate += rate;
  1822. effect[i].duration = umax(effect[i].duration, duration);
  1823. return;
  1824. }
  1825. }
  1826. if( i == pmax ) {
  1827. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
  1828. return;
  1829. }
  1830. effect[i].sc = sc;
  1831. effect[i].rate = rate;
  1832. effect[i].skill_id = skill_id;
  1833. effect[i].target = target;
  1834. effect[i].duration = duration;
  1835. }
  1836. /** Adjust/add drop rate modifier for player
  1837. * @param drop: Player's sd->add_drop (struct s_add_drop)
  1838. * @param max: Max bonus can be received
  1839. * @param nameid: item id that will be dropped
  1840. * @param group: group id
  1841. * @param class_: target class
  1842. * @param race: target race. if < 0, means monster_id
  1843. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  1844. */
  1845. static void pc_bonus_item_drop(struct s_add_drop *drop, const short max, unsigned short nameid, uint16 group, int class_, short race, int rate)
  1846. {
  1847. uint8 i;
  1848. if (!nameid && !group) {
  1849. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  1850. return;
  1851. }
  1852. if (nameid && !itemdb_exists(nameid)) {
  1853. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  1854. return;
  1855. }
  1856. if (group && !itemdb_group_exists(group)) {
  1857. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  1858. return;
  1859. }
  1860. //Apply config rate adjustment settings.
  1861. if (rate >= 0) { //Absolute drop.
  1862. if (battle_config.item_rate_adddrop != 100)
  1863. rate = rate*battle_config.item_rate_adddrop/100;
  1864. if (rate < battle_config.item_drop_adddrop_min)
  1865. rate = battle_config.item_drop_adddrop_min;
  1866. else if (rate > battle_config.item_drop_adddrop_max)
  1867. rate = battle_config.item_drop_adddrop_max;
  1868. } else { //Relative drop, max/min limits are applied at drop time.
  1869. if (battle_config.item_rate_adddrop != 100)
  1870. rate = rate*battle_config.item_rate_adddrop/100;
  1871. if (rate > -1)
  1872. rate = -1;
  1873. }
  1874. //Find match entry, and adjust the rate only
  1875. for (i = 0; i < max; i++) {
  1876. if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
  1877. continue;
  1878. if (drop[i].nameid == nameid &&
  1879. drop[i].group == group &&
  1880. drop[i].race == race &&
  1881. drop[i].class_ == class_
  1882. )
  1883. {
  1884. //Adjust the rate if it has same classification
  1885. if ((rate < 0 && drop[i].rate < 0) ||
  1886. (rate > 0 && drop[i].rate > 0))
  1887. {
  1888. drop[i].rate += rate;
  1889. return;
  1890. }
  1891. }
  1892. }
  1893. ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
  1894. if (i >= max) {
  1895. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid:%hu group:%d class_:%d race:%d rate:%d)\n",max,nameid,group,class_,race,rate);
  1896. return;
  1897. }
  1898. drop[i].nameid = nameid;
  1899. drop[i].group = group;
  1900. drop[i].race = race;
  1901. drop[i].class_ = class_;
  1902. drop[i].rate = rate;
  1903. }
  1904. bool pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned int pos,bool onskill)
  1905. {
  1906. int i;
  1907. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1908. if( i == max )
  1909. {
  1910. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1911. return false;
  1912. }
  1913. if( !onskill )
  1914. {
  1915. if( !(flag&BF_RANGEMASK) )
  1916. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1917. if( !(flag&BF_WEAPONMASK) )
  1918. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1919. if( !(flag&BF_SKILLMASK) )
  1920. {
  1921. if( flag&(BF_MAGIC|BF_MISC) )
  1922. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1923. if( flag&BF_WEAPON )
  1924. flag|=BF_NORMAL|BF_SKILL;
  1925. }
  1926. }
  1927. bonus[i].rate = rate;
  1928. bonus[i].duration = dur;
  1929. bonus[i].active = INVALID_TIMER;
  1930. bonus[i].atk_type = flag;
  1931. bonus[i].pos = pos;
  1932. bonus[i].bonus_script = aStrdup(script);
  1933. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1934. return true;
  1935. }
  1936. void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1937. {
  1938. int i;
  1939. if (!sd)
  1940. return;
  1941. for( i = 0; i < max; i++ )
  1942. {
  1943. if( autobonus[i].active != INVALID_TIMER )
  1944. {
  1945. if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
  1946. {
  1947. if( autobonus[i].bonus_script )
  1948. {
  1949. int j;
  1950. unsigned int equip_pos_idx = 0;
  1951. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1952. for(j = 0; j < EQI_MAX; j++) {
  1953. if(sd->equip_index[j] >= 0)
  1954. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1955. }
  1956. if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
  1957. script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
  1958. }
  1959. continue;
  1960. }
  1961. else
  1962. { // Logout / Unequipped an item with an activated bonus
  1963. delete_timer(autobonus[i].active,pc_endautobonus);
  1964. autobonus[i].active = INVALID_TIMER;
  1965. }
  1966. }
  1967. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1968. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1969. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1970. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1971. autobonus[i].active = INVALID_TIMER;
  1972. }
  1973. }
  1974. void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1975. {
  1976. if (!sd || !autobonus)
  1977. return;
  1978. if( autobonus->other_script )
  1979. {
  1980. int j;
  1981. unsigned int equip_pos_idx = 0;
  1982. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  1983. for(j = 0; j < EQI_MAX; j++) {
  1984. if(sd->equip_index[j] >= 0)
  1985. equip_pos_idx |= sd->status.inventory[sd->equip_index[j]].equip;
  1986. }
  1987. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  1988. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  1989. }
  1990. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1991. sd->state.autobonus |= autobonus->pos;
  1992. status_calc_pc(sd,SCO_FORCE);
  1993. }
  1994. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1995. {
  1996. struct map_session_data *sd = map_id2sd(id);
  1997. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1998. nullpo_ret(sd);
  1999. nullpo_ret(autobonus);
  2000. autobonus->active = INVALID_TIMER;
  2001. sd->state.autobonus &= ~autobonus->pos;
  2002. status_calc_pc(sd,SCO_FORCE);
  2003. return 0;
  2004. }
  2005. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2006. {
  2007. uint8 i;
  2008. struct weapon_data* wd;
  2009. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2010. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  2011. if (i == MAX_PC_BONUS)
  2012. {
  2013. ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2014. return;
  2015. }
  2016. if (!(flag&BF_RANGEMASK))
  2017. flag |= BF_SHORT|BF_LONG;
  2018. if (!(flag&BF_WEAPONMASK))
  2019. flag |= BF_WEAPON;
  2020. if (!(flag&BF_SKILLMASK))
  2021. {
  2022. if (flag&(BF_MAGIC|BF_MISC))
  2023. flag |= BF_SKILL;
  2024. if (flag&BF_WEAPON)
  2025. flag |= BF_NORMAL|BF_SKILL;
  2026. }
  2027. wd->addele2[i].ele = ele;
  2028. wd->addele2[i].rate = rate;
  2029. wd->addele2[i].flag = flag;
  2030. }
  2031. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2032. {
  2033. uint8 i;
  2034. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  2035. if (i == MAX_PC_BONUS)
  2036. {
  2037. ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  2038. return;
  2039. }
  2040. if (!(flag&BF_RANGEMASK))
  2041. flag |= BF_SHORT|BF_LONG;
  2042. if (!(flag&BF_WEAPONMASK))
  2043. flag |= BF_WEAPON;
  2044. if (!(flag&BF_SKILLMASK))
  2045. {
  2046. if (flag&(BF_MAGIC|BF_MISC))
  2047. flag |= BF_SKILL;
  2048. if (flag&BF_WEAPON)
  2049. flag |= BF_NORMAL|BF_SKILL;
  2050. }
  2051. sd->subele2[i].ele = ele;
  2052. sd->subele2[i].rate = rate;
  2053. sd->subele2[i].flag = flag;
  2054. }
  2055. /** Add item group heal rate bonus to player
  2056. * @param sd Player
  2057. * @param group_id Item Group ID
  2058. * @param rate
  2059. * @author Cydh
  2060. */
  2061. void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
  2062. struct s_pc_itemgrouphealrate *entry;
  2063. uint8 i;
  2064. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  2065. if (sd->itemgrouphealrate[i]->group_id == group_id)
  2066. break;
  2067. }
  2068. if (i != sd->itemgrouphealrate_count) {
  2069. sd->itemgrouphealrate[i]->rate += rate;
  2070. return;
  2071. }
  2072. if (i >= UINT8_MAX) {
  2073. ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
  2074. return;
  2075. }
  2076. entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
  2077. entry->group_id = group_id;
  2078. entry->rate = rate;
  2079. RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
  2080. sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
  2081. }
  2082. /** Clear item group heal rate from player
  2083. * @param sd Player
  2084. * @author Cydh
  2085. */
  2086. void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
  2087. if (!sd || !sd->itemgrouphealrate_count)
  2088. return;
  2089. else {
  2090. uint8 i;
  2091. for( i = 0; i < sd->itemgrouphealrate_count; i++ )
  2092. ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
  2093. sd->itemgrouphealrate_count = 0;
  2094. aFree(sd->itemgrouphealrate);
  2095. sd->itemgrouphealrate = NULL;
  2096. }
  2097. }
  2098. /*==========================================
  2099. * Add a bonus(type) to player sd
  2100. * format: bonus bBonusName,val;
  2101. * @param sd
  2102. * @param type Bonus type used by bBonusName
  2103. * @param val Value that usually for rate or fixed value
  2104. *------------------------------------------*/
  2105. void pc_bonus(struct map_session_data *sd,int type,int val)
  2106. {
  2107. struct status_data *status;
  2108. int bonus;
  2109. nullpo_retv(sd);
  2110. status = &sd->base_status;
  2111. switch(type){
  2112. case SP_STR:
  2113. case SP_AGI:
  2114. case SP_VIT:
  2115. case SP_INT:
  2116. case SP_DEX:
  2117. case SP_LUK:
  2118. if(sd->state.lr_flag != 2)
  2119. sd->param_bonus[type-SP_STR]+=val;
  2120. break;
  2121. case SP_ATK1:
  2122. if(!sd->state.lr_flag) {
  2123. bonus = status->rhw.atk + val;
  2124. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2125. }
  2126. else if(sd->state.lr_flag == 1) {
  2127. bonus = status->lhw.atk + val;
  2128. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2129. }
  2130. break;
  2131. case SP_ATK2:
  2132. if(!sd->state.lr_flag) {
  2133. bonus = status->rhw.atk2 + val;
  2134. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2135. }
  2136. else if(sd->state.lr_flag == 1) {
  2137. bonus = status->lhw.atk2 + val;
  2138. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2139. }
  2140. break;
  2141. case SP_BASE_ATK:
  2142. if(sd->state.lr_flag != 2) {
  2143. #ifdef RENEWAL
  2144. sd->bonus.eatk += val;
  2145. #else
  2146. bonus = status->batk + val;
  2147. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2148. #endif
  2149. }
  2150. break;
  2151. case SP_DEF1:
  2152. if(sd->state.lr_flag != 2) {
  2153. bonus = status->def + val;
  2154. #ifdef RENEWAL
  2155. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2156. #else
  2157. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2158. #endif
  2159. }
  2160. break;
  2161. case SP_DEF2:
  2162. if(sd->state.lr_flag != 2) {
  2163. bonus = status->def2 + val;
  2164. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2165. }
  2166. break;
  2167. case SP_MDEF1:
  2168. if(sd->state.lr_flag != 2) {
  2169. bonus = status->mdef + val;
  2170. #ifdef RENEWAL
  2171. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2172. #else
  2173. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2174. #endif
  2175. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2176. sd->bonus.shieldmdef += bonus;
  2177. }
  2178. }
  2179. break;
  2180. case SP_MDEF2:
  2181. if(sd->state.lr_flag != 2) {
  2182. bonus = status->mdef2 + val;
  2183. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2184. }
  2185. break;
  2186. case SP_HIT:
  2187. if(sd->state.lr_flag != 2) {
  2188. bonus = status->hit + val;
  2189. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2190. } else
  2191. sd->bonus.arrow_hit+=val;
  2192. break;
  2193. case SP_FLEE1:
  2194. if(sd->state.lr_flag != 2) {
  2195. bonus = status->flee + val;
  2196. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2197. }
  2198. break;
  2199. case SP_FLEE2:
  2200. if(sd->state.lr_flag != 2) {
  2201. bonus = status->flee2 + val*10;
  2202. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2203. }
  2204. break;
  2205. case SP_CRITICAL:
  2206. if(sd->state.lr_flag != 2) {
  2207. bonus = status->cri + val*10;
  2208. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2209. } else
  2210. sd->bonus.arrow_cri += val*10;
  2211. break;
  2212. case SP_ATKELE:
  2213. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2214. switch (sd->state.lr_flag)
  2215. {
  2216. case 2:
  2217. switch (sd->status.weapon) {
  2218. case W_BOW:
  2219. case W_REVOLVER:
  2220. case W_RIFLE:
  2221. case W_GATLING:
  2222. case W_SHOTGUN:
  2223. case W_GRENADE:
  2224. //Become weapon element.
  2225. status->rhw.ele=val;
  2226. break;
  2227. default: //Become arrow element.
  2228. sd->bonus.arrow_ele=val;
  2229. break;
  2230. }
  2231. break;
  2232. case 1:
  2233. status->lhw.ele=val;
  2234. break;
  2235. default:
  2236. status->rhw.ele=val;
  2237. break;
  2238. }
  2239. break;
  2240. case SP_DEFELE:
  2241. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2242. if(sd->state.lr_flag != 2)
  2243. status->def_ele=val;
  2244. break;
  2245. case SP_MAXHP:
  2246. if(sd->state.lr_flag == 2)
  2247. break;
  2248. sd->bonus.hp += val;
  2249. break;
  2250. case SP_MAXSP:
  2251. if(sd->state.lr_flag == 2)
  2252. break;
  2253. sd->bonus.sp += val;
  2254. break;
  2255. case SP_MAXHPRATE:
  2256. if(sd->state.lr_flag != 2)
  2257. sd->hprate+=val;
  2258. break;
  2259. case SP_MAXSPRATE:
  2260. if(sd->state.lr_flag != 2)
  2261. sd->sprate+=val;
  2262. break;
  2263. case SP_SPRATE:
  2264. if(sd->state.lr_flag != 2)
  2265. sd->dsprate+=val;
  2266. break;
  2267. case SP_ATTACKRANGE:
  2268. switch (sd->state.lr_flag) {
  2269. case 2:
  2270. switch (sd->status.weapon) {
  2271. case W_BOW:
  2272. case W_REVOLVER:
  2273. case W_RIFLE:
  2274. case W_GATLING:
  2275. case W_SHOTGUN:
  2276. case W_GRENADE:
  2277. status->rhw.range += val;
  2278. }
  2279. break;
  2280. case 1:
  2281. status->lhw.range += val;
  2282. break;
  2283. default:
  2284. status->rhw.range += val;
  2285. break;
  2286. }
  2287. break;
  2288. case SP_SPEED_RATE: //Non stackable increase
  2289. if(sd->state.lr_flag != 2)
  2290. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2291. break;
  2292. case SP_SPEED_ADDRATE: //Stackable increase
  2293. if(sd->state.lr_flag != 2)
  2294. sd->bonus.speed_add_rate -= val;
  2295. break;
  2296. case SP_ASPD: //Raw increase
  2297. if(sd->state.lr_flag != 2)
  2298. sd->bonus.aspd_add -= 10*val;
  2299. break;
  2300. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2301. if(sd->state.lr_flag != 2)
  2302. #ifndef RENEWAL_ASPD
  2303. status->aspd_rate -= 10*val;
  2304. #else
  2305. status->aspd_rate2 += val;
  2306. #endif
  2307. break;
  2308. case SP_HP_RECOV_RATE:
  2309. if(sd->state.lr_flag != 2)
  2310. sd->hprecov_rate += val;
  2311. break;
  2312. case SP_SP_RECOV_RATE:
  2313. if(sd->state.lr_flag != 2)
  2314. sd->sprecov_rate += val;
  2315. break;
  2316. case SP_CRITICAL_DEF:
  2317. if(sd->state.lr_flag != 2)
  2318. sd->bonus.critical_def += val;
  2319. break;
  2320. case SP_NEAR_ATK_DEF:
  2321. if(sd->state.lr_flag != 2)
  2322. sd->bonus.near_attack_def_rate += val;
  2323. break;
  2324. case SP_LONG_ATK_DEF:
  2325. if(sd->state.lr_flag != 2)
  2326. sd->bonus.long_attack_def_rate += val;
  2327. break;
  2328. case SP_DOUBLE_RATE:
  2329. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2330. sd->bonus.double_rate = val;
  2331. break;
  2332. case SP_DOUBLE_ADD_RATE:
  2333. if(sd->state.lr_flag == 0)
  2334. sd->bonus.double_add_rate += val;
  2335. break;
  2336. case SP_MATK_RATE:
  2337. if(sd->state.lr_flag != 2)
  2338. sd->matk_rate += val;
  2339. break;
  2340. case SP_IGNORE_DEF_ELE:
  2341. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2342. if(!sd->state.lr_flag)
  2343. sd->right_weapon.ignore_def_ele |= 1<<val;
  2344. else if(sd->state.lr_flag == 1)
  2345. sd->left_weapon.ignore_def_ele |= 1<<val;
  2346. break;
  2347. case SP_IGNORE_DEF_RACE:
  2348. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2349. if(!sd->state.lr_flag)
  2350. sd->right_weapon.ignore_def_race |= 1<<val;
  2351. else if(sd->state.lr_flag == 1)
  2352. sd->left_weapon.ignore_def_race |= 1<<val;
  2353. break;
  2354. case SP_IGNORE_DEF_CLASS:
  2355. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2356. if(!sd->state.lr_flag)
  2357. sd->right_weapon.ignore_def_class |= 1<<val;
  2358. else if(sd->state.lr_flag == 1)
  2359. sd->left_weapon.ignore_def_class |= 1<<val;
  2360. break;
  2361. case SP_ATK_RATE:
  2362. if(sd->state.lr_flag != 2)
  2363. sd->bonus.atk_rate += val;
  2364. break;
  2365. case SP_MAGIC_ATK_DEF:
  2366. if(sd->state.lr_flag != 2)
  2367. sd->bonus.magic_def_rate += val;
  2368. break;
  2369. case SP_MISC_ATK_DEF:
  2370. if(sd->state.lr_flag != 2)
  2371. sd->bonus.misc_def_rate += val;
  2372. break;
  2373. case SP_IGNORE_MDEF_ELE:
  2374. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2375. if(sd->state.lr_flag != 2)
  2376. sd->bonus.ignore_mdef_ele |= 1<<val;
  2377. break;
  2378. case SP_IGNORE_MDEF_RACE:
  2379. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2380. if(sd->state.lr_flag != 2)
  2381. sd->bonus.ignore_mdef_race |= 1<<val;
  2382. break;
  2383. case SP_PERFECT_HIT_RATE:
  2384. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2385. sd->bonus.perfect_hit = val;
  2386. break;
  2387. case SP_PERFECT_HIT_ADD_RATE:
  2388. if(sd->state.lr_flag != 2)
  2389. sd->bonus.perfect_hit_add += val;
  2390. break;
  2391. case SP_CRITICAL_RATE:
  2392. if(sd->state.lr_flag != 2)
  2393. sd->critical_rate+=val;
  2394. break;
  2395. case SP_DEF_RATIO_ATK_ELE:
  2396. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2397. if(!sd->state.lr_flag)
  2398. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2399. else if(sd->state.lr_flag == 1)
  2400. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2401. break;
  2402. case SP_DEF_RATIO_ATK_RACE:
  2403. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2404. if(!sd->state.lr_flag)
  2405. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2406. else if(sd->state.lr_flag == 1)
  2407. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2408. break;
  2409. case SP_DEF_RATIO_ATK_CLASS:
  2410. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2411. if(!sd->state.lr_flag)
  2412. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2413. else if(sd->state.lr_flag == 1)
  2414. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2415. break;
  2416. case SP_HIT_RATE:
  2417. if(sd->state.lr_flag != 2)
  2418. sd->hit_rate += val;
  2419. break;
  2420. case SP_FLEE_RATE:
  2421. if(sd->state.lr_flag != 2)
  2422. sd->flee_rate += val;
  2423. break;
  2424. case SP_FLEE2_RATE:
  2425. if(sd->state.lr_flag != 2)
  2426. sd->flee2_rate += val;
  2427. break;
  2428. case SP_DEF_RATE:
  2429. if(sd->state.lr_flag != 2)
  2430. sd->def_rate += val;
  2431. break;
  2432. case SP_DEF2_RATE:
  2433. if(sd->state.lr_flag != 2)
  2434. sd->def2_rate += val;
  2435. break;
  2436. case SP_MDEF_RATE:
  2437. if(sd->state.lr_flag != 2)
  2438. sd->mdef_rate += val;
  2439. break;
  2440. case SP_MDEF2_RATE:
  2441. if(sd->state.lr_flag != 2)
  2442. sd->mdef2_rate += val;
  2443. break;
  2444. case SP_RESTART_FULL_RECOVER:
  2445. if(sd->state.lr_flag != 2)
  2446. sd->special_state.restart_full_recover = 1;
  2447. break;
  2448. case SP_NO_CASTCANCEL:
  2449. if(sd->state.lr_flag != 2)
  2450. sd->special_state.no_castcancel = 1;
  2451. break;
  2452. case SP_NO_CASTCANCEL2:
  2453. if(sd->state.lr_flag != 2)
  2454. sd->special_state.no_castcancel2 = 1;
  2455. break;
  2456. case SP_NO_SIZEFIX:
  2457. if(sd->state.lr_flag != 2)
  2458. sd->special_state.no_sizefix = 1;
  2459. break;
  2460. case SP_NO_MAGIC_DAMAGE:
  2461. if(sd->state.lr_flag == 2)
  2462. break;
  2463. val+= sd->special_state.no_magic_damage;
  2464. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2465. break;
  2466. case SP_NO_WEAPON_DAMAGE:
  2467. if(sd->state.lr_flag == 2)
  2468. break;
  2469. val+= sd->special_state.no_weapon_damage;
  2470. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2471. break;
  2472. case SP_NO_MISC_DAMAGE:
  2473. if(sd->state.lr_flag == 2)
  2474. break;
  2475. val+= sd->special_state.no_misc_damage;
  2476. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2477. break;
  2478. case SP_NO_GEMSTONE:
  2479. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2480. sd->special_state.no_gemstone = 1;
  2481. break;
  2482. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2483. if(sd->state.lr_flag != 2) {
  2484. sd->special_state.intravision = 1;
  2485. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2486. }
  2487. break;
  2488. case SP_NO_KNOCKBACK:
  2489. if(sd->state.lr_flag != 2)
  2490. sd->special_state.no_knockback = 1;
  2491. break;
  2492. case SP_SPLASH_RANGE:
  2493. if(sd->bonus.splash_range < val)
  2494. sd->bonus.splash_range = val;
  2495. break;
  2496. case SP_SPLASH_ADD_RANGE:
  2497. sd->bonus.splash_add_range += val;
  2498. break;
  2499. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2500. if(sd->state.lr_flag != 2)
  2501. sd->bonus.short_weapon_damage_return += val;
  2502. break;
  2503. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->bonus.long_weapon_damage_return += val;
  2506. break;
  2507. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2508. if(sd->state.lr_flag != 2)
  2509. sd->bonus.magic_damage_return += val;
  2510. break;
  2511. case SP_ALL_STATS: // [Valaris]
  2512. if(sd->state.lr_flag!=2) {
  2513. sd->param_bonus[SP_STR-SP_STR]+=val;
  2514. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2515. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2516. sd->param_bonus[SP_INT-SP_STR]+=val;
  2517. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2518. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2519. }
  2520. break;
  2521. case SP_AGI_VIT: // [Valaris]
  2522. if(sd->state.lr_flag!=2) {
  2523. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2524. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2525. }
  2526. break;
  2527. case SP_AGI_DEX_STR: // [Valaris]
  2528. if(sd->state.lr_flag!=2) {
  2529. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2530. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2531. sd->param_bonus[SP_STR-SP_STR]+=val;
  2532. }
  2533. break;
  2534. case SP_PERFECT_HIDE: // [Valaris]
  2535. if(sd->state.lr_flag!=2)
  2536. sd->special_state.perfect_hiding=1;
  2537. break;
  2538. case SP_UNBREAKABLE:
  2539. if(sd->state.lr_flag!=2)
  2540. sd->bonus.unbreakable += val;
  2541. break;
  2542. case SP_UNBREAKABLE_WEAPON:
  2543. if(sd->state.lr_flag != 2)
  2544. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2545. break;
  2546. case SP_UNBREAKABLE_ARMOR:
  2547. if(sd->state.lr_flag != 2)
  2548. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2549. break;
  2550. case SP_UNBREAKABLE_HELM:
  2551. if(sd->state.lr_flag != 2)
  2552. sd->bonus.unbreakable_equip |= EQP_HELM;
  2553. break;
  2554. case SP_UNBREAKABLE_SHIELD:
  2555. if(sd->state.lr_flag != 2)
  2556. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2557. break;
  2558. case SP_UNBREAKABLE_GARMENT:
  2559. if(sd->state.lr_flag != 2)
  2560. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2561. break;
  2562. case SP_UNBREAKABLE_SHOES:
  2563. if(sd->state.lr_flag != 2)
  2564. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2565. break;
  2566. case SP_CLASSCHANGE: // [Valaris]
  2567. if(sd->state.lr_flag !=2)
  2568. sd->bonus.classchange=val;
  2569. break;
  2570. case SP_LONG_ATK_RATE:
  2571. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2572. sd->bonus.long_attack_atk_rate+=val;
  2573. break;
  2574. case SP_BREAK_WEAPON_RATE:
  2575. if(sd->state.lr_flag != 2)
  2576. sd->bonus.break_weapon_rate+=val;
  2577. break;
  2578. case SP_BREAK_ARMOR_RATE:
  2579. if(sd->state.lr_flag != 2)
  2580. sd->bonus.break_armor_rate+=val;
  2581. break;
  2582. case SP_ADD_STEAL_RATE:
  2583. if(sd->state.lr_flag != 2)
  2584. sd->bonus.add_steal_rate+=val;
  2585. break;
  2586. case SP_DELAYRATE:
  2587. if(sd->state.lr_flag != 2)
  2588. sd->delayrate+=val;
  2589. break;
  2590. case SP_CRIT_ATK_RATE:
  2591. if(sd->state.lr_flag != 2)
  2592. sd->bonus.crit_atk_rate += val;
  2593. break;
  2594. case SP_NO_REGEN:
  2595. if(sd->state.lr_flag != 2)
  2596. sd->regen.state.block|=val;
  2597. break;
  2598. case SP_UNSTRIPABLE_WEAPON:
  2599. if(sd->state.lr_flag != 2)
  2600. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2601. break;
  2602. case SP_UNSTRIPABLE:
  2603. case SP_UNSTRIPABLE_ARMOR:
  2604. if(sd->state.lr_flag != 2)
  2605. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2606. break;
  2607. case SP_UNSTRIPABLE_HELM:
  2608. if(sd->state.lr_flag != 2)
  2609. sd->bonus.unstripable_equip |= EQP_HELM;
  2610. break;
  2611. case SP_UNSTRIPABLE_SHIELD:
  2612. if(sd->state.lr_flag != 2)
  2613. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2614. break;
  2615. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2616. if(!sd->state.lr_flag) {
  2617. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2618. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2619. } else if(sd->state.lr_flag == 1) {
  2620. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2621. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2622. }
  2623. break;
  2624. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2625. if(!sd->state.lr_flag) {
  2626. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2627. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2628. } else if(sd->state.lr_flag == 1) {
  2629. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2630. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2631. }
  2632. break;
  2633. case SP_SP_GAIN_VALUE:
  2634. if(!sd->state.lr_flag)
  2635. sd->bonus.sp_gain_value += val;
  2636. break;
  2637. case SP_HP_GAIN_VALUE:
  2638. if(!sd->state.lr_flag)
  2639. sd->bonus.hp_gain_value += val;
  2640. break;
  2641. case SP_MAGIC_SP_GAIN_VALUE:
  2642. if(!sd->state.lr_flag)
  2643. sd->bonus.magic_sp_gain_value += val;
  2644. break;
  2645. case SP_MAGIC_HP_GAIN_VALUE:
  2646. if(!sd->state.lr_flag)
  2647. sd->bonus.magic_hp_gain_value += val;
  2648. break;
  2649. case SP_ADD_HEAL_RATE:
  2650. if(sd->state.lr_flag != 2)
  2651. sd->bonus.add_heal_rate += val;
  2652. break;
  2653. case SP_ADD_HEAL2_RATE:
  2654. if(sd->state.lr_flag != 2)
  2655. sd->bonus.add_heal2_rate += val;
  2656. break;
  2657. case SP_ADD_ITEM_HEAL_RATE:
  2658. if(sd->state.lr_flag != 2)
  2659. sd->bonus.itemhealrate2 += val;
  2660. break;
  2661. case SP_EMATK:
  2662. if(sd->state.lr_flag != 2)
  2663. sd->bonus.ematk += val;
  2664. break;
  2665. #ifdef RENEWAL_CAST
  2666. case SP_FIXCASTRATE:
  2667. if(sd->state.lr_flag != 2)
  2668. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2669. break;
  2670. case SP_ADD_FIXEDCAST:
  2671. if(sd->state.lr_flag != 2)
  2672. sd->bonus.add_fixcast += val;
  2673. break;
  2674. case SP_CASTRATE:
  2675. case SP_VARCASTRATE:
  2676. if(sd->state.lr_flag != 2)
  2677. sd->bonus.varcastrate -= val;
  2678. break;
  2679. case SP_ADD_VARIABLECAST:
  2680. if(sd->state.lr_flag != 2)
  2681. sd->bonus.add_varcast += val;
  2682. break;
  2683. #else
  2684. case SP_ADD_FIXEDCAST:
  2685. case SP_FIXCASTRATE:
  2686. case SP_ADD_VARIABLECAST:
  2687. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2688. break;
  2689. case SP_VARCASTRATE:
  2690. case SP_CASTRATE:
  2691. if(sd->state.lr_flag != 2)
  2692. sd->castrate += val;
  2693. break;
  2694. #endif
  2695. case SP_ADDMAXWEIGHT:
  2696. if (sd->state.lr_flag != 2)
  2697. sd->max_weight += val;
  2698. break;
  2699. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  2700. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  2701. break;
  2702. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  2703. if (sd->state.lr_flag != 2)
  2704. sd->bonus.critical_rangeatk += val*10;
  2705. else
  2706. sd->bonus.arrow_cri += val*10;
  2707. break;
  2708. case SP_WEAPON_ATK_RATE:
  2709. if (sd->state.lr_flag != 2)
  2710. sd->bonus.weapon_atk_rate += val;
  2711. break;
  2712. case SP_WEAPON_MATK_RATE:
  2713. if (sd->state.lr_flag != 2)
  2714. sd->bonus.weapon_matk_rate += val;
  2715. break;
  2716. case SP_NO_MADO_FUEL:
  2717. if (sd->state.lr_flag != 2)
  2718. sd->special_state.no_mado_fuel = 1;
  2719. break;
  2720. default:
  2721. if (running_npc_stat_calc_event) {
  2722. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  2723. }
  2724. else if (current_equip_combo_pos > 0) {
  2725. 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);
  2726. }
  2727. else {
  2728. 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);
  2729. }
  2730. break;
  2731. }
  2732. }
  2733. /*==========================================
  2734. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2735. * format: bonus2 bBonusName,type2,val;
  2736. * @param sd
  2737. * @param type Bonus type used by bBonusName
  2738. * @param type2
  2739. * @param val Value that usually for rate or fixed value
  2740. *------------------------------------------*/
  2741. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2742. {
  2743. int i;
  2744. nullpo_retv(sd);
  2745. switch(type){
  2746. case SP_ADDELE: // bonus2 bAddEle,e,x;
  2747. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  2748. if(!sd->state.lr_flag)
  2749. sd->right_weapon.addele[type2]+=val;
  2750. else if(sd->state.lr_flag == 1)
  2751. sd->left_weapon.addele[type2]+=val;
  2752. else if(sd->state.lr_flag == 2)
  2753. sd->arrow_addele[type2]+=val;
  2754. break;
  2755. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  2756. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  2757. if(!sd->state.lr_flag)
  2758. sd->right_weapon.addrace[type2]+=val;
  2759. else if(sd->state.lr_flag == 1)
  2760. sd->left_weapon.addrace[type2]+=val;
  2761. else if(sd->state.lr_flag == 2)
  2762. sd->arrow_addrace[type2]+=val;
  2763. break;
  2764. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  2765. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  2766. if(!sd->state.lr_flag)
  2767. sd->right_weapon.addclass[type2]+=val;
  2768. else if(sd->state.lr_flag == 1)
  2769. sd->left_weapon.addclass[type2]+=val;
  2770. else if(sd->state.lr_flag == 2)
  2771. sd->arrow_addclass[type2]+=val;
  2772. break;
  2773. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  2774. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  2775. if(!sd->state.lr_flag)
  2776. sd->right_weapon.addsize[type2]+=val;
  2777. else if(sd->state.lr_flag == 1)
  2778. sd->left_weapon.addsize[type2]+=val;
  2779. else if(sd->state.lr_flag == 2)
  2780. sd->arrow_addsize[type2]+=val;
  2781. break;
  2782. case SP_SUBELE: // bonus2 bSubEle,e,x;
  2783. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  2784. if(sd->state.lr_flag != 2)
  2785. sd->subele_script[type2] += val;
  2786. break;
  2787. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  2788. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  2789. if(sd->state.lr_flag != 2)
  2790. sd->subrace[type2]+=val;
  2791. break;
  2792. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  2793. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  2794. if(sd->state.lr_flag != 2)
  2795. sd->subclass[type2]+=val;
  2796. break;
  2797. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  2798. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  2799. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2800. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  2801. break;
  2802. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  2803. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  2804. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2805. sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  2806. break;
  2807. case SP_RESEFF: // bonus2 bResEff,eff,n;
  2808. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2809. ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
  2810. break;
  2811. }
  2812. if(sd->state.lr_flag == 2)
  2813. break;
  2814. i = sd->reseff[type2]+val;
  2815. sd->reseff[type2]= cap_value(i, -10000, 10000);
  2816. break;
  2817. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  2818. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  2819. if(sd->state.lr_flag != 2)
  2820. sd->magic_addele_script[type2] += val;
  2821. break;
  2822. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  2823. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  2824. if(sd->state.lr_flag != 2)
  2825. sd->magic_addrace[type2]+=val;
  2826. break;
  2827. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  2828. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  2829. if(sd->state.lr_flag != 2)
  2830. sd->magic_addclass[type2]+=val;
  2831. break;
  2832. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  2833. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  2834. if(sd->state.lr_flag != 2)
  2835. sd->magic_addsize[type2]+=val;
  2836. break;
  2837. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  2838. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  2839. if(sd->state.lr_flag != 2)
  2840. sd->magic_atk_ele[type2]+=val;
  2841. break;
  2842. case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
  2843. switch (sd->state.lr_flag) {
  2844. case 0: //Right hand
  2845. ARR_FIND(0, ARRAYLENGTH(sd->right_weapon.add_dmg), i, sd->right_weapon.add_dmg[i].rate == 0 || sd->right_weapon.add_dmg[i].class_ == type2);
  2846. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2847. {
  2848. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2849. break;
  2850. }
  2851. sd->right_weapon.add_dmg[i].class_ = type2;
  2852. sd->right_weapon.add_dmg[i].rate += val;
  2853. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2854. memmove(&sd->right_weapon.add_dmg[i], &sd->right_weapon.add_dmg[i+1], sizeof(sd->right_weapon.add_dmg) - (i+1)*sizeof(sd->right_weapon.add_dmg[0]));
  2855. break;
  2856. case 1: //Left hand
  2857. ARR_FIND(0, ARRAYLENGTH(sd->left_weapon.add_dmg), i, sd->left_weapon.add_dmg[i].rate == 0 || sd->left_weapon.add_dmg[i].class_ == type2);
  2858. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2859. {
  2860. ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2861. break;
  2862. }
  2863. sd->left_weapon.add_dmg[i].class_ = type2;
  2864. sd->left_weapon.add_dmg[i].rate += val;
  2865. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2866. memmove(&sd->left_weapon.add_dmg[i], &sd->left_weapon.add_dmg[i+1], sizeof(sd->left_weapon.add_dmg) - (i+1)*sizeof(sd->left_weapon.add_dmg[0]));
  2867. break;
  2868. }
  2869. break;
  2870. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  2871. if(sd->state.lr_flag == 2)
  2872. break;
  2873. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2874. if (i == ARRAYLENGTH(sd->add_mdmg))
  2875. {
  2876. ShowError("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2877. break;
  2878. }
  2879. sd->add_mdmg[i].class_ = type2;
  2880. sd->add_mdmg[i].rate += val;
  2881. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2882. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2883. break;
  2884. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  2885. if(sd->state.lr_flag == 2)
  2886. break;
  2887. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2888. if (i == ARRAYLENGTH(sd->add_def))
  2889. {
  2890. ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2891. break;
  2892. }
  2893. sd->add_def[i].class_ = type2;
  2894. sd->add_def[i].rate += val;
  2895. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2896. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2897. break;
  2898. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  2899. if(sd->state.lr_flag == 2)
  2900. break;
  2901. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2902. if (i == ARRAYLENGTH(sd->add_mdef))
  2903. {
  2904. ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2905. break;
  2906. }
  2907. sd->add_mdef[i].class_ = type2;
  2908. sd->add_mdef[i].rate += val;
  2909. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2910. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2911. break;
  2912. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  2913. if(!sd->state.lr_flag) {
  2914. sd->right_weapon.hp_drain_rate.rate += type2;
  2915. sd->right_weapon.hp_drain_rate.per += val;
  2916. }
  2917. else if(sd->state.lr_flag == 1) {
  2918. sd->left_weapon.hp_drain_rate.rate += type2;
  2919. sd->left_weapon.hp_drain_rate.per += val;
  2920. }
  2921. break;
  2922. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  2923. if(!sd->state.lr_flag) {
  2924. sd->right_weapon.sp_drain_rate.rate += type2;
  2925. sd->right_weapon.sp_drain_rate.per += val;
  2926. }
  2927. else if(sd->state.lr_flag == 1) {
  2928. sd->left_weapon.sp_drain_rate.rate += type2;
  2929. sd->left_weapon.sp_drain_rate.per += val;
  2930. }
  2931. break;
  2932. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  2933. if(sd->state.lr_flag != 2) {
  2934. sd->bonus.sp_vanish_rate += type2;
  2935. sd->bonus.sp_vanish_per += val;
  2936. }
  2937. break;
  2938. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  2939. if(sd->state.lr_flag != 2) {
  2940. sd->bonus.hp_vanish_rate += type2;
  2941. sd->bonus.hp_vanish_per += val;
  2942. }
  2943. break;
  2944. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  2945. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2946. sd->bonus.get_zeny_rate = val;
  2947. sd->bonus.get_zeny_num = type2;
  2948. }
  2949. break;
  2950. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  2951. if(sd->state.lr_flag != 2) {
  2952. sd->bonus.get_zeny_rate += val;
  2953. sd->bonus.get_zeny_num += type2;
  2954. }
  2955. break;
  2956. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  2957. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  2958. if(sd->state.lr_flag == 2)
  2959. break;
  2960. sd->weapon_coma_ele[type2] += val;
  2961. sd->special_state.bonus_coma = 1;
  2962. break;
  2963. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  2964. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  2965. if(sd->state.lr_flag == 2)
  2966. break;
  2967. sd->weapon_coma_race[type2] += val;
  2968. sd->special_state.bonus_coma = 1;
  2969. break;
  2970. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  2971. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  2972. if(sd->state.lr_flag == 2)
  2973. break;
  2974. sd->weapon_coma_class[type2] += val;
  2975. sd->special_state.bonus_coma = 1;
  2976. break;
  2977. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  2978. if(sd->state.lr_flag != 2)
  2979. sd->weapon_atk[type2]+=val;
  2980. break;
  2981. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  2982. if(sd->state.lr_flag != 2)
  2983. sd->weapon_damage_rate[type2]+=val;
  2984. break;
  2985. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  2986. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  2987. if(sd->state.lr_flag != 2)
  2988. sd->critaddrace[type2] += val*10;
  2989. break;
  2990. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  2991. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  2992. if(sd->state.lr_flag != 2)
  2993. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
  2994. break;
  2995. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  2996. if(sd->state.lr_flag == 2)
  2997. break;
  2998. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2999. if (i == ARRAYLENGTH(sd->skillatk))
  3000. { //Better mention this so the array length can be updated. [Skotlex]
  3001. ShowError("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  3002. break;
  3003. }
  3004. if (sd->skillatk[i].id == type2)
  3005. sd->skillatk[i].val += val;
  3006. else {
  3007. sd->skillatk[i].id = type2;
  3008. sd->skillatk[i].val = val;
  3009. }
  3010. break;
  3011. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3012. if(sd->state.lr_flag == 2)
  3013. break;
  3014. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  3015. if (i == ARRAYLENGTH(sd->skillheal))
  3016. { // Better mention this so the array length can be updated. [Skotlex]
  3017. ShowError("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  3018. break;
  3019. }
  3020. if (sd->skillheal[i].id == type2)
  3021. sd->skillheal[i].val += val;
  3022. else {
  3023. sd->skillheal[i].id = type2;
  3024. sd->skillheal[i].val = val;
  3025. }
  3026. break;
  3027. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3028. if(sd->state.lr_flag == 2)
  3029. break;
  3030. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  3031. if (i == ARRAYLENGTH(sd->skillheal2))
  3032. { // Better mention this so the array length can be updated. [Skotlex]
  3033. ShowError("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  3034. break;
  3035. }
  3036. if (sd->skillheal2[i].id == type2)
  3037. sd->skillheal2[i].val += val;
  3038. else {
  3039. sd->skillheal2[i].id = type2;
  3040. sd->skillheal2[i].val = val;
  3041. }
  3042. break;
  3043. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3044. if(sd->state.lr_flag == 2)
  3045. break;
  3046. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  3047. if (i == ARRAYLENGTH(sd->skillblown))
  3048. { //Better mention this so the array length can be updated. [Skotlex]
  3049. ShowError("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  3050. break;
  3051. }
  3052. if(sd->skillblown[i].id == type2)
  3053. sd->skillblown[i].val += val;
  3054. else {
  3055. sd->skillblown[i].id = type2;
  3056. sd->skillblown[i].val = val;
  3057. }
  3058. break;
  3059. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3060. if(sd->state.lr_flag != 2) {
  3061. sd->hp_loss.value = type2;
  3062. sd->hp_loss.rate = val;
  3063. }
  3064. break;
  3065. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3066. if(sd->state.lr_flag != 2) {
  3067. sd->hp_regen.value = type2;
  3068. sd->hp_regen.rate = val;
  3069. }
  3070. break;
  3071. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3072. if (sd->state.lr_flag != 2) {
  3073. sd->percent_hp_regen.value = type2;
  3074. sd->percent_hp_regen.rate = val;
  3075. }
  3076. break;
  3077. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3078. if (sd->state.lr_flag != 2) {
  3079. sd->percent_sp_regen.value = type2;
  3080. sd->percent_sp_regen.rate = val;
  3081. }
  3082. break;
  3083. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3084. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3085. if(sd->state.lr_flag != 2)
  3086. sd->right_weapon.addrace2[type2] += val;
  3087. else
  3088. sd->left_weapon.addrace2[type2] += val;
  3089. break;
  3090. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3091. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3092. if(sd->state.lr_flag != 2)
  3093. sd->subsize[type2]+=val;
  3094. break;
  3095. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3096. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3097. if(sd->state.lr_flag != 2)
  3098. sd->subrace2[type2]+=val;
  3099. break;
  3100. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3101. if(sd->state.lr_flag == 2)
  3102. break;
  3103. if (!itemdb_exists(type2)) {
  3104. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3105. break;
  3106. }
  3107. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  3108. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  3109. ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  3110. break;
  3111. }
  3112. sd->itemhealrate[i].nameid = type2;
  3113. sd->itemhealrate[i].rate += val;
  3114. break;
  3115. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3116. if (sd->state.lr_flag == 2)
  3117. break;
  3118. if (!type2 || !itemdb_group_exists(type2)) {
  3119. ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3120. break;
  3121. }
  3122. pc_itemgrouphealrate(sd, type2, val);
  3123. break;
  3124. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3125. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3126. if(sd->state.lr_flag != 2)
  3127. sd->expaddrace[type2]+=val;
  3128. break;
  3129. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3130. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3131. if(sd->state.lr_flag != 2)
  3132. sd->expaddclass[type2]+=val;
  3133. break;
  3134. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3135. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3136. if(sd->state.lr_flag != 2)
  3137. sd->sp_gain_race[type2]+=val;
  3138. break;
  3139. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3140. if (sd->state.lr_flag != 2)
  3141. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
  3142. break;
  3143. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3144. if (sd->state.lr_flag != 2)
  3145. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
  3146. break;
  3147. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3148. if(sd->state.lr_flag != 2) {
  3149. sd->sp_loss.value = type2;
  3150. sd->sp_loss.rate = val;
  3151. }
  3152. break;
  3153. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3154. if(sd->state.lr_flag != 2) {
  3155. sd->sp_regen.value = type2;
  3156. sd->sp_regen.rate = val;
  3157. }
  3158. break;
  3159. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3160. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3161. if(!sd->state.lr_flag) {
  3162. sd->right_weapon.hp_drain_race[type2] += val;
  3163. }
  3164. else if(sd->state.lr_flag == 1) {
  3165. sd->left_weapon.hp_drain_race[type2] += val;
  3166. }
  3167. break;
  3168. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3169. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3170. if(!sd->state.lr_flag) {
  3171. sd->right_weapon.sp_drain_race[type2] += val;
  3172. }
  3173. else if(sd->state.lr_flag == 1) {
  3174. sd->left_weapon.sp_drain_race[type2] += val;
  3175. }
  3176. break;
  3177. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3178. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3179. if(!sd->state.lr_flag) {
  3180. sd->right_weapon.hp_drain_class[type2] += val;
  3181. }
  3182. else if(sd->state.lr_flag == 1) {
  3183. sd->left_weapon.hp_drain_class[type2] += val;
  3184. }
  3185. break;
  3186. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3187. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3188. if(!sd->state.lr_flag) {
  3189. sd->right_weapon.sp_drain_class[type2] += val;
  3190. }
  3191. else if(sd->state.lr_flag == 1) {
  3192. sd->left_weapon.sp_drain_class[type2] += val;
  3193. }
  3194. break;
  3195. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3196. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3197. if(sd->state.lr_flag != 2)
  3198. sd->ignore_mdef_by_race[type2] += val;
  3199. break;
  3200. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3201. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3202. if(sd->state.lr_flag != 2)
  3203. sd->ignore_mdef_by_class[type2] += val;
  3204. break;
  3205. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3206. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3207. if(sd->state.lr_flag != 2)
  3208. sd->ignore_def_by_race[type2] += val;
  3209. break;
  3210. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3211. PC_BONUS_CHK_RACE(type2, SP_IGNORE_DEF_CLASS_RATE);
  3212. if (sd->state.lr_flag != 2)
  3213. sd->ignore_def_by_class[type2] += val;
  3214. break;
  3215. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3216. if(sd->state.lr_flag == 2)
  3217. break;
  3218. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  3219. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  3220. ShowError("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  3221. break;
  3222. }
  3223. if (sd->skillusesprate[i].id == type2)
  3224. sd->skillusesprate[i].val += val;
  3225. else {
  3226. sd->skillusesprate[i].id = type2;
  3227. sd->skillusesprate[i].val = val;
  3228. }
  3229. break;
  3230. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3231. if(sd->state.lr_flag == 2)
  3232. break;
  3233. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  3234. if (i == ARRAYLENGTH(sd->skillcooldown))
  3235. {
  3236. ShowError("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  3237. break;
  3238. }
  3239. if (sd->skillcooldown[i].id == type2)
  3240. sd->skillcooldown[i].val += val;
  3241. else {
  3242. sd->skillcooldown[i].id = type2;
  3243. sd->skillcooldown[i].val = val;
  3244. }
  3245. break;
  3246. #ifdef RENEWAL_CAST
  3247. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3248. if(sd->state.lr_flag == 2)
  3249. break;
  3250. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  3251. if (i == ARRAYLENGTH(sd->skillfixcast))
  3252. {
  3253. ShowError("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  3254. break;
  3255. }
  3256. if (sd->skillfixcast[i].id == type2)
  3257. sd->skillfixcast[i].val += val;
  3258. else {
  3259. sd->skillfixcast[i].id = type2;
  3260. sd->skillfixcast[i].val = val;
  3261. }
  3262. break;
  3263. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3264. if(sd->state.lr_flag == 2)
  3265. break;
  3266. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  3267. if (i == ARRAYLENGTH(sd->skillvarcast))
  3268. {
  3269. ShowError("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  3270. break;
  3271. }
  3272. if (sd->skillvarcast[i].id == type2)
  3273. sd->skillvarcast[i].val += val;
  3274. else {
  3275. sd->skillvarcast[i].id = type2;
  3276. sd->skillvarcast[i].val = val;
  3277. }
  3278. break;
  3279. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3280. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3281. if(sd->state.lr_flag == 2)
  3282. break;
  3283. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3284. if (i == ARRAYLENGTH(sd->skillcastrate))
  3285. {
  3286. ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
  3287. break;
  3288. }
  3289. if(sd->skillcastrate[i].id == type2)
  3290. sd->skillcastrate[i].val -= val;
  3291. else {
  3292. sd->skillcastrate[i].id = type2;
  3293. sd->skillcastrate[i].val -= val;
  3294. }
  3295. break;
  3296. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3297. if(sd->state.lr_flag == 2)
  3298. break;
  3299. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  3300. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  3301. {
  3302. ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  3303. break;
  3304. }
  3305. if(sd->skillfixcastrate[i].id == type2)
  3306. sd->skillfixcastrate[i].val -= val;
  3307. else {
  3308. sd->skillfixcastrate[i].id = type2;
  3309. sd->skillfixcastrate[i].val -= val;
  3310. }
  3311. break;
  3312. #else
  3313. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3314. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3315. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3316. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3317. break;
  3318. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3319. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3320. if(sd->state.lr_flag == 2)
  3321. break;
  3322. ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
  3323. if (i == ARRAYLENGTH(sd->skillcastrate))
  3324. { //Better mention this so the array length can be updated. [Skotlex]
  3325. ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
  3326. (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
  3327. break;
  3328. }
  3329. if(sd->skillcastrate[i].id == type2)
  3330. sd->skillcastrate[i].val += val;
  3331. else {
  3332. sd->skillcastrate[i].id = type2;
  3333. sd->skillcastrate[i].val = val;
  3334. }
  3335. break;
  3336. #endif
  3337. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3338. if(sd->state.lr_flag == 2)
  3339. break;
  3340. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  3341. if (i == ARRAYLENGTH(sd->skillusesp)) {
  3342. ShowError("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  3343. break;
  3344. }
  3345. if (sd->skillusesp[i].id == type2)
  3346. sd->skillusesp[i].val += val;
  3347. else {
  3348. sd->skillusesp[i].id = type2;
  3349. sd->skillusesp[i].val = val;
  3350. }
  3351. break;
  3352. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3353. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
  3354. if (i == ARRAYLENGTH(sd->subskill)) {
  3355. ShowError("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->subskill), type2, val);
  3356. break;
  3357. }
  3358. if (sd->subskill[i].id == type2)
  3359. sd->subskill[i].val += val;
  3360. else {
  3361. sd->subskill[i].id = type2;
  3362. sd->subskill[i].val = val;
  3363. }
  3364. break;
  3365. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3366. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3367. sd->subdefele[type2] += val;
  3368. break;
  3369. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3370. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3371. sd->coma_class[type2] += val;
  3372. sd->special_state.bonus_coma = 1;
  3373. break;
  3374. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3375. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3376. sd->coma_race[type2] += val;
  3377. sd->special_state.bonus_coma = 1;
  3378. break;
  3379. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3380. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3381. if(sd->state.lr_flag != 2)
  3382. sd->magic_addrace2[type2] += val;
  3383. break;
  3384. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3385. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3386. if (sd->state.lr_flag != 2)
  3387. sd->ignore_mdef_by_race2[type2] += val;
  3388. break;
  3389. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3390. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3391. if (sd->state.lr_flag != 2)
  3392. sd->dropaddrace[type2] += val;
  3393. break;
  3394. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3395. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3396. if (sd->state.lr_flag != 2)
  3397. sd->dropaddclass[type2] += val;
  3398. break;
  3399. default:
  3400. if (running_npc_stat_calc_event) {
  3401. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3402. }
  3403. else if (current_equip_combo_pos > 0) {
  3404. 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);
  3405. }
  3406. else {
  3407. 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);
  3408. }
  3409. break;
  3410. }
  3411. }
  3412. /**
  3413. * Gives item bonus to player for format: bonus3 bBonusName,type2,val;
  3414. * @param sd
  3415. * @param type Bonus type used by bBonusName
  3416. * @param type2
  3417. * @param val Value that usually for rate or fixed value
  3418. */
  3419. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3420. {
  3421. nullpo_retv(sd);
  3422. switch(type){
  3423. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3424. if(sd->state.lr_flag != 2)
  3425. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
  3426. break;
  3427. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3428. if(sd->state.lr_flag != 2)
  3429. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
  3430. break;
  3431. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3432. if(sd->state.lr_flag != 2)
  3433. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
  3434. break;
  3435. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3436. if(sd->state.lr_flag != 2)
  3437. {
  3438. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3439. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3440. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  3441. target?-type2:type2, type3, val, 0, current_equip_card_id);
  3442. }
  3443. break;
  3444. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3445. if(sd->state.lr_flag != 2)
  3446. {
  3447. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3448. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3449. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  3450. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3451. }
  3452. break;
  3453. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3454. if (sd->state.lr_flag != 2)
  3455. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
  3456. break;
  3457. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3458. if (sd->state.lr_flag != 2)
  3459. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
  3460. break;
  3461. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3462. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3463. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3464. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3465. break;
  3466. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3467. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3468. if(sd->state.lr_flag != 2)
  3469. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
  3470. break;
  3471. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3472. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3473. if( sd->state.lr_flag != 2 )
  3474. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
  3475. break;
  3476. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3477. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3478. if (sd->state.lr_flag != 2)
  3479. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3480. break;
  3481. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3482. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3483. if (sd->state.lr_flag != 2)
  3484. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3485. break;
  3486. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3487. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3488. if(sd->state.lr_flag != 2) {
  3489. sd->sp_vanish_race[type2].rate += type3;
  3490. sd->sp_vanish_race[type2].per += val;
  3491. }
  3492. break;
  3493. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3494. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3495. if(sd->state.lr_flag != 2) {
  3496. sd->hp_vanish_race[type2].rate += type3;
  3497. sd->hp_vanish_race[type2].per += val;
  3498. }
  3499. break;
  3500. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3501. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3502. if (sd->state.lr_flag == 2)
  3503. break;
  3504. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3505. //if (type3 > sd->norecover_state_race[type2].rate) {
  3506. // sd->norecover_state_race[type2].rate = type3;
  3507. // sd->norecover_state_race[type2].tick = val;
  3508. // break;
  3509. //}
  3510. sd->norecover_state_race[type2].rate = type3;
  3511. sd->norecover_state_race[type2].tick = val;
  3512. break;
  3513. default:
  3514. if (running_npc_stat_calc_event) {
  3515. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3516. }
  3517. else if (current_equip_combo_pos > 0) {
  3518. 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);
  3519. }
  3520. else {
  3521. 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);
  3522. }
  3523. break;
  3524. }
  3525. }
  3526. /**
  3527. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
  3528. * @param sd
  3529. * @param type Bonus type used by bBonusName
  3530. * @param type2
  3531. * @param type3
  3532. * @param val Value that usually for rate or fixed value
  3533. */
  3534. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3535. {
  3536. nullpo_retv(sd);
  3537. switch(type){
  3538. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3539. if(sd->state.lr_flag != 2)
  3540. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3541. break;
  3542. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3543. if(sd->state.lr_flag != 2)
  3544. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3545. break;
  3546. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3547. if(sd->state.lr_flag != 2)
  3548. {
  3549. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3550. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3551. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3552. }
  3553. break;
  3554. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3555. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3556. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  3557. sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3558. break;
  3559. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3560. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3561. if (sd->state.lr_flag != 2)
  3562. pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
  3563. break;
  3564. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3565. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3566. if( sd->state.lr_flag != 2 )
  3567. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
  3568. break;
  3569. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3570. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3571. if(sd->state.lr_flag == 2)
  3572. break;
  3573. sd->def_set_race[type2].rate = type3;
  3574. sd->def_set_race[type2].tick = type4;
  3575. sd->def_set_race[type2].value = val;
  3576. break;
  3577. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3578. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3579. if(sd->state.lr_flag == 2)
  3580. break;
  3581. sd->mdef_set_race[type2].rate = type3;
  3582. sd->mdef_set_race[type2].tick = type4;
  3583. sd->mdef_set_race[type2].value = val;
  3584. break;
  3585. default:
  3586. if (running_npc_stat_calc_event) {
  3587. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3588. }
  3589. else if (current_equip_combo_pos > 0) {
  3590. 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);
  3591. }
  3592. else {
  3593. 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);
  3594. }
  3595. break;
  3596. }
  3597. }
  3598. /**
  3599. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3600. * @param sd
  3601. * @param type Bonus type used by bBonusName
  3602. * @param type2
  3603. * @param type3
  3604. * @param type4
  3605. * @param val Value that usually for rate or fixed value
  3606. */
  3607. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3608. {
  3609. nullpo_retv(sd);
  3610. switch(type){
  3611. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3612. if(sd->state.lr_flag != 2)
  3613. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3614. break;
  3615. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3616. if(sd->state.lr_flag != 2)
  3617. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3618. break;
  3619. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3620. if(sd->state.lr_flag != 2)
  3621. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3622. break;
  3623. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3624. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3625. if( sd->state.lr_flag != 2 )
  3626. pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
  3627. break;
  3628. default:
  3629. if (running_npc_stat_calc_event) {
  3630. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3631. }
  3632. else if (current_equip_combo_pos > 0) {
  3633. 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);
  3634. }
  3635. else {
  3636. 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);
  3637. }
  3638. break;
  3639. }
  3640. }
  3641. /*==========================================
  3642. * Grants a player a given skill. Flag values are:
  3643. * 0 - Grant permanent skill to be bound to skill tree
  3644. * 1 - Grant an item skill (temporary)
  3645. * 2 - Like 1, except the level granted can stack with previously learned level.
  3646. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3647. *------------------------------------------*/
  3648. bool pc_skill(TBL_PC* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3649. uint16 idx = 0;
  3650. nullpo_ret(sd);
  3651. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3652. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3653. return false;
  3654. }
  3655. if (level > MAX_SKILL_LEVEL) {
  3656. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3657. return false;
  3658. }
  3659. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3660. 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);
  3661. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3662. }
  3663. switch (type) {
  3664. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3665. sd->status.skill[idx].id = skill_id;
  3666. sd->status.skill[idx].lv = level;
  3667. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3668. if (level == 0) { //Remove skill.
  3669. sd->status.skill[idx].id = 0;
  3670. clif_deleteskill(sd,skill_id);
  3671. } else
  3672. clif_addskill(sd,skill_id);
  3673. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3674. status_calc_pc(sd, SCO_NONE);
  3675. break;
  3676. case ADDSKILL_TEMP: //Item bonus skill.
  3677. if (sd->status.skill[idx].id != 0) {
  3678. if (sd->status.skill[idx].lv >= level)
  3679. return true;
  3680. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3681. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3682. } else {
  3683. sd->status.skill[idx].id = skill_id;
  3684. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3685. }
  3686. sd->status.skill[idx].lv = level;
  3687. break;
  3688. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3689. if (sd->status.skill[idx].id != 0) {
  3690. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3691. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3692. } else {
  3693. sd->status.skill[idx].id = skill_id;
  3694. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3695. }
  3696. sd->status.skill[idx].lv += level;
  3697. break;
  3698. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3699. sd->status.skill[idx].id = skill_id;
  3700. sd->status.skill[idx].lv = level;
  3701. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3702. if (level == 0) { //Remove skill.
  3703. sd->status.skill[idx].id = 0;
  3704. clif_deleteskill(sd,skill_id);
  3705. } else
  3706. clif_addskill(sd,skill_id);
  3707. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3708. status_calc_pc(sd, SCO_NONE);
  3709. break;
  3710. default:
  3711. return false;
  3712. }
  3713. return true;
  3714. }
  3715. /*==========================================
  3716. * Append a card to an item ?
  3717. *------------------------------------------*/
  3718. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3719. {
  3720. int i;
  3721. unsigned short nameid;
  3722. nullpo_ret(sd);
  3723. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3724. return 0; //Invalid item index.
  3725. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3726. return 0; //Invalid card index.
  3727. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3728. return 0; // target item missing
  3729. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3730. return 0; // target card missing
  3731. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3732. return 0; // only weapons and armor are allowed
  3733. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3734. return 0; // must be a card
  3735. if( sd->status.inventory[idx_equip].identify == 0 )
  3736. return 0; // target must be identified
  3737. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3738. return 0; // card slots reserved for other purposes
  3739. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3740. return 0; // card cannot be compounded on this item type
  3741. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3742. return 0; // attempted to place shield card on left-hand weapon.
  3743. if( sd->status.inventory[idx_equip].equip != 0 )
  3744. return 0; // item must be unequipped
  3745. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3746. if( i == sd->inventory_data[idx_equip]->slot )
  3747. return 0; // no free slots
  3748. // remember the card id to insert
  3749. nameid = sd->status.inventory[idx_card].nameid;
  3750. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3751. {// failed
  3752. clif_insert_card(sd,idx_equip,idx_card,1);
  3753. }
  3754. else
  3755. {// success
  3756. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3757. sd->status.inventory[idx_equip].card[i] = nameid;
  3758. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3759. clif_insert_card(sd,idx_equip,idx_card,0);
  3760. }
  3761. return 0;
  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 < 0) val = 0;
  3780. if(orig_value > 0 && val < 1) val = 1;
  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 < 0) val = 0;
  3794. if(orig_value > 0 && val < 1) val = 1;
  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->status.inventory[i].nameid==nameid){
  3820. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.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->status.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_disp_onlyself(sd,output,strlen(output));
  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_disp_onlyself(sd,output,strlen(output));
  3899. }
  3900. return 0;
  3901. }
  3902. /**
  3903. * Attempts to remove Cash Points from player
  3904. * @param sd: Player
  3905. * @param price: Points player has to pay
  3906. * @param points: Points player has
  3907. * @param type: Log type
  3908. * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
  3909. */
  3910. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  3911. {
  3912. int cash;
  3913. nullpo_retr(-1,sd);
  3914. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3915. if( price < 0 || points < 0 )
  3916. {
  3917. 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);
  3918. return -2;
  3919. }
  3920. if( points > price )
  3921. {
  3922. 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);
  3923. points = price;
  3924. }
  3925. cash = price-points;
  3926. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3927. {
  3928. 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);
  3929. return -1;
  3930. }
  3931. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints-cash);
  3932. if( cash ){
  3933. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  3934. }
  3935. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints-points);
  3936. if( points ){
  3937. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  3938. }
  3939. if( battle_config.cashshop_show_points )
  3940. {
  3941. char output[CHAT_SIZE_MAX];
  3942. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
  3943. clif_disp_onlyself(sd, output, strlen(output));
  3944. }
  3945. return cash+points;
  3946. }
  3947. /**
  3948. * Attempts to give Cash Points to player
  3949. * @param sd: Player
  3950. * @param cash: Cash player gets
  3951. * @param points: Points player has
  3952. * @param type: Log type
  3953. * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
  3954. */
  3955. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  3956. {
  3957. char output[CHAT_SIZE_MAX];
  3958. nullpo_retr(-1,sd);
  3959. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3960. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3961. if( cash > 0 )
  3962. {
  3963. if( cash > MAX_ZENY-sd->cashPoints )
  3964. {
  3965. 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);
  3966. cash = MAX_ZENY-sd->cashPoints;
  3967. }
  3968. pc_setaccountreg(sd, add_str("#CASHPOINTS"), sd->cashPoints+cash);
  3969. if( cash ){
  3970. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  3971. }
  3972. if( battle_config.cashshop_show_points )
  3973. {
  3974. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  3975. clif_disp_onlyself(sd, output, strlen(output));
  3976. }
  3977. return cash;
  3978. }
  3979. else if( cash < 0 )
  3980. {
  3981. 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);
  3982. return -1;
  3983. }
  3984. if( points > 0 )
  3985. {
  3986. if( points > MAX_ZENY-sd->kafraPoints )
  3987. {
  3988. 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);
  3989. points = MAX_ZENY-sd->kafraPoints;
  3990. }
  3991. pc_setaccountreg(sd, add_str("#KAFRAPOINTS"), sd->kafraPoints+points);
  3992. if( points ){
  3993. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  3994. }
  3995. if( battle_config.cashshop_show_points )
  3996. {
  3997. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  3998. clif_disp_onlyself(sd, output, strlen(output));
  3999. }
  4000. return points;
  4001. }
  4002. else if( points < 0 )
  4003. {
  4004. 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);
  4005. return -1;
  4006. }
  4007. return -2; //shouldn't happen but just in case
  4008. }
  4009. /**
  4010. * Searching a specified itemid in inventory and return his stored index
  4011. * @param sd Player
  4012. * @param nameid Find this Item!
  4013. * @return Stored index in inventory, or -1 if not found.
  4014. **/
  4015. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4016. short i;
  4017. nullpo_retr(-1, sd);
  4018. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && (sd->status.inventory[i].amount > 0 || nameid == 0) );
  4019. return ( i < MAX_INVENTORY ) ? i : -1;
  4020. }
  4021. /** Attempt to add a new item to player inventory
  4022. * @param sd
  4023. * @param item_data
  4024. * @param amount
  4025. * @param log_type
  4026. * @return
  4027. * 0 = success
  4028. * 1 = invalid itemid not found or negative amount
  4029. * 2 = overweight
  4030. * 3 = ?
  4031. * 4 = no free place found
  4032. * 5 = max amount reached
  4033. * 6 = ?
  4034. * 7 = stack limitation
  4035. */
  4036. char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4037. struct item_data *id;
  4038. int16 i;
  4039. unsigned int w;
  4040. nullpo_retr(1, sd);
  4041. nullpo_retr(1, item);
  4042. if( item->nameid == 0 || amount <= 0 )
  4043. return ADDITEM_INVALID;
  4044. if( amount > MAX_AMOUNT )
  4045. return ADDITEM_OVERAMOUNT;
  4046. id = itemdb_search(item->nameid);
  4047. if( id->stack.inventory && amount > id->stack.amount )
  4048. {// item stack limitation
  4049. return ADDITEM_STACKLIMIT;
  4050. }
  4051. w = id->weight*amount;
  4052. if(sd->weight + w > sd->max_weight)
  4053. return ADDITEM_OVERWEIGHT;
  4054. i = MAX_INVENTORY;
  4055. if (id->flag.guid && !item->unique_id)
  4056. item->unique_id = pc_generate_unique_id(sd);
  4057. // Stackable | Non Rental
  4058. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4059. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4060. if( sd->status.inventory[i].nameid == item->nameid &&
  4061. sd->status.inventory[i].bound == item->bound &&
  4062. sd->status.inventory[i].expire_time == 0 &&
  4063. sd->status.inventory[i].unique_id == item->unique_id &&
  4064. memcmp(&sd->status.inventory[i].card, &item->card, sizeof(item->card)) == 0
  4065. )
  4066. {
  4067. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->status.inventory[i].amount ) )
  4068. return ADDITEM_OVERAMOUNT;
  4069. sd->status.inventory[i].amount += amount;
  4070. clif_additem(sd,i,amount,0);
  4071. break;
  4072. }
  4073. }
  4074. }
  4075. if (i >= MAX_INVENTORY) {
  4076. i = pc_search_inventory(sd,0);
  4077. if( i < 0 )
  4078. return ADDITEM_OVERITEM;
  4079. memcpy(&sd->status.inventory[i], item, sizeof(sd->status.inventory[0]));
  4080. // clear equip and favorite fields first, just in case
  4081. if( item->equip )
  4082. sd->status.inventory[i].equip = 0;
  4083. if( item->favorite )
  4084. sd->status.inventory[i].favorite = 0;
  4085. sd->status.inventory[i].amount = amount;
  4086. sd->inventory_data[i] = id;
  4087. sd->last_addeditem_index = i;
  4088. if (!itemdb_isstackable2(id) || id->flag.guid)
  4089. sd->status.inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4090. clif_additem(sd,i,amount,0);
  4091. }
  4092. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  4093. sd->weight += w;
  4094. clif_updatestatus(sd,SP_WEIGHT);
  4095. //Auto-equip
  4096. if(id->flag.autoequip)
  4097. pc_equipitem(sd, i, id->equip);
  4098. /* rental item check */
  4099. if( item->expire_time ) {
  4100. if( time(NULL) > item->expire_time ) {
  4101. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  4102. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4103. } else {
  4104. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4105. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  4106. pc_inventory_rental_add(sd, seconds);
  4107. }
  4108. }
  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
  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->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4125. return 1;
  4126. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  4127. sd->status.inventory[n].amount -= amount;
  4128. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4129. if( sd->status.inventory[n].amount <= 0 ){
  4130. if(sd->status.inventory[n].equip)
  4131. pc_unequipitem(sd,n,3);
  4132. memset(&sd->status.inventory[n],0,sizeof(sd->status.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->status.inventory[n].nameid <= 0 ||
  4156. sd->status.inventory[n].amount <= 0 ||
  4157. sd->status.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[sd->bl.m].flag.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->status.inventory[n]) )
  4168. {
  4169. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4170. return false;
  4171. }
  4172. if (!map_addflooritem(&sd->status.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->status.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( !item->script ) //if it has no script, you can't really consume it!
  4262. return false;
  4263. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4264. return true;
  4265. if(map[sd->bl.m].flag.noitemconsumption) //consumable but mapflag prevent it
  4266. return false;
  4267. //Prevent mass item usage. [Skotlex]
  4268. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4269. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4270. )
  4271. return false;
  4272. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4273. clif_msg(sd,ITEM_NOUSE_SITTING);
  4274. return false; // You cannot use this item while sitting.
  4275. }
  4276. if (sd->state.storage_flag && item->type != IT_CASH) {
  4277. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd,388));
  4278. return false; // You cannot use this item while storage is open.
  4279. }
  4280. if (item->flag.dead_branch && (map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m)))
  4281. return false;
  4282. switch( nameid ) {
  4283. case ITEMID_ANODYNE:
  4284. if( map_flag_gvg(sd->bl.m) )
  4285. return false;
  4286. case ITEMID_WING_OF_FLY:
  4287. case ITEMID_GIANT_FLY_WING:
  4288. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) ) {
  4289. clif_skill_teleportmessage(sd,0);
  4290. return false;
  4291. }
  4292. case ITEMID_WING_OF_BUTTERFLY:
  4293. case ITEMID_DUN_TELE_SCROLL1:
  4294. case ITEMID_DUN_TELE_SCROLL2:
  4295. case ITEMID_DUN_TELE_SCROLL3:
  4296. case ITEMID_WOB_RUNE:
  4297. case ITEMID_WOB_SCHWALTZ:
  4298. case ITEMID_WOB_RACHEL:
  4299. case ITEMID_WOB_LOCAL:
  4300. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4301. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4302. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4303. return false;
  4304. }
  4305. if( nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && map[sd->bl.m].flag.noreturn )
  4306. return false;
  4307. break;
  4308. case ITEMID_BUBBLE_GUM:
  4309. case ITEMID_COMP_BUBBLE_GUM:
  4310. if( sd->sc.data[SC_ITEMBOOST] )
  4311. return false;
  4312. break;
  4313. case ITEMID_BATTLE_MANUAL:
  4314. case ITEMID_COMP_BATTLE_MANUAL:
  4315. case ITEMID_THICK_BATTLE_MANUAL:
  4316. case ITEMID_NOBLE_NAMEPLATE:
  4317. case ITEMID_BATTLE_MANUAL25:
  4318. case ITEMID_BATTLE_MANUAL100:
  4319. case ITEMID_BATTLE_MANUAL300:
  4320. if( sd->sc.data[SC_EXPBOOST] )
  4321. return false;
  4322. break;
  4323. case ITEMID_JOB_MANUAL50:
  4324. if( sd->sc.data[SC_JEXPBOOST] )
  4325. return false;
  4326. break;
  4327. case ITEMID_MERCENARY_RED_POTION:
  4328. case ITEMID_MERCENARY_BLUE_POTION:
  4329. case ITEMID_M_CENTER_POTION:
  4330. case ITEMID_M_AWAKENING_POTION:
  4331. case ITEMID_M_BERSERK_POTION:
  4332. if( sd->md == NULL || sd->md->db == NULL )
  4333. return false;
  4334. if( sd->md->sc.data[SC_BERSERK] )
  4335. return false;
  4336. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4337. return false;
  4338. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4339. return false;
  4340. break;
  4341. case ITEMID_NEURALIZER:
  4342. if( !map[sd->bl.m].flag.reset )
  4343. return false;
  4344. break;
  4345. }
  4346. if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL )
  4347. return false; // Mercenary Scrolls
  4348. /**
  4349. * Only Rune Knights may use runes
  4350. **/
  4351. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  4352. return false;
  4353. /**
  4354. * Only GCross may use poisons
  4355. **/
  4356. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  4357. return false;
  4358. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4359. if( pc_is90overweight(sd) ) {
  4360. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4361. return false;
  4362. }
  4363. if( !pc_inventoryblank(sd) ) {
  4364. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 732)); //Item cannot be open when inventory is full
  4365. return false;
  4366. }
  4367. }
  4368. //Gender check
  4369. if(item->sex != 2 && sd->status.sex != item->sex)
  4370. return false;
  4371. //Required level check
  4372. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4373. return false;
  4374. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4375. return false;
  4376. //Not equipable by class. [Skotlex]
  4377. if (!(
  4378. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4379. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4380. ))
  4381. return false;
  4382. if (sd->sc.count && (
  4383. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4384. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4385. sd->sc.data[SC_TRICKDEAD] ||
  4386. sd->sc.data[SC_HIDING] ||
  4387. sd->sc.data[SC__SHADOWFORM] ||
  4388. sd->sc.data[SC__INVISIBILITY] ||
  4389. sd->sc.data[SC__MANHOLE] ||
  4390. sd->sc.data[SC_KAGEHUMI] ||
  4391. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4392. sd->sc.data[SC_HEAT_BARREL_AFTER] ||
  4393. sd->sc.data[SC_KINGS_GRACE] ||
  4394. sd->sc.data[SC_SUHIDE]))
  4395. return false;
  4396. if (!pc_isItemClass(sd,item))
  4397. return false;
  4398. //Dead Branch items
  4399. if( item->flag.dead_branch )
  4400. log_branch(sd);
  4401. return true;
  4402. }
  4403. /*==========================================
  4404. * Last checks to use an item.
  4405. * Return:
  4406. * 0 = fail
  4407. * 1 = success
  4408. *------------------------------------------*/
  4409. int pc_useitem(struct map_session_data *sd,int n)
  4410. {
  4411. unsigned int tick = gettick();
  4412. int amount;
  4413. unsigned short nameid;
  4414. struct script_code *script;
  4415. struct item item;
  4416. struct item_data *id;
  4417. nullpo_ret(sd);
  4418. if (sd->npc_id) {
  4419. if (sd->progressbar.npc_id) {
  4420. clif_progressbar_abort(sd);
  4421. return 0; // First item use attempt cancels the progress bar
  4422. }
  4423. #ifdef RENEWAL
  4424. if (pc_hasprogress(sd, WIP_DISABLE_SKILLITEM)) {
  4425. clif_msg(sd, WORK_IN_PROGRESS);
  4426. return 0;
  4427. }
  4428. #else
  4429. if (!sd->npc_item_flag)
  4430. return 0;
  4431. #endif
  4432. }
  4433. item = sd->status.inventory[n];
  4434. id = sd->inventory_data[n];
  4435. if (item.nameid == 0 || item.amount <= 0)
  4436. return 0;
  4437. if( !pc_isUseitem(sd,n) )
  4438. return 0;
  4439. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4440. nameid = id->nameid;
  4441. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4442. return 0;
  4443. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4444. if( id->flag.delay_consume ) {
  4445. if( nameid != ITEMID_REINS_OF_MOUNT && &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  4446. return 0;
  4447. else if( pc_issit(sd) )
  4448. return 0;
  4449. }
  4450. //Since most delay-consume items involve using a "skill-type" target cursor,
  4451. //perform a skill-use check before going through. [Skotlex]
  4452. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4453. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  4454. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4455. return 0;
  4456. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4457. return 0;
  4458. /* on restricted maps the item is consumed but the effect is not used */
  4459. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4460. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4461. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4462. clif_useitemack(sd,n,item.amount-1,true);
  4463. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4464. }
  4465. return 0;/* regardless, effect is not run */
  4466. }
  4467. sd->itemid = item.nameid;
  4468. sd->itemindex = n;
  4469. if(sd->catch_target_class != -1) //Abort pet catching.
  4470. sd->catch_target_class = -1;
  4471. amount = item.amount;
  4472. script = id->script;
  4473. //Check if the item is to be consumed immediately [Skotlex]
  4474. if (id->flag.delay_consume)
  4475. clif_useitemack(sd, n, amount, true);
  4476. else
  4477. {
  4478. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4479. {
  4480. clif_useitemack(sd, n, amount - 1, true);
  4481. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4482. }
  4483. else
  4484. clif_useitemack(sd, n, 0, false);
  4485. }
  4486. if(item.card[0]==CARD0_CREATE &&
  4487. pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4488. {
  4489. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4490. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  4491. potion_flag = 3; //Even more effective potions.
  4492. }
  4493. //Update item use time.
  4494. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4495. if( itemdb_iscashfood(nameid) )
  4496. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4497. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4498. potion_flag = 0;
  4499. return 1;
  4500. }
  4501. /**
  4502. * Add item on cart for given index.
  4503. * @param sd
  4504. * @param item
  4505. * @param amount
  4506. * @param log_type
  4507. * @return 0 = success; 1 = fail; 2 = no slot
  4508. */
  4509. unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4510. {
  4511. struct item_data *data;
  4512. int i,w;
  4513. nullpo_retr(1, sd);
  4514. nullpo_retr(1, item);
  4515. if(item->nameid == 0 || amount <= 0)
  4516. return 1;
  4517. data = itemdb_search(item->nameid);
  4518. if( data->stack.cart && amount > data->stack.amount )
  4519. {// item stack limitation
  4520. return 1;
  4521. }
  4522. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4523. { // Check item trade restrictions [Skotlex]
  4524. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4525. return 1;
  4526. }
  4527. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4528. return 1;
  4529. // ID no longer points to inventory/kafra ID. While we get a new one we don't want to mess up vending creation.
  4530. item->id = 0;
  4531. i = MAX_CART;
  4532. if( itemdb_isstackable2(data) && !item->expire_time )
  4533. {
  4534. for (i = 0; i < MAX_CART; i++) {
  4535. if (sd->status.cart[i].nameid == item->nameid
  4536. && sd->status.cart[i].bound == item->bound
  4537. && sd->status.cart[i].unique_id == item->unique_id
  4538. && memcmp(sd->status.cart[i].card, item->card, sizeof(item->card)) == 0
  4539. )
  4540. break;
  4541. }
  4542. }
  4543. if( i < MAX_CART )
  4544. {// item already in cart, stack it
  4545. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  4546. return 2; // no slot
  4547. sd->status.cart[i].amount+=amount;
  4548. clif_cart_additem(sd,i,amount,0);
  4549. }
  4550. else
  4551. {// item not stackable or not present, add it
  4552. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  4553. if( i == MAX_CART )
  4554. return 2; // no slot
  4555. memcpy(&sd->status.cart[i],item,sizeof(sd->status.cart[0]));
  4556. sd->status.cart[i].amount=amount;
  4557. sd->cart_num++;
  4558. clif_cart_additem(sd,i,amount,0);
  4559. }
  4560. sd->status.cart[i].favorite = 0;/* clear */
  4561. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  4562. sd->cart_weight += w;
  4563. clif_updatestatus(sd,SP_CARTINFO);
  4564. return 0;
  4565. }
  4566. /*==========================================
  4567. * Delete item on cart for given index.
  4568. *------------------------------------------*/
  4569. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4570. {
  4571. nullpo_retv(sd);
  4572. if(sd->status.cart[n].nameid == 0 ||
  4573. sd->status.cart[n].amount<amount)
  4574. return;
  4575. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  4576. sd->status.cart[n].amount -= amount;
  4577. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  4578. if(sd->status.cart[n].amount <= 0){
  4579. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  4580. sd->cart_num--;
  4581. }
  4582. if(!type) {
  4583. clif_cart_delitem(sd,n,amount);
  4584. clif_updatestatus(sd,SP_CARTINFO);
  4585. }
  4586. }
  4587. /*==========================================
  4588. * Transfer item from inventory to cart.
  4589. *------------------------------------------*/
  4590. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4591. {
  4592. struct item *item_data;
  4593. char flag;
  4594. nullpo_retv(sd);
  4595. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4596. return;
  4597. item_data = &sd->status.inventory[idx];
  4598. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4599. return;
  4600. if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
  4601. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4602. else {
  4603. clif_dropitem(sd,idx,0);
  4604. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  4605. }
  4606. }
  4607. /*==========================================
  4608. * Get number of item in cart.
  4609. * Return:
  4610. -1 = itemid not found or no amount found
  4611. x = remaining itemid on cart after get
  4612. *------------------------------------------*/
  4613. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4614. {
  4615. struct item* item_data;
  4616. nullpo_retr(-1, sd);
  4617. item_data = &sd->status.cart[idx];
  4618. if( item_data->nameid == 0 || item_data->amount == 0 )
  4619. return -1;
  4620. return item_data->amount - amount;
  4621. }
  4622. /*==========================================
  4623. * Retrieve an item at index idx from cart.
  4624. *------------------------------------------*/
  4625. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4626. {
  4627. struct item *item_data;
  4628. unsigned char flag = 0;
  4629. nullpo_retv(sd);
  4630. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4631. return;
  4632. item_data=&sd->status.cart[idx];
  4633. if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4634. return;
  4635. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4636. pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4637. else {
  4638. clif_dropitem(sd,idx,0);
  4639. clif_additem(sd,0,0,flag);
  4640. }
  4641. }
  4642. /*==========================================
  4643. * Bound Item Check
  4644. * Type:
  4645. * 1 Account Bound
  4646. * 2 Guild Bound
  4647. * 3 Party Bound
  4648. * 4 Character Bound
  4649. *------------------------------------------*/
  4650. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4651. {
  4652. int i=0, j=0;
  4653. for(i=0;i<MAX_INVENTORY;i++){
  4654. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0 && sd->status.inventory[i].bound == type) {
  4655. idxlist[j] = i;
  4656. j++;
  4657. }
  4658. }
  4659. return j;
  4660. }
  4661. /*==========================================
  4662. * Display item stolen msg to player sd
  4663. *------------------------------------------*/
  4664. int pc_show_steal(struct block_list *bl,va_list ap)
  4665. {
  4666. struct map_session_data *sd;
  4667. int itemid;
  4668. struct item_data *item=NULL;
  4669. char output[100];
  4670. sd=va_arg(ap,struct map_session_data *);
  4671. itemid=va_arg(ap,int);
  4672. if((item=itemdb_exists(itemid))==NULL)
  4673. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4674. else
  4675. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4676. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4677. return 0;
  4678. }
  4679. /*==========================================
  4680. * Steal an item from bl (mob).
  4681. * Return:
  4682. * 0 = fail
  4683. * 1 = succes
  4684. *------------------------------------------*/
  4685. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4686. {
  4687. int i,itemid;
  4688. double rate;
  4689. unsigned char flag = 0;
  4690. struct status_data *sd_status, *md_status;
  4691. struct mob_data *md;
  4692. struct item tmp_item;
  4693. if(!sd || !bl || bl->type!=BL_MOB)
  4694. return 0;
  4695. md = (TBL_MOB *)bl;
  4696. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE ) ) //already stolen from / status change check
  4697. return 0;
  4698. sd_status= status_get_status_data(&sd->bl);
  4699. md_status= status_get_status_data(bl);
  4700. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4701. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4702. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4703. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4704. ) { //Can't steal from
  4705. md->state.steal_flag = UCHAR_MAX;
  4706. return 0;
  4707. }
  4708. // base skill success chance (percentual)
  4709. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4710. rate += sd->bonus.add_steal_rate;
  4711. if( rate < 1 )
  4712. return 0;
  4713. // Try dropping one item, in the order from first to last possible slot.
  4714. // Droprate is affected by the skill success rate.
  4715. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4716. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4717. break;
  4718. if( i == MAX_STEAL_DROP )
  4719. return 0;
  4720. itemid = md->db->dropitem[i].nameid;
  4721. memset(&tmp_item,0,sizeof(tmp_item));
  4722. tmp_item.nameid = itemid;
  4723. tmp_item.amount = 1;
  4724. tmp_item.identify = itemdb_isidentified(itemid);
  4725. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4726. //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?
  4727. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4728. if(flag) { //Failed to steal due to overweight
  4729. clif_additem(sd,0,0,flag);
  4730. return 0;
  4731. }
  4732. if(battle_config.show_steal_in_same_party)
  4733. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4734. //Logs items, Stolen from mobs [Lupus]
  4735. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4736. //A Rare Steal Global Announce by Lupus
  4737. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4738. struct item_data *i_data;
  4739. char message[128];
  4740. i_data = itemdb_search(itemid);
  4741. sprintf (message, msg_txt(sd,542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4742. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4743. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4744. }
  4745. return 1;
  4746. }
  4747. /*==========================================
  4748. * Stole zeny from bl (mob)
  4749. * return
  4750. * 0 = fail
  4751. * 1 = success
  4752. *------------------------------------------*/
  4753. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4754. {
  4755. int rate,skill;
  4756. struct mob_data *md;
  4757. if(!sd || !target || target->type != BL_MOB)
  4758. return 0;
  4759. md = (TBL_MOB*)target;
  4760. 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)
  4761. return 0;
  4762. // FIXME: This formula is either custom or outdated.
  4763. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4764. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4765. if(rnd()%1000 < rate)
  4766. {
  4767. int amount = md->level*10 + rnd()%100;
  4768. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4769. md->state.steal_coin_flag = 1;
  4770. return 1;
  4771. }
  4772. return 0;
  4773. }
  4774. /*==========================================
  4775. * Set's a player position.
  4776. * @param sd
  4777. * @param mapindex
  4778. * @param x
  4779. * @param y
  4780. * @param clrtype
  4781. * @return SETPOS_OK Success
  4782. * SETPOS_MAPINDEX Invalid map index
  4783. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  4784. * SETPOS_AUTOTRADE Player is in autotrade state
  4785. *------------------------------------------*/
  4786. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4787. {
  4788. int16 m;
  4789. nullpo_retr(SETPOS_OK,sd);
  4790. if( !mapindex || !mapindex_id2name(mapindex) ) {
  4791. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4792. return SETPOS_MAPINDEX;
  4793. }
  4794. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  4795. return SETPOS_AUTOTRADE;
  4796. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  4797. pc_setstand(sd, true);
  4798. pc_setrestartvalue(sd,1);
  4799. }
  4800. m = map_mapindex2mapid(mapindex);
  4801. sd->state.changemap = (sd->mapindex != mapindex);
  4802. sd->state.warping = 1;
  4803. sd->state.workinprogress = WIP_DISABLE_NONE;
  4804. if(map[sd->bl.m].instance_id && sd->state.changemap && !map[m].instance_id) {
  4805. bool instance_found = false;
  4806. struct party_data *p = NULL;
  4807. struct guild *g = NULL;
  4808. if (sd->instance_id) {
  4809. instance_delusers(sd->instance_id);
  4810. instance_found = true;
  4811. }
  4812. if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
  4813. instance_delusers(p->instance_id);
  4814. instance_found = true;
  4815. }
  4816. if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  4817. instance_delusers(g->instance_id);
  4818. }
  4819. if( sd->state.changemap ) { // Misc map-changing settings
  4820. int i;
  4821. sd->state.pmap = sd->bl.m;
  4822. if (sd->sc.count) { // Cancel some map related stuff.
  4823. if (sd->sc.data[SC_JAILED])
  4824. return SETPOS_MAPINDEX; //You may not get out!
  4825. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4826. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4827. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4828. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4829. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4830. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4831. if (sd->sc.data[SC_KNOWLEDGE]) {
  4832. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4833. if (sce->timer != INVALID_TIMER)
  4834. delete_timer(sce->timer, status_change_timer);
  4835. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4836. }
  4837. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4838. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4839. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4840. }
  4841. for( i = 0; i < EQI_MAX; i++ ) {
  4842. if( sd->equip_index[i] >= 0 )
  4843. if( pc_isequip(sd,sd->equip_index[i]) )
  4844. pc_unequipitem(sd,sd->equip_index[i],2);
  4845. }
  4846. if (battle_config.clear_unit_onwarp&BL_PC)
  4847. skill_clear_unitgroup(&sd->bl);
  4848. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4849. guild_send_dot_remove(sd);
  4850. bg_send_dot_remove(sd);
  4851. if (sd->regen.state.gc)
  4852. sd->regen.state.gc = 0;
  4853. // make sure vending is allowed here
  4854. if (sd->state.vending && map[m].flag.novending) {
  4855. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  4856. vending_closevending(sd);
  4857. }
  4858. channel_pcquit(sd,4); //quit map chan
  4859. }
  4860. if( m < 0 )
  4861. {
  4862. uint32 ip;
  4863. uint16 port;
  4864. //if can't find any map-servers, just abort setting position.
  4865. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4866. return SETPOS_NO_MAPSERVER;
  4867. if (sd->npc_id)
  4868. npc_event_dequeue(sd);
  4869. npc_script_event(sd, NPCE_LOGOUT);
  4870. //remove from map, THEN change x/y coordinates
  4871. unit_remove_map_pc(sd,clrtype);
  4872. sd->mapindex = mapindex;
  4873. sd->bl.x=x;
  4874. sd->bl.y=y;
  4875. pc_clean_skilltree(sd);
  4876. chrif_save(sd,2);
  4877. chrif_changemapserver(sd, ip, (short)port);
  4878. //Free session data from this map server [Kevin]
  4879. unit_free_pc(sd);
  4880. return SETPOS_OK;
  4881. }
  4882. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4883. {
  4884. 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);
  4885. x = y = 0; // make it random
  4886. }
  4887. if( x == 0 && y == 0 ) { // pick a random walkable cell
  4888. int c=0;
  4889. do {
  4890. x = rnd()%(map[m].xs-2)+1;
  4891. y = rnd()%(map[m].ys-2)+1;
  4892. c++;
  4893. if(c > (map[m].xs * map[m].ys)*3){ //force out
  4894. 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));
  4895. return SETPOS_OK; //preventing warp
  4896. //break; //allow warp anyway
  4897. }
  4898. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  4899. }
  4900. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4901. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  4902. vending_closevending(sd);
  4903. }
  4904. if(sd->bl.prev != NULL){
  4905. unit_remove_map_pc(sd,clrtype);
  4906. clif_changemap(sd,m,x,y); // [MouseJstr]
  4907. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  4908. sd->state.rewarp = 1;
  4909. sd->mapindex = mapindex;
  4910. sd->bl.m = m;
  4911. sd->bl.x = sd->ud.to_x = x;
  4912. sd->bl.y = sd->ud.to_y = y;
  4913. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4914. { // Increased guild castle regen [Valaris]
  4915. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4916. if(gc && gc->guild_id == sd->status.guild_id)
  4917. sd->regen.state.gc = 1;
  4918. }
  4919. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4920. {
  4921. sd->pd->bl.m = m;
  4922. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4923. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4924. sd->pd->ud.dir = sd->ud.dir;
  4925. }
  4926. if( hom_is_active(sd->hd) )
  4927. {
  4928. sd->hd->bl.m = m;
  4929. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4930. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4931. sd->hd->ud.dir = sd->ud.dir;
  4932. }
  4933. if( sd->md )
  4934. {
  4935. sd->md->bl.m = m;
  4936. sd->md->bl.x = sd->md->ud.to_x = x;
  4937. sd->md->bl.y = sd->md->ud.to_y = y;
  4938. sd->md->ud.dir = sd->ud.dir;
  4939. }
  4940. if( sd->ed ) {
  4941. sd->ed->bl.m = m;
  4942. sd->ed->bl.x = sd->ed->ud.to_x = x;
  4943. sd->ed->bl.y = sd->ed->ud.to_y = y;
  4944. sd->ed->ud.dir = sd->ud.dir;
  4945. }
  4946. pc_cell_basilica(sd);
  4947. //check if we gonna be rewarped [lighta]
  4948. if(npc_check_areanpc(1,m,x,y,0)){
  4949. sd->count_rewarp++;
  4950. }
  4951. else
  4952. sd->count_rewarp = 0;
  4953. return SETPOS_OK;
  4954. }
  4955. /*==========================================
  4956. * Warp player sd to random location on current map.
  4957. * May fail if no walkable cell found (1000 attempts).
  4958. * Return:
  4959. * 0 = Success
  4960. * 1,2,3 = Fail
  4961. *------------------------------------------*/
  4962. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  4963. {
  4964. int x,y,i=0;
  4965. int16 m;
  4966. nullpo_ret(sd);
  4967. m=sd->bl.m;
  4968. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4969. return 3;
  4970. do {
  4971. x = rnd()%(map[m].xs-2)+1;
  4972. y = rnd()%(map[m].ys-2)+1;
  4973. } while((map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1))) && (i++) < 1000);
  4974. if (i < 1000)
  4975. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4976. return 3;
  4977. }
  4978. /*==========================================
  4979. * Records a memo point at sd's current position
  4980. * pos - entry to replace, (-1: shift oldest entry out)
  4981. *------------------------------------------*/
  4982. bool pc_memo(struct map_session_data* sd, int pos)
  4983. {
  4984. int skill;
  4985. nullpo_ret(sd);
  4986. // check mapflags
  4987. if( sd->bl.m >= 0 && (map[sd->bl.m].flag.nomemo || map[sd->bl.m].flag.nowarpto) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  4988. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4989. return false;
  4990. }
  4991. // check inputs
  4992. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4993. return false; // invalid input
  4994. // check required skill level
  4995. skill = pc_checkskill(sd, AL_WARP);
  4996. if( skill < 1 ) {
  4997. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4998. return false;
  4999. }
  5000. if( skill < 2 || skill - 2 < pos ) {
  5001. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5002. return false;
  5003. }
  5004. if( pos == -1 )
  5005. {
  5006. uint8 i;
  5007. // prevent memo-ing the same map multiple times
  5008. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5009. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5010. pos = 0;
  5011. }
  5012. if( map[sd->bl.m].instance_id ) {
  5013. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5014. return false;
  5015. }
  5016. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5017. sd->status.memo_point[pos].x = sd->bl.x;
  5018. sd->status.memo_point[pos].y = sd->bl.y;
  5019. clif_skill_memomessage(sd, 0);
  5020. return true;
  5021. }
  5022. //
  5023. // Skills
  5024. //
  5025. /**
  5026. * Get the skill current cooldown for player.
  5027. * (get the db base cooldown for skill + player specific cooldown)
  5028. * @param sd : player pointer
  5029. * @param id : skill id
  5030. * @param lv : skill lv
  5031. * @return player skill cooldown
  5032. */
  5033. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5034. uint8 i;
  5035. uint16 idx = skill_get_index(skill_id);
  5036. int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
  5037. if (!idx) return 0;
  5038. if (skill_db[idx]->cooldown[skill_lv - 1])
  5039. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5040. ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
  5041. if (i < cooldownlen) {
  5042. cooldown += sd->skillcooldown[i].val;
  5043. cooldown = max(0,cooldown);
  5044. }
  5045. return cooldown;
  5046. }
  5047. /*==========================================
  5048. * Return player sd skill_lv learned for given skill
  5049. *------------------------------------------*/
  5050. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5051. {
  5052. uint16 idx = 0;
  5053. if (sd == NULL)
  5054. return 0;
  5055. if ((idx = skill_get_index(skill_id)) == 0) {
  5056. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5057. return 0;
  5058. }
  5059. if (SKILL_CHK_GUILD(skill_id) ) {
  5060. struct guild *g;
  5061. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5062. return guild_checkskill(g,skill_id);
  5063. return 0;
  5064. }
  5065. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5066. }
  5067. /**
  5068. * Check if we still have the correct weapon to continue the skill (actually status)
  5069. * If not ending it
  5070. * @param sd
  5071. * @return 0:error, 1:check done
  5072. */
  5073. static void pc_checkallowskill(struct map_session_data *sd)
  5074. {
  5075. const enum sc_type scw_list[] = {
  5076. SC_TWOHANDQUICKEN,
  5077. SC_ONEHAND,
  5078. SC_AURABLADE,
  5079. SC_PARRYING,
  5080. SC_SPEARQUICKEN,
  5081. SC_ADRENALINE,
  5082. SC_ADRENALINE2,
  5083. SC_DANCING,
  5084. SC_GATLINGFEVER,
  5085. };
  5086. uint8 i;
  5087. nullpo_retv(sd);
  5088. if(!sd->sc.count)
  5089. return;
  5090. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5091. { // Skills requiring specific weapon types
  5092. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5093. continue;
  5094. if(sd->sc.data[scw_list[i]] &&
  5095. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5096. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5097. }
  5098. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5099. // Spurt requires bare hands (feet, in fact xD)
  5100. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5101. if(sd->status.shield <= 0) { // Skills requiring a shield
  5102. const enum sc_type scs_list[] = {
  5103. SC_AUTOGUARD,
  5104. SC_DEFENDER,
  5105. SC_REFLECTSHIELD,
  5106. SC_REFLECTDAMAGE
  5107. };
  5108. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5109. if(sd->sc.data[scs_list[i]])
  5110. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5111. }
  5112. }
  5113. /*==========================================
  5114. * Return equipped index of item on player sd at pos
  5115. * Return
  5116. * -1 : Nothing equipped
  5117. * idx : (this index could be used in inventory to found item_data)
  5118. *------------------------------------------*/
  5119. short pc_checkequip(struct map_session_data *sd,int pos)
  5120. {
  5121. uint8 i;
  5122. nullpo_retr(-1, sd);
  5123. for(i=0;i<EQI_MAX;i++){
  5124. if(pos & equip_bitmask[i])
  5125. return sd->equip_index[i];
  5126. }
  5127. return -1;
  5128. }
  5129. /*==========================================
  5130. * Check if sd has nameid equipped somewhere
  5131. * @sd : the player session
  5132. * @nameid : id of the item to check
  5133. * @min : : see pc.h enum equip_index from ? to @max
  5134. * @max : see pc.h enum equip_index for @min to ?
  5135. * -return true,false
  5136. *------------------------------------------*/
  5137. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5138. {
  5139. int i;
  5140. for(i = min; i < max; i++) {
  5141. if(equip_bitmask[i]) {
  5142. int idx = sd->equip_index[i];
  5143. if (sd->status.inventory[idx].nameid == nameid)
  5144. return true;
  5145. }
  5146. }
  5147. return false;
  5148. }
  5149. /*==========================================
  5150. * Convert's from the client's lame Job ID system
  5151. * to the map server's 'makes sense' system. [Skotlex]
  5152. *------------------------------------------*/
  5153. int pc_jobid2mapid(unsigned short b_class)
  5154. {
  5155. switch(b_class)
  5156. {
  5157. //Novice And 1-1 Jobs
  5158. case JOB_NOVICE: return MAPID_NOVICE;
  5159. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5160. case JOB_MAGE: return MAPID_MAGE;
  5161. case JOB_ARCHER: return MAPID_ARCHER;
  5162. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5163. case JOB_MERCHANT: return MAPID_MERCHANT;
  5164. case JOB_THIEF: return MAPID_THIEF;
  5165. case JOB_TAEKWON: return MAPID_TAEKWON;
  5166. case JOB_WEDDING: return MAPID_WEDDING;
  5167. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5168. case JOB_NINJA: return MAPID_NINJA;
  5169. case JOB_XMAS: return MAPID_XMAS;
  5170. case JOB_SUMMER: return MAPID_SUMMER;
  5171. case JOB_HANBOK: return MAPID_HANBOK;
  5172. case JOB_GANGSI: return MAPID_GANGSI;
  5173. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5174. //2-1 Jobs
  5175. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5176. case JOB_KNIGHT: return MAPID_KNIGHT;
  5177. case JOB_WIZARD: return MAPID_WIZARD;
  5178. case JOB_HUNTER: return MAPID_HUNTER;
  5179. case JOB_PRIEST: return MAPID_PRIEST;
  5180. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5181. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5182. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5183. case JOB_KAGEROU:
  5184. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5185. case JOB_REBELLION: return MAPID_REBELLION;
  5186. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5187. //2-2 Jobs
  5188. case JOB_CRUSADER: return MAPID_CRUSADER;
  5189. case JOB_SAGE: return MAPID_SAGE;
  5190. case JOB_BARD:
  5191. case JOB_DANCER: return MAPID_BARDDANCER;
  5192. case JOB_MONK: return MAPID_MONK;
  5193. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5194. case JOB_ROGUE: return MAPID_ROGUE;
  5195. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5196. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5197. //Trans Novice And Trans 1-1 Jobs
  5198. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5199. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5200. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5201. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5202. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5203. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5204. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5205. //Trans 2-1 Jobs
  5206. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5207. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5208. case JOB_SNIPER: return MAPID_SNIPER;
  5209. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5210. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5211. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5212. //Trans 2-2 Jobs
  5213. case JOB_PALADIN: return MAPID_PALADIN;
  5214. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5215. case JOB_CLOWN:
  5216. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5217. case JOB_CHAMPION: return MAPID_CHAMPION;
  5218. case JOB_CREATOR: return MAPID_CREATOR;
  5219. case JOB_STALKER: return MAPID_STALKER;
  5220. //Baby Novice And Baby 1-1 Jobs
  5221. case JOB_BABY: return MAPID_BABY;
  5222. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5223. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5224. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5225. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5226. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5227. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5228. //Baby 2-1 Jobs
  5229. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5230. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5231. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5232. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5233. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5234. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5235. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5236. //Baby 2-2 Jobs
  5237. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5238. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5239. case JOB_BABY_BARD:
  5240. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5241. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5242. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5243. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5244. //3-1 Jobs
  5245. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5246. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5247. case JOB_WARLOCK: return MAPID_WARLOCK;
  5248. case JOB_RANGER: return MAPID_RANGER;
  5249. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5250. case JOB_MECHANIC: return MAPID_MECHANIC;
  5251. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5252. //3-2 Jobs
  5253. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5254. case JOB_SORCERER: return MAPID_SORCERER;
  5255. case JOB_MINSTREL:
  5256. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5257. case JOB_SURA: return MAPID_SURA;
  5258. case JOB_GENETIC: return MAPID_GENETIC;
  5259. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5260. //Trans 3-1 Jobs
  5261. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5262. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5263. case JOB_RANGER_T: return MAPID_RANGER_T;
  5264. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5265. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5266. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5267. //Trans 3-2 Jobs
  5268. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5269. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5270. case JOB_MINSTREL_T:
  5271. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5272. case JOB_SURA_T: return MAPID_SURA_T;
  5273. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5274. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5275. //Baby 3-1 Jobs
  5276. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5277. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5278. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5279. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5280. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5281. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5282. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5283. //Baby 3-2 Jobs
  5284. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5285. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5286. case JOB_BABY_MINSTREL:
  5287. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5288. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5289. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5290. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5291. //Doram Jobs
  5292. case JOB_SUMMONER: return MAPID_SUMMONER;
  5293. default:
  5294. return -1;
  5295. }
  5296. }
  5297. //Reverts the map-style class id to the client-style one.
  5298. int pc_mapid2jobid(unsigned short class_, int sex)
  5299. {
  5300. switch(class_) {
  5301. //Novice And 1-1 Jobs
  5302. case MAPID_NOVICE: return JOB_NOVICE;
  5303. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5304. case MAPID_MAGE: return JOB_MAGE;
  5305. case MAPID_ARCHER: return JOB_ARCHER;
  5306. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5307. case MAPID_MERCHANT: return JOB_MERCHANT;
  5308. case MAPID_THIEF: return JOB_THIEF;
  5309. case MAPID_TAEKWON: return JOB_TAEKWON;
  5310. case MAPID_WEDDING: return JOB_WEDDING;
  5311. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5312. case MAPID_NINJA: return JOB_NINJA;
  5313. case MAPID_XMAS: return JOB_XMAS;
  5314. case MAPID_SUMMER: return JOB_SUMMER;
  5315. case MAPID_HANBOK: return JOB_HANBOK;
  5316. case MAPID_GANGSI: return JOB_GANGSI;
  5317. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5318. //2-1 Jobs
  5319. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5320. case MAPID_KNIGHT: return JOB_KNIGHT;
  5321. case MAPID_WIZARD: return JOB_WIZARD;
  5322. case MAPID_HUNTER: return JOB_HUNTER;
  5323. case MAPID_PRIEST: return JOB_PRIEST;
  5324. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5325. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5326. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5327. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5328. case MAPID_REBELLION: return JOB_REBELLION;
  5329. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5330. //2-2 Jobs
  5331. case MAPID_CRUSADER: return JOB_CRUSADER;
  5332. case MAPID_SAGE: return JOB_SAGE;
  5333. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5334. case MAPID_MONK: return JOB_MONK;
  5335. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5336. case MAPID_ROGUE: return JOB_ROGUE;
  5337. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5338. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5339. //Trans Novice And Trans 2-1 Jobs
  5340. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5341. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5342. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5343. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5344. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5345. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5346. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5347. //Trans 2-1 Jobs
  5348. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5349. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5350. case MAPID_SNIPER: return JOB_SNIPER;
  5351. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5352. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5353. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5354. //Trans 2-2 Jobs
  5355. case MAPID_PALADIN: return JOB_PALADIN;
  5356. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5357. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5358. case MAPID_CHAMPION: return JOB_CHAMPION;
  5359. case MAPID_CREATOR: return JOB_CREATOR;
  5360. case MAPID_STALKER: return JOB_STALKER;
  5361. //Baby Novice And Baby 1-1 Jobs
  5362. case MAPID_BABY: return JOB_BABY;
  5363. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5364. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5365. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5366. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5367. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5368. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5369. //Baby 2-1 Jobs
  5370. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5371. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5372. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5373. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5374. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5375. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5376. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5377. //Baby 2-2 Jobs
  5378. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5379. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5380. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5381. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5382. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5383. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5384. //3-1 Jobs
  5385. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5386. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5387. case MAPID_WARLOCK: return JOB_WARLOCK;
  5388. case MAPID_RANGER: return JOB_RANGER;
  5389. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5390. case MAPID_MECHANIC: return JOB_MECHANIC;
  5391. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5392. //3-2 Jobs
  5393. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5394. case MAPID_SORCERER: return JOB_SORCERER;
  5395. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5396. case MAPID_SURA: return JOB_SURA;
  5397. case MAPID_GENETIC: return JOB_GENETIC;
  5398. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5399. //Trans 3-1 Jobs
  5400. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5401. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5402. case MAPID_RANGER_T: return JOB_RANGER_T;
  5403. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5404. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5405. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5406. //Trans 3-2 Jobs
  5407. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5408. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5409. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5410. case MAPID_SURA_T: return JOB_SURA_T;
  5411. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5412. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5413. //Baby 3-1 Jobs
  5414. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5415. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5416. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5417. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5418. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5419. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5420. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5421. //Baby 3-2 Jobs
  5422. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5423. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5424. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5425. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5426. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5427. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5428. //Doram Jobs
  5429. case MAPID_SUMMONER: return JOB_SUMMONER;
  5430. default:
  5431. return -1;
  5432. }
  5433. }
  5434. /*====================================================
  5435. * This function return the name of the job (by [Yor])
  5436. *----------------------------------------------------*/
  5437. const char* job_name(int class_)
  5438. {
  5439. switch (class_) {
  5440. case JOB_NOVICE:
  5441. case JOB_SWORDMAN:
  5442. case JOB_MAGE:
  5443. case JOB_ARCHER:
  5444. case JOB_ACOLYTE:
  5445. case JOB_MERCHANT:
  5446. case JOB_THIEF:
  5447. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5448. case JOB_KNIGHT:
  5449. case JOB_PRIEST:
  5450. case JOB_WIZARD:
  5451. case JOB_BLACKSMITH:
  5452. case JOB_HUNTER:
  5453. case JOB_ASSASSIN:
  5454. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5455. case JOB_KNIGHT2:
  5456. return msg_txt(NULL,557);
  5457. case JOB_CRUSADER:
  5458. case JOB_MONK:
  5459. case JOB_SAGE:
  5460. case JOB_ROGUE:
  5461. case JOB_ALCHEMIST:
  5462. case JOB_BARD:
  5463. case JOB_DANCER:
  5464. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5465. case JOB_CRUSADER2:
  5466. return msg_txt(NULL,563);
  5467. case JOB_WEDDING:
  5468. case JOB_SUPER_NOVICE:
  5469. case JOB_GUNSLINGER:
  5470. case JOB_NINJA:
  5471. case JOB_XMAS:
  5472. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5473. case JOB_SUMMER:
  5474. return msg_txt(NULL,621);
  5475. case JOB_HANBOK:
  5476. return msg_txt(NULL,694);
  5477. case JOB_OKTOBERFEST:
  5478. return msg_txt(NULL,696);
  5479. case JOB_NOVICE_HIGH:
  5480. case JOB_SWORDMAN_HIGH:
  5481. case JOB_MAGE_HIGH:
  5482. case JOB_ARCHER_HIGH:
  5483. case JOB_ACOLYTE_HIGH:
  5484. case JOB_MERCHANT_HIGH:
  5485. case JOB_THIEF_HIGH:
  5486. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5487. case JOB_LORD_KNIGHT:
  5488. case JOB_HIGH_PRIEST:
  5489. case JOB_HIGH_WIZARD:
  5490. case JOB_WHITESMITH:
  5491. case JOB_SNIPER:
  5492. case JOB_ASSASSIN_CROSS:
  5493. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5494. case JOB_LORD_KNIGHT2:
  5495. return msg_txt(NULL,582);
  5496. case JOB_PALADIN:
  5497. case JOB_CHAMPION:
  5498. case JOB_PROFESSOR:
  5499. case JOB_STALKER:
  5500. case JOB_CREATOR:
  5501. case JOB_CLOWN:
  5502. case JOB_GYPSY:
  5503. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5504. case JOB_PALADIN2:
  5505. return msg_txt(NULL,588);
  5506. case JOB_BABY:
  5507. case JOB_BABY_SWORDMAN:
  5508. case JOB_BABY_MAGE:
  5509. case JOB_BABY_ARCHER:
  5510. case JOB_BABY_ACOLYTE:
  5511. case JOB_BABY_MERCHANT:
  5512. case JOB_BABY_THIEF:
  5513. return msg_txt(NULL,595 - JOB_BABY + class_);
  5514. case JOB_BABY_KNIGHT:
  5515. case JOB_BABY_PRIEST:
  5516. case JOB_BABY_WIZARD:
  5517. case JOB_BABY_BLACKSMITH:
  5518. case JOB_BABY_HUNTER:
  5519. case JOB_BABY_ASSASSIN:
  5520. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5521. case JOB_BABY_KNIGHT2:
  5522. return msg_txt(NULL,602);
  5523. case JOB_BABY_CRUSADER:
  5524. case JOB_BABY_MONK:
  5525. case JOB_BABY_SAGE:
  5526. case JOB_BABY_ROGUE:
  5527. case JOB_BABY_ALCHEMIST:
  5528. case JOB_BABY_BARD:
  5529. case JOB_BABY_DANCER:
  5530. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5531. case JOB_BABY_CRUSADER2:
  5532. return msg_txt(NULL,608);
  5533. case JOB_SUPER_BABY:
  5534. return msg_txt(NULL,615);
  5535. case JOB_TAEKWON:
  5536. return msg_txt(NULL,616);
  5537. case JOB_STAR_GLADIATOR:
  5538. case JOB_STAR_GLADIATOR2:
  5539. return msg_txt(NULL,617);
  5540. case JOB_SOUL_LINKER:
  5541. return msg_txt(NULL,618);
  5542. case JOB_GANGSI:
  5543. case JOB_DEATH_KNIGHT:
  5544. case JOB_DARK_COLLECTOR:
  5545. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5546. case JOB_RUNE_KNIGHT:
  5547. case JOB_WARLOCK:
  5548. case JOB_RANGER:
  5549. case JOB_ARCH_BISHOP:
  5550. case JOB_MECHANIC:
  5551. case JOB_GUILLOTINE_CROSS:
  5552. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5553. case JOB_RUNE_KNIGHT_T:
  5554. case JOB_WARLOCK_T:
  5555. case JOB_RANGER_T:
  5556. case JOB_ARCH_BISHOP_T:
  5557. case JOB_MECHANIC_T:
  5558. case JOB_GUILLOTINE_CROSS_T:
  5559. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5560. case JOB_ROYAL_GUARD:
  5561. case JOB_SORCERER:
  5562. case JOB_MINSTREL:
  5563. case JOB_WANDERER:
  5564. case JOB_SURA:
  5565. case JOB_GENETIC:
  5566. case JOB_SHADOW_CHASER:
  5567. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5568. case JOB_ROYAL_GUARD_T:
  5569. case JOB_SORCERER_T:
  5570. case JOB_MINSTREL_T:
  5571. case JOB_WANDERER_T:
  5572. case JOB_SURA_T:
  5573. case JOB_GENETIC_T:
  5574. case JOB_SHADOW_CHASER_T:
  5575. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5576. case JOB_RUNE_KNIGHT2:
  5577. case JOB_RUNE_KNIGHT_T2:
  5578. return msg_txt(NULL,625);
  5579. case JOB_ROYAL_GUARD2:
  5580. case JOB_ROYAL_GUARD_T2:
  5581. return msg_txt(NULL,631);
  5582. case JOB_RANGER2:
  5583. case JOB_RANGER_T2:
  5584. return msg_txt(NULL,627);
  5585. case JOB_MECHANIC2:
  5586. case JOB_MECHANIC_T2:
  5587. return msg_txt(NULL,629);
  5588. case JOB_BABY_RUNE:
  5589. case JOB_BABY_WARLOCK:
  5590. case JOB_BABY_RANGER:
  5591. case JOB_BABY_BISHOP:
  5592. case JOB_BABY_MECHANIC:
  5593. case JOB_BABY_CROSS:
  5594. case JOB_BABY_GUARD:
  5595. case JOB_BABY_SORCERER:
  5596. case JOB_BABY_MINSTREL:
  5597. case JOB_BABY_WANDERER:
  5598. case JOB_BABY_SURA:
  5599. case JOB_BABY_GENETIC:
  5600. case JOB_BABY_CHASER:
  5601. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5602. case JOB_BABY_RUNE2:
  5603. return msg_txt(NULL,638);
  5604. case JOB_BABY_GUARD2:
  5605. return msg_txt(NULL,644);
  5606. case JOB_BABY_RANGER2:
  5607. return msg_txt(NULL,640);
  5608. case JOB_BABY_MECHANIC2:
  5609. return msg_txt(NULL,642);
  5610. case JOB_SUPER_NOVICE_E:
  5611. case JOB_SUPER_BABY_E:
  5612. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5613. case JOB_KAGEROU:
  5614. case JOB_OBORO:
  5615. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5616. case JOB_REBELLION:
  5617. return msg_txt(NULL,695);
  5618. case JOB_SUMMONER:
  5619. return msg_txt(NULL,697);
  5620. default:
  5621. return msg_txt(NULL,655);
  5622. }
  5623. }
  5624. /*====================================================
  5625. * Timered function to make id follow a target.
  5626. * @id = bl.id (player only atm)
  5627. * target is define in sd->followtarget (bl.id)
  5628. * used by pc_follow
  5629. *----------------------------------------------------*/
  5630. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  5631. {
  5632. struct map_session_data *sd;
  5633. struct block_list *tbl;
  5634. sd = map_id2sd(id);
  5635. nullpo_ret(sd);
  5636. if (sd->followtimer != tid){
  5637. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5638. sd->followtimer = INVALID_TIMER;
  5639. return 0;
  5640. }
  5641. sd->followtimer = INVALID_TIMER;
  5642. tbl = map_id2bl(sd->followtarget);
  5643. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  5644. {
  5645. pc_stop_following(sd);
  5646. return 0;
  5647. }
  5648. // either player or target is currently detached from map blocks (could be teleporting),
  5649. // but still connected to this map, so we'll just increment the timer and check back later
  5650. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5651. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5652. {
  5653. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5654. if (!check_distance_bl(&sd->bl, tbl, 5))
  5655. unit_walktobl(&sd->bl, tbl, 5, 0);
  5656. } else
  5657. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5658. }
  5659. sd->followtimer = add_timer(
  5660. tick + 1000, // increase time a bit to loosen up map's load
  5661. pc_follow_timer, sd->bl.id, 0);
  5662. return 0;
  5663. }
  5664. int pc_stop_following (struct map_session_data *sd)
  5665. {
  5666. nullpo_ret(sd);
  5667. if (sd->followtimer != INVALID_TIMER) {
  5668. delete_timer(sd->followtimer,pc_follow_timer);
  5669. sd->followtimer = INVALID_TIMER;
  5670. }
  5671. sd->followtarget = -1;
  5672. sd->ud.target_to = 0;
  5673. unit_stop_walking(&sd->bl, 1);
  5674. return 0;
  5675. }
  5676. int pc_follow(struct map_session_data *sd,int target_id)
  5677. {
  5678. struct block_list *bl = map_id2bl(target_id);
  5679. if (bl == NULL /*|| bl->type != BL_PC*/)
  5680. return 1;
  5681. if (sd->followtimer != INVALID_TIMER)
  5682. pc_stop_following(sd);
  5683. sd->followtarget = target_id;
  5684. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5685. return 0;
  5686. }
  5687. int pc_checkbaselevelup(struct map_session_data *sd) {
  5688. unsigned int next = pc_nextbaseexp(sd);
  5689. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5690. return 0;
  5691. do {
  5692. sd->status.base_exp -= next;
  5693. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5694. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  5695. sd->status.base_exp = next-1;
  5696. next = pc_gets_status_point(sd->status.base_level);
  5697. sd->status.base_level++;
  5698. sd->status.status_point += next;
  5699. if( pc_is_maxbaselv(sd) ){
  5700. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5701. break;
  5702. }
  5703. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5704. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5705. status_calc_pet(sd->pd,SCO_NONE);
  5706. clif_updatestatus(sd,SP_STATUSPOINT);
  5707. clif_updatestatus(sd,SP_BASELEVEL);
  5708. clif_updatestatus(sd,SP_BASEEXP);
  5709. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5710. status_calc_pc(sd,SCO_FORCE);
  5711. status_percent_heal(&sd->bl,100,100);
  5712. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5713. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5714. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5715. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5716. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5717. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5718. if (sd->state.snovice_dead_flag)
  5719. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5720. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5721. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5722. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5723. }
  5724. clif_misceffect(&sd->bl,0);
  5725. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5726. if(sd->status.party_id)
  5727. party_send_levelup(sd);
  5728. pc_baselevelchanged(sd);
  5729. return 1;
  5730. }
  5731. void pc_baselevelchanged(struct map_session_data *sd) {
  5732. uint8 i;
  5733. for( i = 0; i < EQI_MAX; i++ ) {
  5734. if( sd->equip_index[i] >= 0 ) {
  5735. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5736. pc_unequipitem(sd, sd->equip_index[i], 3);
  5737. }
  5738. }
  5739. pc_show_questinfo(sd);
  5740. }
  5741. int pc_checkjoblevelup(struct map_session_data *sd)
  5742. {
  5743. unsigned int next = pc_nextjobexp(sd);
  5744. nullpo_ret(sd);
  5745. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  5746. return 0;
  5747. do {
  5748. sd->status.job_exp -= next;
  5749. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5750. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5751. sd->status.job_exp = next-1;
  5752. sd->status.job_level ++;
  5753. sd->status.skill_point ++;
  5754. if( pc_is_maxjoblv(sd) ){
  5755. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  5756. break;
  5757. }
  5758. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5759. clif_updatestatus(sd,SP_JOBLEVEL);
  5760. clif_updatestatus(sd,SP_JOBEXP);
  5761. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5762. clif_updatestatus(sd,SP_SKILLPOINT);
  5763. status_calc_pc(sd,SCO_FORCE);
  5764. clif_misceffect(&sd->bl,1);
  5765. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  5766. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5767. npc_script_event(sd, NPCE_JOBLVUP);
  5768. return 1;
  5769. }
  5770. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  5771. * @param sd Player
  5772. * @param base_exp Base EXP before peronal bonuses
  5773. * @param job_exp Job EXP before peronal bonuses
  5774. * @param src Block list that affecting the exp calculation
  5775. */
  5776. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5777. {
  5778. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  5779. if (src) {
  5780. struct status_data *status = status_get_status_data(src);
  5781. if( sd->expaddrace[status->race] )
  5782. bonus += sd->expaddrace[status->race];
  5783. if( sd->expaddrace[RC_ALL] )
  5784. bonus += sd->expaddrace[RC_ALL];
  5785. if( sd->expaddclass[status->class_] )
  5786. bonus += sd->expaddclass[status->class_];
  5787. if( sd->expaddclass[CLASS_ALL] )
  5788. bonus += sd->expaddclass[CLASS_ALL];
  5789. if (battle_config.pk_mode &&
  5790. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5791. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5792. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  5793. vip_bonus_base = battle_config.vip_base_exp_increase;
  5794. vip_bonus_job = battle_config.vip_job_exp_increase;
  5795. }
  5796. }
  5797. // Give EXPBOOST for quests even if src is NULL.
  5798. if (&sd->sc && sd->sc.data[SC_EXPBOOST]) {
  5799. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5800. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  5801. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  5802. }
  5803. if (*base_exp) {
  5804. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  5805. *base_exp = cap_value(exp, 1, UINT_MAX);
  5806. }
  5807. // Give JEXPBOOST for quests even if src is NULL.
  5808. if (&sd->sc && sd->sc.data[SC_JEXPBOOST])
  5809. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5810. if (*job_exp) {
  5811. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  5812. *job_exp = cap_value(exp, 1, UINT_MAX);
  5813. }
  5814. return;
  5815. }
  5816. /**
  5817. * Show EXP gained by player in percentage by @showexp
  5818. * @param sd Player
  5819. * @param base_exp Base EXP gained/loss
  5820. * @param next_base_exp Base EXP needed for next base level
  5821. * @param job_exp Job EXP gained/loss
  5822. * @param next_job_exp Job EXP needed for next job level
  5823. * @param lost True:EXP penalty, lose EXP
  5824. **/
  5825. 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) {
  5826. char output[CHAT_SIZE_MAX];
  5827. nullpo_retv(sd);
  5828. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  5829. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  5830. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  5831. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  5832. clif_disp_onlyself(sd, output, strlen(output));
  5833. }
  5834. /**
  5835. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  5836. * @param sd Player
  5837. * @param src EXP source
  5838. * @param base_exp Base EXP gained
  5839. * @param base_exp Job EXP gained
  5840. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  5841. * @return
  5842. **/
  5843. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  5844. {
  5845. unsigned int nextb = 0, nextj = 0;
  5846. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  5847. nullpo_retv(sd);
  5848. if(sd->bl.prev == NULL || pc_isdead(sd))
  5849. return;
  5850. if (!(exp_flag&2)) {
  5851. if (!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5852. return; // no exp on pvp maps
  5853. if (sd->status.guild_id>0)
  5854. base_exp -= guild_payexp(sd,base_exp);
  5855. }
  5856. flag = ((base_exp) ? 1 : 0) |
  5857. ((job_exp) ? 2 : 0) |
  5858. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  5859. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  5860. if (!(exp_flag&2))
  5861. pc_calcexp(sd, &base_exp, &job_exp, src);
  5862. nextb = pc_nextbaseexp(sd);
  5863. nextj = pc_nextjobexp(sd);
  5864. if (flag&4){
  5865. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  5866. base_exp = 0;
  5867. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  5868. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  5869. }
  5870. if (flag&8){
  5871. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  5872. job_exp = 0;
  5873. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  5874. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  5875. }
  5876. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  5877. //Note that this value should never be greater than the original
  5878. //therefore no overflow checks are needed. [Skotlex]
  5879. if (nextb > 0) {
  5880. float nextbp = (float) base_exp / (float) nextb;
  5881. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  5882. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5883. }
  5884. if (nextj > 0) {
  5885. float nextjp = (float) job_exp / (float) nextj;
  5886. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  5887. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5888. }
  5889. }
  5890. // Give EXP for Base Level
  5891. if (base_exp) {
  5892. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  5893. sd->status.base_exp = UINT32_MAX;
  5894. else
  5895. sd->status.base_exp += base_exp;
  5896. if (!pc_checkbaselevelup(sd))
  5897. clif_updatestatus(sd,SP_BASEEXP);
  5898. }
  5899. // Give EXP for Job Level
  5900. if (job_exp) {
  5901. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  5902. sd->status.job_exp = UINT32_MAX;
  5903. else
  5904. sd->status.job_exp += job_exp;
  5905. if (!pc_checkjoblevelup(sd))
  5906. clif_updatestatus(sd,SP_JOBEXP);
  5907. }
  5908. if (flag&1)
  5909. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  5910. if (flag&2)
  5911. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  5912. if (sd->state.showexp && (base_exp || job_exp))
  5913. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  5914. }
  5915. /**
  5916. * Lost Base/Job EXP from a player
  5917. * @param sd Player
  5918. * @param base_exp Base EXP lost
  5919. * @param job_exp Job EXP lost
  5920. **/
  5921. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  5922. nullpo_retv(sd);
  5923. if (base_exp) {
  5924. base_exp = u32min(sd->status.base_exp, base_exp);
  5925. sd->status.base_exp -= base_exp;
  5926. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  5927. clif_updatestatus(sd, SP_BASEEXP);
  5928. }
  5929. if (job_exp) {
  5930. job_exp = u32min(sd->status.job_exp, job_exp);
  5931. sd->status.job_exp -= job_exp;
  5932. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  5933. clif_updatestatus(sd, SP_JOBEXP);
  5934. }
  5935. if (sd->state.showexp && (base_exp || job_exp))
  5936. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  5937. }
  5938. /**
  5939. * Returns max base level for this character's class.
  5940. * @param class_: Player's class
  5941. * @return Max Base Level
  5942. */
  5943. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  5944. return job_info[pc_class2idx(class_)].max_level[0];
  5945. }
  5946. /**
  5947. * Returns max base level for this character.
  5948. * @param sd Player
  5949. * @return Max Base Level
  5950. **/
  5951. unsigned int pc_maxbaselv(struct map_session_data *sd){
  5952. return pc_class_maxbaselv(sd->status.class_);
  5953. }
  5954. /**
  5955. * Returns max job level for this character's class.
  5956. * @param class_: Player's class
  5957. * @return Max Job Level
  5958. */
  5959. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  5960. return job_info[pc_class2idx(class_)].max_level[1];
  5961. }
  5962. /**
  5963. * Returns max job level for this character.
  5964. * @param sd Player
  5965. * @return Max Job Level
  5966. **/
  5967. unsigned int pc_maxjoblv(struct map_session_data *sd){
  5968. return pc_class_maxjoblv(sd->status.class_);
  5969. }
  5970. /**
  5971. * Check if player is reached max base level
  5972. * @param sd
  5973. * @return True if reached max level
  5974. **/
  5975. bool pc_is_maxbaselv(struct map_session_data *sd) {
  5976. nullpo_retr(false, sd);
  5977. return (sd->status.base_level >= pc_maxbaselv(sd));
  5978. }
  5979. /**
  5980. * Check if player is reached max base level
  5981. * @param sd
  5982. * @return True if reached max level
  5983. **/
  5984. bool pc_is_maxjoblv(struct map_session_data *sd) {
  5985. nullpo_retr(false, sd);
  5986. return (sd->status.job_level >= pc_maxjoblv(sd));
  5987. }
  5988. /**
  5989. * Base exp needed for player to level up.
  5990. * @param sd
  5991. * @return Base EXP needed for next base level
  5992. **/
  5993. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  5994. nullpo_ret(sd);
  5995. if (sd->status.base_level == 0) // Is this something that possible?
  5996. return 0;
  5997. if (pc_is_maxbaselv(sd))
  5998. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  5999. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6000. }
  6001. /**
  6002. * Job exp needed for player to level up.
  6003. * @param sd
  6004. * @return Job EXP needed for next job level
  6005. **/
  6006. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6007. nullpo_ret(sd);
  6008. if (sd->status.job_level == 0) // Is this something that possible?
  6009. return 0;
  6010. if (pc_is_maxjoblv(sd))
  6011. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6012. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6013. }
  6014. /// Returns the value of the specified stat.
  6015. static int pc_getstat(struct map_session_data* sd, int type)
  6016. {
  6017. nullpo_retr(-1, sd);
  6018. switch( type ) {
  6019. case SP_STR: return sd->status.str;
  6020. case SP_AGI: return sd->status.agi;
  6021. case SP_VIT: return sd->status.vit;
  6022. case SP_INT: return sd->status.int_;
  6023. case SP_DEX: return sd->status.dex;
  6024. case SP_LUK: return sd->status.luk;
  6025. default:
  6026. return -1;
  6027. }
  6028. }
  6029. /// Sets the specified stat to the specified value.
  6030. /// Returns the new value.
  6031. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6032. {
  6033. nullpo_retr(-1, sd);
  6034. switch( type ) {
  6035. case SP_STR: sd->status.str = val; break;
  6036. case SP_AGI: sd->status.agi = val; break;
  6037. case SP_VIT: sd->status.vit = val; break;
  6038. case SP_INT: sd->status.int_ = val; break;
  6039. case SP_DEX: sd->status.dex = val; break;
  6040. case SP_LUK: sd->status.luk = val; break;
  6041. default:
  6042. return -1;
  6043. }
  6044. return val;
  6045. }
  6046. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6047. int pc_gets_status_point(int level)
  6048. {
  6049. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6050. return (statp[level+1] - statp[level]);
  6051. else //Default increase
  6052. return ((level+15) / 5);
  6053. }
  6054. #ifdef RENEWAL_STAT
  6055. /// Renewal status point cost formula
  6056. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6057. #else
  6058. /// Pre-Renewal status point cost formula
  6059. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6060. #endif
  6061. /// Returns the number of stat points needed to change the specified stat by val.
  6062. /// If val is negative, returns the number of stat points that would be needed to
  6063. /// raise the specified stat from (current value - val) to current value.
  6064. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6065. {
  6066. int low, high, sp = 0, max = 0;
  6067. if ( val == 0 )
  6068. return 0;
  6069. low = pc_getstat(sd,type);
  6070. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6071. if ( low >= max && val > 0 )
  6072. return 0; // Official servers show '0' when max is reached
  6073. high = low + val;
  6074. if ( val < 0 )
  6075. swap(low, high);
  6076. for ( ; low < high; low++ )
  6077. sp += PC_STATUS_POINT_COST(low);
  6078. return sp;
  6079. }
  6080. /**
  6081. * Returns the value the specified stat can be increased by with the current
  6082. * amount of available status points for the current character's class.
  6083. *
  6084. * @param sd The target character.
  6085. * @param type Stat to verify.
  6086. * @return Maximum value the stat could grow by.
  6087. */
  6088. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6089. {
  6090. int base, final_val, status_points, max_param;
  6091. nullpo_ret(sd);
  6092. base = final_val = pc_getstat(sd, type);
  6093. status_points = sd->status.status_point;
  6094. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6095. while (final_val <= max_param && status_points >= 0) {
  6096. status_points -= PC_STATUS_POINT_COST(final_val);
  6097. final_val++;
  6098. }
  6099. final_val--;
  6100. return (final_val > base ? final_val-base : 0);
  6101. }
  6102. /**
  6103. * Raises a stat by the specified amount.
  6104. *
  6105. * Obeys max_parameter limits.
  6106. * Subtracts status points according to the cost of the increased stat points.
  6107. *
  6108. * @param sd The target character.
  6109. * @param type The stat to change (see enum _sp)
  6110. * @param increase The stat increase (strictly positive) amount.
  6111. * @retval true if the stat was increased by any amount.
  6112. * @retval false if there were no changes.
  6113. */
  6114. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6115. {
  6116. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6117. nullpo_ret(sd);
  6118. // check conditions
  6119. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6120. clif_statusupack(sd, type, 0, 0);
  6121. return false;
  6122. }
  6123. // check limits
  6124. current = pc_getstat(sd, type);
  6125. max_increase = pc_maxparameterincrease(sd, type);
  6126. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6127. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6128. clif_statusupack(sd, type, 0, 0);
  6129. return false;
  6130. }
  6131. // check status points
  6132. needed_points = pc_need_status_point(sd, type, increase);
  6133. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6134. clif_statusupack(sd, type, 0, 0);
  6135. return false;
  6136. }
  6137. // set new values
  6138. final_value = pc_setstat(sd, type, current + increase);
  6139. sd->status.status_point -= needed_points;
  6140. status_calc_pc(sd,SCO_NONE);
  6141. // update increase cost indicator
  6142. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6143. // update statpoint count
  6144. clif_updatestatus(sd, SP_STATUSPOINT);
  6145. // update stat value
  6146. clif_statusupack(sd, type, 1, final_value); // required
  6147. if( final_value > 255 )
  6148. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6149. return true;
  6150. }
  6151. /**
  6152. * Raises a stat by the specified amount.
  6153. *
  6154. * Obeys max_parameter limits.
  6155. * Does not subtract status points for the cost of the modified stat points.
  6156. *
  6157. * @param sd The target character.
  6158. * @param type The stat to change (see enum _sp)
  6159. * @param val The stat increase (or decrease) amount.
  6160. * @return the stat increase amount.
  6161. * @retval 0 if no changes were made.
  6162. */
  6163. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6164. {
  6165. int max, need;
  6166. nullpo_ret(sd);
  6167. if( type < SP_STR || type > SP_LUK )
  6168. {
  6169. clif_statusupack(sd,type,0,0);
  6170. return 0;
  6171. }
  6172. need = pc_need_status_point(sd,type,1);
  6173. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6174. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6175. status_calc_pc(sd,SCO_NONE);
  6176. // update increase cost indicator
  6177. if( need != pc_need_status_point(sd,type,1) )
  6178. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6179. // update stat value
  6180. clif_statusupack(sd,type,1,val); // required
  6181. if( val > 255 )
  6182. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6183. return val;
  6184. }
  6185. /*==========================================
  6186. * Update skill_lv for player sd
  6187. * Skill point allocation
  6188. *------------------------------------------*/
  6189. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6190. {
  6191. uint16 idx = skill_get_index(skill_id);
  6192. nullpo_retv(sd);
  6193. if (!idx) {
  6194. if (skill_id)
  6195. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6196. return;
  6197. }
  6198. // Level up guild skill
  6199. if (SKILL_CHK_GUILD(skill_id)) {
  6200. guild_skillup(sd, skill_id);
  6201. return;
  6202. }
  6203. // Level up homunculus skill
  6204. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6205. hom_skillup(sd->hd, skill_id);
  6206. return;
  6207. }
  6208. else {
  6209. if( sd->status.skill_point > 0 &&
  6210. sd->status.skill[idx].id &&
  6211. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6212. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6213. {
  6214. int lv, range, upgradable;
  6215. sd->status.skill[idx].lv++;
  6216. sd->status.skill_point--;
  6217. if( !skill_get_inf(skill_id) )
  6218. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6219. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6220. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6221. else
  6222. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6223. lv = sd->status.skill[idx].lv;
  6224. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6225. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6226. clif_skillup(sd,skill_id,lv,range,upgradable);
  6227. clif_updatestatus(sd,SP_SKILLPOINT);
  6228. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6229. clif_updatestatus(sd,SP_CARTINFO);
  6230. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6231. clif_skillinfoblock(sd);
  6232. }
  6233. //else
  6234. // 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);
  6235. }
  6236. }
  6237. /*==========================================
  6238. * /allskill
  6239. *------------------------------------------*/
  6240. int pc_allskillup(struct map_session_data *sd)
  6241. {
  6242. int i;
  6243. nullpo_ret(sd);
  6244. for (i = 0; i < MAX_SKILL; i++) {
  6245. 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) {
  6246. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6247. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6248. if (sd->status.skill[i].lv == 0)
  6249. sd->status.skill[i].id = 0;
  6250. }
  6251. }
  6252. if (!pc_grant_allskills(sd, true)) {
  6253. uint16 sk_id;
  6254. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6255. int inf2 = 0;
  6256. uint16 sk_idx = 0;
  6257. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6258. continue;
  6259. inf2 = skill_get_inf2(sk_id);
  6260. if (
  6261. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6262. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6263. sk_id == SG_DEVIL
  6264. )
  6265. continue; //Cannot be learned normally.
  6266. sd->status.skill[sk_idx].id = sk_id;
  6267. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6268. }
  6269. }
  6270. status_calc_pc(sd,SCO_NONE);
  6271. //Required because if you could level up all skills previously,
  6272. //the update will not be sent as only the lv variable changes.
  6273. clif_skillinfoblock(sd);
  6274. return 0;
  6275. }
  6276. /*==========================================
  6277. * /resetlvl
  6278. *------------------------------------------*/
  6279. int pc_resetlvl(struct map_session_data* sd,int type)
  6280. {
  6281. int i;
  6282. nullpo_ret(sd);
  6283. if (type != 3) //Also reset skills
  6284. pc_resetskill(sd, 0);
  6285. if(type == 1){
  6286. sd->status.skill_point=0;
  6287. sd->status.base_level=1;
  6288. sd->status.job_level=1;
  6289. sd->status.base_exp=0;
  6290. sd->status.job_exp=0;
  6291. if(sd->sc.option !=0)
  6292. sd->sc.option = 0;
  6293. sd->status.str=1;
  6294. sd->status.agi=1;
  6295. sd->status.vit=1;
  6296. sd->status.int_=1;
  6297. sd->status.dex=1;
  6298. sd->status.luk=1;
  6299. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6300. sd->status.status_point=100; // not 88 [celest]
  6301. // give platinum skills upon changing
  6302. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6303. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6304. }
  6305. }
  6306. if(type == 2){
  6307. sd->status.skill_point=0;
  6308. sd->status.base_level=1;
  6309. sd->status.job_level=1;
  6310. sd->status.base_exp=0;
  6311. sd->status.job_exp=0;
  6312. }
  6313. if(type == 3){
  6314. sd->status.base_level=1;
  6315. sd->status.base_exp=0;
  6316. }
  6317. if(type == 4){
  6318. sd->status.job_level=1;
  6319. sd->status.job_exp=0;
  6320. }
  6321. clif_updatestatus(sd,SP_STATUSPOINT);
  6322. clif_updatestatus(sd,SP_STR);
  6323. clif_updatestatus(sd,SP_AGI);
  6324. clif_updatestatus(sd,SP_VIT);
  6325. clif_updatestatus(sd,SP_INT);
  6326. clif_updatestatus(sd,SP_DEX);
  6327. clif_updatestatus(sd,SP_LUK);
  6328. clif_updatestatus(sd,SP_BASELEVEL);
  6329. clif_updatestatus(sd,SP_JOBLEVEL);
  6330. clif_updatestatus(sd,SP_STATUSPOINT);
  6331. clif_updatestatus(sd,SP_BASEEXP);
  6332. clif_updatestatus(sd,SP_JOBEXP);
  6333. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6334. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6335. clif_updatestatus(sd,SP_SKILLPOINT);
  6336. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6337. clif_updatestatus(sd,SP_UAGI);
  6338. clif_updatestatus(sd,SP_UVIT);
  6339. clif_updatestatus(sd,SP_UINT);
  6340. clif_updatestatus(sd,SP_UDEX);
  6341. clif_updatestatus(sd,SP_ULUK); // End Addition
  6342. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6343. if(sd->equip_index[i] >= 0)
  6344. if(pc_isequip(sd,sd->equip_index[i]))
  6345. pc_unequipitem(sd,sd->equip_index[i],2);
  6346. }
  6347. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6348. party_send_levelup(sd);
  6349. status_calc_pc(sd, SCO_FORCE);
  6350. clif_skillinfoblock(sd);
  6351. return 0;
  6352. }
  6353. /*==========================================
  6354. * /resetstate
  6355. *------------------------------------------*/
  6356. int pc_resetstate(struct map_session_data* sd)
  6357. {
  6358. nullpo_ret(sd);
  6359. if (battle_config.use_statpoint_table)
  6360. { // New statpoint table used here - Dexity
  6361. if (sd->status.base_level > MAX_LEVEL)
  6362. { //statp[] goes out of bounds, can't reset!
  6363. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6364. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6365. return 0;
  6366. }
  6367. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6368. }
  6369. else
  6370. {
  6371. int add=0;
  6372. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6373. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6374. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6375. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6376. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6377. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6378. sd->status.status_point+=add;
  6379. }
  6380. pc_setstat(sd, SP_STR, 1);
  6381. pc_setstat(sd, SP_AGI, 1);
  6382. pc_setstat(sd, SP_VIT, 1);
  6383. pc_setstat(sd, SP_INT, 1);
  6384. pc_setstat(sd, SP_DEX, 1);
  6385. pc_setstat(sd, SP_LUK, 1);
  6386. clif_updatestatus(sd,SP_STR);
  6387. clif_updatestatus(sd,SP_AGI);
  6388. clif_updatestatus(sd,SP_VIT);
  6389. clif_updatestatus(sd,SP_INT);
  6390. clif_updatestatus(sd,SP_DEX);
  6391. clif_updatestatus(sd,SP_LUK);
  6392. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6393. clif_updatestatus(sd,SP_UAGI);
  6394. clif_updatestatus(sd,SP_UVIT);
  6395. clif_updatestatus(sd,SP_UINT);
  6396. clif_updatestatus(sd,SP_UDEX);
  6397. clif_updatestatus(sd,SP_ULUK); // End Addition
  6398. clif_updatestatus(sd,SP_STATUSPOINT);
  6399. if( sd->mission_mobid ) { //bugreport:2200
  6400. sd->mission_mobid = 0;
  6401. sd->mission_count = 0;
  6402. pc_setglobalreg(sd, add_str("TK_MISSION_ID"), 0);
  6403. }
  6404. status_calc_pc(sd, SCO_NONE);
  6405. return 1;
  6406. }
  6407. /*==========================================
  6408. * /resetskill
  6409. * if flag&1, perform block resync and status_calc call.
  6410. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6411. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6412. *------------------------------------------*/
  6413. int pc_resetskill(struct map_session_data* sd, int flag)
  6414. {
  6415. int i, skill_point=0;
  6416. nullpo_ret(sd);
  6417. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6418. return 0;
  6419. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6420. /**
  6421. * 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)
  6422. **/
  6423. if( pc_is_taekwon_ranker(sd) )
  6424. return 0;
  6425. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6426. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6427. i = sd->sc.option;
  6428. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6429. i &= ~OPTION_RIDING;
  6430. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6431. i &= ~OPTION_FALCON;
  6432. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6433. i &= ~OPTION_DRAGON;
  6434. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6435. i &= ~OPTION_WUG;
  6436. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6437. i &= ~OPTION_WUGRIDER;
  6438. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6439. i &= ~OPTION_MADOGEAR;
  6440. #ifndef NEW_CARTS
  6441. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6442. i &= ~OPTION_CART;
  6443. #else
  6444. if( sd->sc.data[SC_PUSH_CART] )
  6445. pc_setcart(sd, 0);
  6446. #endif
  6447. if( i != sd->sc.option )
  6448. pc_setoption(sd, i);
  6449. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6450. hom_vaporize(sd, HOM_ST_ACTIVE);
  6451. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6452. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6453. }
  6454. for( i = 1; i < MAX_SKILL; i++ )
  6455. {
  6456. uint8 lv = sd->status.skill[i].lv;
  6457. int inf2;
  6458. uint16 skill_id = skill_idx2id(i);
  6459. if (lv == 0 || skill_id == 0)
  6460. continue;
  6461. inf2 = skill_get_inf2(skill_id);
  6462. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6463. continue;
  6464. // Don't reset trick dead if not a novice/baby
  6465. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6466. {
  6467. sd->status.skill[i].lv = 0;
  6468. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6469. continue;
  6470. }
  6471. // do not reset basic skill
  6472. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6473. continue;
  6474. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6475. continue;
  6476. if( flag&4 && !skill_ischangesex(skill_id) )
  6477. continue;
  6478. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6479. { //Only handle quest skills in a special way when you can't learn them manually
  6480. if( battle_config.quest_skill_reset && !(flag&2) )
  6481. { //Wipe them
  6482. sd->status.skill[i].lv = 0;
  6483. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6484. }
  6485. continue;
  6486. }
  6487. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6488. skill_point += lv;
  6489. else
  6490. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6491. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6492. if( !(flag&2) )
  6493. {// reset
  6494. sd->status.skill[i].lv = 0;
  6495. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6496. }
  6497. }
  6498. if( flag&2 || !skill_point ) return skill_point;
  6499. sd->status.skill_point += skill_point;
  6500. if (flag&1) {
  6501. clif_updatestatus(sd,SP_SKILLPOINT);
  6502. clif_skillinfoblock(sd);
  6503. status_calc_pc(sd, SCO_FORCE);
  6504. }
  6505. return skill_point;
  6506. }
  6507. /*==========================================
  6508. * /resetfeel [Komurka]
  6509. *------------------------------------------*/
  6510. int pc_resetfeel(struct map_session_data* sd)
  6511. {
  6512. int i;
  6513. nullpo_ret(sd);
  6514. for (i=0; i<MAX_PC_FEELHATE; i++)
  6515. {
  6516. sd->feel_map[i].m = -1;
  6517. sd->feel_map[i].index = 0;
  6518. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6519. }
  6520. return 0;
  6521. }
  6522. int pc_resethate(struct map_session_data* sd)
  6523. {
  6524. int i;
  6525. nullpo_ret(sd);
  6526. for (i=0; i<3; i++)
  6527. {
  6528. sd->hate_mob[i] = -1;
  6529. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6530. }
  6531. return 0;
  6532. }
  6533. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6534. {
  6535. int i, bonus = 0;
  6536. nullpo_ret(sd);
  6537. skill_id = skill_dummy2skill_id(skill_id);
  6538. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  6539. if( i < ARRAYLENGTH(sd->skillatk) )
  6540. bonus = sd->skillatk[i].val;
  6541. return bonus;
  6542. }
  6543. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6544. {
  6545. int i, bonus = 0;
  6546. nullpo_ret(sd);
  6547. skill_id = skill_dummy2skill_id(skill_id);
  6548. ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
  6549. if( i < ARRAYLENGTH(sd->subskill) )
  6550. bonus = sd->subskill[i].val;
  6551. return bonus;
  6552. }
  6553. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6554. int i, bonus = sd->bonus.add_heal_rate;
  6555. skill_id = skill_dummy2skill_id(skill_id);
  6556. if( bonus ) {
  6557. switch( skill_id ) {
  6558. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6559. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6560. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6561. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6562. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6563. }
  6564. }
  6565. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  6566. if( i < ARRAYLENGTH(sd->skillheal) )
  6567. bonus += sd->skillheal[i].val;
  6568. return bonus;
  6569. }
  6570. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6571. int i, bonus = sd->bonus.add_heal2_rate;
  6572. skill_id = skill_dummy2skill_id(skill_id);
  6573. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  6574. if( i < ARRAYLENGTH(sd->skillheal2) )
  6575. bonus += sd->skillheal2[i].val;
  6576. return bonus;
  6577. }
  6578. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6579. {
  6580. if( !pc_isdead(sd) )
  6581. return; // not applicable
  6582. if( sd->bg_id && bg_member_respawn(sd) )
  6583. return; // member revived by battleground
  6584. pc_setstand(sd, true);
  6585. pc_setrestartvalue(sd,3);
  6586. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6587. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6588. }
  6589. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  6590. {
  6591. struct map_session_data *sd = map_id2sd(id);
  6592. if( sd != NULL )
  6593. {
  6594. sd->pvp_point=0;
  6595. pc_respawn(sd,CLR_OUTSIGHT);
  6596. }
  6597. return 0;
  6598. }
  6599. /*==========================================
  6600. * Invoked when a player has received damage
  6601. *------------------------------------------*/
  6602. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6603. {
  6604. if (sp) clif_updatestatus(sd,SP_SP);
  6605. if (hp) clif_updatestatus(sd,SP_HP);
  6606. else return;
  6607. if (!src)
  6608. return;
  6609. if( pc_issit(sd) ) {
  6610. pc_setstand(sd, true);
  6611. skill_sit(sd,0);
  6612. }
  6613. if (sd->progressbar.npc_id)
  6614. clif_progressbar_abort(sd);
  6615. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6616. pet_target_check(sd->pd,src,1);
  6617. if( sd->status.ele_id > 0 )
  6618. elemental_set_target(sd,src);
  6619. sd->canlog_tick = gettick();
  6620. }
  6621. int pc_close_npc_timer(int tid, unsigned int tick, int id, intptr_t data)
  6622. {
  6623. TBL_PC *sd = map_id2sd(id);
  6624. if(sd) pc_close_npc(sd,data);
  6625. return 0;
  6626. }
  6627. /*
  6628. * Method to properly close npc for player and clear anything related
  6629. * @flag == 1 : produce close button
  6630. * @flag == 2 : directly close it
  6631. */
  6632. void pc_close_npc(struct map_session_data *sd,int flag)
  6633. {
  6634. nullpo_retv(sd);
  6635. if (sd->npc_id || sd->npc_shopid) {
  6636. if (sd->state.using_fake_npc) {
  6637. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6638. sd->state.using_fake_npc = 0;
  6639. }
  6640. if (sd->st) {
  6641. if(sd->st->state == RUN){ //wait ending code execution
  6642. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6643. return;
  6644. }
  6645. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6646. sd->st->mes_active = 0;
  6647. }
  6648. sd->state.menu_or_input = 0;
  6649. sd->npc_menu = 0;
  6650. sd->npc_shopid = 0;
  6651. #ifdef SECURE_NPCTIMEOUT
  6652. sd->npc_idle_timer = INVALID_TIMER;
  6653. #endif
  6654. clif_scriptclose(sd,sd->npc_id);
  6655. clif_scriptclear(sd,sd->npc_id); // [Ind/Hercules]
  6656. if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
  6657. script_free_state(sd->st);
  6658. sd->st = NULL;
  6659. sd->npc_id = 0;
  6660. }
  6661. }
  6662. }
  6663. /*==========================================
  6664. * Invoked when a player has negative current hp
  6665. *------------------------------------------*/
  6666. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6667. {
  6668. int i=0,k=0;
  6669. unsigned int tick = gettick();
  6670. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6671. // Super Novices have no kill or die functions attached when saved by their angel
  6672. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6673. unsigned int exp = pc_nextbaseexp(sd);
  6674. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6675. sd->state.snovice_dead_flag = 1;
  6676. pc_setrestartvalue(sd,1);
  6677. status_percent_heal(&sd->bl, 100, 100);
  6678. clif_resurrection(&sd->bl, 1);
  6679. if(battle_config.pc_invincible_time)
  6680. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6681. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6682. if(map_flag_gvg(sd->bl.m))
  6683. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6684. return 0;
  6685. }
  6686. }
  6687. for(k = 0; k < MAX_DEVOTION; k++) {
  6688. if (sd->devotion[k]){
  6689. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6690. if (devsd)
  6691. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6692. sd->devotion[k] = 0;
  6693. }
  6694. }
  6695. if(sd->shadowform_id) { //if we were target of shadowform
  6696. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6697. sd->shadowform_id = 0; //should be remove on status end anyway
  6698. }
  6699. if(sd->status.pet_id > 0 && sd->pd) {
  6700. struct pet_data *pd = sd->pd;
  6701. if( !map[sd->bl.m].flag.noexppenalty ) {
  6702. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  6703. if( pd->pet.intimate < 0 )
  6704. pd->pet.intimate = 0;
  6705. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  6706. }
  6707. if( sd->pd->target_id ) // Unlock all targets...
  6708. pet_unlocktarget(sd->pd);
  6709. }
  6710. if (sd->status.hom_id > 0) {
  6711. if(battle_config.homunculus_auto_vapor && sd->hd)
  6712. hom_vaporize(sd, HOM_ST_ACTIVE);
  6713. }
  6714. if( sd->md )
  6715. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  6716. if( sd->ed )
  6717. elemental_delete(sd->ed);
  6718. // Leave duel if you die [LuzZza]
  6719. if(battle_config.duel_autoleave_when_die) {
  6720. if(sd->duel_group > 0)
  6721. duel_leave(sd->duel_group, sd);
  6722. if(sd->duel_invite > 0)
  6723. duel_reject(sd->duel_invite, sd);
  6724. }
  6725. pc_close_npc(sd,2); //close npc if we were using one
  6726. /* e.g. not killed thru pc_damage */
  6727. if( pc_issit(sd) ) {
  6728. clif_status_load(&sd->bl,SI_SIT,0);
  6729. }
  6730. pc_setdead(sd);
  6731. pc_setglobalreg(sd, add_str("PC_DIE_COUNTER"), sd->die_counter+1);
  6732. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  6733. //Reset menu skills/item skills
  6734. if ((sd->skillitem) != 0)
  6735. sd->skillitem = sd->skillitemlv = 0;
  6736. if ((sd->menuskill_id) != 0)
  6737. sd->menuskill_id = sd->menuskill_val = 0;
  6738. //Reset ticks.
  6739. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  6740. if ( sd->spiritball !=0 )
  6741. pc_delspiritball(sd,sd->spiritball,0);
  6742. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  6743. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  6744. if (src)
  6745. switch (src->type) {
  6746. case BL_MOB:
  6747. {
  6748. struct mob_data *md=(struct mob_data *)src;
  6749. if(md->target_id==sd->bl.id)
  6750. mob_unlocktarget(md,tick);
  6751. if(battle_config.mobs_level_up && md->status.hp &&
  6752. (unsigned int)md->level < pc_maxbaselv(sd) &&
  6753. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  6754. ) { // monster level up [Valaris]
  6755. clif_misceffect(&md->bl,0);
  6756. md->level++;
  6757. status_calc_mob(md, SCO_NONE);
  6758. status_percent_heal(src,10,0);
  6759. if( battle_config.show_mob_info&4 )
  6760. {// update name with new level
  6761. clif_charnameack(0, &md->bl);
  6762. }
  6763. }
  6764. src = battle_get_master(src); // Maybe Player Summon
  6765. }
  6766. break;
  6767. case BL_PET: //Pass on to master...
  6768. case BL_HOM:
  6769. case BL_MER:
  6770. src = battle_get_master(src);
  6771. break;
  6772. }
  6773. if (src && src->type == BL_PC) {
  6774. struct map_session_data *ssd = (struct map_session_data *)src;
  6775. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  6776. npc_script_event(ssd, NPCE_KILLPC);
  6777. if (battle_config.pk_mode&2) {
  6778. ssd->status.manner -= 5;
  6779. if(ssd->status.manner < 0)
  6780. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  6781. #if 0
  6782. // PK/Karma system code (not enabled yet) [celest]
  6783. // originally from Kade Online, so i don't know if any of these is correct ^^;
  6784. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  6785. // karma going down = more 'good' / more honourable.
  6786. // The Karma System way...
  6787. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  6788. sd->status.karma--;
  6789. ssd->status.karma--;
  6790. }
  6791. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  6792. ssd->status.karma++;
  6793. // or the PK System way...
  6794. if (sd->status.karma > 0) // player killed is dishonourable?
  6795. ssd->status.karma--; // honour points earned
  6796. sd->status.karma++; // honour points lost
  6797. // To-do: Receive exp on certain occasions
  6798. #endif
  6799. }
  6800. }
  6801. if(battle_config.bone_drop==2
  6802. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  6803. {
  6804. struct item item_tmp;
  6805. memset(&item_tmp,0,sizeof(item_tmp));
  6806. item_tmp.nameid=ITEMID_SKULL_;
  6807. item_tmp.identify=1;
  6808. item_tmp.card[0]=CARD0_CREATE;
  6809. item_tmp.card[1]=0;
  6810. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  6811. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  6812. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6813. }
  6814. //Remove bonus_script when dead
  6815. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  6816. // changed penalty options, added death by player if pk_mode [Valaris]
  6817. if(battle_config.death_penalty_type
  6818. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  6819. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  6820. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  6821. {
  6822. uint32 base_penalty = 0;
  6823. uint32 job_penalty = 0;
  6824. uint32 zeny_penalty = 0;
  6825. if (pc_isvip(sd)) { // EXP penalty for VIP
  6826. base_penalty = battle_config.vip_exp_penalty_base;
  6827. job_penalty = battle_config.vip_exp_penalty_job;
  6828. zeny_penalty = battle_config.vip_zeny_penalty;
  6829. } else {
  6830. base_penalty = battle_config.death_penalty_base;
  6831. job_penalty = battle_config.death_penalty_job;
  6832. zeny_penalty = battle_config.zeny_penalty;
  6833. }
  6834. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  6835. switch (battle_config.death_penalty_type) {
  6836. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  6837. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  6838. }
  6839. if (base_penalty){ //recheck after altering to speedup
  6840. if (battle_config.pk_mode && src && src->type==BL_PC)
  6841. base_penalty *= 2;
  6842. base_penalty = u32min(sd->status.base_exp, base_penalty);
  6843. }
  6844. }
  6845. else
  6846. base_penalty = 0;
  6847. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  6848. switch (battle_config.death_penalty_type) {
  6849. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  6850. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  6851. }
  6852. if (job_penalty) {
  6853. if (battle_config.pk_mode && src && src->type==BL_PC)
  6854. job_penalty *= 2;
  6855. job_penalty = u32min(sd->status.job_exp, job_penalty);
  6856. }
  6857. }
  6858. else
  6859. job_penalty = 0;
  6860. if (base_penalty || job_penalty)
  6861. pc_lostexp(sd, base_penalty, job_penalty);
  6862. if( zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty) {
  6863. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  6864. if(zeny_penalty)
  6865. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  6866. }
  6867. }
  6868. if(map[sd->bl.m].flag.pvp_nightmaredrop) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  6869. int j;
  6870. for(j=0;j<MAX_DROP_PER_MAP;j++){
  6871. int id = map[sd->bl.m].drop_list[j].drop_id;
  6872. int type = map[sd->bl.m].drop_list[j].drop_type;
  6873. int per = map[sd->bl.m].drop_list[j].drop_per;
  6874. if(id == 0)
  6875. continue;
  6876. if(id == -1){
  6877. int eq_num=0,eq_n[MAX_INVENTORY];
  6878. memset(eq_n,0,sizeof(eq_n));
  6879. for(i=0;i<MAX_INVENTORY;i++) {
  6880. if( (type == 1 && !sd->status.inventory[i].equip)
  6881. || (type == 2 && sd->status.inventory[i].equip)
  6882. || type == 3)
  6883. {
  6884. int l;
  6885. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  6886. if( l < MAX_INVENTORY )
  6887. eq_n[l] = i;
  6888. eq_num++;
  6889. }
  6890. }
  6891. if(eq_num > 0){
  6892. int n = eq_n[rnd()%eq_num];
  6893. if(rnd()%10000 < per) {
  6894. if(sd->status.inventory[n].equip)
  6895. pc_unequipitem(sd,n,3);
  6896. pc_dropitem(sd,n,1);
  6897. }
  6898. }
  6899. }
  6900. else if(id > 0) {
  6901. for(i=0;i<MAX_INVENTORY;i++){
  6902. if(sd->status.inventory[i].nameid == id
  6903. && rnd()%10000 < per
  6904. && ((type == 1 && !sd->status.inventory[i].equip)
  6905. || (type == 2 && sd->status.inventory[i].equip)
  6906. || type == 3) ){
  6907. if(sd->status.inventory[i].equip)
  6908. pc_unequipitem(sd,i,3);
  6909. pc_dropitem(sd,i,1);
  6910. break;
  6911. }
  6912. }
  6913. }
  6914. }
  6915. }
  6916. // pvp
  6917. // disable certain pvp functions on pk_mode [Valaris]
  6918. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank ) {
  6919. sd->pvp_point -= 5;
  6920. sd->pvp_lost++;
  6921. if( src && src->type == BL_PC ) {
  6922. struct map_session_data *ssd = (struct map_session_data *)src;
  6923. ssd->pvp_point++;
  6924. ssd->pvp_won++;
  6925. }
  6926. if( sd->pvp_point < 0 ) {
  6927. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6928. return 1|8;
  6929. }
  6930. }
  6931. //GvG
  6932. if( map_flag_gvg(sd->bl.m) ) {
  6933. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6934. return 1|8;
  6935. }
  6936. else if( sd->bg_id ) {
  6937. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6938. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  6939. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6940. return 1|8;
  6941. }
  6942. }
  6943. //Reset "can log out" tick.
  6944. if( battle_config.prevent_logout )
  6945. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6946. return 1;
  6947. }
  6948. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6949. if(hp) clif_updatestatus(sd,SP_HP);
  6950. if(sp) clif_updatestatus(sd,SP_SP);
  6951. pc_setstand(sd, true);
  6952. if(battle_config.pc_invincible_time > 0)
  6953. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6954. if( sd->state.gmaster_flag ) {
  6955. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  6956. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  6957. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  6958. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  6959. }
  6960. }
  6961. // script
  6962. //
  6963. /*==========================================
  6964. * script reading pc status registry
  6965. *------------------------------------------*/
  6966. int pc_readparam(struct map_session_data* sd,int type)
  6967. {
  6968. int val = 0;
  6969. nullpo_ret(sd);
  6970. switch(type) {
  6971. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6972. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6973. case SP_ZENY: val = sd->status.zeny; break;
  6974. case SP_BASELEVEL: val = sd->status.base_level; break;
  6975. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6976. case SP_CLASS: val = sd->status.class_; break;
  6977. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6978. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6979. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6980. case SP_SEX: val = sd->status.sex; break;
  6981. case SP_WEIGHT: val = sd->weight; break;
  6982. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6983. case SP_BASEEXP: val = sd->status.base_exp; break;
  6984. case SP_JOBEXP: val = sd->status.job_exp; break;
  6985. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6986. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6987. case SP_HP: val = sd->battle_status.hp; break;
  6988. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6989. case SP_SP: val = sd->battle_status.sp; break;
  6990. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6991. case SP_STR: val = sd->status.str; break;
  6992. case SP_AGI: val = sd->status.agi; break;
  6993. case SP_VIT: val = sd->status.vit; break;
  6994. case SP_INT: val = sd->status.int_; break;
  6995. case SP_DEX: val = sd->status.dex; break;
  6996. case SP_LUK: val = sd->status.luk; break;
  6997. case SP_KARMA: val = sd->status.karma; break;
  6998. case SP_MANNER: val = sd->status.manner; break;
  6999. case SP_FAME: val = sd->status.fame; break;
  7000. case SP_KILLERRID: val = sd->killerrid; break;
  7001. case SP_KILLEDRID: val = sd->killedrid; break;
  7002. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7003. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7004. case SP_CHARRENAME: val = sd->status.rename; break;
  7005. case SP_CHARFONT: val = sd->status.font; break;
  7006. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7007. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7008. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7009. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7010. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7011. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7012. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  7013. case SP_DEF1: val = sd->battle_status.def; break;
  7014. case SP_DEF2: val = sd->battle_status.def2; break;
  7015. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7016. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7017. case SP_HIT: val = sd->battle_status.hit; break;
  7018. case SP_FLEE1: val = sd->battle_status.flee; break;
  7019. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7020. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7021. case SP_MAXHPRATE: val = sd->hprate; break;
  7022. case SP_MAXSPRATE: val = sd->sprate; break;
  7023. case SP_SPRATE: val = sd->dsprate; break;
  7024. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7025. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7026. case SP_ASPD_RATE:
  7027. #ifndef RENEWAL_ASPD
  7028. val = sd->battle_status.aspd_rate;
  7029. #else
  7030. val = sd->battle_status.aspd_rate2;
  7031. #endif
  7032. break;
  7033. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7034. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7035. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7036. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7037. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7038. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7039. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7040. case SP_MATK_RATE: val = sd->matk_rate; break;
  7041. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7042. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7043. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7044. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7045. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7046. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7047. case SP_HIT_RATE: val = sd->hit_rate; break;
  7048. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7049. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7050. case SP_DEF_RATE: val = sd->def_rate; break;
  7051. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7052. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7053. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7054. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7055. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7056. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7057. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7058. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7059. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7060. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7061. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7062. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7063. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7064. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7065. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7066. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7067. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7068. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7069. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7070. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7071. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7072. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7073. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7074. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7075. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7076. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7077. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7078. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7079. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7080. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7081. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7082. case SP_DELAYRATE: val = sd->delayrate; break;
  7083. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7084. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7085. case SP_UNSTRIPABLE:
  7086. case SP_UNSTRIPABLE_ARMOR:
  7087. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7088. break;
  7089. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7090. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7091. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7092. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7093. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7094. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7095. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7096. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7097. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7098. case SP_EMATK: val = sd->bonus.ematk; break;
  7099. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7100. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7101. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7102. case SP_CASTRATE:
  7103. case SP_VARCASTRATE:
  7104. #ifdef RENEWAL_CAST
  7105. val = sd->bonus.varcastrate; break;
  7106. #else
  7107. val = sd->castrate; break;
  7108. #endif
  7109. default:
  7110. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7111. return -1;
  7112. }
  7113. return val;
  7114. }
  7115. /*==========================================
  7116. * script set pc status registry
  7117. *------------------------------------------*/
  7118. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7119. {
  7120. nullpo_retr(false,sd);
  7121. switch(type){
  7122. case SP_BASELEVEL:
  7123. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7124. val = pc_maxbaselv(sd);
  7125. if ((unsigned int)val > sd->status.base_level) {
  7126. int i = 0;
  7127. int stat=0;
  7128. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7129. stat += pc_gets_status_point(sd->status.base_level + i);
  7130. sd->status.status_point += stat;
  7131. }
  7132. sd->status.base_level = (unsigned int)val;
  7133. sd->status.base_exp = 0;
  7134. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7135. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7136. clif_updatestatus(sd, SP_STATUSPOINT);
  7137. clif_updatestatus(sd, SP_BASEEXP);
  7138. status_calc_pc(sd, SCO_FORCE);
  7139. if(sd->status.party_id)
  7140. party_send_levelup(sd);
  7141. break;
  7142. case SP_JOBLEVEL:
  7143. if ((unsigned int)val >= sd->status.job_level) {
  7144. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7145. sd->status.skill_point += val - sd->status.job_level;
  7146. clif_updatestatus(sd, SP_SKILLPOINT);
  7147. }
  7148. sd->status.job_level = (unsigned int)val;
  7149. sd->status.job_exp = 0;
  7150. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7151. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7152. clif_updatestatus(sd, SP_JOBEXP);
  7153. status_calc_pc(sd, SCO_FORCE);
  7154. break;
  7155. case SP_SKILLPOINT:
  7156. sd->status.skill_point = val;
  7157. break;
  7158. case SP_STATUSPOINT:
  7159. sd->status.status_point = val;
  7160. break;
  7161. case SP_ZENY:
  7162. if( val < 0 )
  7163. return false;// can't set negative zeny
  7164. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7165. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7166. break;
  7167. case SP_BASEEXP:
  7168. {
  7169. val = cap_value(val, 0, INT_MAX);
  7170. if ((unsigned int)val < sd->status.base_exp) // Lost
  7171. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7172. else // Gained
  7173. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7174. }
  7175. return true;
  7176. case SP_JOBEXP:
  7177. {
  7178. val = cap_value(val, 0, INT_MAX);
  7179. if ((unsigned int)val < sd->status.job_exp) // Lost
  7180. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7181. else // Gained
  7182. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7183. }
  7184. return true;
  7185. case SP_SEX:
  7186. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7187. break;
  7188. case SP_WEIGHT:
  7189. sd->weight = val;
  7190. break;
  7191. case SP_MAXWEIGHT:
  7192. sd->max_weight = val;
  7193. break;
  7194. case SP_HP:
  7195. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7196. break;
  7197. case SP_MAXHP:
  7198. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7199. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7200. {
  7201. sd->battle_status.hp = sd->battle_status.max_hp;
  7202. clif_updatestatus(sd, SP_HP);
  7203. }
  7204. break;
  7205. case SP_SP:
  7206. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7207. break;
  7208. case SP_MAXSP:
  7209. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7210. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7211. {
  7212. sd->battle_status.sp = sd->battle_status.max_sp;
  7213. clif_updatestatus(sd, SP_SP);
  7214. }
  7215. break;
  7216. case SP_STR:
  7217. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7218. break;
  7219. case SP_AGI:
  7220. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7221. break;
  7222. case SP_VIT:
  7223. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7224. break;
  7225. case SP_INT:
  7226. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7227. break;
  7228. case SP_DEX:
  7229. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7230. break;
  7231. case SP_LUK:
  7232. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7233. break;
  7234. case SP_KARMA:
  7235. sd->status.karma = val;
  7236. break;
  7237. case SP_MANNER:
  7238. sd->status.manner = val;
  7239. if( val < 0 )
  7240. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7241. else {
  7242. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7243. clif_manner_message(sd, 5);
  7244. }
  7245. return true; // status_change_start/status_change_end already sends packets warning the client
  7246. case SP_FAME:
  7247. sd->status.fame = val;
  7248. break;
  7249. case SP_KILLERRID:
  7250. sd->killerrid = val;
  7251. return true;
  7252. case SP_KILLEDRID:
  7253. sd->killedrid = val;
  7254. return true;
  7255. case SP_CHARMOVE:
  7256. sd->status.character_moves = val;
  7257. return true;
  7258. case SP_CHARRENAME:
  7259. sd->status.rename = val;
  7260. return true;
  7261. case SP_CHARFONT:
  7262. sd->status.font = val;
  7263. clif_font(sd);
  7264. return true;
  7265. case SP_BANK_VAULT:
  7266. if (val < 0)
  7267. return false;
  7268. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7269. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7270. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7271. return true;
  7272. case SP_ROULETTE_BRONZE:
  7273. sd->roulette_point.bronze = val;
  7274. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7275. return true;
  7276. case SP_ROULETTE_SILVER:
  7277. sd->roulette_point.silver = val;
  7278. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7279. return true;
  7280. case SP_ROULETTE_GOLD:
  7281. sd->roulette_point.gold = val;
  7282. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7283. return true;
  7284. default:
  7285. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7286. return false;
  7287. }
  7288. clif_updatestatus(sd,type);
  7289. return true;
  7290. }
  7291. /*==========================================
  7292. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7293. *------------------------------------------*/
  7294. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7295. {
  7296. if (type) {
  7297. if (hp)
  7298. clif_heal(sd->fd,SP_HP,hp);
  7299. if (sp)
  7300. clif_heal(sd->fd,SP_SP,sp);
  7301. } else {
  7302. if(hp)
  7303. clif_updatestatus(sd,SP_HP);
  7304. if(sp)
  7305. clif_updatestatus(sd,SP_SP);
  7306. }
  7307. return;
  7308. }
  7309. /*==========================================
  7310. * HP/SP Recovery
  7311. * Heal player hp and/or sp linearly.
  7312. * Calculate bonus by status.
  7313. *------------------------------------------*/
  7314. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  7315. {
  7316. int bonus, tmp, penalty = 0;
  7317. if(hp) {
  7318. int i;
  7319. bonus = 100 + (sd->battle_status.vit<<1)
  7320. + pc_checkskill(sd,SM_RECOVERY)*10
  7321. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7322. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7323. if (potion_flag > 1)
  7324. bonus += bonus*(potion_flag-1)*50/100;
  7325. //All item bonuses.
  7326. bonus += sd->bonus.itemhealrate2;
  7327. //Item Group bonuses
  7328. bonus += bonus*pc_get_itemgroup_bonus(sd, itemid)/100;
  7329. //Individual item bonuses.
  7330. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  7331. {
  7332. if (sd->itemhealrate[i].nameid == itemid) {
  7333. bonus += bonus*sd->itemhealrate[i].rate/100;
  7334. break;
  7335. }
  7336. }
  7337. tmp = hp * bonus / 100; // overflow check
  7338. if(bonus != 100 && tmp > hp)
  7339. hp = tmp;
  7340. // Recovery Potion
  7341. if( sd->sc.data[SC_INCHEALRATE] )
  7342. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  7343. // 2014 Halloween Event : Pumpkin Bonus
  7344. if(sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7345. hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100.);
  7346. }
  7347. if(sp) {
  7348. bonus = 100 + (sd->battle_status.int_<<1)
  7349. + pc_checkskill(sd,MG_SRECOVERY)*10
  7350. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  7351. if (potion_flag > 1)
  7352. bonus += bonus*(potion_flag-1)*50/100;
  7353. tmp = sp * bonus / 100;
  7354. if(bonus != 100 && tmp > sp)
  7355. sp = tmp;
  7356. }
  7357. if (sd->sc.count) {
  7358. // Critical Wound and Death Hurt stack
  7359. if (sd->sc.data[SC_CRITICALWOUND])
  7360. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7361. if (sd->sc.data[SC_DEATHHURT])
  7362. penalty += 20;
  7363. if (sd->sc.data[SC_NORECOVER_STATE])
  7364. penalty = 100;
  7365. if (penalty > 0) {
  7366. hp -= hp * penalty / 100;
  7367. sp -= sp * penalty / 100;
  7368. }
  7369. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7370. hp += hp / 2; // 1.5 times
  7371. sp -= sp / 2;
  7372. }
  7373. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7374. hp += hp / 10;
  7375. sp += sp / 10;
  7376. }
  7377. #ifdef RENEWAL
  7378. if (sd->sc.data[SC_EXTREMITYFIST2])
  7379. sp = 0;
  7380. #endif
  7381. if (sd->sc.data[SC_BITESCAR])
  7382. hp = 0;
  7383. }
  7384. return status_heal(&sd->bl, hp, sp, 1);
  7385. }
  7386. /*==========================================
  7387. * HP/SP Recovery
  7388. * Heal player hp nad/or sp by rate
  7389. *------------------------------------------*/
  7390. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7391. {
  7392. nullpo_ret(sd);
  7393. if (hp > 100) hp = 100;
  7394. else if (hp <-100) hp = -100;
  7395. if (sp > 100) sp = 100;
  7396. else if (sp <-100) sp = -100;
  7397. if(hp >= 0 && sp >= 0) //Heal
  7398. return status_percent_heal(&sd->bl, hp, sp);
  7399. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7400. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7401. //Crossed signs
  7402. if(hp) {
  7403. if(hp > 0)
  7404. status_percent_heal(&sd->bl, hp, 0);
  7405. else
  7406. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7407. }
  7408. if(sp) {
  7409. if(sp > 0)
  7410. status_percent_heal(&sd->bl, 0, sp);
  7411. else
  7412. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7413. }
  7414. return 0;
  7415. }
  7416. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7417. {
  7418. struct mob_data *md;
  7419. int flag;
  7420. md = (struct mob_data *)bl;
  7421. nullpo_ret(md);
  7422. flag = va_arg(ap, int);
  7423. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7424. status_kill(&md->bl);
  7425. return 1;
  7426. }
  7427. /**
  7428. * Called when player changes job
  7429. * Rewrote to make it tidider [Celest]
  7430. * @param sd
  7431. * @param job JOB ID. See enum e_job
  7432. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7433. * @return True if success, false if failed
  7434. **/
  7435. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7436. {
  7437. int i, fame_flag = 0;
  7438. int b_class;
  7439. nullpo_retr(false,sd);
  7440. if (job < 0)
  7441. return false;
  7442. //Normalize job.
  7443. b_class = pc_jobid2mapid(job);
  7444. if (b_class == -1)
  7445. return false;
  7446. switch (upper) {
  7447. case 1:
  7448. b_class|= JOBL_UPPER;
  7449. break;
  7450. case 2:
  7451. b_class|= JOBL_BABY;
  7452. break;
  7453. }
  7454. //This will automatically adjust bard/dancer classes to the correct gender
  7455. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7456. job = pc_mapid2jobid(b_class, sd->status.sex);
  7457. if (job == -1)
  7458. return false;
  7459. if ((unsigned short)b_class == sd->class_)
  7460. return false; //Nothing to change.
  7461. // changing from 1st to 2nd job
  7462. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7463. sd->change_level_2nd = sd->status.job_level;
  7464. pc_setglobalreg(sd, add_str("jobchange_level"), sd->change_level_2nd);
  7465. }
  7466. // changing from 2nd to 3rd job
  7467. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7468. sd->change_level_3rd = sd->status.job_level;
  7469. pc_setglobalreg(sd, add_str("jobchange_level_3rd"), sd->change_level_3rd);
  7470. }
  7471. if(sd->cloneskill_idx > 0) {
  7472. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7473. sd->status.skill[sd->cloneskill_idx].id = 0;
  7474. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7475. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7476. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7477. }
  7478. sd->cloneskill_idx = 0;
  7479. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7480. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7481. }
  7482. if(sd->reproduceskill_idx > 0) {
  7483. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7484. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7485. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7486. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7487. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7488. }
  7489. sd->reproduceskill_idx = 0;
  7490. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7491. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7492. }
  7493. // Give or reduce transcendent status points
  7494. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7495. sd->status.status_point += battle_config.transcendent_status_points;
  7496. clif_updatestatus(sd,SP_STATUSPOINT);
  7497. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7498. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7499. // The player already used his bonus points, so we have to reset his status points
  7500. pc_resetstate(sd);
  7501. }
  7502. sd->status.status_point -= battle_config.transcendent_status_points;
  7503. clif_updatestatus(sd,SP_STATUSPOINT);
  7504. }
  7505. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7506. const int class_ = pc_class2idx(sd->status.class_);
  7507. uint16 skill_id;
  7508. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7509. //Remove status specific to your current tree skills.
  7510. enum sc_type sc = status_skill2sc(skill_id);
  7511. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7512. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7513. }
  7514. }
  7515. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7516. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7517. pc_resetfeel(sd);
  7518. }
  7519. // Reset body style to 0 before changing job to avoid
  7520. // errors since not every job has a alternate outfit.
  7521. sd->status.body = 0;
  7522. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7523. sd->status.class_ = job;
  7524. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7525. sd->class_ = (unsigned short)b_class;
  7526. sd->status.job_level=1;
  7527. sd->status.job_exp=0;
  7528. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7529. sd->status.base_level = pc_maxbaselv(sd);
  7530. sd->status.base_exp=0;
  7531. pc_resetstate(sd);
  7532. clif_updatestatus(sd,SP_STATUSPOINT);
  7533. clif_updatestatus(sd,SP_BASELEVEL);
  7534. clif_updatestatus(sd,SP_BASEEXP);
  7535. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7536. }
  7537. clif_updatestatus(sd,SP_JOBLEVEL);
  7538. clif_updatestatus(sd,SP_JOBEXP);
  7539. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7540. for(i=0;i<EQI_MAX;i++) {
  7541. if(sd->equip_index[i] >= 0)
  7542. if(pc_isequip(sd,sd->equip_index[i]))
  7543. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7544. }
  7545. //Change look, if disguised, you need to undisguise
  7546. //to correctly calculate new job sprite without
  7547. if (sd->disguise)
  7548. pc_disguise(sd, 0);
  7549. status_set_viewdata(&sd->bl, job);
  7550. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7551. #if PACKETVER >= 20151001
  7552. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7553. #endif
  7554. if(sd->vd.cloth_color)
  7555. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7556. /*
  7557. if(sd->vd.body_style)
  7558. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7559. */
  7560. //Update skill tree.
  7561. pc_calc_skilltree(sd);
  7562. clif_skillinfoblock(sd);
  7563. if (sd->ed)
  7564. elemental_delete(sd->ed);
  7565. if (sd->state.vending)
  7566. vending_closevending(sd);
  7567. if (sd->state.buyingstore)
  7568. buyingstore_close(sd);
  7569. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7570. //Remove peco/cart/falcon
  7571. i = sd->sc.option;
  7572. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7573. i&=~OPTION_RIDING;
  7574. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7575. i&=~OPTION_FALCON;
  7576. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7577. i&=~OPTION_DRAGON;
  7578. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7579. i&=~OPTION_WUGRIDER;
  7580. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7581. i&=~OPTION_WUG;
  7582. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7583. i&=~OPTION_MADOGEAR;
  7584. #ifndef NEW_CARTS
  7585. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7586. i&=~OPTION_CART;
  7587. #else
  7588. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7589. pc_setcart(sd, 0);
  7590. #endif
  7591. if(i != sd->sc.option)
  7592. pc_setoption(sd, i);
  7593. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7594. hom_vaporize(sd, HOM_ST_ACTIVE);
  7595. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7596. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7597. if(sd->status.manner < 0)
  7598. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7599. status_calc_pc(sd,SCO_FORCE);
  7600. pc_checkallowskill(sd);
  7601. pc_equiplookall(sd);
  7602. pc_show_questinfo(sd);
  7603. //if you were previously famous, not anymore.
  7604. if (fame_flag) {
  7605. chrif_save(sd,0);
  7606. chrif_buildfamelist();
  7607. } else if (sd->status.fame > 0) {
  7608. //It may be that now they are famous?
  7609. switch (sd->class_&MAPID_UPPERMASK) {
  7610. case MAPID_BLACKSMITH:
  7611. case MAPID_ALCHEMIST:
  7612. case MAPID_TAEKWON:
  7613. chrif_save(sd,0);
  7614. chrif_buildfamelist();
  7615. break;
  7616. }
  7617. }
  7618. return true;
  7619. }
  7620. /*==========================================
  7621. * Tell client player sd has change equipement
  7622. *------------------------------------------*/
  7623. void pc_equiplookall(struct map_session_data *sd)
  7624. {
  7625. nullpo_retv(sd);
  7626. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7627. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7628. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7629. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7630. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7631. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7632. }
  7633. /*==========================================
  7634. * Tell client player sd has change look (hair,equip...)
  7635. *------------------------------------------*/
  7636. void pc_changelook(struct map_session_data *sd,int type,int val) {
  7637. nullpo_retv(sd);
  7638. switch(type) {
  7639. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  7640. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  7641. if (sd->status.hair != val) {
  7642. sd->status.hair = val;
  7643. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7644. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7645. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  7646. }
  7647. break;
  7648. case LOOK_WEAPON:
  7649. sd->status.weapon = val;
  7650. break;
  7651. case LOOK_HEAD_BOTTOM:
  7652. sd->status.head_bottom = val;
  7653. break;
  7654. case LOOK_HEAD_TOP:
  7655. sd->status.head_top = val;
  7656. break;
  7657. case LOOK_HEAD_MID:
  7658. sd->status.head_mid = val;
  7659. break;
  7660. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  7661. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  7662. if (sd->status.hair_color != val) {
  7663. sd->status.hair_color = val;
  7664. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  7665. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  7666. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  7667. }
  7668. break;
  7669. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  7670. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  7671. sd->status.clothes_color = val;
  7672. break;
  7673. case LOOK_SHIELD:
  7674. sd->status.shield = val;
  7675. break;
  7676. case LOOK_SHOES:
  7677. break;
  7678. case LOOK_ROBE:
  7679. sd->status.robe = val;
  7680. break;
  7681. case LOOK_BODY2:
  7682. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  7683. sd->status.body = val;
  7684. break;
  7685. }
  7686. clif_changelook(&sd->bl, type, val);
  7687. }
  7688. /*==========================================
  7689. * Give an option (type) to player (sd) and display it to client
  7690. *------------------------------------------*/
  7691. void pc_setoption(struct map_session_data *sd,int type)
  7692. {
  7693. int p_type, new_look=0;
  7694. nullpo_retv(sd);
  7695. p_type = sd->sc.option;
  7696. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  7697. sd->sc.option=type;
  7698. clif_changeoption(&sd->bl);
  7699. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7700. { // Mounting
  7701. clif_status_load(&sd->bl,SI_RIDING,1);
  7702. status_calc_pc(sd,SCO_NONE);
  7703. }
  7704. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  7705. { // Dismount
  7706. clif_status_load(&sd->bl,SI_RIDING,0);
  7707. status_calc_pc(sd,SCO_NONE);
  7708. }
  7709. #ifndef NEW_CARTS
  7710. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  7711. clif_cartlist(sd);
  7712. clif_updatestatus(sd, SP_CARTINFO);
  7713. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7714. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  7715. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  7716. clif_clearcart(sd->fd);
  7717. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7718. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  7719. }
  7720. #endif
  7721. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  7722. clif_status_load(&sd->bl,SI_FALCON,1);
  7723. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  7724. clif_status_load(&sd->bl,SI_FALCON,0);
  7725. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  7726. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  7727. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  7728. status_calc_pc(sd,SCO_NONE);
  7729. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  7730. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  7731. status_calc_pc(sd,SCO_NONE);
  7732. }
  7733. }
  7734. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  7735. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  7736. status_calc_pc(sd,SCO_NONE);
  7737. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  7738. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  7739. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  7740. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  7741. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  7742. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  7743. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  7744. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7745. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  7746. status_calc_pc(sd,SCO_NONE);
  7747. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  7748. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  7749. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  7750. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  7751. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  7752. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  7753. }
  7754. }
  7755. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  7756. new_look = JOB_STAR_GLADIATOR2;
  7757. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  7758. new_look = -1;
  7759. if (sd->disguise || !new_look)
  7760. return; //Disguises break sprite changes
  7761. if (new_look < 0) { //Restore normal look.
  7762. status_set_viewdata(&sd->bl, sd->status.class_);
  7763. new_look = sd->vd.class_;
  7764. }
  7765. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  7766. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  7767. if (sd->vd.cloth_color)
  7768. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7769. if( sd->vd.body_style )
  7770. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7771. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  7772. }
  7773. /*==========================================
  7774. * Give player a cart
  7775. *------------------------------------------*/
  7776. bool pc_setcart(struct map_session_data *sd,int type) {
  7777. #ifndef NEW_CARTS
  7778. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  7779. int option;
  7780. #endif
  7781. nullpo_retr(false,sd);
  7782. if( type < 0 || type > MAX_CARTS )
  7783. return false;// Never trust the values sent by the client! [Skotlex]
  7784. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  7785. return false;// Push cart is required
  7786. #ifdef NEW_CARTS
  7787. switch( type ) {
  7788. case 0:
  7789. if( !sd->sc.data[SC_PUSH_CART] )
  7790. return 0;
  7791. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  7792. clif_clearcart(sd->fd);
  7793. break;
  7794. default:/* everything else is an allowed ID so we can move on */
  7795. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  7796. clif_cartlist(sd);
  7797. clif_updatestatus(sd, SP_CARTINFO);
  7798. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  7799. clif_efst_status_change(&sd->bl, sd->bl.id, AREA, SI_ON_PUSH_CART, 9999, type, 0, 0);
  7800. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  7801. sd->sc.data[SC_PUSH_CART]->val1 = type;
  7802. break;
  7803. }
  7804. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  7805. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  7806. #else
  7807. // Update option
  7808. option = sd->sc.option;
  7809. option &= ~OPTION_CART;// clear cart bits
  7810. option |= cart[type]; // set cart
  7811. pc_setoption(sd, option);
  7812. #endif
  7813. return true;
  7814. }
  7815. /*==========================================
  7816. * Give player a falcon
  7817. *------------------------------------------*/
  7818. void pc_setfalcon(TBL_PC* sd, int flag)
  7819. {
  7820. if( flag ){
  7821. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  7822. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  7823. } else if( pc_isfalcon(sd) ){
  7824. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  7825. }
  7826. }
  7827. /*==========================================
  7828. * Set player riding
  7829. *------------------------------------------*/
  7830. void pc_setriding(TBL_PC* sd, int flag)
  7831. {
  7832. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  7833. return;
  7834. if( flag ){
  7835. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  7836. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  7837. } else if( pc_isriding(sd) ){
  7838. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  7839. }
  7840. }
  7841. /*==========================================
  7842. * Give player a mado
  7843. *------------------------------------------*/
  7844. void pc_setmadogear(TBL_PC* sd, int flag)
  7845. {
  7846. if( flag ){
  7847. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  7848. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  7849. } else if( pc_ismadogear(sd) ){
  7850. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  7851. }
  7852. }
  7853. /*==========================================
  7854. * Check if player can drop an item
  7855. *------------------------------------------*/
  7856. bool pc_candrop(struct map_session_data *sd, struct item *item)
  7857. {
  7858. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  7859. return false;
  7860. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  7861. return false;
  7862. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  7863. }
  7864. /**
  7865. * Determines whether a player can attack based on status changes
  7866. * Why not use status_check_skilluse?
  7867. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  7868. * Even ground-based attacks should be blocked by these statuses
  7869. * Called from unit_attack and unit_attack_timer_sub
  7870. * @retval true Can attack
  7871. **/
  7872. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  7873. nullpo_retr(false, sd);
  7874. if (!&sd->sc)
  7875. return true;
  7876. if( sd->sc.data[SC_BASILICA] ||
  7877. sd->sc.data[SC__SHADOWFORM] ||
  7878. sd->sc.data[SC__MANHOLE] ||
  7879. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  7880. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  7881. sd->sc.data[SC_CRYSTALIZE] ||
  7882. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  7883. sd->sc.data[SC_TRICKDEAD] ||
  7884. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  7885. sd->sc.data[SC_BLADESTOP] ||
  7886. sd->sc.data[SC_DEEPSLEEP] ||
  7887. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  7888. sd->sc.data[SC_KINGS_GRACE] )
  7889. return false;
  7890. return true;
  7891. }
  7892. /*==========================================
  7893. * Read '@type' variables (temporary numeric char reg)
  7894. *------------------------------------------*/
  7895. int pc_readreg(struct map_session_data* sd, int64 reg)
  7896. {
  7897. return i64db_iget(sd->regs.vars, reg);
  7898. }
  7899. /*==========================================
  7900. * Set '@type' variables (temporary numeric char reg)
  7901. *------------------------------------------*/
  7902. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  7903. {
  7904. unsigned int index = script_getvaridx(reg);
  7905. nullpo_retr(false, sd);
  7906. if( val ) {
  7907. i64db_iput(sd->regs.vars, reg, val);
  7908. if( index )
  7909. script_array_update(&sd->regs, reg, false);
  7910. } else {
  7911. i64db_remove(sd->regs.vars, reg);
  7912. if( index )
  7913. script_array_update(&sd->regs, reg, true);
  7914. }
  7915. return true;
  7916. }
  7917. /*==========================================
  7918. * Read '@type$' variables (temporary string char reg)
  7919. *------------------------------------------*/
  7920. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  7921. {
  7922. struct script_reg_str *p = NULL;
  7923. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7924. return p ? p->value : NULL;
  7925. }
  7926. /*==========================================
  7927. * Set '@type$' variables (temporary string char reg)
  7928. *------------------------------------------*/
  7929. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  7930. {
  7931. struct script_reg_str *p = NULL;
  7932. unsigned int index = script_getvaridx(reg);
  7933. DBData prev;
  7934. nullpo_retr(false, sd);
  7935. if( str[0] ) {
  7936. p = ers_alloc(str_reg_ers, struct script_reg_str);
  7937. p->value = aStrdup(str);
  7938. p->flag.type = 1;
  7939. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  7940. p = (struct script_reg_str *)db_data2ptr(&prev);
  7941. if( p->value )
  7942. aFree(p->value);
  7943. ers_free(str_reg_ers, p);
  7944. } else {
  7945. if( index )
  7946. script_array_update(&sd->regs, reg, false);
  7947. }
  7948. } else {
  7949. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  7950. p = (struct script_reg_str *)db_data2ptr(&prev);
  7951. if( p->value )
  7952. aFree(p->value);
  7953. ers_free(str_reg_ers, p);
  7954. if( index )
  7955. script_array_update(&sd->regs, reg, true);
  7956. }
  7957. }
  7958. return true;
  7959. }
  7960. /**
  7961. * Serves the following variable types:
  7962. * - 'type' (permanent numeric char reg)
  7963. * - '#type' (permanent numeric account reg)
  7964. * - '##type' (permanent numeric account reg2)
  7965. **/
  7966. int pc_readregistry(struct map_session_data *sd, int64 reg)
  7967. {
  7968. struct script_reg_num *p = NULL;
  7969. if (!sd->vars_ok) {
  7970. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7971. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7972. //intif->request_registry(sd,type==3?4:type);
  7973. set_eof(sd->fd);
  7974. return 0;
  7975. }
  7976. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  7977. return p ? p->value : 0;
  7978. }
  7979. /**
  7980. * Serves the following variable types:
  7981. * - 'type$' (permanent str char reg)
  7982. * - '#type$' (permanent str account reg)
  7983. * - '##type$' (permanent str account reg2)
  7984. **/
  7985. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  7986. {
  7987. struct script_reg_str *p = NULL;
  7988. if (!sd->vars_ok) {
  7989. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  7990. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7991. //intif->request_registry(sd,type==3?4:type);
  7992. set_eof(sd->fd);
  7993. return NULL;
  7994. }
  7995. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  7996. return p ? p->value : NULL;
  7997. }
  7998. /**
  7999. * Serves the following variable types:
  8000. * - 'type' (permanent numeric char reg)
  8001. * - '#type' (permanent numeric account reg)
  8002. * - '##type' (permanent numeric account reg2)
  8003. **/
  8004. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8005. {
  8006. struct script_reg_num *p = NULL;
  8007. const char *regname = get_str(script_getvarid(reg));
  8008. unsigned int index = script_getvaridx(reg);
  8009. // These should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table!
  8010. switch( regname[0] ) {
  8011. default: //Char reg
  8012. if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
  8013. int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  8014. sd->die_counter = val;
  8015. if( i )
  8016. status_calc_pc(sd,SCO_NONE); // Lost the bonus.
  8017. } else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
  8018. val = cap_value(val, 0, 1999);
  8019. sd->cook_mastery = val;
  8020. }
  8021. break;
  8022. case '#':
  8023. if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
  8024. val = cap_value(val, 0, MAX_ZENY);
  8025. sd->cashPoints = val;
  8026. } else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
  8027. val = cap_value(val, 0, MAX_ZENY);
  8028. sd->kafraPoints = val;
  8029. }
  8030. break;
  8031. }
  8032. if ( !reg_load && !sd->vars_ok ) {
  8033. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8034. return 0;
  8035. }
  8036. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8037. if( val ) {
  8038. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8039. script_array_update(&sd->regs, reg, false);
  8040. p->value = val;
  8041. } else {
  8042. p->value = 0;
  8043. if( index )
  8044. script_array_update(&sd->regs, reg, true);
  8045. }
  8046. if (!reg_load)
  8047. p->flag.update = 1;/* either way, it will require either delete or replace */
  8048. } else if( val ) {
  8049. DBData prev;
  8050. if( index )
  8051. script_array_update(&sd->regs, reg, false);
  8052. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8053. p->value = val;
  8054. if (!reg_load)
  8055. p->flag.update = 1;
  8056. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8057. p = (struct script_reg_num *)db_data2ptr(&prev);
  8058. ers_free(num_reg_ers, p);
  8059. }
  8060. }
  8061. if (!reg_load && p)
  8062. sd->vars_dirty = true;
  8063. return 1;
  8064. }
  8065. /**
  8066. * Serves the following variable types:
  8067. * - 'type$' (permanent str char reg)
  8068. * - '#type$' (permanent str account reg)
  8069. * - '##type$' (permanent str account reg2)
  8070. **/
  8071. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8072. {
  8073. struct script_reg_str *p = NULL;
  8074. const char *regname = get_str(script_getvarid(reg));
  8075. unsigned int index = script_getvaridx(reg);
  8076. if (!reg_load && !sd->vars_ok) {
  8077. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8078. return 0;
  8079. }
  8080. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8081. if( val[0] ) {
  8082. if( p->value )
  8083. aFree(p->value);
  8084. else if ( index ) // an entry that was deleted, so we reset
  8085. script_array_update(&sd->regs, reg, false);
  8086. p->value = aStrdup(val);
  8087. } else {
  8088. p->value = NULL;
  8089. if( index )
  8090. script_array_update(&sd->regs, reg, true);
  8091. }
  8092. if( !reg_load )
  8093. p->flag.update = 1; // either way, it will require either delete or replace
  8094. } else if( val[0] ) {
  8095. DBData prev;
  8096. if( index )
  8097. script_array_update(&sd->regs, reg, false);
  8098. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8099. p->value = aStrdup(val);
  8100. if( !reg_load )
  8101. p->flag.update = 1;
  8102. p->flag.type = 1;
  8103. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8104. p = (struct script_reg_str *)db_data2ptr(&prev);
  8105. if( p->value )
  8106. aFree(p->value);
  8107. ers_free(str_reg_ers, p);
  8108. }
  8109. }
  8110. if( !reg_load && p )
  8111. sd->vars_dirty = true;
  8112. return 1;
  8113. }
  8114. /**
  8115. * Set value of player variable
  8116. * @param sd Player
  8117. * @param reg Variable name
  8118. * @param value
  8119. * @return True if success, false if failed.
  8120. **/
  8121. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8122. char prefix = reg[0];
  8123. nullpo_retr(false, sd);
  8124. if (reg[strlen(reg)-1] == '$') {
  8125. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8126. return false;
  8127. }
  8128. val = cap_value(val, INT_MIN, INT_MAX);
  8129. switch (prefix) {
  8130. case '.':
  8131. case '\'':
  8132. case '$':
  8133. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8134. return false;
  8135. case '@':
  8136. return pc_setreg(sd, add_str(reg), val);
  8137. case '#':
  8138. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8139. default:
  8140. return pc_setglobalreg(sd, add_str(reg), val);
  8141. }
  8142. return false;
  8143. }
  8144. /**
  8145. * Get value of player variable
  8146. * @param sd Player
  8147. * @param reg Variable name
  8148. * @return Variable value or 0 if failed.
  8149. **/
  8150. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8151. char prefix = reg[0];
  8152. nullpo_ret(sd);
  8153. if (reg[strlen(reg)-1] == '$') {
  8154. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8155. return 0;
  8156. }
  8157. switch (prefix) {
  8158. case '.':
  8159. case '\'':
  8160. case '$':
  8161. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8162. return 0;
  8163. case '@':
  8164. return pc_readreg(sd, add_str(reg));
  8165. case '#':
  8166. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8167. default:
  8168. return pc_readglobalreg(sd, add_str(reg));
  8169. }
  8170. return 0;
  8171. }
  8172. /*==========================================
  8173. * Exec eventtimer for player sd (retrieved from map_session (id))
  8174. *------------------------------------------*/
  8175. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  8176. {
  8177. struct map_session_data *sd=map_id2sd(id);
  8178. char *p = (char *)data;
  8179. int i;
  8180. if(sd==NULL)
  8181. return 0;
  8182. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8183. if( i < MAX_EVENTTIMER )
  8184. {
  8185. sd->eventtimer[i] = INVALID_TIMER;
  8186. sd->eventcount--;
  8187. npc_event(sd,p,0);
  8188. }
  8189. else
  8190. ShowError("pc_eventtimer: no such event timer\n");
  8191. if (p) aFree(p);
  8192. return 0;
  8193. }
  8194. /*==========================================
  8195. * Add eventtimer for player sd ?
  8196. *------------------------------------------*/
  8197. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8198. {
  8199. int i;
  8200. nullpo_retr(false,sd);
  8201. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8202. if( i == MAX_EVENTTIMER )
  8203. return false;
  8204. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8205. sd->eventcount++;
  8206. return true;
  8207. }
  8208. /*==========================================
  8209. * Del eventtimer for player sd ?
  8210. *------------------------------------------*/
  8211. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8212. {
  8213. char* p = NULL;
  8214. int i;
  8215. nullpo_retr(false,sd);
  8216. if (sd->eventcount == 0)
  8217. return false;
  8218. // find the named event timer
  8219. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8220. sd->eventtimer[i] != INVALID_TIMER &&
  8221. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8222. strcmp(p, name) == 0
  8223. );
  8224. if( i == MAX_EVENTTIMER )
  8225. return false; // not found
  8226. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8227. sd->eventtimer[i] = INVALID_TIMER;
  8228. if(sd->eventcount > 0)
  8229. sd->eventcount--;
  8230. aFree(p);
  8231. return true;
  8232. }
  8233. /*==========================================
  8234. * Update eventtimer count for player sd
  8235. *------------------------------------------*/
  8236. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8237. {
  8238. int i;
  8239. nullpo_retv(sd);
  8240. for(i=0;i<MAX_EVENTTIMER;i++)
  8241. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8242. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8243. addtick_timer(sd->eventtimer[i],tick);
  8244. break;
  8245. }
  8246. }
  8247. /*==========================================
  8248. * Remove all eventtimer for player sd
  8249. *------------------------------------------*/
  8250. void pc_cleareventtimer(struct map_session_data *sd)
  8251. {
  8252. int i;
  8253. nullpo_retv(sd);
  8254. if (sd->eventcount == 0)
  8255. return;
  8256. for(i=0;i<MAX_EVENTTIMER;i++){
  8257. if( sd->eventtimer[i] != INVALID_TIMER ){
  8258. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8259. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8260. sd->eventtimer[i] = INVALID_TIMER;
  8261. if(sd->eventcount > 0) //avoid looping to max val
  8262. sd->eventcount--;
  8263. if (p) aFree(p);
  8264. }
  8265. }
  8266. }
  8267. /**
  8268. * Called when an item with combo is worn
  8269. * @param *sd
  8270. * @param *data struct item_data
  8271. * @return success numbers of succeed combo
  8272. */
  8273. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8274. uint16 i;
  8275. int success = 0;
  8276. for( i = 0; i < data->combos_count; i++ ) {
  8277. struct itemchk {
  8278. int idx;
  8279. short card[MAX_SLOTS];
  8280. } *combo_idx;
  8281. int idx, j;
  8282. int nb_itemCombo;
  8283. unsigned int pos = 0;
  8284. /* ensure this isn't a duplicate combo */
  8285. if( sd->combos.bonus != NULL ) {
  8286. int x;
  8287. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8288. /* found a match, skip this combo */
  8289. if( x < sd->combos.count )
  8290. continue;
  8291. }
  8292. nb_itemCombo = data->combos[i]->count;
  8293. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8294. continue;
  8295. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8296. for(j=0; j < nb_itemCombo; j++){
  8297. combo_idx[j].idx=-1;
  8298. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8299. }
  8300. for( j = 0; j < nb_itemCombo; j++ ) {
  8301. uint16 id = data->combos[i]->nameid[j], k;
  8302. bool found = false;
  8303. for( k = 0; k < EQI_MAX; k++ ) {
  8304. short index = sd->equip_index[k];
  8305. if( index < 0 )
  8306. continue;
  8307. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8308. continue;
  8309. if (!sd->inventory_data[index] )
  8310. continue;
  8311. if ( itemdb_type(id) != IT_CARD ) {
  8312. if ( sd->inventory_data[index]->nameid != id )
  8313. continue;
  8314. if(j>0){ //check if this item not already used
  8315. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8316. uint8 z;
  8317. for (z = 0; z < nb_itemCombo-1; z++)
  8318. if(combo_idx[z].idx == index) //we already have that index recorded
  8319. do_continue=true;
  8320. if(do_continue)
  8321. continue;
  8322. }
  8323. combo_idx[j].idx = index;
  8324. pos |= sd->status.inventory[index].equip;
  8325. found = true;
  8326. break;
  8327. } else { //Cards
  8328. uint16 z;
  8329. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  8330. continue;
  8331. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  8332. bool do_continue=false;
  8333. if (sd->status.inventory[index].card[z] != id)
  8334. continue;
  8335. if(j>0){
  8336. int c1, c2;
  8337. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8338. if(combo_idx[c1].idx == index){
  8339. for (c2 = 0; c2 < sd->inventory_data[index]->slot; c2++){
  8340. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8341. do_continue = true;
  8342. break;
  8343. }
  8344. }
  8345. }
  8346. }
  8347. }
  8348. if(do_continue)
  8349. continue;
  8350. combo_idx[j].idx = index;
  8351. combo_idx[j].card[z] = id;
  8352. pos |= sd->status.inventory[index].equip;
  8353. found = true;
  8354. break;
  8355. }
  8356. }
  8357. }
  8358. if( !found )
  8359. break;/* we haven't found all the ids for this combo, so we can return */
  8360. }
  8361. aFree(combo_idx);
  8362. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8363. if( j < nb_itemCombo )
  8364. continue;
  8365. /* we got here, means all items in the combo are matching */
  8366. idx = sd->combos.count;
  8367. if( sd->combos.bonus == NULL ) {
  8368. CREATE(sd->combos.bonus, struct script_code *, 1);
  8369. CREATE(sd->combos.id, unsigned short, 1);
  8370. CREATE(sd->combos.pos, unsigned int, 1);
  8371. sd->combos.count = 1;
  8372. } else {
  8373. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8374. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8375. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8376. }
  8377. /* we simply copy the pointer */
  8378. sd->combos.bonus[idx] = data->combos[i]->script;
  8379. /* save this combo's id */
  8380. sd->combos.id[idx] = data->combos[i]->id;
  8381. /* save pos of combo*/
  8382. sd->combos.pos[idx] = pos;
  8383. success++;
  8384. }
  8385. return success;
  8386. }
  8387. /**
  8388. * Called when an item with combo is removed
  8389. * @param *sd
  8390. * @param *data struct item_data
  8391. * @return retval numbers of removed combo
  8392. */
  8393. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8394. int i, retval = 0;
  8395. if( sd->combos.bonus == NULL )
  8396. return 0;/* nothing to do here, player has no combos */
  8397. for( i = 0; i < data->combos_count; i++ ) {
  8398. /* check if this combo exists in this user */
  8399. int x = 0, cursor = 0, j;
  8400. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8401. /* no match, skip this combo */
  8402. if(x >= sd->combos.count)
  8403. continue;
  8404. sd->combos.bonus[x] = NULL;
  8405. sd->combos.id[x] = 0;
  8406. sd->combos.pos[x] = 0;
  8407. retval++;
  8408. /* check if combo requirements still fit */
  8409. if( pc_checkcombo( sd, data ) )
  8410. continue;
  8411. /* move next value to empty slot */
  8412. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8413. if( sd->combos.bonus[j] == NULL )
  8414. continue;
  8415. if( cursor != j ) {
  8416. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8417. sd->combos.id[cursor] = sd->combos.id[j];
  8418. sd->combos.pos[cursor] = sd->combos.pos[j];
  8419. }
  8420. cursor++;
  8421. }
  8422. /* it's empty, we can clear all the memory */
  8423. if( (sd->combos.count = cursor) == 0 ) {
  8424. aFree(sd->combos.bonus);
  8425. aFree(sd->combos.id);
  8426. aFree(sd->combos.pos);
  8427. sd->combos.bonus = NULL;
  8428. sd->combos.id = NULL;
  8429. sd->combos.pos = NULL;
  8430. return retval; /* we also can return at this point for we have no more combos to check */
  8431. }
  8432. }
  8433. return retval;
  8434. }
  8435. /**
  8436. * Load combo data(s) of player
  8437. * @param *sd
  8438. * @return ret numbers of succeed combo
  8439. */
  8440. int pc_load_combo(struct map_session_data *sd) {
  8441. int i, ret = 0;
  8442. for( i = 0; i < EQI_MAX; i++ ) {
  8443. struct item_data *id = NULL;
  8444. short idx = sd->equip_index[i];
  8445. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8446. continue;
  8447. if( id->combos_count )
  8448. ret += pc_checkcombo(sd,id);
  8449. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  8450. struct item_data *data;
  8451. int j;
  8452. for( j = 0; j < id->slot; j++ ) {
  8453. if (!sd->status.inventory[idx].card[j])
  8454. continue;
  8455. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  8456. if( data->combos_count )
  8457. ret += pc_checkcombo(sd,data);
  8458. }
  8459. }
  8460. }
  8461. }
  8462. return ret;
  8463. }
  8464. /*==========================================
  8465. * Equip item on player sd at req_pos from inventory index n
  8466. * return: false - fail; true - success
  8467. *------------------------------------------*/
  8468. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
  8469. {
  8470. int i, pos, flag = 0, iflag;
  8471. struct item_data *id;
  8472. uint8 res = ITEM_EQUIP_ACK_OK;
  8473. nullpo_retr(false,sd);
  8474. if( n < 0 || n >= MAX_INVENTORY ) {
  8475. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8476. return false;
  8477. }
  8478. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8479. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8480. return false;
  8481. }
  8482. if (!(id = sd->inventory_data[n]))
  8483. return false;
  8484. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8485. if(battle_config.battle_log)
  8486. ShowInfo("equip %hu(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  8487. if((res = pc_isequip(sd,n))) {
  8488. clif_equipitemack(sd,n,0,res); // fail
  8489. return false;
  8490. }
  8491. if (!(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  8492. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8493. return false;
  8494. }
  8495. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8496. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8497. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8498. return false;
  8499. }
  8500. if ((sd->class_&MAPID_BASEMASK) == MAPID_GUNSLINGER) {
  8501. /** Failing condition:
  8502. * 1. Always failed to equip ammo if no weapon equipped yet
  8503. * 2. Grenade only can be equipped if weapon is Grenade Launcher
  8504. * 3. Bullet cannot be equipped if the weapon is Grenade Launcher
  8505. * (4. The rest is relying on item job/class restriction).
  8506. **/
  8507. if (id->type == IT_AMMO) {
  8508. int w_idx = sd->equip_index[EQI_HAND_R];
  8509. enum weapon_type w_type = (w_idx != -1) ? (enum weapon_type)sd->inventory_data[w_idx]->look : W_FIST;
  8510. if (w_idx == -1 ||
  8511. (id->look == A_GRENADE && w_type != W_GRENADE) ||
  8512. (id->look != A_GRENADE && w_type == W_GRENADE))
  8513. {
  8514. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8515. return false;
  8516. }
  8517. }
  8518. else if (id->type == IT_WEAPON && id->look >= W_REVOLVER && id->look <= W_GRENADE) {
  8519. int a_idx = sd->equip_index[EQI_AMMO];
  8520. if (a_idx != -1) {
  8521. enum ammo_type a_type = (enum ammo_type)sd->inventory_data[a_idx]->look;
  8522. if ((a_type == A_GRENADE && id->look != W_GRENADE) ||
  8523. (a_type != A_GRENADE && id->look == W_GRENADE))
  8524. {
  8525. clif_equipitemack(sd, 0, 0, ITEM_EQUIP_ACK_FAIL);
  8526. return false;
  8527. }
  8528. }
  8529. }
  8530. }
  8531. if (id->flag.bindOnEquip && !sd->status.inventory[n].bound) {
  8532. sd->status.inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8533. clif_notify_bindOnEquip(sd,n);
  8534. }
  8535. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  8536. pos = req_pos&EQP_ACC;
  8537. if (pos == EQP_ACC) //User specified both slots..
  8538. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8539. }
  8540. if(pos == EQP_SHADOW_ACC) { // Shadow System
  8541. pos = req_pos&EQP_SHADOW_ACC;
  8542. if (pos == EQP_SHADOW_ACC)
  8543. pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8544. }
  8545. if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8546. pos = (req_pos&EQP_ARMS);
  8547. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8548. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8549. }
  8550. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8551. //Update skill-block range database when weapon range changes. [Skotlex]
  8552. i = sd->equip_index[EQI_HAND_R];
  8553. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8554. flag = 1;
  8555. else
  8556. flag = id->range != sd->inventory_data[i]->range;
  8557. }
  8558. for(i=0;i<EQI_MAX;i++) {
  8559. if(pos & equip_bitmask[i]) {
  8560. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8561. pc_unequipitem(sd,sd->equip_index[i],2);
  8562. sd->equip_index[i] = n;
  8563. }
  8564. }
  8565. if(pos==EQP_AMMO) {
  8566. clif_arrowequip(sd,n);
  8567. clif_arrow_fail(sd,3);
  8568. }
  8569. else
  8570. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8571. sd->status.inventory[n].equip=pos;
  8572. if(pos & EQP_HAND_R) {
  8573. if(id)
  8574. sd->weapontype1 = id->look;
  8575. else
  8576. sd->weapontype1 = 0;
  8577. pc_calcweapontype(sd);
  8578. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8579. }
  8580. if(pos & EQP_HAND_L) {
  8581. if(id) {
  8582. if(id->type == IT_WEAPON) {
  8583. sd->status.shield = 0;
  8584. sd->weapontype2 = id->look;
  8585. }
  8586. else
  8587. if(id->type == IT_ARMOR) {
  8588. sd->status.shield = id->look;
  8589. sd->weapontype2 = 0;
  8590. }
  8591. }
  8592. else
  8593. sd->status.shield = sd->weapontype2 = 0;
  8594. pc_calcweapontype(sd);
  8595. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8596. }
  8597. //Added check to prevent sending the same look on multiple slots ->
  8598. //causes client to redraw item on top of itself. (suggested by Lupus)
  8599. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  8600. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  8601. sd->status.head_bottom = id->look;
  8602. else
  8603. sd->status.head_bottom = 0;
  8604. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8605. }
  8606. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  8607. if(id)
  8608. sd->status.head_top = id->look;
  8609. else
  8610. sd->status.head_top = 0;
  8611. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8612. }
  8613. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  8614. if(id && !(pos&EQP_HEAD_TOP))
  8615. sd->status.head_mid = id->look;
  8616. else
  8617. sd->status.head_mid = 0;
  8618. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8619. }
  8620. if(pos & EQP_COSTUME_HEAD_TOP) {
  8621. if(id){
  8622. sd->status.head_top = id->look;
  8623. } else
  8624. sd->status.head_top = 0;
  8625. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8626. }
  8627. if(pos & EQP_COSTUME_HEAD_MID) {
  8628. if(id && !(pos&EQP_HEAD_TOP)){
  8629. sd->status.head_mid = id->look;
  8630. } else
  8631. sd->status.head_mid = 0;
  8632. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8633. }
  8634. if(pos & EQP_COSTUME_HEAD_LOW) {
  8635. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  8636. sd->status.head_bottom = id->look;
  8637. } else
  8638. sd->status.head_bottom = 0;
  8639. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8640. }
  8641. if(pos & EQP_SHOES)
  8642. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8643. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8644. sd->status.robe = id ? id->look : 0;
  8645. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  8646. }
  8647. if(pos & EQP_COSTUME_GARMENT) {
  8648. sd->status.robe = id ? id->look : 0;
  8649. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8650. }
  8651. pc_checkallowskill(sd); //Check if status changes should be halted.
  8652. iflag = sd->npc_item_flag;
  8653. /* check for combos (MUST be before status_calc_pc) */
  8654. if( id->combos_count )
  8655. pc_checkcombo(sd,id);
  8656. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8657. ; //No cards
  8658. else {
  8659. for( i = 0; i < id->slot; i++ ) {
  8660. struct item_data *data;
  8661. if (!sd->status.inventory[n].card[i])
  8662. continue;
  8663. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8664. if( data->combos_count )
  8665. pc_checkcombo(sd,data);
  8666. }
  8667. }
  8668. }
  8669. status_calc_pc(sd,SCO_NONE);
  8670. if (flag) //Update skill data
  8671. clif_skillinfoblock(sd);
  8672. //OnEquip script [Skotlex]
  8673. if (id) {
  8674. //only run the script if item isn't restricted
  8675. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  8676. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8677. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8678. ; //No cards
  8679. else {
  8680. for( i = 0; i < id->slot; i++ ) {
  8681. struct item_data *data;
  8682. if (!sd->status.inventory[n].card[i])
  8683. continue;
  8684. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8685. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  8686. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  8687. }
  8688. }
  8689. }
  8690. }
  8691. sd->npc_item_flag = iflag;
  8692. return true;
  8693. }
  8694. /*==========================================
  8695. * Called when attemting to unequip an item from player
  8696. * type:
  8697. * 0 - only unequip
  8698. * 1 - calculate status after unequipping
  8699. * 2 - force unequip
  8700. * return: false - fail; true - success
  8701. *------------------------------------------*/
  8702. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  8703. int i, iflag;
  8704. bool status_cacl = false;
  8705. nullpo_retr(false,sd);
  8706. if (n < 0 || n >= MAX_INVENTORY) {
  8707. clif_unequipitemack(sd,0,0,0);
  8708. return false;
  8709. }
  8710. if (!sd->status.inventory[n].equip) {
  8711. clif_unequipitemack(sd,n,0,0);
  8712. return false; //Nothing to unequip
  8713. }
  8714. // status change that makes player cannot unequip equipment
  8715. if (!(flag&2) && sd->sc.count &&
  8716. (sd->sc.data[SC_BERSERK] ||
  8717. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8718. sd->sc.data[SC__BLOODYLUST] ||
  8719. sd->sc.data[SC_KYOUGAKU] ||
  8720. (sd->sc.data[SC_PYROCLASTIC] &&
  8721. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  8722. {
  8723. clif_unequipitemack(sd,n,0,0);
  8724. return false;
  8725. }
  8726. if (battle_config.battle_log)
  8727. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  8728. for(i = 0; i < EQI_MAX; i++) {
  8729. if (sd->status.inventory[n].equip & equip_bitmask[i])
  8730. sd->equip_index[i] = -1;
  8731. }
  8732. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  8733. sd->weapontype1 = 0;
  8734. sd->status.weapon = sd->weapontype2;
  8735. pc_calcweapontype(sd);
  8736. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8737. if( !battle_config.dancing_weaponswitch_fix )
  8738. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  8739. }
  8740. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  8741. sd->status.shield = sd->weapontype2 = 0;
  8742. pc_calcweapontype(sd);
  8743. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8744. }
  8745. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  8746. sd->status.head_bottom = 0;
  8747. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8748. }
  8749. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  8750. sd->status.head_top = 0;
  8751. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8752. }
  8753. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  8754. sd->status.head_mid = 0;
  8755. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8756. }
  8757. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  8758. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  8759. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8760. }
  8761. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  8762. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  8763. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8764. }
  8765. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  8766. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  8767. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8768. }
  8769. if(sd->status.inventory[n].equip & EQP_SHOES)
  8770. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8771. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  8772. sd->status.robe = 0;
  8773. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  8774. }
  8775. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  8776. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  8777. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  8778. }
  8779. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  8780. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  8781. // On weapon change (right and left hand)
  8782. if ((sd->status.inventory[n].equip & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  8783. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  8784. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  8785. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  8786. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  8787. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8788. }
  8789. // On armor change
  8790. if (sd->status.inventory[n].equip & EQP_ARMOR) {
  8791. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  8792. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  8793. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  8794. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  8795. }
  8796. // On ammo change
  8797. if (sd->inventory_data[n]->type == IT_AMMO)
  8798. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  8799. if( sd->state.autobonus&sd->status.inventory[n].equip )
  8800. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  8801. sd->status.inventory[n].equip = 0;
  8802. iflag = sd->npc_item_flag;
  8803. /* check for combos (MUST be before status_calc_pc) */
  8804. if ( sd->inventory_data[n] ) {
  8805. if( sd->inventory_data[n]->combos_count ) {
  8806. if( pc_removecombo(sd,sd->inventory_data[n]) )
  8807. status_cacl = true;
  8808. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8809. ; //No cards
  8810. else {
  8811. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8812. struct item_data *data;
  8813. if (!sd->status.inventory[n].card[i])
  8814. continue;
  8815. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8816. if( data->combos_count ) {
  8817. if( pc_removecombo(sd,data) )
  8818. status_cacl = true;
  8819. }
  8820. }
  8821. }
  8822. }
  8823. }
  8824. if(flag&1 || status_cacl) {
  8825. pc_checkallowskill(sd);
  8826. status_calc_pc(sd,SCO_NONE);
  8827. }
  8828. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  8829. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  8830. //OnUnEquip script [Skotlex]
  8831. if (sd->inventory_data[n]) {
  8832. if (sd->inventory_data[n]->unequip_script)
  8833. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8834. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  8835. ; //No cards
  8836. else {
  8837. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  8838. struct item_data *data;
  8839. if (!sd->status.inventory[n].card[i])
  8840. continue;
  8841. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  8842. if( data->unequip_script )
  8843. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  8844. }
  8845. }
  8846. }
  8847. }
  8848. sd->npc_item_flag = iflag;
  8849. return true;
  8850. }
  8851. /*==========================================
  8852. * Checking if player (sd) has an invalid item
  8853. * and is unequiped on map load (item_noequip)
  8854. *------------------------------------------*/
  8855. void pc_checkitem(struct map_session_data *sd) {
  8856. int i, calc_flag = 0;
  8857. struct item it;
  8858. nullpo_retv(sd);
  8859. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  8860. return;
  8861. pc_check_available_item(sd); // Check for invalid(ated) items.
  8862. for( i = 0; i < MAX_INVENTORY; i++ ) {
  8863. it = sd->status.inventory[i];
  8864. if( it.nameid == 0 )
  8865. continue;
  8866. if( !it.equip )
  8867. continue;
  8868. if( it.equip&~pc_equippoint(sd,i) ) {
  8869. pc_unequipitem(sd, i, 2);
  8870. calc_flag = 1;
  8871. continue;
  8872. }
  8873. 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) ) {
  8874. pc_unequipitem(sd, i, 2);
  8875. calc_flag = 1;
  8876. continue;
  8877. }
  8878. }
  8879. if( calc_flag && sd->state.active ) {
  8880. pc_checkallowskill(sd);
  8881. status_calc_pc(sd,SCO_NONE);
  8882. }
  8883. }
  8884. /*==========================================
  8885. * Checks for unavailable items and removes them.
  8886. *------------------------------------------*/
  8887. void pc_check_available_item(struct map_session_data *sd)
  8888. {
  8889. int i;
  8890. unsigned short nameid;
  8891. char output[256];
  8892. nullpo_retv(sd);
  8893. if (battle_config.item_check&0x1) { // Check for invalid(ated) items in inventory.
  8894. for(i = 0; i < MAX_INVENTORY; i++) {
  8895. nameid = sd->status.inventory[i].nameid;
  8896. if (!nameid)
  8897. continue;
  8898. if (!itemdb_available(nameid)) {
  8899. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  8900. clif_displaymessage(sd->fd, output);
  8901. ShowWarning("Removed invalid/disabled item id %hu from inventory (amount=%d, char_id=%d).\n", nameid, sd->status.inventory[i].amount, sd->status.char_id);
  8902. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  8903. continue;
  8904. }
  8905. if (!sd->status.inventory[i].unique_id && !itemdb_isstackable(nameid))
  8906. sd->status.inventory[i].unique_id = pc_generate_unique_id(sd);
  8907. }
  8908. }
  8909. if (battle_config.item_check&0x2) { // Check for invalid(ated) items in cart.
  8910. for(i = 0; i < MAX_CART; i++) {
  8911. nameid = sd->status.cart[i].nameid;
  8912. if (!nameid)
  8913. continue;
  8914. if (!itemdb_available(nameid)) {
  8915. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  8916. clif_displaymessage(sd->fd, output);
  8917. ShowWarning("Removed invalid/disabled item id %hu from cart (amount=%d, char_id=%d).\n", nameid, sd->status.cart[i].amount, sd->status.char_id);
  8918. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  8919. continue;
  8920. }
  8921. if (!sd->status.cart[i].unique_id && !itemdb_isstackable(nameid))
  8922. sd->status.cart[i].unique_id = pc_generate_unique_id(sd);
  8923. }
  8924. }
  8925. if (battle_config.item_check&0x4) { // Check for invalid(ated) items in storage.
  8926. for(i = 0; i < sd->storage_size; i++) {
  8927. nameid = sd->status.storage.items[i].nameid;
  8928. if (!nameid)
  8929. continue;
  8930. if (!itemdb_available(nameid)) {
  8931. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  8932. clif_displaymessage(sd->fd, output);
  8933. ShowWarning("Removed invalid/disabled item id %hu from storage (amount=%d, char_id=%d).\n", nameid, sd->status.storage.items[i].amount, sd->status.char_id);
  8934. storage_delitem(sd, i, sd->status.storage.items[i].amount);
  8935. continue;
  8936. }
  8937. if (!sd->status.storage.items[i].unique_id && !itemdb_isstackable(nameid))
  8938. sd->status.storage.items[i].unique_id = pc_generate_unique_id(sd);
  8939. }
  8940. }
  8941. }
  8942. /*==========================================
  8943. * Update PVP rank for sd1 in cmp to sd2
  8944. *------------------------------------------*/
  8945. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  8946. {
  8947. struct map_session_data *sd1,*sd2;
  8948. sd1=(struct map_session_data *)bl;
  8949. sd2=va_arg(ap,struct map_session_data *);
  8950. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  8951. {// cannot register pvp rank for hidden GMs
  8952. return 0;
  8953. }
  8954. if( sd1->pvp_point > sd2->pvp_point )
  8955. sd2->pvp_rank++;
  8956. return 0;
  8957. }
  8958. /*==========================================
  8959. * Calculate new rank beetween all present players (map_foreachinarea)
  8960. * and display result
  8961. *------------------------------------------*/
  8962. int pc_calc_pvprank(struct map_session_data *sd)
  8963. {
  8964. int old = sd->pvp_rank;
  8965. struct map_data *m = &map[sd->bl.m];
  8966. sd->pvp_rank=1;
  8967. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  8968. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  8969. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  8970. return sd->pvp_rank;
  8971. }
  8972. /*==========================================
  8973. * Calculate next sd ranking calculation from config
  8974. *------------------------------------------*/
  8975. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  8976. {
  8977. struct map_session_data *sd;
  8978. sd=map_id2sd(id);
  8979. if(sd==NULL)
  8980. return 0;
  8981. sd->pvp_timer = INVALID_TIMER;
  8982. if( sd->sc.option&OPTION_INVISIBLE )
  8983. {// do not calculate the pvp rank for a hidden GM
  8984. return 0;
  8985. }
  8986. if( pc_calc_pvprank(sd) > 0 )
  8987. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  8988. return 0;
  8989. }
  8990. /*==========================================
  8991. * Checking if sd is married
  8992. * Return:
  8993. * partner_id = yes
  8994. * 0 = no
  8995. *------------------------------------------*/
  8996. int pc_ismarried(struct map_session_data *sd)
  8997. {
  8998. if(sd == NULL)
  8999. return -1;
  9000. if(sd->status.partner_id > 0)
  9001. return sd->status.partner_id;
  9002. else
  9003. return 0;
  9004. }
  9005. /*==========================================
  9006. * Marry player sd to player dstsd
  9007. * Return:
  9008. * false = fail
  9009. * true = success
  9010. *------------------------------------------*/
  9011. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9012. {
  9013. if(sd == NULL || dstsd == NULL ||
  9014. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9015. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9016. return false;
  9017. sd->status.partner_id = dstsd->status.char_id;
  9018. dstsd->status.partner_id = sd->status.char_id;
  9019. return true;
  9020. }
  9021. /*==========================================
  9022. * Divorce sd from its partner
  9023. * Return:
  9024. * false = fail
  9025. * true = success
  9026. *------------------------------------------*/
  9027. bool pc_divorce(struct map_session_data *sd)
  9028. {
  9029. struct map_session_data *p_sd;
  9030. int i;
  9031. if( sd == NULL || !pc_ismarried(sd) )
  9032. return false;
  9033. if( !sd->status.partner_id )
  9034. return false; // Char is not married
  9035. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9036. { // Lets char server do the divorce
  9037. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9038. return false; // No char server connected
  9039. return true;
  9040. }
  9041. // Both players online, lets do the divorce manually
  9042. sd->status.partner_id = 0;
  9043. p_sd->status.partner_id = 0;
  9044. for( i = 0; i < MAX_INVENTORY; i++ )
  9045. {
  9046. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  9047. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9048. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  9049. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9050. }
  9051. clif_divorced(sd, p_sd->status.name);
  9052. clif_divorced(p_sd, sd->status.name);
  9053. return true;
  9054. }
  9055. /**
  9056. * Get the partner map_session_data of a player
  9057. * @param sd : the husband|wife session
  9058. * @return partner session or NULL
  9059. */
  9060. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9061. if (!sd || !pc_ismarried(sd))
  9062. return NULL;
  9063. return map_charid2sd(sd->status.partner_id);
  9064. }
  9065. /**
  9066. * Get the father map_session_data of a player
  9067. * @param sd : the baby session
  9068. * @return father session or NULL
  9069. */
  9070. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9071. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9072. return NULL;
  9073. return map_charid2sd(sd->status.father);
  9074. }
  9075. /**
  9076. * Get the mother map_session_data of a player
  9077. * @param sd : the baby session
  9078. * @return mother session or NULL
  9079. */
  9080. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9081. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9082. return NULL;
  9083. return map_charid2sd(sd->status.mother);
  9084. }
  9085. /*==========================================
  9086. * Get sd children charid. (Need to be married)
  9087. *------------------------------------------*/
  9088. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9089. {
  9090. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9091. // charid2sd returns NULL if not found
  9092. return NULL;
  9093. return map_charid2sd(sd->status.child);
  9094. }
  9095. /*==========================================
  9096. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9097. *------------------------------------------*/
  9098. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  9099. {
  9100. int hp = 0, sp = 0;
  9101. if( pc_isdead(sd) )
  9102. return;
  9103. if (sd->hp_loss.value) {
  9104. sd->hp_loss.tick += diff_tick;
  9105. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9106. hp += sd->hp_loss.value;
  9107. sd->hp_loss.tick -= sd->hp_loss.rate;
  9108. }
  9109. if(hp >= sd->battle_status.hp)
  9110. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9111. }
  9112. if (sd->sp_loss.value) {
  9113. sd->sp_loss.tick += diff_tick;
  9114. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9115. sp += sd->sp_loss.value;
  9116. sd->sp_loss.tick -= sd->sp_loss.rate;
  9117. }
  9118. }
  9119. if (hp > 0 || sp > 0)
  9120. status_zap(&sd->bl, hp, sp);
  9121. }
  9122. //Character regen. Flag is used to know which types of regen can take place.
  9123. //&1: HP regen
  9124. //&2: SP regen
  9125. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  9126. {
  9127. int hp = 0, sp = 0;
  9128. if (sd->hp_regen.value) {
  9129. sd->hp_regen.tick += diff_tick;
  9130. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9131. hp += sd->hp_regen.value;
  9132. sd->hp_regen.tick -= sd->hp_regen.rate;
  9133. }
  9134. }
  9135. if (sd->sp_regen.value) {
  9136. sd->sp_regen.tick += diff_tick;
  9137. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9138. sp += sd->sp_regen.value;
  9139. sd->sp_regen.tick -= sd->sp_regen.rate;
  9140. }
  9141. }
  9142. if (sd->percent_hp_regen.value) {
  9143. sd->percent_hp_regen.tick += diff_tick;
  9144. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9145. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9146. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9147. }
  9148. }
  9149. if (sd->percent_sp_regen.value) {
  9150. sd->percent_sp_regen.tick += diff_tick;
  9151. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9152. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9153. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9154. }
  9155. }
  9156. if (hp > 0 || sp > 0)
  9157. status_heal(&sd->bl, hp, sp, 0);
  9158. }
  9159. /*==========================================
  9160. * Memo player sd savepoint. (map,x,y)
  9161. *------------------------------------------*/
  9162. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9163. {
  9164. nullpo_retv(sd);
  9165. sd->status.save_point.map = mapindex;
  9166. sd->status.save_point.x = x;
  9167. sd->status.save_point.y = y;
  9168. }
  9169. /*==========================================
  9170. * Save 1 player data at autosave interval
  9171. *------------------------------------------*/
  9172. static int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  9173. {
  9174. int interval;
  9175. struct s_mapiterator* iter;
  9176. struct map_session_data* sd;
  9177. static int last_save_id = 0, save_flag = 0;
  9178. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9179. save_flag = 0;
  9180. else
  9181. save_flag = 1; //Noone was saved, so save first found char.
  9182. iter = mapit_getallusers();
  9183. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9184. {
  9185. if(sd->bl.id == last_save_id && save_flag != 1) {
  9186. save_flag = 1;
  9187. continue;
  9188. }
  9189. if(save_flag != 1) //Not our turn to save yet.
  9190. continue;
  9191. //Save char.
  9192. last_save_id = sd->bl.id;
  9193. save_flag = 2;
  9194. if (pc_isvip(sd)) // Check if we're still VIP
  9195. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9196. chrif_save(sd,0);
  9197. break;
  9198. }
  9199. mapit_free(iter);
  9200. interval = autosave_interval/(map_usercount()+1);
  9201. if(interval < minsave_interval)
  9202. interval = minsave_interval;
  9203. add_timer(gettick()+interval,pc_autosave,0,0);
  9204. return 0;
  9205. }
  9206. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9207. {
  9208. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  9209. { //Night/day state does not match.
  9210. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  9211. sd->state.night = night_flag;
  9212. return 1;
  9213. }
  9214. return 0;
  9215. }
  9216. /*================================================
  9217. * timer to do the day [Yor]
  9218. * data: 0 = called by timer, 1 = gmcommand/script
  9219. *------------------------------------------------*/
  9220. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  9221. {
  9222. char tmp_soutput[1024];
  9223. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9224. return 0;
  9225. if (!night_flag)
  9226. return 0; //Already day.
  9227. night_flag = 0; // 0=day, 1=night [Yor]
  9228. map_foreachpc(pc_daynight_timer_sub);
  9229. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9230. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9231. return 0;
  9232. }
  9233. /*================================================
  9234. * timer to do the night [Yor]
  9235. * data: 0 = called by timer, 1 = gmcommand/script
  9236. *------------------------------------------------*/
  9237. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  9238. {
  9239. char tmp_soutput[1024];
  9240. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9241. return 0;
  9242. if (night_flag)
  9243. return 0; //Already nigth.
  9244. night_flag = 1; // 0=day, 1=night [Yor]
  9245. map_foreachpc(pc_daynight_timer_sub);
  9246. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9247. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9248. return 0;
  9249. }
  9250. /**
  9251. * Attempt to stand up a player
  9252. * @param sd
  9253. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9254. * @return True if success, Fals if failed
  9255. */
  9256. bool pc_setstand(struct map_session_data *sd, bool force){
  9257. nullpo_ret(sd);
  9258. // Cannot stand yet
  9259. // TODO: Move to SCS_NOSTAND [Cydh]
  9260. if (!force && &sd->sc && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9261. return false;
  9262. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9263. clif_status_load(&sd->bl,SI_SIT,0);
  9264. clif_standing(&sd->bl); //Inform area PC is standing
  9265. //Reset sitting tick.
  9266. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9267. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9268. return true;
  9269. }
  9270. /**
  9271. * Mechanic (MADO GEAR)
  9272. **/
  9273. void pc_overheat(struct map_session_data *sd, int val) {
  9274. int heat = val, skill,
  9275. limit[] = { 10, 20, 28, 46, 66 };
  9276. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  9277. return; // already burning
  9278. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  9279. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  9280. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  9281. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  9282. }
  9283. heat = max(0,heat); // Avoid negative HEAT
  9284. if( heat >= limit[skill] )
  9285. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  9286. else
  9287. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  9288. return;
  9289. }
  9290. /**
  9291. * Check if player is autolooting given itemID.
  9292. */
  9293. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9294. {
  9295. uint8 i = 0;
  9296. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9297. return true;
  9298. if (!sd->state.autolooting)
  9299. return false;
  9300. if (sd->state.autolooting)
  9301. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9302. return (i != AUTOLOOTITEM_SIZE);
  9303. }
  9304. /**
  9305. * Checks if player can use @/#command
  9306. * @param sd Player map session data
  9307. * @param command Command name without @/# and params
  9308. * @param type is it atcommand or charcommand
  9309. */
  9310. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9311. {
  9312. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9313. }
  9314. /**
  9315. * Checks if commands used by a player should be logged
  9316. * according to their group setting.
  9317. * @param sd Player map session data
  9318. */
  9319. bool pc_should_log_commands(struct map_session_data *sd)
  9320. {
  9321. return pc_group_should_log_commands(pc_get_group_id(sd));
  9322. }
  9323. /**
  9324. * Spirit Charm expiration timer.
  9325. * @see TimerFunc
  9326. */
  9327. static int pc_spiritcharm_timer(int tid, unsigned int tick, int id, intptr_t data)
  9328. {
  9329. struct map_session_data *sd;
  9330. int i;
  9331. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9332. return 1;
  9333. if (sd->spiritcharm <= 0) {
  9334. 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);
  9335. sd->spiritcharm = 0;
  9336. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9337. return 0;
  9338. }
  9339. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9340. if (i == sd->spiritcharm) {
  9341. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9342. return 0;
  9343. }
  9344. sd->spiritcharm--;
  9345. if (i != sd->spiritcharm)
  9346. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9347. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9348. if (sd->spiritcharm <= 0)
  9349. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9350. clif_spiritcharm(sd);
  9351. return 0;
  9352. }
  9353. /**
  9354. * Adds a spirit charm.
  9355. * @param sd: Target character
  9356. * @param interval: Duration
  9357. * @param max: Maximum amount of charms to add
  9358. * @param type: Charm type (@see spirit_charm_types)
  9359. */
  9360. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9361. {
  9362. int tid, i;
  9363. nullpo_retv(sd);
  9364. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9365. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9366. if (max > MAX_SPIRITCHARM)
  9367. max = MAX_SPIRITCHARM;
  9368. if (sd->spiritcharm < 0)
  9369. sd->spiritcharm = 0;
  9370. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9371. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9372. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9373. sd->spiritcharm--;
  9374. if (sd->spiritcharm != 0)
  9375. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9376. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9377. }
  9378. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9379. 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);
  9380. if (i != sd->spiritcharm)
  9381. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9382. sd->spiritcharm_timer[i] = tid;
  9383. sd->spiritcharm++;
  9384. sd->spiritcharm_type = type;
  9385. clif_spiritcharm(sd);
  9386. }
  9387. /**
  9388. * Removes one or more spirit charms.
  9389. * @param sd: The target character
  9390. * @param count: Amount of charms to remove
  9391. * @param type: Type of charm to remove
  9392. */
  9393. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9394. {
  9395. int i;
  9396. nullpo_retv(sd);
  9397. if (sd->spiritcharm_type != type)
  9398. return;
  9399. if (sd->spiritcharm <= 0) {
  9400. sd->spiritcharm = 0;
  9401. return;
  9402. }
  9403. if (count <= 0)
  9404. return;
  9405. if (count > sd->spiritcharm)
  9406. count = sd->spiritcharm;
  9407. sd->spiritcharm -= count;
  9408. if (count > MAX_SPIRITCHARM)
  9409. count = MAX_SPIRITCHARM;
  9410. for (i = 0; i < count; i++) {
  9411. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9412. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9413. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9414. }
  9415. }
  9416. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9417. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9418. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9419. }
  9420. if (sd->spiritcharm <= 0)
  9421. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9422. clif_spiritcharm(sd);
  9423. }
  9424. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9425. /**
  9426. * Renewal EXP/Item Drop rate modifier based on level penalty
  9427. * @param level_diff: Monster and Player level difference
  9428. * @param mob_class: Monster class
  9429. * @param mode: Monster mode
  9430. * @param type: 1 - EXP, 2 - Item Drop
  9431. * @return Penalty rate
  9432. */
  9433. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9434. {
  9435. int rate = 100;
  9436. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9437. return rate;
  9438. if (level_diff < 0)
  9439. level_diff = MAX_LEVEL + (~level_diff + 1);
  9440. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9441. return rate;
  9442. return 100; // Penalty not found, return default
  9443. }
  9444. #endif
  9445. int pc_split_str(char *str,char **val,int num)
  9446. {
  9447. int i;
  9448. for (i=0; i<num && str; i++){
  9449. val[i] = str;
  9450. str = strchr(str,',');
  9451. if (str && i<num-1) //Do not remove a trailing comma.
  9452. *str++=0;
  9453. }
  9454. return i;
  9455. }
  9456. int pc_split_atoi(char* str, int* val, char sep, int max)
  9457. {
  9458. int i,j;
  9459. for (i=0; i<max; i++) {
  9460. if (!str) break;
  9461. val[i] = atoi(str);
  9462. str = strchr(str,sep);
  9463. if (str)
  9464. *str++=0;
  9465. }
  9466. //Zero up the remaining.
  9467. for(j=i; j < max; j++)
  9468. val[j] = 0;
  9469. return i;
  9470. }
  9471. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9472. {
  9473. static int warning=0;
  9474. int i,j;
  9475. for (i=0; i<max; i++) {
  9476. double f;
  9477. if (!str) break;
  9478. f = atof(str);
  9479. if (f < 0)
  9480. val[i] = 0;
  9481. else if (f > UINT_MAX) {
  9482. val[i] = UINT_MAX;
  9483. if (!warning) {
  9484. warning = 1;
  9485. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9486. }
  9487. } else
  9488. val[i] = (unsigned int)f;
  9489. str = strchr(str,sep);
  9490. if (str)
  9491. *str++=0;
  9492. }
  9493. //Zero up the remaining.
  9494. for(j=i; j < max; j++)
  9495. val[j] = 0;
  9496. return i;
  9497. }
  9498. /*==========================================
  9499. * sub DB reading.
  9500. * Function used to read skill_tree.txt
  9501. *------------------------------------------*/
  9502. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9503. {
  9504. uint32 baselv, joblv, baselv_max, joblv_max;
  9505. uint16 skill_id, skill_lv, skill_lv_max;
  9506. int idx, class_;
  9507. unsigned int i, offset, skill_idx;
  9508. class_ = atoi(fields[0]);
  9509. skill_id = (uint16)atoi(fields[1]);
  9510. skill_lv = (uint16)atoi(fields[2]);
  9511. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9512. baselv = (uint32)atoi(fields[3]);
  9513. joblv = (uint32)atoi(fields[4]);
  9514. offset = 5;
  9515. } else {
  9516. baselv = joblv = 0;
  9517. offset = 3;
  9518. }
  9519. if(!pcdb_checkid(class_))
  9520. {
  9521. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  9522. return false;
  9523. }
  9524. idx = pc_class2idx(class_);
  9525. if (!skill_get_index(skill_id)) {
  9526. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  9527. return false;
  9528. }
  9529. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9530. 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);
  9531. skill_lv = skill_lv_max;
  9532. }
  9533. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  9534. 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);
  9535. baselv = baselv_max;
  9536. }
  9537. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  9538. 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);
  9539. joblv = joblv_max;
  9540. }
  9541. //This is to avoid adding two lines for the same skill. [Skotlex]
  9542. 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 );
  9543. if( skill_idx == MAX_SKILL_TREE )
  9544. {
  9545. 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_);
  9546. return false;
  9547. }
  9548. else if(skill_tree[idx][skill_idx].skill_id)
  9549. {
  9550. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  9551. }
  9552. skill_tree[idx][skill_idx].skill_id = skill_id;
  9553. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  9554. skill_tree[idx][skill_idx].baselv = baselv;
  9555. skill_tree[idx][skill_idx].joblv = joblv;
  9556. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  9557. {
  9558. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  9559. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  9560. if (skill_id == 0)
  9561. continue;
  9562. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  9563. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  9564. return false;
  9565. }
  9566. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  9567. 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);
  9568. skill_lv = skill_lv_max;
  9569. }
  9570. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  9571. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  9572. }
  9573. return true;
  9574. }
  9575. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9576. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  9577. {
  9578. int type, class_, diff;
  9579. type = atoi(fields[0]); //1=experience, 2=item drop
  9580. class_ = atoi(fields[1]);
  9581. diff = atoi(fields[2]);
  9582. if( type != 1 && type != 2 ){
  9583. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  9584. return false;
  9585. }
  9586. if( !CHK_CLASS(class_) ){
  9587. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  9588. return false;
  9589. }
  9590. diff = min(diff, MAX_LEVEL);
  9591. if( diff < 0 )
  9592. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  9593. level_penalty[type][class_][diff] = atoi(fields[3]);
  9594. return true;
  9595. }
  9596. #endif
  9597. /** [Cydh]
  9598. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  9599. * @param level Base level of player
  9600. * @param class_ Job ID @see enum e_job
  9601. * @return base_hp
  9602. */
  9603. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  9604. double base_hp;
  9605. uint16 i, idx = pc_class2idx(class_);
  9606. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  9607. #ifndef RENEWAL
  9608. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  9609. #endif
  9610. for (i = 2; i <= level; i++)
  9611. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  9612. if (class_ == JOB_SUMMONER)
  9613. base_hp += floor((base_hp / 2) + 0.5);
  9614. return (unsigned int)base_hp;
  9615. }
  9616. /** [Playtester]
  9617. * Calculates base sp of player.
  9618. * @param level Base level of player
  9619. * @param class_ Job ID @see enum e_job
  9620. * @return base_sp
  9621. */
  9622. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  9623. double base_sp;
  9624. uint16 idx = pc_class2idx(class_);
  9625. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  9626. switch (class_) {
  9627. case JOB_NINJA:
  9628. if (level >= 10)
  9629. base_sp -= 22;
  9630. else
  9631. base_sp = 11 + 3*level;
  9632. break;
  9633. case JOB_GUNSLINGER:
  9634. if (level > 10)
  9635. base_sp -= 18;
  9636. else
  9637. base_sp = 9 + 3*level;
  9638. break;
  9639. case JOB_SUMMONER:
  9640. base_sp -= floor(base_sp / 2);
  9641. break;
  9642. }
  9643. return (unsigned int)base_sp;
  9644. }
  9645. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  9646. static bool pc_readdb_job1(char* fields[], int columns, int current){
  9647. int idx, class_;
  9648. unsigned int i;
  9649. class_ = atoi(fields[0]);
  9650. if (!pcdb_checkid(class_)) {
  9651. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  9652. return false;
  9653. }
  9654. idx = pc_class2idx(class_);
  9655. job_info[idx].max_weight_base = atoi(fields[1]);
  9656. job_info[idx].hp_factor = atoi(fields[2]);
  9657. job_info[idx].hp_multiplicator = atoi(fields[3]);
  9658. job_info[idx].sp_factor = atoi(fields[4]);
  9659. #ifdef RENEWAL_ASPD
  9660. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  9661. #else
  9662. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  9663. #endif
  9664. {
  9665. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  9666. }
  9667. return true;
  9668. }
  9669. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  9670. static bool pc_readdb_job2(char* fields[], int columns, int current)
  9671. {
  9672. int idx, class_, i;
  9673. class_ = atoi(fields[0]);
  9674. if(!pcdb_checkid(class_))
  9675. {
  9676. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  9677. return false;
  9678. }
  9679. idx = pc_class2idx(class_);
  9680. for(i = 1; i < columns; i++)
  9681. {
  9682. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  9683. }
  9684. return true;
  9685. }
  9686. //Reading job_exp.txt line
  9687. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  9688. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  9689. {
  9690. int idx, i, type;
  9691. int job_id,job_count,jobs[CLASS_COUNT];
  9692. unsigned int ui, maxlvl;
  9693. maxlvl = atoi(fields[0]);
  9694. if(maxlvl > MAX_LEVEL || maxlvl<1){
  9695. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  9696. return false;
  9697. }
  9698. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9699. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  9700. return false;
  9701. }
  9702. type = atoi(fields[2]);
  9703. if(type < 0 || type > 1){
  9704. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  9705. return false;
  9706. }
  9707. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  9708. if (job_count < 1)
  9709. return false;
  9710. job_id = jobs[0];
  9711. if(!pcdb_checkid(job_id)){
  9712. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  9713. return false;
  9714. }
  9715. idx = pc_class2idx(job_id);
  9716. job_info[idx].max_level[type] = maxlvl;
  9717. for(i=0; i<maxlvl; i++)
  9718. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  9719. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  9720. //The reasoning behind the -2 is this... if the max level is 5, then the array
  9721. //should look like this:
  9722. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  9723. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  9724. job_info[idx].max_level[type]--;
  9725. if (job_info[idx].max_level[type] < maxlvl) {
  9726. 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]);
  9727. ShowInfo("Filling the missing values with the last exp entry.\n");
  9728. //Fill the requested values with the last entry.
  9729. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  9730. for (; ui+2 < maxlvl; ui++)
  9731. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  9732. job_info[idx].max_level[type] = maxlvl;
  9733. }
  9734. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9735. for (i = 1; i < job_count; i++) {
  9736. job_id = jobs[i];
  9737. if (!pcdb_checkid(job_id)) {
  9738. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  9739. continue;
  9740. }
  9741. idx = pc_class2idx(job_id);
  9742. 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]));
  9743. job_info[idx].max_level[type] = maxlvl;
  9744. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  9745. }
  9746. return true;
  9747. }
  9748. /**
  9749. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  9750. * startlvl,endlvl,class,type,values...
  9751. */
  9752. #ifdef HP_SP_TABLES
  9753. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  9754. {
  9755. int i, startlvl, endlvl;
  9756. int job_count,jobs[CLASS_COUNT];
  9757. short type;
  9758. startlvl = atoi(fields[0]);
  9759. if(startlvl<1){
  9760. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9761. return false;
  9762. }
  9763. endlvl = atoi(fields[1]);
  9764. if(endlvl<1 || endlvl<startlvl){
  9765. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  9766. return false;
  9767. }
  9768. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  9769. 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);
  9770. return false;
  9771. }
  9772. type = atoi(fields[3]);
  9773. if(type < 0 || type > 1){
  9774. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  9775. return false;
  9776. }
  9777. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  9778. if (job_count < 1)
  9779. return false;
  9780. for (i = 0; i < job_count; i++) {
  9781. int idx, job_id = jobs[i], use_endlvl;
  9782. if (!pcdb_checkid(job_id)) {
  9783. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  9784. return false;
  9785. }
  9786. idx = pc_class2idx(job_id);
  9787. if (startlvl > job_info[idx].max_level[0]) {
  9788. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  9789. return false;
  9790. }
  9791. //Just read until available max level for this job, don't use MAX_LEVEL!
  9792. use_endlvl = endlvl;
  9793. if (use_endlvl > job_info[idx].max_level[0])
  9794. use_endlvl = job_info[idx].max_level[0];
  9795. if(type == 0) { //hp type
  9796. uint16 j;
  9797. for(j = 0; j < use_endlvl; j++) {
  9798. if (atoi(fields[j+4])) {
  9799. uint16 lvl_idx = startlvl-1+j;
  9800. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  9801. //Tells if this HP is lower than previous level (but not for 99->100)
  9802. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  9803. 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",
  9804. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  9805. }
  9806. }
  9807. }
  9808. else { //sp type
  9809. uint16 j;
  9810. for(j = 0; j < use_endlvl; j++) {
  9811. if (atoi(fields[j+4])) {
  9812. uint16 lvl_idx = startlvl-1+j;
  9813. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  9814. //Tells if this SP is lower than previous level (but not for 99->100)
  9815. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  9816. 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",
  9817. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  9818. }
  9819. }
  9820. }
  9821. }
  9822. return true;
  9823. }
  9824. #endif
  9825. /** [Cydh]
  9826. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  9827. */
  9828. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  9829. {
  9830. int idx, class_;
  9831. uint16 str, agi, vit, int_, dex, luk;
  9832. script_get_constant(trim(fields[0]),&class_);
  9833. if ((idx = pc_class2idx(class_)) < 0) {
  9834. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  9835. return false;
  9836. }
  9837. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  9838. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  9839. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  9840. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  9841. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  9842. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  9843. job_info[idx].max_param.str = str;
  9844. job_info[idx].max_param.agi = agi;
  9845. job_info[idx].max_param.vit = vit;
  9846. job_info[idx].max_param.int_ = int_;
  9847. job_info[idx].max_param.dex = dex;
  9848. job_info[idx].max_param.luk = luk;
  9849. return true;
  9850. }
  9851. /**
  9852. * Read job_noenter_map.txt
  9853. **/
  9854. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  9855. int idx, class_ = -1;
  9856. if (ISDIGIT(str[0][0])) {
  9857. class_ = atoi(str[0]);
  9858. } else {
  9859. if (!script_get_constant(str[0], &class_)) {
  9860. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  9861. return false;
  9862. }
  9863. }
  9864. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  9865. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
  9866. return false;
  9867. }
  9868. job_info[idx].noenter_map.zone = atoi(str[1]);
  9869. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  9870. return true;
  9871. }
  9872. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  9873. int i=1;
  9874. char line[24000]; //FIXME this seem too big
  9875. FILE *fp;
  9876. sprintf(line, "%s/statpoint.txt", basedir);
  9877. fp=fopen(line,"r");
  9878. if(fp == NULL){
  9879. if(silent==0) ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  9880. return max(last_s,i);
  9881. } else {
  9882. int entries=0;
  9883. while(fgets(line, sizeof(line), fp))
  9884. {
  9885. int stat;
  9886. trim(line);
  9887. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  9888. continue;
  9889. if ((stat=strtoul(line,NULL,10))<0)
  9890. stat=0;
  9891. if (i > MAX_LEVEL)
  9892. break;
  9893. statp[i]=stat;
  9894. i++;
  9895. entries++;
  9896. }
  9897. fclose(fp);
  9898. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/%s"CL_RESET"'.\n", entries, basedir,"statpoint.txt");
  9899. }
  9900. return max(last_s,i);
  9901. }
  9902. /*==========================================
  9903. * pc DB reading.
  9904. * job_exp.txt - required experience values
  9905. * skill_tree.txt - skill tree for every class
  9906. * attr_fix.txt - elemental adjustment table
  9907. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  9908. * job_db2.txt - job,stats bonuses/lvl
  9909. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  9910. *------------------------------------------*/
  9911. void pc_readdb(void) {
  9912. int i, k, s = 1;
  9913. const char* dbsubpath[] = {
  9914. "",
  9915. "/"DBIMPORT,
  9916. //add other path here
  9917. };
  9918. //reset
  9919. memset(job_info,0,sizeof(job_info)); // job_info table
  9920. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9921. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  9922. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  9923. for( k=1; k < 3; k++ ){ // fill in the blanks
  9924. int j;
  9925. for( j = 0; j < CLASS_ALL; j++ ){
  9926. int tmp = 0;
  9927. for( i = 0; i < MAX_LEVEL*2; i++ ){
  9928. if( i == MAX_LEVEL+1 )
  9929. tmp = level_penalty[k][j][0];// reset
  9930. if( level_penalty[k][j][i] > 0 )
  9931. tmp = level_penalty[k][j][i];
  9932. else
  9933. level_penalty[k][j][i] = tmp;
  9934. }
  9935. }
  9936. }
  9937. #endif
  9938. // reset then read statspoint
  9939. memset(statp,0,sizeof(statp));
  9940. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  9941. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  9942. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  9943. char* dbsubpath1 = (char*)aMalloc(n1+1);
  9944. char* dbsubpath2 = (char*)aMalloc(n2+1);
  9945. if(i==0) {
  9946. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9947. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  9948. }
  9949. else {
  9950. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  9951. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  9952. }
  9953. s = pc_read_statsdb(dbsubpath2,s,i);
  9954. if (i == 0)
  9955. #ifdef RENEWAL_ASPD
  9956. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9957. #else
  9958. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9959. #endif
  9960. else
  9961. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i);
  9962. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i);
  9963. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i); //support till 1000lvl
  9964. #ifdef HP_SP_TABLES
  9965. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i); //Make it support until lvl 500!
  9966. #endif
  9967. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i);
  9968. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i);
  9969. aFree(dbsubpath1);
  9970. aFree(dbsubpath2);
  9971. }
  9972. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  9973. memset(skill_tree, 0, sizeof(skill_tree));
  9974. 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);
  9975. 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);
  9976. // generate the remaining parts of the db if necessary
  9977. k = battle_config.use_statpoint_table; //save setting
  9978. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  9979. statp[0] = 45; // seed value
  9980. for (; s <= MAX_LEVEL; s++)
  9981. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  9982. battle_config.use_statpoint_table = k; //restore setting
  9983. //Checking if all class have their data
  9984. for (i = 0; i < JOB_MAX; i++) {
  9985. int idx;
  9986. uint16 j;
  9987. if (!pcdb_checkid(i))
  9988. continue;
  9989. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST)
  9990. continue; //Classes that do not need exp tables.
  9991. idx = pc_class2idx(i);
  9992. if (!job_info[idx].max_level[0])
  9993. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  9994. if (!job_info[idx].max_level[1])
  9995. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  9996. //Init and checking the empty value of Base HP/SP [Cydh]
  9997. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  9998. if (job_info[idx].base_hp[j] == 0)
  9999. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10000. if (job_info[idx].base_sp[j] == 0)
  10001. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10002. }
  10003. }
  10004. }
  10005. // Read MOTD on startup. [Valaris]
  10006. int pc_read_motd(void)
  10007. {
  10008. FILE* fp;
  10009. // clear old MOTD
  10010. memset(motd_text, 0, sizeof(motd_text));
  10011. // read current MOTD
  10012. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10013. {
  10014. unsigned int entries = 0;
  10015. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  10016. {
  10017. char* buf = motd_text[entries];
  10018. unsigned int lines = 0;
  10019. size_t len;
  10020. lines++;
  10021. if( buf[0] == '/' && buf[1] == '/' )
  10022. continue;
  10023. len = strlen(buf);
  10024. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10025. len--;
  10026. if( len ) {
  10027. char * ptr;
  10028. buf[len] = 0;
  10029. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10030. 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);
  10031. }
  10032. else {// empty line
  10033. buf[0] = ' ';
  10034. buf[1] = 0;
  10035. }
  10036. entries++;
  10037. }
  10038. fclose(fp);
  10039. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  10040. }
  10041. else
  10042. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  10043. return 0;
  10044. }
  10045. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10046. int i,cursor = 0;
  10047. struct item_cd* cd = NULL;
  10048. if( load ) {
  10049. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10050. // no item cooldown is associated with this character
  10051. return;
  10052. }
  10053. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10054. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10055. sd->item_delay[cursor].tick = cd->tick[i];
  10056. sd->item_delay[cursor].nameid = cd->nameid[i];
  10057. cursor++;
  10058. }
  10059. }
  10060. idb_remove(itemcd_db,sd->status.char_id);
  10061. } else {
  10062. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10063. // create a new skill cooldown object for map storage
  10064. CREATE( cd, struct item_cd, 1 );
  10065. idb_put( itemcd_db, sd->status.char_id, cd );
  10066. }
  10067. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10068. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10069. cd->tick[cursor] = sd->item_delay[i].tick;
  10070. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10071. cursor++;
  10072. }
  10073. }
  10074. }
  10075. return;
  10076. }
  10077. /**
  10078. * Add item delay to player's item delay data
  10079. * @param sd Player
  10080. * @param id Item data
  10081. * @param tick Current tick
  10082. * @param n Item index in inventory
  10083. * @return 0: No delay, can consume item.
  10084. * 1: Has delay, cancel consumption.
  10085. **/
  10086. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10087. int i;
  10088. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10089. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10090. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10091. if( i < MAX_ITEMDELAYS ) {
  10092. if( sd->item_delay[i].nameid ) {// found
  10093. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10094. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10095. char e_msg[CHAT_SIZE_MAX];
  10096. if( e_tick > 99 )
  10097. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10098. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10099. else
  10100. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10101. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10102. clif_colormes(sd->fd,color_table[COLOR_YELLOW],e_msg);
  10103. return 1; // Delay has not expired yet
  10104. }
  10105. } else {// not yet used item (all slots are initially empty)
  10106. sd->item_delay[i].nameid = id->nameid;
  10107. }
  10108. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10109. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10110. } else {// should not happen
  10111. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10112. }
  10113. //clean up used delays so we can give room for more
  10114. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10115. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10116. sd->item_delay[i].tick = 0;
  10117. sd->item_delay[i].nameid = 0;
  10118. }
  10119. }
  10120. return 0;
  10121. }
  10122. /**
  10123. * Check if player has delay to reuse item
  10124. * @param sd Player
  10125. * @param id Item data
  10126. * @param tick Current tick
  10127. * @param n Item index in inventory
  10128. * @return 0: No delay, can consume item.
  10129. * 1: Has delay, cancel consumption.
  10130. **/
  10131. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
  10132. struct status_change *sc = NULL;
  10133. nullpo_retr(0, sd);
  10134. nullpo_retr(0, id);
  10135. // Do normal delay assignment
  10136. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10137. return pc_itemcd_add(sd, id, tick, n);
  10138. // Send reply of delay remains
  10139. if (sc->data[id->delay_sc]) {
  10140. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10141. clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
  10142. return 1;
  10143. }
  10144. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10145. return 0;
  10146. }
  10147. /**
  10148. * Clear the dmglog data from player
  10149. * @param sd
  10150. * @param md
  10151. **/
  10152. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10153. {
  10154. uint8 i;
  10155. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10156. if (i < DAMAGELOG_SIZE) {
  10157. md->dmglog[i].id = 0;
  10158. md->dmglog[i].dmg = 0;
  10159. md->dmglog[i].flag = 0;
  10160. }
  10161. }
  10162. /**
  10163. * Add log to player's dmglog
  10164. * @param sd
  10165. * @param id Monster's GID
  10166. **/
  10167. void pc_damage_log_add(struct map_session_data *sd, int id)
  10168. {
  10169. uint8 i = 0;
  10170. if (!sd || !id)
  10171. return;
  10172. //Only store new data, don't need to renew the old one with same id
  10173. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10174. if (i < DAMAGELOG_SIZE_PC)
  10175. return;
  10176. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10177. if (sd->dmglog[i] == 0) {
  10178. sd->dmglog[i] = id;
  10179. return;
  10180. }
  10181. }
  10182. }
  10183. /**
  10184. * Clear dmglog data from player
  10185. * @param sd
  10186. * @param id Monster's id
  10187. **/
  10188. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10189. {
  10190. uint8 i;
  10191. struct mob_data *md = NULL;
  10192. if (!sd)
  10193. return;
  10194. if (!id) {
  10195. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10196. if( !sd->dmglog[i] ) //skip the empty value
  10197. continue;
  10198. if ((md = map_id2md(sd->dmglog[i])))
  10199. pc_clear_log_damage_sub(sd->status.char_id,md);
  10200. sd->dmglog[i] = 0;
  10201. }
  10202. }
  10203. else {
  10204. if ((md = map_id2md(id)))
  10205. pc_clear_log_damage_sub(sd->status.char_id,md);
  10206. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10207. if (i < DAMAGELOG_SIZE_PC)
  10208. sd->dmglog[i] = 0;
  10209. }
  10210. }
  10211. /**
  10212. * Status change data arrived from char-server
  10213. * @param sd: Player data
  10214. */
  10215. void pc_scdata_received(struct map_session_data *sd) {
  10216. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10217. }
  10218. /**
  10219. * Check player account expiration time and rental item expirations
  10220. * @param sd: Player data
  10221. */
  10222. void pc_check_expiration(struct map_session_data *sd) {
  10223. #ifndef ENABLE_SC_SAVING
  10224. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10225. #endif
  10226. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10227. time_t exp_time = sd->expiration_time;
  10228. char tmpstr[1024];
  10229. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10230. clif_wis_message(sd->fd,wisp_server_name,tmpstr,strlen(tmpstr) + 1);
  10231. pc_expire_check(sd);
  10232. }
  10233. }
  10234. int pc_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10235. struct map_session_data *sd = map_id2sd(id);
  10236. if( !sd ) return 0;
  10237. sd->expiration_tid = INVALID_TIMER;
  10238. if( sd->fd )
  10239. clif_authfail_fd(sd->fd,10);
  10240. map_quit(sd);
  10241. return 0;
  10242. }
  10243. int pc_autotrade_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10244. struct map_session_data *sd = map_id2sd(id);
  10245. if (!sd)
  10246. return 0;
  10247. sd->autotrade_tid = INVALID_TIMER;
  10248. if (sd->state.autotrade&2)
  10249. vending_reopen(sd);
  10250. if (sd->state.autotrade&4)
  10251. buyingstore_reopen(sd);
  10252. if (!sd->vender_id && !sd->buyer_id) {
  10253. sd->state.autotrade = 0;
  10254. map_quit(sd);
  10255. }
  10256. return 0;
  10257. }
  10258. /* this timer exists only when a character with a expire timer > 24h is online */
  10259. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10260. int pc_global_expiration_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10261. struct s_mapiterator* iter;
  10262. struct map_session_data* sd;
  10263. iter = mapit_getallusers();
  10264. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10265. if( sd->expiration_time )
  10266. pc_expire_check(sd);
  10267. mapit_free(iter);
  10268. return 0;
  10269. }
  10270. void pc_expire_check(struct map_session_data *sd) {
  10271. /* ongoing timer */
  10272. if( sd->expiration_tid != INVALID_TIMER )
  10273. return;
  10274. /* not within the next 24h, enable the global check */
  10275. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10276. /* global check not running, enable */
  10277. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10278. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10279. return;
  10280. }
  10281. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10282. }
  10283. /**
  10284. * Deposit some money to bank
  10285. * @param sd
  10286. * @param money Amount of money to deposit
  10287. **/
  10288. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10289. unsigned int limit_check = money + sd->bank_vault;
  10290. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10291. return BDA_OVERFLOW;
  10292. } else if ( money > sd->status.zeny ) {
  10293. return BDA_NO_MONEY;
  10294. }
  10295. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10296. return BDA_NO_MONEY;
  10297. sd->bank_vault += money;
  10298. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10299. if( save_settings&CHARSAVE_BANK )
  10300. chrif_save(sd,0);
  10301. return BDA_SUCCESS;
  10302. }
  10303. /**
  10304. * Withdraw money from bank
  10305. * @param sd
  10306. * @param money Amount of money that will be withdrawn
  10307. **/
  10308. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10309. unsigned int limit_check = money + sd->status.zeny;
  10310. if( money <= 0 ) {
  10311. return BWA_UNKNOWN_ERROR;
  10312. } else if ( money > sd->bank_vault ) {
  10313. return BWA_NO_MONEY;
  10314. } else if ( limit_check > MAX_ZENY ) {
  10315. /* no official response for this scenario exists. */
  10316. clif_colormes(sd->fd,color_table[COLOR_RED],msg_txt(sd,1495)); //You can't withdraw that much money
  10317. return BWA_UNKNOWN_ERROR;
  10318. }
  10319. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10320. return BWA_NO_MONEY;
  10321. sd->bank_vault -= money;
  10322. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10323. if( save_settings&CHARSAVE_BANK )
  10324. chrif_save(sd,0);
  10325. return BWA_SUCCESS;
  10326. }
  10327. /**
  10328. * Clear Crimson Marker data from caster
  10329. * @param sd: Player
  10330. **/
  10331. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10332. uint8 i;
  10333. if (!sd)
  10334. return;
  10335. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10336. struct block_list *bl = NULL;
  10337. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10338. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10339. sd->c_marker[i] = 0;
  10340. }
  10341. }
  10342. /**
  10343. * Show version to player
  10344. * @param sd: Player
  10345. **/
  10346. void pc_show_version(struct map_session_data *sd) {
  10347. const char* svn = get_svn_revision();
  10348. char buf[CHAT_SIZE_MAX];
  10349. if( svn[0] != UNKNOWN_VERSION )
  10350. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10351. else {
  10352. const char* git = get_git_hash();
  10353. if( git[0] != UNKNOWN_VERSION )
  10354. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10355. else
  10356. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10357. }
  10358. clif_displaymessage(sd->fd,buf);
  10359. }
  10360. /**
  10361. * Run bonus_script on player
  10362. * @param sd
  10363. * @author [Cydh]
  10364. **/
  10365. void pc_bonus_script(struct map_session_data *sd) {
  10366. int now = gettick();
  10367. struct linkdb_node *node = NULL, *next = NULL;
  10368. if (!sd || !(node = sd->bonus_script.head))
  10369. return;
  10370. while (node) {
  10371. struct s_bonus_script_entry *entry = NULL;
  10372. next = node->next;
  10373. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10374. // Only start timer for new bonus_script
  10375. if (entry->tid == INVALID_TIMER) {
  10376. if (entry->icon != SI_BLANK) // Gives status icon if exist
  10377. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10378. entry->tick += now;
  10379. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10380. }
  10381. if (entry->script)
  10382. run_script(entry->script, 0, sd->bl.id, 0);
  10383. else
  10384. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10385. }
  10386. node = next;
  10387. }
  10388. }
  10389. /**
  10390. * Add bonus_script to player
  10391. * @param sd Player
  10392. * @param script_str Script string
  10393. * @param dur Duration in ms
  10394. * @param icon SI
  10395. * @param flag Flags @see enum e_bonus_script_flags
  10396. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10397. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10398. * @author [Cydh]
  10399. **/
  10400. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum si_type icon, uint16 flag, uint8 type) {
  10401. struct script_code *script = NULL;
  10402. struct linkdb_node *node = NULL;
  10403. struct s_bonus_script_entry *entry = NULL;
  10404. if (!sd)
  10405. return NULL;
  10406. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10407. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10408. return NULL;
  10409. }
  10410. // Duplication checks
  10411. if ((node = sd->bonus_script.head)) {
  10412. while (node) {
  10413. entry = (struct s_bonus_script_entry *)node->data;
  10414. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10415. int newdur = gettick() + dur;
  10416. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10417. settick_timer(entry->tid, newdur);
  10418. script_free_code(script);
  10419. return NULL;
  10420. }
  10421. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10422. break;
  10423. else { // No duplicate bonus
  10424. script_free_code(script);
  10425. return NULL;
  10426. }
  10427. }
  10428. node = node->next;
  10429. }
  10430. }
  10431. CREATE(entry, struct s_bonus_script_entry, 1);
  10432. entry->script_buf = StringBuf_Malloc();
  10433. StringBuf_AppendStr(entry->script_buf, script_str);
  10434. entry->tid = INVALID_TIMER;
  10435. entry->flag = flag;
  10436. entry->icon = icon;
  10437. entry->tick = dur; // Use duration first, on run change to expire time
  10438. entry->type = type;
  10439. entry->script = script;
  10440. sd->bonus_script.count++;
  10441. return entry;
  10442. }
  10443. /**
  10444. * Remove bonus_script data from player
  10445. * @param sd: Target player
  10446. * @param list: Bonus script entry from player
  10447. * @author [Cydh]
  10448. **/
  10449. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10450. if (entry->tid != INVALID_TIMER)
  10451. delete_timer(entry->tid, pc_bonus_script_timer);
  10452. if (entry->script)
  10453. script_free_code(entry->script);
  10454. if (entry->script_buf)
  10455. StringBuf_Free(entry->script_buf);
  10456. if (sd) {
  10457. if (entry->icon != SI_BLANK)
  10458. clif_status_load(&sd->bl, entry->icon, 0);
  10459. if (sd->bonus_script.count > 0)
  10460. sd->bonus_script.count--;
  10461. }
  10462. aFree(entry);
  10463. }
  10464. /**
  10465. * Do final process if no entry left
  10466. * @param sd
  10467. **/
  10468. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10469. if (sd->bonus_script.count == 0) {
  10470. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10471. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10472. linkdb_final(&sd->bonus_script.head);
  10473. }
  10474. }
  10475. /**
  10476. * Timer for bonus_script
  10477. * @param tid
  10478. * @param tick
  10479. * @param id
  10480. * @param data
  10481. * @author [Cydh]
  10482. **/
  10483. int pc_bonus_script_timer(int tid, unsigned int tick, int id, intptr_t data) {
  10484. struct map_session_data *sd;
  10485. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10486. sd = map_id2sd(id);
  10487. if (!sd) {
  10488. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10489. return 0;
  10490. }
  10491. if (tid == INVALID_TIMER)
  10492. return 0;
  10493. if (!sd->bonus_script.head || entry == NULL) {
  10494. ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
  10495. return 0;
  10496. }
  10497. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10498. pc_bonus_script_free_entry(sd, entry);
  10499. pc_bonus_script_check_final(sd);
  10500. status_calc_pc(sd,SCO_NONE);
  10501. return 0;
  10502. }
  10503. /**
  10504. * Check then clear all active timer(s) of bonus_script data from player based on reason
  10505. * @param sd: Target player
  10506. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  10507. * @author [Cydh]
  10508. **/
  10509. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  10510. struct linkdb_node *node = NULL;
  10511. uint16 count = 0;
  10512. if (!sd || !(node = sd->bonus_script.head))
  10513. return;
  10514. while (node) {
  10515. struct linkdb_node *next = node->next;
  10516. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  10517. if (entry && (
  10518. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  10519. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  10520. (flag&entry->flag) || // Matched flag
  10521. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  10522. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  10523. )
  10524. )
  10525. {
  10526. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10527. pc_bonus_script_free_entry(sd, entry);
  10528. count++;
  10529. }
  10530. node = next;
  10531. }
  10532. pc_bonus_script_check_final(sd);
  10533. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  10534. status_calc_pc(sd,SCO_NONE);
  10535. }
  10536. /** [Cydh]
  10537. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  10538. * @param sd: Target player
  10539. */
  10540. void pc_cell_basilica(struct map_session_data *sd) {
  10541. nullpo_retv(sd);
  10542. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  10543. if (&sd->sc && sd->sc.data[SC_BASILICA])
  10544. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  10545. }
  10546. else if (!(&sd->sc) || !sd->sc.data[SC_BASILICA])
  10547. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
  10548. }
  10549. /** [Cydh]
  10550. * Get maximum specified parameter for specified class
  10551. * @param class_: sd->class
  10552. * @param sex: sd->status.sex
  10553. * @param flag: parameter will be checked
  10554. * @return max_param
  10555. */
  10556. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  10557. int idx = -1, class_ = sd->class_;
  10558. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  10559. short max_param = 0;
  10560. switch (param) {
  10561. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  10562. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  10563. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  10564. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  10565. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  10566. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  10567. }
  10568. if (max_param > 0)
  10569. return max_param;
  10570. }
  10571. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  10572. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  10573. ((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)) :
  10574. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  10575. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  10576. }
  10577. /**
  10578. * Get max ASPD for player based on Class
  10579. * @param sd Player
  10580. * @return ASPD
  10581. */
  10582. short pc_maxaspd(struct map_session_data *sd) {
  10583. nullpo_ret(sd);
  10584. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  10585. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  10586. battle_config.max_aspd ));
  10587. }
  10588. /**
  10589. * Calculates total item-group related bonuses for the given item
  10590. * @param sd Player
  10591. * @param nameid Item ID
  10592. * @return Heal rate
  10593. **/
  10594. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  10595. short bonus = 0;
  10596. uint8 i;
  10597. if (!sd->itemgrouphealrate_count)
  10598. return bonus;
  10599. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10600. uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
  10601. struct s_item_group_db *group = NULL;
  10602. if (!group_id || !(group = itemdb_group_exists(group_id)))
  10603. continue;
  10604. for (j = 0; j < group->random[0].data_qty; j++) {
  10605. if (group->random[0].data[j].nameid == nameid) {
  10606. bonus += sd->itemgrouphealrate[i]->rate;
  10607. break;
  10608. }
  10609. }
  10610. }
  10611. return bonus;
  10612. }
  10613. /**
  10614. * Calculates total item-group related bonuses for the given item group
  10615. * @param sd Player
  10616. * @param group_id Item Group ID
  10617. * @return Heal rate
  10618. **/
  10619. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  10620. short bonus = 0;
  10621. uint8 i;
  10622. if (!sd->itemgrouphealrate_count)
  10623. return bonus;
  10624. for (i = 0; i < sd->itemgrouphealrate_count; i++) {
  10625. if (sd->itemgrouphealrate[i]->group_id == group_id)
  10626. return sd->itemgrouphealrate[i]->rate;
  10627. }
  10628. return bonus;
  10629. }
  10630. /**
  10631. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  10632. * @param eqi Item EQI of enum equip_index
  10633. * @param *equip_index Player's equip_index[]
  10634. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  10635. * @return True if item in same inventory index, False if doesn't
  10636. */
  10637. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  10638. if (index < 0 || index >= MAX_INVENTORY)
  10639. return true;
  10640. // Dual weapon checks
  10641. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  10642. return true;
  10643. // Headgear with Mid & Low location
  10644. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  10645. return true;
  10646. // Headgear with Top & Mid or Low location
  10647. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  10648. return true;
  10649. // Headgear with Mid & Low location
  10650. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  10651. return true;
  10652. // Headgear with Top & Mid or Low location
  10653. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  10654. return true;
  10655. return false;
  10656. }
  10657. /**
  10658. * Generate Unique item ID for player
  10659. * @param sd : Player
  10660. * @return A generated Unique item ID
  10661. */
  10662. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  10663. nullpo_ret(sd);
  10664. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  10665. }
  10666. /**
  10667. * Validating skill from player after logged on
  10668. * @param sd
  10669. **/
  10670. void pc_validate_skill(struct map_session_data *sd) {
  10671. if (sd) {
  10672. uint16 i = 0, count = 0;
  10673. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  10674. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  10675. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  10676. for (i = 0; i < MAX_SKILL; i++) {
  10677. uint16 idx = 0;
  10678. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  10679. continue;
  10680. if ((idx = skill_get_index(tmp_skills[i].id))) {
  10681. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  10682. count++;
  10683. }
  10684. else
  10685. 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);
  10686. }
  10687. }
  10688. }
  10689. /**
  10690. * Toggle to remember if the questinfo is displayed yet or not.
  10691. * @param qi_display Display flag
  10692. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  10693. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  10694. **/
  10695. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  10696. if (show) {
  10697. // Check if need to be displayed
  10698. if ((*qi_display) != 1) {
  10699. (*qi_display) = 1;
  10700. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  10701. }
  10702. }
  10703. else {
  10704. // Check if need to be hide
  10705. if ((*qi_display) != 0) {
  10706. (*qi_display) = 0;
  10707. #if PACKETVER >= 20120410
  10708. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  10709. #else
  10710. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  10711. #endif
  10712. }
  10713. }
  10714. }
  10715. /**
  10716. * Show available NPC Quest / Event Icon Check [Kisuka]
  10717. * @param sd Player
  10718. **/
  10719. void pc_show_questinfo(struct map_session_data *sd) {
  10720. #if PACKETVER >= 20090218
  10721. struct questinfo *qi = NULL;
  10722. unsigned short i;
  10723. uint8 j;
  10724. int8 mystate = 0;
  10725. bool failed = false;
  10726. nullpo_retv(sd);
  10727. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10728. return;
  10729. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10730. return;
  10731. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  10732. qi = &map[sd->bl.m].qi_data[i];
  10733. if (!qi)
  10734. continue;
  10735. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  10736. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10737. continue;
  10738. }
  10739. // Level range checks
  10740. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  10741. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10742. continue;
  10743. }
  10744. // Quest requirements
  10745. if (qi->req_count) {
  10746. failed = false;
  10747. for (j = 0; j < qi->req_count; j++) {
  10748. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  10749. mystate = mystate + (mystate < 1);
  10750. if (mystate != qi->req[j].state) {
  10751. failed = true;
  10752. break;
  10753. }
  10754. }
  10755. if (failed) {
  10756. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10757. continue;
  10758. }
  10759. }
  10760. // Job requirements
  10761. if (qi->jobid_count) {
  10762. failed = true;
  10763. for (j = 0; j < qi->jobid_count; j++) {
  10764. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  10765. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10766. failed = false;
  10767. break;
  10768. }
  10769. }
  10770. if (!failed)
  10771. continue;
  10772. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  10773. }
  10774. else {
  10775. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  10776. }
  10777. }
  10778. #endif
  10779. }
  10780. /**
  10781. * Reinit the questinfo for player when changing map
  10782. * @param sd Player
  10783. **/
  10784. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  10785. #if PACKETVER >= 20090218
  10786. nullpo_retv(sd);
  10787. if (sd->qi_display) {
  10788. aFree(sd->qi_display);
  10789. sd->qi_display = NULL;
  10790. }
  10791. sd->qi_count = 0;
  10792. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  10793. return;
  10794. if (!map[sd->bl.m].qi_count || !map[sd->bl.m].qi_data)
  10795. return;
  10796. CREATE(sd->qi_display, bool, (sd->qi_count = map[sd->bl.m].qi_count));
  10797. #endif
  10798. }
  10799. /**
  10800. * Check if a job is allowed to enter the map
  10801. * @param jobid Job ID see enum e_job or sd->status.class_
  10802. * @param m ID -an index- for direct indexing map[] array
  10803. * @return 1 if job is allowed, 0 otherwise
  10804. **/
  10805. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  10806. uint16 idx = 0;
  10807. // Map is other map server.
  10808. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  10809. if (m < 0)
  10810. return true;
  10811. if (m >= MAX_MAP_PER_SERVER || !map[m].cell)
  10812. return false;
  10813. if (!pcdb_checkid(jobid))
  10814. return false;
  10815. idx = pc_class2idx(jobid);
  10816. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  10817. return true;
  10818. if ((!map_flag_vs(m) && job_info[idx].noenter_map.zone&1) || // Normal
  10819. (map[m].flag.pvp && job_info[idx].noenter_map.zone&2) || // PVP
  10820. (map_flag_gvg2(m) && job_info[idx].noenter_map.zone&4) || // GVG
  10821. (map[m].flag.battleground && job_info[idx].noenter_map.zone&8) || // Battleground
  10822. (map[m].flag.restricted && job_info[idx].noenter_map.zone&(8*map[m].zone)) // Zone restriction
  10823. )
  10824. return false;
  10825. return true;
  10826. }
  10827. /*==========================================
  10828. * pc Init/Terminate
  10829. *------------------------------------------*/
  10830. void do_final_pc(void) {
  10831. db_destroy(itemcd_db);
  10832. do_final_pc_groups();
  10833. ers_destroy(pc_sc_display_ers);
  10834. ers_destroy(pc_itemgrouphealrate_ers);
  10835. ers_destroy(num_reg_ers);
  10836. ers_destroy(str_reg_ers);
  10837. }
  10838. void do_init_pc(void) {
  10839. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  10840. pc_readdb();
  10841. pc_read_motd(); // Read MOTD [Valaris]
  10842. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  10843. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  10844. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  10845. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  10846. add_timer_func_list(pc_autosave, "pc_autosave");
  10847. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  10848. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  10849. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  10850. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  10851. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  10852. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  10853. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  10854. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  10855. // 0=day, 1=night [Yor]
  10856. night_flag = battle_config.night_at_start ? 1 : 0;
  10857. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  10858. int day_duration = battle_config.day_duration;
  10859. int night_duration = battle_config.night_duration;
  10860. // add night/day timer [Yor]
  10861. add_timer_func_list(map_day_timer, "map_day_timer");
  10862. add_timer_func_list(map_night_timer, "map_night_timer");
  10863. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  10864. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  10865. }
  10866. do_init_pc_groups();
  10867. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  10868. pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
  10869. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10870. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
  10871. ers_chunk_size(pc_sc_display_ers, 150);
  10872. ers_chunk_size(num_reg_ers, 300);
  10873. ers_chunk_size(str_reg_ers, 50);
  10874. }