pc.c 290 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899
  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 "battle.h" // battle_config
  16. #include "battleground.h"
  17. #include "chrif.h"
  18. #include "clif.h"
  19. #include "date.h" // is_day_of_*()
  20. #include "duel.h"
  21. #include "intif.h"
  22. #include "itemdb.h"
  23. #include "log.h"
  24. #include "mail.h"
  25. #include "map.h"
  26. #include "path.h"
  27. #include "homunculus.h"
  28. #include "instance.h"
  29. #include "mercenary.h"
  30. #include "elemental.h"
  31. #include "npc.h" // fake_nd
  32. #include "pet.h" // pet_unlocktarget()
  33. #include "party.h" // party_search()
  34. #include "guild.h" // guild_search(), guild_request_info()
  35. #include "script.h" // script_config
  36. #include "skill.h"
  37. #include "status.h" // struct status_data
  38. #include "pc.h"
  39. #include "pc_groups.h"
  40. #include "quest.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  46. static unsigned int exp_table[CLASS_COUNT][2][MAX_LEVEL];
  47. static unsigned int max_level[CLASS_COUNT][2];
  48. static unsigned int statp[MAX_LEVEL+1];
  49. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  50. static unsigned int level_penalty[3][RC_MAX][MAX_LEVEL*2+1];
  51. #endif
  52. // h-files are for declarations, not for implementations... [Shinomori]
  53. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  54. // timer for night.day implementation
  55. int day_timer_tid;
  56. int night_timer_tid;
  57. struct fame_list smith_fame_list[MAX_FAME_LIST];
  58. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  59. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  60. static unsigned short equip_pos[EQI_MAX]={EQP_ACC_L,EQP_ACC_R,EQP_SHOES,EQP_GARMENT,EQP_HEAD_LOW,EQP_HEAD_MID,EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_GARMENT,EQP_AMMO};
  61. #define MOTD_LINE_SIZE 128
  62. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  63. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  64. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  65. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  66. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  67. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  68. };
  69. /**
  70. * Item Cool Down Delay Saving
  71. * Struct item_cd is not a member of struct map_session_data
  72. * to keep cooldowns in memory between player log-ins.
  73. * All cooldowns are reset when server is restarted.
  74. **/
  75. DBMap* itemcd_db = NULL; // char_id -> struct skill_cd
  76. struct item_cd {
  77. unsigned int tick[MAX_ITEMDELAYS];//tick
  78. short nameid[MAX_ITEMDELAYS];//skill id
  79. };
  80. //Converts a class to its array index for CLASS_COUNT defined arrays.
  81. //Note that it does not do a validity check for speed purposes, where parsing
  82. //player input make sure to use a pcdb_checkid first!
  83. int pc_class2idx(int class_) {
  84. if (class_ >= JOB_NOVICE_HIGH)
  85. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  86. return class_;
  87. }
  88. inline int pc_get_group_id(struct map_session_data *sd) {
  89. return sd->group_id;
  90. }
  91. inline int pc_get_group_level(struct map_session_data *sd) {
  92. return sd->group_level;
  93. }
  94. static int pc_invincible_timer(int tid, unsigned int tick, int id, intptr_t data)
  95. {
  96. struct map_session_data *sd;
  97. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  98. return 1;
  99. if(sd->invincible_timer != tid){
  100. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  101. return 0;
  102. }
  103. sd->invincible_timer = INVALID_TIMER;
  104. skill_unit_move(&sd->bl,tick,1);
  105. return 0;
  106. }
  107. void pc_setinvincibletimer(struct map_session_data* sd, int val)
  108. {
  109. nullpo_retv(sd);
  110. if( sd->invincible_timer != INVALID_TIMER )
  111. delete_timer(sd->invincible_timer,pc_invincible_timer);
  112. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  113. }
  114. void pc_delinvincibletimer(struct map_session_data* sd)
  115. {
  116. nullpo_retv(sd);
  117. if( sd->invincible_timer != INVALID_TIMER )
  118. {
  119. delete_timer(sd->invincible_timer,pc_invincible_timer);
  120. sd->invincible_timer = INVALID_TIMER;
  121. skill_unit_move(&sd->bl,gettick(),1);
  122. }
  123. }
  124. static int pc_spiritball_timer(int tid, unsigned int tick, int id, intptr_t data)
  125. {
  126. struct map_session_data *sd;
  127. int i;
  128. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  129. return 1;
  130. if( sd->spiritball <= 0 )
  131. {
  132. 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);
  133. sd->spiritball = 0;
  134. return 0;
  135. }
  136. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  137. if( i == sd->spiritball )
  138. {
  139. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  140. return 0;
  141. }
  142. sd->spiritball--;
  143. if( i != sd->spiritball )
  144. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  145. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  146. clif_spiritball(&sd->bl);
  147. return 0;
  148. }
  149. int pc_addspiritball(struct map_session_data *sd,int interval,int max)
  150. {
  151. int tid, i;
  152. nullpo_ret(sd);
  153. if(max > MAX_SKILL_LEVEL)
  154. max = MAX_SKILL_LEVEL;
  155. if(sd->spiritball < 0)
  156. sd->spiritball = 0;
  157. if( sd->spiritball && sd->spiritball >= max )
  158. {
  159. if(sd->spirit_timer[0] != INVALID_TIMER)
  160. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  161. sd->spiritball--;
  162. if( sd->spiritball != 0 )
  163. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  164. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  165. }
  166. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  167. 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);
  168. if( i != sd->spiritball )
  169. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  170. sd->spirit_timer[i] = tid;
  171. sd->spiritball++;
  172. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  173. clif_millenniumshield(sd,sd->spiritball);
  174. else
  175. clif_spiritball(&sd->bl);
  176. return 0;
  177. }
  178. int pc_delspiritball(struct map_session_data *sd,int count,int type)
  179. {
  180. int i;
  181. nullpo_ret(sd);
  182. if(sd->spiritball <= 0) {
  183. sd->spiritball = 0;
  184. return 0;
  185. }
  186. if(count <= 0)
  187. return 0;
  188. if(count > sd->spiritball)
  189. count = sd->spiritball;
  190. sd->spiritball -= count;
  191. if(count > MAX_SKILL_LEVEL)
  192. count = MAX_SKILL_LEVEL;
  193. for(i=0;i<count;i++) {
  194. if(sd->spirit_timer[i] != INVALID_TIMER) {
  195. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  196. sd->spirit_timer[i] = INVALID_TIMER;
  197. }
  198. }
  199. for(i=count;i<MAX_SKILL_LEVEL;i++) {
  200. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  201. sd->spirit_timer[i] = INVALID_TIMER;
  202. }
  203. if(!type) {
  204. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  205. clif_millenniumshield(sd,sd->spiritball);
  206. else
  207. clif_spiritball(&sd->bl);
  208. }
  209. return 0;
  210. }
  211. static int pc_check_banding( struct block_list *bl, va_list ap ) {
  212. int *c, *b_sd;
  213. struct block_list *src;
  214. struct map_session_data *tsd;
  215. struct status_change *sc;
  216. nullpo_ret(bl);
  217. nullpo_ret(tsd = (struct map_session_data*)bl);
  218. nullpo_ret(src = va_arg(ap,struct block_list *));
  219. c = va_arg(ap,int *);
  220. b_sd = va_arg(ap, int *);
  221. if(pc_isdead(tsd))
  222. return 0;
  223. sc = status_get_sc(bl);
  224. if( bl == src )
  225. return 0;
  226. if( sc && sc->data[SC_BANDING] )
  227. {
  228. b_sd[(*c)++] = tsd->bl.id;
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
  234. int c;
  235. int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
  236. int i, j, hp, extra_hp = 0, tmp_qty = 0, tmp_hp;
  237. struct map_session_data *bsd;
  238. struct status_change *sc;
  239. int range = skill_get_splash(LG_BANDING,skill_lv);
  240. nullpo_ret(sd);
  241. c = 0;
  242. memset(b_sd, 0, sizeof(b_sd));
  243. i = party_foreachsamemap(pc_check_banding,sd,range,&sd->bl,&c,&b_sd);
  244. if( c < 1 ) //just recalc status no need to recalc hp
  245. { // No more Royal Guards in Banding found.
  246. if( (sc = status_get_sc(&sd->bl)) != NULL && sc->data[SC_BANDING] )
  247. {
  248. sc->data[SC_BANDING]->val2 = 0; // Reset the counter
  249. status_calc_bl(&sd->bl, status_sc2scb_flag(SC_BANDING));
  250. }
  251. return 0;
  252. }
  253. //Add yourself
  254. hp = status_get_hp(&sd->bl);
  255. i++;
  256. // Get total HP of all Royal Guards in party.
  257. for( j = 0; j < i; j++ )
  258. {
  259. bsd = map_id2sd(b_sd[j]);
  260. if( bsd != NULL )
  261. hp += status_get_hp(&bsd->bl);
  262. }
  263. // Set average HP.
  264. hp = hp / i;
  265. // If a Royal Guard have full HP, give more HP to others that haven't full HP.
  266. for( j = 0; j < i; j++ )
  267. {
  268. bsd = map_id2sd(b_sd[j]);
  269. if( bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0 )
  270. {
  271. extra_hp += tmp_hp;
  272. tmp_qty++;
  273. }
  274. }
  275. if( extra_hp > 0 && tmp_qty > 0 )
  276. hp += extra_hp / tmp_qty;
  277. for( j = 0; j < i; j++ )
  278. {
  279. bsd = map_id2sd(b_sd[j]);
  280. if( bsd != NULL )
  281. {
  282. status_set_hp(&bsd->bl,hp,0); // Set hp
  283. if( (sc = status_get_sc(&bsd->bl)) != NULL && sc->data[SC_BANDING] )
  284. {
  285. sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
  286. status_calc_bl(&bsd->bl, status_sc2scb_flag(SC_BANDING)); // Set atk and def.
  287. }
  288. }
  289. }
  290. return c;
  291. }
  292. // Increases a player's fame points and displays a notice to him
  293. void pc_addfame(struct map_session_data *sd,int count)
  294. {
  295. nullpo_retv(sd);
  296. sd->status.fame += count;
  297. if(sd->status.fame > MAX_FAME)
  298. sd->status.fame = MAX_FAME;
  299. switch(sd->class_&MAPID_UPPERMASK){
  300. case MAPID_BLACKSMITH: // Blacksmith
  301. clif_fame_blacksmith(sd,count);
  302. break;
  303. case MAPID_ALCHEMIST: // Alchemist
  304. clif_fame_alchemist(sd,count);
  305. break;
  306. case MAPID_TAEKWON: // Taekwon
  307. clif_fame_taekwon(sd,count);
  308. break;
  309. }
  310. chrif_updatefamelist(sd);
  311. }
  312. // Check whether a player ID is in the fame rankers' list of its job, returns his/her position if so, 0 else
  313. unsigned char pc_famerank(int char_id, int job)
  314. {
  315. int i;
  316. switch(job){
  317. case MAPID_BLACKSMITH: // Blacksmith
  318. for(i = 0; i < MAX_FAME_LIST; i++){
  319. if(smith_fame_list[i].id == char_id)
  320. return i + 1;
  321. }
  322. break;
  323. case MAPID_ALCHEMIST: // Alchemist
  324. for(i = 0; i < MAX_FAME_LIST; i++){
  325. if(chemist_fame_list[i].id == char_id)
  326. return i + 1;
  327. }
  328. break;
  329. case MAPID_TAEKWON: // Taekwon
  330. for(i = 0; i < MAX_FAME_LIST; i++){
  331. if(taekwon_fame_list[i].id == char_id)
  332. return i + 1;
  333. }
  334. break;
  335. }
  336. return 0;
  337. }
  338. int pc_setrestartvalue(struct map_session_data *sd,int type) {
  339. struct status_data *status, *b_status;
  340. nullpo_ret(sd);
  341. b_status = &sd->base_status;
  342. status = &sd->battle_status;
  343. if (type&1) { //Normal resurrection
  344. status->hp = 1; //Otherwise status_heal may fail if dead.
  345. status_heal(&sd->bl, b_status->hp, 0, 1);
  346. if( status->sp < b_status->sp )
  347. status_set_sp(&sd->bl, b_status->sp, 1);
  348. } else { //Just for saving on the char-server (with values as if respawned)
  349. sd->status.hp = b_status->hp;
  350. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  351. }
  352. return 0;
  353. }
  354. /*==========================================
  355. Rental System
  356. *------------------------------------------*/
  357. static int pc_inventory_rental_end(int tid, unsigned int tick, int id, intptr_t data)
  358. {
  359. struct map_session_data *sd = map_id2sd(id);
  360. if( sd == NULL )
  361. return 0;
  362. if( tid != sd->rental_timer )
  363. {
  364. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  365. return 0;
  366. }
  367. pc_inventory_rentals(sd);
  368. return 1;
  369. }
  370. int pc_inventory_rental_clear(struct map_session_data *sd)
  371. {
  372. if( sd->rental_timer != INVALID_TIMER )
  373. {
  374. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  375. sd->rental_timer = INVALID_TIMER;
  376. }
  377. return 1;
  378. }
  379. void pc_inventory_rentals(struct map_session_data *sd)
  380. {
  381. int i, c = 0;
  382. unsigned int expire_tick, next_tick = UINT_MAX;
  383. for( i = 0; i < MAX_INVENTORY; i++ )
  384. { // Check for Rentals on Inventory
  385. if( sd->status.inventory[i].nameid == 0 )
  386. continue; // Nothing here
  387. if( sd->status.inventory[i].expire_time == 0 )
  388. continue;
  389. if( sd->status.inventory[i].expire_time <= time(NULL) ) {
  390. if( sd->status.inventory[i].nameid == ITEMID_REINS_OF_MOUNT
  391. && sd->sc.option&OPTION_MOUNTING ) {
  392. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);
  393. }
  394. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  395. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  396. } else {
  397. expire_tick = (unsigned int)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
  398. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
  399. next_tick = min(expire_tick, next_tick);
  400. c++;
  401. }
  402. }
  403. 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
  404. sd->rental_timer = add_timer(gettick() + min(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  405. else
  406. sd->rental_timer = INVALID_TIMER;
  407. }
  408. void pc_inventory_rental_add(struct map_session_data *sd, int seconds)
  409. {
  410. int tick = seconds * 1000;
  411. if( sd == NULL )
  412. return;
  413. if( sd->rental_timer != INVALID_TIMER )
  414. {
  415. const struct TimerData * td;
  416. td = get_timer(sd->rental_timer);
  417. if( DIFF_TICK(td->tick, gettick()) > tick )
  418. { // Update Timer as this one ends first than the current one
  419. pc_inventory_rental_clear(sd);
  420. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  421. }
  422. }
  423. else
  424. sd->rental_timer = add_timer(gettick() + min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  425. }
  426. /**
  427. * Determines if player can give / drop / trade / vend items
  428. */
  429. bool pc_can_give_items(struct map_session_data *sd)
  430. {
  431. return pc_has_permission(sd, PC_PERM_TRADE);
  432. }
  433. /*==========================================
  434. * prepares character for saving.
  435. *------------------------------------------*/
  436. int pc_makesavestatus(struct map_session_data *sd)
  437. {
  438. nullpo_ret(sd);
  439. if(!battle_config.save_clothcolor)
  440. sd->status.clothes_color=0;
  441. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  442. //status change load/saving. [Skotlex]
  443. #ifdef NEW_CARTS
  444. sd->status.option = sd->sc.option&(OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  445. #else
  446. sd->status.option = sd->sc.option&(OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR|OPTION_MOUNTING);
  447. #endif
  448. if (sd->sc.data[SC_JAILED])
  449. { //When Jailed, do not move last point.
  450. if(pc_isdead(sd)){
  451. pc_setrestartvalue(sd,0);
  452. } else {
  453. sd->status.hp = sd->battle_status.hp;
  454. sd->status.sp = sd->battle_status.sp;
  455. }
  456. sd->status.last_point.map = sd->mapindex;
  457. sd->status.last_point.x = sd->bl.x;
  458. sd->status.last_point.y = sd->bl.y;
  459. return 0;
  460. }
  461. if(pc_isdead(sd)){
  462. pc_setrestartvalue(sd,0);
  463. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  464. } else {
  465. sd->status.hp = sd->battle_status.hp;
  466. sd->status.sp = sd->battle_status.sp;
  467. sd->status.last_point.map = sd->mapindex;
  468. sd->status.last_point.x = sd->bl.x;
  469. sd->status.last_point.y = sd->bl.y;
  470. }
  471. if(map[sd->bl.m].flag.nosave){
  472. struct map_data *m=&map[sd->bl.m];
  473. if(m->save.map)
  474. memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
  475. else
  476. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  477. }
  478. return 0;
  479. }
  480. /*==========================================
  481. * Off init ? Connection?
  482. *------------------------------------------*/
  483. int pc_setnewpc(struct map_session_data *sd, int account_id, int char_id, int login_id1, unsigned int client_tick, int sex, int fd)
  484. {
  485. nullpo_ret(sd);
  486. sd->bl.id = account_id;
  487. sd->status.account_id = account_id;
  488. sd->status.char_id = char_id;
  489. sd->status.sex = sex;
  490. sd->login_id1 = login_id1;
  491. sd->login_id2 = 0; // at this point, we can not know the value :(
  492. sd->client_tick = client_tick;
  493. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  494. sd->bl.type = BL_PC;
  495. sd->canlog_tick = gettick();
  496. //Required to prevent homunculus copuing a base speed of 0.
  497. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  498. return 0;
  499. }
  500. int pc_equippoint(struct map_session_data *sd,int n)
  501. {
  502. int ep = 0;
  503. nullpo_ret(sd);
  504. if(!sd->inventory_data[n])
  505. return 0;
  506. if (!itemdb_isequip2(sd->inventory_data[n]))
  507. return 0; //Not equippable by players.
  508. ep = sd->inventory_data[n]->equip;
  509. if(sd->inventory_data[n]->look == W_DAGGER ||
  510. sd->inventory_data[n]->look == W_1HSWORD ||
  511. sd->inventory_data[n]->look == W_1HAXE) {
  512. if(ep == EQP_HAND_R && (pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN ||
  513. (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO))//Kagerou and Oboro can dual wield daggers. [Rytech]
  514. return EQP_ARMS;
  515. }
  516. return ep;
  517. }
  518. int pc_setinventorydata(struct map_session_data *sd)
  519. {
  520. int i,id;
  521. nullpo_ret(sd);
  522. for(i=0;i<MAX_INVENTORY;i++) {
  523. id = sd->status.inventory[i].nameid;
  524. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  525. }
  526. return 0;
  527. }
  528. int pc_calcweapontype(struct map_session_data *sd)
  529. {
  530. nullpo_ret(sd);
  531. // single-hand
  532. if(sd->weapontype2 == W_FIST) {
  533. sd->status.weapon = sd->weapontype1;
  534. return 1;
  535. }
  536. if(sd->weapontype1 == W_FIST) {
  537. sd->status.weapon = sd->weapontype2;
  538. return 1;
  539. }
  540. // dual-wield
  541. sd->status.weapon = 0;
  542. switch (sd->weapontype1){
  543. case W_DAGGER:
  544. switch (sd->weapontype2) {
  545. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  546. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  547. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  548. }
  549. break;
  550. case W_1HSWORD:
  551. switch (sd->weapontype2) {
  552. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  553. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  554. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  555. }
  556. break;
  557. case W_1HAXE:
  558. switch (sd->weapontype2) {
  559. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  560. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  561. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  562. }
  563. }
  564. // unknown, default to right hand type
  565. if (!sd->status.weapon)
  566. sd->status.weapon = sd->weapontype1;
  567. return 2;
  568. }
  569. int pc_setequipindex(struct map_session_data *sd)
  570. {
  571. int i,j;
  572. nullpo_ret(sd);
  573. for(i=0;i<EQI_MAX;i++)
  574. sd->equip_index[i] = -1;
  575. for(i=0;i<MAX_INVENTORY;i++) {
  576. if(sd->status.inventory[i].nameid <= 0)
  577. continue;
  578. if(sd->status.inventory[i].equip) {
  579. for(j=0;j<EQI_MAX;j++)
  580. if(sd->status.inventory[i].equip & equip_pos[j])
  581. sd->equip_index[j] = i;
  582. if(sd->status.inventory[i].equip & EQP_HAND_R)
  583. {
  584. if(sd->inventory_data[i])
  585. sd->weapontype1 = sd->inventory_data[i]->look;
  586. else
  587. sd->weapontype1 = 0;
  588. }
  589. if( sd->status.inventory[i].equip & EQP_HAND_L )
  590. {
  591. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  592. sd->weapontype2 = sd->inventory_data[i]->look;
  593. else
  594. sd->weapontype2 = 0;
  595. }
  596. }
  597. }
  598. pc_calcweapontype(sd);
  599. return 0;
  600. }
  601. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  602. //{
  603. // int i;
  604. // struct item *item = &sd->status.inventory[eqindex];
  605. // struct item_data *data;
  606. //
  607. // //Crafted/made/hatched items.
  608. // if (itemdb_isspecial(item->card[0]))
  609. // return 1;
  610. //
  611. // /* scan for enchant armor gems */
  612. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  613. // s = MAX_SLOTS - 1;
  614. //
  615. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  616. // return( i < s ) ? 0 : 1;
  617. //}
  618. bool pc_isequipped(struct map_session_data *sd, int nameid)
  619. {
  620. int i, j, index;
  621. for( i = 0; i < EQI_MAX; i++ )
  622. {
  623. index = sd->equip_index[i];
  624. if( index < 0 ) continue;
  625. if( i == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  626. if( i == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  627. if( i == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  628. if( !sd->inventory_data[index] ) continue;
  629. if( sd->inventory_data[index]->nameid == nameid )
  630. return true;
  631. for( j = 0; j < sd->inventory_data[index]->slot; j++ )
  632. if( sd->status.inventory[index].card[j] == nameid )
  633. return true;
  634. }
  635. return false;
  636. }
  637. bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
  638. {
  639. if( !p1_sd || !p2_sd || !b_sd )
  640. return false;
  641. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  642. return false; // already adopted baby / in adopt request
  643. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  644. return false; // You need to be married and in party with baby to adopt
  645. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  646. return false; // Not married, wrong married
  647. if( p2_sd->status.party_id != p1_sd->status.party_id )
  648. return false; // Both parents need to be in the same party
  649. // Parents need to have their ring equipped
  650. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  651. return false;
  652. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  653. return false;
  654. // Already adopted a baby
  655. if( p1_sd->status.child || p2_sd->status.child ) {
  656. clif_Adopt_reply(p1_sd, 0);
  657. return false;
  658. }
  659. // Parents need at least lvl 70 to adopt
  660. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  661. clif_Adopt_reply(p1_sd, 1);
  662. return false;
  663. }
  664. if( b_sd->status.partner_id ) {
  665. clif_Adopt_reply(p1_sd, 2);
  666. return false;
  667. }
  668. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE ) )
  669. return false;
  670. return true;
  671. }
  672. /*==========================================
  673. * Adoption Process
  674. *------------------------------------------*/
  675. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  676. {
  677. int job, joblevel;
  678. unsigned int jobexp;
  679. if( !pc_can_Adopt(p1_sd, p2_sd, b_sd) )
  680. return false;
  681. // Preserve current job levels and progress
  682. joblevel = b_sd->status.job_level;
  683. jobexp = b_sd->status.job_exp;
  684. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  685. if( job != -1 && !pc_jobchange(b_sd, job, 0) )
  686. { // Success, proceed to configure parents and baby skills
  687. p1_sd->status.child = b_sd->status.char_id;
  688. p2_sd->status.child = b_sd->status.char_id;
  689. b_sd->status.father = p1_sd->status.char_id;
  690. b_sd->status.mother = p2_sd->status.char_id;
  691. // Restore progress
  692. b_sd->status.job_level = joblevel;
  693. clif_updatestatus(b_sd, SP_JOBLEVEL);
  694. b_sd->status.job_exp = jobexp;
  695. clif_updatestatus(b_sd, SP_JOBEXP);
  696. // Baby Skills
  697. pc_skill(b_sd, WE_BABY, 1, 0);
  698. pc_skill(b_sd, WE_CALLPARENT, 1, 0);
  699. // Parents Skills
  700. pc_skill(p1_sd, WE_CALLBABY, 1, 0);
  701. pc_skill(p2_sd, WE_CALLBABY, 1, 0);
  702. return true;
  703. }
  704. return false; // Job Change Fail
  705. }
  706. /*=================================================
  707. * Checks if the player can equip the item at index n in inventory.
  708. * Returns 0 (no) or 1 (yes).
  709. *------------------------------------------------*/
  710. int pc_isequip(struct map_session_data *sd,int n)
  711. {
  712. struct item_data *item;
  713. nullpo_ret(sd);
  714. item = sd->inventory_data[n];
  715. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  716. return 1;
  717. if(item == NULL)
  718. return 0;
  719. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  720. return 0;
  721. #ifdef RENEWAL
  722. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  723. return 0;
  724. #endif
  725. if(item->sex != 2 && sd->status.sex != item->sex)
  726. return 0;
  727. if (sd->sc.count) {
  728. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  729. return 0;
  730. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  731. return 0;
  732. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  733. return 0;
  734. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  735. return 0;
  736. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  737. return 0;
  738. if(item->equip && sd->sc.data[SC_KYOUGAKU])
  739. return 0;
  740. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  741. //Spirit of Super Novice equip bonuses. [Skotlex]
  742. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  743. return 1; //Can equip all helms
  744. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON)
  745. switch(item->look) { //In weapons, the look determines type of weapon.
  746. case W_DAGGER: //Level 4 Knives are equippable.. this means all knives, I'd guess?
  747. case W_1HSWORD: //All 1H swords
  748. case W_1HAXE: //All 1H Axes
  749. case W_MACE: //All 1H Maces
  750. case W_STAFF: //All 1H Staves
  751. return 1;
  752. }
  753. }
  754. }
  755. //Not equipable by class. [Skotlex]
  756. if (!(1<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
  757. return 0;
  758. //Not usable by upper class. [Inkfish]
  759. while( 1 ) {
  760. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  761. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  762. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  763. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  764. return 0;
  765. }
  766. return 1;
  767. }
  768. /*==========================================
  769. * No problem with the session id
  770. * set the status that has been sent from char server
  771. *------------------------------------------*/
  772. bool pc_authok(struct map_session_data *sd, int login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  773. {
  774. int i;
  775. unsigned long tick = gettick();
  776. uint32 ip = session[sd->fd]->client_addr;
  777. sd->login_id2 = login_id2;
  778. sd->group_id = group_id;
  779. /* load user permissions */
  780. pc_group_pc_load(sd);
  781. memcpy(&sd->status, st, sizeof(*st));
  782. if (st->sex != sd->status.sex) {
  783. clif_authfail_fd(sd->fd, 0);
  784. return false;
  785. }
  786. //Set the map-server used job id. [Skotlex]
  787. i = pc_jobid2mapid(sd->status.class_);
  788. if (i == -1) { //Invalid class?
  789. 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);
  790. sd->status.class_ = JOB_NOVICE;
  791. sd->class_ = MAPID_NOVICE;
  792. } else
  793. sd->class_ = i;
  794. // Checks and fixes to character status data, that are required
  795. // in case of configuration change or stuff, which cannot be
  796. // checked on char-server.
  797. if( sd->status.hair < MIN_HAIR_STYLE || sd->status.hair > MAX_HAIR_STYLE )
  798. {
  799. sd->status.hair = MIN_HAIR_STYLE;
  800. }
  801. if( sd->status.hair_color < MIN_HAIR_COLOR || sd->status.hair_color > MAX_HAIR_COLOR )
  802. {
  803. sd->status.hair_color = MIN_HAIR_COLOR;
  804. }
  805. if( sd->status.clothes_color < MIN_CLOTH_COLOR || sd->status.clothes_color > MAX_CLOTH_COLOR )
  806. {
  807. sd->status.clothes_color = MIN_CLOTH_COLOR;
  808. }
  809. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  810. if(!sd->status.hp) pc_setdead(sd);
  811. sd->state.connect_new = 1;
  812. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  813. sd->invincible_timer = INVALID_TIMER;
  814. sd->npc_timer_id = INVALID_TIMER;
  815. sd->pvp_timer = INVALID_TIMER;
  816. /**
  817. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  818. **/
  819. #if SECURE_NPCTIMEOUT
  820. /**
  821. * Initialize to defaults/expected
  822. **/
  823. sd->npc_idle_timer = INVALID_TIMER;
  824. sd->npc_idle_tick = tick;
  825. #endif
  826. sd->canuseitem_tick = tick;
  827. sd->canusecashfood_tick = tick;
  828. sd->canequip_tick = tick;
  829. sd->cantalk_tick = tick;
  830. sd->canskill_tick = tick;
  831. sd->cansendmail_tick = tick;
  832. for(i = 0; i < MAX_SKILL_LEVEL; i++)
  833. sd->spirit_timer[i] = INVALID_TIMER;
  834. for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
  835. sd->autobonus[i].active = INVALID_TIMER;
  836. for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
  837. sd->autobonus2[i].active = INVALID_TIMER;
  838. for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
  839. sd->autobonus3[i].active = INVALID_TIMER;
  840. if (battle_config.item_auto_get)
  841. sd->state.autoloot = 10000;
  842. if (battle_config.disp_experience)
  843. sd->state.showexp = 1;
  844. if (battle_config.disp_zeny)
  845. sd->state.showzeny = 1;
  846. if (!(battle_config.display_skill_fail&2))
  847. sd->state.showdelay = 1;
  848. pc_setinventorydata(sd);
  849. pc_setequipindex(sd);
  850. status_change_init(&sd->bl);
  851. if (pc_can_use_command(sd, "hide", COMMAND_ATCOMMAND))
  852. sd->status.option &= (OPTION_MASK | OPTION_INVISIBLE);
  853. else
  854. sd->status.option &= OPTION_MASK;
  855. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  856. //Set here because we need the inventory data for weapon sprite parsing.
  857. status_set_viewdata(&sd->bl, sd->status.class_);
  858. unit_dataset(&sd->bl);
  859. sd->guild_x = -1;
  860. sd->guild_y = -1;
  861. // Event Timers
  862. for( i = 0; i < MAX_EVENTTIMER; i++ )
  863. sd->eventtimer[i] = INVALID_TIMER;
  864. // Rental Timer
  865. sd->rental_timer = INVALID_TIMER;
  866. for( i = 0; i < 3; i++ )
  867. sd->hate_mob[i] = -1;
  868. //warp player
  869. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
  870. 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);
  871. // try warping to a default map instead (church graveyard)
  872. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
  873. // if we fail again
  874. clif_authfail_fd(sd->fd, 0);
  875. return false;
  876. }
  877. }
  878. clif_authok(sd);
  879. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  880. sd->die_counter=-1;
  881. //display login notice
  882. ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
  883. " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
  884. " Packet Ver: '"CL_WHITE"%d"CL_RESET"', IP: '"CL_WHITE"%d.%d.%d.%d"CL_RESET"',"
  885. " Group '"CL_WHITE"%d"CL_RESET"').\n",
  886. sd->status.name, sd->status.account_id, sd->status.char_id,
  887. sd->packet_ver, CONVIP(ip), sd->group_id);
  888. // Send friends list
  889. clif_friendslist_send(sd);
  890. if( !changing_mapservers ) {
  891. if (battle_config.display_version == 1){
  892. char buf[256];
  893. sprintf(buf, "SVN version: %s", get_svn_revision());
  894. clif_displaymessage(sd->fd, buf);
  895. }
  896. // Message of the Day [Valaris]
  897. for(i=0; motd_text[i][0] && i < MOTD_LINE_SIZE; i++) {
  898. if (battle_config.motd_type)
  899. clif_disp_onlyself(sd,motd_text[i],strlen(motd_text[i]));
  900. else
  901. clif_displaymessage(sd->fd, motd_text[i]);
  902. }
  903. // message of the limited time of the account
  904. if (expiration_time != 0) { // don't display if it's unlimited or unknow value
  905. char tmpstr[1024];
  906. strftime(tmpstr, sizeof(tmpstr) - 1, msg_txt(501), localtime(&expiration_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  907. clif_wis_message(sd->fd, wisp_server_name, tmpstr, strlen(tmpstr)+1);
  908. }
  909. /**
  910. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  911. **/
  912. clif_changemap(sd,sd->mapindex,sd->bl.x,sd->bl.y);
  913. }
  914. /**
  915. * Check if player have any cool downs on
  916. **/
  917. skill_cooldown_load(sd);
  918. /**
  919. * Check if player have any item cooldowns on
  920. **/
  921. pc_itemcd_do(sd,true);
  922. // Request all registries (auth is considered completed whence they arrive)
  923. intif_request_registry(sd,7);
  924. return true;
  925. }
  926. /*==========================================
  927. * Closes a connection because it failed to be authenticated from the char server.
  928. *------------------------------------------*/
  929. void pc_authfail(struct map_session_data *sd)
  930. {
  931. clif_authfail_fd(sd->fd, 0);
  932. return;
  933. }
  934. //Attempts to set a mob.
  935. int pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  936. {
  937. int class_;
  938. if (!sd || !bl || pos < 0 || pos > 2)
  939. return 0;
  940. if (sd->hate_mob[pos] != -1)
  941. { //Can't change hate targets.
  942. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  943. return 0;
  944. }
  945. class_ = status_get_class(bl);
  946. if (!pcdb_checkid(class_)) {
  947. unsigned int max_hp = status_get_max_hp(bl);
  948. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  949. return 0;
  950. if (pos != status_get_size(bl))
  951. return 0; //Wrong size
  952. }
  953. sd->hate_mob[pos] = class_;
  954. pc_setglobalreg(sd,sg_info[pos].hate_var,class_+1);
  955. clif_hate_info(sd, pos, class_, 1);
  956. return 1;
  957. }
  958. /*==========================================
  959. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  960. *------------------------------------------*/
  961. int pc_reg_received(struct map_session_data *sd)
  962. {
  963. int i,j;
  964. sd->change_level_2nd = pc_readglobalreg(sd,"jobchange_level");
  965. sd->change_level_3rd = pc_readglobalreg(sd,"jobchange_level_3rd");
  966. sd->die_counter = pc_readglobalreg(sd,"PC_DIE_COUNTER");
  967. // Cash shop
  968. sd->cashPoints = pc_readaccountreg(sd,"#CASHPOINTS");
  969. sd->kafraPoints = pc_readaccountreg(sd,"#KAFRAPOINTS");
  970. // Cooking Exp
  971. sd->cook_mastery = pc_readglobalreg(sd,"COOK_MASTERY");
  972. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  973. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  974. sd->mission_mobid = pc_readglobalreg(sd,"TK_MISSION_ID");
  975. sd->mission_count = pc_readglobalreg(sd,"TK_MISSION_COUNT");
  976. }
  977. //SG map and mob read [Komurka]
  978. for(i=0;i<MAX_PC_FEELHATE;i++) //for now - someone need to make reading from txt/sql
  979. {
  980. if ((j = pc_readglobalreg(sd,sg_info[i].feel_var))!=0) {
  981. sd->feel_map[i].index = j;
  982. sd->feel_map[i].m = map_mapindex2mapid(j);
  983. } else {
  984. sd->feel_map[i].index = 0;
  985. sd->feel_map[i].m = -1;
  986. }
  987. sd->hate_mob[i] = pc_readglobalreg(sd,sg_info[i].hate_var)-1;
  988. }
  989. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  990. sd->cloneskill_id = pc_readglobalreg(sd,"CLONE_SKILL");
  991. if (sd->cloneskill_id > 0) {
  992. sd->status.skill[sd->cloneskill_id].id = sd->cloneskill_id;
  993. sd->status.skill[sd->cloneskill_id].lv = pc_readglobalreg(sd,"CLONE_SKILL_LV");
  994. if (sd->status.skill[sd->cloneskill_id].lv > i)
  995. sd->status.skill[sd->cloneskill_id].lv = i;
  996. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  997. }
  998. }
  999. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1000. sd->reproduceskill_id = pc_readglobalreg(sd,"REPRODUCE_SKILL");
  1001. if( sd->reproduceskill_id > 0) {
  1002. sd->status.skill[sd->reproduceskill_id].id = sd->reproduceskill_id;
  1003. sd->status.skill[sd->reproduceskill_id].lv = pc_readglobalreg(sd,"REPRODUCE_SKILL_LV");
  1004. if( i < sd->status.skill[sd->reproduceskill_id].lv)
  1005. sd->status.skill[sd->reproduceskill_id].lv = i;
  1006. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PLAGIARIZED;
  1007. }
  1008. }
  1009. //Weird... maybe registries were reloaded?
  1010. if (sd->state.active)
  1011. return 0;
  1012. sd->state.active = 1;
  1013. if (sd->status.party_id)
  1014. party_member_joined(sd);
  1015. if (sd->status.guild_id)
  1016. guild_member_joined(sd);
  1017. // pet
  1018. if (sd->status.pet_id > 0)
  1019. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1020. // Homunculus [albator]
  1021. if( sd->status.hom_id > 0 )
  1022. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1023. if( sd->status.mer_id > 0 )
  1024. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1025. if( sd->status.ele_id > 0 )
  1026. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1027. map_addiddb(&sd->bl);
  1028. map_delnickdb(sd->status.char_id, sd->status.name);
  1029. if (!chrif_auth_finished(sd))
  1030. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1031. pc_load_combo(sd);
  1032. status_calc_pc(sd,1);
  1033. chrif_scdata_request(sd->status.account_id, sd->status.char_id);
  1034. intif_Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
  1035. intif_request_questlog(sd);
  1036. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1037. sd->state.connect_new = 1;
  1038. clif_parse_LoadEndAck(sd->fd, sd);
  1039. }
  1040. pc_inventory_rentals(sd);
  1041. return 1;
  1042. }
  1043. static int pc_calc_skillpoint(struct map_session_data* sd)
  1044. {
  1045. int i,skill,inf2,skill_point=0;
  1046. nullpo_ret(sd);
  1047. for(i=1;i<MAX_SKILL;i++){
  1048. if( (skill = pc_checkskill(sd,i)) > 0) {
  1049. inf2 = skill_get_inf2(i);
  1050. if((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1051. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1052. ) {
  1053. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1054. skill_point += skill;
  1055. else
  1056. if(sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0)
  1057. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1058. }
  1059. }
  1060. }
  1061. return skill_point;
  1062. }
  1063. /*==========================================
  1064. * Calculation of skill level.
  1065. *------------------------------------------*/
  1066. int pc_calc_skilltree(struct map_session_data *sd)
  1067. {
  1068. int i,id=0,flag;
  1069. int c=0;
  1070. nullpo_ret(sd);
  1071. i = pc_calc_skilltree_normalize_job(sd);
  1072. c = pc_mapid2jobid(i, sd->status.sex);
  1073. if( c == -1 )
  1074. { //Unable to normalize job??
  1075. 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);
  1076. return 1;
  1077. }
  1078. c = pc_class2idx(c);
  1079. for( i = 0; i < MAX_SKILL; i++ ) {
  1080. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1081. sd->status.skill[i].id = 0; //First clear skills.
  1082. /* permanent skills that must be re-checked */
  1083. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1084. switch( i ) {
  1085. case NV_TRICKDEAD:
  1086. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1087. sd->status.skill[i].id = 0;
  1088. sd->status.skill[i].lv = 0;
  1089. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1090. }
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. for( i = 0; i < MAX_SKILL; i++ )
  1096. {
  1097. 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 )
  1098. { // Restore original level of skills after deleting earned skills.
  1099. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1100. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1101. }
  1102. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER && i >= DC_HUMMING && i<= DC_SERVICEFORYOU )
  1103. { //Enable Bard/Dancer spirit linked skills.
  1104. if( sd->status.sex )
  1105. { //Link dancer skills to bard.
  1106. if( sd->status.skill[i-8].lv < 10 )
  1107. continue;
  1108. sd->status.skill[i].id = i;
  1109. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1110. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1111. }
  1112. else
  1113. { //Link bard skills to dancer.
  1114. if( sd->status.skill[i].lv < 10 )
  1115. continue;
  1116. sd->status.skill[i-8].id = i - 8;
  1117. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1118. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1119. }
  1120. }
  1121. }
  1122. if( pc_has_permission(sd, PC_PERM_ALL_SKILL) ) {
  1123. for( i = 0; i < MAX_SKILL; i++ ) {
  1124. switch(i) {
  1125. /**
  1126. * Dummy skills must be added here otherwise they'll be displayed in the,
  1127. * skill tree and since they have no icons they'll give resource errors
  1128. **/
  1129. case SM_SELFPROVOKE:
  1130. case AB_DUPLELIGHT_MELEE:
  1131. case AB_DUPLELIGHT_MAGIC:
  1132. case WL_CHAINLIGHTNING_ATK:
  1133. case WL_TETRAVORTEX_FIRE:
  1134. case WL_TETRAVORTEX_WATER:
  1135. case WL_TETRAVORTEX_WIND:
  1136. case WL_TETRAVORTEX_GROUND:
  1137. case WL_SUMMON_ATK_FIRE:
  1138. case WL_SUMMON_ATK_WIND:
  1139. case WL_SUMMON_ATK_WATER:
  1140. case WL_SUMMON_ATK_GROUND:
  1141. case LG_OVERBRAND_BRANDISH:
  1142. case LG_OVERBRAND_PLUSATK:
  1143. case WM_SEVERE_RAINSTORM_MELEE:
  1144. continue;
  1145. default:
  1146. break;
  1147. }
  1148. if( skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL) )
  1149. continue; //Only skills you can't have are npc/guild ones
  1150. if( skill_get_max(i) > 0 )
  1151. sd->status.skill[i].id = i;
  1152. }
  1153. return 0;
  1154. }
  1155. do {
  1156. flag = 0;
  1157. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1158. {
  1159. int f;
  1160. if( sd->status.skill[id].id )
  1161. continue; //Skill already known.
  1162. f = 1;
  1163. if(!battle_config.skillfree) {
  1164. int j;
  1165. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1166. int k;
  1167. if((k=skill_tree[c][i].need[j].id))
  1168. {
  1169. if (sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED)
  1170. k = 0; //Not learned.
  1171. else
  1172. if (sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1173. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1174. else
  1175. k = pc_checkskill(sd,k);
  1176. if (k < skill_tree[c][i].need[j].lv)
  1177. {
  1178. f = 0;
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. if( sd->status.job_level < skill_tree[c][i].joblv )
  1184. f = 0; // job level requirement wasn't satisfied
  1185. }
  1186. if( f ) {
  1187. int inf2;
  1188. inf2 = skill_get_inf2(id);
  1189. if(!sd->status.skill[id].lv && (
  1190. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1191. inf2&INF2_WEDDING_SKILL ||
  1192. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1193. ))
  1194. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1195. sd->status.skill[id].id = id;
  1196. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1197. sd->status.skill[id].lv = 1; // need to manually specify a skill level
  1198. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1199. }
  1200. flag = 1; // skill list has changed, perform another pass
  1201. }
  1202. }
  1203. } while(flag);
  1204. //
  1205. if( c > 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && sd->status.skill_point == 0 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  1206. {
  1207. /* Taekwon Ranger Bonus Skill Tree
  1208. ============================================
  1209. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1210. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1211. - (sd->status.skill_point == 0) to wait until all skill points are asigned to avoid problems with Job Change quest. */
  1212. for( i = 0; i < MAX_SKILL_TREE && (id = skill_tree[c][i].id) > 0; i++ )
  1213. {
  1214. if( (skill_get_inf2(id)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1215. continue; //Do not include Quest/Wedding skills.
  1216. if( sd->status.skill[id].id == 0 )
  1217. {
  1218. sd->status.skill[id].id = id;
  1219. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1220. }
  1221. else if( id != NV_BASIC )
  1222. {
  1223. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Remember original level
  1224. }
  1225. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_);
  1226. }
  1227. }
  1228. return 0;
  1229. }
  1230. //Checks if you can learn a new skill after having leveled up a skill.
  1231. static void pc_check_skilltree(struct map_session_data *sd, int skill)
  1232. {
  1233. int i,id=0,flag;
  1234. int c=0;
  1235. if(battle_config.skillfree)
  1236. return; //Function serves no purpose if this is set
  1237. i = pc_calc_skilltree_normalize_job(sd);
  1238. c = pc_mapid2jobid(i, sd->status.sex);
  1239. if (c == -1) { //Unable to normalize job??
  1240. 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);
  1241. return;
  1242. }
  1243. c = pc_class2idx(c);
  1244. do {
  1245. flag = 0;
  1246. for( i = 0; i < MAX_SKILL_TREE && (id=skill_tree[c][i].id)>0; i++ )
  1247. {
  1248. int j, f = 1, k;
  1249. if( sd->status.skill[id].id ) //Already learned
  1250. continue;
  1251. for( j = 0; j < MAX_PC_SKILL_REQUIRE; j++ )
  1252. {
  1253. if( (k = skill_tree[c][i].need[j].id) )
  1254. {
  1255. if( sd->status.skill[k].id == 0 || sd->status.skill[k].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[k].flag == SKILL_FLAG_PLAGIARIZED )
  1256. k = 0; //Not learned.
  1257. else
  1258. if( sd->status.skill[k].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1259. k = sd->status.skill[skill_tree[c][i].need[j].id].flag - SKILL_FLAG_REPLACED_LV_0;
  1260. else
  1261. k = pc_checkskill(sd,k);
  1262. if( k < skill_tree[c][i].need[j].lv )
  1263. {
  1264. f = 0;
  1265. break;
  1266. }
  1267. }
  1268. }
  1269. if( !f )
  1270. continue;
  1271. if( sd->status.job_level < skill_tree[c][i].joblv )
  1272. continue;
  1273. j = skill_get_inf2(id);
  1274. if( !sd->status.skill[id].lv && (
  1275. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1276. j&INF2_WEDDING_SKILL ||
  1277. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1278. ) )
  1279. continue; //Cannot be learned via normal means.
  1280. sd->status.skill[id].id = id;
  1281. flag = 1;
  1282. }
  1283. } while(flag);
  1284. }
  1285. // Make sure all the skills are in the correct condition
  1286. // before persisting to the backend.. [MouseJstr]
  1287. int pc_clean_skilltree(struct map_session_data *sd)
  1288. {
  1289. int i;
  1290. for (i = 0; i < MAX_SKILL; i++){
  1291. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED)
  1292. {
  1293. sd->status.skill[i].id = 0;
  1294. sd->status.skill[i].lv = 0;
  1295. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1296. }
  1297. else
  1298. if (sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0){
  1299. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1300. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1301. }
  1302. }
  1303. return 0;
  1304. }
  1305. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1306. {
  1307. int skill_point, novice_skills;
  1308. int c = sd->class_;
  1309. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1310. return c;
  1311. skill_point = pc_calc_skillpoint(sd);
  1312. novice_skills = max_level[pc_class2idx(JOB_NOVICE)][1] - 1;
  1313. // limit 1st class and above to novice job levels
  1314. if(skill_point < novice_skills)
  1315. {
  1316. c = MAPID_NOVICE;
  1317. }
  1318. // limit 2nd class and above to first class job levels (super novices are exempt)
  1319. else if ((sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1320. {
  1321. // regenerate change_level_2nd
  1322. if (!sd->change_level_2nd)
  1323. {
  1324. if (sd->class_&JOBL_THIRD)
  1325. {
  1326. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1327. if (!sd->change_level_3rd)
  1328. sd->change_level_2nd = max_level[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))][1];
  1329. else
  1330. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1331. - (sd->status.job_level - 1)
  1332. - (sd->change_level_3rd - 1)
  1333. - novice_skills;
  1334. }
  1335. else
  1336. {
  1337. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1338. - (sd->status.job_level - 1)
  1339. - novice_skills;
  1340. }
  1341. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  1342. }
  1343. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1344. {
  1345. c &= MAPID_BASEMASK;
  1346. }
  1347. // limit 3rd class to 2nd class/trans job levels
  1348. else if(sd->class_&JOBL_THIRD)
  1349. {
  1350. // regenerate change_level_3rd
  1351. if (!sd->change_level_3rd)
  1352. {
  1353. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1354. - (sd->status.job_level - 1)
  1355. - (sd->change_level_2nd - 1)
  1356. - novice_skills;
  1357. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  1358. }
  1359. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1360. c &= MAPID_UPPERMASK;
  1361. }
  1362. }
  1363. // restore non-limiting flags
  1364. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1365. return c;
  1366. }
  1367. /*==========================================
  1368. * Updates the weight status
  1369. *------------------------------------------
  1370. * 1: overweight 50%
  1371. * 2: overweight 90%
  1372. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1373. */
  1374. int pc_updateweightstatus(struct map_session_data *sd)
  1375. {
  1376. int old_overweight;
  1377. int new_overweight;
  1378. nullpo_retr(1, sd);
  1379. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1380. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1381. if( old_overweight == new_overweight )
  1382. return 0; // no change
  1383. // stop old status change
  1384. if( old_overweight == 1 )
  1385. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1386. else if( old_overweight == 2 )
  1387. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1388. // start new status change
  1389. if( new_overweight == 1 )
  1390. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1391. else if( new_overweight == 2 )
  1392. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1393. // update overweight status
  1394. sd->regen.state.overweight = new_overweight;
  1395. return 0;
  1396. }
  1397. int pc_disguise(struct map_session_data *sd, int class_)
  1398. {
  1399. if (!class_ && !sd->disguise)
  1400. return 0;
  1401. if (class_ && sd->disguise == class_)
  1402. return 0;
  1403. if(sd->sc.option&OPTION_INVISIBLE)
  1404. { //Character is invisible. Stealth class-change. [Skotlex]
  1405. sd->disguise = class_; //viewdata is set on uncloaking.
  1406. return 2;
  1407. }
  1408. if (sd->bl.prev != NULL) {
  1409. pc_stop_walking(sd, 0);
  1410. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1411. }
  1412. if (!class_) {
  1413. sd->disguise = 0;
  1414. class_ = sd->status.class_;
  1415. } else
  1416. sd->disguise=class_;
  1417. status_set_viewdata(&sd->bl, class_);
  1418. clif_changeoption(&sd->bl);
  1419. if (sd->bl.prev != NULL) {
  1420. clif_spawn(&sd->bl);
  1421. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1422. { //It seems the cart info is lost on undisguise.
  1423. clif_cartlist(sd);
  1424. clif_updatestatus(sd,SP_CARTINFO);
  1425. }
  1426. }
  1427. return 1;
  1428. }
  1429. static int pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, short card_id)
  1430. {
  1431. int i;
  1432. if( !rate )
  1433. return 0;
  1434. for( i = 0; i < max && spell[i].id; i++ )
  1435. {
  1436. if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
  1437. {
  1438. if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
  1439. return 0;
  1440. rate += spell[i].rate;
  1441. break;
  1442. }
  1443. }
  1444. if (i == max) {
  1445. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1446. return 0;
  1447. }
  1448. spell[i].id = id;
  1449. spell[i].lv = lv;
  1450. spell[i].rate = rate;
  1451. //Auto-update flag value.
  1452. if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1453. if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1454. if (!(flag&BF_SKILLMASK)) {
  1455. if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1456. if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1457. }
  1458. spell[i].flag|= flag;
  1459. spell[i].card_id = card_id;
  1460. return 1;
  1461. }
  1462. static int pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, short card_id)
  1463. {
  1464. int i;
  1465. if( !rate )
  1466. return 0;
  1467. for( i = 0; i < max && spell[i].id; i++ )
  1468. {
  1469. ; // each autospell works independently
  1470. }
  1471. if( i == max )
  1472. {
  1473. ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
  1474. return 0;
  1475. }
  1476. spell[i].flag = src_skill;
  1477. spell[i].id = id;
  1478. spell[i].lv = lv;
  1479. spell[i].rate = rate;
  1480. spell[i].card_id = card_id;
  1481. return 1;
  1482. }
  1483. static int pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, short rate, short arrow_rate, unsigned char flag)
  1484. {
  1485. int i;
  1486. if (!(flag&(ATF_SHORT|ATF_LONG)))
  1487. flag|=ATF_SHORT|ATF_LONG; //Default range: both
  1488. if (!(flag&(ATF_TARGET|ATF_SELF)))
  1489. flag|=ATF_TARGET; //Default target: enemy.
  1490. if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
  1491. flag|=ATF_WEAPON; //Default type: weapon.
  1492. for (i = 0; i < max && effect[i].flag; i++) {
  1493. if (effect[i].id == id && effect[i].flag == flag)
  1494. {
  1495. effect[i].rate += rate;
  1496. effect[i].arrow_rate += arrow_rate;
  1497. return 1;
  1498. }
  1499. }
  1500. if (i == max) {
  1501. ShowWarning("pc_bonus: Reached max (%d) number of add effects per character!\n", max);
  1502. return 0;
  1503. }
  1504. effect[i].id = id;
  1505. effect[i].rate = rate;
  1506. effect[i].arrow_rate = arrow_rate;
  1507. effect[i].flag = flag;
  1508. return 1;
  1509. }
  1510. static int pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill, unsigned char target)
  1511. {
  1512. int i;
  1513. for( i = 0; i < max && effect[i].skill; i++ )
  1514. {
  1515. if( effect[i].id == id && effect[i].skill == skill && effect[i].target == target )
  1516. {
  1517. effect[i].rate += rate;
  1518. return 1;
  1519. }
  1520. }
  1521. if( i == max ) {
  1522. ShowWarning("pc_bonus: Reached max (%d) number of add effects on skill per character!\n", max);
  1523. return 0;
  1524. }
  1525. effect[i].id = id;
  1526. effect[i].rate = rate;
  1527. effect[i].skill = skill;
  1528. effect[i].target = target;
  1529. return 1;
  1530. }
  1531. static int pc_bonus_item_drop(struct s_add_drop *drop, const short max, short id, short group, int race, int rate)
  1532. {
  1533. int i;
  1534. //Apply config rate adjustment settings.
  1535. if (rate >= 0) { //Absolute drop.
  1536. if (battle_config.item_rate_adddrop != 100)
  1537. rate = rate*battle_config.item_rate_adddrop/100;
  1538. if (rate < battle_config.item_drop_adddrop_min)
  1539. rate = battle_config.item_drop_adddrop_min;
  1540. else if (rate > battle_config.item_drop_adddrop_max)
  1541. rate = battle_config.item_drop_adddrop_max;
  1542. } else { //Relative drop, max/min limits are applied at drop time.
  1543. if (battle_config.item_rate_adddrop != 100)
  1544. rate = rate*battle_config.item_rate_adddrop/100;
  1545. if (rate > -1)
  1546. rate = -1;
  1547. }
  1548. for(i = 0; i < max && (drop[i].id || drop[i].group); i++) {
  1549. if(
  1550. ((id && drop[i].id == id) ||
  1551. (group && drop[i].group == group))
  1552. && race > 0
  1553. ) {
  1554. drop[i].race |= race;
  1555. if(drop[i].rate > 0 && rate > 0)
  1556. { //Both are absolute rates.
  1557. if (drop[i].rate < rate)
  1558. drop[i].rate = rate;
  1559. } else
  1560. if(drop[i].rate < 0 && rate < 0) {
  1561. //Both are relative rates.
  1562. if (drop[i].rate > rate)
  1563. drop[i].rate = rate;
  1564. } else if (rate < 0) //Give preference to relative rate.
  1565. drop[i].rate = rate;
  1566. return 1;
  1567. }
  1568. }
  1569. if(i == max) {
  1570. ShowWarning("pc_bonus: Reached max (%d) number of added drops per character!\n", max);
  1571. return 0;
  1572. }
  1573. drop[i].id = id;
  1574. drop[i].group = group;
  1575. drop[i].race |= race;
  1576. drop[i].rate = rate;
  1577. return 1;
  1578. }
  1579. int pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned short pos,bool onskill)
  1580. {
  1581. int i;
  1582. ARR_FIND(0, max, i, bonus[i].rate == 0);
  1583. if( i == max )
  1584. {
  1585. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
  1586. return 0;
  1587. }
  1588. if( !onskill )
  1589. {
  1590. if( !(flag&BF_RANGEMASK) )
  1591. flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
  1592. if( !(flag&BF_WEAPONMASK) )
  1593. flag|=BF_WEAPON; //No attack type defined? Use weapon.
  1594. if( !(flag&BF_SKILLMASK) )
  1595. {
  1596. if( flag&(BF_MAGIC|BF_MISC) )
  1597. flag|=BF_SKILL; //These two would never trigger without BF_SKILL
  1598. if( flag&BF_WEAPON )
  1599. flag|=BF_NORMAL|BF_SKILL;
  1600. }
  1601. }
  1602. bonus[i].rate = rate;
  1603. bonus[i].duration = dur;
  1604. bonus[i].active = INVALID_TIMER;
  1605. bonus[i].atk_type = flag;
  1606. bonus[i].pos = pos;
  1607. bonus[i].bonus_script = aStrdup(script);
  1608. bonus[i].other_script = other_script?aStrdup(other_script):NULL;
  1609. return 1;
  1610. }
  1611. int pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
  1612. {
  1613. int i;
  1614. nullpo_ret(sd);
  1615. for( i = 0; i < max; i++ )
  1616. {
  1617. if( autobonus[i].active != INVALID_TIMER )
  1618. {
  1619. if( restore && sd->state.autobonus&autobonus[i].pos )
  1620. {
  1621. if( autobonus[i].bonus_script )
  1622. {
  1623. int j;
  1624. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus[i].pos );
  1625. if( j < EQI_MAX-1 )
  1626. script_run_autobonus(autobonus[i].bonus_script,sd->bl.id,sd->equip_index[j]);
  1627. }
  1628. continue;
  1629. }
  1630. else
  1631. { // Logout / Unequipped an item with an activated bonus
  1632. delete_timer(autobonus[i].active,pc_endautobonus);
  1633. autobonus[i].active = INVALID_TIMER;
  1634. }
  1635. }
  1636. if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
  1637. if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
  1638. autobonus[i].bonus_script = autobonus[i].other_script = NULL;
  1639. autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
  1640. autobonus[i].active = INVALID_TIMER;
  1641. }
  1642. return 0;
  1643. }
  1644. int pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
  1645. {
  1646. nullpo_ret(sd);
  1647. nullpo_ret(autobonus);
  1648. if( autobonus->other_script )
  1649. {
  1650. int j;
  1651. ARR_FIND( 0, EQI_MAX-1, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus->pos );
  1652. if( j < EQI_MAX-1 )
  1653. script_run_autobonus(autobonus->other_script,sd->bl.id,sd->equip_index[j]);
  1654. }
  1655. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
  1656. sd->state.autobonus |= autobonus->pos;
  1657. status_calc_pc(sd,0);
  1658. return 0;
  1659. }
  1660. int pc_endautobonus(int tid, unsigned int tick, int id, intptr_t data)
  1661. {
  1662. struct map_session_data *sd = map_id2sd(id);
  1663. struct s_autobonus *autobonus = (struct s_autobonus *)data;
  1664. nullpo_ret(sd);
  1665. nullpo_ret(autobonus);
  1666. autobonus->active = INVALID_TIMER;
  1667. sd->state.autobonus &= ~autobonus->pos;
  1668. status_calc_pc(sd,0);
  1669. return 0;
  1670. }
  1671. int pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1672. {
  1673. int i;
  1674. struct weapon_data* wd;
  1675. wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  1676. ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
  1677. if (i == MAX_PC_BONUS)
  1678. {
  1679. ShowWarning("pc_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1680. return 0;
  1681. }
  1682. if (!(flag&BF_RANGEMASK))
  1683. flag |= BF_SHORT|BF_LONG;
  1684. if (!(flag&BF_WEAPONMASK))
  1685. flag |= BF_WEAPON;
  1686. if (!(flag&BF_SKILLMASK))
  1687. {
  1688. if (flag&(BF_MAGIC|BF_MISC))
  1689. flag |= BF_SKILL;
  1690. if (flag&BF_WEAPON)
  1691. flag |= BF_NORMAL|BF_SKILL;
  1692. }
  1693. wd->addele2[i].ele = ele;
  1694. wd->addele2[i].rate = rate;
  1695. wd->addele2[i].flag = flag;
  1696. return 0;
  1697. }
  1698. int pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  1699. {
  1700. int i;
  1701. ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
  1702. if (i == MAX_PC_BONUS)
  1703. {
  1704. ShowWarning("pc_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
  1705. return 0;
  1706. }
  1707. if (!(flag&BF_RANGEMASK))
  1708. flag |= BF_SHORT|BF_LONG;
  1709. if (!(flag&BF_WEAPONMASK))
  1710. flag |= BF_WEAPON;
  1711. if (!(flag&BF_SKILLMASK))
  1712. {
  1713. if (flag&(BF_MAGIC|BF_MISC))
  1714. flag |= BF_SKILL;
  1715. if (flag&BF_WEAPON)
  1716. flag |= BF_NORMAL|BF_SKILL;
  1717. }
  1718. sd->subele2[i].ele = ele;
  1719. sd->subele2[i].rate = rate;
  1720. sd->subele2[i].flag = flag;
  1721. return 0;
  1722. }
  1723. /*==========================================
  1724. * Add a bonus(type) to player sd
  1725. *------------------------------------------*/
  1726. int pc_bonus(struct map_session_data *sd,int type,int val)
  1727. {
  1728. struct status_data *status;
  1729. int bonus;
  1730. nullpo_ret(sd);
  1731. status = &sd->base_status;
  1732. switch(type){
  1733. case SP_STR:
  1734. case SP_AGI:
  1735. case SP_VIT:
  1736. case SP_INT:
  1737. case SP_DEX:
  1738. case SP_LUK:
  1739. if(sd->state.lr_flag != 2)
  1740. sd->param_bonus[type-SP_STR]+=val;
  1741. break;
  1742. case SP_ATK1:
  1743. if(!sd->state.lr_flag) {
  1744. bonus = status->rhw.atk + val;
  1745. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1746. }
  1747. else if(sd->state.lr_flag == 1) {
  1748. bonus = status->lhw.atk + val;
  1749. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  1750. }
  1751. break;
  1752. case SP_ATK2:
  1753. if(!sd->state.lr_flag) {
  1754. bonus = status->rhw.atk2 + val;
  1755. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1756. }
  1757. else if(sd->state.lr_flag == 1) {
  1758. bonus = status->lhw.atk2 + val;
  1759. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  1760. }
  1761. break;
  1762. case SP_BASE_ATK:
  1763. if(sd->state.lr_flag != 2) {
  1764. //#ifdef RENEWAL
  1765. // sd->bonus.eatk += val;
  1766. //#else
  1767. bonus = status->batk + val;
  1768. status->batk = cap_value(bonus, 0, USHRT_MAX);
  1769. //#endif
  1770. }
  1771. break;
  1772. case SP_DEF1:
  1773. if(sd->state.lr_flag != 2) {
  1774. bonus = status->def + val;
  1775. #ifdef RENEWAL
  1776. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1777. #else
  1778. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1779. #endif
  1780. }
  1781. break;
  1782. case SP_DEF2:
  1783. if(sd->state.lr_flag != 2) {
  1784. bonus = status->def2 + val;
  1785. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1786. }
  1787. break;
  1788. case SP_MDEF1:
  1789. if(sd->state.lr_flag != 2) {
  1790. bonus = status->mdef + val;
  1791. #ifdef RENEWAL
  1792. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1793. #else
  1794. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  1795. #endif
  1796. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  1797. sd->bonus.shieldmdef += bonus;
  1798. }
  1799. }
  1800. break;
  1801. case SP_MDEF2:
  1802. if(sd->state.lr_flag != 2) {
  1803. bonus = status->mdef2 + val;
  1804. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1805. }
  1806. break;
  1807. case SP_HIT:
  1808. if(sd->state.lr_flag != 2) {
  1809. bonus = status->hit + val;
  1810. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1811. } else
  1812. sd->bonus.arrow_hit+=val;
  1813. break;
  1814. case SP_FLEE1:
  1815. if(sd->state.lr_flag != 2) {
  1816. bonus = status->flee + val;
  1817. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1818. }
  1819. break;
  1820. case SP_FLEE2:
  1821. if(sd->state.lr_flag != 2) {
  1822. bonus = status->flee2 + val*10;
  1823. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1824. }
  1825. break;
  1826. case SP_CRITICAL:
  1827. if(sd->state.lr_flag != 2) {
  1828. bonus = status->cri + val*10;
  1829. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  1830. } else
  1831. sd->bonus.arrow_cri += val*10;
  1832. break;
  1833. case SP_ATKELE:
  1834. if(val >= ELE_MAX) {
  1835. ShowError("pc_bonus: SP_ATKELE: Invalid element %d\n", val);
  1836. break;
  1837. }
  1838. switch (sd->state.lr_flag)
  1839. {
  1840. case 2:
  1841. switch (sd->status.weapon) {
  1842. case W_BOW:
  1843. case W_REVOLVER:
  1844. case W_RIFLE:
  1845. case W_GATLING:
  1846. case W_SHOTGUN:
  1847. case W_GRENADE:
  1848. //Become weapon element.
  1849. status->rhw.ele=val;
  1850. break;
  1851. default: //Become arrow element.
  1852. sd->bonus.arrow_ele=val;
  1853. break;
  1854. }
  1855. break;
  1856. case 1:
  1857. status->lhw.ele=val;
  1858. break;
  1859. default:
  1860. status->rhw.ele=val;
  1861. break;
  1862. }
  1863. break;
  1864. case SP_DEFELE:
  1865. if(val >= ELE_MAX) {
  1866. ShowError("pc_bonus: SP_DEFELE: Invalid element %d\n", val);
  1867. break;
  1868. }
  1869. if(sd->state.lr_flag != 2)
  1870. status->def_ele=val;
  1871. break;
  1872. case SP_MAXHP:
  1873. if(sd->state.lr_flag == 2)
  1874. break;
  1875. val += (int)status->max_hp;
  1876. //Negative bonuses will underflow, this will be handled in status_calc_pc through casting
  1877. //If this is called outside of status_calc_pc, you'd better pray they do not underflow and end with UINT_MAX max_hp.
  1878. status->max_hp = (unsigned int)val;
  1879. break;
  1880. case SP_MAXSP:
  1881. if(sd->state.lr_flag == 2)
  1882. break;
  1883. val += (int)status->max_sp;
  1884. status->max_sp = (unsigned int)val;
  1885. break;
  1886. #ifndef RENEWAL_CAST
  1887. case SP_VARCASTRATE:
  1888. #endif
  1889. case SP_CASTRATE:
  1890. if(sd->state.lr_flag != 2)
  1891. sd->castrate+=val;
  1892. break;
  1893. case SP_MAXHPRATE:
  1894. if(sd->state.lr_flag != 2)
  1895. sd->hprate+=val;
  1896. break;
  1897. case SP_MAXSPRATE:
  1898. if(sd->state.lr_flag != 2)
  1899. sd->sprate+=val;
  1900. break;
  1901. case SP_SPRATE:
  1902. if(sd->state.lr_flag != 2)
  1903. sd->dsprate+=val;
  1904. break;
  1905. case SP_ATTACKRANGE:
  1906. switch (sd->state.lr_flag) {
  1907. case 2:
  1908. switch (sd->status.weapon) {
  1909. case W_BOW:
  1910. case W_REVOLVER:
  1911. case W_RIFLE:
  1912. case W_GATLING:
  1913. case W_SHOTGUN:
  1914. case W_GRENADE:
  1915. status->rhw.range += val;
  1916. }
  1917. break;
  1918. case 1:
  1919. status->lhw.range += val;
  1920. break;
  1921. default:
  1922. status->rhw.range += val;
  1923. break;
  1924. }
  1925. break;
  1926. case SP_SPEED_RATE: //Non stackable increase
  1927. if(sd->state.lr_flag != 2)
  1928. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  1929. break;
  1930. case SP_SPEED_ADDRATE: //Stackable increase
  1931. if(sd->state.lr_flag != 2)
  1932. sd->bonus.speed_add_rate -= val;
  1933. break;
  1934. case SP_ASPD: //Raw increase
  1935. if(sd->state.lr_flag != 2)
  1936. sd->bonus.aspd_add -= 10*val;
  1937. break;
  1938. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  1939. if(sd->state.lr_flag != 2)
  1940. #ifndef RENEWAL_ASPD
  1941. status->aspd_rate -= 10*val;
  1942. #else
  1943. status->aspd_rate2 += val;
  1944. #endif
  1945. break;
  1946. case SP_HP_RECOV_RATE:
  1947. if(sd->state.lr_flag != 2)
  1948. sd->hprecov_rate += val;
  1949. break;
  1950. case SP_SP_RECOV_RATE:
  1951. if(sd->state.lr_flag != 2)
  1952. sd->sprecov_rate += val;
  1953. break;
  1954. case SP_CRITICAL_DEF:
  1955. if(sd->state.lr_flag != 2)
  1956. sd->bonus.critical_def += val;
  1957. break;
  1958. case SP_NEAR_ATK_DEF:
  1959. if(sd->state.lr_flag != 2)
  1960. sd->bonus.near_attack_def_rate += val;
  1961. break;
  1962. case SP_LONG_ATK_DEF:
  1963. if(sd->state.lr_flag != 2)
  1964. sd->bonus.long_attack_def_rate += val;
  1965. break;
  1966. case SP_DOUBLE_RATE:
  1967. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  1968. sd->bonus.double_rate = val;
  1969. break;
  1970. case SP_DOUBLE_ADD_RATE:
  1971. if(sd->state.lr_flag == 0)
  1972. sd->bonus.double_add_rate += val;
  1973. break;
  1974. case SP_MATK_RATE:
  1975. if(sd->state.lr_flag != 2)
  1976. sd->matk_rate += val;
  1977. break;
  1978. case SP_IGNORE_DEF_ELE:
  1979. if(val >= ELE_MAX) {
  1980. ShowError("pc_bonus: SP_IGNORE_DEF_ELE: Invalid element %d\n", val);
  1981. break;
  1982. }
  1983. if(!sd->state.lr_flag)
  1984. sd->right_weapon.ignore_def_ele |= 1<<val;
  1985. else if(sd->state.lr_flag == 1)
  1986. sd->left_weapon.ignore_def_ele |= 1<<val;
  1987. break;
  1988. case SP_IGNORE_DEF_RACE:
  1989. if(!sd->state.lr_flag)
  1990. sd->right_weapon.ignore_def_race |= 1<<val;
  1991. else if(sd->state.lr_flag == 1)
  1992. sd->left_weapon.ignore_def_race |= 1<<val;
  1993. break;
  1994. case SP_ATK_RATE:
  1995. if(sd->state.lr_flag != 2)
  1996. sd->bonus.atk_rate += val;
  1997. break;
  1998. case SP_MAGIC_ATK_DEF:
  1999. if(sd->state.lr_flag != 2)
  2000. sd->bonus.magic_def_rate += val;
  2001. break;
  2002. case SP_MISC_ATK_DEF:
  2003. if(sd->state.lr_flag != 2)
  2004. sd->bonus.misc_def_rate += val;
  2005. break;
  2006. case SP_IGNORE_MDEF_RATE:
  2007. if(sd->state.lr_flag != 2) {
  2008. sd->ignore_mdef[RC_NONBOSS] += val;
  2009. sd->ignore_mdef[RC_BOSS] += val;
  2010. }
  2011. break;
  2012. case SP_IGNORE_MDEF_ELE:
  2013. if(val >= ELE_MAX) {
  2014. ShowError("pc_bonus: SP_IGNORE_MDEF_ELE: Invalid element %d\n", val);
  2015. break;
  2016. }
  2017. if(sd->state.lr_flag != 2)
  2018. sd->bonus.ignore_mdef_ele |= 1<<val;
  2019. break;
  2020. case SP_IGNORE_MDEF_RACE:
  2021. if(sd->state.lr_flag != 2)
  2022. sd->bonus.ignore_mdef_race |= 1<<val;
  2023. break;
  2024. case SP_PERFECT_HIT_RATE:
  2025. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2026. sd->bonus.perfect_hit = val;
  2027. break;
  2028. case SP_PERFECT_HIT_ADD_RATE:
  2029. if(sd->state.lr_flag != 2)
  2030. sd->bonus.perfect_hit_add += val;
  2031. break;
  2032. case SP_CRITICAL_RATE:
  2033. if(sd->state.lr_flag != 2)
  2034. sd->critical_rate+=val;
  2035. break;
  2036. case SP_DEF_RATIO_ATK_ELE:
  2037. if(val >= ELE_MAX) {
  2038. ShowError("pc_bonus: SP_DEF_RATIO_ATK_ELE: Invalid element %d\n", val);
  2039. break;
  2040. }
  2041. if(!sd->state.lr_flag)
  2042. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2043. else if(sd->state.lr_flag == 1)
  2044. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2045. break;
  2046. case SP_DEF_RATIO_ATK_RACE:
  2047. if(val >= RC_MAX) {
  2048. ShowError("pc_bonus: SP_DEF_RATIO_ATK_RACE: Invalid race %d\n", val);
  2049. break;
  2050. }
  2051. if(!sd->state.lr_flag)
  2052. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2053. else if(sd->state.lr_flag == 1)
  2054. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2055. break;
  2056. case SP_HIT_RATE:
  2057. if(sd->state.lr_flag != 2)
  2058. sd->hit_rate += val;
  2059. break;
  2060. case SP_FLEE_RATE:
  2061. if(sd->state.lr_flag != 2)
  2062. sd->flee_rate += val;
  2063. break;
  2064. case SP_FLEE2_RATE:
  2065. if(sd->state.lr_flag != 2)
  2066. sd->flee2_rate += val;
  2067. break;
  2068. case SP_DEF_RATE:
  2069. if(sd->state.lr_flag != 2)
  2070. sd->def_rate += val;
  2071. break;
  2072. case SP_DEF2_RATE:
  2073. if(sd->state.lr_flag != 2)
  2074. sd->def2_rate += val;
  2075. break;
  2076. case SP_MDEF_RATE:
  2077. if(sd->state.lr_flag != 2)
  2078. sd->mdef_rate += val;
  2079. break;
  2080. case SP_MDEF2_RATE:
  2081. if(sd->state.lr_flag != 2)
  2082. sd->mdef2_rate += val;
  2083. break;
  2084. case SP_RESTART_FULL_RECOVER:
  2085. if(sd->state.lr_flag != 2)
  2086. sd->special_state.restart_full_recover = 1;
  2087. break;
  2088. case SP_NO_CASTCANCEL:
  2089. if(sd->state.lr_flag != 2)
  2090. sd->special_state.no_castcancel = 1;
  2091. break;
  2092. case SP_NO_CASTCANCEL2:
  2093. if(sd->state.lr_flag != 2)
  2094. sd->special_state.no_castcancel2 = 1;
  2095. break;
  2096. case SP_NO_SIZEFIX:
  2097. if(sd->state.lr_flag != 2)
  2098. sd->special_state.no_sizefix = 1;
  2099. break;
  2100. case SP_NO_MAGIC_DAMAGE:
  2101. if(sd->state.lr_flag == 2)
  2102. break;
  2103. val+= sd->special_state.no_magic_damage;
  2104. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2105. break;
  2106. case SP_NO_WEAPON_DAMAGE:
  2107. if(sd->state.lr_flag == 2)
  2108. break;
  2109. val+= sd->special_state.no_weapon_damage;
  2110. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2111. break;
  2112. case SP_NO_MISC_DAMAGE:
  2113. if(sd->state.lr_flag == 2)
  2114. break;
  2115. val+= sd->special_state.no_misc_damage;
  2116. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2117. break;
  2118. case SP_NO_GEMSTONE:
  2119. if(sd->state.lr_flag != 2)
  2120. sd->special_state.no_gemstone = 1;
  2121. break;
  2122. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2123. if(sd->state.lr_flag != 2) {
  2124. sd->special_state.intravision = 1;
  2125. clif_status_load(&sd->bl, SI_INTRAVISION, 1);
  2126. }
  2127. break;
  2128. case SP_NO_KNOCKBACK:
  2129. if(sd->state.lr_flag != 2)
  2130. sd->special_state.no_knockback = 1;
  2131. break;
  2132. case SP_SPLASH_RANGE:
  2133. if(sd->bonus.splash_range < val)
  2134. sd->bonus.splash_range = val;
  2135. break;
  2136. case SP_SPLASH_ADD_RANGE:
  2137. sd->bonus.splash_add_range += val;
  2138. break;
  2139. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2140. if(sd->state.lr_flag != 2)
  2141. sd->bonus.short_weapon_damage_return += val;
  2142. break;
  2143. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2144. if(sd->state.lr_flag != 2)
  2145. sd->bonus.long_weapon_damage_return += val;
  2146. break;
  2147. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2148. if(sd->state.lr_flag != 2)
  2149. sd->bonus.magic_damage_return += val;
  2150. break;
  2151. case SP_ALL_STATS: // [Valaris]
  2152. if(sd->state.lr_flag!=2) {
  2153. sd->param_bonus[SP_STR-SP_STR]+=val;
  2154. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2155. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2156. sd->param_bonus[SP_INT-SP_STR]+=val;
  2157. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2158. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2159. }
  2160. break;
  2161. case SP_AGI_VIT: // [Valaris]
  2162. if(sd->state.lr_flag!=2) {
  2163. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2164. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2165. }
  2166. break;
  2167. case SP_AGI_DEX_STR: // [Valaris]
  2168. if(sd->state.lr_flag!=2) {
  2169. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2170. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2171. sd->param_bonus[SP_STR-SP_STR]+=val;
  2172. }
  2173. break;
  2174. case SP_PERFECT_HIDE: // [Valaris]
  2175. if(sd->state.lr_flag!=2)
  2176. sd->special_state.perfect_hiding=1;
  2177. break;
  2178. case SP_UNBREAKABLE:
  2179. if(sd->state.lr_flag!=2)
  2180. sd->bonus.unbreakable += val;
  2181. break;
  2182. case SP_UNBREAKABLE_WEAPON:
  2183. if(sd->state.lr_flag != 2)
  2184. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2185. break;
  2186. case SP_UNBREAKABLE_ARMOR:
  2187. if(sd->state.lr_flag != 2)
  2188. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2189. break;
  2190. case SP_UNBREAKABLE_HELM:
  2191. if(sd->state.lr_flag != 2)
  2192. sd->bonus.unbreakable_equip |= EQP_HELM;
  2193. break;
  2194. case SP_UNBREAKABLE_SHIELD:
  2195. if(sd->state.lr_flag != 2)
  2196. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2197. break;
  2198. case SP_UNBREAKABLE_GARMENT:
  2199. if(sd->state.lr_flag != 2)
  2200. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2201. break;
  2202. case SP_UNBREAKABLE_SHOES:
  2203. if(sd->state.lr_flag != 2)
  2204. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2205. break;
  2206. case SP_CLASSCHANGE: // [Valaris]
  2207. if(sd->state.lr_flag !=2)
  2208. sd->bonus.classchange=val;
  2209. break;
  2210. case SP_LONG_ATK_RATE:
  2211. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2212. sd->bonus.long_attack_atk_rate+=val;
  2213. break;
  2214. case SP_BREAK_WEAPON_RATE:
  2215. if(sd->state.lr_flag != 2)
  2216. sd->bonus.break_weapon_rate+=val;
  2217. break;
  2218. case SP_BREAK_ARMOR_RATE:
  2219. if(sd->state.lr_flag != 2)
  2220. sd->bonus.break_armor_rate+=val;
  2221. break;
  2222. case SP_ADD_STEAL_RATE:
  2223. if(sd->state.lr_flag != 2)
  2224. sd->bonus.add_steal_rate+=val;
  2225. break;
  2226. case SP_DELAYRATE:
  2227. if(sd->state.lr_flag != 2)
  2228. sd->delayrate+=val;
  2229. break;
  2230. case SP_CRIT_ATK_RATE:
  2231. if(sd->state.lr_flag != 2)
  2232. sd->bonus.crit_atk_rate += val;
  2233. break;
  2234. case SP_NO_REGEN:
  2235. if(sd->state.lr_flag != 2)
  2236. sd->regen.state.block|=val;
  2237. break;
  2238. case SP_UNSTRIPABLE_WEAPON:
  2239. if(sd->state.lr_flag != 2)
  2240. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2241. break;
  2242. case SP_UNSTRIPABLE:
  2243. case SP_UNSTRIPABLE_ARMOR:
  2244. if(sd->state.lr_flag != 2)
  2245. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2246. break;
  2247. case SP_UNSTRIPABLE_HELM:
  2248. if(sd->state.lr_flag != 2)
  2249. sd->bonus.unstripable_equip |= EQP_HELM;
  2250. break;
  2251. case SP_UNSTRIPABLE_SHIELD:
  2252. if(sd->state.lr_flag != 2)
  2253. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2254. break;
  2255. case SP_HP_DRAIN_VALUE:
  2256. if(!sd->state.lr_flag) {
  2257. sd->right_weapon.hp_drain[RC_NONBOSS].value += val;
  2258. sd->right_weapon.hp_drain[RC_BOSS].value += val;
  2259. }
  2260. else if(sd->state.lr_flag == 1) {
  2261. sd->left_weapon.hp_drain[RC_NONBOSS].value += val;
  2262. sd->left_weapon.hp_drain[RC_BOSS].value += val;
  2263. }
  2264. break;
  2265. case SP_SP_DRAIN_VALUE:
  2266. if(!sd->state.lr_flag) {
  2267. sd->right_weapon.sp_drain[RC_NONBOSS].value += val;
  2268. sd->right_weapon.sp_drain[RC_BOSS].value += val;
  2269. }
  2270. else if(sd->state.lr_flag == 1) {
  2271. sd->left_weapon.sp_drain[RC_NONBOSS].value += val;
  2272. sd->left_weapon.sp_drain[RC_BOSS].value += val;
  2273. }
  2274. break;
  2275. case SP_SP_GAIN_VALUE:
  2276. if(!sd->state.lr_flag)
  2277. sd->bonus.sp_gain_value += val;
  2278. break;
  2279. case SP_HP_GAIN_VALUE:
  2280. if(!sd->state.lr_flag)
  2281. sd->bonus.hp_gain_value += val;
  2282. break;
  2283. case SP_MAGIC_SP_GAIN_VALUE:
  2284. if(!sd->state.lr_flag)
  2285. sd->bonus.magic_sp_gain_value += val;
  2286. break;
  2287. case SP_MAGIC_HP_GAIN_VALUE:
  2288. if(!sd->state.lr_flag)
  2289. sd->bonus.magic_hp_gain_value += val;
  2290. break;
  2291. case SP_ADD_HEAL_RATE:
  2292. if(sd->state.lr_flag != 2)
  2293. sd->bonus.add_heal_rate += val;
  2294. break;
  2295. case SP_ADD_HEAL2_RATE:
  2296. if(sd->state.lr_flag != 2)
  2297. sd->bonus.add_heal2_rate += val;
  2298. break;
  2299. case SP_ADD_ITEM_HEAL_RATE:
  2300. if(sd->state.lr_flag != 2)
  2301. sd->bonus.itemhealrate2 += val;
  2302. break;
  2303. case SP_EMATK:
  2304. if(sd->state.lr_flag != 2)
  2305. sd->bonus.ematk += val;
  2306. break;
  2307. case SP_FIXCASTRATE:
  2308. if(sd->state.lr_flag != 2)
  2309. sd->bonus.fixcastrate -= val;
  2310. break;
  2311. case SP_ADD_FIXEDCAST:
  2312. if(sd->state.lr_flag != 2)
  2313. sd->bonus.add_fixcast += val;
  2314. break;
  2315. #ifdef RENEWAL_CAST
  2316. case SP_VARCASTRATE:
  2317. if(sd->state.lr_flag != 2)
  2318. sd->bonus.varcastrate -= val;
  2319. break;
  2320. case SP_ADD_VARIABLECAST:
  2321. if(sd->state.lr_flag != 2)
  2322. sd->bonus.add_varcast += val;
  2323. break;
  2324. #endif
  2325. default:
  2326. ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
  2327. break;
  2328. }
  2329. return 0;
  2330. }
  2331. /*==========================================
  2332. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  2333. *------------------------------------------*/
  2334. int pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  2335. {
  2336. int i;
  2337. nullpo_ret(sd);
  2338. switch(type){
  2339. case SP_ADDELE:
  2340. if(type2 >= ELE_MAX) {
  2341. ShowError("pc_bonus2: SP_ADDELE: Invalid element %d\n", type2);
  2342. break;
  2343. }
  2344. if(!sd->state.lr_flag)
  2345. sd->right_weapon.addele[type2]+=val;
  2346. else if(sd->state.lr_flag == 1)
  2347. sd->left_weapon.addele[type2]+=val;
  2348. else if(sd->state.lr_flag == 2)
  2349. sd->arrow_addele[type2]+=val;
  2350. break;
  2351. case SP_ADDRACE:
  2352. if(!sd->state.lr_flag)
  2353. sd->right_weapon.addrace[type2]+=val;
  2354. else if(sd->state.lr_flag == 1)
  2355. sd->left_weapon.addrace[type2]+=val;
  2356. else if(sd->state.lr_flag == 2)
  2357. sd->arrow_addrace[type2]+=val;
  2358. break;
  2359. case SP_ADDSIZE:
  2360. if(!sd->state.lr_flag)
  2361. sd->right_weapon.addsize[type2]+=val;
  2362. else if(sd->state.lr_flag == 1)
  2363. sd->left_weapon.addsize[type2]+=val;
  2364. else if(sd->state.lr_flag == 2)
  2365. sd->arrow_addsize[type2]+=val;
  2366. break;
  2367. case SP_SUBELE:
  2368. if(type2 >= ELE_MAX) {
  2369. ShowError("pc_bonus2: SP_SUBELE: Invalid element %d\n", type2);
  2370. break;
  2371. }
  2372. if(sd->state.lr_flag != 2)
  2373. sd->subele[type2]+=val;
  2374. break;
  2375. case SP_SUBRACE:
  2376. if(sd->state.lr_flag != 2)
  2377. sd->subrace[type2]+=val;
  2378. break;
  2379. case SP_ADDEFF:
  2380. if (type2 > SC_MAX) {
  2381. ShowWarning("pc_bonus2 (Add Effect): %d is not supported.\n", type2);
  2382. break;
  2383. }
  2384. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2385. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0);
  2386. break;
  2387. case SP_ADDEFF2:
  2388. if (type2 > SC_MAX) {
  2389. ShowWarning("pc_bonus2 (Add Effect2): %d is not supported.\n", type2);
  2390. break;
  2391. }
  2392. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2393. sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF);
  2394. break;
  2395. case SP_RESEFF:
  2396. if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
  2397. ShowWarning("pc_bonus2 (Resist Effect): %d is not supported.\n", type2);
  2398. break;
  2399. }
  2400. if(sd->state.lr_flag == 2)
  2401. break;
  2402. i = sd->reseff[type2-SC_COMMON_MIN]+val;
  2403. sd->reseff[type2-SC_COMMON_MIN]= cap_value(i, 0, 10000);
  2404. break;
  2405. case SP_MAGIC_ADDELE:
  2406. if(type2 >= ELE_MAX) {
  2407. ShowError("pc_bonus2: SP_MAGIC_ADDELE: Invalid element %d\n", type2);
  2408. break;
  2409. }
  2410. if(sd->state.lr_flag != 2)
  2411. sd->magic_addele[type2]+=val;
  2412. break;
  2413. case SP_MAGIC_ADDRACE:
  2414. if(sd->state.lr_flag != 2)
  2415. sd->magic_addrace[type2]+=val;
  2416. break;
  2417. case SP_MAGIC_ADDSIZE:
  2418. if(sd->state.lr_flag != 2)
  2419. sd->magic_addsize[type2]+=val;
  2420. break;
  2421. case SP_MAGIC_ATK_ELE:
  2422. if(sd->state.lr_flag != 2)
  2423. sd->magic_atk_ele[type2]+=val;
  2424. break;
  2425. case SP_ADD_DAMAGE_CLASS:
  2426. switch (sd->state.lr_flag) {
  2427. case 0: //Right hand
  2428. 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);
  2429. if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
  2430. {
  2431. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
  2432. break;
  2433. }
  2434. sd->right_weapon.add_dmg[i].class_ = type2;
  2435. sd->right_weapon.add_dmg[i].rate += val;
  2436. if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2437. 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]));
  2438. break;
  2439. case 1: //Left hand
  2440. 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);
  2441. if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
  2442. {
  2443. ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
  2444. break;
  2445. }
  2446. sd->left_weapon.add_dmg[i].class_ = type2;
  2447. sd->left_weapon.add_dmg[i].rate += val;
  2448. if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
  2449. 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]));
  2450. break;
  2451. }
  2452. break;
  2453. case SP_ADD_MAGIC_DAMAGE_CLASS:
  2454. if(sd->state.lr_flag == 2)
  2455. break;
  2456. ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
  2457. if (i == ARRAYLENGTH(sd->add_mdmg))
  2458. {
  2459. ShowWarning("pc_bonus2: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
  2460. break;
  2461. }
  2462. sd->add_mdmg[i].class_ = type2;
  2463. sd->add_mdmg[i].rate += val;
  2464. if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
  2465. memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
  2466. break;
  2467. case SP_ADD_DEF_CLASS:
  2468. if(sd->state.lr_flag == 2)
  2469. break;
  2470. ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
  2471. if (i == ARRAYLENGTH(sd->add_def))
  2472. {
  2473. ShowWarning("pc_bonus2: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
  2474. break;
  2475. }
  2476. sd->add_def[i].class_ = type2;
  2477. sd->add_def[i].rate += val;
  2478. if (!sd->add_def[i].rate) //Shift the rest of elements up.
  2479. memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
  2480. break;
  2481. case SP_ADD_MDEF_CLASS:
  2482. if(sd->state.lr_flag == 2)
  2483. break;
  2484. ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
  2485. if (i == ARRAYLENGTH(sd->add_mdef))
  2486. {
  2487. ShowWarning("pc_bonus2: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
  2488. break;
  2489. }
  2490. sd->add_mdef[i].class_ = type2;
  2491. sd->add_mdef[i].rate += val;
  2492. if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
  2493. memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
  2494. break;
  2495. case SP_HP_DRAIN_RATE:
  2496. if(!sd->state.lr_flag) {
  2497. sd->right_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2498. sd->right_weapon.hp_drain[RC_NONBOSS].per += val;
  2499. sd->right_weapon.hp_drain[RC_BOSS].rate += type2;
  2500. sd->right_weapon.hp_drain[RC_BOSS].per += val;
  2501. }
  2502. else if(sd->state.lr_flag == 1) {
  2503. sd->left_weapon.hp_drain[RC_NONBOSS].rate += type2;
  2504. sd->left_weapon.hp_drain[RC_NONBOSS].per += val;
  2505. sd->left_weapon.hp_drain[RC_BOSS].rate += type2;
  2506. sd->left_weapon.hp_drain[RC_BOSS].per += val;
  2507. }
  2508. break;
  2509. case SP_HP_DRAIN_VALUE:
  2510. if(!sd->state.lr_flag) {
  2511. sd->right_weapon.hp_drain[RC_NONBOSS].value += type2;
  2512. sd->right_weapon.hp_drain[RC_NONBOSS].type = val;
  2513. sd->right_weapon.hp_drain[RC_BOSS].value += type2;
  2514. sd->right_weapon.hp_drain[RC_BOSS].type = val;
  2515. }
  2516. else if(sd->state.lr_flag == 1) {
  2517. sd->left_weapon.hp_drain[RC_NONBOSS].value += type2;
  2518. sd->left_weapon.hp_drain[RC_NONBOSS].type = val;
  2519. sd->left_weapon.hp_drain[RC_BOSS].value += type2;
  2520. sd->left_weapon.hp_drain[RC_BOSS].type = val;
  2521. }
  2522. break;
  2523. case SP_SP_DRAIN_RATE:
  2524. if(!sd->state.lr_flag) {
  2525. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2526. sd->right_weapon.sp_drain[RC_NONBOSS].per += val;
  2527. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2528. sd->right_weapon.sp_drain[RC_BOSS].per += val;
  2529. }
  2530. else if(sd->state.lr_flag == 1) {
  2531. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2532. sd->left_weapon.sp_drain[RC_NONBOSS].per += val;
  2533. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2534. sd->left_weapon.sp_drain[RC_BOSS].per += val;
  2535. }
  2536. break;
  2537. case SP_SP_DRAIN_VALUE:
  2538. if(!sd->state.lr_flag) {
  2539. sd->right_weapon.sp_drain[RC_NONBOSS].value += type2;
  2540. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2541. sd->right_weapon.sp_drain[RC_BOSS].value += type2;
  2542. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2543. }
  2544. else if(sd->state.lr_flag == 1) {
  2545. sd->left_weapon.sp_drain[RC_NONBOSS].value += type2;
  2546. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2547. sd->left_weapon.sp_drain[RC_BOSS].value += type2;
  2548. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2549. }
  2550. break;
  2551. case SP_SP_VANISH_RATE:
  2552. if(sd->state.lr_flag != 2) {
  2553. sd->bonus.sp_vanish_rate += type2;
  2554. sd->bonus.sp_vanish_per += val;
  2555. }
  2556. break;
  2557. case SP_GET_ZENY_NUM:
  2558. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  2559. sd->bonus.get_zeny_rate = val;
  2560. sd->bonus.get_zeny_num = type2;
  2561. }
  2562. break;
  2563. case SP_ADD_GET_ZENY_NUM:
  2564. if(sd->state.lr_flag != 2) {
  2565. sd->bonus.get_zeny_rate += val;
  2566. sd->bonus.get_zeny_num += type2;
  2567. }
  2568. break;
  2569. case SP_WEAPON_COMA_ELE:
  2570. if(type2 >= ELE_MAX) {
  2571. ShowError("pc_bonus2: SP_WEAPON_COMA_ELE: Invalid element %d\n", type2);
  2572. break;
  2573. }
  2574. if(sd->state.lr_flag == 2)
  2575. break;
  2576. sd->weapon_coma_ele[type2] += val;
  2577. sd->special_state.bonus_coma = 1;
  2578. break;
  2579. case SP_WEAPON_COMA_RACE:
  2580. if(sd->state.lr_flag == 2)
  2581. break;
  2582. sd->weapon_coma_race[type2] += val;
  2583. sd->special_state.bonus_coma = 1;
  2584. break;
  2585. case SP_WEAPON_ATK:
  2586. if(sd->state.lr_flag != 2)
  2587. sd->weapon_atk[type2]+=val;
  2588. break;
  2589. case SP_WEAPON_ATK_RATE:
  2590. if(sd->state.lr_flag != 2)
  2591. sd->weapon_atk_rate[type2]+=val;
  2592. break;
  2593. case SP_CRITICAL_ADDRACE:
  2594. if(sd->state.lr_flag != 2)
  2595. sd->critaddrace[type2] += val*10;
  2596. break;
  2597. case SP_ADDEFF_WHENHIT:
  2598. if (type2 > SC_MAX) {
  2599. ShowWarning("pc_bonus2 (Add Effect when hit): %d is not supported.\n", type2);
  2600. break;
  2601. }
  2602. if(sd->state.lr_flag != 2)
  2603. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0);
  2604. break;
  2605. case SP_SKILL_ATK:
  2606. if(sd->state.lr_flag == 2)
  2607. break;
  2608. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
  2609. if (i == ARRAYLENGTH(sd->skillatk))
  2610. { //Better mention this so the array length can be updated. [Skotlex]
  2611. ShowDebug("run_script: bonus2 bSkillAtk reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
  2612. break;
  2613. }
  2614. if (sd->skillatk[i].id == type2)
  2615. sd->skillatk[i].val += val;
  2616. else {
  2617. sd->skillatk[i].id = type2;
  2618. sd->skillatk[i].val = val;
  2619. }
  2620. break;
  2621. case SP_SKILL_HEAL:
  2622. if(sd->state.lr_flag == 2)
  2623. break;
  2624. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
  2625. if (i == ARRAYLENGTH(sd->skillheal))
  2626. { // Better mention this so the array length can be updated. [Skotlex]
  2627. ShowDebug("run_script: bonus2 bSkillHeal reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
  2628. break;
  2629. }
  2630. if (sd->skillheal[i].id == type2)
  2631. sd->skillheal[i].val += val;
  2632. else {
  2633. sd->skillheal[i].id = type2;
  2634. sd->skillheal[i].val = val;
  2635. }
  2636. break;
  2637. case SP_SKILL_HEAL2:
  2638. if(sd->state.lr_flag == 2)
  2639. break;
  2640. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
  2641. if (i == ARRAYLENGTH(sd->skillheal2))
  2642. { // Better mention this so the array length can be updated. [Skotlex]
  2643. ShowDebug("run_script: bonus2 bSkillHeal2 reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
  2644. break;
  2645. }
  2646. if (sd->skillheal2[i].id == type2)
  2647. sd->skillheal2[i].val += val;
  2648. else {
  2649. sd->skillheal2[i].id = type2;
  2650. sd->skillheal2[i].val = val;
  2651. }
  2652. break;
  2653. case SP_ADD_SKILL_BLOW:
  2654. if(sd->state.lr_flag == 2)
  2655. break;
  2656. ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
  2657. if (i == ARRAYLENGTH(sd->skillblown))
  2658. { //Better mention this so the array length can be updated. [Skotlex]
  2659. ShowDebug("run_script: bonus2 bSkillBlown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
  2660. break;
  2661. }
  2662. if(sd->skillblown[i].id == type2)
  2663. sd->skillblown[i].val += val;
  2664. else {
  2665. sd->skillblown[i].id = type2;
  2666. sd->skillblown[i].val = val;
  2667. }
  2668. break;
  2669. #ifndef RENEWAL_CAST
  2670. case SP_VARCASTRATE:
  2671. #endif
  2672. case SP_CASTRATE:
  2673. if(sd->state.lr_flag == 2)
  2674. break;
  2675. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2676. if (i == ARRAYLENGTH(sd->skillcast))
  2677. { //Better mention this so the array length can be updated. [Skotlex]
  2678. ShowDebug("run_script: bonus2 %s reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
  2679. #ifndef RENEWAL_CAST
  2680. "bCastRate",
  2681. #else
  2682. "bVariableCastrate",
  2683. #endif
  2684. ARRAYLENGTH(sd->skillcast), type2, val);
  2685. break;
  2686. }
  2687. if(sd->skillcast[i].id == type2)
  2688. sd->skillcast[i].val += val;
  2689. else {
  2690. sd->skillcast[i].id = type2;
  2691. sd->skillcast[i].val = val;
  2692. }
  2693. break;
  2694. case SP_FIXCASTRATE:
  2695. if(sd->state.lr_flag == 2)
  2696. break;
  2697. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
  2698. if (i == ARRAYLENGTH(sd->skillfixcastrate))
  2699. {
  2700. ShowDebug("run_script: bonus2 bFixedCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
  2701. break;
  2702. }
  2703. if(sd->skillfixcastrate[i].id == type2)
  2704. sd->skillfixcastrate[i].val += val;
  2705. else {
  2706. sd->skillfixcastrate[i].id = type2;
  2707. sd->skillfixcastrate[i].val = val;
  2708. }
  2709. break;
  2710. case SP_HP_LOSS_RATE:
  2711. if(sd->state.lr_flag != 2) {
  2712. sd->hp_loss.value = type2;
  2713. sd->hp_loss.rate = val;
  2714. }
  2715. break;
  2716. case SP_HP_REGEN_RATE:
  2717. if(sd->state.lr_flag != 2) {
  2718. sd->hp_regen.value = type2;
  2719. sd->hp_regen.rate = val;
  2720. }
  2721. break;
  2722. case SP_ADDRACE2:
  2723. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2724. break;
  2725. if(sd->state.lr_flag != 2)
  2726. sd->right_weapon.addrace2[type2] += val;
  2727. else
  2728. sd->left_weapon.addrace2[type2] += val;
  2729. break;
  2730. case SP_SUBSIZE:
  2731. if(sd->state.lr_flag != 2)
  2732. sd->subsize[type2]+=val;
  2733. break;
  2734. case SP_SUBRACE2:
  2735. if (!(type2 > RC2_NONE && type2 < RC2_MAX))
  2736. break;
  2737. if(sd->state.lr_flag != 2)
  2738. sd->subrace2[type2]+=val;
  2739. break;
  2740. case SP_ADD_ITEM_HEAL_RATE:
  2741. if(sd->state.lr_flag == 2)
  2742. break;
  2743. if (type2 < MAX_ITEMGROUP) { //Group bonus
  2744. sd->itemgrouphealrate[type2] += val;
  2745. break;
  2746. }
  2747. //Standard item bonus.
  2748. for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
  2749. if(i == ARRAYLENGTH(sd->itemhealrate)) {
  2750. ShowWarning("pc_bonus2: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
  2751. break;
  2752. }
  2753. sd->itemhealrate[i].nameid = type2;
  2754. sd->itemhealrate[i].rate += val;
  2755. break;
  2756. case SP_EXP_ADDRACE:
  2757. if(sd->state.lr_flag != 2)
  2758. sd->expaddrace[type2]+=val;
  2759. break;
  2760. case SP_SP_GAIN_RACE:
  2761. if(sd->state.lr_flag != 2)
  2762. sd->sp_gain_race[type2]+=val;
  2763. break;
  2764. case SP_ADD_MONSTER_DROP_ITEM:
  2765. if (sd->state.lr_flag != 2)
  2766. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2767. break;
  2768. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2769. if (sd->state.lr_flag != 2)
  2770. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, (1<<RC_BOSS)|(1<<RC_NONBOSS), val);
  2771. break;
  2772. case SP_SP_LOSS_RATE:
  2773. if(sd->state.lr_flag != 2) {
  2774. sd->sp_loss.value = type2;
  2775. sd->sp_loss.rate = val;
  2776. }
  2777. break;
  2778. case SP_SP_REGEN_RATE:
  2779. if(sd->state.lr_flag != 2) {
  2780. sd->sp_regen.value = type2;
  2781. sd->sp_regen.rate = val;
  2782. }
  2783. break;
  2784. case SP_HP_DRAIN_VALUE_RACE:
  2785. if(!sd->state.lr_flag) {
  2786. sd->right_weapon.hp_drain[type2].value += val;
  2787. }
  2788. else if(sd->state.lr_flag == 1) {
  2789. sd->left_weapon.hp_drain[type2].value += val;
  2790. }
  2791. break;
  2792. case SP_SP_DRAIN_VALUE_RACE:
  2793. if(!sd->state.lr_flag) {
  2794. sd->right_weapon.sp_drain[type2].value += val;
  2795. }
  2796. else if(sd->state.lr_flag == 1) {
  2797. sd->left_weapon.sp_drain[type2].value += val;
  2798. }
  2799. break;
  2800. case SP_IGNORE_MDEF_RATE:
  2801. if(sd->state.lr_flag != 2)
  2802. sd->ignore_mdef[type2] += val;
  2803. break;
  2804. case SP_IGNORE_DEF_RATE:
  2805. if(sd->state.lr_flag != 2)
  2806. sd->ignore_def[type2] += val;
  2807. break;
  2808. case SP_SP_GAIN_RACE_ATTACK:
  2809. if(sd->state.lr_flag != 2)
  2810. sd->sp_gain_race_attack[type2] = cap_value(sd->sp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2811. break;
  2812. case SP_HP_GAIN_RACE_ATTACK:
  2813. if(sd->state.lr_flag != 2)
  2814. sd->hp_gain_race_attack[type2] = cap_value(sd->hp_gain_race_attack[type2] + val, 0, INT16_MAX);
  2815. break;
  2816. case SP_SKILL_USE_SP_RATE: //bonus2 bSkillUseSPrate,n,x;
  2817. if(sd->state.lr_flag == 2)
  2818. break;
  2819. ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
  2820. if (i == ARRAYLENGTH(sd->skillusesprate)) {
  2821. ShowDebug("run_script: bonus2 bSkillUseSPrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
  2822. break;
  2823. }
  2824. if (sd->skillusesprate[i].id == type2)
  2825. sd->skillusesprate[i].val += val;
  2826. else {
  2827. sd->skillusesprate[i].id = type2;
  2828. sd->skillusesprate[i].val = val;
  2829. }
  2830. break;
  2831. case SP_SKILL_COOLDOWN:
  2832. if(sd->state.lr_flag == 2)
  2833. break;
  2834. ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
  2835. if (i == ARRAYLENGTH(sd->skillcooldown))
  2836. {
  2837. ShowDebug("run_script: bonus2 bSkillCoolDown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
  2838. break;
  2839. }
  2840. if (sd->skillcooldown[i].id == type2)
  2841. sd->skillcooldown[i].val += val;
  2842. else {
  2843. sd->skillcooldown[i].id = type2;
  2844. sd->skillcooldown[i].val = val;
  2845. }
  2846. break;
  2847. case SP_SKILL_FIXEDCAST:
  2848. if(sd->state.lr_flag == 2)
  2849. break;
  2850. ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
  2851. if (i == ARRAYLENGTH(sd->skillfixcast))
  2852. {
  2853. ShowDebug("run_script: bonus2 bSkillFixedCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
  2854. break;
  2855. }
  2856. if (sd->skillfixcast[i].id == type2)
  2857. sd->skillfixcast[i].val += val;
  2858. else {
  2859. sd->skillfixcast[i].id = type2;
  2860. sd->skillfixcast[i].val = val;
  2861. }
  2862. break;
  2863. case SP_SKILL_VARIABLECAST:
  2864. if(sd->state.lr_flag == 2)
  2865. break;
  2866. ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
  2867. if (i == ARRAYLENGTH(sd->skillvarcast))
  2868. {
  2869. ShowDebug("run_script: bonus2 bSkillVariableCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
  2870. break;
  2871. }
  2872. if (sd->skillvarcast[i].id == type2)
  2873. sd->skillvarcast[i].val += val;
  2874. else {
  2875. sd->skillvarcast[i].id = type2;
  2876. sd->skillvarcast[i].val = val;
  2877. }
  2878. break;
  2879. #ifdef RENEWAL_CAST
  2880. case SP_VARCASTRATE:
  2881. if(sd->state.lr_flag == 2)
  2882. break;
  2883. ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
  2884. if (i == ARRAYLENGTH(sd->skillcast))
  2885. {
  2886. ShowDebug("run_script: bonus2 bVariableCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",ARRAYLENGTH(sd->skillcast), type2, val);
  2887. break;
  2888. }
  2889. if(sd->skillcast[i].id == type2)
  2890. sd->skillcast[i].val -= val;
  2891. else {
  2892. sd->skillcast[i].id = type2;
  2893. sd->skillcast[i].val -= val;
  2894. }
  2895. break;
  2896. #endif
  2897. case SP_SKILL_USE_SP: //bonus2 bSkillUseSP,n,x;
  2898. if(sd->state.lr_flag == 2)
  2899. break;
  2900. ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
  2901. if (i == ARRAYLENGTH(sd->skillusesp)) {
  2902. ShowDebug("run_script: bonus2 bSkillUseSP reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
  2903. break;
  2904. }
  2905. if (sd->skillusesp[i].id == type2)
  2906. sd->skillusesp[i].val += val;
  2907. else {
  2908. sd->skillusesp[i].id = type2;
  2909. sd->skillusesp[i].val = val;
  2910. }
  2911. break;
  2912. default:
  2913. ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
  2914. break;
  2915. }
  2916. return 0;
  2917. }
  2918. int pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  2919. {
  2920. nullpo_ret(sd);
  2921. switch(type){
  2922. case SP_ADD_MONSTER_DROP_ITEM:
  2923. if(sd->state.lr_flag != 2)
  2924. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, 1<<type3, val);
  2925. break;
  2926. case SP_ADD_CLASS_DROP_ITEM:
  2927. if(sd->state.lr_flag != 2)
  2928. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, -type3, val);
  2929. break;
  2930. case SP_AUTOSPELL:
  2931. if(sd->state.lr_flag != 2)
  2932. {
  2933. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2934. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2935. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
  2936. target?-type2:type2, type3, val, 0, current_equip_card_id);
  2937. }
  2938. break;
  2939. case SP_AUTOSPELL_WHENHIT:
  2940. if(sd->state.lr_flag != 2)
  2941. {
  2942. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  2943. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  2944. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
  2945. target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  2946. }
  2947. break;
  2948. case SP_SP_DRAIN_RATE:
  2949. if(!sd->state.lr_flag) {
  2950. sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2951. sd->right_weapon.sp_drain[RC_NONBOSS].per += type3;
  2952. sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
  2953. sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
  2954. sd->right_weapon.sp_drain[RC_BOSS].per += type3;
  2955. sd->right_weapon.sp_drain[RC_BOSS].type = val;
  2956. }
  2957. else if(sd->state.lr_flag == 1) {
  2958. sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
  2959. sd->left_weapon.sp_drain[RC_NONBOSS].per += type3;
  2960. sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
  2961. sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
  2962. sd->left_weapon.sp_drain[RC_BOSS].per += type3;
  2963. sd->left_weapon.sp_drain[RC_BOSS].type = val;
  2964. }
  2965. break;
  2966. case SP_HP_DRAIN_RATE_RACE:
  2967. if(!sd->state.lr_flag) {
  2968. sd->right_weapon.hp_drain[type2].rate += type3;
  2969. sd->right_weapon.hp_drain[type2].per += val;
  2970. }
  2971. else if(sd->state.lr_flag == 1) {
  2972. sd->left_weapon.hp_drain[type2].rate += type3;
  2973. sd->left_weapon.hp_drain[type2].per += val;
  2974. }
  2975. break;
  2976. case SP_SP_DRAIN_RATE_RACE:
  2977. if(!sd->state.lr_flag) {
  2978. sd->right_weapon.sp_drain[type2].rate += type3;
  2979. sd->right_weapon.sp_drain[type2].per += val;
  2980. }
  2981. else if(sd->state.lr_flag == 1) {
  2982. sd->left_weapon.sp_drain[type2].rate += type3;
  2983. sd->left_weapon.sp_drain[type2].per += val;
  2984. }
  2985. break;
  2986. case SP_ADD_MONSTER_DROP_ITEMGROUP:
  2987. if (sd->state.lr_flag != 2)
  2988. pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, 1<<type3, val);
  2989. break;
  2990. case SP_ADDEFF:
  2991. if (type2 > SC_MAX) {
  2992. ShowWarning("pc_bonus3 (Add Effect): %d is not supported.\n", type2);
  2993. break;
  2994. }
  2995. pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
  2996. sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val);
  2997. break;
  2998. case SP_ADDEFF_WHENHIT:
  2999. if (type2 > SC_MAX) {
  3000. ShowWarning("pc_bonus3 (Add Effect when hit): %d is not supported.\n", type2);
  3001. break;
  3002. }
  3003. if(sd->state.lr_flag != 2)
  3004. pc_bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val);
  3005. break;
  3006. case SP_ADDEFF_ONSKILL:
  3007. if( type3 > SC_MAX ) {
  3008. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type3);
  3009. break;
  3010. }
  3011. if( sd->state.lr_flag != 2 )
  3012. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
  3013. break;
  3014. case SP_ADDELE:
  3015. if (type2 > ELE_MAX) {
  3016. ShowWarning("pc_bonus3 (SP_ADDELE): element %d is out of range.\n", type2);
  3017. break;
  3018. }
  3019. if (sd->state.lr_flag != 2)
  3020. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3021. break;
  3022. case SP_SUBELE:
  3023. if (type2 > ELE_MAX) {
  3024. ShowWarning("pc_bonus3 (SP_SUBELE): element %d is out of range.\n", type2);
  3025. break;
  3026. }
  3027. if (sd->state.lr_flag != 2)
  3028. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3029. break;
  3030. default:
  3031. ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
  3032. break;
  3033. }
  3034. return 0;
  3035. }
  3036. int pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3037. {
  3038. nullpo_ret(sd);
  3039. switch(type){
  3040. case SP_AUTOSPELL:
  3041. if(sd->state.lr_flag != 2)
  3042. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3043. break;
  3044. case SP_AUTOSPELL_WHENHIT:
  3045. if(sd->state.lr_flag != 2)
  3046. 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);
  3047. break;
  3048. case SP_AUTOSPELL_ONSKILL:
  3049. if(sd->state.lr_flag != 2)
  3050. {
  3051. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3052. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3053. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
  3054. }
  3055. break;
  3056. case SP_ADDEFF_ONSKILL:
  3057. if( type2 > SC_MAX ) {
  3058. ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type2);
  3059. break;
  3060. }
  3061. if( sd->state.lr_flag != 2 )
  3062. pc_bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
  3063. break;
  3064. default:
  3065. ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
  3066. break;
  3067. }
  3068. return 0;
  3069. }
  3070. int pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3071. {
  3072. nullpo_ret(sd);
  3073. switch(type){
  3074. case SP_AUTOSPELL:
  3075. if(sd->state.lr_flag != 2)
  3076. pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3077. break;
  3078. case SP_AUTOSPELL_WHENHIT:
  3079. if(sd->state.lr_flag != 2)
  3080. pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3081. break;
  3082. case SP_AUTOSPELL_ONSKILL:
  3083. if(sd->state.lr_flag != 2)
  3084. pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3085. break;
  3086. default:
  3087. ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
  3088. break;
  3089. }
  3090. return 0;
  3091. }
  3092. /*==========================================
  3093. * Grants a player a given skill. Flag values are:
  3094. * 0 - Grant skill unconditionally and forever (only this one invokes status_calc_pc,
  3095. * as the other two are assumed to be invoked from within it)
  3096. * 1 - Grant an item skill (temporary)
  3097. * 2 - Like 1, except the level granted can stack with previously learned level.
  3098. *------------------------------------------*/
  3099. int pc_skill(TBL_PC* sd, int id, int level, int flag)
  3100. {
  3101. nullpo_ret(sd);
  3102. if( id <= 0 || id >= MAX_SKILL || skill_db[id].name == NULL) {
  3103. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", id);
  3104. return 0;
  3105. }
  3106. if( level > MAX_SKILL_LEVEL ) {
  3107. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3108. return 0;
  3109. }
  3110. if( flag == 2 && sd->status.skill[id].lv + level > MAX_SKILL_LEVEL ) {
  3111. ShowError("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d\n", level, MAX_SKILL_LEVEL, sd->status.skill[id].lv);
  3112. return 0;
  3113. }
  3114. switch( flag ){
  3115. case 0: //Set skill data overwriting whatever was there before.
  3116. sd->status.skill[id].id = id;
  3117. sd->status.skill[id].lv = level;
  3118. sd->status.skill[id].flag = SKILL_FLAG_PERM_GRANTED;
  3119. if( level == 0 ) //Remove skill.
  3120. {
  3121. sd->status.skill[id].id = 0;
  3122. clif_deleteskill(sd,id);
  3123. }
  3124. else
  3125. clif_addskill(sd,id);
  3126. if( !skill_get_inf(id) ) //Only recalculate for passive skills.
  3127. status_calc_pc(sd, 0);
  3128. break;
  3129. case 1: //Item bonus skill.
  3130. if( sd->status.skill[id].id == id ){
  3131. if( sd->status.skill[id].lv >= level )
  3132. return 0;
  3133. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT ) //Non-granted skill, store it's level.
  3134. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv;
  3135. } else {
  3136. sd->status.skill[id].id = id;
  3137. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY;
  3138. }
  3139. sd->status.skill[id].lv = level;
  3140. break;
  3141. case 2: //Add skill bonus on top of what you had.
  3142. if( sd->status.skill[id].id == id ){
  3143. if( sd->status.skill[id].flag == SKILL_FLAG_PERMANENT )
  3144. sd->status.skill[id].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[id].lv; // Store previous level.
  3145. } else {
  3146. sd->status.skill[id].id = id;
  3147. sd->status.skill[id].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3148. }
  3149. sd->status.skill[id].lv += level;
  3150. break;
  3151. default: //Unknown flag?
  3152. return 0;
  3153. }
  3154. return 1;
  3155. }
  3156. /*==========================================
  3157. * Append a card to an item ?
  3158. *------------------------------------------*/
  3159. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3160. {
  3161. int i;
  3162. int nameid;
  3163. nullpo_ret(sd);
  3164. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3165. return 0; //Invalid item index.
  3166. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3167. return 0; //Invalid card index.
  3168. if( sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1 )
  3169. return 0; // target item missing
  3170. if( sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1 )
  3171. return 0; // target card missing
  3172. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3173. return 0; // only weapons and armor are allowed
  3174. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3175. return 0; // must be a card
  3176. if( sd->status.inventory[idx_equip].identify == 0 )
  3177. return 0; // target must be identified
  3178. if( itemdb_isspecial(sd->status.inventory[idx_equip].card[0]) )
  3179. return 0; // card slots reserved for other purposes
  3180. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3181. return 0; // card cannot be compounded on this item type
  3182. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3183. return 0; // attempted to place shield card on left-hand weapon.
  3184. if( sd->status.inventory[idx_equip].equip != 0 )
  3185. return 0; // item must be unequipped
  3186. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0 );
  3187. if( i == sd->inventory_data[idx_equip]->slot )
  3188. return 0; // no free slots
  3189. // remember the card id to insert
  3190. nameid = sd->status.inventory[idx_card].nameid;
  3191. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3192. {// failed
  3193. clif_insert_card(sd,idx_equip,idx_card,1);
  3194. }
  3195. else
  3196. {// success
  3197. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->status.inventory[idx_equip]);
  3198. sd->status.inventory[idx_equip].card[i] = nameid;
  3199. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->status.inventory[idx_equip]);
  3200. clif_insert_card(sd,idx_equip,idx_card,0);
  3201. }
  3202. return 0;
  3203. }
  3204. //
  3205. // Items
  3206. //
  3207. /*==========================================
  3208. * Update buying value by skills
  3209. *------------------------------------------*/
  3210. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  3211. {
  3212. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  3213. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  3214. rate1 = 5+skill*2-((skill==10)? 1:0);
  3215. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  3216. rate2 = 5+skill*4;
  3217. if(rate1 < rate2) rate1 = rate2;
  3218. if(rate1)
  3219. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  3220. if(val < 0) val = 0;
  3221. if(orig_value > 0 && val < 1) val = 1;
  3222. return val;
  3223. }
  3224. /*==========================================
  3225. * Update selling value by skills
  3226. *------------------------------------------*/
  3227. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  3228. {
  3229. int skill,val = orig_value,rate = 0;
  3230. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  3231. rate = 5+skill*2-((skill==10)? 1:0);
  3232. if(rate)
  3233. val = (int)((double)orig_value*(double)(100+rate)/100.);
  3234. if(val < 0) val = 0;
  3235. if(orig_value > 0 && val < 1) val = 1;
  3236. return val;
  3237. }
  3238. /*==========================================
  3239. * Checking if we have enough place on inventory for new item
  3240. * Make sure to take 30k as limit (for client I guess)
  3241. *------------------------------------------*/
  3242. int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
  3243. {
  3244. int i;
  3245. struct item_data* data;
  3246. nullpo_ret(sd);
  3247. if(amount > MAX_AMOUNT)
  3248. return ADDITEM_OVERAMOUNT;
  3249. data = itemdb_search(nameid);
  3250. if(!itemdb_isstackable2(data))
  3251. return ADDITEM_NEW;
  3252. if( data->stack.inventory && amount > data->stack.amount )
  3253. return ADDITEM_OVERAMOUNT;
  3254. for(i=0;i<MAX_INVENTORY;i++){
  3255. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  3256. if(sd->status.inventory[i].nameid==nameid){
  3257. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3258. return ADDITEM_OVERAMOUNT;
  3259. return ADDITEM_EXIST;
  3260. }
  3261. }
  3262. return ADDITEM_NEW;
  3263. }
  3264. /*==========================================
  3265. * Return number of available place in inventory
  3266. * Each non stackable item will reduce place by 1
  3267. *------------------------------------------*/
  3268. int pc_inventoryblank(struct map_session_data *sd)
  3269. {
  3270. int i,b;
  3271. nullpo_ret(sd);
  3272. for(i=0,b=0;i<MAX_INVENTORY;i++){
  3273. if(sd->status.inventory[i].nameid==0)
  3274. b++;
  3275. }
  3276. return b;
  3277. }
  3278. /*==========================================
  3279. * attempts to remove zeny from player (sd)
  3280. *------------------------------------------*/
  3281. int pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3282. {
  3283. nullpo_retr(-1,sd);
  3284. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3285. if( zeny < 0 )
  3286. {
  3287. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3288. return 1;
  3289. }
  3290. if( sd->status.zeny < zeny )
  3291. return 1; //Not enough.
  3292. sd->status.zeny -= zeny;
  3293. clif_updatestatus(sd,SP_ZENY);
  3294. if(!tsd) tsd = sd;
  3295. log_zeny(sd, type, tsd, -zeny);
  3296. if( zeny > 0 && sd->state.showzeny ) {
  3297. char output[255];
  3298. sprintf(output, "Removed %dz.", zeny);
  3299. clif_disp_onlyself(sd,output,strlen(output));
  3300. }
  3301. return 0;
  3302. }
  3303. /*==========================================
  3304. * Cash Shop
  3305. *------------------------------------------*/
  3306. int pc_paycash(struct map_session_data *sd, int price, int points)
  3307. {
  3308. int cash;
  3309. nullpo_retr(-1,sd);
  3310. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3311. if( price < 0 || points < 0 )
  3312. {
  3313. 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);
  3314. return -2;
  3315. }
  3316. if( points > price )
  3317. {
  3318. 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);
  3319. points = price;
  3320. }
  3321. cash = price-points;
  3322. if( sd->cashPoints < cash || sd->kafraPoints < points )
  3323. {
  3324. 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);
  3325. return -1;
  3326. }
  3327. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints-cash);
  3328. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints-points);
  3329. if( battle_config.cashshop_show_points )
  3330. {
  3331. char output[128];
  3332. sprintf(output, msg_txt(504), points, cash, sd->kafraPoints, sd->cashPoints);
  3333. clif_disp_onlyself(sd, output, strlen(output));
  3334. }
  3335. return cash+points;
  3336. }
  3337. int pc_getcash(struct map_session_data *sd, int cash, int points)
  3338. {
  3339. char output[128];
  3340. nullpo_retr(-1,sd);
  3341. cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3342. points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3343. if( cash > 0 )
  3344. {
  3345. if( cash > MAX_ZENY-sd->cashPoints )
  3346. {
  3347. 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);
  3348. cash = MAX_ZENY-sd->cashPoints;
  3349. }
  3350. pc_setaccountreg(sd, "#CASHPOINTS", sd->cashPoints+cash);
  3351. if( battle_config.cashshop_show_points )
  3352. {
  3353. sprintf(output, msg_txt(505), cash, sd->cashPoints);
  3354. clif_disp_onlyself(sd, output, strlen(output));
  3355. }
  3356. return cash;
  3357. }
  3358. else if( cash < 0 )
  3359. {
  3360. 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);
  3361. return -1;
  3362. }
  3363. if( points > 0 )
  3364. {
  3365. if( points > MAX_ZENY-sd->kafraPoints )
  3366. {
  3367. 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);
  3368. points = MAX_ZENY-sd->kafraPoints;
  3369. }
  3370. pc_setaccountreg(sd, "#KAFRAPOINTS", sd->kafraPoints+points);
  3371. if( battle_config.cashshop_show_points )
  3372. {
  3373. sprintf(output, msg_txt(506), points, sd->kafraPoints);
  3374. clif_disp_onlyself(sd, output, strlen(output));
  3375. }
  3376. return points;
  3377. }
  3378. else if( points < 0 )
  3379. {
  3380. 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);
  3381. return -1;
  3382. }
  3383. return -2; //shouldn't happen but jsut in case
  3384. }
  3385. /*==========================================
  3386. * Attempts to give zeny to player (sd)
  3387. * tsd (optional) from who for log (if null take sd)
  3388. *------------------------------------------*/
  3389. int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  3390. {
  3391. nullpo_retr(-1,sd);
  3392. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  3393. if( zeny < 0 )
  3394. {
  3395. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  3396. return 1;
  3397. }
  3398. if( zeny > MAX_ZENY - sd->status.zeny )
  3399. zeny = MAX_ZENY - sd->status.zeny;
  3400. sd->status.zeny += zeny;
  3401. clif_updatestatus(sd,SP_ZENY);
  3402. if(!tsd) tsd = sd;
  3403. log_zeny(sd, type, tsd, zeny);
  3404. if( zeny > 0 && sd->state.showzeny ) {
  3405. char output[255];
  3406. sprintf(output, "Gained %dz.", zeny);
  3407. clif_disp_onlyself(sd,output,strlen(output));
  3408. }
  3409. return 0;
  3410. }
  3411. /*==========================================
  3412. * Searching a specified itemid in inventory and return his stored index
  3413. *------------------------------------------*/
  3414. int pc_search_inventory(struct map_session_data *sd,int item_id)
  3415. {
  3416. int i;
  3417. nullpo_retr(-1, sd);
  3418. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
  3419. return ( i < MAX_INVENTORY ) ? i : -1;
  3420. }
  3421. /*==========================================
  3422. * Attempt to add a new item to inventory.
  3423. * Return:
  3424. 0 = success
  3425. 1 = invalid itemid not found or negative amount
  3426. 2 = overweight
  3427. 3 = ?
  3428. 4 = no free place found
  3429. 5 = max amount reached
  3430. 6 = ?
  3431. 7 = stack limitation
  3432. *------------------------------------------*/
  3433. int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3434. {
  3435. struct item_data *data;
  3436. int i;
  3437. unsigned int w;
  3438. nullpo_retr(1, sd);
  3439. nullpo_retr(1, item_data);
  3440. if( item_data->nameid <= 0 || amount <= 0 )
  3441. return 1;
  3442. if( amount > MAX_AMOUNT )
  3443. return 5;
  3444. data = itemdb_search(item_data->nameid);
  3445. if( data->stack.inventory && amount > data->stack.amount )
  3446. {// item stack limitation
  3447. return 7;
  3448. }
  3449. w = data->weight*amount;
  3450. if(sd->weight + w > sd->max_weight)
  3451. return 2;
  3452. i = MAX_INVENTORY;
  3453. if( itemdb_isstackable2(data) && item_data->expire_time == 0 )
  3454. { // Stackable | Non Rental
  3455. for( i = 0; i < MAX_INVENTORY; i++ )
  3456. {
  3457. if( sd->status.inventory[i].nameid == item_data->nameid && memcmp(&sd->status.inventory[i].card, &item_data->card, sizeof(item_data->card)) == 0 )
  3458. {
  3459. if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
  3460. return 5;
  3461. sd->status.inventory[i].amount += amount;
  3462. clif_additem(sd,i,amount,0);
  3463. break;
  3464. }
  3465. }
  3466. }
  3467. if( i >= MAX_INVENTORY )
  3468. {
  3469. i = pc_search_inventory(sd,0);
  3470. if( i < 0 )
  3471. return 4;
  3472. memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
  3473. // clear equips field first, just in case
  3474. if( item_data->equip )
  3475. sd->status.inventory[i].equip = 0;
  3476. sd->status.inventory[i].amount = amount;
  3477. sd->inventory_data[i] = data;
  3478. clif_additem(sd,i,amount,0);
  3479. }
  3480. #ifdef NSI_UNIQUE_ID
  3481. if( !itemdb_isstackable2(data) && !item_data->unique_id )
  3482. sd->status.inventory[i].unique_id = itemdb_unique_id(0,0);
  3483. #endif
  3484. log_pick_pc(sd, log_type, amount, &sd->status.inventory[i]);
  3485. sd->weight += w;
  3486. clif_updatestatus(sd,SP_WEIGHT);
  3487. //Auto-equip
  3488. if(data->flag.autoequip)
  3489. pc_equipitem(sd, i, data->equip);
  3490. /* rental item check */
  3491. if( item_data->expire_time ) {
  3492. if( time(NULL) > item_data->expire_time ) {
  3493. clif_rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
  3494. pc_delitem(sd, i, sd->status.inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  3495. } else {
  3496. int seconds = (int)( item_data->expire_time - time(NULL) );
  3497. clif_rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
  3498. pc_inventory_rental_add(sd, seconds);
  3499. }
  3500. }
  3501. return 0;
  3502. }
  3503. /*==========================================
  3504. * Remove an item at index n from inventory by amount.
  3505. * Parameters :
  3506. * @type
  3507. * 1 : don't notify deletion
  3508. * 2 : don't notify weight change
  3509. * Return:
  3510. * 0 = success
  3511. * 1 = invalid itemid or negative amount
  3512. *------------------------------------------*/
  3513. int pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  3514. {
  3515. nullpo_retr(1, sd);
  3516. if(sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  3517. return 1;
  3518. log_pick_pc(sd, log_type, -amount, &sd->status.inventory[n]);
  3519. sd->status.inventory[n].amount -= amount;
  3520. sd->weight -= sd->inventory_data[n]->weight*amount ;
  3521. if( sd->status.inventory[n].amount <= 0 ){
  3522. if(sd->status.inventory[n].equip)
  3523. pc_unequipitem(sd,n,3);
  3524. memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
  3525. sd->inventory_data[n] = NULL;
  3526. }
  3527. if(!(type&1))
  3528. clif_delitem(sd,n,amount,reason);
  3529. if(!(type&2))
  3530. clif_updatestatus(sd,SP_WEIGHT);
  3531. return 0;
  3532. }
  3533. /*==========================================
  3534. * Attempt to drop an item.
  3535. * Return:
  3536. * 0 = fail
  3537. * 1 = success
  3538. *------------------------------------------*/
  3539. int pc_dropitem(struct map_session_data *sd,int n,int amount)
  3540. {
  3541. nullpo_retr(1, sd);
  3542. if(n < 0 || n >= MAX_INVENTORY)
  3543. return 0;
  3544. if(amount <= 0)
  3545. return 0;
  3546. if(sd->status.inventory[n].nameid <= 0 ||
  3547. sd->status.inventory[n].amount <= 0 ||
  3548. sd->status.inventory[n].amount < amount ||
  3549. sd->state.trading || sd->state.vending ||
  3550. !sd->inventory_data[n] //pc_delitem would fail on this case.
  3551. )
  3552. return 0;
  3553. if( map[sd->bl.m].flag.nodrop )
  3554. {
  3555. clif_displaymessage (sd->fd, msg_txt(271));
  3556. return 0; //Can't drop items in nodrop mapflag maps.
  3557. }
  3558. if( !pc_candrop(sd,&sd->status.inventory[n]) )
  3559. {
  3560. clif_displaymessage (sd->fd, msg_txt(263));
  3561. return 0;
  3562. }
  3563. if (!map_addflooritem(&sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
  3564. return 0;
  3565. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  3566. clif_dropitem(sd, n, amount);
  3567. return 1;
  3568. }
  3569. /*==========================================
  3570. * Attempt to pick up an item.
  3571. * Return:
  3572. * 0 = fail
  3573. * 1 = success
  3574. *------------------------------------------*/
  3575. int pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  3576. {
  3577. int flag=0;
  3578. unsigned int tick = gettick();
  3579. struct map_session_data *first_sd = NULL,*second_sd = NULL,*third_sd = NULL;
  3580. struct party_data *p=NULL;
  3581. nullpo_ret(sd);
  3582. nullpo_ret(fitem);
  3583. if(!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  3584. return 0; // Distance is too far
  3585. if (sd->status.party_id)
  3586. p = party_search(sd->status.party_id);
  3587. if(fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id)
  3588. {
  3589. first_sd = map_charid2sd(fitem->first_get_charid);
  3590. if(DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  3591. if (!(p && p->party.item&1 &&
  3592. first_sd && first_sd->status.party_id == sd->status.party_id
  3593. ))
  3594. return 0;
  3595. }
  3596. else
  3597. if(fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id)
  3598. {
  3599. second_sd = map_charid2sd(fitem->second_get_charid);
  3600. if(DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  3601. if(!(p && p->party.item&1 &&
  3602. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3603. (second_sd && second_sd->status.party_id == sd->status.party_id))
  3604. ))
  3605. return 0;
  3606. }
  3607. else
  3608. if(fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id)
  3609. {
  3610. third_sd = map_charid2sd(fitem->third_get_charid);
  3611. if(DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  3612. if(!(p && p->party.item&1 &&
  3613. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  3614. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  3615. (third_sd && third_sd->status.party_id == sd->status.party_id))
  3616. ))
  3617. return 0;
  3618. }
  3619. }
  3620. }
  3621. }
  3622. //This function takes care of giving the item to whoever should have it, considering party-share options.
  3623. if ((flag = party_share_loot(p,sd,&fitem->item_data, fitem->first_get_charid))) {
  3624. clif_additem(sd,0,0,flag);
  3625. return 1;
  3626. }
  3627. //Display pickup animation.
  3628. pc_stop_attack(sd);
  3629. clif_takeitem(&sd->bl,&fitem->bl);
  3630. map_clearflooritem(&fitem->bl);
  3631. return 1;
  3632. }
  3633. /*==========================================
  3634. * Check if item is usable.
  3635. * Return:
  3636. * 0 = no
  3637. * 1 = yes
  3638. *------------------------------------------*/
  3639. int pc_isUseitem(struct map_session_data *sd,int n)
  3640. {
  3641. struct item_data *item;
  3642. int nameid;
  3643. nullpo_ret(sd);
  3644. item = sd->inventory_data[n];
  3645. nameid = sd->status.inventory[n].nameid;
  3646. if( item == NULL )
  3647. return 0;
  3648. //Not consumable item
  3649. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  3650. return 0;
  3651. if( !item->script ) //if it has no script, you can't really consume it!
  3652. return 0;
  3653. switch( nameid ) //@TODO, lot oh harcoded nameid here
  3654. {
  3655. case 605: // Anodyne
  3656. if( map_flag_gvg(sd->bl.m) )
  3657. return 0;
  3658. case 606:
  3659. if( pc_issit(sd) )
  3660. return 0;
  3661. break;
  3662. case 601: // Fly Wing
  3663. case 12212: // Giant Fly Wing
  3664. if( map[sd->bl.m].flag.noteleport || map_flag_gvg(sd->bl.m) )
  3665. {
  3666. clif_skill_teleportmessage(sd,0);
  3667. return 0;
  3668. }
  3669. case 602: // ButterFly Wing
  3670. case 14527: // Dungeon Teleport Scroll
  3671. case 14581: // Dungeon Teleport Scroll
  3672. case 14582: // Yellow Butterfly Wing
  3673. case 14583: // Green Butterfly Wing
  3674. case 14584: // Red Butterfly Wing
  3675. case 14585: // Blue Butterfly Wing
  3676. case 14591: // Siege Teleport Scroll
  3677. if( sd->duel_group && !battle_config.duel_allow_teleport )
  3678. {
  3679. clif_displaymessage(sd->fd, msg_txt(663));
  3680. return 0;
  3681. }
  3682. if( nameid != 601 && nameid != 12212 && map[sd->bl.m].flag.noreturn )
  3683. return 0;
  3684. break;
  3685. case 604: // Dead Branch
  3686. case 12024: // Red Pouch
  3687. case 12103: // Bloody Branch
  3688. case 12109: // Poring Box
  3689. if( map[sd->bl.m].flag.nobranch || map_flag_gvg(sd->bl.m) )
  3690. return 0;
  3691. break;
  3692. case 12210: // Bubble Gum
  3693. case 12264: // Comp Bubble Gum
  3694. if( sd->sc.data[SC_ITEMBOOST] )
  3695. return 0;
  3696. break;
  3697. case 12208: // Battle Manual
  3698. case 12263: // Comp Battle Manual
  3699. case 12312: // Thick Battle Manual
  3700. case 12705: // Noble Nameplate
  3701. case 14532: // Battle_Manual25
  3702. case 14533: // Battle_Manual100
  3703. case 14545: // Battle_Manual300
  3704. if( sd->sc.data[SC_EXPBOOST] )
  3705. return 0;
  3706. break;
  3707. case 14592: // JOB_Battle_Manual
  3708. if( sd->sc.data[SC_JEXPBOOST] )
  3709. return 0;
  3710. break;
  3711. // Mercenary Items
  3712. case 12184: // Mercenary's Red Potion
  3713. case 12185: // Mercenary's Blue Potion
  3714. case 12241: // Mercenary's Concentration Potion
  3715. case 12242: // Mercenary's Awakening Potion
  3716. case 12243: // Mercenary's Berserk Potion
  3717. if( sd->md == NULL || sd->md->db == NULL )
  3718. return 0;
  3719. if (sd->md->sc.data[SC_BERSERK] || sd->md->sc.data[SC_SATURDAYNIGHTFEVER] || sd->md->sc.data[SC__BLOODYLUST])
  3720. return 0;
  3721. if( nameid == 12242 && sd->md->db->lv < 40 )
  3722. return 0;
  3723. if( nameid == 12243 && sd->md->db->lv < 80 )
  3724. return 0;
  3725. break;
  3726. case 12213: //Neuralizer
  3727. if( !map[sd->bl.m].flag.reset )
  3728. return 0;
  3729. break;
  3730. }
  3731. if( nameid >= 12153 && nameid <= 12182 && sd->md != NULL )
  3732. return 0; // Mercenary Scrolls
  3733. /**
  3734. * Only Rune Knights may use runes
  3735. **/
  3736. if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  3737. return 0;
  3738. /**
  3739. * Only GCross may use poisons
  3740. **/
  3741. else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
  3742. return 0;
  3743. //Gender check
  3744. if(item->sex != 2 && sd->status.sex != item->sex)
  3745. return 0;
  3746. //Required level check
  3747. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  3748. return 0;
  3749. #ifdef RENEWAL
  3750. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  3751. return 0;
  3752. #endif
  3753. //Not equipable by class. [Skotlex]
  3754. if (!(
  3755. (1<<(sd->class_&MAPID_BASEMASK)) &
  3756. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  3757. ))
  3758. return 0;
  3759. //Not usable by upper class. [Inkfish]
  3760. while( 1 ) {
  3761. if( item->class_upper&1 && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
  3762. if( item->class_upper&2 && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
  3763. if( item->class_upper&4 && sd->class_&JOBL_BABY ) break;
  3764. if( item->class_upper&8 && sd->class_&JOBL_THIRD ) break;
  3765. return 0;
  3766. }
  3767. //Dead Branch & Bloody Branch & Porings Box
  3768. // FIXME: outdated, use constants or database
  3769. if( nameid == 604 || nameid == 12103 || nameid == 12109 )
  3770. log_branch(sd);
  3771. return 1;
  3772. }
  3773. /*==========================================
  3774. * Last checks to use an item.
  3775. * Return:
  3776. * 0 = fail
  3777. * 1 = success
  3778. *------------------------------------------*/
  3779. int pc_useitem(struct map_session_data *sd,int n)
  3780. {
  3781. unsigned int tick = gettick();
  3782. int amount, nameid;
  3783. struct script_code *script;
  3784. nullpo_ret(sd);
  3785. if( sd->npc_id ){
  3786. #ifdef RENEWAL
  3787. clif_msg(sd, 0x783); // TODO look for the client date that has this message.
  3788. #endif
  3789. return 0;
  3790. }
  3791. if( sd->status.inventory[n].nameid <= 0 || sd->status.inventory[n].amount <= 0 )
  3792. return 0;
  3793. if( !pc_isUseitem(sd,n) )
  3794. return 0;
  3795. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  3796. nameid = sd->inventory_data[n]->nameid;
  3797. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  3798. return 0;
  3799. if (sd->sc.count && (
  3800. sd->sc.data[SC_BERSERK] || sd->sc.data[SC__BLOODYLUST] ||
  3801. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  3802. sd->sc.data[SC_TRICKDEAD] ||
  3803. sd->sc.data[SC_HIDING] ||
  3804. sd->sc.data[SC__SHADOWFORM] ||
  3805. sd->sc.data[SC__MANHOLE] ||
  3806. sd->sc.data[SC_KAGEHUMI] ||
  3807. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM)
  3808. ))
  3809. return 0;
  3810. //Prevent mass item usage. [Skotlex]
  3811. if( DIFF_TICK(sd->canuseitem_tick, tick) > 0 ||
  3812. (itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick, tick) > 0)
  3813. )
  3814. return 0;
  3815. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  3816. if( sd->inventory_data[n]->flag.delay_consume ) {
  3817. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.option&OPTION_MOUNTING )
  3818. return 0;
  3819. else if( pc_issit(sd) )
  3820. return 0;
  3821. }
  3822. //Since most delay-consume items involve using a "skill-type" target cursor,
  3823. //perform a skill-use check before going through. [Skotlex]
  3824. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  3825. //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
  3826. if( sd->inventory_data[n]->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  3827. return 0;
  3828. if( sd->inventory_data[n]->delay > 0 ) {
  3829. int i;
  3830. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
  3831. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  3832. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  3833. if( i < MAX_ITEMDELAYS ) {
  3834. if( sd->item_delay[i].nameid ) {// found
  3835. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  3836. int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  3837. char e_msg[100];
  3838. if( e_tick > 99 )
  3839. sprintf(e_msg,"Item Failed. [%s] is cooling down. wait %.1f minutes.",
  3840. itemdb_jname(sd->status.inventory[n].nameid),
  3841. (double)e_tick / 60);
  3842. else
  3843. sprintf(e_msg,"Item Failed. [%s] is cooling down. wait %d seconds.",
  3844. itemdb_jname(sd->status.inventory[n].nameid),
  3845. e_tick+1);
  3846. clif_colormes(sd,COLOR_RED,e_msg);
  3847. return 0; // Delay has not expired yet
  3848. }
  3849. } else {// not yet used item (all slots are initially empty)
  3850. sd->item_delay[i].nameid = nameid;
  3851. }
  3852. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  3853. } else {// should not happen
  3854. ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%d, char_id=%d)\n", nameid, sd->status.char_id);
  3855. }
  3856. //clean up used delays so we can give room for more
  3857. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  3858. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  3859. sd->item_delay[i].tick = 0;
  3860. sd->item_delay[i].nameid = 0;
  3861. }
  3862. }
  3863. }
  3864. /* on restricted maps the item is consumed but the effect is not used */
  3865. if (
  3866. (!map_flag_vs(sd->bl.m) && sd->inventory_data[n]->flag.no_equip&1) || // Normal
  3867. (map[sd->bl.m].flag.pvp && sd->inventory_data[n]->flag.no_equip&2) || // PVP
  3868. (map_flag_gvg(sd->bl.m) && sd->inventory_data[n]->flag.no_equip&4) || // GVG
  3869. (map[sd->bl.m].flag.battleground && sd->inventory_data[n]->flag.no_equip&8) || // Battleground
  3870. (map[sd->bl.m].flag.restricted && sd->inventory_data[n]->flag.no_equip&(8*map[sd->bl.m].zone)) // Zone restriction
  3871. ) {
  3872. if( battle_config.item_restricted_consumption_type ) {
  3873. clif_useitemack(sd,n,sd->status.inventory[n].amount-1,true);
  3874. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  3875. }
  3876. return 0;/* regardless, effect is not run */
  3877. }
  3878. sd->itemid = sd->status.inventory[n].nameid;
  3879. sd->itemindex = n;
  3880. if(sd->catch_target_class != -1) //Abort pet catching.
  3881. sd->catch_target_class = -1;
  3882. amount = sd->status.inventory[n].amount;
  3883. script = sd->inventory_data[n]->script;
  3884. //Check if the item is to be consumed immediately [Skotlex]
  3885. if( sd->inventory_data[n]->flag.delay_consume )
  3886. clif_useitemack(sd,n,amount,true);
  3887. else {
  3888. if( sd->status.inventory[n].expire_time == 0 ) {
  3889. clif_useitemack(sd,n,amount-1,true);
  3890. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  3891. } else
  3892. clif_useitemack(sd,n,0,false);
  3893. }
  3894. if(sd->status.inventory[n].card[0]==CARD0_CREATE &&
  3895. pc_famerank(MakeDWord(sd->status.inventory[n].card[2],sd->status.inventory[n].card[3]), MAPID_ALCHEMIST))
  3896. {
  3897. potion_flag = 2; // Famous player's potions have 50% more efficiency
  3898. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  3899. potion_flag = 3; //Even more effective potions.
  3900. }
  3901. //Update item use time.
  3902. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  3903. if( itemdb_iscashfood(nameid) )
  3904. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  3905. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  3906. potion_flag = 0;
  3907. return 1;
  3908. }
  3909. /*==========================================
  3910. * Add item on cart for given index.
  3911. * Return:
  3912. * 0 = success
  3913. * 1 = fail
  3914. *------------------------------------------*/
  3915. int pc_cart_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
  3916. {
  3917. struct item_data *data;
  3918. int i,w;
  3919. nullpo_retr(1, sd);
  3920. nullpo_retr(1, item_data);
  3921. if(item_data->nameid <= 0 || amount <= 0)
  3922. return 1;
  3923. data = itemdb_search(item_data->nameid);
  3924. if( data->stack.cart && amount > data->stack.amount )
  3925. {// item stack limitation
  3926. return 1;
  3927. }
  3928. if( !itemdb_cancartstore(item_data, pc_get_group_level(sd)) )
  3929. { // Check item trade restrictions [Skotlex]
  3930. clif_displaymessage (sd->fd, msg_txt(264));
  3931. return 1;
  3932. }
  3933. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  3934. return 1;
  3935. i = MAX_CART;
  3936. if( itemdb_isstackable2(data) && !item_data->expire_time )
  3937. {
  3938. ARR_FIND( 0, MAX_CART, i,
  3939. sd->status.cart[i].nameid == item_data->nameid &&
  3940. sd->status.cart[i].card[0] == item_data->card[0] && sd->status.cart[i].card[1] == item_data->card[1] &&
  3941. sd->status.cart[i].card[2] == item_data->card[2] && sd->status.cart[i].card[3] == item_data->card[3] );
  3942. };
  3943. if( i < MAX_CART )
  3944. {// item already in cart, stack it
  3945. if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
  3946. return 1; // no room
  3947. sd->status.cart[i].amount+=amount;
  3948. clif_cart_additem(sd,i,amount,0);
  3949. }
  3950. else
  3951. {// item not stackable or not present, add it
  3952. ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
  3953. if( i == MAX_CART )
  3954. return 1; // no room
  3955. memcpy(&sd->status.cart[i],item_data,sizeof(sd->status.cart[0]));
  3956. sd->status.cart[i].amount=amount;
  3957. sd->cart_num++;
  3958. clif_cart_additem(sd,i,amount,0);
  3959. }
  3960. sd->status.cart[i].favorite = 0;/* clear */
  3961. log_pick_pc(sd, log_type, amount, &sd->status.cart[i]);
  3962. sd->cart_weight += w;
  3963. clif_updatestatus(sd,SP_CARTINFO);
  3964. return 0;
  3965. }
  3966. /*==========================================
  3967. * Delete item on cart for given index.
  3968. * Return:
  3969. * 0 = success
  3970. * 1 = fail
  3971. *------------------------------------------*/
  3972. int pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  3973. {
  3974. nullpo_retr(1, sd);
  3975. if(sd->status.cart[n].nameid==0 ||
  3976. sd->status.cart[n].amount<amount)
  3977. return 1;
  3978. log_pick_pc(sd, log_type, -amount, &sd->status.cart[n]);
  3979. sd->status.cart[n].amount -= amount;
  3980. sd->cart_weight -= itemdb_weight(sd->status.cart[n].nameid)*amount ;
  3981. if(sd->status.cart[n].amount <= 0){
  3982. memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
  3983. sd->cart_num--;
  3984. }
  3985. if(!type) {
  3986. clif_cart_delitem(sd,n,amount);
  3987. clif_updatestatus(sd,SP_CARTINFO);
  3988. }
  3989. return 0;
  3990. }
  3991. /*==========================================
  3992. * Transfer item from inventory to cart.
  3993. * Return:
  3994. * 0 = fail
  3995. * 1 = succes
  3996. *------------------------------------------*/
  3997. int pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  3998. {
  3999. struct item *item_data;
  4000. nullpo_ret(sd);
  4001. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4002. return 1;
  4003. item_data = &sd->status.inventory[idx];
  4004. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
  4005. return 1;
  4006. if( pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE) == 0 )
  4007. return pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4008. return 1;
  4009. }
  4010. /*==========================================
  4011. * Get number of item in cart.
  4012. * Return:
  4013. -1 = itemid not found or no amount found
  4014. x = remaining itemid on cart after get
  4015. *------------------------------------------*/
  4016. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4017. {
  4018. struct item* item_data;
  4019. nullpo_retr(-1, sd);
  4020. item_data = &sd->status.cart[idx];
  4021. if( item_data->nameid == 0 || item_data->amount == 0 )
  4022. return -1;
  4023. return item_data->amount - amount;
  4024. }
  4025. /*==========================================
  4026. * Retrieve an item at index idx from cart.
  4027. * Return:
  4028. * 0 = player not found or (FIXME) succes (from pc_cart_delitem)
  4029. * 1 = failure
  4030. *------------------------------------------*/
  4031. int pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4032. {
  4033. struct item *item_data;
  4034. int flag;
  4035. nullpo_ret(sd);
  4036. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4037. return 1;
  4038. item_data=&sd->status.cart[idx];
  4039. if(item_data->nameid==0 || amount < 1 || item_data->amount<amount || sd->state.vending )
  4040. return 1;
  4041. if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
  4042. return pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
  4043. clif_additem(sd,0,0,flag);
  4044. return 1;
  4045. }
  4046. /*==========================================
  4047. * Display item stolen msg to player sd
  4048. *------------------------------------------*/
  4049. int pc_show_steal(struct block_list *bl,va_list ap)
  4050. {
  4051. struct map_session_data *sd;
  4052. int itemid;
  4053. struct item_data *item=NULL;
  4054. char output[100];
  4055. sd=va_arg(ap,struct map_session_data *);
  4056. itemid=va_arg(ap,int);
  4057. if((item=itemdb_exists(itemid))==NULL)
  4058. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4059. else
  4060. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4061. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4062. return 0;
  4063. }
  4064. /*==========================================
  4065. * Steal an item from bl (mob).
  4066. * Return:
  4067. * 0 = fail
  4068. * 1 = succes
  4069. *------------------------------------------*/
  4070. int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4071. {
  4072. int i,itemid,flag;
  4073. double rate;
  4074. struct status_data *sd_status, *md_status;
  4075. struct mob_data *md;
  4076. struct item tmp_item;
  4077. if(!sd || !bl || bl->type!=BL_MOB)
  4078. return 0;
  4079. md = (TBL_MOB *)bl;
  4080. 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
  4081. return 0;
  4082. sd_status= status_get_status_data(&sd->bl);
  4083. md_status= status_get_status_data(bl);
  4084. if( md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
  4085. map[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
  4086. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4087. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4088. ) { //Can't steal from
  4089. md->state.steal_flag = UCHAR_MAX;
  4090. return 0;
  4091. }
  4092. // base skill success chance (percentual)
  4093. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4094. rate += sd->bonus.add_steal_rate;
  4095. if( rate < 1 )
  4096. return 0;
  4097. // Try dropping one item, in the order from first to last possible slot.
  4098. // Droprate is affected by the skill success rate.
  4099. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4100. if( md->db->dropitem[i].nameid > 0 && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
  4101. break;
  4102. if( i == MAX_STEAL_DROP )
  4103. return 0;
  4104. itemid = md->db->dropitem[i].nameid;
  4105. memset(&tmp_item,0,sizeof(tmp_item));
  4106. tmp_item.nameid = itemid;
  4107. tmp_item.amount = 1;
  4108. tmp_item.identify = itemdb_isidentified(itemid);
  4109. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4110. //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?
  4111. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4112. if(flag) { //Failed to steal due to overweight
  4113. clif_additem(sd,0,0,flag);
  4114. return 0;
  4115. }
  4116. if(battle_config.show_steal_in_same_party)
  4117. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4118. //Logs items, Stolen from mobs [Lupus]
  4119. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4120. //A Rare Steal Global Announce by Lupus
  4121. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4122. struct item_data *i_data;
  4123. char message[128];
  4124. i_data = itemdb_search(itemid);
  4125. sprintf (message, msg_txt(542), (sd->status.name != NULL)?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4126. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4127. intif_broadcast(message,strlen(message)+1,0);
  4128. }
  4129. return 1;
  4130. }
  4131. /*==========================================
  4132. * Stole zeny from bl (mob)
  4133. * return
  4134. * 0 = fail
  4135. * 1 = success
  4136. *------------------------------------------*/
  4137. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4138. {
  4139. int rate,skill;
  4140. struct mob_data *md;
  4141. if(!sd || !target || target->type != BL_MOB)
  4142. return 0;
  4143. md = (TBL_MOB*)target;
  4144. if( md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS )
  4145. return 0;
  4146. if( mob_is_treasure(md) )
  4147. return 0;
  4148. // FIXME: This formula is either custom or outdated.
  4149. skill = pc_checkskill(sd,RG_STEALCOIN)*10;
  4150. rate = skill + (sd->status.base_level - md->level)*3 + sd->battle_status.dex*2 + sd->battle_status.luk*2;
  4151. if(rnd()%1000 < rate)
  4152. {
  4153. int amount = md->level*10 + rnd()%100;
  4154. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4155. md->state.steal_coin_flag = 1;
  4156. return 1;
  4157. }
  4158. return 0;
  4159. }
  4160. /*==========================================
  4161. * Set's a player position.
  4162. * Return values:
  4163. * 0 - Success.
  4164. * 1 - Invalid map index.
  4165. * 2 - Map not in this map-server, and failed to locate alternate map-server.
  4166. *------------------------------------------*/
  4167. int pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  4168. {
  4169. struct party_data *p;
  4170. int16 m;
  4171. nullpo_ret(sd);
  4172. if( !mapindex || !mapindex_id2name(mapindex) )
  4173. {
  4174. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  4175. return 1;
  4176. }
  4177. if( pc_isdead(sd) )
  4178. { //Revive dead people before warping them
  4179. pc_setstand(sd);
  4180. pc_setrestartvalue(sd,1);
  4181. }
  4182. m = map_mapindex2mapid(mapindex);
  4183. if( map[m].flag.src4instance && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  4184. {
  4185. // Request the mapid of this src map into the instance of the party
  4186. int im = instance_map2imap(m, p->instance_id);
  4187. if( im < 0 )
  4188. ; // Player will enter the src map for instances
  4189. else
  4190. { // Changes destiny to the instance map, not the source map
  4191. m = im;
  4192. mapindex = map_id2index(m);
  4193. }
  4194. }
  4195. sd->state.changemap = (sd->mapindex != mapindex);
  4196. sd->state.warping = 1;
  4197. if( sd->state.changemap ) { // Misc map-changing settings
  4198. int i;
  4199. sd->state.pmap = sd->bl.m;
  4200. if (sd->sc.count) { // Cancel some map related stuff.
  4201. if (sd->sc.data[SC_JAILED])
  4202. return 1; //You may not get out!
  4203. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  4204. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  4205. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  4206. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  4207. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  4208. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  4209. if (sd->sc.data[SC_KNOWLEDGE]) {
  4210. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  4211. if (sce->timer != INVALID_TIMER)
  4212. delete_timer(sce->timer, status_change_timer);
  4213. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  4214. }
  4215. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  4216. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  4217. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  4218. }
  4219. for( i = 0; i < EQI_MAX; i++ ) {
  4220. if( sd->equip_index[ i ] >= 0 )
  4221. if( !pc_isequip( sd , sd->equip_index[ i ] ) )
  4222. pc_unequipitem( sd , sd->equip_index[ i ] , 2 );
  4223. }
  4224. if (battle_config.clear_unit_onwarp&BL_PC)
  4225. skill_clear_unitgroup(&sd->bl);
  4226. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  4227. guild_send_dot_remove(sd);
  4228. bg_send_dot_remove(sd);
  4229. if (sd->regen.state.gc)
  4230. sd->regen.state.gc = 0;
  4231. // make sure vending is allowed here
  4232. if (sd->state.vending && map[m].flag.novending) {
  4233. clif_displaymessage (sd->fd, msg_txt(276)); // "You can't open a shop on this map"
  4234. vending_closevending(sd);
  4235. }
  4236. if( raChSys.local && map[sd->bl.m].channel && idb_exists(map[sd->bl.m].channel->users, sd->status.char_id) ) {
  4237. clif_chsys_left(map[sd->bl.m].channel,sd);
  4238. }
  4239. }
  4240. if( m < 0 )
  4241. {
  4242. uint32 ip;
  4243. uint16 port;
  4244. //if can't find any map-servers, just abort setting position.
  4245. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  4246. return 2;
  4247. if (sd->npc_id)
  4248. npc_event_dequeue(sd);
  4249. npc_script_event(sd, NPCE_LOGOUT);
  4250. //remove from map, THEN change x/y coordinates
  4251. unit_remove_map_pc(sd,clrtype);
  4252. sd->mapindex = mapindex;
  4253. sd->bl.x=x;
  4254. sd->bl.y=y;
  4255. pc_clean_skilltree(sd);
  4256. chrif_save(sd,2);
  4257. chrif_changemapserver(sd, ip, (short)port);
  4258. //Free session data from this map server [Kevin]
  4259. unit_free_pc(sd);
  4260. return 0;
  4261. }
  4262. if( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys )
  4263. {
  4264. 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);
  4265. x = y = 0; // make it random
  4266. }
  4267. if( x == 0 && y == 0 )
  4268. {// pick a random walkable cell
  4269. do {
  4270. x=rnd()%(map[m].xs-2)+1;
  4271. y=rnd()%(map[m].ys-2)+1;
  4272. } while(map_getcell(m,x,y,CELL_CHKNOPASS));
  4273. }
  4274. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  4275. clif_displaymessage (sd->fd, msg_txt(204)); // "You can't open a shop on this cell."
  4276. vending_closevending(sd);
  4277. }
  4278. if(sd->bl.prev != NULL){
  4279. unit_remove_map_pc(sd,clrtype);
  4280. clif_changemap(sd,map[m].index,x,y); // [MouseJstr]
  4281. } else if(sd->state.active)
  4282. //Tag player for rewarping after map-loading is done. [Skotlex]
  4283. sd->state.rewarp = 1;
  4284. sd->mapindex = mapindex;
  4285. sd->bl.m = m;
  4286. sd->bl.x = sd->ud.to_x = x;
  4287. sd->bl.y = sd->ud.to_y = y;
  4288. if( sd->status.guild_id > 0 && map[m].flag.gvg_castle )
  4289. { // Increased guild castle regen [Valaris]
  4290. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  4291. if(gc && gc->guild_id == sd->status.guild_id)
  4292. sd->regen.state.gc = 1;
  4293. }
  4294. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  4295. {
  4296. sd->pd->bl.m = m;
  4297. sd->pd->bl.x = sd->pd->ud.to_x = x;
  4298. sd->pd->bl.y = sd->pd->ud.to_y = y;
  4299. sd->pd->ud.dir = sd->ud.dir;
  4300. }
  4301. if( merc_is_hom_active(sd->hd) )
  4302. {
  4303. sd->hd->bl.m = m;
  4304. sd->hd->bl.x = sd->hd->ud.to_x = x;
  4305. sd->hd->bl.y = sd->hd->ud.to_y = y;
  4306. sd->hd->ud.dir = sd->ud.dir;
  4307. }
  4308. if( sd->md )
  4309. {
  4310. sd->md->bl.m = m;
  4311. sd->md->bl.x = sd->md->ud.to_x = x;
  4312. sd->md->bl.y = sd->md->ud.to_y = y;
  4313. sd->md->ud.dir = sd->ud.dir;
  4314. }
  4315. return 0;
  4316. }
  4317. /*==========================================
  4318. * Warp player sd to random location on current map.
  4319. * May fail if no walkable cell found (1000 attempts).
  4320. * Return:
  4321. * 0 = fail or FIXME success (from pc_setpos)
  4322. * x(1|2) = fail
  4323. *------------------------------------------*/
  4324. int pc_randomwarp(struct map_session_data *sd, clr_type type)
  4325. {
  4326. int x,y,i=0;
  4327. int16 m;
  4328. nullpo_ret(sd);
  4329. m=sd->bl.m;
  4330. if (map[sd->bl.m].flag.noteleport) //Teleport forbidden
  4331. return 0;
  4332. do{
  4333. x=rnd()%(map[m].xs-2)+1;
  4334. y=rnd()%(map[m].ys-2)+1;
  4335. }while(map_getcell(m,x,y,CELL_CHKNOPASS) && (i++)<1000 );
  4336. if (i < 1000)
  4337. return pc_setpos(sd,map[sd->bl.m].index,x,y,type);
  4338. return 0;
  4339. }
  4340. /*==========================================
  4341. * Records a memo point at sd's current position
  4342. * pos - entry to replace, (-1: shift oldest entry out)
  4343. *------------------------------------------*/
  4344. int pc_memo(struct map_session_data* sd, int pos)
  4345. {
  4346. int skill;
  4347. nullpo_ret(sd);
  4348. // check mapflags
  4349. 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) ) {
  4350. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  4351. return 0;
  4352. }
  4353. // check inputs
  4354. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  4355. return 0; // invalid input
  4356. // check required skill level
  4357. skill = pc_checkskill(sd, AL_WARP);
  4358. if( skill < 1 ) {
  4359. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  4360. return 0;
  4361. }
  4362. if( skill < 2 || skill - 2 < pos ) {
  4363. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  4364. return 0;
  4365. }
  4366. if( pos == -1 )
  4367. {
  4368. int i;
  4369. // prevent memo-ing the same map multiple times
  4370. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  4371. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  4372. pos = 0;
  4373. }
  4374. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  4375. sd->status.memo_point[pos].x = sd->bl.x;
  4376. sd->status.memo_point[pos].y = sd->bl.y;
  4377. clif_skill_memomessage(sd, 0);
  4378. return 1;
  4379. }
  4380. //
  4381. // Skills
  4382. //
  4383. /*==========================================
  4384. * Return player sd skill_lv learned for given skill
  4385. *------------------------------------------*/
  4386. int pc_checkskill(struct map_session_data *sd,uint16 skill_id)
  4387. {
  4388. if(sd == NULL) return 0;
  4389. if( skill_id >= GD_SKILLBASE && skill_id < GD_MAX ) {
  4390. struct guild *g;
  4391. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  4392. return guild_checkskill(g,skill_id);
  4393. return 0;
  4394. } else if(skill_id >= ARRAYLENGTH(sd->status.skill) ) {
  4395. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  4396. return 0;
  4397. }
  4398. if(sd->status.skill[skill_id].id == skill_id)
  4399. return (sd->status.skill[skill_id].lv);
  4400. return 0;
  4401. }
  4402. /*==========================================
  4403. * Chk if we still have the correct weapon to continue the skill (actually status)
  4404. * If not ending it
  4405. * Return
  4406. * 0 - No status found or all done
  4407. *------------------------------------------*/
  4408. int pc_checkallowskill(struct map_session_data *sd)
  4409. {
  4410. const enum sc_type scw_list[] = {
  4411. SC_TWOHANDQUICKEN,
  4412. SC_ONEHAND,
  4413. SC_AURABLADE,
  4414. SC_PARRYING,
  4415. SC_SPEARQUICKEN,
  4416. SC_ADRENALINE,
  4417. SC_ADRENALINE2,
  4418. SC_DANCING,
  4419. SC_GATLINGFEVER,
  4420. SC_FEARBREEZE
  4421. };
  4422. const enum sc_type scs_list[] = {
  4423. SC_AUTOGUARD,
  4424. SC_DEFENDER,
  4425. SC_REFLECTSHIELD,
  4426. SC_REFLECTDAMAGE
  4427. };
  4428. int i;
  4429. nullpo_ret(sd);
  4430. if(!sd->sc.count)
  4431. return 0;
  4432. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  4433. { // Skills requiring specific weapon types
  4434. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  4435. continue;
  4436. if(sd->sc.data[scw_list[i]] &&
  4437. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  4438. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  4439. }
  4440. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  4441. // Spurt requires bare hands (feet, in fact xD)
  4442. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  4443. if(sd->status.shield <= 0) { // Skills requiring a shield
  4444. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  4445. if(sd->sc.data[scs_list[i]])
  4446. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  4447. }
  4448. return 0;
  4449. }
  4450. /*==========================================
  4451. * Return equiped itemid? on player sd at pos
  4452. * Return
  4453. * -1 : mean nothing equiped
  4454. * idx : (this index could be used in inventory to found item_data)
  4455. *------------------------------------------*/
  4456. int pc_checkequip(struct map_session_data *sd,int pos)
  4457. {
  4458. int i;
  4459. nullpo_retr(-1, sd);
  4460. for(i=0;i<EQI_MAX;i++){
  4461. if(pos & equip_pos[i])
  4462. return sd->equip_index[i];
  4463. }
  4464. return -1;
  4465. }
  4466. /*==========================================
  4467. * Convert's from the client's lame Job ID system
  4468. * to the map server's 'makes sense' system. [Skotlex]
  4469. *------------------------------------------*/
  4470. int pc_jobid2mapid(unsigned short b_class)
  4471. {
  4472. switch(b_class)
  4473. {
  4474. //Novice And 1-1 Jobs
  4475. case JOB_NOVICE: return MAPID_NOVICE;
  4476. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  4477. case JOB_MAGE: return MAPID_MAGE;
  4478. case JOB_ARCHER: return MAPID_ARCHER;
  4479. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  4480. case JOB_MERCHANT: return MAPID_MERCHANT;
  4481. case JOB_THIEF: return MAPID_THIEF;
  4482. case JOB_TAEKWON: return MAPID_TAEKWON;
  4483. case JOB_WEDDING: return MAPID_WEDDING;
  4484. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  4485. case JOB_NINJA: return MAPID_NINJA;
  4486. case JOB_XMAS: return MAPID_XMAS;
  4487. case JOB_SUMMER: return MAPID_SUMMER;
  4488. case JOB_HANBOK: return MAPID_HANBOK;
  4489. case JOB_GANGSI: return MAPID_GANGSI;
  4490. //2-1 Jobs
  4491. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  4492. case JOB_KNIGHT: return MAPID_KNIGHT;
  4493. case JOB_WIZARD: return MAPID_WIZARD;
  4494. case JOB_HUNTER: return MAPID_HUNTER;
  4495. case JOB_PRIEST: return MAPID_PRIEST;
  4496. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  4497. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  4498. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  4499. case JOB_KAGEROU:
  4500. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  4501. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  4502. //2-2 Jobs
  4503. case JOB_CRUSADER: return MAPID_CRUSADER;
  4504. case JOB_SAGE: return MAPID_SAGE;
  4505. case JOB_BARD:
  4506. case JOB_DANCER: return MAPID_BARDDANCER;
  4507. case JOB_MONK: return MAPID_MONK;
  4508. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  4509. case JOB_ROGUE: return MAPID_ROGUE;
  4510. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  4511. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  4512. //Trans Novice And Trans 1-1 Jobs
  4513. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  4514. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  4515. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  4516. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  4517. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  4518. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  4519. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  4520. //Trans 2-1 Jobs
  4521. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  4522. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  4523. case JOB_SNIPER: return MAPID_SNIPER;
  4524. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  4525. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  4526. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  4527. //Trans 2-2 Jobs
  4528. case JOB_PALADIN: return MAPID_PALADIN;
  4529. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  4530. case JOB_CLOWN:
  4531. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  4532. case JOB_CHAMPION: return MAPID_CHAMPION;
  4533. case JOB_CREATOR: return MAPID_CREATOR;
  4534. case JOB_STALKER: return MAPID_STALKER;
  4535. //Baby Novice And Baby 1-1 Jobs
  4536. case JOB_BABY: return MAPID_BABY;
  4537. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  4538. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  4539. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  4540. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  4541. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  4542. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  4543. //Baby 2-1 Jobs
  4544. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  4545. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  4546. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  4547. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  4548. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  4549. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  4550. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  4551. //Baby 2-2 Jobs
  4552. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  4553. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  4554. case JOB_BABY_BARD:
  4555. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  4556. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  4557. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  4558. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  4559. //3-1 Jobs
  4560. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  4561. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  4562. case JOB_WARLOCK: return MAPID_WARLOCK;
  4563. case JOB_RANGER: return MAPID_RANGER;
  4564. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  4565. case JOB_MECHANIC: return MAPID_MECHANIC;
  4566. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  4567. //3-2 Jobs
  4568. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  4569. case JOB_SORCERER: return MAPID_SORCERER;
  4570. case JOB_MINSTREL:
  4571. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  4572. case JOB_SURA: return MAPID_SURA;
  4573. case JOB_GENETIC: return MAPID_GENETIC;
  4574. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  4575. //Trans 3-1 Jobs
  4576. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  4577. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  4578. case JOB_RANGER_T: return MAPID_RANGER_T;
  4579. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  4580. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  4581. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  4582. //Trans 3-2 Jobs
  4583. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  4584. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  4585. case JOB_MINSTREL_T:
  4586. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  4587. case JOB_SURA_T: return MAPID_SURA_T;
  4588. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  4589. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  4590. //Baby 3-1 Jobs
  4591. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  4592. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  4593. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  4594. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  4595. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  4596. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  4597. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  4598. //Baby 3-2 Jobs
  4599. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  4600. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  4601. case JOB_BABY_MINSTREL:
  4602. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  4603. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  4604. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  4605. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  4606. default:
  4607. return -1;
  4608. }
  4609. }
  4610. //Reverts the map-style class id to the client-style one.
  4611. int pc_mapid2jobid(unsigned short class_, int sex)
  4612. {
  4613. switch(class_)
  4614. {
  4615. //Novice And 1-1 Jobs
  4616. case MAPID_NOVICE: return JOB_NOVICE;
  4617. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  4618. case MAPID_MAGE: return JOB_MAGE;
  4619. case MAPID_ARCHER: return JOB_ARCHER;
  4620. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  4621. case MAPID_MERCHANT: return JOB_MERCHANT;
  4622. case MAPID_THIEF: return JOB_THIEF;
  4623. case MAPID_TAEKWON: return JOB_TAEKWON;
  4624. case MAPID_WEDDING: return JOB_WEDDING;
  4625. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  4626. case MAPID_NINJA: return JOB_NINJA;
  4627. case MAPID_XMAS: return JOB_XMAS;
  4628. case MAPID_SUMMER: return JOB_SUMMER;
  4629. case MAPID_HANBOK: return JOB_HANBOK;
  4630. case MAPID_GANGSI: return JOB_GANGSI;
  4631. //2-1 Jobs
  4632. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  4633. case MAPID_KNIGHT: return JOB_KNIGHT;
  4634. case MAPID_WIZARD: return JOB_WIZARD;
  4635. case MAPID_HUNTER: return JOB_HUNTER;
  4636. case MAPID_PRIEST: return JOB_PRIEST;
  4637. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  4638. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  4639. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  4640. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  4641. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  4642. //2-2 Jobs
  4643. case MAPID_CRUSADER: return JOB_CRUSADER;
  4644. case MAPID_SAGE: return JOB_SAGE;
  4645. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  4646. case MAPID_MONK: return JOB_MONK;
  4647. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  4648. case MAPID_ROGUE: return JOB_ROGUE;
  4649. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  4650. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  4651. //Trans Novice And Trans 2-1 Jobs
  4652. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  4653. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  4654. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  4655. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  4656. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  4657. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  4658. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  4659. //Trans 2-1 Jobs
  4660. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  4661. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  4662. case MAPID_SNIPER: return JOB_SNIPER;
  4663. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  4664. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  4665. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  4666. //Trans 2-2 Jobs
  4667. case MAPID_PALADIN: return JOB_PALADIN;
  4668. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  4669. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  4670. case MAPID_CHAMPION: return JOB_CHAMPION;
  4671. case MAPID_CREATOR: return JOB_CREATOR;
  4672. case MAPID_STALKER: return JOB_STALKER;
  4673. //Baby Novice And Baby 1-1 Jobs
  4674. case MAPID_BABY: return JOB_BABY;
  4675. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  4676. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  4677. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  4678. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  4679. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  4680. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  4681. //Baby 2-1 Jobs
  4682. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  4683. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  4684. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  4685. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  4686. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  4687. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  4688. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  4689. //Baby 2-2 Jobs
  4690. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  4691. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  4692. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  4693. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  4694. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  4695. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  4696. //3-1 Jobs
  4697. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  4698. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  4699. case MAPID_WARLOCK: return JOB_WARLOCK;
  4700. case MAPID_RANGER: return JOB_RANGER;
  4701. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  4702. case MAPID_MECHANIC: return JOB_MECHANIC;
  4703. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  4704. //3-2 Jobs
  4705. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  4706. case MAPID_SORCERER: return JOB_SORCERER;
  4707. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  4708. case MAPID_SURA: return JOB_SURA;
  4709. case MAPID_GENETIC: return JOB_GENETIC;
  4710. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  4711. //Trans 3-1 Jobs
  4712. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  4713. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  4714. case MAPID_RANGER_T: return JOB_RANGER_T;
  4715. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  4716. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  4717. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  4718. //Trans 3-2 Jobs
  4719. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  4720. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  4721. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  4722. case MAPID_SURA_T: return JOB_SURA_T;
  4723. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  4724. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  4725. //Baby 3-1 Jobs
  4726. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  4727. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  4728. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  4729. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  4730. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  4731. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  4732. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  4733. //Baby 3-2 Jobs
  4734. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  4735. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  4736. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  4737. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  4738. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  4739. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  4740. default:
  4741. return -1;
  4742. }
  4743. }
  4744. /*====================================================
  4745. * This function return the name of the job (by [Yor])
  4746. *----------------------------------------------------*/
  4747. const char* job_name(int class_)
  4748. {
  4749. switch (class_) {
  4750. case JOB_NOVICE:
  4751. case JOB_SWORDMAN:
  4752. case JOB_MAGE:
  4753. case JOB_ARCHER:
  4754. case JOB_ACOLYTE:
  4755. case JOB_MERCHANT:
  4756. case JOB_THIEF:
  4757. return msg_txt(550 - JOB_NOVICE+class_);
  4758. case JOB_KNIGHT:
  4759. case JOB_PRIEST:
  4760. case JOB_WIZARD:
  4761. case JOB_BLACKSMITH:
  4762. case JOB_HUNTER:
  4763. case JOB_ASSASSIN:
  4764. return msg_txt(557 - JOB_KNIGHT+class_);
  4765. case JOB_KNIGHT2:
  4766. return msg_txt(557);
  4767. case JOB_CRUSADER:
  4768. case JOB_MONK:
  4769. case JOB_SAGE:
  4770. case JOB_ROGUE:
  4771. case JOB_ALCHEMIST:
  4772. case JOB_BARD:
  4773. case JOB_DANCER:
  4774. return msg_txt(563 - JOB_CRUSADER+class_);
  4775. case JOB_CRUSADER2:
  4776. return msg_txt(563);
  4777. case JOB_WEDDING:
  4778. case JOB_SUPER_NOVICE:
  4779. case JOB_GUNSLINGER:
  4780. case JOB_NINJA:
  4781. case JOB_XMAS:
  4782. return msg_txt(570 - JOB_WEDDING+class_);
  4783. case JOB_SUMMER:
  4784. return msg_txt(621);
  4785. case JOB_HANBOK:
  4786. return msg_txt(694);
  4787. case JOB_NOVICE_HIGH:
  4788. case JOB_SWORDMAN_HIGH:
  4789. case JOB_MAGE_HIGH:
  4790. case JOB_ARCHER_HIGH:
  4791. case JOB_ACOLYTE_HIGH:
  4792. case JOB_MERCHANT_HIGH:
  4793. case JOB_THIEF_HIGH:
  4794. return msg_txt(575 - JOB_NOVICE_HIGH+class_);
  4795. case JOB_LORD_KNIGHT:
  4796. case JOB_HIGH_PRIEST:
  4797. case JOB_HIGH_WIZARD:
  4798. case JOB_WHITESMITH:
  4799. case JOB_SNIPER:
  4800. case JOB_ASSASSIN_CROSS:
  4801. return msg_txt(582 - JOB_LORD_KNIGHT+class_);
  4802. case JOB_LORD_KNIGHT2:
  4803. return msg_txt(582);
  4804. case JOB_PALADIN:
  4805. case JOB_CHAMPION:
  4806. case JOB_PROFESSOR:
  4807. case JOB_STALKER:
  4808. case JOB_CREATOR:
  4809. case JOB_CLOWN:
  4810. case JOB_GYPSY:
  4811. return msg_txt(588 - JOB_PALADIN + class_);
  4812. case JOB_PALADIN2:
  4813. return msg_txt(588);
  4814. case JOB_BABY:
  4815. case JOB_BABY_SWORDMAN:
  4816. case JOB_BABY_MAGE:
  4817. case JOB_BABY_ARCHER:
  4818. case JOB_BABY_ACOLYTE:
  4819. case JOB_BABY_MERCHANT:
  4820. case JOB_BABY_THIEF:
  4821. return msg_txt(595 - JOB_BABY + class_);
  4822. case JOB_BABY_KNIGHT:
  4823. case JOB_BABY_PRIEST:
  4824. case JOB_BABY_WIZARD:
  4825. case JOB_BABY_BLACKSMITH:
  4826. case JOB_BABY_HUNTER:
  4827. case JOB_BABY_ASSASSIN:
  4828. return msg_txt(602 - JOB_BABY_KNIGHT + class_);
  4829. case JOB_BABY_KNIGHT2:
  4830. return msg_txt(602);
  4831. case JOB_BABY_CRUSADER:
  4832. case JOB_BABY_MONK:
  4833. case JOB_BABY_SAGE:
  4834. case JOB_BABY_ROGUE:
  4835. case JOB_BABY_ALCHEMIST:
  4836. case JOB_BABY_BARD:
  4837. case JOB_BABY_DANCER:
  4838. return msg_txt(608 - JOB_BABY_CRUSADER + class_);
  4839. case JOB_BABY_CRUSADER2:
  4840. return msg_txt(608);
  4841. case JOB_SUPER_BABY:
  4842. return msg_txt(615);
  4843. case JOB_TAEKWON:
  4844. return msg_txt(616);
  4845. case JOB_STAR_GLADIATOR:
  4846. case JOB_STAR_GLADIATOR2:
  4847. return msg_txt(617);
  4848. case JOB_SOUL_LINKER:
  4849. return msg_txt(618);
  4850. case JOB_GANGSI:
  4851. case JOB_DEATH_KNIGHT:
  4852. case JOB_DARK_COLLECTOR:
  4853. return msg_txt(622 - JOB_GANGSI+class_);
  4854. case JOB_RUNE_KNIGHT:
  4855. case JOB_WARLOCK:
  4856. case JOB_RANGER:
  4857. case JOB_ARCH_BISHOP:
  4858. case JOB_MECHANIC:
  4859. case JOB_GUILLOTINE_CROSS:
  4860. return msg_txt(625 - JOB_RUNE_KNIGHT+class_);
  4861. case JOB_RUNE_KNIGHT_T:
  4862. case JOB_WARLOCK_T:
  4863. case JOB_RANGER_T:
  4864. case JOB_ARCH_BISHOP_T:
  4865. case JOB_MECHANIC_T:
  4866. case JOB_GUILLOTINE_CROSS_T:
  4867. return msg_txt(681 - JOB_RUNE_KNIGHT_T+class_);
  4868. case JOB_ROYAL_GUARD:
  4869. case JOB_SORCERER:
  4870. case JOB_MINSTREL:
  4871. case JOB_WANDERER:
  4872. case JOB_SURA:
  4873. case JOB_GENETIC:
  4874. case JOB_SHADOW_CHASER:
  4875. return msg_txt(631 - JOB_ROYAL_GUARD+class_);
  4876. case JOB_ROYAL_GUARD_T:
  4877. case JOB_SORCERER_T:
  4878. case JOB_MINSTREL_T:
  4879. case JOB_WANDERER_T:
  4880. case JOB_SURA_T:
  4881. case JOB_GENETIC_T:
  4882. case JOB_SHADOW_CHASER_T:
  4883. return msg_txt(687 - JOB_ROYAL_GUARD_T+class_);
  4884. case JOB_RUNE_KNIGHT2:
  4885. case JOB_RUNE_KNIGHT_T2:
  4886. return msg_txt(625);
  4887. case JOB_ROYAL_GUARD2:
  4888. case JOB_ROYAL_GUARD_T2:
  4889. return msg_txt(631);
  4890. case JOB_RANGER2:
  4891. case JOB_RANGER_T2:
  4892. return msg_txt(627);
  4893. case JOB_MECHANIC2:
  4894. case JOB_MECHANIC_T2:
  4895. return msg_txt(629);
  4896. case JOB_BABY_RUNE:
  4897. case JOB_BABY_WARLOCK:
  4898. case JOB_BABY_RANGER:
  4899. case JOB_BABY_BISHOP:
  4900. case JOB_BABY_MECHANIC:
  4901. case JOB_BABY_CROSS:
  4902. case JOB_BABY_GUARD:
  4903. case JOB_BABY_SORCERER:
  4904. case JOB_BABY_MINSTREL:
  4905. case JOB_BABY_WANDERER:
  4906. case JOB_BABY_SURA:
  4907. case JOB_BABY_GENETIC:
  4908. case JOB_BABY_CHASER:
  4909. return msg_txt(638 - JOB_BABY_RUNE+class_);
  4910. case JOB_BABY_RUNE2:
  4911. return msg_txt(638);
  4912. case JOB_BABY_GUARD2:
  4913. return msg_txt(644);
  4914. case JOB_BABY_RANGER2:
  4915. return msg_txt(640);
  4916. case JOB_BABY_MECHANIC2:
  4917. return msg_txt(642);
  4918. case JOB_SUPER_NOVICE_E:
  4919. case JOB_SUPER_BABY_E:
  4920. return msg_txt(651 - JOB_SUPER_NOVICE_E+class_);
  4921. case JOB_KAGEROU:
  4922. case JOB_OBORO:
  4923. return msg_txt(653 - JOB_KAGEROU+class_);
  4924. default:
  4925. return msg_txt(655);
  4926. }
  4927. }
  4928. int pc_follow_timer(int tid, unsigned int tick, int id, intptr_t data)
  4929. {
  4930. struct map_session_data *sd;
  4931. struct block_list *tbl;
  4932. sd = map_id2sd(id);
  4933. nullpo_ret(sd);
  4934. if (sd->followtimer != tid){
  4935. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  4936. sd->followtimer = INVALID_TIMER;
  4937. return 0;
  4938. }
  4939. sd->followtimer = INVALID_TIMER;
  4940. tbl = map_id2bl(sd->followtarget);
  4941. if (tbl == NULL || pc_isdead(sd) || status_isdead(tbl))
  4942. {
  4943. pc_stop_following(sd);
  4944. return 0;
  4945. }
  4946. // either player or target is currently detached from map blocks (could be teleporting),
  4947. // but still connected to this map, so we'll just increment the timer and check back later
  4948. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  4949. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  4950. {
  4951. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  4952. if (!check_distance_bl(&sd->bl, tbl, 5))
  4953. unit_walktobl(&sd->bl, tbl, 5, 0);
  4954. } else
  4955. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  4956. }
  4957. sd->followtimer = add_timer(
  4958. tick + 1000, // increase time a bit to loosen up map's load
  4959. pc_follow_timer, sd->bl.id, 0);
  4960. return 0;
  4961. }
  4962. int pc_stop_following (struct map_session_data *sd)
  4963. {
  4964. nullpo_ret(sd);
  4965. if (sd->followtimer != INVALID_TIMER) {
  4966. delete_timer(sd->followtimer,pc_follow_timer);
  4967. sd->followtimer = INVALID_TIMER;
  4968. }
  4969. sd->followtarget = -1;
  4970. sd->ud.target_to = 0;
  4971. unit_stop_walking(&sd->bl, 1);
  4972. return 0;
  4973. }
  4974. int pc_follow(struct map_session_data *sd,int target_id)
  4975. {
  4976. struct block_list *bl = map_id2bl(target_id);
  4977. if (bl == NULL /*|| bl->type != BL_PC*/)
  4978. return 1;
  4979. if (sd->followtimer != INVALID_TIMER)
  4980. pc_stop_following(sd);
  4981. sd->followtarget = target_id;
  4982. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  4983. return 0;
  4984. }
  4985. int pc_checkbaselevelup(struct map_session_data *sd) {
  4986. unsigned int next = pc_nextbaseexp(sd);
  4987. if (!next || sd->status.base_exp < next)
  4988. return 0;
  4989. do {
  4990. sd->status.base_exp -= next;
  4991. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  4992. if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
  4993. sd->status.base_exp = next-1;
  4994. next = pc_gets_status_point(sd->status.base_level);
  4995. sd->status.base_level ++;
  4996. sd->status.status_point += next;
  4997. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  4998. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  4999. status_calc_pet(sd->pd,0);
  5000. clif_updatestatus(sd,SP_STATUSPOINT);
  5001. clif_updatestatus(sd,SP_BASELEVEL);
  5002. clif_updatestatus(sd,SP_BASEEXP);
  5003. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5004. status_calc_pc(sd,0);
  5005. status_percent_heal(&sd->bl,100,100);
  5006. if((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5007. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5008. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5009. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5010. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5011. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5012. if (sd->state.snovice_dead_flag)
  5013. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5014. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5015. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5016. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5017. }
  5018. clif_misceffect(&sd->bl,0);
  5019. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  5020. if(sd->status.party_id)
  5021. party_send_levelup(sd);
  5022. pc_baselevelchanged(sd);
  5023. return 1;
  5024. }
  5025. void pc_baselevelchanged(struct map_session_data *sd) {
  5026. #ifdef RENEWAL
  5027. int i;
  5028. for( i = 0; i < EQI_MAX; i++ ) {
  5029. if( sd->equip_index[i] >= 0 ) {
  5030. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  5031. pc_unequipitem(sd, sd->equip_index[i], 3);
  5032. }
  5033. }
  5034. #endif
  5035. }
  5036. int pc_checkjoblevelup(struct map_session_data *sd)
  5037. {
  5038. unsigned int next = pc_nextjobexp(sd);
  5039. nullpo_ret(sd);
  5040. if(!next || sd->status.job_exp < next)
  5041. return 0;
  5042. do {
  5043. sd->status.job_exp -= next;
  5044. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5045. if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
  5046. sd->status.job_exp = next-1;
  5047. sd->status.job_level ++;
  5048. sd->status.skill_point ++;
  5049. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  5050. clif_updatestatus(sd,SP_JOBLEVEL);
  5051. clif_updatestatus(sd,SP_JOBEXP);
  5052. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5053. clif_updatestatus(sd,SP_SKILLPOINT);
  5054. status_calc_pc(sd,0);
  5055. clif_misceffect(&sd->bl,1);
  5056. if (pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd))
  5057. clif_status_change(&sd->bl,SI_DEVIL, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  5058. npc_script_event(sd, NPCE_JOBLVUP);
  5059. return 1;
  5060. }
  5061. /*==========================================
  5062. * Alters experienced based on self bonuses that do not get even shared to the party.
  5063. *------------------------------------------*/
  5064. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  5065. {
  5066. int bonus = 0;
  5067. struct status_data *status = status_get_status_data(src);
  5068. if (sd->expaddrace[status->race])
  5069. bonus += sd->expaddrace[status->race];
  5070. bonus += sd->expaddrace[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
  5071. if (battle_config.pk_mode &&
  5072. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  5073. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  5074. if (sd->sc.data[SC_EXPBOOST])
  5075. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  5076. *base_exp = (unsigned int) cap_value(*base_exp + (double)*base_exp * bonus/100., 1, UINT_MAX);
  5077. if (sd->sc.data[SC_JEXPBOOST])
  5078. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  5079. *job_exp = (unsigned int) cap_value(*job_exp + (double)*job_exp * bonus/100., 1, UINT_MAX);
  5080. return;
  5081. }
  5082. /*==========================================
  5083. * Give x exp at sd player and calculate remaining exp for next lvl
  5084. *------------------------------------------*/
  5085. int pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp,unsigned int job_exp,bool quest)
  5086. {
  5087. float nextbp=0, nextjp=0;
  5088. unsigned int nextb=0, nextj=0;
  5089. nullpo_ret(sd);
  5090. if(sd->bl.prev == NULL || pc_isdead(sd))
  5091. return 0;
  5092. if(!battle_config.pvp_exp && map[sd->bl.m].flag.pvp) // [MouseJstr]
  5093. return 0; // no exp on pvp maps
  5094. if(sd->status.guild_id>0)
  5095. base_exp-=guild_payexp(sd,base_exp);
  5096. if(src) pc_calcexp(sd, &base_exp, &job_exp, src);
  5097. nextb = pc_nextbaseexp(sd);
  5098. nextj = pc_nextjobexp(sd);
  5099. if(sd->state.showexp || battle_config.max_exp_gain_rate){
  5100. if (nextb > 0)
  5101. nextbp = (float) base_exp / (float) nextb;
  5102. if (nextj > 0)
  5103. nextjp = (float) job_exp / (float) nextj;
  5104. if(battle_config.max_exp_gain_rate) {
  5105. if (nextbp > battle_config.max_exp_gain_rate/1000.) {
  5106. //Note that this value should never be greater than the original
  5107. //base_exp, therefore no overflow checks are needed. [Skotlex]
  5108. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  5109. if (sd->state.showexp)
  5110. nextbp = (float) base_exp / (float) nextb;
  5111. }
  5112. if (nextjp > battle_config.max_exp_gain_rate/1000.) {
  5113. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  5114. if (sd->state.showexp)
  5115. nextjp = (float) job_exp / (float) nextj;
  5116. }
  5117. }
  5118. }
  5119. //Cap exp to the level up requirement of the previous level when you are at max level, otherwise cap at UINT_MAX (this is required for some S. Novice bonuses). [Skotlex]
  5120. if (base_exp) {
  5121. nextb = nextb?UINT_MAX:pc_thisbaseexp(sd);
  5122. if(sd->status.base_exp > nextb - base_exp)
  5123. sd->status.base_exp = nextb;
  5124. else
  5125. sd->status.base_exp += base_exp;
  5126. pc_checkbaselevelup(sd);
  5127. clif_updatestatus(sd,SP_BASEEXP);
  5128. }
  5129. if (job_exp) {
  5130. nextj = nextj?UINT_MAX:pc_thisjobexp(sd);
  5131. if(sd->status.job_exp > nextj - job_exp)
  5132. sd->status.job_exp = nextj;
  5133. else
  5134. sd->status.job_exp += job_exp;
  5135. pc_checkjoblevelup(sd);
  5136. clif_updatestatus(sd,SP_JOBEXP);
  5137. }
  5138. if(base_exp)
  5139. clif_displayexp(sd, base_exp, SP_BASEEXP, quest);
  5140. if(job_exp)
  5141. clif_displayexp(sd, job_exp, SP_JOBEXP, quest);
  5142. if(sd->state.showexp) {
  5143. char output[256];
  5144. sprintf(output,
  5145. "Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
  5146. clif_disp_onlyself(sd,output,strlen(output));
  5147. }
  5148. return 1;
  5149. }
  5150. /*==========================================
  5151. * Returns max level for this character.
  5152. *------------------------------------------*/
  5153. unsigned int pc_maxbaselv(struct map_session_data *sd)
  5154. {
  5155. return max_level[pc_class2idx(sd->status.class_)][0];
  5156. }
  5157. unsigned int pc_maxjoblv(struct map_session_data *sd)
  5158. {
  5159. return max_level[pc_class2idx(sd->status.class_)][1];
  5160. }
  5161. /*==========================================
  5162. * base level exp lookup.
  5163. *------------------------------------------*/
  5164. //Base exp needed for next level.
  5165. unsigned int pc_nextbaseexp(struct map_session_data *sd)
  5166. {
  5167. nullpo_ret(sd);
  5168. if(sd->status.base_level>=pc_maxbaselv(sd) || sd->status.base_level<=0)
  5169. return 0;
  5170. return exp_table[pc_class2idx(sd->status.class_)][0][sd->status.base_level-1];
  5171. }
  5172. //Base exp needed for this level.
  5173. unsigned int pc_thisbaseexp(struct map_session_data *sd)
  5174. {
  5175. if(sd->status.base_level>pc_maxbaselv(sd) || sd->status.base_level<=1)
  5176. return 0;
  5177. return exp_table[pc_class2idx(sd->status.class_)][0][sd->status.base_level-2];
  5178. }
  5179. /*==========================================
  5180. * job level exp lookup
  5181. * Return:
  5182. * 0 = not found
  5183. * x = exp for level
  5184. *------------------------------------------*/
  5185. //Job exp needed for next level.
  5186. unsigned int pc_nextjobexp(struct map_session_data *sd)
  5187. {
  5188. nullpo_ret(sd);
  5189. if(sd->status.job_level>=pc_maxjoblv(sd) || sd->status.job_level<=0)
  5190. return 0;
  5191. return exp_table[pc_class2idx(sd->status.class_)][1][sd->status.job_level-1];
  5192. }
  5193. //Job exp needed for this level.
  5194. unsigned int pc_thisjobexp(struct map_session_data *sd)
  5195. {
  5196. if(sd->status.job_level>pc_maxjoblv(sd) || sd->status.job_level<=1)
  5197. return 0;
  5198. return exp_table[pc_class2idx(sd->status.class_)][1][sd->status.job_level-2];
  5199. }
  5200. /// Returns the value of the specified stat.
  5201. static int pc_getstat(struct map_session_data* sd, int type)
  5202. {
  5203. nullpo_retr(-1, sd);
  5204. switch( type ) {
  5205. case SP_STR: return sd->status.str;
  5206. case SP_AGI: return sd->status.agi;
  5207. case SP_VIT: return sd->status.vit;
  5208. case SP_INT: return sd->status.int_;
  5209. case SP_DEX: return sd->status.dex;
  5210. case SP_LUK: return sd->status.luk;
  5211. default:
  5212. return -1;
  5213. }
  5214. }
  5215. /// Sets the specified stat to the specified value.
  5216. /// Returns the new value.
  5217. static int pc_setstat(struct map_session_data* sd, int type, int val)
  5218. {
  5219. nullpo_retr(-1, sd);
  5220. switch( type ) {
  5221. case SP_STR: sd->status.str = val; break;
  5222. case SP_AGI: sd->status.agi = val; break;
  5223. case SP_VIT: sd->status.vit = val; break;
  5224. case SP_INT: sd->status.int_ = val; break;
  5225. case SP_DEX: sd->status.dex = val; break;
  5226. case SP_LUK: sd->status.luk = val; break;
  5227. default:
  5228. return -1;
  5229. }
  5230. return val;
  5231. }
  5232. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  5233. int pc_gets_status_point(int level)
  5234. {
  5235. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  5236. return (statp[level+1] - statp[level]);
  5237. else //Default increase
  5238. return ((level+15) / 5);
  5239. }
  5240. /// Returns the number of stat points needed to change the specified stat by val.
  5241. /// If val is negative, returns the number of stat points that would be needed to
  5242. /// raise the specified stat from (current value - val) to current value.
  5243. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  5244. {
  5245. int low, high, sp = 0;
  5246. if ( val == 0 )
  5247. return 0;
  5248. low = pc_getstat(sd,type);
  5249. if ( low >= pc_maxparameter(sd) && val > 0 )
  5250. return 0; // Official servers show '0' when max is reached
  5251. high = low + val;
  5252. if ( val < 0 )
  5253. swap(low, high);
  5254. for ( ; low < high; low++ )
  5255. #ifdef RENEWAL // renewal status point cost formula
  5256. sp += (low < 100) ? (2 + (low - 1) / 10) : (16 + 4 * ((low - 100) / 5));
  5257. #else
  5258. sp += ( 1 + (low + 9) / 10 );
  5259. #endif
  5260. return sp;
  5261. }
  5262. /// Raises a stat by 1.
  5263. /// Obeys max_parameter limits.
  5264. /// Subtracts stat points.
  5265. ///
  5266. /// @param type The stat to change (see enum _sp)
  5267. int pc_statusup(struct map_session_data* sd, int type)
  5268. {
  5269. int max, need, val;
  5270. nullpo_ret(sd);
  5271. // check conditions
  5272. need = pc_need_status_point(sd,type,1);
  5273. if( type < SP_STR || type > SP_LUK || need < 0 || need > sd->status.status_point )
  5274. {
  5275. clif_statusupack(sd,type,0,0);
  5276. return 1;
  5277. }
  5278. // check limits
  5279. max = pc_maxparameter(sd);
  5280. if( pc_getstat(sd,type) >= max )
  5281. {
  5282. clif_statusupack(sd,type,0,0);
  5283. return 1;
  5284. }
  5285. // set new values
  5286. val = pc_setstat(sd, type, pc_getstat(sd,type) + 1);
  5287. sd->status.status_point -= need;
  5288. status_calc_pc(sd,0);
  5289. // update increase cost indicator
  5290. if( need != pc_need_status_point(sd,type,1) )
  5291. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5292. // update statpoint count
  5293. clif_updatestatus(sd,SP_STATUSPOINT);
  5294. // update stat value
  5295. clif_statusupack(sd,type,1,val); // required
  5296. if( val > 255 )
  5297. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5298. return 0;
  5299. }
  5300. /// Raises a stat by the specified amount.
  5301. /// Obeys max_parameter limits.
  5302. /// Does not subtract stat points.
  5303. ///
  5304. /// @param type The stat to change (see enum _sp)
  5305. /// @param val The stat increase amount.
  5306. int pc_statusup2(struct map_session_data* sd, int type, int val)
  5307. {
  5308. int max, need;
  5309. nullpo_ret(sd);
  5310. if( type < SP_STR || type > SP_LUK )
  5311. {
  5312. clif_statusupack(sd,type,0,0);
  5313. return 1;
  5314. }
  5315. need = pc_need_status_point(sd,type,1);
  5316. // set new value
  5317. max = pc_maxparameter(sd);
  5318. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  5319. status_calc_pc(sd,0);
  5320. // update increase cost indicator
  5321. if( need != pc_need_status_point(sd,type,1) )
  5322. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  5323. // update stat value
  5324. clif_statusupack(sd,type,1,val); // required
  5325. if( val > 255 )
  5326. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  5327. return 0;
  5328. }
  5329. /*==========================================
  5330. * Update skill_lv for player sd
  5331. * Skill point allocation
  5332. *------------------------------------------*/
  5333. int pc_skillup(struct map_session_data *sd,uint16 skill_id)
  5334. {
  5335. nullpo_ret(sd);
  5336. if( skill_id >= GD_SKILLBASE && skill_id < GD_SKILLBASE+MAX_GUILDSKILL )
  5337. {
  5338. guild_skillup(sd, skill_id);
  5339. return 0;
  5340. }
  5341. if( skill_id >= HM_SKILLBASE && skill_id < HM_SKILLBASE+MAX_HOMUNSKILL && sd->hd )
  5342. {
  5343. merc_hom_skillup(sd->hd, skill_id);
  5344. return 0;
  5345. }
  5346. if(skill_id >= MAX_SKILL )
  5347. return 0;
  5348. if( sd->status.skill_point > 0 &&
  5349. sd->status.skill[skill_id].id &&
  5350. sd->status.skill[skill_id].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  5351. sd->status.skill[skill_id].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  5352. {
  5353. int lv,range, upgradable;
  5354. sd->status.skill[skill_id].lv++;
  5355. sd->status.skill_point--;
  5356. if( !skill_get_inf(skill_id) )
  5357. status_calc_pc(sd,0); // Only recalculate for passive skills.
  5358. else if( sd->status.skill_point == 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5359. pc_calc_skilltree(sd); // Required to grant all TK Ranger skills.
  5360. else
  5361. pc_check_skilltree(sd, skill_id); // Check if a new skill can Lvlup
  5362. lv = sd->status.skill[skill_id].lv;
  5363. range = skill_get_range2(&sd->bl, skill_id, lv);
  5364. upgradable = (lv < skill_tree_get_max(sd->status.skill[skill_id].id, sd->status.class_)) ? 1 : 0;
  5365. clif_skillup(sd,skill_id,lv,range,upgradable);
  5366. clif_updatestatus(sd,SP_SKILLPOINT);
  5367. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  5368. clif_updatestatus(sd,SP_CARTINFO);
  5369. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  5370. clif_skillinfoblock(sd);
  5371. }
  5372. return 0;
  5373. }
  5374. /*==========================================
  5375. * /allskill
  5376. *------------------------------------------*/
  5377. int pc_allskillup(struct map_session_data *sd)
  5378. {
  5379. int i,id;
  5380. nullpo_ret(sd);
  5381. for(i=0;i<MAX_SKILL;i++){
  5382. 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) {
  5383. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  5384. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5385. if (sd->status.skill[i].lv == 0)
  5386. sd->status.skill[i].id = 0;
  5387. }
  5388. }
  5389. if (pc_has_permission(sd, PC_PERM_ALL_SKILL))
  5390. { //Get ALL skills except npc/guild ones. [Skotlex]
  5391. //and except SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  5392. for(i=0;i<MAX_SKILL;i++){
  5393. switch( i ) {
  5394. case SG_DEVIL:
  5395. case MO_TRIPLEATTACK:
  5396. case RG_SNATCHER:
  5397. continue;
  5398. default:
  5399. if( !(skill_get_inf2(i)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)) )
  5400. if ( ( sd->status.skill[i].lv = skill_get_max(i) ) )//Nonexistant skills should return a max of 0 anyway.
  5401. sd->status.skill[i].id = i;
  5402. }
  5403. }
  5404. } else {
  5405. int inf2;
  5406. for(i=0;i < MAX_SKILL_TREE && (id=skill_tree[pc_class2idx(sd->status.class_)][i].id)>0;i++){
  5407. inf2 = skill_get_inf2(id);
  5408. if (
  5409. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  5410. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  5411. id==SG_DEVIL
  5412. )
  5413. continue; //Cannot be learned normally.
  5414. sd->status.skill[id].id = id;
  5415. sd->status.skill[id].lv = skill_tree_get_max(id, sd->status.class_); // celest
  5416. }
  5417. }
  5418. status_calc_pc(sd,0);
  5419. //Required because if you could level up all skills previously,
  5420. //the update will not be sent as only the lv variable changes.
  5421. clif_skillinfoblock(sd);
  5422. return 0;
  5423. }
  5424. /*==========================================
  5425. * /resetlvl
  5426. *------------------------------------------*/
  5427. int pc_resetlvl(struct map_session_data* sd,int type)
  5428. {
  5429. int i;
  5430. nullpo_ret(sd);
  5431. if (type != 3) //Also reset skills
  5432. pc_resetskill(sd, 0);
  5433. if(type == 1){
  5434. sd->status.skill_point=0;
  5435. sd->status.base_level=1;
  5436. sd->status.job_level=1;
  5437. sd->status.base_exp=0;
  5438. sd->status.job_exp=0;
  5439. if(sd->sc.option !=0)
  5440. sd->sc.option = 0;
  5441. sd->status.str=1;
  5442. sd->status.agi=1;
  5443. sd->status.vit=1;
  5444. sd->status.int_=1;
  5445. sd->status.dex=1;
  5446. sd->status.luk=1;
  5447. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  5448. sd->status.status_point=100; // not 88 [celest]
  5449. // give platinum skills upon changing
  5450. pc_skill(sd,142,1,0);
  5451. pc_skill(sd,143,1,0);
  5452. }
  5453. }
  5454. if(type == 2){
  5455. sd->status.skill_point=0;
  5456. sd->status.base_level=1;
  5457. sd->status.job_level=1;
  5458. sd->status.base_exp=0;
  5459. sd->status.job_exp=0;
  5460. }
  5461. if(type == 3){
  5462. sd->status.base_level=1;
  5463. sd->status.base_exp=0;
  5464. }
  5465. if(type == 4){
  5466. sd->status.job_level=1;
  5467. sd->status.job_exp=0;
  5468. }
  5469. clif_updatestatus(sd,SP_STATUSPOINT);
  5470. clif_updatestatus(sd,SP_STR);
  5471. clif_updatestatus(sd,SP_AGI);
  5472. clif_updatestatus(sd,SP_VIT);
  5473. clif_updatestatus(sd,SP_INT);
  5474. clif_updatestatus(sd,SP_DEX);
  5475. clif_updatestatus(sd,SP_LUK);
  5476. clif_updatestatus(sd,SP_BASELEVEL);
  5477. clif_updatestatus(sd,SP_JOBLEVEL);
  5478. clif_updatestatus(sd,SP_STATUSPOINT);
  5479. clif_updatestatus(sd,SP_BASEEXP);
  5480. clif_updatestatus(sd,SP_JOBEXP);
  5481. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5482. clif_updatestatus(sd,SP_NEXTJOBEXP);
  5483. clif_updatestatus(sd,SP_SKILLPOINT);
  5484. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5485. clif_updatestatus(sd,SP_UAGI);
  5486. clif_updatestatus(sd,SP_UVIT);
  5487. clif_updatestatus(sd,SP_UINT);
  5488. clif_updatestatus(sd,SP_UDEX);
  5489. clif_updatestatus(sd,SP_ULUK); // End Addition
  5490. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  5491. if(sd->equip_index[i] >= 0)
  5492. if(!pc_isequip(sd,sd->equip_index[i]))
  5493. pc_unequipitem(sd,sd->equip_index[i],2);
  5494. }
  5495. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  5496. party_send_levelup(sd);
  5497. status_calc_pc(sd,0);
  5498. clif_skillinfoblock(sd);
  5499. return 0;
  5500. }
  5501. /*==========================================
  5502. * /resetstate
  5503. *------------------------------------------*/
  5504. int pc_resetstate(struct map_session_data* sd)
  5505. {
  5506. nullpo_ret(sd);
  5507. if (battle_config.use_statpoint_table)
  5508. { // New statpoint table used here - Dexity
  5509. if (sd->status.base_level > MAX_LEVEL)
  5510. { //statp[] goes out of bounds, can't reset!
  5511. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  5512. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  5513. return 0;
  5514. }
  5515. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  5516. }
  5517. else
  5518. {
  5519. int add=0;
  5520. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  5521. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  5522. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  5523. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  5524. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  5525. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  5526. sd->status.status_point+=add;
  5527. }
  5528. pc_setstat(sd, SP_STR, 1);
  5529. pc_setstat(sd, SP_AGI, 1);
  5530. pc_setstat(sd, SP_VIT, 1);
  5531. pc_setstat(sd, SP_INT, 1);
  5532. pc_setstat(sd, SP_DEX, 1);
  5533. pc_setstat(sd, SP_LUK, 1);
  5534. clif_updatestatus(sd,SP_STR);
  5535. clif_updatestatus(sd,SP_AGI);
  5536. clif_updatestatus(sd,SP_VIT);
  5537. clif_updatestatus(sd,SP_INT);
  5538. clif_updatestatus(sd,SP_DEX);
  5539. clif_updatestatus(sd,SP_LUK);
  5540. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  5541. clif_updatestatus(sd,SP_UAGI);
  5542. clif_updatestatus(sd,SP_UVIT);
  5543. clif_updatestatus(sd,SP_UINT);
  5544. clif_updatestatus(sd,SP_UDEX);
  5545. clif_updatestatus(sd,SP_ULUK); // End Addition
  5546. clif_updatestatus(sd,SP_STATUSPOINT);
  5547. if( sd->mission_mobid ) { //bugreport:2200
  5548. sd->mission_mobid = 0;
  5549. sd->mission_count = 0;
  5550. pc_setglobalreg(sd,"TK_MISSION_ID", 0);
  5551. }
  5552. status_calc_pc(sd,0);
  5553. return 1;
  5554. }
  5555. /*==========================================
  5556. * /resetskill
  5557. * if flag&1, perform block resync and status_calc call.
  5558. * if flag&2, just count total amount of skill points used by player, do not really reset.
  5559. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  5560. *------------------------------------------*/
  5561. int pc_resetskill(struct map_session_data* sd, int flag)
  5562. {
  5563. int i, lv, inf2, skill_point=0;
  5564. nullpo_ret(sd);
  5565. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  5566. return 0;
  5567. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  5568. /**
  5569. * It has been confirmed on official server that when you reset skills with a ranked tweakwon your skills are not reset (because you have all of them anyway)
  5570. **/
  5571. if( (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc_famerank(sd->status.char_id, MAPID_TAEKWON) )
  5572. return 0;
  5573. if( pc_checkskill(sd, SG_DEVIL) && !pc_nextjobexp(sd) )
  5574. clif_status_load(&sd->bl, SI_DEVIL, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  5575. i = sd->sc.option;
  5576. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  5577. i &= ~OPTION_RIDING;
  5578. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  5579. i &= ~OPTION_FALCON;
  5580. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  5581. i &= ~OPTION_DRAGON;
  5582. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  5583. i &= ~OPTION_WUG;
  5584. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  5585. i &= ~OPTION_WUGRIDER;
  5586. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  5587. i &= ~OPTION_MADOGEAR;
  5588. if( i&OPTION_MOUNTING )
  5589. i &= ~OPTION_MOUNTING;
  5590. #ifndef NEW_CARTS
  5591. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  5592. i &= ~OPTION_CART;
  5593. #else
  5594. if( sd->sc.data[SC_PUSH_CART] )
  5595. pc_setcart(sd, 0);
  5596. #endif
  5597. if( i != sd->sc.option )
  5598. pc_setoption(sd, i);
  5599. if( merc_is_hom_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  5600. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  5601. }
  5602. for( i = 1; i < MAX_SKILL; i++ )
  5603. {
  5604. lv = sd->status.skill[i].lv;
  5605. if (lv < 1) continue;
  5606. inf2 = skill_get_inf2(i);
  5607. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  5608. continue;
  5609. // Don't reset trick dead if not a novice/baby
  5610. if( i == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5611. {
  5612. sd->status.skill[i].lv = 0;
  5613. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5614. continue;
  5615. }
  5616. // do not reset basic skill
  5617. if( i == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  5618. continue;
  5619. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  5620. continue;
  5621. if( flag&4 && !skill_ischangesex(i) )
  5622. continue;
  5623. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  5624. { //Only handle quest skills in a special way when you can't learn them manually
  5625. if( battle_config.quest_skill_reset && !(flag&2) )
  5626. { //Wipe them
  5627. sd->status.skill[i].lv = 0;
  5628. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5629. }
  5630. continue;
  5631. }
  5632. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  5633. skill_point += lv;
  5634. else
  5635. if( sd->status.skill[i].flag == SKILL_FLAG_REPLACED_LV_0 )
  5636. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  5637. if( !(flag&2) )
  5638. {// reset
  5639. sd->status.skill[i].lv = 0;
  5640. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  5641. }
  5642. }
  5643. if( flag&2 || !skill_point ) return skill_point;
  5644. sd->status.skill_point += skill_point;
  5645. if( flag&1 )
  5646. {
  5647. clif_updatestatus(sd,SP_SKILLPOINT);
  5648. clif_skillinfoblock(sd);
  5649. status_calc_pc(sd,0);
  5650. }
  5651. return skill_point;
  5652. }
  5653. /*==========================================
  5654. * /resetfeel [Komurka]
  5655. *------------------------------------------*/
  5656. int pc_resetfeel(struct map_session_data* sd)
  5657. {
  5658. int i;
  5659. nullpo_ret(sd);
  5660. for (i=0; i<MAX_PC_FEELHATE; i++)
  5661. {
  5662. sd->feel_map[i].m = -1;
  5663. sd->feel_map[i].index = 0;
  5664. pc_setglobalreg(sd,sg_info[i].feel_var,0);
  5665. }
  5666. return 0;
  5667. }
  5668. int pc_resethate(struct map_session_data* sd)
  5669. {
  5670. int i;
  5671. nullpo_ret(sd);
  5672. for (i=0; i<3; i++)
  5673. {
  5674. sd->hate_mob[i] = -1;
  5675. pc_setglobalreg(sd,sg_info[i].hate_var,0);
  5676. }
  5677. return 0;
  5678. }
  5679. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  5680. {
  5681. int i, bonus = 0;
  5682. nullpo_ret(sd);
  5683. ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
  5684. if( i < ARRAYLENGTH(sd->skillatk) ) bonus = sd->skillatk[i].val;
  5685. if(sd->sc.data[SC_PYROTECHNIC_OPTION] || sd->sc.data[SC_AQUAPLAY_OPTION])
  5686. bonus += 10;
  5687. return bonus;
  5688. }
  5689. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  5690. int i, bonus = sd->bonus.add_heal_rate;
  5691. if( bonus ) {
  5692. switch( skill_id ) {
  5693. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  5694. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  5695. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  5696. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  5697. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  5698. }
  5699. }
  5700. ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
  5701. if( i < ARRAYLENGTH(sd->skillheal) )
  5702. bonus += sd->skillheal[i].val;
  5703. return bonus;
  5704. }
  5705. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  5706. int i, bonus = sd->bonus.add_heal2_rate;
  5707. ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
  5708. if( i < ARRAYLENGTH(sd->skillheal2) )
  5709. bonus += sd->skillheal2[i].val;
  5710. return bonus;
  5711. }
  5712. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  5713. {
  5714. if( !pc_isdead(sd) )
  5715. return; // not applicable
  5716. if( sd->bg_id && bg_member_respawn(sd) )
  5717. return; // member revived by battleground
  5718. pc_setstand(sd);
  5719. pc_setrestartvalue(sd,3);
  5720. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
  5721. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  5722. }
  5723. static int pc_respawn_timer(int tid, unsigned int tick, int id, intptr_t data)
  5724. {
  5725. struct map_session_data *sd = map_id2sd(id);
  5726. if( sd != NULL )
  5727. {
  5728. sd->pvp_point=0;
  5729. pc_respawn(sd,CLR_OUTSIGHT);
  5730. }
  5731. return 0;
  5732. }
  5733. /*==========================================
  5734. * Invoked when a player has received damage
  5735. *------------------------------------------*/
  5736. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  5737. {
  5738. if (sp) clif_updatestatus(sd,SP_SP);
  5739. if (hp) clif_updatestatus(sd,SP_HP);
  5740. else return;
  5741. if( !src || src == &sd->bl )
  5742. return;
  5743. if( pc_issit(sd) )
  5744. {
  5745. pc_setstand(sd);
  5746. skill_sit(sd,0);
  5747. }
  5748. if( sd->progressbar.npc_id )
  5749. clif_progressbar_abort(sd);
  5750. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  5751. pet_target_check(sd,src,1);
  5752. if( sd->status.ele_id > 0 )
  5753. elemental_set_target(sd,src);
  5754. sd->canlog_tick = gettick();
  5755. }
  5756. /*==========================================
  5757. * Invoked when a player has negative current hp
  5758. *------------------------------------------*/
  5759. int pc_dead(struct map_session_data *sd,struct block_list *src)
  5760. {
  5761. int i=0,j=0,k=0;
  5762. unsigned int tick = gettick();
  5763. for(k = 0; k < 5; k++)
  5764. if (sd->devotion[k]){
  5765. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  5766. if (devsd)
  5767. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  5768. sd->devotion[k] = 0;
  5769. }
  5770. if(sd->status.pet_id > 0 && sd->pd) {
  5771. struct pet_data *pd = sd->pd;
  5772. if( !map[sd->bl.m].flag.noexppenalty ) {
  5773. pet_set_intimate(pd, pd->pet.intimate - pd->petDB->die);
  5774. if( pd->pet.intimate < 0 )
  5775. pd->pet.intimate = 0;
  5776. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  5777. }
  5778. if( sd->pd->target_id ) // Unlock all targets...
  5779. pet_unlocktarget(sd->pd);
  5780. }
  5781. if (sd->status.hom_id > 0){
  5782. if(battle_config.homunculus_auto_vapor && sd->hd && !sd->hd->sc.data[SC_LIGHT_OF_REGENE])
  5783. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  5784. }
  5785. if( sd->md )
  5786. merc_delete(sd->md, 3); // Your mercenary soldier has ran away.
  5787. if( sd->ed )
  5788. elemental_delete(sd->ed, 0);
  5789. // Leave duel if you die [LuzZza]
  5790. if(battle_config.duel_autoleave_when_die) {
  5791. if(sd->duel_group > 0)
  5792. duel_leave(sd->duel_group, sd);
  5793. if(sd->duel_invite > 0)
  5794. duel_reject(sd->duel_invite, sd);
  5795. }
  5796. pc_setglobalreg(sd,"PC_DIE_COUNTER",sd->die_counter+1);
  5797. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  5798. if( sd->bg_id ) {
  5799. struct battleground_data *bg;
  5800. if( (bg = bg_team_search(sd->bg_id)) != NULL && bg->die_event[0] )
  5801. npc_event(sd, bg->die_event, 0);
  5802. }
  5803. // Clear anything NPC-related when you die and was interacting with one.
  5804. if (sd->npc_id)
  5805. {
  5806. if (sd->state.using_fake_npc) {
  5807. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  5808. sd->state.using_fake_npc = 0;
  5809. }
  5810. if (sd->state.menu_or_input)
  5811. sd->state.menu_or_input = 0;
  5812. if (sd->npc_menu)
  5813. sd->npc_menu = 0;
  5814. sd->npc_id = 0;
  5815. if (sd->st && sd->st->state != END)
  5816. sd->st->state = END;
  5817. }
  5818. npc_script_event(sd,NPCE_DIE);
  5819. /* e.g. not killed thru pc_damage */
  5820. if( pc_issit(sd) ) {
  5821. clif_status_load(&sd->bl,SI_SIT,0);
  5822. }
  5823. pc_setdead(sd);
  5824. //Reset menu skills/item skills
  5825. if (sd->skillitem)
  5826. sd->skillitem = sd->skillitemlv = 0;
  5827. if (sd->menuskill_id)
  5828. sd->menuskill_id = sd->menuskill_val = 0;
  5829. //Reset ticks.
  5830. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  5831. if ( sd && sd->spiritball )
  5832. pc_delspiritball(sd,sd->spiritball,0);
  5833. for(i = 1; i < 5; i++)
  5834. pc_del_talisman(sd, sd->talisman[i], i);
  5835. if (src)
  5836. switch (src->type) {
  5837. case BL_MOB:
  5838. {
  5839. struct mob_data *md=(struct mob_data *)src;
  5840. if(md->target_id==sd->bl.id)
  5841. mob_unlocktarget(md,tick);
  5842. if(battle_config.mobs_level_up && md->status.hp &&
  5843. (unsigned int)md->level < pc_maxbaselv(sd) &&
  5844. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  5845. ) { // monster level up [Valaris]
  5846. clif_misceffect(&md->bl,0);
  5847. md->level++;
  5848. status_calc_mob(md, 0);
  5849. status_percent_heal(src,10,0);
  5850. if( battle_config.show_mob_info&4 )
  5851. {// update name with new level
  5852. clif_charnameack(0, &md->bl);
  5853. }
  5854. }
  5855. src = battle_get_master(src); // Maybe Player Summon
  5856. }
  5857. break;
  5858. case BL_PET: //Pass on to master...
  5859. src = &((TBL_PET*)src)->msd->bl;
  5860. break;
  5861. case BL_HOM:
  5862. src = &((TBL_HOM*)src)->master->bl;
  5863. break;
  5864. case BL_MER:
  5865. src = &((TBL_MER*)src)->master->bl;
  5866. break;
  5867. }
  5868. if (src && src->type == BL_PC)
  5869. {
  5870. struct map_session_data *ssd = (struct map_session_data *)src;
  5871. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  5872. npc_script_event(ssd, NPCE_KILLPC);
  5873. if (battle_config.pk_mode&2) {
  5874. ssd->status.manner -= 5;
  5875. if(ssd->status.manner < 0)
  5876. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  5877. #if 0
  5878. // PK/Karma system code (not enabled yet) [celest]
  5879. // originally from Kade Online, so i don't know if any of these is correct ^^;
  5880. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  5881. // karma going down = more 'good' / more honourable.
  5882. // The Karma System way...
  5883. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  5884. sd->status.karma--;
  5885. ssd->status.karma--;
  5886. }
  5887. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  5888. ssd->status.karma++;
  5889. // or the PK System way...
  5890. if (sd->status.karma > 0) // player killed is dishonourable?
  5891. ssd->status.karma--; // honour points earned
  5892. sd->status.karma++; // honour points lost
  5893. // To-do: Receive exp on certain occasions
  5894. #endif
  5895. }
  5896. }
  5897. if(battle_config.bone_drop==2
  5898. || (battle_config.bone_drop==1 && map[sd->bl.m].flag.pvp))
  5899. {
  5900. struct item item_tmp;
  5901. memset(&item_tmp,0,sizeof(item_tmp));
  5902. item_tmp.nameid=ITEMID_SKULL_;
  5903. item_tmp.identify=1;
  5904. item_tmp.card[0]=CARD0_CREATE;
  5905. item_tmp.card[1]=0;
  5906. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  5907. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  5908. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5909. }
  5910. // activate Steel body if a super novice dies at 99+% exp [celest]
  5911. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && !sd->state.snovice_dead_flag)
  5912. {
  5913. unsigned int next = pc_nextbaseexp(sd);
  5914. if( next == 0 ) next = pc_thisbaseexp(sd);
  5915. if( get_percentage(sd->status.base_exp,next) >= 99 ) {
  5916. sd->state.snovice_dead_flag = 1;
  5917. pc_setstand(sd);
  5918. status_percent_heal(&sd->bl, 100, 100);
  5919. clif_resurrection(&sd->bl, 1);
  5920. if(battle_config.pc_invincible_time)
  5921. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  5922. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,1,skill_get_time(MO_STEELBODY,1));
  5923. if(map_flag_gvg(sd->bl.m))
  5924. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5925. return 0;
  5926. }
  5927. }
  5928. // changed penalty options, added death by player if pk_mode [Valaris]
  5929. if(battle_config.death_penalty_type
  5930. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  5931. && !map[sd->bl.m].flag.noexppenalty && !map_flag_gvg(sd->bl.m)
  5932. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE])
  5933. {
  5934. unsigned int base_penalty =0;
  5935. if (battle_config.death_penalty_base > 0) {
  5936. switch (battle_config.death_penalty_type) {
  5937. case 1:
  5938. base_penalty = (unsigned int) ((double)pc_nextbaseexp(sd) * (double)battle_config.death_penalty_base/10000);
  5939. break;
  5940. case 2:
  5941. base_penalty = (unsigned int) ((double)sd->status.base_exp * (double)battle_config.death_penalty_base/10000);
  5942. break;
  5943. }
  5944. if(base_penalty) {
  5945. if (battle_config.pk_mode && src && src->type==BL_PC)
  5946. base_penalty*=2;
  5947. sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
  5948. clif_updatestatus(sd,SP_BASEEXP);
  5949. }
  5950. }
  5951. if(battle_config.death_penalty_job > 0)
  5952. {
  5953. base_penalty = 0;
  5954. switch (battle_config.death_penalty_type) {
  5955. case 1:
  5956. base_penalty = (unsigned int) ((double)pc_nextjobexp(sd) * (double)battle_config.death_penalty_job/10000);
  5957. break;
  5958. case 2:
  5959. base_penalty = (unsigned int) ((double)sd->status.job_exp * (double)battle_config.death_penalty_job/10000);
  5960. break;
  5961. }
  5962. if(base_penalty) {
  5963. if (battle_config.pk_mode && src && src->type==BL_PC)
  5964. base_penalty*=2;
  5965. sd->status.job_exp -= min(sd->status.job_exp, base_penalty);
  5966. clif_updatestatus(sd,SP_JOBEXP);
  5967. }
  5968. }
  5969. if(battle_config.zeny_penalty > 0 && !map[sd->bl.m].flag.nozenypenalty)
  5970. {
  5971. base_penalty = (unsigned int)((double)sd->status.zeny * (double)battle_config.zeny_penalty / 10000.);
  5972. if(base_penalty)
  5973. pc_payzeny(sd, base_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  5974. }
  5975. }
  5976. if(map[sd->bl.m].flag.pvp_nightmaredrop)
  5977. { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  5978. for(j=0;j<MAX_DROP_PER_MAP;j++){
  5979. int id = map[sd->bl.m].drop_list[j].drop_id;
  5980. int type = map[sd->bl.m].drop_list[j].drop_type;
  5981. int per = map[sd->bl.m].drop_list[j].drop_per;
  5982. if(id == 0)
  5983. continue;
  5984. if(id == -1){
  5985. int eq_num=0,eq_n[MAX_INVENTORY];
  5986. memset(eq_n,0,sizeof(eq_n));
  5987. for(i=0;i<MAX_INVENTORY;i++){
  5988. if( (type == 1 && !sd->status.inventory[i].equip)
  5989. || (type == 2 && sd->status.inventory[i].equip)
  5990. || type == 3)
  5991. {
  5992. int k;
  5993. ARR_FIND( 0, MAX_INVENTORY, k, eq_n[k] <= 0 );
  5994. if( k < MAX_INVENTORY )
  5995. eq_n[k] = i;
  5996. eq_num++;
  5997. }
  5998. }
  5999. if(eq_num > 0){
  6000. int n = eq_n[rnd()%eq_num];
  6001. if(rnd()%10000 < per){
  6002. if(sd->status.inventory[n].equip)
  6003. pc_unequipitem(sd,n,3);
  6004. pc_dropitem(sd,n,1);
  6005. }
  6006. }
  6007. }
  6008. else if(id > 0){
  6009. for(i=0;i<MAX_INVENTORY;i++){
  6010. if(sd->status.inventory[i].nameid == id
  6011. && rnd()%10000 < per
  6012. && ((type == 1 && !sd->status.inventory[i].equip)
  6013. || (type == 2 && sd->status.inventory[i].equip)
  6014. || type == 3) ){
  6015. if(sd->status.inventory[i].equip)
  6016. pc_unequipitem(sd,i,3);
  6017. pc_dropitem(sd,i,1);
  6018. break;
  6019. }
  6020. }
  6021. }
  6022. }
  6023. }
  6024. // pvp
  6025. // disable certain pvp functions on pk_mode [Valaris]
  6026. if( map[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map[sd->bl.m].flag.pvp_nocalcrank )
  6027. {
  6028. sd->pvp_point -= 5;
  6029. sd->pvp_lost++;
  6030. if( src && src->type == BL_PC )
  6031. {
  6032. struct map_session_data *ssd = (struct map_session_data *)src;
  6033. ssd->pvp_point++;
  6034. ssd->pvp_won++;
  6035. }
  6036. if( sd->pvp_point < 0 )
  6037. {
  6038. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  6039. return 1|8;
  6040. }
  6041. }
  6042. //GvG
  6043. if( map_flag_gvg(sd->bl.m) )
  6044. {
  6045. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6046. return 1|8;
  6047. }
  6048. else if( sd->bg_id )
  6049. {
  6050. struct battleground_data *bg = bg_team_search(sd->bg_id);
  6051. if( bg && bg->mapindex > 0 )
  6052. { // Respawn by BG
  6053. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  6054. return 1|8;
  6055. }
  6056. }
  6057. //Reset "can log out" tick.
  6058. if( battle_config.prevent_logout )
  6059. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  6060. return 1;
  6061. }
  6062. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  6063. if(hp) clif_updatestatus(sd,SP_HP);
  6064. if(sp) clif_updatestatus(sd,SP_SP);
  6065. pc_setstand(sd);
  6066. if(battle_config.pc_invincible_time > 0)
  6067. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6068. if( sd->state.gmaster_flag ) {
  6069. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->state.gmaster_flag,GD_LEADERSHIP));
  6070. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->state.gmaster_flag,GD_GLORYWOUNDS));
  6071. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->state.gmaster_flag,GD_SOULCOLD));
  6072. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->state.gmaster_flag,GD_HAWKEYES));
  6073. }
  6074. }
  6075. // script
  6076. //
  6077. /*==========================================
  6078. * script reading pc status registry
  6079. *------------------------------------------*/
  6080. int pc_readparam(struct map_session_data* sd,int type)
  6081. {
  6082. int val = 0;
  6083. nullpo_ret(sd);
  6084. switch(type) {
  6085. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  6086. case SP_STATUSPOINT: val = sd->status.status_point; break;
  6087. case SP_ZENY: val = sd->status.zeny; break;
  6088. case SP_BASELEVEL: val = sd->status.base_level; break;
  6089. case SP_JOBLEVEL: val = sd->status.job_level; break;
  6090. case SP_CLASS: val = sd->status.class_; break;
  6091. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  6092. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  6093. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  6094. case SP_SEX: val = sd->status.sex; break;
  6095. case SP_WEIGHT: val = sd->weight; break;
  6096. case SP_MAXWEIGHT: val = sd->max_weight; break;
  6097. case SP_BASEEXP: val = sd->status.base_exp; break;
  6098. case SP_JOBEXP: val = sd->status.job_exp; break;
  6099. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  6100. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  6101. case SP_HP: val = sd->battle_status.hp; break;
  6102. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  6103. case SP_SP: val = sd->battle_status.sp; break;
  6104. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  6105. case SP_STR: val = sd->status.str; break;
  6106. case SP_AGI: val = sd->status.agi; break;
  6107. case SP_VIT: val = sd->status.vit; break;
  6108. case SP_INT: val = sd->status.int_; break;
  6109. case SP_DEX: val = sd->status.dex; break;
  6110. case SP_LUK: val = sd->status.luk; break;
  6111. case SP_KARMA: val = sd->status.karma; break;
  6112. case SP_MANNER: val = sd->status.manner; break;
  6113. case SP_FAME: val = sd->status.fame; break;
  6114. case SP_KILLERRID: val = sd->killerrid; break;
  6115. case SP_KILLEDRID: val = sd->killedrid; break;
  6116. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  6117. case SP_CHARMOVE: val = sd->status.character_moves; break;
  6118. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  6119. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  6120. case SP_BASE_ATK: val = sd->battle_status.batk; break;
  6121. case SP_DEF1: val = sd->battle_status.def; break;
  6122. case SP_DEF2: val = sd->battle_status.def2; break;
  6123. case SP_MDEF1: val = sd->battle_status.mdef; break;
  6124. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  6125. case SP_HIT: val = sd->battle_status.hit; break;
  6126. case SP_FLEE1: val = sd->battle_status.flee; break;
  6127. case SP_FLEE2: val = sd->battle_status.flee2; break;
  6128. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  6129. #ifndef RENEWAL_CAST
  6130. case SP_VARCASTRATE:
  6131. #endif
  6132. case SP_CASTRATE:
  6133. val = sd->castrate+=val;
  6134. break;
  6135. case SP_MAXHPRATE: val = sd->hprate; break;
  6136. case SP_MAXSPRATE: val = sd->sprate; break;
  6137. case SP_SPRATE: val = sd->dsprate; break;
  6138. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  6139. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  6140. case SP_ASPD_RATE:
  6141. #ifndef RENEWAL_ASPD
  6142. val = sd->battle_status.aspd_rate;
  6143. #else
  6144. val = sd->battle_status.aspd_rate2;
  6145. #endif
  6146. break;
  6147. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  6148. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  6149. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  6150. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  6151. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  6152. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  6153. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  6154. case SP_MATK_RATE: val = sd->matk_rate; break;
  6155. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  6156. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  6157. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  6158. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  6159. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  6160. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  6161. case SP_HIT_RATE: val = sd->hit_rate; break;
  6162. case SP_FLEE_RATE: val = sd->flee_rate; break;
  6163. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  6164. case SP_DEF_RATE: val = sd->def_rate; break;
  6165. case SP_DEF2_RATE: val = sd->def2_rate; break;
  6166. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  6167. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  6168. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  6169. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  6170. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  6171. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  6172. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  6173. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  6174. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  6175. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  6176. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  6177. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  6178. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  6179. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  6180. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  6181. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  6182. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  6183. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  6184. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  6185. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  6186. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  6187. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  6188. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  6189. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  6190. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  6191. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  6192. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  6193. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  6194. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  6195. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  6196. case SP_DELAYRATE: val = sd->delayrate; break;
  6197. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  6198. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  6199. case SP_UNSTRIPABLE:
  6200. case SP_UNSTRIPABLE_ARMOR:
  6201. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  6202. break;
  6203. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  6204. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  6205. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  6206. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  6207. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  6208. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  6209. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  6210. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  6211. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  6212. case SP_EMATK: val = sd->bonus.ematk; break;
  6213. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  6214. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  6215. #ifdef RENEWAL_CAST
  6216. case SP_VARCASTRATE: val = sd->bonus.varcastrate; break;
  6217. case SP_ADD_VARIABLECAST:val = sd->bonus.add_varcast; break;
  6218. #endif
  6219. }
  6220. return val;
  6221. }
  6222. /*==========================================
  6223. * script set pc status registry
  6224. *------------------------------------------*/
  6225. int pc_setparam(struct map_session_data *sd,int type,int val)
  6226. {
  6227. int i = 0;
  6228. nullpo_ret(sd);
  6229. switch(type){
  6230. case SP_BASELEVEL:
  6231. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  6232. val = pc_maxbaselv(sd);
  6233. if ((unsigned int)val > sd->status.base_level) {
  6234. int stat=0;
  6235. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  6236. stat += pc_gets_status_point(sd->status.base_level + i);
  6237. sd->status.status_point += stat;
  6238. }
  6239. sd->status.base_level = (unsigned int)val;
  6240. sd->status.base_exp = 0;
  6241. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  6242. clif_updatestatus(sd, SP_NEXTBASEEXP);
  6243. clif_updatestatus(sd, SP_STATUSPOINT);
  6244. clif_updatestatus(sd, SP_BASEEXP);
  6245. status_calc_pc(sd, 0);
  6246. if(sd->status.party_id)
  6247. {
  6248. party_send_levelup(sd);
  6249. }
  6250. break;
  6251. case SP_JOBLEVEL:
  6252. if ((unsigned int)val >= sd->status.job_level) {
  6253. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  6254. sd->status.skill_point += val - sd->status.job_level;
  6255. clif_updatestatus(sd, SP_SKILLPOINT);
  6256. }
  6257. sd->status.job_level = (unsigned int)val;
  6258. sd->status.job_exp = 0;
  6259. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  6260. clif_updatestatus(sd, SP_NEXTJOBEXP);
  6261. clif_updatestatus(sd, SP_JOBEXP);
  6262. status_calc_pc(sd, 0);
  6263. break;
  6264. case SP_SKILLPOINT:
  6265. sd->status.skill_point = val;
  6266. break;
  6267. case SP_STATUSPOINT:
  6268. sd->status.status_point = val;
  6269. break;
  6270. case SP_ZENY:
  6271. if( val < 0 )
  6272. return 0;// can't set negative zeny
  6273. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  6274. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  6275. break;
  6276. case SP_BASEEXP:
  6277. if(pc_nextbaseexp(sd) > 0) {
  6278. sd->status.base_exp = val;
  6279. pc_checkbaselevelup(sd);
  6280. }
  6281. break;
  6282. case SP_JOBEXP:
  6283. if(pc_nextjobexp(sd) > 0) {
  6284. sd->status.job_exp = val;
  6285. pc_checkjoblevelup(sd);
  6286. }
  6287. break;
  6288. case SP_SEX:
  6289. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  6290. break;
  6291. case SP_WEIGHT:
  6292. sd->weight = val;
  6293. break;
  6294. case SP_MAXWEIGHT:
  6295. sd->max_weight = val;
  6296. break;
  6297. case SP_HP:
  6298. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  6299. break;
  6300. case SP_MAXHP:
  6301. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  6302. if( sd->battle_status.max_hp < sd->battle_status.hp )
  6303. {
  6304. sd->battle_status.hp = sd->battle_status.max_hp;
  6305. clif_updatestatus(sd, SP_HP);
  6306. }
  6307. break;
  6308. case SP_SP:
  6309. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  6310. break;
  6311. case SP_MAXSP:
  6312. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  6313. if( sd->battle_status.max_sp < sd->battle_status.sp )
  6314. {
  6315. sd->battle_status.sp = sd->battle_status.max_sp;
  6316. clif_updatestatus(sd, SP_SP);
  6317. }
  6318. break;
  6319. case SP_STR:
  6320. sd->status.str = cap_value(val, 1, pc_maxparameter(sd));
  6321. break;
  6322. case SP_AGI:
  6323. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd));
  6324. break;
  6325. case SP_VIT:
  6326. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd));
  6327. break;
  6328. case SP_INT:
  6329. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd));
  6330. break;
  6331. case SP_DEX:
  6332. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd));
  6333. break;
  6334. case SP_LUK:
  6335. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd));
  6336. break;
  6337. case SP_KARMA:
  6338. sd->status.karma = val;
  6339. break;
  6340. case SP_MANNER:
  6341. sd->status.manner = val;
  6342. break;
  6343. case SP_FAME:
  6344. sd->status.fame = val;
  6345. break;
  6346. case SP_KILLERRID:
  6347. sd->killerrid = val;
  6348. return 1;
  6349. case SP_KILLEDRID:
  6350. sd->killedrid = val;
  6351. return 1;
  6352. case SP_CHARMOVE:
  6353. sd->status.character_moves = val;
  6354. return 1;
  6355. default:
  6356. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  6357. return 0;
  6358. }
  6359. clif_updatestatus(sd,type);
  6360. return 1;
  6361. }
  6362. /*==========================================
  6363. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  6364. *------------------------------------------*/
  6365. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  6366. {
  6367. if (type) {
  6368. if (hp)
  6369. clif_heal(sd->fd,SP_HP,hp);
  6370. if (sp)
  6371. clif_heal(sd->fd,SP_SP,sp);
  6372. } else {
  6373. if(hp)
  6374. clif_updatestatus(sd,SP_HP);
  6375. if(sp)
  6376. clif_updatestatus(sd,SP_SP);
  6377. }
  6378. return;
  6379. }
  6380. /*==========================================
  6381. * HP/SP Recovery
  6382. * Heal player hp and/or sp linearly.
  6383. * Calculate bonus by status.
  6384. *------------------------------------------*/
  6385. int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
  6386. {
  6387. int bonus;
  6388. if(hp) {
  6389. int i;
  6390. bonus = 100 + (sd->battle_status.vit<<1)
  6391. + pc_checkskill(sd,SM_RECOVERY)*10
  6392. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6393. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  6394. if (potion_flag > 1)
  6395. bonus += bonus*(potion_flag-1)*50/100;
  6396. //All item bonuses.
  6397. bonus += sd->bonus.itemhealrate2;
  6398. //Item Group bonuses
  6399. bonus += bonus*itemdb_group_bonus(sd, itemid)/100;
  6400. //Individual item bonuses.
  6401. for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++)
  6402. {
  6403. if (sd->itemhealrate[i].nameid == itemid) {
  6404. bonus += bonus*sd->itemhealrate[i].rate/100;
  6405. break;
  6406. }
  6407. }
  6408. if(bonus!=100)
  6409. hp = hp * bonus / 100;
  6410. // Recovery Potion
  6411. if( sd->sc.data[SC_INCHEALRATE] )
  6412. hp += (int)(hp * sd->sc.data[SC_INCHEALRATE]->val1/100.);
  6413. }
  6414. if(sp) {
  6415. bonus = 100 + (sd->battle_status.int_<<1)
  6416. + pc_checkskill(sd,MG_SRECOVERY)*10
  6417. + pc_checkskill(sd,AM_LEARNINGPOTION)*5;
  6418. if (potion_flag > 1)
  6419. bonus += bonus*(potion_flag-1)*50/100;
  6420. if(bonus != 100)
  6421. sp = sp * bonus / 100;
  6422. }
  6423. if( sd->sc.count ) {
  6424. if ( sd->sc.data[SC_CRITICALWOUND] ) {
  6425. hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6426. sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
  6427. }
  6428. if ( sd->sc.data[SC_DEATHHURT] ) {
  6429. hp -= hp * 20 / 100;
  6430. sp -= sp * 20 / 100;
  6431. }
  6432. if( sd->sc.data[SC_VITALITYACTIVATION] ){
  6433. hp += hp / 2; // 1.5 times
  6434. sp -= sp / 2;
  6435. }
  6436. if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
  6437. hp += hp / 10;
  6438. sp += sp / 10;
  6439. }
  6440. #ifdef RENEWAL
  6441. if( sd->sc.data[SC_EXTREMITYFIST2] )
  6442. sp = 0;
  6443. #endif
  6444. }
  6445. return status_heal(&sd->bl, hp, sp, 1);
  6446. }
  6447. /*==========================================
  6448. * HP/SP Recovery
  6449. * Heal player hp nad/or sp by rate
  6450. *------------------------------------------*/
  6451. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  6452. {
  6453. nullpo_ret(sd);
  6454. if(hp > 100) hp = 100;
  6455. else
  6456. if(hp <-100) hp =-100;
  6457. if(sp > 100) sp = 100;
  6458. else
  6459. if(sp <-100) sp =-100;
  6460. if(hp >= 0 && sp >= 0) //Heal
  6461. return status_percent_heal(&sd->bl, hp, sp);
  6462. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  6463. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  6464. //Crossed signs
  6465. if(hp) {
  6466. if(hp > 0)
  6467. status_percent_heal(&sd->bl, hp, 0);
  6468. else
  6469. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  6470. }
  6471. if(sp) {
  6472. if(sp > 0)
  6473. status_percent_heal(&sd->bl, 0, sp);
  6474. else
  6475. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  6476. }
  6477. return 0;
  6478. }
  6479. static int jobchange_killclone(struct block_list *bl, va_list ap)
  6480. {
  6481. struct mob_data *md;
  6482. int flag;
  6483. md = (struct mob_data *)bl;
  6484. nullpo_ret(md);
  6485. flag = va_arg(ap, int);
  6486. if (md->master_id && md->special_state.clone && md->master_id == flag)
  6487. status_kill(&md->bl);
  6488. return 1;
  6489. }
  6490. /*==========================================
  6491. * Called when player changes job
  6492. * Rewrote to make it tidider [Celest]
  6493. *------------------------------------------*/
  6494. int pc_jobchange(struct map_session_data *sd,int job, int upper)
  6495. {
  6496. int i, fame_flag=0;
  6497. int b_class;
  6498. nullpo_ret(sd);
  6499. if (job < 0)
  6500. return 1;
  6501. //Normalize job.
  6502. b_class = pc_jobid2mapid(job);
  6503. if (b_class == -1)
  6504. return 1;
  6505. switch (upper) {
  6506. case 1:
  6507. b_class|= JOBL_UPPER;
  6508. break;
  6509. case 2:
  6510. b_class|= JOBL_BABY;
  6511. break;
  6512. }
  6513. //This will automatically adjust bard/dancer classes to the correct gender
  6514. //That is, if you try to jobchange into dancer, it will turn you to bard.
  6515. job = pc_mapid2jobid(b_class, sd->status.sex);
  6516. if (job == -1)
  6517. return 1;
  6518. if ((unsigned short)b_class == sd->class_)
  6519. return 1; //Nothing to change.
  6520. // changing from 1st to 2nd job
  6521. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (b_class&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  6522. sd->change_level_2nd = sd->status.job_level;
  6523. pc_setglobalreg (sd, "jobchange_level", sd->change_level_2nd);
  6524. }
  6525. // changing from 2nd to 3rd job
  6526. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  6527. sd->change_level_3rd = sd->status.job_level;
  6528. pc_setglobalreg (sd, "jobchange_level_3rd", sd->change_level_3rd);
  6529. }
  6530. if(sd->cloneskill_id) {
  6531. if( sd->status.skill[sd->cloneskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6532. sd->status.skill[sd->cloneskill_id].id = 0;
  6533. sd->status.skill[sd->cloneskill_id].lv = 0;
  6534. sd->status.skill[sd->cloneskill_id].flag = SKILL_FLAG_PERMANENT;
  6535. clif_deleteskill(sd,sd->cloneskill_id);
  6536. }
  6537. sd->cloneskill_id = 0;
  6538. pc_setglobalreg(sd, "CLONE_SKILL", 0);
  6539. pc_setglobalreg(sd, "CLONE_SKILL_LV", 0);
  6540. }
  6541. if(sd->reproduceskill_id) {
  6542. if( sd->status.skill[sd->reproduceskill_id].flag == SKILL_FLAG_PLAGIARIZED ) {
  6543. sd->status.skill[sd->reproduceskill_id].id = 0;
  6544. sd->status.skill[sd->reproduceskill_id].lv = 0;
  6545. sd->status.skill[sd->reproduceskill_id].flag = SKILL_FLAG_PERMANENT;
  6546. clif_deleteskill(sd,sd->reproduceskill_id);
  6547. }
  6548. sd->reproduceskill_id = 0;
  6549. pc_setglobalreg(sd, "REPRODUCE_SKILL",0);
  6550. pc_setglobalreg(sd, "REPRODUCE_SKILL_LV",0);
  6551. }
  6552. // Give or reduce transcendent status points
  6553. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  6554. sd->status.status_point += 52;
  6555. clif_updatestatus(sd,SP_STATUSPOINT);
  6556. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  6557. if( sd->status.status_point < 52 ){
  6558. // The player already used his bonus points, so we have to reset his status points
  6559. pc_resetstate(sd);
  6560. }
  6561. sd->status.status_point -= 52;
  6562. clif_updatestatus(sd,SP_STATUSPOINT);
  6563. }
  6564. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  6565. const int class_ = pc_class2idx(sd->status.class_);
  6566. short id;
  6567. for(i = 0; i < MAX_SKILL_TREE && (id = skill_tree[class_][i].id) > 0; i++) {
  6568. //Remove status specific to your current tree skills.
  6569. enum sc_type sc = status_skill2sc(id);
  6570. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  6571. status_change_end(&sd->bl, sc, INVALID_TIMER);
  6572. }
  6573. }
  6574. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  6575. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  6576. pc_resetfeel(sd);
  6577. }
  6578. sd->status.class_ = job;
  6579. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  6580. sd->class_ = (unsigned short)b_class;
  6581. sd->status.job_level=1;
  6582. sd->status.job_exp=0;
  6583. if (sd->status.base_level > pc_maxbaselv(sd)) {
  6584. sd->status.base_level = pc_maxbaselv(sd);
  6585. sd->status.base_exp=0;
  6586. pc_resetstate(sd);
  6587. clif_updatestatus(sd,SP_STATUSPOINT);
  6588. clif_updatestatus(sd,SP_BASELEVEL);
  6589. clif_updatestatus(sd,SP_BASEEXP);
  6590. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6591. }
  6592. clif_updatestatus(sd,SP_JOBLEVEL);
  6593. clif_updatestatus(sd,SP_JOBEXP);
  6594. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6595. for(i=0;i<EQI_MAX;i++) {
  6596. if(sd->equip_index[i] >= 0)
  6597. if(!pc_isequip(sd,sd->equip_index[i]))
  6598. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  6599. }
  6600. //Change look, if disguised, you need to undisguise
  6601. //to correctly calculate new job sprite without
  6602. if (sd->disguise)
  6603. pc_disguise(sd, 0);
  6604. status_set_viewdata(&sd->bl, job);
  6605. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  6606. if(sd->vd.cloth_color)
  6607. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6608. //Update skill tree.
  6609. pc_calc_skilltree(sd);
  6610. clif_skillinfoblock(sd);
  6611. if (sd->ed)
  6612. elemental_delete(sd->ed, 0);
  6613. if (sd->state.vending)
  6614. vending_closevending(sd);
  6615. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  6616. //Remove peco/cart/falcon
  6617. i = sd->sc.option;
  6618. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  6619. i&=~OPTION_RIDING;
  6620. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  6621. i&=~OPTION_FALCON;
  6622. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  6623. i&=~OPTION_DRAGON;
  6624. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  6625. i&=~OPTION_WUGRIDER;
  6626. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  6627. i&=~OPTION_WUG;
  6628. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  6629. i&=~OPTION_MADOGEAR;
  6630. #ifndef NEW_CARTS
  6631. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  6632. i&=~OPTION_CART;
  6633. #else
  6634. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  6635. pc_setcart(sd, 0);
  6636. #endif
  6637. if(i != sd->sc.option)
  6638. pc_setoption(sd, i);
  6639. if(merc_is_hom_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  6640. merc_hom_vaporize(sd, HOM_ST_ACTIVE);
  6641. if(sd->status.manner < 0)
  6642. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  6643. status_calc_pc(sd,0);
  6644. pc_checkallowskill(sd);
  6645. pc_equiplookall(sd);
  6646. //if you were previously famous, not anymore.
  6647. if (fame_flag) {
  6648. chrif_save(sd,0);
  6649. chrif_buildfamelist();
  6650. } else if (sd->status.fame > 0) {
  6651. //It may be that now they are famous?
  6652. switch (sd->class_&MAPID_UPPERMASK) {
  6653. case MAPID_BLACKSMITH:
  6654. case MAPID_ALCHEMIST:
  6655. case MAPID_TAEKWON:
  6656. chrif_save(sd,0);
  6657. chrif_buildfamelist();
  6658. break;
  6659. }
  6660. }
  6661. return 0;
  6662. }
  6663. /*==========================================
  6664. * Tell client player sd has change equipement
  6665. *------------------------------------------*/
  6666. int pc_equiplookall(struct map_session_data *sd)
  6667. {
  6668. nullpo_ret(sd);
  6669. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  6670. clif_changelook(&sd->bl,LOOK_SHOES,0);
  6671. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  6672. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  6673. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  6674. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  6675. return 0;
  6676. }
  6677. /*==========================================
  6678. * Tell client player sd has change look (hair,equip...)
  6679. *------------------------------------------*/
  6680. int pc_changelook(struct map_session_data *sd,int type,int val)
  6681. {
  6682. nullpo_ret(sd);
  6683. switch(type){
  6684. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  6685. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  6686. if (sd->status.hair != val)
  6687. {
  6688. sd->status.hair=val;
  6689. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6690. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6691. GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
  6692. }
  6693. break;
  6694. case LOOK_WEAPON:
  6695. sd->status.weapon=val;
  6696. break;
  6697. case LOOK_HEAD_BOTTOM:
  6698. sd->status.head_bottom=val;
  6699. break;
  6700. case LOOK_HEAD_TOP:
  6701. sd->status.head_top=val;
  6702. break;
  6703. case LOOK_HEAD_MID:
  6704. sd->status.head_mid=val;
  6705. break;
  6706. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  6707. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  6708. if (sd->status.hair_color != val)
  6709. {
  6710. sd->status.hair_color=val;
  6711. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  6712. intif_guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
  6713. GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
  6714. }
  6715. break;
  6716. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  6717. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  6718. sd->status.clothes_color=val;
  6719. break;
  6720. case LOOK_SHIELD:
  6721. sd->status.shield=val;
  6722. break;
  6723. case LOOK_SHOES:
  6724. break;
  6725. case LOOK_ROBE:
  6726. sd->status.robe = val;
  6727. break;
  6728. }
  6729. clif_changelook(&sd->bl,type,val);
  6730. return 0;
  6731. }
  6732. /*==========================================
  6733. * Give an option (type) to player (sd) and display it to client
  6734. *------------------------------------------*/
  6735. int pc_setoption(struct map_session_data *sd,int type)
  6736. {
  6737. int p_type, new_look=0;
  6738. nullpo_ret(sd);
  6739. p_type = sd->sc.option;
  6740. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  6741. sd->sc.option=type;
  6742. clif_changeoption(&sd->bl);
  6743. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6744. { // Mounting
  6745. clif_status_load(&sd->bl,SI_RIDING,1);
  6746. status_calc_pc(sd,0);
  6747. }
  6748. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  6749. { // Dismount
  6750. clif_status_load(&sd->bl,SI_RIDING,0);
  6751. status_calc_pc(sd,0);
  6752. }
  6753. #ifndef NEW_CARTS
  6754. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  6755. clif_cartlist(sd);
  6756. clif_updatestatus(sd, SP_CARTINFO);
  6757. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6758. status_calc_pc(sd,0); //Apply speed penalty.
  6759. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  6760. clif_clearcart(sd->fd);
  6761. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6762. status_calc_pc(sd,0); //Remove speed penalty.
  6763. }
  6764. #endif
  6765. if (type&OPTION_MOUNTING && !(p_type&OPTION_MOUNTING) ) {
  6766. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,2,1,0,0);
  6767. status_calc_pc(sd,0);
  6768. } else if (!(type&OPTION_MOUNTING) && p_type&OPTION_MOUNTING) {
  6769. clif_status_load_notick(&sd->bl,SI_ALL_RIDING,0,0,0,0);
  6770. status_calc_pc(sd,0);
  6771. }
  6772. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  6773. clif_status_load(&sd->bl,SI_FALCON,1);
  6774. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  6775. clif_status_load(&sd->bl,SI_FALCON,0);
  6776. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  6777. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  6778. clif_status_load(&sd->bl,SI_WUGRIDER,1);
  6779. status_calc_pc(sd,0);
  6780. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  6781. clif_status_load(&sd->bl,SI_WUGRIDER,0);
  6782. status_calc_pc(sd,0);
  6783. }
  6784. }
  6785. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  6786. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  6787. status_calc_pc(sd, 0);
  6788. status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
  6789. status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
  6790. status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
  6791. status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
  6792. status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
  6793. status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
  6794. status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
  6795. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  6796. status_calc_pc(sd, 0);
  6797. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  6798. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  6799. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  6800. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  6801. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  6802. }
  6803. }
  6804. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  6805. new_look = JOB_STAR_GLADIATOR2;
  6806. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  6807. new_look = -1;
  6808. if (type&OPTION_WEDDING && !(p_type&OPTION_WEDDING))
  6809. new_look = JOB_WEDDING;
  6810. else if (!(type&OPTION_WEDDING) && p_type&OPTION_WEDDING)
  6811. new_look = -1;
  6812. if (type&OPTION_XMAS && !(p_type&OPTION_XMAS))
  6813. new_look = JOB_XMAS;
  6814. else if (!(type&OPTION_XMAS) && p_type&OPTION_XMAS)
  6815. new_look = -1;
  6816. if (type&OPTION_SUMMER && !(p_type&OPTION_SUMMER))
  6817. new_look = JOB_SUMMER;
  6818. else if (!(type&OPTION_SUMMER) && p_type&OPTION_SUMMER)
  6819. new_look = -1;
  6820. if (sd->disguise || !new_look)
  6821. return 0; //Disguises break sprite changes
  6822. if (new_look < 0) { //Restore normal look.
  6823. status_set_viewdata(&sd->bl, sd->status.class_);
  6824. new_look = sd->vd.class_;
  6825. }
  6826. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  6827. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  6828. if (sd->vd.cloth_color)
  6829. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  6830. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  6831. return 0;
  6832. }
  6833. /*==========================================
  6834. * Give player a cart
  6835. *------------------------------------------*/
  6836. int pc_setcart(struct map_session_data *sd,int type) {
  6837. #ifndef NEW_CARTS
  6838. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  6839. int option;
  6840. #endif
  6841. nullpo_ret(sd);
  6842. if( type < 0 || type > MAX_CARTS )
  6843. return 1;// Never trust the values sent by the client! [Skotlex]
  6844. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  6845. return 1;// Push cart is required
  6846. if( type == 0 && pc_iscarton(sd) )
  6847. status_change_end(&sd->bl,SC_GN_CARTBOOST,INVALID_TIMER);
  6848. #ifdef NEW_CARTS
  6849. switch( type ) {
  6850. case 0:
  6851. if( !sd->sc.data[SC_PUSH_CART] )
  6852. return 0;
  6853. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  6854. clif_clearcart(sd->fd);
  6855. break;
  6856. default:/* everything else is an allowed ID so we can move on */
  6857. if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
  6858. clif_cartlist(sd);
  6859. clif_updatestatus(sd, SP_CARTINFO);
  6860. sc_start(&sd->bl,&sd->bl, SC_PUSH_CART, 100, type, 0);
  6861. clif_status_load_notick(&sd->bl, SI_ON_PUSH_CART, 2 , type, 0, 0);
  6862. if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
  6863. sd->sc.data[SC_PUSH_CART]->val1 = type;
  6864. break;
  6865. }
  6866. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  6867. status_calc_pc(sd,0); //Recalc speed penalty.
  6868. #else
  6869. // Update option
  6870. option = sd->sc.option;
  6871. option &= ~OPTION_CART;// clear cart bits
  6872. option |= cart[type]; // set cart
  6873. pc_setoption(sd, option);
  6874. #endif
  6875. return 0;
  6876. }
  6877. /*==========================================
  6878. * Give player a falcon
  6879. *------------------------------------------*/
  6880. int pc_setfalcon(TBL_PC* sd, int flag)
  6881. {
  6882. if( flag ){
  6883. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  6884. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  6885. } else if( pc_isfalcon(sd) ){
  6886. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  6887. }
  6888. return 0;
  6889. }
  6890. /*==========================================
  6891. * Set player riding
  6892. *------------------------------------------*/
  6893. int pc_setriding(TBL_PC* sd, int flag)
  6894. {
  6895. if( flag ){
  6896. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  6897. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  6898. } else if( pc_isriding(sd) ){
  6899. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  6900. }
  6901. return 0;
  6902. }
  6903. /*==========================================
  6904. * Give player a mado
  6905. *------------------------------------------*/
  6906. int pc_setmadogear(TBL_PC* sd, int flag)
  6907. {
  6908. if( flag ){
  6909. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  6910. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  6911. } else if( pc_ismadogear(sd) ){
  6912. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  6913. }
  6914. return 0;
  6915. }
  6916. /*==========================================
  6917. * Check if player can drop an item
  6918. *------------------------------------------*/
  6919. int pc_candrop(struct map_session_data *sd, struct item *item)
  6920. {
  6921. if( item && item->expire_time )
  6922. return 0;
  6923. if( !pc_can_give_items(sd) ) //check if this GM level can drop items
  6924. return 0;
  6925. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  6926. }
  6927. /*==========================================
  6928. * Read ram register for player sd
  6929. * get val (int) from reg for player sd
  6930. *------------------------------------------*/
  6931. int pc_readreg(struct map_session_data* sd, int reg)
  6932. {
  6933. int i;
  6934. nullpo_ret(sd);
  6935. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  6936. return ( i < sd->reg_num ) ? sd->reg[i].data : 0;
  6937. }
  6938. /*==========================================
  6939. * Set ram register for player sd
  6940. * memo val(int) at reg for player sd
  6941. *------------------------------------------*/
  6942. int pc_setreg(struct map_session_data* sd, int reg, int val)
  6943. {
  6944. int i;
  6945. nullpo_ret(sd);
  6946. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].index == reg );
  6947. if( i < sd->reg_num )
  6948. {// overwrite existing entry
  6949. sd->reg[i].data = val;
  6950. return 1;
  6951. }
  6952. ARR_FIND( 0, sd->reg_num, i, sd->reg[i].data == 0 );
  6953. if( i == sd->reg_num )
  6954. {// nothing free, increase size
  6955. sd->reg_num++;
  6956. RECREATE(sd->reg, struct script_reg, sd->reg_num);
  6957. }
  6958. sd->reg[i].index = reg;
  6959. sd->reg[i].data = val;
  6960. return 1;
  6961. }
  6962. /*==========================================
  6963. * Read ram register for player sd
  6964. * get val (str) from reg for player sd
  6965. *------------------------------------------*/
  6966. char* pc_readregstr(struct map_session_data* sd, int reg)
  6967. {
  6968. int i;
  6969. nullpo_ret(sd);
  6970. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  6971. return ( i < sd->regstr_num ) ? sd->regstr[i].data : NULL;
  6972. }
  6973. /*==========================================
  6974. * Set ram register for player sd
  6975. * memo val(str) at reg for player sd
  6976. *------------------------------------------*/
  6977. int pc_setregstr(struct map_session_data* sd, int reg, const char* str)
  6978. {
  6979. int i;
  6980. nullpo_ret(sd);
  6981. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].index == reg );
  6982. if( i < sd->regstr_num )
  6983. {// found entry, update
  6984. if( str == NULL || *str == '\0' )
  6985. {// empty string
  6986. if( sd->regstr[i].data != NULL )
  6987. aFree(sd->regstr[i].data);
  6988. sd->regstr[i].data = NULL;
  6989. }
  6990. else if( sd->regstr[i].data )
  6991. {// recreate
  6992. size_t len = strlen(str)+1;
  6993. RECREATE(sd->regstr[i].data, char, len);
  6994. memcpy(sd->regstr[i].data, str, len*sizeof(char));
  6995. }
  6996. else
  6997. {// create
  6998. sd->regstr[i].data = aStrdup(str);
  6999. }
  7000. return 1;
  7001. }
  7002. if( str == NULL || *str == '\0' )
  7003. return 1;// nothing to add, empty string
  7004. ARR_FIND( 0, sd->regstr_num, i, sd->regstr[i].data == NULL );
  7005. if( i == sd->regstr_num )
  7006. {// nothing free, increase size
  7007. sd->regstr_num++;
  7008. RECREATE(sd->regstr, struct script_regstr, sd->regstr_num);
  7009. }
  7010. sd->regstr[i].index = reg;
  7011. sd->regstr[i].data = aStrdup(str);
  7012. return 1;
  7013. }
  7014. int pc_readregistry(struct map_session_data *sd,const char *reg,int type)
  7015. {
  7016. struct global_reg *sd_reg;
  7017. int i,max;
  7018. nullpo_ret(sd);
  7019. switch (type) {
  7020. case 3: //Char reg
  7021. sd_reg = sd->save_reg.global;
  7022. max = sd->save_reg.global_num;
  7023. break;
  7024. case 2: //Account reg
  7025. sd_reg = sd->save_reg.account;
  7026. max = sd->save_reg.account_num;
  7027. break;
  7028. case 1: //Account2 reg
  7029. sd_reg = sd->save_reg.account2;
  7030. max = sd->save_reg.account2_num;
  7031. break;
  7032. default:
  7033. return 0;
  7034. }
  7035. if (max == -1) {
  7036. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7037. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7038. intif_request_registry(sd,type==3?4:type);
  7039. return 0;
  7040. }
  7041. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7042. return ( i < max ) ? atoi(sd_reg[i].value) : 0;
  7043. }
  7044. char* pc_readregistry_str(struct map_session_data *sd,const char *reg,int type)
  7045. {
  7046. struct global_reg *sd_reg;
  7047. int i,max;
  7048. nullpo_ret(sd);
  7049. switch (type) {
  7050. case 3: //Char reg
  7051. sd_reg = sd->save_reg.global;
  7052. max = sd->save_reg.global_num;
  7053. break;
  7054. case 2: //Account reg
  7055. sd_reg = sd->save_reg.account;
  7056. max = sd->save_reg.account_num;
  7057. break;
  7058. case 1: //Account2 reg
  7059. sd_reg = sd->save_reg.account2;
  7060. max = sd->save_reg.account2_num;
  7061. break;
  7062. default:
  7063. return NULL;
  7064. }
  7065. if (max == -1) {
  7066. ShowError("pc_readregistry: Trying to read reg value %s (type %d) before it's been loaded!\n", reg, type);
  7067. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  7068. intif_request_registry(sd,type==3?4:type);
  7069. return NULL;
  7070. }
  7071. ARR_FIND( 0, max, i, strcmp(sd_reg[i].str,reg) == 0 );
  7072. return ( i < max ) ? sd_reg[i].value : NULL;
  7073. }
  7074. int pc_setregistry(struct map_session_data *sd,const char *reg,int val,int type)
  7075. {
  7076. struct global_reg *sd_reg;
  7077. int i,*max, regmax;
  7078. nullpo_ret(sd);
  7079. switch( type )
  7080. {
  7081. case 3: //Char reg
  7082. if( !strcmp(reg,"PC_DIE_COUNTER") && sd->die_counter != val )
  7083. {
  7084. i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
  7085. sd->die_counter = val;
  7086. if( i )
  7087. status_calc_pc(sd,0); // Lost the bonus.
  7088. }
  7089. else if( !strcmp(reg,"COOK_MASTERY") && sd->cook_mastery != val )
  7090. {
  7091. val = cap_value(val, 0, 1999);
  7092. sd->cook_mastery = val;
  7093. }
  7094. sd_reg = sd->save_reg.global;
  7095. max = &sd->save_reg.global_num;
  7096. regmax = GLOBAL_REG_NUM;
  7097. break;
  7098. case 2: //Account reg
  7099. if( !strcmp(reg,"#CASHPOINTS") && sd->cashPoints != val )
  7100. {
  7101. val = cap_value(val, 0, MAX_ZENY);
  7102. sd->cashPoints = val;
  7103. }
  7104. else if( !strcmp(reg,"#KAFRAPOINTS") && sd->kafraPoints != val )
  7105. {
  7106. val = cap_value(val, 0, MAX_ZENY);
  7107. sd->kafraPoints = val;
  7108. }
  7109. sd_reg = sd->save_reg.account;
  7110. max = &sd->save_reg.account_num;
  7111. regmax = ACCOUNT_REG_NUM;
  7112. break;
  7113. case 1: //Account2 reg
  7114. sd_reg = sd->save_reg.account2;
  7115. max = &sd->save_reg.account2_num;
  7116. regmax = ACCOUNT_REG2_NUM;
  7117. break;
  7118. default:
  7119. return 0;
  7120. }
  7121. if (*max == -1) {
  7122. ShowError("pc_setregistry : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7123. return 1;
  7124. }
  7125. // delete reg
  7126. if (val == 0) {
  7127. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7128. if( i < *max )
  7129. {
  7130. if (i != *max - 1)
  7131. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7132. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7133. (*max)--;
  7134. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7135. }
  7136. return 1;
  7137. }
  7138. // change value if found
  7139. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7140. if( i < *max )
  7141. {
  7142. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7143. sd->state.reg_dirty |= 1<<(type-1);
  7144. return 1;
  7145. }
  7146. // add value if not found
  7147. if (i < regmax) {
  7148. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7149. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7150. safesnprintf(sd_reg[i].value, sizeof(sd_reg[i].value), "%d", val);
  7151. (*max)++;
  7152. sd->state.reg_dirty |= 1<<(type-1);
  7153. return 1;
  7154. }
  7155. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7156. return 0;
  7157. }
  7158. int pc_setregistry_str(struct map_session_data *sd,const char *reg,const char *val,int type)
  7159. {
  7160. struct global_reg *sd_reg;
  7161. int i,*max, regmax;
  7162. nullpo_ret(sd);
  7163. if (reg[strlen(reg)-1] != '$') {
  7164. ShowError("pc_setregistry_str : reg %s must be string (end in '$') to use this!\n", reg);
  7165. return 0;
  7166. }
  7167. switch (type) {
  7168. case 3: //Char reg
  7169. sd_reg = sd->save_reg.global;
  7170. max = &sd->save_reg.global_num;
  7171. regmax = GLOBAL_REG_NUM;
  7172. break;
  7173. case 2: //Account reg
  7174. sd_reg = sd->save_reg.account;
  7175. max = &sd->save_reg.account_num;
  7176. regmax = ACCOUNT_REG_NUM;
  7177. break;
  7178. case 1: //Account2 reg
  7179. sd_reg = sd->save_reg.account2;
  7180. max = &sd->save_reg.account2_num;
  7181. regmax = ACCOUNT_REG2_NUM;
  7182. break;
  7183. default:
  7184. return 0;
  7185. }
  7186. if (*max == -1) {
  7187. ShowError("pc_setregistry_str : refusing to set %s (type %d) until vars are received.\n", reg, type);
  7188. return 0;
  7189. }
  7190. // delete reg
  7191. if (!val || strcmp(val,"")==0)
  7192. {
  7193. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7194. if( i < *max )
  7195. {
  7196. if (i != *max - 1)
  7197. memcpy(&sd_reg[i], &sd_reg[*max - 1], sizeof(struct global_reg));
  7198. memset(&sd_reg[*max - 1], 0, sizeof(struct global_reg));
  7199. (*max)--;
  7200. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7201. if (type!=3) intif_saveregistry(sd,type);
  7202. }
  7203. return 1;
  7204. }
  7205. // change value if found
  7206. ARR_FIND( 0, *max, i, strcmp(sd_reg[i].str, reg) == 0 );
  7207. if( i < *max )
  7208. {
  7209. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7210. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7211. if (type!=3) intif_saveregistry(sd,type);
  7212. return 1;
  7213. }
  7214. // add value if not found
  7215. if (i < regmax) {
  7216. memset(&sd_reg[i], 0, sizeof(struct global_reg));
  7217. safestrncpy(sd_reg[i].str, reg, sizeof(sd_reg[i].str));
  7218. safestrncpy(sd_reg[i].value, val, sizeof(sd_reg[i].value));
  7219. (*max)++;
  7220. sd->state.reg_dirty |= 1<<(type-1); //Mark this registry as "need to be saved"
  7221. if (type!=3) intif_saveregistry(sd,type);
  7222. return 1;
  7223. }
  7224. ShowError("pc_setregistry : couldn't set %s, limit of registries reached (%d)\n", reg, regmax);
  7225. return 0;
  7226. }
  7227. /*==========================================
  7228. * Exec eventtimer for player sd (retrieved from map_session (id))
  7229. *------------------------------------------*/
  7230. static int pc_eventtimer(int tid, unsigned int tick, int id, intptr_t data)
  7231. {
  7232. struct map_session_data *sd=map_id2sd(id);
  7233. char *p = (char *)data;
  7234. int i;
  7235. if(sd==NULL)
  7236. return 0;
  7237. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  7238. if( i < MAX_EVENTTIMER )
  7239. {
  7240. sd->eventtimer[i] = INVALID_TIMER;
  7241. sd->eventcount--;
  7242. npc_event(sd,p,0);
  7243. }
  7244. else
  7245. ShowError("pc_eventtimer: no such event timer\n");
  7246. if (p) aFree(p);
  7247. return 0;
  7248. }
  7249. /*==========================================
  7250. * Add eventtimer for player sd ?
  7251. *------------------------------------------*/
  7252. int pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  7253. {
  7254. int i;
  7255. nullpo_ret(sd);
  7256. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  7257. if( i == MAX_EVENTTIMER )
  7258. return 0;
  7259. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  7260. sd->eventcount++;
  7261. return 1;
  7262. }
  7263. /*==========================================
  7264. * Del eventtimer for player sd ?
  7265. *------------------------------------------*/
  7266. int pc_deleventtimer(struct map_session_data *sd,const char *name)
  7267. {
  7268. char* p = NULL;
  7269. int i;
  7270. nullpo_ret(sd);
  7271. if (sd->eventcount <= 0)
  7272. return 0;
  7273. // find the named event timer
  7274. ARR_FIND( 0, MAX_EVENTTIMER, i,
  7275. sd->eventtimer[i] != INVALID_TIMER &&
  7276. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  7277. strcmp(p, name) == 0
  7278. );
  7279. if( i == MAX_EVENTTIMER )
  7280. return 0; // not found
  7281. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7282. sd->eventtimer[i] = INVALID_TIMER;
  7283. sd->eventcount--;
  7284. aFree(p);
  7285. return 1;
  7286. }
  7287. /*==========================================
  7288. * Update eventtimer count for player sd
  7289. *------------------------------------------*/
  7290. int pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  7291. {
  7292. int i;
  7293. nullpo_ret(sd);
  7294. for(i=0;i<MAX_EVENTTIMER;i++)
  7295. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  7296. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  7297. addtick_timer(sd->eventtimer[i],tick);
  7298. break;
  7299. }
  7300. return 0;
  7301. }
  7302. /*==========================================
  7303. * Remove all eventtimer for player sd
  7304. *------------------------------------------*/
  7305. int pc_cleareventtimer(struct map_session_data *sd)
  7306. {
  7307. int i;
  7308. nullpo_ret(sd);
  7309. if (sd->eventcount <= 0)
  7310. return 0;
  7311. for(i=0;i<MAX_EVENTTIMER;i++)
  7312. if( sd->eventtimer[i] != INVALID_TIMER ){
  7313. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  7314. delete_timer(sd->eventtimer[i],pc_eventtimer);
  7315. sd->eventtimer[i] = INVALID_TIMER;
  7316. sd->eventcount--;
  7317. if (p) aFree(p);
  7318. }
  7319. return 0;
  7320. }
  7321. /* called when a item with combo is worn */
  7322. int pc_checkcombo(struct map_session_data *sd, struct item_data *data ) {
  7323. int i, j, k, z;
  7324. int index, idx, success = 0;
  7325. for( i = 0; i < data->combos_count; i++ ) {
  7326. /* ensure this isn't a duplicate combo */
  7327. if( sd->combos.bonus != NULL ) {
  7328. int x;
  7329. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7330. /* found a match, skip this combo */
  7331. if( x < sd->combos.count )
  7332. continue;
  7333. }
  7334. for( j = 0; j < data->combos[i]->count; j++ ) {
  7335. int id = data->combos[i]->nameid[j];
  7336. bool found = false;
  7337. for( k = 0; k < EQI_MAX; k++ ) {
  7338. index = sd->equip_index[k];
  7339. if( index < 0 ) continue;
  7340. if( k == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
  7341. if( k == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
  7342. if( k == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;
  7343. if(!sd->inventory_data[index])
  7344. continue;
  7345. if ( itemdb_type(id) != IT_CARD ) {
  7346. if ( sd->inventory_data[index]->nameid != id )
  7347. continue;
  7348. found = true;
  7349. break;
  7350. } else { //Cards
  7351. if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
  7352. continue;
  7353. for (z = 0; z < sd->inventory_data[index]->slot; z++) {
  7354. if (sd->status.inventory[index].card[z] != id)
  7355. continue;
  7356. // We have found a match
  7357. found = true;
  7358. break;
  7359. }
  7360. }
  7361. }
  7362. if( !found )
  7363. break;/* we haven't found all the ids for this combo, so we can return */
  7364. }
  7365. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  7366. if( j < data->combos[i]->count )
  7367. continue;
  7368. /* we got here, means all items in the combo are matching */
  7369. idx = sd->combos.count;
  7370. if( sd->combos.bonus == NULL ) {
  7371. CREATE(sd->combos.bonus, struct script_code *, 1);
  7372. CREATE(sd->combos.id, unsigned short, 1);
  7373. sd->combos.count = 1;
  7374. } else {
  7375. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  7376. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  7377. }
  7378. /* we simply copy the pointer */
  7379. sd->combos.bonus[idx] = data->combos[i]->script;
  7380. /* save this combo's id */
  7381. sd->combos.id[idx] = data->combos[i]->id;
  7382. success++;
  7383. }
  7384. return success;
  7385. }
  7386. /* called when a item with combo is removed */
  7387. int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  7388. int i, retval = 0;
  7389. if( sd->combos.bonus == NULL )
  7390. return 0;/* nothing to do here, player has no combos */
  7391. for( i = 0; i < data->combos_count; i++ ) {
  7392. /* check if this combo exists in this user */
  7393. int x = 0, cursor = 0, j;
  7394. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  7395. /* no match, skip this combo */
  7396. if( !(x < sd->combos.count) )
  7397. continue;
  7398. sd->combos.bonus[x] = NULL;
  7399. sd->combos.id[x] = 0;
  7400. retval++;
  7401. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  7402. if( sd->combos.bonus[j] == NULL )
  7403. continue;
  7404. if( cursor != j ) {
  7405. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  7406. sd->combos.id[cursor] = sd->combos.id[j];
  7407. }
  7408. cursor++;
  7409. }
  7410. /* check if combo requirements still fit */
  7411. if( pc_checkcombo( sd, data ) )
  7412. continue;
  7413. /* it's empty, we can clear all the memory */
  7414. if( (sd->combos.count = cursor) == 0 ) {
  7415. aFree(sd->combos.bonus);
  7416. aFree(sd->combos.id);
  7417. sd->combos.bonus = NULL;
  7418. sd->combos.id = NULL;
  7419. return retval; /* we also can return at this point for we have no more combos to check */
  7420. }
  7421. }
  7422. return retval;
  7423. }
  7424. int pc_load_combo(struct map_session_data *sd) {
  7425. int i, ret = 0;
  7426. for( i = 0; i < EQI_MAX; i++ ) {
  7427. struct item_data *id = NULL;
  7428. int idx = sd->equip_index[i];
  7429. if( sd->equip_index[i] < 0 || !(id = sd->inventory_data[idx] ) )
  7430. continue;
  7431. if( id->combos_count )
  7432. ret += pc_checkcombo(sd,id);
  7433. if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
  7434. struct item_data *data;
  7435. int j;
  7436. for( j = 0; j < id->slot; j++ ) {
  7437. if (!sd->status.inventory[idx].card[j])
  7438. continue;
  7439. if ( ( data = itemdb_exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
  7440. if( data->combos_count )
  7441. ret += pc_checkcombo(sd,data);
  7442. }
  7443. }
  7444. }
  7445. }
  7446. return ret;
  7447. }
  7448. /*==========================================
  7449. * Equip item on player sd at req_pos from inventory index n
  7450. *------------------------------------------*/
  7451. int pc_equipitem(struct map_session_data *sd,int n,int req_pos)
  7452. {
  7453. int i,pos,flag=0,iflag;
  7454. struct item_data *id;
  7455. nullpo_ret(sd);
  7456. if( n < 0 || n >= MAX_INVENTORY ) {
  7457. clif_equipitemack(sd,0,0,0);
  7458. return 0;
  7459. }
  7460. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 )
  7461. {
  7462. clif_equipitemack(sd,n,0,0);
  7463. return 0;
  7464. }
  7465. id = sd->inventory_data[n];
  7466. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  7467. if(battle_config.battle_log)
  7468. ShowInfo("equip %d(%d) %x:%x\n",sd->status.inventory[n].nameid,n,id?id->equip:0,req_pos);
  7469. if(!pc_isequip(sd,n) || !(pos&req_pos) || sd->status.inventory[n].equip != 0 || sd->status.inventory[n].attribute==1 ) { // [Valaris]
  7470. // FIXME: pc_isequip: equip level failure uses 2 instead of 0
  7471. clif_equipitemack(sd,n,0,0); // fail
  7472. return 0;
  7473. }
  7474. if (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] || sd->sc.data[SC__BLOODYLUST])
  7475. {
  7476. clif_equipitemack(sd,n,0,0); // fail
  7477. return 0;
  7478. }
  7479. if(pos == EQP_ACC) { //Accesories should only go in one of the two,
  7480. pos = req_pos&EQP_ACC;
  7481. if (pos == EQP_ACC) //User specified both slots..
  7482. pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  7483. }
  7484. if(pos == EQP_ARMS && id->equip == EQP_HAND_R)
  7485. { //Dual wield capable weapon.
  7486. pos = (req_pos&EQP_ARMS);
  7487. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  7488. pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  7489. }
  7490. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC)
  7491. { //Update skill-block range database when weapon range changes. [Skotlex]
  7492. i = sd->equip_index[EQI_HAND_R];
  7493. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  7494. flag = 1;
  7495. else
  7496. flag = id->range != sd->inventory_data[i]->range;
  7497. }
  7498. for(i=0;i<EQI_MAX;i++) {
  7499. if(pos & equip_pos[i]) {
  7500. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  7501. pc_unequipitem(sd,sd->equip_index[i],2);
  7502. sd->equip_index[i] = n;
  7503. }
  7504. }
  7505. if(pos==EQP_AMMO){
  7506. clif_arrowequip(sd,n);
  7507. clif_arrow_fail(sd,3);
  7508. }
  7509. else
  7510. clif_equipitemack(sd,n,pos,1);
  7511. sd->status.inventory[n].equip=pos;
  7512. if(pos & EQP_HAND_R) {
  7513. if(id)
  7514. sd->weapontype1 = id->look;
  7515. else
  7516. sd->weapontype1 = 0;
  7517. pc_calcweapontype(sd);
  7518. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7519. }
  7520. if(pos & EQP_HAND_L) {
  7521. if(id) {
  7522. if(id->type == IT_WEAPON) {
  7523. sd->status.shield = 0;
  7524. sd->weapontype2 = id->look;
  7525. }
  7526. else
  7527. if(id->type == IT_ARMOR) {
  7528. sd->status.shield = id->look;
  7529. sd->weapontype2 = 0;
  7530. }
  7531. }
  7532. else
  7533. sd->status.shield = sd->weapontype2 = 0;
  7534. pc_calcweapontype(sd);
  7535. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7536. }
  7537. //Added check to prevent sending the same look on multiple slots ->
  7538. //causes client to redraw item on top of itself. (suggested by Lupus)
  7539. if(pos & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
  7540. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
  7541. sd->status.head_bottom = id->look;
  7542. else
  7543. sd->status.head_bottom = 0;
  7544. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7545. }
  7546. if(pos & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
  7547. if(id)
  7548. sd->status.head_top = id->look;
  7549. else
  7550. sd->status.head_top = 0;
  7551. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7552. }
  7553. if(pos & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
  7554. if(id && !(pos&EQP_HEAD_TOP))
  7555. sd->status.head_mid = id->look;
  7556. else
  7557. sd->status.head_mid = 0;
  7558. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7559. }
  7560. if(pos & EQP_COSTUME_HEAD_TOP) {
  7561. if(id){
  7562. sd->status.head_top = id->look;
  7563. } else
  7564. sd->status.head_top = 0;
  7565. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7566. }
  7567. if(pos & EQP_COSTUME_HEAD_MID) {
  7568. if(id && !(pos&EQP_HEAD_TOP)){
  7569. sd->status.head_mid = id->look;
  7570. } else
  7571. sd->status.head_mid = 0;
  7572. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7573. }
  7574. if(pos & EQP_COSTUME_HEAD_LOW) {
  7575. if(id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
  7576. sd->status.head_bottom = id->look;
  7577. } else
  7578. sd->status.head_bottom = 0;
  7579. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7580. }
  7581. if(pos & EQP_SHOES)
  7582. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7583. if(pos&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7584. sd->status.robe = id ? id->look : 0;
  7585. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  7586. }
  7587. if(pos & EQP_COSTUME_GARMENT) {
  7588. sd->status.robe = id ? id->look : 0;
  7589. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7590. }
  7591. pc_checkallowskill(sd); //Check if status changes should be halted.
  7592. iflag = sd->npc_item_flag;
  7593. /* check for combos (MUST be before status_calc_pc) */
  7594. if ( id ) {
  7595. if( id->combos_count )
  7596. pc_checkcombo(sd,id);
  7597. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7598. ; //No cards
  7599. else {
  7600. for( i = 0; i < id->slot; i++ ) {
  7601. struct item_data *data;
  7602. if (!sd->status.inventory[n].card[i])
  7603. continue;
  7604. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7605. if( data->combos_count )
  7606. pc_checkcombo(sd,data);
  7607. }
  7608. }
  7609. }
  7610. }
  7611. status_calc_pc(sd,0);
  7612. if (flag) //Update skill data
  7613. clif_skillinfoblock(sd);
  7614. //OnEquip script [Skotlex]
  7615. if (id) {
  7616. if (id->equip_script)
  7617. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7618. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7619. ; //No cards
  7620. else {
  7621. for( i = 0; i < id->slot; i++ ) {
  7622. struct item_data *data;
  7623. if (!sd->status.inventory[n].card[i])
  7624. continue;
  7625. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7626. if( data->equip_script )
  7627. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  7628. }
  7629. }
  7630. }
  7631. }
  7632. sd->npc_item_flag = iflag;
  7633. return 0;
  7634. }
  7635. /*==========================================
  7636. * Called when attemting to unequip an item from player
  7637. * type:
  7638. * 0 - only unequip
  7639. * 1 - calculate status after unequipping
  7640. * 2 - force unequip
  7641. *------------------------------------------*/
  7642. int pc_unequipitem(struct map_session_data *sd,int n,int flag) {
  7643. int i,iflag;
  7644. bool status_cacl = false;
  7645. nullpo_ret(sd);
  7646. if( n < 0 || n >= MAX_INVENTORY ) {
  7647. clif_unequipitemack(sd,0,0,0);
  7648. return 0;
  7649. }
  7650. // if player is berserk then cannot unequip
  7651. if (!(flag & 2) && sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] || sd->sc.data[SC__BLOODYLUST]))
  7652. {
  7653. clif_unequipitemack(sd,n,0,0);
  7654. return 0;
  7655. }
  7656. if( !(flag&2) && sd->sc.count && sd->sc.data[SC_KYOUGAKU] )
  7657. {
  7658. clif_unequipitemack(sd,n,0,0);
  7659. return 0;
  7660. }
  7661. if(battle_config.battle_log)
  7662. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),sd->status.inventory[n].equip);
  7663. if(!sd->status.inventory[n].equip){ //Nothing to unequip
  7664. clif_unequipitemack(sd,n,0,0);
  7665. return 0;
  7666. }
  7667. for(i=0;i<EQI_MAX;i++) {
  7668. if(sd->status.inventory[n].equip & equip_pos[i])
  7669. sd->equip_index[i] = -1;
  7670. }
  7671. if(sd->status.inventory[n].equip & EQP_HAND_R) {
  7672. sd->weapontype1 = 0;
  7673. sd->status.weapon = sd->weapontype2;
  7674. pc_calcweapontype(sd);
  7675. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7676. if( !battle_config.dancing_weaponswitch_fix )
  7677. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  7678. }
  7679. if(sd->status.inventory[n].equip & EQP_HAND_L) {
  7680. sd->status.shield = sd->weapontype2 = 0;
  7681. pc_calcweapontype(sd);
  7682. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  7683. }
  7684. if(sd->status.inventory[n].equip & EQP_HEAD_LOW && pc_checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1 ) {
  7685. sd->status.head_bottom = 0;
  7686. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7687. }
  7688. if(sd->status.inventory[n].equip & EQP_HEAD_TOP && pc_checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1 ) {
  7689. sd->status.head_top = 0;
  7690. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7691. }
  7692. if(sd->status.inventory[n].equip & EQP_HEAD_MID && pc_checkequip(sd,EQP_COSTUME_HEAD_MID) == -1 ) {
  7693. sd->status.head_mid = 0;
  7694. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7695. }
  7696. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_TOP) {
  7697. sd->status.head_top = ( pc_checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_TOP)]->look : 0;
  7698. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7699. }
  7700. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_MID) {
  7701. sd->status.head_mid = ( pc_checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_MID)]->look : 0;
  7702. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7703. }
  7704. if(sd->status.inventory[n].equip & EQP_COSTUME_HEAD_LOW) {
  7705. sd->status.head_bottom = ( pc_checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_HEAD_LOW)]->look : 0;
  7706. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7707. }
  7708. if(sd->status.inventory[n].equip & EQP_SHOES)
  7709. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7710. if(sd->status.inventory[n].equip&EQP_GARMENT && pc_checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
  7711. sd->status.robe = 0;
  7712. clif_changelook(&sd->bl, LOOK_ROBE, 0);
  7713. }
  7714. if(sd->status.inventory[n].equip & EQP_COSTUME_GARMENT) {
  7715. sd->status.robe = ( pc_checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc_checkequip(sd,EQP_GARMENT)]->look : 0;
  7716. clif_changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
  7717. }
  7718. clif_unequipitemack(sd,n,sd->status.inventory[n].equip,1);
  7719. if((sd->status.inventory[n].equip & EQP_ARMS) &&
  7720. sd->weapontype1 == 0 && sd->weapontype2 == 0 && (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
  7721. skill_enchant_elemental_end(&sd->bl,-1);
  7722. if(sd->status.inventory[n].equip & EQP_ARMOR) {
  7723. // On Armor Change...
  7724. status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
  7725. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  7726. }
  7727. if( sd->state.autobonus&sd->status.inventory[n].equip )
  7728. sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]
  7729. sd->status.inventory[n].equip=0;
  7730. iflag = sd->npc_item_flag;
  7731. /* check for combos (MUST be before status_calc_pc) */
  7732. if ( sd->inventory_data[n] ) {
  7733. if( sd->inventory_data[n]->combos_count ) {
  7734. if( pc_removecombo(sd,sd->inventory_data[n]) )
  7735. status_cacl = true;
  7736. } if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7737. ; //No cards
  7738. else {
  7739. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7740. struct item_data *data;
  7741. if (!sd->status.inventory[n].card[i])
  7742. continue;
  7743. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7744. if( data->combos_count ) {
  7745. if( pc_removecombo(sd,data) )
  7746. status_cacl = true;
  7747. }
  7748. }
  7749. }
  7750. }
  7751. }
  7752. if(flag&1 || status_cacl) {
  7753. pc_checkallowskill(sd);
  7754. status_calc_pc(sd,0);
  7755. }
  7756. if(sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race,sd->battle_status.def_ele))
  7757. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  7758. //OnUnEquip script [Skotlex]
  7759. if (sd->inventory_data[n]) {
  7760. if (sd->inventory_data[n]->unequip_script)
  7761. run_script(sd->inventory_data[n]->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7762. if(itemdb_isspecial(sd->status.inventory[n].card[0]))
  7763. ; //No cards
  7764. else {
  7765. for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
  7766. struct item_data *data;
  7767. if (!sd->status.inventory[n].card[i])
  7768. continue;
  7769. if ( ( data = itemdb_exists(sd->status.inventory[n].card[i]) ) != NULL ) {
  7770. if( data->unequip_script )
  7771. run_script(data->unequip_script,0,sd->bl.id,fake_nd->bl.id);
  7772. }
  7773. }
  7774. }
  7775. }
  7776. sd->npc_item_flag = iflag;
  7777. return 0;
  7778. }
  7779. /*==========================================
  7780. * Checking if player (sd) have unauthorize, invalide item
  7781. * on inventory, cart, equiped for the map (item_noequip)
  7782. *------------------------------------------*/
  7783. int pc_checkitem(struct map_session_data *sd)
  7784. {
  7785. int i,id,calc_flag = 0;
  7786. nullpo_ret(sd);
  7787. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  7788. return 0;
  7789. if( battle_config.item_check ) {// check for invalid(ated) items
  7790. for( i = 0; i < MAX_INVENTORY; i++ ) {
  7791. id = sd->status.inventory[i].nameid;
  7792. if( id && !itemdb_available(id) ) {
  7793. ShowWarning("Removed invalid/disabled item id %d from inventory (amount=%d, char_id=%d).\n", id, sd->status.inventory[i].amount, sd->status.char_id);
  7794. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  7795. }
  7796. }
  7797. for( i = 0; i < MAX_CART; i++ ) {
  7798. id = sd->status.cart[i].nameid;
  7799. if( id && !itemdb_available(id) ) {
  7800. ShowWarning("Removed invalid/disabled item id %d from cart (amount=%d, char_id=%d).\n", id, sd->status.cart[i].amount, sd->status.char_id);
  7801. pc_cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_OTHER);
  7802. }
  7803. }
  7804. }
  7805. for( i = 0; i < MAX_INVENTORY; i++) {
  7806. if( sd->status.inventory[i].nameid == 0 )
  7807. continue;
  7808. if( !sd->status.inventory[i].equip )
  7809. continue;
  7810. if( sd->status.inventory[i].equip&~pc_equippoint(sd,i) ) {
  7811. pc_unequipitem(sd, i, 2);
  7812. calc_flag = 1;
  7813. continue;
  7814. }
  7815. }
  7816. if( calc_flag && sd->state.active ) {
  7817. pc_checkallowskill(sd);
  7818. status_calc_pc(sd,0);
  7819. }
  7820. return 0;
  7821. }
  7822. /*==========================================
  7823. * Update PVP rank for sd1 in cmp to sd2
  7824. *------------------------------------------*/
  7825. int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  7826. {
  7827. struct map_session_data *sd1,*sd2;
  7828. sd1=(struct map_session_data *)bl;
  7829. sd2=va_arg(ap,struct map_session_data *);
  7830. if( sd1->sc.option&OPTION_INVISIBLE || sd2->sc.option&OPTION_INVISIBLE )
  7831. {// cannot register pvp rank for hidden GMs
  7832. return 0;
  7833. }
  7834. if( sd1->pvp_point > sd2->pvp_point )
  7835. sd2->pvp_rank++;
  7836. return 0;
  7837. }
  7838. /*==========================================
  7839. * Calculate new rank beetween all present players (map_foreachinarea)
  7840. * and display result
  7841. *------------------------------------------*/
  7842. int pc_calc_pvprank(struct map_session_data *sd)
  7843. {
  7844. int old;
  7845. struct map_data *m;
  7846. m=&map[sd->bl.m];
  7847. old=sd->pvp_rank;
  7848. sd->pvp_rank=1;
  7849. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  7850. if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
  7851. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
  7852. return sd->pvp_rank;
  7853. }
  7854. /*==========================================
  7855. * Calculate next sd ranking calculation from config
  7856. *------------------------------------------*/
  7857. int pc_calc_pvprank_timer(int tid, unsigned int tick, int id, intptr_t data)
  7858. {
  7859. struct map_session_data *sd;
  7860. sd=map_id2sd(id);
  7861. if(sd==NULL)
  7862. return 0;
  7863. sd->pvp_timer = INVALID_TIMER;
  7864. if( sd->sc.option&OPTION_INVISIBLE )
  7865. {// do not calculate the pvp rank for a hidden GM
  7866. return 0;
  7867. }
  7868. if( pc_calc_pvprank(sd) > 0 )
  7869. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  7870. return 0;
  7871. }
  7872. /*==========================================
  7873. * Checking if sd is married
  7874. * Return:
  7875. * partner_id = yes
  7876. * 0 = no
  7877. *------------------------------------------*/
  7878. int pc_ismarried(struct map_session_data *sd)
  7879. {
  7880. if(sd == NULL)
  7881. return -1;
  7882. if(sd->status.partner_id > 0)
  7883. return sd->status.partner_id;
  7884. else
  7885. return 0;
  7886. }
  7887. /*==========================================
  7888. * Marry player sd to player dstsd
  7889. * Return:
  7890. * -1 = fail
  7891. * 0 = success
  7892. *------------------------------------------*/
  7893. int pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  7894. {
  7895. if(sd == NULL || dstsd == NULL ||
  7896. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  7897. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  7898. return -1;
  7899. sd->status.partner_id = dstsd->status.char_id;
  7900. dstsd->status.partner_id = sd->status.char_id;
  7901. return 0;
  7902. }
  7903. /*==========================================
  7904. * Divorce sd from its partner
  7905. * Return:
  7906. * -1 = fail
  7907. * 0 = success
  7908. *------------------------------------------*/
  7909. int pc_divorce(struct map_session_data *sd)
  7910. {
  7911. struct map_session_data *p_sd;
  7912. int i;
  7913. if( sd == NULL || !pc_ismarried(sd) )
  7914. return -1;
  7915. if( !sd->status.partner_id )
  7916. return -1; // Char is not married
  7917. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  7918. { // Lets char server do the divorce
  7919. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  7920. return -1; // No char server connected
  7921. return 0;
  7922. }
  7923. // Both players online, lets do the divorce manually
  7924. sd->status.partner_id = 0;
  7925. p_sd->status.partner_id = 0;
  7926. for( i = 0; i < MAX_INVENTORY; i++ )
  7927. {
  7928. if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
  7929. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  7930. if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
  7931. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  7932. }
  7933. clif_divorced(sd, p_sd->status.name);
  7934. clif_divorced(p_sd, sd->status.name);
  7935. return 0;
  7936. }
  7937. /*==========================================
  7938. * Get sd partner charid. (Married partner)
  7939. *------------------------------------------*/
  7940. struct map_session_data *pc_get_partner(struct map_session_data *sd)
  7941. {
  7942. if (sd && pc_ismarried(sd))
  7943. // charid2sd returns NULL if not found
  7944. return map_charid2sd(sd->status.partner_id);
  7945. return NULL;
  7946. }
  7947. /*==========================================
  7948. * Get sd father charid. (Need to be baby)
  7949. *------------------------------------------*/
  7950. struct map_session_data *pc_get_father (struct map_session_data *sd)
  7951. {
  7952. if (sd && sd->class_&JOBL_BABY && sd->status.father > 0)
  7953. // charid2sd returns NULL if not found
  7954. return map_charid2sd(sd->status.father);
  7955. return NULL;
  7956. }
  7957. /*==========================================
  7958. * Get sd mother charid. (Need to be baby)
  7959. *------------------------------------------*/
  7960. struct map_session_data *pc_get_mother (struct map_session_data *sd)
  7961. {
  7962. if (sd && sd->class_&JOBL_BABY && sd->status.mother > 0)
  7963. // charid2sd returns NULL if not found
  7964. return map_charid2sd(sd->status.mother);
  7965. return NULL;
  7966. }
  7967. /*==========================================
  7968. * Get sd children charid. (Need to be married)
  7969. *------------------------------------------*/
  7970. struct map_session_data *pc_get_child (struct map_session_data *sd)
  7971. {
  7972. if (sd && pc_ismarried(sd) && sd->status.child > 0)
  7973. // charid2sd returns NULL if not found
  7974. return map_charid2sd(sd->status.child);
  7975. return NULL;
  7976. }
  7977. /*==========================================
  7978. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  7979. *------------------------------------------*/
  7980. void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
  7981. {
  7982. int hp = 0, sp = 0;
  7983. if( pc_isdead(sd) )
  7984. return;
  7985. if (sd->hp_loss.value) {
  7986. sd->hp_loss.tick += diff_tick;
  7987. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  7988. hp += sd->hp_loss.value;
  7989. sd->hp_loss.tick -= sd->hp_loss.rate;
  7990. }
  7991. if(hp >= sd->battle_status.hp)
  7992. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  7993. }
  7994. if (sd->sp_loss.value) {
  7995. sd->sp_loss.tick += diff_tick;
  7996. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  7997. sp += sd->sp_loss.value;
  7998. sd->sp_loss.tick -= sd->sp_loss.rate;
  7999. }
  8000. }
  8001. if (hp > 0 || sp > 0)
  8002. status_zap(&sd->bl, hp, sp);
  8003. return;
  8004. }
  8005. //Character regen. Flag is used to know which types of regen can take place.
  8006. //&1: HP regen
  8007. //&2: SP regen
  8008. void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
  8009. {
  8010. int hp = 0, sp = 0;
  8011. if (sd->hp_regen.value) {
  8012. sd->hp_regen.tick += diff_tick;
  8013. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  8014. hp += sd->hp_regen.value;
  8015. sd->hp_regen.tick -= sd->hp_regen.rate;
  8016. }
  8017. }
  8018. if (sd->sp_regen.value) {
  8019. sd->sp_regen.tick += diff_tick;
  8020. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  8021. sp += sd->sp_regen.value;
  8022. sd->sp_regen.tick -= sd->sp_regen.rate;
  8023. }
  8024. }
  8025. if (hp > 0 || sp > 0)
  8026. status_heal(&sd->bl, hp, sp, 0);
  8027. return;
  8028. }
  8029. /*==========================================
  8030. * Memo player sd savepoint. (map,x,y)
  8031. *------------------------------------------*/
  8032. int pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  8033. {
  8034. nullpo_ret(sd);
  8035. sd->status.save_point.map = mapindex;
  8036. sd->status.save_point.x = x;
  8037. sd->status.save_point.y = y;
  8038. return 0;
  8039. }
  8040. /*==========================================
  8041. * Save 1 player data at autosave intervalle
  8042. *------------------------------------------*/
  8043. int pc_autosave(int tid, unsigned int tick, int id, intptr_t data)
  8044. {
  8045. int interval;
  8046. struct s_mapiterator* iter;
  8047. struct map_session_data* sd;
  8048. static int last_save_id = 0, save_flag = 0;
  8049. if(save_flag == 2) //Someone was saved on last call, normal cycle
  8050. save_flag = 0;
  8051. else
  8052. save_flag = 1; //Noone was saved, so save first found char.
  8053. iter = mapit_getallusers();
  8054. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8055. {
  8056. if(sd->bl.id == last_save_id && save_flag != 1) {
  8057. save_flag = 1;
  8058. continue;
  8059. }
  8060. if(save_flag != 1) //Not our turn to save yet.
  8061. continue;
  8062. //Save char.
  8063. last_save_id = sd->bl.id;
  8064. save_flag = 2;
  8065. chrif_save(sd,0);
  8066. break;
  8067. }
  8068. mapit_free(iter);
  8069. interval = autosave_interval/(map_usercount()+1);
  8070. if(interval < minsave_interval)
  8071. interval = minsave_interval;
  8072. add_timer(gettick()+interval,pc_autosave,0,0);
  8073. return 0;
  8074. }
  8075. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  8076. {
  8077. if (sd->state.night != night_flag && map[sd->bl.m].flag.nightenabled)
  8078. { //Night/day state does not match.
  8079. clif_status_load(&sd->bl, SI_NIGHT, night_flag); //New night effect by dynamix [Skotlex]
  8080. sd->state.night = night_flag;
  8081. return 1;
  8082. }
  8083. return 0;
  8084. }
  8085. /*================================================
  8086. * timer to do the day [Yor]
  8087. * data: 0 = called by timer, 1 = gmcommand/script
  8088. *------------------------------------------------*/
  8089. int map_day_timer(int tid, unsigned int tick, int id, intptr_t data)
  8090. {
  8091. char tmp_soutput[1024];
  8092. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  8093. return 0;
  8094. if (!night_flag)
  8095. return 0; //Already day.
  8096. night_flag = 0; // 0=day, 1=night [Yor]
  8097. map_foreachpc(pc_daynight_timer_sub);
  8098. strcpy(tmp_soutput, (data == 0) ? msg_txt(502) : msg_txt(60)); // The day has arrived!
  8099. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8100. return 0;
  8101. }
  8102. /*================================================
  8103. * timer to do the night [Yor]
  8104. * data: 0 = called by timer, 1 = gmcommand/script
  8105. *------------------------------------------------*/
  8106. int map_night_timer(int tid, unsigned int tick, int id, intptr_t data)
  8107. {
  8108. char tmp_soutput[1024];
  8109. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  8110. return 0;
  8111. if (night_flag)
  8112. return 0; //Already nigth.
  8113. night_flag = 1; // 0=day, 1=night [Yor]
  8114. map_foreachpc(pc_daynight_timer_sub);
  8115. strcpy(tmp_soutput, (data == 0) ? msg_txt(503) : msg_txt(59)); // The night has fallen...
  8116. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, 0);
  8117. return 0;
  8118. }
  8119. void pc_setstand(struct map_session_data *sd){
  8120. nullpo_retv(sd);
  8121. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  8122. clif_status_load(&sd->bl,SI_SIT,0);
  8123. //Reset sitting tick.
  8124. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  8125. sd->state.dead_sit = sd->vd.dead_sit = 0;
  8126. }
  8127. /**
  8128. * Mechanic (MADO GEAR)
  8129. **/
  8130. void pc_overheat(struct map_session_data *sd, int val) {
  8131. int heat = val, skill,
  8132. limit[] = { 10, 20, 28, 46, 66 };
  8133. if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
  8134. return; // already burning
  8135. skill = cap_value(pc_checkskill(sd,NC_MAINFRAME),0,4);
  8136. if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
  8137. heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
  8138. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8139. }
  8140. heat = max(0,heat); // Avoid negative HEAT
  8141. if( heat >= limit[skill] )
  8142. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT,100,0,1000);
  8143. else
  8144. sc_start(&sd->bl,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);
  8145. return;
  8146. }
  8147. /**
  8148. * Check if player is autolooting given itemID.
  8149. */
  8150. bool pc_isautolooting(struct map_session_data *sd, int nameid)
  8151. {
  8152. int i;
  8153. if( !sd->state.autolooting )
  8154. return false;
  8155. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  8156. return (i != AUTOLOOTITEM_SIZE);
  8157. }
  8158. /**
  8159. * Checks if player can use @/#command
  8160. * @param sd Player map session data
  8161. * @param command Command name without @/# and params
  8162. * @param type is it atcommand or charcommand
  8163. */
  8164. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  8165. {
  8166. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  8167. }
  8168. /**
  8169. * Checks if commands used by a player should be logged
  8170. * according to their group setting.
  8171. * @param sd Player map session data
  8172. */
  8173. bool pc_should_log_commands(struct map_session_data *sd)
  8174. {
  8175. return pc_group_should_log_commands(pc_get_group_id(sd));
  8176. }
  8177. static int pc_talisman_timer(int tid, unsigned int tick, int id, intptr_t data)
  8178. {
  8179. struct map_session_data *sd;
  8180. int i, type;
  8181. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  8182. return 1;
  8183. ARR_FIND(1, 5, type, sd->talisman[type] > 0);
  8184. if( sd->talisman[type] <= 0 )
  8185. {
  8186. ShowError("pc_talisman_timer: %d talisman's available. (aid=%d cid=%d tid=%d)\n", sd->talisman[type], sd->status.account_id, sd->status.char_id, tid);
  8187. sd->talisman[type] = 0;
  8188. return 0;
  8189. }
  8190. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == tid);
  8191. if( i == sd->talisman[type] )
  8192. {
  8193. ShowError("pc_talisman_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  8194. return 0;
  8195. }
  8196. sd->talisman[type]--;
  8197. if( i != sd->talisman[type] )
  8198. memmove(sd->talisman_timer[type]+i, sd->talisman_timer[type]+i+1, (sd->talisman[type]-i)*sizeof(int));
  8199. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8200. clif_talisman(sd, type);
  8201. return 0;
  8202. }
  8203. int pc_add_talisman(struct map_session_data *sd,int interval,int max,int type)
  8204. {
  8205. int tid, i;
  8206. nullpo_ret(sd);
  8207. if(max > 10)
  8208. max = 10;
  8209. if(sd->talisman[type] < 0)
  8210. sd->talisman[type] = 0;
  8211. if( sd->talisman[type] && sd->talisman[type] >= max )
  8212. {
  8213. if(sd->talisman_timer[type][0] != INVALID_TIMER)
  8214. delete_timer(sd->talisman_timer[type][0],pc_talisman_timer);
  8215. sd->talisman[type]--;
  8216. if( sd->talisman[type] != 0 )
  8217. memmove(sd->talisman_timer[type]+0, sd->talisman_timer[type]+1, (sd->talisman[type])*sizeof(int));
  8218. sd->talisman_timer[type][sd->talisman[type]] = INVALID_TIMER;
  8219. }
  8220. tid = add_timer(gettick()+interval, pc_talisman_timer, sd->bl.id, 0);
  8221. ARR_FIND(0, sd->talisman[type], i, sd->talisman_timer[type][i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->talisman_timer[type][i])->tick) < 0);
  8222. if( i != sd->talisman[type] )
  8223. memmove(sd->talisman_timer[type]+i+1, sd->talisman_timer[type]+i, (sd->talisman[type]-i)*sizeof(int));
  8224. sd->talisman_timer[type][i] = tid;
  8225. sd->talisman[type]++;
  8226. clif_talisman(sd, type);
  8227. return 0;
  8228. }
  8229. int pc_del_talisman(struct map_session_data *sd,int count,int type)
  8230. {
  8231. int i;
  8232. nullpo_ret(sd);
  8233. if( sd->talisman[type] <= 0 ) {
  8234. sd->talisman[type] = 0;
  8235. return 0;
  8236. }
  8237. if( count <= 0 )
  8238. return 0;
  8239. if( count > sd->talisman[type] )
  8240. count = sd->talisman[type];
  8241. sd->talisman[type] -= count;
  8242. if( count > 10 )
  8243. count = 10;
  8244. for(i = 0; i < count; i++) {
  8245. if(sd->talisman_timer[type][i] != INVALID_TIMER) {
  8246. delete_timer(sd->talisman_timer[type][i],pc_talisman_timer);
  8247. sd->talisman_timer[type][i] = INVALID_TIMER;
  8248. }
  8249. }
  8250. for(i = count; i < 10; i++) {
  8251. sd->talisman_timer[type][i-count] = sd->talisman_timer[type][i];
  8252. sd->talisman_timer[type][i] = INVALID_TIMER;
  8253. }
  8254. clif_talisman(sd, type);
  8255. return 0;
  8256. }
  8257. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8258. /*==========================================
  8259. * Renewal EXP/Itemdrop rate modifier base on level penalty
  8260. * 1=exp 2=itemdrop
  8261. *------------------------------------------*/
  8262. int pc_level_penalty_mod(struct map_session_data *sd, int mob_level, uint32 mob_race, uint32 mob_mode, int type)
  8263. {
  8264. int diff, rate = 100, i;
  8265. nullpo_ret(sd);
  8266. diff = mob_level - sd->status.base_level;
  8267. if( diff < 0 )
  8268. diff = MAX_LEVEL + ( ~diff + 1 );
  8269. for(i=0; i<RC_MAX; i++){
  8270. int tmp;
  8271. if( mob_race != i ){
  8272. if( mob_mode&MD_BOSS && i < RC_BOSS )
  8273. i = RC_BOSS;
  8274. else if( i <= RC_BOSS )
  8275. continue;
  8276. }
  8277. if( (tmp=level_penalty[type][i][diff]) > 0 ){
  8278. rate = tmp;
  8279. break;
  8280. }
  8281. }
  8282. return rate;
  8283. }
  8284. #endif
  8285. int pc_split_str(char *str,char **val,int num)
  8286. {
  8287. int i;
  8288. for (i=0; i<num && str; i++){
  8289. val[i] = str;
  8290. str = strchr(str,',');
  8291. if (str && i<num-1) //Do not remove a trailing comma.
  8292. *str++=0;
  8293. }
  8294. return i;
  8295. }
  8296. int pc_split_atoi(char* str, int* val, char sep, int max)
  8297. {
  8298. int i,j;
  8299. for (i=0; i<max; i++) {
  8300. if (!str) break;
  8301. val[i] = atoi(str);
  8302. str = strchr(str,sep);
  8303. if (str)
  8304. *str++=0;
  8305. }
  8306. //Zero up the remaining.
  8307. for(j=i; j < max; j++)
  8308. val[j] = 0;
  8309. return i;
  8310. }
  8311. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  8312. {
  8313. static int warning=0;
  8314. int i,j;
  8315. double f;
  8316. for (i=0; i<max; i++) {
  8317. if (!str) break;
  8318. f = atof(str);
  8319. if (f < 0)
  8320. val[i] = 0;
  8321. else if (f > UINT_MAX) {
  8322. val[i] = UINT_MAX;
  8323. if (!warning) {
  8324. warning = 1;
  8325. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  8326. }
  8327. } else
  8328. val[i] = (unsigned int)f;
  8329. str = strchr(str,sep);
  8330. if (str)
  8331. *str++=0;
  8332. }
  8333. //Zero up the remaining.
  8334. for(j=i; j < max; j++)
  8335. val[j] = 0;
  8336. return i;
  8337. }
  8338. /*==========================================
  8339. * sub DB reading.
  8340. * Function used to read skill_tree.txt
  8341. *------------------------------------------*/
  8342. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  8343. {
  8344. unsigned char joblv = 0, skill_lv;
  8345. uint16 skill_id;
  8346. int idx, class_;
  8347. unsigned int i, offset = 3, skill_idx;
  8348. class_ = atoi(fields[0]);
  8349. skill_id = (uint16)atoi(fields[1]);
  8350. skill_lv = (unsigned char)atoi(fields[2]);
  8351. if(columns==4+MAX_PC_SKILL_REQUIRE*2)
  8352. {// job level requirement extra column
  8353. joblv = (unsigned char)atoi(fields[3]);
  8354. offset++;
  8355. }
  8356. if(!pcdb_checkid(class_))
  8357. {
  8358. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  8359. return false;
  8360. }
  8361. idx = pc_class2idx(class_);
  8362. //This is to avoid adding two lines for the same skill. [Skotlex]
  8363. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].id == 0 || skill_tree[idx][skill_idx].id == skill_id );
  8364. if( skill_idx == MAX_SKILL_TREE )
  8365. {
  8366. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per class has been reached.\n", skill_id, class_);
  8367. return false;
  8368. }
  8369. else if(skill_tree[idx][skill_idx].id)
  8370. {
  8371. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job class %d.\n", skill_id, class_);
  8372. }
  8373. skill_tree[idx][skill_idx].id = skill_id;
  8374. skill_tree[idx][skill_idx].max = skill_lv;
  8375. skill_tree[idx][skill_idx].joblv = joblv;
  8376. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  8377. {
  8378. skill_tree[idx][skill_idx].need[i].id = atoi(fields[i*2+offset]);
  8379. skill_tree[idx][skill_idx].need[i].lv = atoi(fields[i*2+offset+1]);
  8380. }
  8381. return true;
  8382. }
  8383. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8384. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  8385. {
  8386. int type, race, diff;
  8387. type = atoi(fields[0]);
  8388. race = atoi(fields[1]);
  8389. diff = atoi(fields[2]);
  8390. if( type != 1 && type != 2 ){
  8391. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  8392. return false;
  8393. }
  8394. if( race < 0 || race > RC_MAX ){
  8395. ShowWarning("pc_readdb_levelpenalty: Invalid race %d specified.\n", race);
  8396. return false;
  8397. }
  8398. diff = min(diff, MAX_LEVEL);
  8399. if( diff < 0 )
  8400. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  8401. level_penalty[type][race][diff] = atoi(fields[3]);
  8402. return true;
  8403. }
  8404. #endif
  8405. /*==========================================
  8406. * pc DB reading.
  8407. * exp.txt - required experience values
  8408. * skill_tree.txt - skill tree for every class
  8409. * attr_fix.txt - elemental adjustment table
  8410. *------------------------------------------*/
  8411. int pc_readdb(void)
  8412. {
  8413. int i,j,k,tmp=0;
  8414. FILE *fp;
  8415. char line[24000],*p;
  8416. //reset
  8417. memset(exp_table,0,sizeof(exp_table));
  8418. memset(max_level,0,sizeof(max_level));
  8419. sprintf(line, "%s/"DBPATH"exp.txt", db_path);
  8420. fp=fopen(line, "r");
  8421. if(fp==NULL){
  8422. ShowError("can't read %s\n", line);
  8423. return 1;
  8424. }
  8425. while(fgets(line, sizeof(line), fp))
  8426. {
  8427. int jobs[CLASS_COUNT], job_count, job, job_id;
  8428. int type;
  8429. unsigned int ui,maxlv;
  8430. char *split[4];
  8431. if(line[0]=='/' && line[1]=='/')
  8432. continue;
  8433. if (pc_split_str(line,split,4) < 4)
  8434. continue;
  8435. job_count = pc_split_atoi(split[1],jobs,':',CLASS_COUNT);
  8436. if (job_count < 1)
  8437. continue;
  8438. job_id = jobs[0];
  8439. if (!pcdb_checkid(job_id)) {
  8440. ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
  8441. continue;
  8442. }
  8443. type = atoi(split[2]);
  8444. if (type < 0 || type > 1) {
  8445. ShowError("pc_readdb: Invalid type %d (must be 0 for base levels, 1 for job levels).\n", type);
  8446. continue;
  8447. }
  8448. maxlv = atoi(split[0]);
  8449. if (maxlv > MAX_LEVEL) {
  8450. ShowWarning("pc_readdb: Specified max level %u for job %d is beyond server's limit (%u).\n ", maxlv, job_id, MAX_LEVEL);
  8451. maxlv = MAX_LEVEL;
  8452. }
  8453. job = jobs[0] = pc_class2idx(job_id);
  8454. //We send one less and then one more because the last entry in the exp array should hold 0.
  8455. max_level[job][type] = pc_split_atoui(split[3], exp_table[job][type],',',maxlv-1)+1;
  8456. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  8457. //The reasoning behind the -2 is this... if the max level is 5, then the array
  8458. //should look like this:
  8459. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  8460. while ((ui = max_level[job][type]) >= 2 && exp_table[job][type][ui-2] <= 0)
  8461. max_level[job][type]--;
  8462. if (max_level[job][type] < maxlv) {
  8463. ShowWarning("pc_readdb: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlv, job_id, max_level[job][type]);
  8464. ShowInfo("Filling the missing values with the last exp entry.\n");
  8465. //Fill the requested values with the last entry.
  8466. ui = (max_level[job][type] <= 2? 0: max_level[job][type]-2);
  8467. for (; ui+2 < maxlv; ui++)
  8468. exp_table[job][type][ui] = exp_table[job][type][ui-1];
  8469. max_level[job][type] = maxlv;
  8470. }
  8471. // ShowDebug("%s - Class %d: %d\n", type?"Job":"Base", job_id, max_level[job][type]);
  8472. for (i = 1; i < job_count; i++) {
  8473. job_id = jobs[i];
  8474. if (!pcdb_checkid(job_id)) {
  8475. ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
  8476. continue;
  8477. }
  8478. job = pc_class2idx(job_id);
  8479. memcpy(exp_table[job][type], exp_table[jobs[0]][type], sizeof(exp_table[0][0]));
  8480. max_level[job][type] = maxlv;
  8481. // ShowDebug("%s - Class %d: %u\n", type?"Job":"Base", job_id, max_level[job][type]);
  8482. }
  8483. }
  8484. fclose(fp);
  8485. for (i = 0; i < JOB_MAX; i++) {
  8486. if (!pcdb_checkid(i)) continue;
  8487. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK)
  8488. continue; //Classes that do not need exp tables.
  8489. j = pc_class2idx(i);
  8490. if (!max_level[j][0])
  8491. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  8492. if (!max_level[j][1])
  8493. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  8494. }
  8495. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","exp.txt");
  8496. // Reset and read skilltree
  8497. memset(skill_tree,0,sizeof(skill_tree));
  8498. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3+MAX_PC_SKILL_REQUIRE*2, 4+MAX_PC_SKILL_REQUIRE*2, -1, &pc_readdb_skilltree);
  8499. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  8500. sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty);
  8501. for( k=1; k < 3; k++ ){ // fill in the blanks
  8502. for( j = 0; j < RC_MAX; j++ ){
  8503. tmp = 0;
  8504. for( i = 0; i < MAX_LEVEL*2; i++ ){
  8505. if( i == MAX_LEVEL+1 )
  8506. tmp = level_penalty[k][j][0];// reset
  8507. if( level_penalty[k][j][i] > 0 )
  8508. tmp = level_penalty[k][j][i];
  8509. else
  8510. level_penalty[k][j][i] = tmp;
  8511. }
  8512. }
  8513. }
  8514. #endif
  8515. // Reset then read attr_fix
  8516. for(i=0;i<4;i++)
  8517. for(j=0;j<ELE_MAX;j++)
  8518. for(k=0;k<ELE_MAX;k++)
  8519. attr_fix_table[i][j][k]=100;
  8520. sprintf(line, "%s/"DBPATH"attr_fix.txt", db_path);
  8521. fp=fopen(line,"r");
  8522. if(fp==NULL){
  8523. ShowError("can't read %s\n", line);
  8524. return 1;
  8525. }
  8526. while(fgets(line, sizeof(line), fp))
  8527. {
  8528. char *split[10];
  8529. int lv,n;
  8530. if(line[0]=='/' && line[1]=='/')
  8531. continue;
  8532. for(j=0,p=line;j<3 && p;j++){
  8533. split[j]=p;
  8534. p=strchr(p,',');
  8535. if(p) *p++=0;
  8536. }
  8537. if( j < 2 )
  8538. continue;
  8539. lv=atoi(split[0]);
  8540. n=atoi(split[1]);
  8541. for(i=0;i<n && i<ELE_MAX;){
  8542. if( !fgets(line, sizeof(line), fp) )
  8543. break;
  8544. if(line[0]=='/' && line[1]=='/')
  8545. continue;
  8546. for(j=0,p=line;j<n && j<ELE_MAX && p;j++){
  8547. while(*p==32 && *p>0)
  8548. p++;
  8549. attr_fix_table[lv-1][i][j]=atoi(p);
  8550. if(battle_config.attr_recover == 0 && attr_fix_table[lv-1][i][j] < 0)
  8551. attr_fix_table[lv-1][i][j] = 0;
  8552. p=strchr(p,',');
  8553. if(p) *p++=0;
  8554. }
  8555. i++;
  8556. }
  8557. }
  8558. fclose(fp);
  8559. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","attr_fix.txt");
  8560. // reset then read statspoint
  8561. memset(statp,0,sizeof(statp));
  8562. i=1;
  8563. sprintf(line, "%s/"DBPATH"statpoint.txt", db_path);
  8564. fp=fopen(line,"r");
  8565. if(fp == NULL){
  8566. ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
  8567. //return 1;
  8568. } else {
  8569. while(fgets(line, sizeof(line), fp))
  8570. {
  8571. int stat;
  8572. if(line[0]=='/' && line[1]=='/')
  8573. continue;
  8574. if ((stat=strtoul(line,NULL,10))<0)
  8575. stat=0;
  8576. if (i > MAX_LEVEL)
  8577. break;
  8578. statp[i]=stat;
  8579. i++;
  8580. }
  8581. fclose(fp);
  8582. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","statpoint.txt");
  8583. }
  8584. // generate the remaining parts of the db if necessary
  8585. k = battle_config.use_statpoint_table; //save setting
  8586. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  8587. statp[0] = 45; // seed value
  8588. for (; i <= MAX_LEVEL; i++)
  8589. statp[i] = statp[i-1] + pc_gets_status_point(i-1);
  8590. battle_config.use_statpoint_table = k; //restore setting
  8591. return 0;
  8592. }
  8593. // Read MOTD on startup. [Valaris]
  8594. int pc_read_motd(void)
  8595. {
  8596. FILE* fp;
  8597. // clear old MOTD
  8598. memset(motd_text, 0, sizeof(motd_text));
  8599. // read current MOTD
  8600. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  8601. {
  8602. char* buf, * ptr;
  8603. unsigned int lines = 0, entries = 0;
  8604. size_t len;
  8605. while( entries < MOTD_LINE_SIZE && fgets(motd_text[entries], sizeof(motd_text[entries]), fp) )
  8606. {
  8607. lines++;
  8608. buf = motd_text[entries];
  8609. if( buf[0] == '/' && buf[1] == '/' )
  8610. {
  8611. continue;
  8612. }
  8613. len = strlen(buf);
  8614. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) )
  8615. {// strip trailing EOL characters
  8616. len--;
  8617. }
  8618. if( len )
  8619. {
  8620. buf[len] = 0;
  8621. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH )
  8622. {// crashes newer clients
  8623. 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);
  8624. }
  8625. }
  8626. else
  8627. {// empty line
  8628. buf[0] = ' ';
  8629. buf[1] = 0;
  8630. }
  8631. entries++;
  8632. }
  8633. fclose(fp);
  8634. ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, motd_txt);
  8635. }
  8636. else
  8637. {
  8638. ShowWarning("File '"CL_WHITE"%s"CL_RESET"' not found.\n", motd_txt);
  8639. }
  8640. return 0;
  8641. }
  8642. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  8643. int i,cursor = 0;
  8644. struct item_cd* cd = NULL;
  8645. if( load ) {
  8646. if( !(cd = idb_get(itemcd_db, sd->status.char_id)) ) {
  8647. // no skill cooldown is associated with this character
  8648. return;
  8649. }
  8650. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8651. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  8652. sd->item_delay[cursor].tick = cd->tick[i];
  8653. sd->item_delay[cursor].nameid = cd->nameid[i];
  8654. cursor++;
  8655. }
  8656. }
  8657. idb_remove(itemcd_db,sd->status.char_id);
  8658. } else {
  8659. if( !(cd = idb_get(itemcd_db,sd->status.char_id)) ) {
  8660. // create a new skill cooldown object for map storage
  8661. CREATE( cd, struct item_cd, 1 );
  8662. idb_put( itemcd_db, sd->status.char_id, cd );
  8663. }
  8664. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  8665. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  8666. cd->tick[cursor] = sd->item_delay[i].tick;
  8667. cd->nameid[cursor] = sd->item_delay[i].nameid;
  8668. cursor++;
  8669. }
  8670. }
  8671. }
  8672. return;
  8673. }
  8674. /*==========================================
  8675. * pc Init/Terminate
  8676. *------------------------------------------*/
  8677. void do_final_pc(void) {
  8678. db_destroy(itemcd_db);
  8679. do_final_pc_groups();
  8680. return;
  8681. }
  8682. int do_init_pc(void) {
  8683. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  8684. pc_readdb();
  8685. pc_read_motd(); // Read MOTD [Valaris]
  8686. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  8687. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  8688. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  8689. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  8690. add_timer_func_list(pc_autosave, "pc_autosave");
  8691. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  8692. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  8693. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  8694. add_timer_func_list(pc_talisman_timer, "pc_talisman_timer");
  8695. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  8696. // 0=day, 1=night [Yor]
  8697. night_flag = battle_config.night_at_start ? 1 : 0;
  8698. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  8699. int day_duration = battle_config.day_duration;
  8700. int night_duration = battle_config.night_duration;
  8701. // add night/day timer [Yor]
  8702. add_timer_func_list(map_day_timer, "map_day_timer");
  8703. add_timer_func_list(map_night_timer, "map_night_timer");
  8704. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  8705. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  8706. }
  8707. do_init_pc_groups();
  8708. return 0;
  8709. }