pc.cpp 406 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/database.hpp"
  11. #include "../common/ers.hpp" // ers_destroy
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "script.hpp" // struct script_reg, struct script_regstr
  50. #include "searchstore.hpp" // struct s_search_store_info
  51. #include "status.hpp" // OPTION_*, struct weapon_atk
  52. #include "storage.hpp"
  53. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  54. #include "vending.hpp" // struct s_vending
  55. using namespace rathena;
  56. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  57. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  58. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  59. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  60. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  61. static unsigned int statp[MAX_LEVEL+1];
  62. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  63. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  64. #endif
  65. // h-files are for declarations, not for implementations... [Shinomori]
  66. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  67. // timer for night.day implementation
  68. int day_timer_tid = INVALID_TIMER;
  69. int night_timer_tid = INVALID_TIMER;
  70. struct eri *pc_sc_display_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. struct s_attendance_reward{
  79. uint16 item_id;
  80. uint16 amount;
  81. };
  82. struct s_attendance_period{
  83. uint32 start;
  84. uint32 end;
  85. std::map<uint32,std::shared_ptr<struct s_attendance_reward>> rewards;
  86. };
  87. class AttendanceDatabase : public TypesafeYamlDatabase<uint32,s_attendance_period>{
  88. public:
  89. AttendanceDatabase() : TypesafeYamlDatabase( "ATTENDANCE_DB", 1 ){
  90. }
  91. const std::string getDefaultLocation();
  92. uint64 parseBodyNode( const YAML::Node& node );
  93. };
  94. const std::string AttendanceDatabase::getDefaultLocation(){
  95. return std::string(db_path) + "/attendance.yml";
  96. }
  97. /**
  98. * Reads and parses an entry from the attendance_db.
  99. * @param node: YAML node containing the entry.
  100. * @return count of successfully parsed rows
  101. */
  102. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  103. uint32 start;
  104. if( !this->asUInt32( node, "Start", start ) ){
  105. return 0;
  106. }
  107. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  108. bool exists = attendance_period != nullptr;
  109. if( !exists ){
  110. if( !this->nodeExists( node, "End" ) ){
  111. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  112. return 0;
  113. }
  114. if( !this->nodeExists( node, "Rewards" ) ){
  115. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  116. return 0;
  117. }
  118. attendance_period = std::make_shared<s_attendance_period>();
  119. attendance_period->start = start;
  120. }
  121. // If it does not exist yet, we need to check it for sure
  122. bool requiresCollisionDetection = !exists;
  123. if( this->nodeExists( node, "End" ) ){
  124. uint32 end;
  125. if( !this->asUInt32( node, "End", end ) ){
  126. return 0;
  127. }
  128. // If the period is outdated already, we do not even bother parsing
  129. if( end < date_get( DT_YYYYMMDD ) ){
  130. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  131. return 0;
  132. }
  133. if( !exists || attendance_period->end != end ){
  134. requiresCollisionDetection = true;
  135. attendance_period->end = end;
  136. }
  137. }
  138. // Collision detection
  139. if( requiresCollisionDetection ){
  140. bool collision = false;
  141. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  142. std::shared_ptr<s_attendance_period> period = pair.second;
  143. if( exists && period->start == attendance_period->start ){
  144. // Dont compare to yourself
  145. continue;
  146. }
  147. // Check if start is inside another period
  148. if( period->start <= attendance_period->start && start <= period->end ){
  149. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  150. collision = true;
  151. break;
  152. }
  153. // Check if end is inside another period
  154. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  155. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  156. collision = true;
  157. break;
  158. }
  159. }
  160. if( collision ){
  161. return 0;
  162. }
  163. }
  164. if( this->nodeExists( node, "Rewards" ) ){
  165. const YAML::Node& rewardsNode = node["Rewards"];
  166. for( const YAML::Node& rewardNode : rewardsNode ){
  167. uint32 day;
  168. if( !this->asUInt32( rewardNode, "Day", day ) ){
  169. continue;
  170. }
  171. day -= 1;
  172. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  173. bool reward_exists = reward != nullptr;
  174. if( !reward_exists ){
  175. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  176. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  177. return 0;
  178. }
  179. reward = std::make_shared<s_attendance_reward>();
  180. }
  181. if( this->nodeExists( rewardNode, "ItemId" ) ){
  182. uint16 item_id;
  183. if( !this->asUInt16( rewardNode, "ItemId", item_id ) ){
  184. continue;
  185. }
  186. if( item_id == 0 || !itemdb_exists( item_id ) ){
  187. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day + 1 );
  188. continue;
  189. }
  190. reward->item_id = item_id;
  191. }
  192. if( this->nodeExists( rewardNode, "Amount" ) ){
  193. uint16 amount;
  194. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  195. continue;
  196. }
  197. if( amount == 0 ){
  198. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  199. amount = 1;
  200. }else if( amount > MAX_AMOUNT ){
  201. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  202. amount = MAX_AMOUNT;
  203. }
  204. reward->amount = amount;
  205. }else{
  206. if( !reward_exists ){
  207. reward->amount = 1;
  208. }
  209. }
  210. if( !reward_exists ){
  211. attendance_period->rewards[day] = reward;
  212. }
  213. }
  214. bool missing_day = false;
  215. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  216. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  217. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  218. missing_day = true;
  219. break;
  220. }
  221. }
  222. if( missing_day ){
  223. return 0;
  224. }
  225. }
  226. if( !exists ){
  227. this->put( start, attendance_period );
  228. }
  229. return 1;
  230. }
  231. AttendanceDatabase attendance_db;
  232. #define MOTD_LINE_SIZE 128
  233. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  234. bool reg_load;
  235. /**
  236. * Translation table from athena equip index to aegis bitmask
  237. */
  238. unsigned int equip_bitmask[EQI_MAX] = {
  239. EQP_ACC_L, // EQI_ACC_L
  240. EQP_ACC_R, // EQI_ACC_R
  241. EQP_SHOES, // EQI_SHOES
  242. EQP_GARMENT, // EQI_GARMENT
  243. EQP_HEAD_LOW, // EQI_HEAD_LOW
  244. EQP_HEAD_MID, // EQI_HEAD_MID
  245. EQP_HEAD_TOP, // EQI_HEAD_TOP
  246. EQP_ARMOR, // EQI_ARMOR
  247. EQP_HAND_L, // EQI_HAND_L
  248. EQP_HAND_R, // EQI_HAND_R
  249. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  250. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  251. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  252. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  253. EQP_AMMO, // EQI_AMMO
  254. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  255. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  256. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  257. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  258. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  259. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  260. };
  261. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  262. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  263. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  264. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  265. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  266. };
  267. void pc_set_reg_load( bool val ){
  268. reg_load = val;
  269. }
  270. /**
  271. * Item Cool Down Delay Saving
  272. * Struct item_cd is not a member of struct map_session_data
  273. * to keep cooldowns in memory between player log-ins.
  274. * All cooldowns are reset when server is restarted.
  275. **/
  276. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  277. struct item_cd {
  278. t_tick tick[MAX_ITEMDELAYS]; //tick
  279. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  280. };
  281. /**
  282. * Converts a class to its array index for CLASS_COUNT defined arrays.
  283. * Note that it does not do a validity check for speed purposes, where parsing
  284. * player input make sure to use a pcdb_checkid first!
  285. * @param class_ Job ID see enum e_job
  286. * @return Class Index
  287. */
  288. int pc_class2idx(int class_) {
  289. if (class_ >= JOB_NOVICE_HIGH)
  290. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  291. return class_;
  292. }
  293. /**
  294. * Get player's group ID
  295. * @param sd
  296. * @return Group ID
  297. */
  298. int pc_get_group_id(struct map_session_data *sd) {
  299. return sd->group_id;
  300. }
  301. /** Get player's group Level
  302. * @param sd
  303. * @return Group Level
  304. */
  305. int pc_get_group_level(struct map_session_data *sd) {
  306. return sd->group_level;
  307. }
  308. static TIMER_FUNC(pc_invincible_timer){
  309. struct map_session_data *sd;
  310. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  311. return 1;
  312. if(sd->invincible_timer != tid){
  313. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  314. return 0;
  315. }
  316. sd->invincible_timer = INVALID_TIMER;
  317. skill_unit_move(&sd->bl,tick,1);
  318. return 0;
  319. }
  320. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  321. nullpo_retv(sd);
  322. if( sd->invincible_timer != INVALID_TIMER )
  323. delete_timer(sd->invincible_timer,pc_invincible_timer);
  324. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  325. }
  326. void pc_delinvincibletimer(struct map_session_data* sd)
  327. {
  328. nullpo_retv(sd);
  329. if( sd->invincible_timer != INVALID_TIMER )
  330. {
  331. delete_timer(sd->invincible_timer,pc_invincible_timer);
  332. sd->invincible_timer = INVALID_TIMER;
  333. skill_unit_move(&sd->bl,gettick(),1);
  334. }
  335. }
  336. static TIMER_FUNC(pc_spiritball_timer){
  337. struct map_session_data *sd;
  338. int i;
  339. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  340. return 1;
  341. if( sd->spiritball <= 0 )
  342. {
  343. 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);
  344. sd->spiritball = 0;
  345. return 0;
  346. }
  347. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  348. if( i == sd->spiritball )
  349. {
  350. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  351. return 0;
  352. }
  353. sd->spiritball--;
  354. if( i != sd->spiritball )
  355. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  356. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  357. clif_spiritball(&sd->bl);
  358. return 0;
  359. }
  360. /**
  361. * Adds a spiritball to player for 'interval' ms
  362. * @param sd
  363. * @param interval
  364. * @param max
  365. */
  366. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  367. {
  368. int tid;
  369. uint8 i;
  370. nullpo_retv(sd);
  371. if(max > MAX_SPIRITBALL)
  372. max = MAX_SPIRITBALL;
  373. if(sd->spiritball < 0)
  374. sd->spiritball = 0;
  375. if( sd->spiritball && sd->spiritball >= max )
  376. {
  377. if(sd->spirit_timer[0] != INVALID_TIMER)
  378. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  379. sd->spiritball--;
  380. if( sd->spiritball != 0 )
  381. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  382. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  383. }
  384. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  385. 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);
  386. if( i != sd->spiritball )
  387. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  388. sd->spirit_timer[i] = tid;
  389. sd->spiritball++;
  390. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  391. clif_millenniumshield(&sd->bl,sd->spiritball);
  392. else
  393. clif_spiritball(&sd->bl);
  394. }
  395. /**
  396. * Removes number of spiritball from player
  397. * @param sd
  398. * @param count
  399. * @param type 1 = doesn't give client effect
  400. */
  401. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  402. {
  403. uint8 i;
  404. nullpo_retv(sd);
  405. if(sd->spiritball <= 0) {
  406. sd->spiritball = 0;
  407. return;
  408. }
  409. if(count == 0)
  410. return;
  411. if(count > sd->spiritball)
  412. count = sd->spiritball;
  413. sd->spiritball -= count;
  414. if(count > MAX_SPIRITBALL)
  415. count = MAX_SPIRITBALL;
  416. for(i=0;i<count;i++) {
  417. if(sd->spirit_timer[i] != INVALID_TIMER) {
  418. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  419. sd->spirit_timer[i] = INVALID_TIMER;
  420. }
  421. }
  422. for(i=count;i<MAX_SPIRITBALL;i++) {
  423. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  424. sd->spirit_timer[i] = INVALID_TIMER;
  425. }
  426. if(!type) {
  427. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  428. clif_millenniumshield(&sd->bl,sd->spiritball);
  429. else
  430. clif_spiritball(&sd->bl);
  431. }
  432. }
  433. /**
  434. * Increases a player's fame points and displays a notice to him
  435. * @param sd Player
  436. * @param count Fame point
  437. */
  438. void pc_addfame(struct map_session_data *sd,int count)
  439. {
  440. enum e_rank ranktype;
  441. nullpo_retv(sd);
  442. sd->status.fame += count;
  443. if(sd->status.fame > MAX_FAME)
  444. sd->status.fame = MAX_FAME;
  445. switch(sd->class_&MAPID_UPPERMASK){
  446. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  447. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  448. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  449. default:
  450. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  451. return;
  452. }
  453. clif_update_rankingpoint(sd,ranktype,count);
  454. chrif_updatefamelist(sd);
  455. }
  456. /**
  457. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  458. * @param sd
  459. * @param job Job use enum e_mapid
  460. * @return Rank
  461. */
  462. unsigned char pc_famerank(uint32 char_id, int job)
  463. {
  464. uint8 i;
  465. switch(job){
  466. case MAPID_BLACKSMITH: // Blacksmith
  467. for(i = 0; i < MAX_FAME_LIST; i++){
  468. if(smith_fame_list[i].id == char_id)
  469. return i + 1;
  470. }
  471. break;
  472. case MAPID_ALCHEMIST: // Alchemist
  473. for(i = 0; i < MAX_FAME_LIST; i++){
  474. if(chemist_fame_list[i].id == char_id)
  475. return i + 1;
  476. }
  477. break;
  478. case MAPID_TAEKWON: // Taekwon
  479. for(i = 0; i < MAX_FAME_LIST; i++){
  480. if(taekwon_fame_list[i].id == char_id)
  481. return i + 1;
  482. }
  483. break;
  484. }
  485. return 0;
  486. }
  487. /**
  488. * Restart player's HP & SP value
  489. * @param sd
  490. * @param type Restart type: 1 - Normal Resurection
  491. */
  492. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  493. struct status_data *status, *b_status;
  494. nullpo_retv(sd);
  495. b_status = &sd->base_status;
  496. status = &sd->battle_status;
  497. if (type&1) { //Normal resurrection
  498. status->hp = 1; //Otherwise status_heal may fail if dead.
  499. status_heal(&sd->bl, b_status->hp, 0, 1);
  500. if( status->sp < b_status->sp )
  501. status_set_sp(&sd->bl, b_status->sp, 1);
  502. } else { //Just for saving on the char-server (with values as if respawned)
  503. sd->status.hp = b_status->hp;
  504. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  505. }
  506. }
  507. /*==========================================
  508. Rental System
  509. *------------------------------------------*/
  510. /**
  511. * Ends a rental and removes the item/effect
  512. * @param tid: Tick ID
  513. * @param tick: Timer
  514. * @param id: Timer ID
  515. * @param data: Data
  516. * @return false - failure, true - success
  517. */
  518. TIMER_FUNC(pc_inventory_rental_end){
  519. struct map_session_data *sd = map_id2sd(id);
  520. if( sd == NULL )
  521. return 0;
  522. if( tid != sd->rental_timer ) {
  523. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  524. return 0;
  525. }
  526. pc_inventory_rentals(sd);
  527. return 1;
  528. }
  529. /**
  530. * Removes the rental timer from the player
  531. * @param sd: Player data
  532. */
  533. void pc_inventory_rental_clear(struct map_session_data *sd)
  534. {
  535. if( sd->rental_timer != INVALID_TIMER ) {
  536. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  537. sd->rental_timer = INVALID_TIMER;
  538. }
  539. }
  540. /**
  541. * Check for items in the player's inventory that are rental type
  542. * @param sd: Player data
  543. */
  544. void pc_inventory_rentals(struct map_session_data *sd)
  545. {
  546. int i, c = 0;
  547. unsigned int next_tick = UINT_MAX;
  548. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  549. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  550. continue; // Nothing here
  551. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  552. continue;
  553. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  554. if (sd->inventory_data[i]->unequip_script)
  555. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  556. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  557. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  558. } else {
  559. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  560. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  561. next_tick = umin(expire_tick * 1000U, next_tick);
  562. c++;
  563. }
  564. }
  565. 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
  566. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  567. else
  568. sd->rental_timer = INVALID_TIMER;
  569. }
  570. /**
  571. * Add a rental item to the player and adjusts the rental timer appropriately
  572. * @param sd: Player data
  573. * @param seconds: Rental time
  574. */
  575. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  576. {
  577. t_tick tick = seconds * 1000;
  578. if( sd == NULL )
  579. return;
  580. if( sd->rental_timer != INVALID_TIMER ) {
  581. const struct TimerData * td;
  582. td = get_timer(sd->rental_timer);
  583. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  584. pc_inventory_rental_clear(sd);
  585. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  586. }
  587. } else
  588. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  589. }
  590. /**
  591. * Check if the player can sell the current item
  592. * @param sd: map_session_data of the player
  593. * @param item: struct of the checking item
  594. * @param shoptype: NPC's sub type see enum npc_subtype
  595. * @return bool 'true' is sellable, 'false' otherwise
  596. */
  597. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  598. if (sd == NULL || item == NULL)
  599. return false;
  600. if (item->equip > 0 || item->amount < 0)
  601. return false;
  602. if (battle_config.hide_fav_sell && item->favorite)
  603. return false; //Cannot sell favs (optional config)
  604. if (!battle_config.rental_transaction && item->expire_time)
  605. return false; // Cannot Sell Rental Items
  606. if( item->equipSwitch ){
  607. return false;
  608. }
  609. if (itemdb_ishatched_egg(item))
  610. return false;
  611. switch (shoptype) {
  612. case NPCTYPE_SHOP:
  613. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  614. item->bound != BOUND_GUILD ||
  615. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  616. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  617. ))
  618. return true;
  619. break;
  620. case NPCTYPE_ITEMSHOP:
  621. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  622. item->bound != BOUND_GUILD ||
  623. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  624. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  625. ))
  626. return true;
  627. else if (!item->bound) {
  628. struct item_data *itd = itemdb_search(item->nameid);
  629. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  630. return true;
  631. }
  632. break;
  633. }
  634. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  635. return false;
  636. if (item->bound && !pc_can_give_bounded_items(sd))
  637. return false; // Don't allow sale of bound items
  638. return true;
  639. }
  640. /**
  641. * Determines if player can give / drop / trade / vend items
  642. */
  643. bool pc_can_give_items(struct map_session_data *sd)
  644. {
  645. return pc_has_permission(sd, PC_PERM_TRADE);
  646. }
  647. /**
  648. * Determines if player can give / drop / trade / vend bounded items
  649. */
  650. bool pc_can_give_bounded_items(struct map_session_data *sd)
  651. {
  652. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  653. }
  654. /*==========================================
  655. * Prepares character for saving.
  656. * @param sd
  657. *------------------------------------------*/
  658. void pc_makesavestatus(struct map_session_data *sd) {
  659. nullpo_retv(sd);
  660. if(!battle_config.save_clothcolor)
  661. sd->status.clothes_color = 0;
  662. // Since this is currently not officially released,
  663. // its best to have a forced option to not save body styles.
  664. if(!battle_config.save_body_style)
  665. sd->status.body = 0;
  666. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  667. //status change load/saving. [Skotlex]
  668. #ifdef NEW_CARTS
  669. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  670. #else
  671. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  672. #endif
  673. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  674. if(pc_isdead(sd)){
  675. pc_setrestartvalue(sd, 0);
  676. } else {
  677. sd->status.hp = sd->battle_status.hp;
  678. sd->status.sp = sd->battle_status.sp;
  679. }
  680. sd->status.last_point.map = sd->mapindex;
  681. sd->status.last_point.x = sd->bl.x;
  682. sd->status.last_point.y = sd->bl.y;
  683. return;
  684. }
  685. if(pc_isdead(sd)) {
  686. pc_setrestartvalue(sd, 0);
  687. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  688. } else {
  689. sd->status.hp = sd->battle_status.hp;
  690. sd->status.sp = sd->battle_status.sp;
  691. sd->status.last_point.map = sd->mapindex;
  692. sd->status.last_point.x = sd->bl.x;
  693. sd->status.last_point.y = sd->bl.y;
  694. }
  695. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  696. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  697. if(mapdata->save.map)
  698. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  699. else
  700. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  701. }
  702. }
  703. /*==========================================
  704. * Off init ? Connection?
  705. *------------------------------------------*/
  706. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  707. nullpo_retv(sd);
  708. sd->bl.id = account_id;
  709. sd->status.account_id = account_id;
  710. sd->status.char_id = char_id;
  711. sd->status.sex = sex;
  712. sd->login_id1 = login_id1;
  713. sd->login_id2 = 0; // at this point, we can not know the value :(
  714. sd->client_tick = client_tick;
  715. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  716. sd->bl.type = BL_PC;
  717. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  718. sd->canlog_tick = gettick();
  719. //Required to prevent homunculus copuing a base speed of 0.
  720. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  721. }
  722. /**
  723. * Get equip point for an equip
  724. * @param sd
  725. * @param id
  726. */
  727. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  728. int ep = 0;
  729. nullpo_ret(sd);
  730. nullpo_ret(id);
  731. if (!itemdb_isequip2(id))
  732. return 0; //Not equippable by players.
  733. ep = id->equip;
  734. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  735. if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
  736. if (ep == EQP_WEAPON)
  737. return EQP_ARMS;
  738. if (ep == EQP_SHADOW_WEAPON)
  739. return EQP_SHADOW_ARMS;
  740. }
  741. }
  742. return ep;
  743. }
  744. /**
  745. * Get equip point for an equip
  746. * @param sd
  747. * @param n Equip index in inventory
  748. */
  749. int pc_equippoint(struct map_session_data *sd,int n){
  750. nullpo_ret(sd);
  751. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  752. }
  753. /**
  754. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  755. * @param sd : player session
  756. * @return 0 sucess, 1:invalid sd
  757. */
  758. void pc_setinventorydata(struct map_session_data *sd)
  759. {
  760. uint8 i;
  761. nullpo_retv(sd);
  762. for(i = 0; i < MAX_INVENTORY; i++) {
  763. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  764. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  765. }
  766. }
  767. /**
  768. * 'Calculates' weapon type
  769. * @param sd : Player
  770. */
  771. void pc_calcweapontype(struct map_session_data *sd)
  772. {
  773. nullpo_retv(sd);
  774. // single-hand
  775. if(sd->weapontype2 == W_FIST) {
  776. sd->status.weapon = sd->weapontype1;
  777. return;
  778. }
  779. if(sd->weapontype1 == W_FIST) {
  780. sd->status.weapon = sd->weapontype2;
  781. return;
  782. }
  783. // dual-wield
  784. sd->status.weapon = 0;
  785. switch (sd->weapontype1){
  786. case W_DAGGER:
  787. switch (sd->weapontype2) {
  788. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  789. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  790. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  791. }
  792. break;
  793. case W_1HSWORD:
  794. switch (sd->weapontype2) {
  795. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  796. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  797. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  798. }
  799. break;
  800. case W_1HAXE:
  801. switch (sd->weapontype2) {
  802. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  803. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  804. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  805. }
  806. }
  807. // unknown, default to right hand type
  808. if (!sd->status.weapon)
  809. sd->status.weapon = sd->weapontype1;
  810. }
  811. /**
  812. * Set equip index
  813. * @param sd : Player
  814. */
  815. void pc_setequipindex(struct map_session_data *sd)
  816. {
  817. uint16 i;
  818. nullpo_retv(sd);
  819. for (i = 0; i < EQI_MAX; i++){
  820. sd->equip_index[i] = -1;
  821. sd->equip_switch_index[i] = -1;
  822. }
  823. for (i = 0; i < MAX_INVENTORY; i++) {
  824. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  825. continue;
  826. if (sd->inventory.u.items_inventory[i].equip) {
  827. uint8 j;
  828. for (j = 0; j < EQI_MAX; j++)
  829. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  830. sd->equip_index[j] = i;
  831. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  832. if (sd->inventory_data[i])
  833. sd->weapontype1 = sd->inventory_data[i]->look;
  834. else
  835. sd->weapontype1 = 0;
  836. }
  837. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  838. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  839. sd->weapontype2 = sd->inventory_data[i]->look;
  840. else
  841. sd->weapontype2 = 0;
  842. }
  843. }
  844. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  845. for (uint8 j = 0; j < EQI_MAX; j++) {
  846. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  847. sd->equip_switch_index[j] = i;
  848. }
  849. }
  850. }
  851. }
  852. pc_calcweapontype(sd);
  853. }
  854. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  855. //{
  856. // int i;
  857. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  858. // struct item_data *data;
  859. //
  860. // //Crafted/made/hatched items.
  861. // if (itemdb_isspecial(item->card[0]))
  862. // return 1;
  863. //
  864. // /* scan for enchant armor gems */
  865. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  866. // s = MAX_SLOTS - 1;
  867. //
  868. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  869. // return( i < s ) ? 0 : 1;
  870. //}
  871. /**
  872. * Check if an item is equiped by player
  873. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  874. * @param sd : player session
  875. * @param nameid : itemid
  876. * @return 1:yes, 0:no
  877. */
  878. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  879. {
  880. uint8 i;
  881. for( i = 0; i < EQI_MAX; i++ )
  882. {
  883. short index = sd->equip_index[i], j;
  884. if( index < 0 )
  885. continue;
  886. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  887. continue;
  888. if( !sd->inventory_data[index] )
  889. continue;
  890. if( sd->inventory_data[index]->nameid == nameid )
  891. return true;
  892. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  893. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  894. return true;
  895. }
  896. }
  897. return false;
  898. }
  899. /**
  900. * Check adoption rules
  901. * @param p1_sd: Player 1
  902. * @param p2_sd: Player 2
  903. * @param b_sd: Player that will be adopted
  904. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  905. * ADOPT_ALREADY_ADOPTED - Already adopted
  906. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  907. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  908. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  909. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  910. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  911. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  912. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  913. */
  914. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  915. {
  916. if( !p1_sd || !p2_sd || !b_sd )
  917. return ADOPT_CHARACTER_NOT_FOUND;
  918. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  919. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  920. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  921. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  922. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  923. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  924. if( p2_sd->status.party_id != p1_sd->status.party_id )
  925. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  926. // Parents need to have their ring equipped
  927. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  928. return ADOPT_EQUIP_RINGS;
  929. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  930. return ADOPT_EQUIP_RINGS;
  931. // Already adopted a baby
  932. if( p1_sd->status.child || p2_sd->status.child ) {
  933. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  934. return ADOPT_MORE_CHILDREN;
  935. }
  936. // Parents need at least lvl 70 to adopt
  937. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  938. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  939. return ADOPT_LEVEL_70;
  940. }
  941. if( b_sd->status.partner_id ) {
  942. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  943. return ADOPT_MARRIED;
  944. }
  945. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
  946. return ADOPT_NOT_NOVICE;
  947. return ADOPT_ALLOWED;
  948. }
  949. /*==========================================
  950. * Adoption Process
  951. *------------------------------------------*/
  952. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  953. {
  954. int job, joblevel;
  955. unsigned int jobexp;
  956. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  957. return false;
  958. // Preserve current job levels and progress
  959. joblevel = b_sd->status.job_level;
  960. jobexp = b_sd->status.job_exp;
  961. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  962. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  963. { // Success, proceed to configure parents and baby skills
  964. p1_sd->status.child = b_sd->status.char_id;
  965. p2_sd->status.child = b_sd->status.char_id;
  966. b_sd->status.father = p1_sd->status.char_id;
  967. b_sd->status.mother = p2_sd->status.char_id;
  968. // Restore progress
  969. b_sd->status.job_level = joblevel;
  970. clif_updatestatus(b_sd, SP_JOBLEVEL);
  971. b_sd->status.job_exp = jobexp;
  972. clif_updatestatus(b_sd, SP_JOBEXP);
  973. // Baby Skills
  974. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  975. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  976. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  977. // Parents Skills
  978. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  979. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  980. chrif_save(p1_sd, CSAVE_NORMAL);
  981. chrif_save(p2_sd, CSAVE_NORMAL);
  982. chrif_save(b_sd, CSAVE_NORMAL);
  983. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  984. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  985. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  986. return true;
  987. }
  988. return false; // Job Change Fail
  989. }
  990. /*==========================================
  991. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  992. Returns:
  993. false : Cannot use/equip
  994. true : Can use/equip
  995. * Credits:
  996. [Inkfish] for first idea
  997. [Haru] for third-classes extension
  998. [Cydh] finishing :D
  999. *------------------------------------------*/
  1000. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1001. while (1) {
  1002. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1003. break;
  1004. #ifndef RENEWAL
  1005. //allow third classes to use trans. class items
  1006. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1007. break;
  1008. //third-baby classes can use same item too
  1009. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1010. break;
  1011. //don't need to decide specific rules for third-classes?
  1012. //items for third classes can be used for all third classes
  1013. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1014. break;
  1015. #else
  1016. //trans. classes (exl. third-trans.)
  1017. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1018. break;
  1019. //baby classes (exl. third-baby)
  1020. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1021. break;
  1022. //third classes (exl. third-trans. and baby-third)
  1023. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1024. break;
  1025. //trans-third classes
  1026. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1027. break;
  1028. //third-baby classes
  1029. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1030. break;
  1031. #endif
  1032. return false;
  1033. }
  1034. return true;
  1035. }
  1036. /*=================================================
  1037. * Checks if the player can equip the item at index n in inventory.
  1038. * @param sd
  1039. * @param n Item index in inventory
  1040. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1041. *------------------------------------------------*/
  1042. uint8 pc_isequip(struct map_session_data *sd,int n)
  1043. {
  1044. struct item_data *item;
  1045. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1046. item = sd->inventory_data[n];
  1047. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1048. return ITEM_EQUIP_ACK_OK;
  1049. if(item == NULL)
  1050. return ITEM_EQUIP_ACK_FAIL;
  1051. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1052. return ITEM_EQUIP_ACK_FAILLEVEL;
  1053. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1054. return ITEM_EQUIP_ACK_FAILLEVEL;
  1055. if(item->sex != 2 && sd->status.sex != item->sex)
  1056. return ITEM_EQUIP_ACK_FAIL;
  1057. //fail to equip if item is restricted
  1058. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1059. return ITEM_EQUIP_ACK_FAIL;
  1060. if (item->equip&EQP_AMMO) {
  1061. switch (item->look) {
  1062. case AMMO_ARROW:
  1063. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1064. clif_msg(sd, ITEM_NEED_BOW);
  1065. return ITEM_EQUIP_ACK_FAIL;
  1066. }
  1067. break;
  1068. case AMMO_THROWABLE_DAGGER:
  1069. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1070. return ITEM_EQUIP_ACK_FAIL;
  1071. break;
  1072. case AMMO_BULLET:
  1073. case AMMO_SHELL:
  1074. if (battle_config.ammo_check_weapon && sd->status.weapon != W_REVOLVER && sd->status.weapon != W_RIFLE && sd->status.weapon != W_GATLING && sd->status.weapon != W_SHOTGUN
  1075. #ifdef RENEWAL
  1076. && sd->status.weapon != W_GRENADE
  1077. #endif
  1078. ) {
  1079. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1080. return ITEM_EQUIP_ACK_FAIL;
  1081. }
  1082. break;
  1083. #ifndef RENEWAL
  1084. case AMMO_GRENADE:
  1085. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1086. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1087. return ITEM_EQUIP_ACK_FAIL;
  1088. }
  1089. break;
  1090. #endif
  1091. case AMMO_CANNONBALL:
  1092. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1093. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1094. return ITEM_EQUIP_ACK_FAIL;
  1095. }
  1096. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1097. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1098. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1099. return ITEM_EQUIP_ACK_FAIL;
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. if (sd->sc.count) {
  1105. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1106. return ITEM_EQUIP_ACK_FAIL;
  1107. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1108. return ITEM_EQUIP_ACK_FAIL;
  1109. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1110. return ITEM_EQUIP_ACK_FAIL;
  1111. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1112. return ITEM_EQUIP_ACK_FAIL;
  1113. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1114. return ITEM_EQUIP_ACK_FAIL;
  1115. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1116. return ITEM_EQUIP_ACK_FAIL;
  1117. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1118. return ITEM_EQUIP_ACK_FAIL;
  1119. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1120. //Spirit of Super Novice equip bonuses. [Skotlex]
  1121. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1122. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1123. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1124. switch(item->look) { //In weapons, the look determines type of weapon.
  1125. case W_DAGGER: //All level 4 - Daggers
  1126. case W_1HSWORD: //All level 4 - 1H Swords
  1127. case W_1HAXE: //All level 4 - 1H Axes
  1128. case W_MACE: //All level 4 - 1H Maces
  1129. case W_STAFF: //All level 4 - 1H Staves
  1130. case W_2HSTAFF: //All level 4 - 2H Staves
  1131. return ITEM_EQUIP_ACK_OK;
  1132. }
  1133. }
  1134. }
  1135. //Not equipable by class. [Skotlex]
  1136. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1137. return ITEM_EQUIP_ACK_FAIL;
  1138. if (!pc_isItemClass(sd, item))
  1139. return ITEM_EQUIP_ACK_FAIL;
  1140. return ITEM_EQUIP_ACK_OK;
  1141. }
  1142. /*==========================================
  1143. * No problem with the session id
  1144. * set the status that has been sent from char server
  1145. *------------------------------------------*/
  1146. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1147. {
  1148. int i;
  1149. t_tick tick = gettick();
  1150. uint32 ip = session[sd->fd]->client_addr;
  1151. sd->login_id2 = login_id2;
  1152. sd->group_id = group_id;
  1153. /* load user permissions */
  1154. pc_group_pc_load(sd);
  1155. memcpy(&sd->status, st, sizeof(*st));
  1156. if (st->sex != sd->status.sex) {
  1157. clif_authfail_fd(sd->fd, 0);
  1158. return false;
  1159. }
  1160. //Set the map-server used job id. [Skotlex]
  1161. i = pc_jobid2mapid(sd->status.class_);
  1162. if (i == -1) { //Invalid class?
  1163. 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);
  1164. sd->status.class_ = JOB_NOVICE;
  1165. sd->class_ = MAPID_NOVICE;
  1166. } else
  1167. sd->class_ = i;
  1168. // Checks and fixes to character status data, that are required
  1169. // in case of configuration change or stuff, which cannot be
  1170. // checked on char-server.
  1171. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1172. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1173. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1174. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1175. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1176. sd->state.connect_new = 1;
  1177. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1178. sd->invincible_timer = INVALID_TIMER;
  1179. sd->npc_timer_id = INVALID_TIMER;
  1180. sd->pvp_timer = INVALID_TIMER;
  1181. sd->expiration_tid = INVALID_TIMER;
  1182. sd->autotrade_tid = INVALID_TIMER;
  1183. sd->respawn_tid = INVALID_TIMER;
  1184. #ifdef SECURE_NPCTIMEOUT
  1185. // Initialize to defaults/expected
  1186. sd->npc_idle_timer = INVALID_TIMER;
  1187. sd->npc_idle_tick = tick;
  1188. sd->npc_idle_type = NPCT_INPUT;
  1189. sd->state.ignoretimeout = false;
  1190. #endif
  1191. sd->canuseitem_tick = tick;
  1192. sd->canusecashfood_tick = tick;
  1193. sd->canequip_tick = tick;
  1194. sd->cantalk_tick = tick;
  1195. sd->canskill_tick = tick;
  1196. sd->cansendmail_tick = tick;
  1197. sd->idletime = last_tick;
  1198. for(i = 0; i < MAX_SPIRITBALL; i++)
  1199. sd->spirit_timer[i] = INVALID_TIMER;
  1200. if (battle_config.item_auto_get)
  1201. sd->state.autoloot = 10000;
  1202. if (battle_config.disp_experience)
  1203. sd->state.showexp = 1;
  1204. if (battle_config.disp_zeny)
  1205. sd->state.showzeny = 1;
  1206. #ifdef VIP_ENABLE
  1207. if (!battle_config.vip_disp_rate)
  1208. sd->vip.disableshowrate = 1;
  1209. #endif
  1210. if (!(battle_config.display_skill_fail&2))
  1211. sd->state.showdelay = 1;
  1212. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1213. memset(&sd->cart, 0, sizeof(struct s_storage));
  1214. memset(&sd->storage, 0, sizeof(struct s_storage));
  1215. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1216. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1217. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1218. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1219. sd->status.option &= ~OPTION_INVISIBLE;
  1220. }
  1221. status_change_init(&sd->bl);
  1222. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1223. unit_dataset(&sd->bl);
  1224. sd->guild_x = -1;
  1225. sd->guild_y = -1;
  1226. sd->delayed_damage = 0;
  1227. // Event Timers
  1228. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1229. sd->eventtimer[i] = INVALID_TIMER;
  1230. // Rental Timer
  1231. sd->rental_timer = INVALID_TIMER;
  1232. for( i = 0; i < 3; i++ )
  1233. sd->hate_mob[i] = -1;
  1234. sd->quest_log = NULL;
  1235. sd->num_quests = 0;
  1236. sd->avail_quests = 0;
  1237. sd->save_quest = false;
  1238. sd->count_rewarp = 0;
  1239. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1240. sd->regs.arrays = NULL;
  1241. sd->vars_dirty = false;
  1242. sd->vars_ok = false;
  1243. sd->vars_received = 0x0;
  1244. sd->qi_display = nullptr;
  1245. sd->qi_count = 0;
  1246. //warp player
  1247. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1248. 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);
  1249. // try warping to a default map instead (church graveyard)
  1250. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1251. // if we fail again
  1252. clif_authfail_fd(sd->fd, 0);
  1253. return false;
  1254. }
  1255. }
  1256. clif_authok(sd);
  1257. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1258. sd->die_counter=-1;
  1259. //display login notice
  1260. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1261. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1262. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1263. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1264. sd->status.name, sd->status.account_id, sd->status.char_id,
  1265. CONVIP(ip), sd->group_id);
  1266. // Send friends list
  1267. clif_friendslist_send(sd);
  1268. if( !changing_mapservers ) {
  1269. if (battle_config.display_version == 1)
  1270. pc_show_version(sd);
  1271. // Message of the Day [Valaris]
  1272. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1273. if (battle_config.motd_type)
  1274. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1275. else
  1276. clif_displaymessage(sd->fd, motd_text[i]);
  1277. }
  1278. if (expiration_time != 0)
  1279. sd->expiration_time = expiration_time;
  1280. /**
  1281. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1282. **/
  1283. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1284. }
  1285. pc_validate_skill(sd);
  1286. /* [Ind] */
  1287. sd->sc_display = NULL;
  1288. sd->sc_display_count = 0;
  1289. // Player has not yet received the CashShop list
  1290. sd->status.cashshop_sent = false;
  1291. sd->last_addeditem_index = -1;
  1292. sd->bonus_script.head = NULL;
  1293. sd->bonus_script.count = 0;
  1294. #if PACKETVER >= 20150513
  1295. sd->hatEffectIDs = NULL;
  1296. sd->hatEffectCount = 0;
  1297. #endif
  1298. sd->catch_target_class = PET_CATCH_FAIL;
  1299. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1300. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1301. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1302. clif_updatestatus(sd, SP_BASEEXP);
  1303. }
  1304. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1305. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1306. clif_updatestatus(sd, SP_JOBEXP);
  1307. }
  1308. // Request all registries (auth is considered completed whence they arrive)
  1309. intif_request_registry(sd,7);
  1310. return true;
  1311. }
  1312. /*==========================================
  1313. * Closes a connection because it failed to be authenticated from the char server.
  1314. *------------------------------------------*/
  1315. void pc_authfail(struct map_session_data *sd)
  1316. {
  1317. clif_authfail_fd(sd->fd, 0);
  1318. return;
  1319. }
  1320. /**
  1321. * Player register a bl as hatred
  1322. * @param sd : player session
  1323. * @param pos : hate position [0;2]
  1324. * @param bl : target bl
  1325. * @return false:failed, true:success
  1326. */
  1327. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1328. {
  1329. int class_;
  1330. if (!sd || !bl || pos < 0 || pos > 2)
  1331. return false;
  1332. if (sd->hate_mob[pos] != -1)
  1333. { //Can't change hate targets.
  1334. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1335. return false;
  1336. }
  1337. class_ = status_get_class(bl);
  1338. if (!pcdb_checkid(class_)) {
  1339. unsigned int max_hp = status_get_max_hp(bl);
  1340. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1341. return false;
  1342. if (pos != status_get_size(bl))
  1343. return false; //Wrong size
  1344. }
  1345. sd->hate_mob[pos] = class_;
  1346. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1347. clif_hate_info(sd, pos, class_, 1);
  1348. return true;
  1349. }
  1350. /*==========================================
  1351. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1352. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1353. * See intif_parse_StorageReceived() for item operations [lighta]
  1354. *------------------------------------------*/
  1355. void pc_reg_received(struct map_session_data *sd)
  1356. {
  1357. uint8 i;
  1358. sd->vars_ok = true;
  1359. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1360. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1361. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1362. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1363. if (msg_checklangtype(sd->langtype,true) < 0)
  1364. sd->langtype = 0; //invalid langtype reset to default
  1365. // Cash shop
  1366. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1367. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1368. // Cooking Exp
  1369. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1370. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1371. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1372. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1373. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1374. }
  1375. if (battle_config.feature_banking)
  1376. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1377. if (battle_config.feature_roulette) {
  1378. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1379. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1380. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1381. }
  1382. sd->roulette.prizeIdx = -1;
  1383. //SG map and mob read [Komurka]
  1384. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1385. uint16 j;
  1386. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1387. sd->feel_map[i].index = j;
  1388. sd->feel_map[i].m = map_mapindex2mapid(j);
  1389. } else {
  1390. sd->feel_map[i].index = 0;
  1391. sd->feel_map[i].m = -1;
  1392. }
  1393. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1394. }
  1395. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1396. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1397. sd->cloneskill_idx = skill_get_index(skid);
  1398. if (sd->cloneskill_idx > 0) {
  1399. sd->status.skill[sd->cloneskill_idx].id = skid;
  1400. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1401. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1402. sd->status.skill[sd->cloneskill_idx].lv = i;
  1403. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1404. }
  1405. }
  1406. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1407. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1408. sd->reproduceskill_idx = skill_get_index(skid);
  1409. if (sd->reproduceskill_idx > 0) {
  1410. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1411. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1412. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1413. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1414. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1415. }
  1416. }
  1417. //Weird... maybe registries were reloaded?
  1418. if (sd->state.active)
  1419. return;
  1420. sd->state.active = 1;
  1421. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1422. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1423. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1424. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1425. if (sd->status.party_id)
  1426. party_member_joined(sd);
  1427. if (sd->status.guild_id)
  1428. guild_member_joined(sd);
  1429. if( sd->status.clan_id )
  1430. clan_member_joined(sd);
  1431. // pet
  1432. if (sd->status.pet_id > 0)
  1433. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1434. // Homunculus [albator]
  1435. if( sd->status.hom_id > 0 )
  1436. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1437. if( sd->status.mer_id > 0 )
  1438. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1439. if( sd->status.ele_id > 0 )
  1440. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1441. map_addiddb(&sd->bl);
  1442. map_delnickdb(sd->status.char_id, sd->status.name);
  1443. if (!chrif_auth_finished(sd))
  1444. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1445. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1446. chrif_bsdata_request(sd->status.char_id);
  1447. #ifdef VIP_ENABLE
  1448. sd->vip.time = 0;
  1449. sd->vip.enabled = 0;
  1450. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1451. #endif
  1452. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1453. intif_request_questlog(sd);
  1454. if (battle_config.feature_achievement) {
  1455. sd->achievement_data.total_score = 0;
  1456. sd->achievement_data.level = 0;
  1457. sd->achievement_data.save = false;
  1458. sd->achievement_data.count = 0;
  1459. sd->achievement_data.incompleteCount = 0;
  1460. sd->achievement_data.achievements = NULL;
  1461. intif_request_achievements(sd->status.char_id);
  1462. }
  1463. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1464. sd->state.connect_new = 1;
  1465. clif_parse_LoadEndAck(sd->fd, sd);
  1466. }
  1467. if( pc_isinvisible(sd) ) {
  1468. sd->vd.class_ = JT_INVISIBLE;
  1469. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1470. // decrement the number of pvp players on the map
  1471. map_getmapdata(sd->bl.m)->users_pvp--;
  1472. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1473. // unregister the player for ranking
  1474. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1475. sd->pvp_timer = INVALID_TIMER;
  1476. }
  1477. clif_changeoption( &sd->bl );
  1478. }
  1479. channel_autojoin(sd);
  1480. }
  1481. static int pc_calc_skillpoint(struct map_session_data* sd)
  1482. {
  1483. uint16 i, skill_point = 0;
  1484. nullpo_ret(sd);
  1485. for(i = 1; i < MAX_SKILL; i++) {
  1486. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1487. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1488. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1489. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1490. )
  1491. {
  1492. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1493. skill_point += sd->status.skill[i].lv;
  1494. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1495. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1496. }
  1497. }
  1498. }
  1499. return skill_point;
  1500. }
  1501. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1502. uint16 i = 0;
  1503. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1504. return false;
  1505. /**
  1506. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1507. * skill tree and since they have no icons they'll give resource errors
  1508. * Get ALL skills except npc/guild ones. [Skotlex]
  1509. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1510. **/
  1511. for( i = 0; i < MAX_SKILL; i++ ) {
  1512. uint16 skill_id = skill_idx2id(i);
  1513. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1514. continue;
  1515. switch (skill_id) {
  1516. case SM_SELFPROVOKE:
  1517. case AB_DUPLELIGHT_MELEE:
  1518. case AB_DUPLELIGHT_MAGIC:
  1519. case WL_CHAINLIGHTNING_ATK:
  1520. case WL_TETRAVORTEX_FIRE:
  1521. case WL_TETRAVORTEX_WATER:
  1522. case WL_TETRAVORTEX_WIND:
  1523. case WL_TETRAVORTEX_GROUND:
  1524. case WL_SUMMON_ATK_FIRE:
  1525. case WL_SUMMON_ATK_WIND:
  1526. case WL_SUMMON_ATK_WATER:
  1527. case WL_SUMMON_ATK_GROUND:
  1528. case LG_OVERBRAND_BRANDISH:
  1529. case LG_OVERBRAND_PLUSATK:
  1530. case WM_SEVERE_RAINSTORM_MELEE:
  1531. case RL_R_TRIP_PLUSATK:
  1532. case SG_DEVIL:
  1533. case MO_TRIPLEATTACK:
  1534. case RG_SNATCHER:
  1535. continue;
  1536. default:
  1537. {
  1538. uint8 lv = (uint8)skill_get_max(skill_id);
  1539. if (lv > 0) {
  1540. sd->status.skill[i].id = skill_id;
  1541. if (addlv)
  1542. sd->status.skill[i].lv = lv;
  1543. }
  1544. }
  1545. break;
  1546. }
  1547. }
  1548. return true;
  1549. }
  1550. /*==========================================
  1551. * Calculation of skill level.
  1552. * @param sd
  1553. *------------------------------------------*/
  1554. void pc_calc_skilltree(struct map_session_data *sd)
  1555. {
  1556. int i, flag;
  1557. int c = 0;
  1558. nullpo_retv(sd);
  1559. i = pc_calc_skilltree_normalize_job(sd);
  1560. c = pc_mapid2jobid(i, sd->status.sex);
  1561. if( c == -1 )
  1562. { //Unable to normalize job??
  1563. 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);
  1564. return;
  1565. }
  1566. c = pc_class2idx(c);
  1567. for( i = 0; i < MAX_SKILL; i++ ) {
  1568. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1569. sd->status.skill[i].id = 0; //First clear skills.
  1570. /* permanent skills that must be re-checked */
  1571. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1572. uint16 sk_id = skill_idx2id(i);
  1573. if (!sk_id) {
  1574. sd->status.skill[i].id = 0;
  1575. sd->status.skill[i].lv = 0;
  1576. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1577. continue;
  1578. }
  1579. switch (sk_id) {
  1580. case NV_TRICKDEAD:
  1581. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1582. sd->status.skill[i].id = 0;
  1583. sd->status.skill[i].lv = 0;
  1584. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1585. }
  1586. break;
  1587. }
  1588. }
  1589. }
  1590. for( i = 0; i < MAX_SKILL; i++ ) {
  1591. uint16 skill_id = 0;
  1592. // Restore original level of skills after deleting earned skills.
  1593. 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 ) {
  1594. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1595. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1596. }
  1597. //Enable Bard/Dancer spirit linked skills.
  1598. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1599. continue;
  1600. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1601. //Link Dancer skills to bard.
  1602. if( sd->status.sex ) {
  1603. if( sd->status.skill[i-8].lv < 10 )
  1604. continue;
  1605. sd->status.skill[i].id = skill_id;
  1606. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1607. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1608. }
  1609. //Link Bard skills to dancer.
  1610. else {
  1611. if( sd->status.skill[i].lv < 10 )
  1612. continue;
  1613. sd->status.skill[i-8].id = skill_id - 8;
  1614. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1615. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1616. }
  1617. }
  1618. }
  1619. // Removes Taekwon Ranker skill bonus
  1620. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1621. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1622. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1623. uint16 sk_id = skill_tree[c_][i].skill_id;
  1624. uint16 sk_idx = 0;
  1625. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1626. continue;
  1627. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1628. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1629. continue;
  1630. sd->status.skill[sk_idx].id = 0;
  1631. }
  1632. }
  1633. }
  1634. // Grant all skills
  1635. pc_grant_allskills(sd, false);
  1636. do {
  1637. uint16 skid = 0;
  1638. flag = 0;
  1639. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1640. bool fail = false;
  1641. uint16 sk_idx = skill_get_index(skid);
  1642. if (sd->status.skill[sk_idx].id)
  1643. continue; //Skill already known.
  1644. if (!battle_config.skillfree) {
  1645. uint8 j;
  1646. // Checking required skills
  1647. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1648. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1649. uint16 sk_need_idx = 0;
  1650. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1651. short sk_need = sk_need_id;
  1652. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  1653. sk_need = 0; //Not learned.
  1654. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1655. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1656. else
  1657. sk_need = pc_checkskill(sd,sk_need_id);
  1658. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1659. fail = true;
  1660. break;
  1661. }
  1662. }
  1663. }
  1664. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1665. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1666. class_ = pc_class2idx(class_);
  1667. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1668. fail = true; // base level requirement wasn't satisfied
  1669. }
  1670. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1671. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1672. class_ = pc_class2idx(class_);
  1673. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1674. fail = true; // job level requirement wasn't satisfied
  1675. }
  1676. }
  1677. if (!fail) {
  1678. int inf2 = skill_get_inf2(skid);
  1679. if (!sd->status.skill[sk_idx].lv && (
  1680. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1681. inf2&INF2_WEDDING_SKILL ||
  1682. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1683. ))
  1684. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1685. sd->status.skill[sk_idx].id = skid;
  1686. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1687. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1688. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1689. }
  1690. flag = 1; // skill list has changed, perform another pass
  1691. }
  1692. }
  1693. } while(flag);
  1694. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1695. unsigned short skid = 0;
  1696. /* Taekwon Ranker Bonus Skill Tree
  1697. ============================================
  1698. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1699. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1700. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1701. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1702. uint16 sk_idx = 0;
  1703. if (!(sk_idx = skill_get_index(skid)))
  1704. continue;
  1705. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1706. continue; //Do not include Quest/Wedding skills.
  1707. if( sd->status.skill[sk_idx].id == 0 ) {
  1708. sd->status.skill[sk_idx].id = skid;
  1709. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1710. } else if( skid != NV_BASIC )
  1711. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1712. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1713. }
  1714. }
  1715. }
  1716. //Checks if you can learn a new skill after having leveled up a skill.
  1717. static void pc_check_skilltree(struct map_session_data *sd)
  1718. {
  1719. int i, flag = 0;
  1720. int c = 0;
  1721. if (battle_config.skillfree)
  1722. return; //Function serves no purpose if this is set
  1723. i = pc_calc_skilltree_normalize_job(sd);
  1724. c = pc_mapid2jobid(i, sd->status.sex);
  1725. if (c == -1) { //Unable to normalize job??
  1726. 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);
  1727. return;
  1728. }
  1729. c = pc_class2idx(c);
  1730. do {
  1731. uint16 skid = 0;
  1732. flag = 0;
  1733. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1734. uint16 sk_idx = skill_get_index(skid);
  1735. bool fail = false;
  1736. uint8 j = 0;
  1737. if (sd->status.skill[sk_idx].id) //Already learned
  1738. continue;
  1739. // Checking required skills
  1740. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1741. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1742. uint16 sk_need_idx = 0;
  1743. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1744. short sk_need = sk_need_id;
  1745. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  1746. sk_need = 0; //Not learned.
  1747. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1748. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1749. else
  1750. sk_need = pc_checkskill(sd,sk_need_id);
  1751. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1752. fail = true;
  1753. break;
  1754. }
  1755. }
  1756. }
  1757. if( fail )
  1758. continue;
  1759. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1760. continue;
  1761. j = skill_get_inf2(skid);
  1762. if( !sd->status.skill[sk_idx].lv && (
  1763. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1764. j&INF2_WEDDING_SKILL ||
  1765. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1766. ) )
  1767. continue; //Cannot be learned via normal means.
  1768. sd->status.skill[sk_idx].id = skid;
  1769. flag = 1;
  1770. }
  1771. } while(flag);
  1772. }
  1773. // Make sure all the skills are in the correct condition
  1774. // before persisting to the backend.. [MouseJstr]
  1775. void pc_clean_skilltree(struct map_session_data *sd)
  1776. {
  1777. uint16 i;
  1778. for (i = 0; i < MAX_SKILL; i++){
  1779. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1780. sd->status.skill[i].id = 0;
  1781. sd->status.skill[i].lv = 0;
  1782. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1783. }
  1784. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1785. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1786. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1787. }
  1788. }
  1789. }
  1790. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1791. {
  1792. int skill_point, novice_skills;
  1793. int c = sd->class_;
  1794. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1795. return c;
  1796. skill_point = pc_calc_skillpoint(sd);
  1797. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1798. // limit 1st class and above to novice job levels
  1799. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1800. {
  1801. c = MAPID_NOVICE;
  1802. }
  1803. // limit 2nd class and above to first class job levels (super novices are exempt)
  1804. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1805. {
  1806. // regenerate change_level_2nd
  1807. if (!sd->change_level_2nd)
  1808. {
  1809. if (sd->class_&JOBL_THIRD)
  1810. {
  1811. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1812. if (!sd->change_level_3rd)
  1813. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1814. else
  1815. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1816. - (sd->status.job_level - 1)
  1817. - (sd->change_level_3rd - 1)
  1818. - novice_skills;
  1819. }
  1820. else
  1821. {
  1822. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1823. - (sd->status.job_level - 1)
  1824. - novice_skills;
  1825. }
  1826. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1827. }
  1828. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1829. {
  1830. c &= MAPID_BASEMASK;
  1831. }
  1832. // limit 3rd class to 2nd class/trans job levels
  1833. else if(sd->class_&JOBL_THIRD)
  1834. {
  1835. // regenerate change_level_3rd
  1836. if (!sd->change_level_3rd)
  1837. {
  1838. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1839. - (sd->status.job_level - 1)
  1840. - (sd->change_level_2nd - 1)
  1841. - novice_skills;
  1842. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1843. }
  1844. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1845. c &= MAPID_UPPERMASK;
  1846. }
  1847. }
  1848. // restore non-limiting flags
  1849. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1850. return c;
  1851. }
  1852. /*==========================================
  1853. * Updates the weight status
  1854. *------------------------------------------
  1855. * 1: overweight 50% for pre-renewal and 70% for renewal
  1856. * 2: overweight 90%
  1857. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1858. */
  1859. void pc_updateweightstatus(struct map_session_data *sd)
  1860. {
  1861. int old_overweight;
  1862. int new_overweight;
  1863. nullpo_retv(sd);
  1864. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1865. #ifdef RENEWAL
  1866. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1867. #else
  1868. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1869. #endif
  1870. if( old_overweight == new_overweight )
  1871. return; // no change
  1872. // stop old status change
  1873. if( old_overweight == 1 )
  1874. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1875. else if( old_overweight == 2 )
  1876. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1877. // start new status change
  1878. if( new_overweight == 1 )
  1879. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1880. else if( new_overweight == 2 )
  1881. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1882. // update overweight status
  1883. sd->regen.state.overweight = new_overweight;
  1884. }
  1885. int pc_disguise(struct map_session_data *sd, int class_)
  1886. {
  1887. if (!class_ && !sd->disguise)
  1888. return 0;
  1889. if (class_ && sd->disguise == class_)
  1890. return 0;
  1891. if(pc_isinvisible(sd))
  1892. { //Character is invisible. Stealth class-change. [Skotlex]
  1893. sd->disguise = class_; //viewdata is set on uncloaking.
  1894. return 2;
  1895. }
  1896. if (sd->bl.prev != NULL) {
  1897. pc_stop_walking(sd, 0);
  1898. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1899. }
  1900. if (!class_) {
  1901. sd->disguise = 0;
  1902. class_ = sd->status.class_;
  1903. } else
  1904. sd->disguise=class_;
  1905. status_set_viewdata(&sd->bl, class_);
  1906. clif_changeoption(&sd->bl);
  1907. if (sd->bl.prev != NULL) {
  1908. clif_spawn(&sd->bl);
  1909. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1910. { //It seems the cart info is lost on undisguise.
  1911. clif_cartlist(sd);
  1912. clif_updatestatus(sd,SP_CARTINFO);
  1913. }
  1914. if (sd->chatID) {
  1915. struct chat_data* cd;
  1916. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1917. clif_dispchat(cd,0);
  1918. }
  1919. }
  1920. return 1;
  1921. }
  1922. /// Show error message
  1923. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1924. /// Check for valid Element, break & show error message if invalid Element
  1925. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1926. /// Check for valid Race, break & show error message if invalid Race
  1927. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1928. /// Check for valid Race2, break & show error message if invalid Race2
  1929. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1930. /// Check for valid Class, break & show error message if invalid Class
  1931. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1932. /// Check for valid Size, break & show error message if invalid Size
  1933. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1934. /// Check for valid SC, break & show error message if invalid SC
  1935. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1936. /**
  1937. * Add auto spell bonus for player while attacking/attacked
  1938. * @param spell: Spell array
  1939. * @param id: Skill to cast
  1940. * @param lv: Skill level
  1941. * @param rate: Success chance
  1942. * @param flag: Battle flag
  1943. * @param card_id: Used to prevent card stacking
  1944. */
  1945. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1946. {
  1947. if (spell.size() == MAX_PC_BONUS) {
  1948. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1949. return;
  1950. }
  1951. if (!rate)
  1952. return;
  1953. if (!(flag&BF_RANGEMASK))
  1954. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1955. if (!(flag&BF_WEAPONMASK))
  1956. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1957. if (!(flag&BF_SKILLMASK)) {
  1958. if (flag&(BF_MAGIC | BF_MISC))
  1959. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1960. if (flag&BF_WEAPON)
  1961. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1962. }
  1963. for (auto &it : spell) {
  1964. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  1965. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  1966. return;
  1967. it.rate = cap_value(it.rate + rate, -10000, 10000);
  1968. return;
  1969. }
  1970. }
  1971. struct s_autospell entry = {};
  1972. if (rate < -10000 || rate > 10000)
  1973. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1974. entry.id = id;
  1975. entry.lv = lv;
  1976. entry.rate = cap_value(rate, -10000, 10000);
  1977. entry.flag = flag;
  1978. entry.card_id = card_id;
  1979. spell.push_back(entry);
  1980. }
  1981. /**
  1982. * Add auto spell bonus for player while using skills
  1983. * @param spell: Spell array
  1984. * @param src_skill: Trigger skill
  1985. * @param id: Support or target type
  1986. * @param lv: Skill level
  1987. * @param rate: Success chance
  1988. * @param card_id: Used to prevent card stacking
  1989. */
  1990. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1991. {
  1992. if (spell.size() == MAX_PC_BONUS) {
  1993. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1994. return;
  1995. }
  1996. if (!rate)
  1997. return;
  1998. struct s_autospell entry = {};
  1999. if (rate < -10000 || rate > 10000)
  2000. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2001. entry.flag = src_skill;
  2002. entry.id = id;
  2003. entry.lv = lv;
  2004. entry.rate = cap_value(rate, -10000, 10000);
  2005. entry.card_id = card_id;
  2006. spell.push_back(entry);
  2007. }
  2008. /**
  2009. * Add inflict effect bonus for player while attacking/attacked
  2010. * @param effect: Effect array
  2011. * @param sc: SC/Effect type
  2012. * @param rate: Success chance
  2013. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2014. * @param flag: Target flag
  2015. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2016. */
  2017. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2018. {
  2019. if (effect.size() == MAX_PC_BONUS) {
  2020. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2021. return;
  2022. }
  2023. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2024. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2025. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2026. flag |= ATF_TARGET; //Default target: enemy.
  2027. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2028. flag |= ATF_WEAPON; //Default type: weapon.
  2029. if (!duration)
  2030. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2031. for (auto &it : effect) {
  2032. if (it.sc == sc && it.flag == flag) {
  2033. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2034. it.arrow_rate += arrow_rate;
  2035. it.duration = umax(it.duration, duration);
  2036. return;
  2037. }
  2038. }
  2039. struct s_addeffect entry = {};
  2040. if (rate < -10000 || rate > 10000)
  2041. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2042. entry.sc = sc;
  2043. entry.rate = cap_value(rate, -10000, 10000);
  2044. entry.arrow_rate = arrow_rate;
  2045. entry.flag = flag;
  2046. entry.duration = duration;
  2047. effect.push_back(entry);
  2048. }
  2049. /**
  2050. * Add inflict effect bonus for player while attacking using skill
  2051. * @param effect: Effect array
  2052. * @param sc: SC/Effect type
  2053. * @param rate: Success chance
  2054. * @param skill_id: Skill to cast
  2055. * @param target: Target type
  2056. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2057. */
  2058. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  2059. {
  2060. if (effect.size() == MAX_PC_BONUS) {
  2061. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2062. return;
  2063. }
  2064. if (!duration)
  2065. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2066. for (auto &it : effect) {
  2067. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2068. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2069. it.duration = umax(it.duration, duration);
  2070. return;
  2071. }
  2072. }
  2073. struct s_addeffectonskill entry = {};
  2074. if (rate < -10000 || rate > 10000)
  2075. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2076. entry.sc = sc;
  2077. entry.rate = cap_value(rate, -10000, 10000);
  2078. entry.skill_id = skill_id;
  2079. entry.target = target;
  2080. entry.duration = duration;
  2081. effect.push_back(entry);
  2082. }
  2083. /**
  2084. * Adjust/add drop rate modifier for player
  2085. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2086. * @param nameid: item id that will be dropped
  2087. * @param group: group id
  2088. * @param class_: target class
  2089. * @param race: target race. if < 0, means monster_id
  2090. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2091. */
  2092. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2093. {
  2094. if (!nameid && !group) {
  2095. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2096. return;
  2097. }
  2098. if (nameid && !itemdb_exists(nameid)) {
  2099. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2100. return;
  2101. }
  2102. if (group && !itemdb_group_exists(group)) {
  2103. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2104. return;
  2105. }
  2106. if (drop.size() == MAX_PC_BONUS) {
  2107. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %hu group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2108. return;
  2109. }
  2110. //Apply config rate adjustment settings.
  2111. if (rate >= 0) { //Absolute drop.
  2112. if (battle_config.item_rate_adddrop != 100)
  2113. rate = rate*battle_config.item_rate_adddrop/100;
  2114. if (rate < battle_config.item_drop_adddrop_min)
  2115. rate = battle_config.item_drop_adddrop_min;
  2116. else if (rate > battle_config.item_drop_adddrop_max)
  2117. rate = battle_config.item_drop_adddrop_max;
  2118. } else { //Relative drop, max/min limits are applied at drop time.
  2119. if (battle_config.item_rate_adddrop != 100)
  2120. rate = rate*battle_config.item_rate_adddrop/100;
  2121. if (rate > -1)
  2122. rate = -1;
  2123. }
  2124. for (auto &it : drop) {
  2125. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2126. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2127. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2128. return;
  2129. }
  2130. }
  2131. struct s_add_drop entry = {};
  2132. if (rate < -10000 || rate > 10000)
  2133. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2134. entry.nameid = nameid;
  2135. entry.group = group;
  2136. entry.race = race;
  2137. entry.class_ = class_;
  2138. entry.rate = cap_value(rate, -10000, 10000);
  2139. drop.push_back(entry);
  2140. }
  2141. /**
  2142. * Add autobonus to player when attacking/attacked
  2143. * @param bonus: Bonus array
  2144. * @param script: Script to execute
  2145. * @param rate: Success chance
  2146. * @param dur: Duration
  2147. * @param flag: Battle flag/skill
  2148. * @param other_script: Secondary script to execute
  2149. * @param pos: Item equip position
  2150. * @param onskill: Skill used to trigger autobonus
  2151. * @return True on success or false otherwise
  2152. */
  2153. bool pc_addautobonus(std::vector<s_autobonus> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill)
  2154. {
  2155. if (bonus.size() == MAX_PC_BONUS) {
  2156. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2157. return false;
  2158. }
  2159. if (!onskill) {
  2160. if (!(flag&BF_RANGEMASK))
  2161. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2162. if (!(flag&BF_WEAPONMASK))
  2163. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2164. if (!(flag&BF_SKILLMASK)) {
  2165. if (flag&(BF_MAGIC | BF_MISC))
  2166. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2167. if (flag&BF_WEAPON)
  2168. flag |= BF_NORMAL | BF_SKILL;
  2169. }
  2170. }
  2171. struct s_autobonus entry = {};
  2172. if (rate < -10000 || rate > 10000)
  2173. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2174. entry.rate = cap_value(rate, -10000, 10000);
  2175. entry.duration = dur;
  2176. entry.active = INVALID_TIMER;
  2177. entry.atk_type = flag;
  2178. entry.pos = pos;
  2179. entry.bonus_script = aStrdup(script);
  2180. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2181. bonus.push_back(entry);
  2182. return true;
  2183. }
  2184. /**
  2185. * Remove an autobonus from player
  2186. * @param sd: Player data
  2187. * @param bonus: Autobonus array
  2188. * @param restore: Run script on clearing or not
  2189. */
  2190. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2191. {
  2192. nullpo_retv(sd);
  2193. std::vector<s_autobonus>::iterator it = bonus.begin();
  2194. while( it != bonus.end() ){
  2195. s_autobonus b = *it;
  2196. if (b.active != INVALID_TIMER) {
  2197. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2198. if (b.bonus_script) {
  2199. unsigned int equip_pos_idx = 0;
  2200. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2201. for (uint8 j = 0; j < EQI_MAX; j++) {
  2202. if (sd->equip_index[j] >= 0)
  2203. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2204. }
  2205. if ((equip_pos_idx&b.pos) == b.pos)
  2206. script_run_autobonus(b.bonus_script, sd, b.pos);
  2207. }
  2208. it++;
  2209. continue;
  2210. } else { // Logout / Unequipped an item with an activated bonus
  2211. delete_timer(b.active, pc_endautobonus);
  2212. b.active = INVALID_TIMER;
  2213. }
  2214. }
  2215. if (b.bonus_script)
  2216. aFree(b.bonus_script);
  2217. if (b.other_script)
  2218. aFree(b.other_script);
  2219. it = bonus.erase(it);
  2220. }
  2221. }
  2222. /**
  2223. * Execute autobonus on player
  2224. * @param sd: Player data
  2225. * @param autobonus: Autobonus to run
  2226. */
  2227. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2228. {
  2229. nullpo_retv(sd);
  2230. nullpo_retv(autobonus);
  2231. if (autobonus->active != INVALID_TIMER)
  2232. delete_timer(autobonus->active, pc_endautobonus);
  2233. if( autobonus->other_script )
  2234. {
  2235. int j;
  2236. unsigned int equip_pos_idx = 0;
  2237. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2238. for(j = 0; j < EQI_MAX; j++) {
  2239. if(sd->equip_index[j] >= 0)
  2240. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2241. }
  2242. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2243. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2244. }
  2245. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2246. sd->state.autobonus |= autobonus->pos;
  2247. status_calc_pc(sd,SCO_FORCE);
  2248. }
  2249. /**
  2250. * Remove autobonus timer from player
  2251. */
  2252. TIMER_FUNC(pc_endautobonus){
  2253. struct map_session_data *sd = map_id2sd(id);
  2254. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2255. nullpo_ret(sd);
  2256. nullpo_ret(bonus);
  2257. for( struct s_autobonus& autobonus : *bonus ){
  2258. if( autobonus.active == tid ){
  2259. autobonus.active = INVALID_TIMER;
  2260. sd->state.autobonus &= ~autobonus.pos;
  2261. break;
  2262. }
  2263. }
  2264. status_calc_pc(sd,SCO_FORCE);
  2265. return 0;
  2266. }
  2267. /**
  2268. * Add element bonus to player when attacking
  2269. * @param sd: Player data
  2270. * @param ele: Element to adjust
  2271. * @param rate: Success chance
  2272. * @param flag: Battle flag
  2273. */
  2274. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2275. {
  2276. nullpo_retv(sd);
  2277. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2278. if (wd->addele2.size() == MAX_PC_BONUS) {
  2279. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2280. return;
  2281. }
  2282. if (!(flag&BF_RANGEMASK))
  2283. flag |= BF_SHORT | BF_LONG;
  2284. if (!(flag&BF_WEAPONMASK))
  2285. flag |= BF_WEAPON;
  2286. if (!(flag&BF_SKILLMASK)) {
  2287. if (flag&(BF_MAGIC | BF_MISC))
  2288. flag |= BF_SKILL;
  2289. if (flag&BF_WEAPON)
  2290. flag |= BF_NORMAL | BF_SKILL;
  2291. }
  2292. for (auto &it : wd->addele2) {
  2293. if (it.ele == ele && it.flag == flag) {
  2294. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2295. return;
  2296. }
  2297. }
  2298. struct s_addele2 entry = {};
  2299. if (rate < -10000 || rate > 10000)
  2300. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2301. entry.ele = ele;
  2302. entry.rate = cap_value(rate, -10000, 10000);
  2303. entry.flag = flag;
  2304. wd->addele2.push_back(entry);
  2305. }
  2306. /**
  2307. * Reduce element bonus to player when attacking
  2308. * @param sd: Player data
  2309. * @param ele: Element to adjust
  2310. * @param rate: Success chance
  2311. * @param flag: Battle flag
  2312. */
  2313. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2314. {
  2315. nullpo_retv(sd);
  2316. if (sd->subele2.size() == MAX_PC_BONUS) {
  2317. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2318. return;
  2319. }
  2320. if (!(flag&BF_RANGEMASK))
  2321. flag |= BF_SHORT | BF_LONG;
  2322. if (!(flag&BF_WEAPONMASK))
  2323. flag |= BF_WEAPON;
  2324. if (!(flag&BF_SKILLMASK)) {
  2325. if (flag&(BF_MAGIC | BF_MISC))
  2326. flag |= BF_SKILL;
  2327. if (flag&BF_WEAPON)
  2328. flag |= BF_NORMAL | BF_SKILL;
  2329. }
  2330. for (auto &it : sd->subele2) {
  2331. if (it.ele == ele && it.flag == flag) {
  2332. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2333. return;
  2334. }
  2335. }
  2336. struct s_addele2 entry = {};
  2337. if (rate < -10000 || rate > 10000)
  2338. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2339. entry.ele = ele;
  2340. entry.rate = cap_value(rate, -10000, 10000);
  2341. entry.flag = flag;
  2342. sd->subele2.push_back(entry);
  2343. }
  2344. /**
  2345. * General item bonus for player
  2346. * @param bonus: Bonus array
  2347. * @param id: Key
  2348. * @param val: Value
  2349. * @param cap_rate: If Value is a rate value that needs to be capped
  2350. */
  2351. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2352. {
  2353. for (auto &it : bonus) {
  2354. if (it.id == id) {
  2355. if (cap_rate)
  2356. it.val = cap_value(it.val + val, -10000, 10000);
  2357. else
  2358. it.val += val;
  2359. return;
  2360. }
  2361. }
  2362. struct s_item_bonus entry = {};
  2363. if (cap_rate && (val < -10000 || val > 10000)) {
  2364. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2365. val = cap_value(val, -10000, 10000);
  2366. }
  2367. entry.id = id;
  2368. entry.val = val;
  2369. bonus.push_back(entry);
  2370. }
  2371. /**
  2372. * Remove HP/SP to player when attacking
  2373. * @param bonus: Bonus array
  2374. * @param rate: Success chance
  2375. * @param per: Percentage of HP/SP to vanish
  2376. * @param flag: Battle flag
  2377. */
  2378. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2379. if (bonus.size() == MAX_PC_BONUS) {
  2380. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2381. return;
  2382. }
  2383. if (!(flag&BF_RANGEMASK))
  2384. flag |= BF_SHORT | BF_LONG;
  2385. if (!(flag&BF_WEAPONMASK))
  2386. flag |= BF_WEAPON;
  2387. if (!(flag&BF_SKILLMASK)) {
  2388. if (flag&(BF_MAGIC | BF_MISC))
  2389. flag |= BF_SKILL;
  2390. if (flag&BF_WEAPON)
  2391. flag |= BF_NORMAL | BF_SKILL;
  2392. }
  2393. for (auto &it : bonus) {
  2394. if (it.flag == flag) {
  2395. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2396. it.per += per;
  2397. return;
  2398. }
  2399. }
  2400. struct s_vanish_bonus entry = {};
  2401. if (rate < -10000 || rate > 10000)
  2402. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2403. entry.rate = cap_value(rate, -10000, 10000);
  2404. entry.per = per;
  2405. entry.flag = flag;
  2406. bonus.push_back(entry);
  2407. }
  2408. /*==========================================
  2409. * Add a bonus(type) to player sd
  2410. * format: bonus bBonusName,val;
  2411. * @param sd
  2412. * @param type Bonus type used by bBonusName
  2413. * @param val Value that usually for rate or fixed value
  2414. *------------------------------------------*/
  2415. void pc_bonus(struct map_session_data *sd,int type,int val)
  2416. {
  2417. struct status_data *status;
  2418. int bonus;
  2419. nullpo_retv(sd);
  2420. status = &sd->base_status;
  2421. switch(type){
  2422. case SP_STR:
  2423. case SP_AGI:
  2424. case SP_VIT:
  2425. case SP_INT:
  2426. case SP_DEX:
  2427. case SP_LUK:
  2428. if(sd->state.lr_flag != 2)
  2429. sd->param_bonus[type-SP_STR]+=val;
  2430. break;
  2431. case SP_ATK1:
  2432. if(!sd->state.lr_flag) {
  2433. bonus = status->rhw.atk + val;
  2434. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2435. }
  2436. else if(sd->state.lr_flag == 1) {
  2437. bonus = status->lhw.atk + val;
  2438. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2439. }
  2440. break;
  2441. case SP_ATK2:
  2442. if(!sd->state.lr_flag) {
  2443. bonus = status->rhw.atk2 + val;
  2444. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2445. }
  2446. else if(sd->state.lr_flag == 1) {
  2447. bonus = status->lhw.atk2 + val;
  2448. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2449. }
  2450. break;
  2451. case SP_BASE_ATK:
  2452. if(sd->state.lr_flag != 2) {
  2453. #ifdef RENEWAL
  2454. bonus = sd->bonus.eatk + val;
  2455. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2456. #else
  2457. bonus = status->batk + val;
  2458. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2459. #endif
  2460. }
  2461. break;
  2462. case SP_DEF1:
  2463. if(sd->state.lr_flag != 2) {
  2464. bonus = status->def + val;
  2465. #ifdef RENEWAL
  2466. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2467. #else
  2468. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2469. #endif
  2470. }
  2471. break;
  2472. case SP_DEF2:
  2473. if(sd->state.lr_flag != 2) {
  2474. bonus = status->def2 + val;
  2475. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2476. }
  2477. break;
  2478. case SP_MDEF1:
  2479. if(sd->state.lr_flag != 2) {
  2480. bonus = status->mdef + val;
  2481. #ifdef RENEWAL
  2482. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2483. #else
  2484. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2485. #endif
  2486. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2487. sd->bonus.shieldmdef += bonus;
  2488. }
  2489. }
  2490. break;
  2491. case SP_MDEF2:
  2492. if(sd->state.lr_flag != 2) {
  2493. bonus = status->mdef2 + val;
  2494. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2495. }
  2496. break;
  2497. case SP_HIT:
  2498. if(sd->state.lr_flag != 2) {
  2499. bonus = status->hit + val;
  2500. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2501. } else
  2502. sd->bonus.arrow_hit+=val;
  2503. break;
  2504. case SP_FLEE1:
  2505. if(sd->state.lr_flag != 2) {
  2506. bonus = status->flee + val;
  2507. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2508. }
  2509. break;
  2510. case SP_FLEE2:
  2511. if(sd->state.lr_flag != 2) {
  2512. bonus = status->flee2 + val*10;
  2513. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2514. }
  2515. break;
  2516. case SP_CRITICAL:
  2517. if(sd->state.lr_flag != 2) {
  2518. bonus = status->cri + val*10;
  2519. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2520. } else
  2521. sd->bonus.arrow_cri += val*10;
  2522. break;
  2523. case SP_ATKELE:
  2524. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2525. switch (sd->state.lr_flag)
  2526. {
  2527. case 2:
  2528. switch (sd->status.weapon) {
  2529. case W_BOW:
  2530. case W_REVOLVER:
  2531. case W_RIFLE:
  2532. case W_GATLING:
  2533. case W_SHOTGUN:
  2534. case W_GRENADE:
  2535. //Become weapon element.
  2536. status->rhw.ele=val;
  2537. break;
  2538. default: //Become arrow element.
  2539. sd->bonus.arrow_ele=val;
  2540. break;
  2541. }
  2542. break;
  2543. case 1:
  2544. status->lhw.ele=val;
  2545. break;
  2546. default:
  2547. status->rhw.ele=val;
  2548. break;
  2549. }
  2550. break;
  2551. case SP_DEFELE:
  2552. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2553. if(sd->state.lr_flag != 2)
  2554. status->def_ele=val;
  2555. break;
  2556. case SP_MAXHP:
  2557. if(sd->state.lr_flag == 2)
  2558. break;
  2559. sd->bonus.hp += val;
  2560. break;
  2561. case SP_MAXSP:
  2562. if(sd->state.lr_flag == 2)
  2563. break;
  2564. sd->bonus.sp += val;
  2565. break;
  2566. case SP_MAXHPRATE:
  2567. if(sd->state.lr_flag != 2)
  2568. sd->hprate+=val;
  2569. break;
  2570. case SP_MAXSPRATE:
  2571. if(sd->state.lr_flag != 2)
  2572. sd->sprate+=val;
  2573. break;
  2574. case SP_SPRATE:
  2575. if(sd->state.lr_flag != 2)
  2576. sd->dsprate+=val;
  2577. break;
  2578. case SP_ATTACKRANGE:
  2579. switch (sd->state.lr_flag) {
  2580. case 2:
  2581. switch (sd->status.weapon) {
  2582. case W_BOW:
  2583. case W_REVOLVER:
  2584. case W_RIFLE:
  2585. case W_GATLING:
  2586. case W_SHOTGUN:
  2587. case W_GRENADE:
  2588. status->rhw.range += val;
  2589. }
  2590. break;
  2591. case 1:
  2592. status->lhw.range += val;
  2593. break;
  2594. default:
  2595. status->rhw.range += val;
  2596. break;
  2597. }
  2598. break;
  2599. case SP_SPEED_RATE: //Non stackable increase
  2600. if(sd->state.lr_flag != 2)
  2601. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2602. break;
  2603. case SP_SPEED_ADDRATE: //Stackable increase
  2604. if(sd->state.lr_flag != 2)
  2605. sd->bonus.speed_add_rate -= val;
  2606. break;
  2607. case SP_ASPD: //Raw increase
  2608. if(sd->state.lr_flag != 2)
  2609. sd->bonus.aspd_add -= 10*val;
  2610. break;
  2611. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2612. if(sd->state.lr_flag != 2)
  2613. #ifndef RENEWAL_ASPD
  2614. status->aspd_rate -= 10*val;
  2615. #else
  2616. status->aspd_rate2 += val;
  2617. #endif
  2618. break;
  2619. case SP_HP_RECOV_RATE:
  2620. if(sd->state.lr_flag != 2)
  2621. sd->hprecov_rate += val;
  2622. break;
  2623. case SP_SP_RECOV_RATE:
  2624. if(sd->state.lr_flag != 2)
  2625. sd->sprecov_rate += val;
  2626. break;
  2627. case SP_CRITICAL_DEF:
  2628. if(sd->state.lr_flag != 2)
  2629. sd->bonus.critical_def += val;
  2630. break;
  2631. case SP_NEAR_ATK_DEF:
  2632. if(sd->state.lr_flag != 2)
  2633. sd->bonus.near_attack_def_rate += val;
  2634. break;
  2635. case SP_LONG_ATK_DEF:
  2636. if(sd->state.lr_flag != 2)
  2637. sd->bonus.long_attack_def_rate += val;
  2638. break;
  2639. case SP_DOUBLE_RATE:
  2640. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2641. sd->bonus.double_rate = val;
  2642. break;
  2643. case SP_DOUBLE_ADD_RATE:
  2644. if(sd->state.lr_flag == 0)
  2645. sd->bonus.double_add_rate += val;
  2646. break;
  2647. case SP_MATK_RATE:
  2648. if(sd->state.lr_flag != 2)
  2649. sd->matk_rate += val;
  2650. break;
  2651. case SP_IGNORE_DEF_ELE:
  2652. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2653. if(!sd->state.lr_flag)
  2654. sd->right_weapon.ignore_def_ele |= 1<<val;
  2655. else if(sd->state.lr_flag == 1)
  2656. sd->left_weapon.ignore_def_ele |= 1<<val;
  2657. break;
  2658. case SP_IGNORE_DEF_RACE:
  2659. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2660. if(!sd->state.lr_flag)
  2661. sd->right_weapon.ignore_def_race |= 1<<val;
  2662. else if(sd->state.lr_flag == 1)
  2663. sd->left_weapon.ignore_def_race |= 1<<val;
  2664. break;
  2665. case SP_IGNORE_DEF_CLASS:
  2666. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2667. if(!sd->state.lr_flag)
  2668. sd->right_weapon.ignore_def_class |= 1<<val;
  2669. else if(sd->state.lr_flag == 1)
  2670. sd->left_weapon.ignore_def_class |= 1<<val;
  2671. break;
  2672. case SP_ATK_RATE:
  2673. if(sd->state.lr_flag != 2)
  2674. sd->bonus.atk_rate += val;
  2675. break;
  2676. case SP_MAGIC_ATK_DEF:
  2677. if(sd->state.lr_flag != 2)
  2678. sd->bonus.magic_def_rate += val;
  2679. break;
  2680. case SP_MISC_ATK_DEF:
  2681. if(sd->state.lr_flag != 2)
  2682. sd->bonus.misc_def_rate += val;
  2683. break;
  2684. case SP_IGNORE_MDEF_ELE:
  2685. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2686. if(sd->state.lr_flag != 2)
  2687. sd->bonus.ignore_mdef_ele |= 1<<val;
  2688. break;
  2689. case SP_IGNORE_MDEF_RACE:
  2690. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2691. if(sd->state.lr_flag != 2)
  2692. sd->bonus.ignore_mdef_race |= 1<<val;
  2693. break;
  2694. case SP_PERFECT_HIT_RATE:
  2695. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2696. sd->bonus.perfect_hit = val;
  2697. break;
  2698. case SP_PERFECT_HIT_ADD_RATE:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->bonus.perfect_hit_add += val;
  2701. break;
  2702. case SP_CRITICAL_RATE:
  2703. if(sd->state.lr_flag != 2)
  2704. sd->critical_rate+=val;
  2705. break;
  2706. case SP_DEF_RATIO_ATK_ELE:
  2707. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2708. if(!sd->state.lr_flag)
  2709. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2710. else if(sd->state.lr_flag == 1)
  2711. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2712. break;
  2713. case SP_DEF_RATIO_ATK_RACE:
  2714. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2715. if(!sd->state.lr_flag)
  2716. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2717. else if(sd->state.lr_flag == 1)
  2718. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2719. break;
  2720. case SP_DEF_RATIO_ATK_CLASS:
  2721. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2722. if(!sd->state.lr_flag)
  2723. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2724. else if(sd->state.lr_flag == 1)
  2725. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2726. break;
  2727. case SP_HIT_RATE:
  2728. if(sd->state.lr_flag != 2)
  2729. sd->hit_rate += val;
  2730. break;
  2731. case SP_FLEE_RATE:
  2732. if(sd->state.lr_flag != 2)
  2733. sd->flee_rate += val;
  2734. break;
  2735. case SP_FLEE2_RATE:
  2736. if(sd->state.lr_flag != 2)
  2737. sd->flee2_rate += val;
  2738. break;
  2739. case SP_DEF_RATE:
  2740. if(sd->state.lr_flag != 2)
  2741. sd->def_rate += val;
  2742. break;
  2743. case SP_DEF2_RATE:
  2744. if(sd->state.lr_flag != 2)
  2745. sd->def2_rate += val;
  2746. break;
  2747. case SP_MDEF_RATE:
  2748. if(sd->state.lr_flag != 2)
  2749. sd->mdef_rate += val;
  2750. break;
  2751. case SP_MDEF2_RATE:
  2752. if(sd->state.lr_flag != 2)
  2753. sd->mdef2_rate += val;
  2754. break;
  2755. case SP_RESTART_FULL_RECOVER:
  2756. if(sd->state.lr_flag != 2)
  2757. sd->special_state.restart_full_recover = 1;
  2758. break;
  2759. case SP_NO_CASTCANCEL:
  2760. if(sd->state.lr_flag != 2)
  2761. sd->special_state.no_castcancel = 1;
  2762. break;
  2763. case SP_NO_CASTCANCEL2:
  2764. if(sd->state.lr_flag != 2)
  2765. sd->special_state.no_castcancel2 = 1;
  2766. break;
  2767. case SP_NO_SIZEFIX:
  2768. if(sd->state.lr_flag != 2)
  2769. sd->special_state.no_sizefix = 1;
  2770. break;
  2771. case SP_NO_MAGIC_DAMAGE:
  2772. if(sd->state.lr_flag == 2)
  2773. break;
  2774. val+= sd->special_state.no_magic_damage;
  2775. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2776. break;
  2777. case SP_NO_WEAPON_DAMAGE:
  2778. if(sd->state.lr_flag == 2)
  2779. break;
  2780. val+= sd->special_state.no_weapon_damage;
  2781. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2782. break;
  2783. case SP_NO_MISC_DAMAGE:
  2784. if(sd->state.lr_flag == 2)
  2785. break;
  2786. val+= sd->special_state.no_misc_damage;
  2787. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2788. break;
  2789. case SP_NO_GEMSTONE:
  2790. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2791. sd->special_state.no_gemstone = 1;
  2792. break;
  2793. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2794. if(sd->state.lr_flag != 2) {
  2795. sd->special_state.intravision = 1;
  2796. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2797. }
  2798. break;
  2799. case SP_NO_KNOCKBACK:
  2800. if(sd->state.lr_flag != 2)
  2801. sd->special_state.no_knockback = 1;
  2802. break;
  2803. case SP_SPLASH_RANGE:
  2804. if(sd->bonus.splash_range < val)
  2805. sd->bonus.splash_range = val;
  2806. break;
  2807. case SP_SPLASH_ADD_RANGE:
  2808. sd->bonus.splash_add_range += val;
  2809. break;
  2810. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2811. if(sd->state.lr_flag != 2)
  2812. sd->bonus.short_weapon_damage_return += val;
  2813. break;
  2814. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2815. if(sd->state.lr_flag != 2)
  2816. sd->bonus.long_weapon_damage_return += val;
  2817. break;
  2818. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2819. if(sd->state.lr_flag != 2)
  2820. sd->bonus.magic_damage_return += val;
  2821. break;
  2822. case SP_ALL_STATS: // [Valaris]
  2823. if(sd->state.lr_flag!=2) {
  2824. sd->param_bonus[SP_STR-SP_STR]+=val;
  2825. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2826. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2827. sd->param_bonus[SP_INT-SP_STR]+=val;
  2828. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2829. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2830. }
  2831. break;
  2832. case SP_AGI_VIT: // [Valaris]
  2833. if(sd->state.lr_flag!=2) {
  2834. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2835. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2836. }
  2837. break;
  2838. case SP_AGI_DEX_STR: // [Valaris]
  2839. if(sd->state.lr_flag!=2) {
  2840. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2841. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2842. sd->param_bonus[SP_STR-SP_STR]+=val;
  2843. }
  2844. break;
  2845. case SP_PERFECT_HIDE: // [Valaris]
  2846. if(sd->state.lr_flag!=2)
  2847. sd->special_state.perfect_hiding=1;
  2848. break;
  2849. case SP_UNBREAKABLE:
  2850. if(sd->state.lr_flag!=2)
  2851. sd->bonus.unbreakable += val;
  2852. break;
  2853. case SP_UNBREAKABLE_WEAPON:
  2854. if(sd->state.lr_flag != 2)
  2855. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2856. break;
  2857. case SP_UNBREAKABLE_ARMOR:
  2858. if(sd->state.lr_flag != 2)
  2859. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2860. break;
  2861. case SP_UNBREAKABLE_HELM:
  2862. if(sd->state.lr_flag != 2)
  2863. sd->bonus.unbreakable_equip |= EQP_HELM;
  2864. break;
  2865. case SP_UNBREAKABLE_SHIELD:
  2866. if(sd->state.lr_flag != 2)
  2867. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2868. break;
  2869. case SP_UNBREAKABLE_GARMENT:
  2870. if(sd->state.lr_flag != 2)
  2871. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2872. break;
  2873. case SP_UNBREAKABLE_SHOES:
  2874. if(sd->state.lr_flag != 2)
  2875. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2876. break;
  2877. case SP_CLASSCHANGE: // [Valaris]
  2878. if(sd->state.lr_flag !=2)
  2879. sd->bonus.classchange=val;
  2880. break;
  2881. case SP_LONG_ATK_RATE:
  2882. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2883. sd->bonus.long_attack_atk_rate+=val;
  2884. break;
  2885. case SP_BREAK_WEAPON_RATE:
  2886. if(sd->state.lr_flag != 2)
  2887. sd->bonus.break_weapon_rate+=val;
  2888. break;
  2889. case SP_BREAK_ARMOR_RATE:
  2890. if(sd->state.lr_flag != 2)
  2891. sd->bonus.break_armor_rate+=val;
  2892. break;
  2893. case SP_ADD_STEAL_RATE:
  2894. if(sd->state.lr_flag != 2)
  2895. sd->bonus.add_steal_rate+=val;
  2896. break;
  2897. case SP_DELAYRATE:
  2898. if(sd->state.lr_flag != 2)
  2899. sd->delayrate+=val;
  2900. break;
  2901. case SP_CRIT_ATK_RATE:
  2902. if(sd->state.lr_flag != 2)
  2903. sd->bonus.crit_atk_rate += val;
  2904. break;
  2905. case SP_NO_REGEN:
  2906. if(sd->state.lr_flag != 2)
  2907. sd->regen.state.block|=val;
  2908. break;
  2909. case SP_UNSTRIPABLE_WEAPON:
  2910. if(sd->state.lr_flag != 2)
  2911. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2912. break;
  2913. case SP_UNSTRIPABLE:
  2914. case SP_UNSTRIPABLE_ARMOR:
  2915. if(sd->state.lr_flag != 2)
  2916. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2917. break;
  2918. case SP_UNSTRIPABLE_HELM:
  2919. if(sd->state.lr_flag != 2)
  2920. sd->bonus.unstripable_equip |= EQP_HELM;
  2921. break;
  2922. case SP_UNSTRIPABLE_SHIELD:
  2923. if(sd->state.lr_flag != 2)
  2924. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2925. break;
  2926. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2927. if(!sd->state.lr_flag) {
  2928. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2929. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2930. } else if(sd->state.lr_flag == 1) {
  2931. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2932. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2933. }
  2934. break;
  2935. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2936. if(!sd->state.lr_flag) {
  2937. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2938. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2939. } else if(sd->state.lr_flag == 1) {
  2940. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2941. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2942. }
  2943. break;
  2944. case SP_SP_GAIN_VALUE:
  2945. if(!sd->state.lr_flag)
  2946. sd->bonus.sp_gain_value += val;
  2947. break;
  2948. case SP_HP_GAIN_VALUE:
  2949. if(!sd->state.lr_flag)
  2950. sd->bonus.hp_gain_value += val;
  2951. break;
  2952. case SP_LONG_SP_GAIN_VALUE:
  2953. if(!sd->state.lr_flag)
  2954. sd->bonus.long_sp_gain_value += val;
  2955. case SP_LONG_HP_GAIN_VALUE:
  2956. if(!sd->state.lr_flag)
  2957. sd->bonus.long_hp_gain_value += val;
  2958. break;
  2959. case SP_MAGIC_SP_GAIN_VALUE:
  2960. if(!sd->state.lr_flag)
  2961. sd->bonus.magic_sp_gain_value += val;
  2962. break;
  2963. case SP_MAGIC_HP_GAIN_VALUE:
  2964. if(!sd->state.lr_flag)
  2965. sd->bonus.magic_hp_gain_value += val;
  2966. break;
  2967. case SP_ADD_HEAL_RATE:
  2968. if(sd->state.lr_flag != 2)
  2969. sd->bonus.add_heal_rate += val;
  2970. break;
  2971. case SP_ADD_HEAL2_RATE:
  2972. if(sd->state.lr_flag != 2)
  2973. sd->bonus.add_heal2_rate += val;
  2974. break;
  2975. case SP_ADD_ITEM_HEAL_RATE:
  2976. if(sd->state.lr_flag != 2)
  2977. sd->bonus.itemhealrate2 += val;
  2978. break;
  2979. case SP_EMATK:
  2980. if(sd->state.lr_flag != 2)
  2981. sd->bonus.ematk += val;
  2982. break;
  2983. #ifdef RENEWAL_CAST
  2984. case SP_FIXCASTRATE:
  2985. if(sd->state.lr_flag != 2)
  2986. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2987. break;
  2988. case SP_ADD_FIXEDCAST:
  2989. if(sd->state.lr_flag != 2)
  2990. sd->bonus.add_fixcast += val;
  2991. break;
  2992. case SP_CASTRATE:
  2993. case SP_VARCASTRATE:
  2994. if(sd->state.lr_flag != 2)
  2995. sd->bonus.varcastrate -= val;
  2996. break;
  2997. case SP_ADD_VARIABLECAST:
  2998. if(sd->state.lr_flag != 2)
  2999. sd->bonus.add_varcast += val;
  3000. break;
  3001. #else
  3002. case SP_ADD_FIXEDCAST:
  3003. case SP_FIXCASTRATE:
  3004. case SP_ADD_VARIABLECAST:
  3005. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3006. break;
  3007. case SP_VARCASTRATE:
  3008. case SP_CASTRATE:
  3009. if(sd->state.lr_flag != 2)
  3010. sd->castrate += val;
  3011. break;
  3012. #endif
  3013. case SP_ADDMAXWEIGHT:
  3014. if (sd->state.lr_flag != 2)
  3015. sd->add_max_weight += val;
  3016. break;
  3017. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3018. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3019. break;
  3020. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3021. if (sd->state.lr_flag != 2)
  3022. sd->bonus.critical_rangeatk += val*10;
  3023. else
  3024. sd->bonus.arrow_cri += val*10;
  3025. break;
  3026. case SP_WEAPON_ATK_RATE:
  3027. if (sd->state.lr_flag != 2)
  3028. sd->bonus.weapon_atk_rate += val;
  3029. break;
  3030. case SP_WEAPON_MATK_RATE:
  3031. if (sd->state.lr_flag != 2)
  3032. sd->bonus.weapon_matk_rate += val;
  3033. break;
  3034. case SP_NO_MADO_FUEL:
  3035. if (sd->state.lr_flag != 2)
  3036. sd->special_state.no_mado_fuel = 1;
  3037. break;
  3038. case SP_NO_WALK_DELAY:
  3039. if (sd->state.lr_flag != 2)
  3040. sd->special_state.no_walk_delay = 1;
  3041. break;
  3042. default:
  3043. if (running_npc_stat_calc_event) {
  3044. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3045. }
  3046. else if (current_equip_combo_pos > 0) {
  3047. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3048. }
  3049. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3050. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3051. }
  3052. else {
  3053. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3054. }
  3055. break;
  3056. }
  3057. }
  3058. /*==========================================
  3059. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3060. * format: bonus2 bBonusName,type2,val;
  3061. * @param sd
  3062. * @param type Bonus type used by bBonusName
  3063. * @param type2
  3064. * @param val Value that usually for rate or fixed value
  3065. *------------------------------------------*/
  3066. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3067. {
  3068. nullpo_retv(sd);
  3069. switch(type){
  3070. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3071. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3072. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3073. sd->right_weapon.addele[type2]+=val;
  3074. else if(sd->state.lr_flag == 1)
  3075. sd->left_weapon.addele[type2]+=val;
  3076. else if(sd->state.lr_flag == 2)
  3077. sd->arrow_addele[type2]+=val;
  3078. break;
  3079. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3080. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3081. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3082. sd->right_weapon.addrace[type2]+=val;
  3083. else if(sd->state.lr_flag == 1)
  3084. sd->left_weapon.addrace[type2]+=val;
  3085. else if(sd->state.lr_flag == 2)
  3086. sd->arrow_addrace[type2]+=val;
  3087. break;
  3088. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3089. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3090. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3091. sd->right_weapon.addclass[type2]+=val;
  3092. else if(sd->state.lr_flag == 1)
  3093. sd->left_weapon.addclass[type2]+=val;
  3094. else if(sd->state.lr_flag == 2)
  3095. sd->arrow_addclass[type2]+=val;
  3096. break;
  3097. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3098. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3099. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3100. sd->right_weapon.addsize[type2]+=val;
  3101. else if(sd->state.lr_flag == 1)
  3102. sd->left_weapon.addsize[type2]+=val;
  3103. else if(sd->state.lr_flag == 2)
  3104. sd->arrow_addsize[type2]+=val;
  3105. break;
  3106. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3107. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3108. if(sd->state.lr_flag != 2)
  3109. sd->subele_script[type2] += val;
  3110. break;
  3111. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3112. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3113. if(sd->state.lr_flag != 2)
  3114. sd->subrace[type2]+=val;
  3115. break;
  3116. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3117. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3118. if(sd->state.lr_flag != 2)
  3119. sd->subclass[type2]+=val;
  3120. break;
  3121. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3122. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3123. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3124. break;
  3125. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3126. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3127. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  3128. break;
  3129. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3130. if(sd->state.lr_flag == 2)
  3131. break;
  3132. if (sd->reseff.size() == MAX_PC_BONUS) {
  3133. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3134. break;
  3135. }
  3136. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3137. break;
  3138. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3139. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3140. if(sd->state.lr_flag != 2)
  3141. sd->magic_addele_script[type2] += val;
  3142. break;
  3143. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3144. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3145. if(sd->state.lr_flag != 2)
  3146. sd->magic_addrace[type2]+=val;
  3147. break;
  3148. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3149. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3150. if(sd->state.lr_flag != 2)
  3151. sd->magic_addclass[type2]+=val;
  3152. break;
  3153. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3154. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3155. if(sd->state.lr_flag != 2)
  3156. sd->magic_addsize[type2]+=val;
  3157. break;
  3158. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3159. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3160. if(sd->state.lr_flag != 2)
  3161. sd->magic_atk_ele[type2]+=val;
  3162. break;
  3163. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3164. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3165. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3166. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3167. break;
  3168. }
  3169. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3170. }
  3171. break;
  3172. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3173. if(sd->state.lr_flag == 2)
  3174. break;
  3175. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3176. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3177. break;
  3178. }
  3179. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3180. break;
  3181. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3182. if(sd->state.lr_flag == 2)
  3183. break;
  3184. if (sd->add_def.size() == MAX_PC_BONUS) {
  3185. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3186. break;
  3187. }
  3188. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3189. break;
  3190. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3191. if(sd->state.lr_flag == 2)
  3192. break;
  3193. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3194. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3195. break;
  3196. }
  3197. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3198. break;
  3199. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3200. if(!sd->state.lr_flag) {
  3201. sd->right_weapon.hp_drain_rate.rate += type2;
  3202. sd->right_weapon.hp_drain_rate.per += val;
  3203. }
  3204. else if(sd->state.lr_flag == 1) {
  3205. sd->left_weapon.hp_drain_rate.rate += type2;
  3206. sd->left_weapon.hp_drain_rate.per += val;
  3207. }
  3208. break;
  3209. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3210. if(!sd->state.lr_flag) {
  3211. sd->right_weapon.sp_drain_rate.rate += type2;
  3212. sd->right_weapon.sp_drain_rate.per += val;
  3213. }
  3214. else if(sd->state.lr_flag == 1) {
  3215. sd->left_weapon.sp_drain_rate.rate += type2;
  3216. sd->left_weapon.sp_drain_rate.per += val;
  3217. }
  3218. break;
  3219. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3220. if(sd->state.lr_flag != 2) {
  3221. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3222. }
  3223. break;
  3224. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3225. if(sd->state.lr_flag != 2) {
  3226. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3227. }
  3228. break;
  3229. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3230. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3231. sd->bonus.get_zeny_rate = val;
  3232. sd->bonus.get_zeny_num = type2;
  3233. }
  3234. break;
  3235. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3236. if(sd->state.lr_flag != 2) {
  3237. sd->bonus.get_zeny_rate += val;
  3238. sd->bonus.get_zeny_num += type2;
  3239. }
  3240. break;
  3241. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3242. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3243. if(sd->state.lr_flag == 2)
  3244. break;
  3245. sd->weapon_coma_ele[type2] += val;
  3246. sd->special_state.bonus_coma = 1;
  3247. break;
  3248. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3249. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3250. if(sd->state.lr_flag == 2)
  3251. break;
  3252. sd->weapon_coma_race[type2] += val;
  3253. sd->special_state.bonus_coma = 1;
  3254. break;
  3255. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3256. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3257. if(sd->state.lr_flag == 2)
  3258. break;
  3259. sd->weapon_coma_class[type2] += val;
  3260. sd->special_state.bonus_coma = 1;
  3261. break;
  3262. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3263. if(sd->state.lr_flag != 2)
  3264. sd->weapon_atk[type2]+=val;
  3265. break;
  3266. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3267. if(sd->state.lr_flag != 2)
  3268. sd->weapon_damage_rate[type2]+=val;
  3269. break;
  3270. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3271. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3272. if(sd->state.lr_flag != 2)
  3273. sd->critaddrace[type2] += val*10;
  3274. break;
  3275. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3276. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3277. if(sd->state.lr_flag != 2)
  3278. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3279. break;
  3280. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3281. if(sd->state.lr_flag == 2)
  3282. break;
  3283. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3284. ShowWarning("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3285. break;
  3286. }
  3287. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3288. break;
  3289. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3290. if(sd->state.lr_flag == 2)
  3291. break;
  3292. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3293. ShowWarning("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3294. break;
  3295. }
  3296. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3297. break;
  3298. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3299. if(sd->state.lr_flag == 2)
  3300. break;
  3301. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3302. ShowWarning("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3303. break;
  3304. }
  3305. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3306. break;
  3307. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3308. if(sd->state.lr_flag == 2)
  3309. break;
  3310. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3311. ShowWarning("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3312. break;
  3313. }
  3314. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3315. break;
  3316. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3317. if(sd->state.lr_flag != 2) {
  3318. sd->hp_loss.value = type2;
  3319. sd->hp_loss.rate = val;
  3320. }
  3321. break;
  3322. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3323. if(sd->state.lr_flag != 2) {
  3324. sd->hp_regen.value = type2;
  3325. sd->hp_regen.rate = val;
  3326. }
  3327. break;
  3328. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3329. if (sd->state.lr_flag != 2) {
  3330. sd->percent_hp_regen.value = type2;
  3331. sd->percent_hp_regen.rate = val;
  3332. }
  3333. break;
  3334. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3335. if (sd->state.lr_flag != 2) {
  3336. sd->percent_sp_regen.value = type2;
  3337. sd->percent_sp_regen.rate = val;
  3338. }
  3339. break;
  3340. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3341. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3342. if(sd->state.lr_flag != 2)
  3343. sd->right_weapon.addrace2[type2] += val;
  3344. else
  3345. sd->left_weapon.addrace2[type2] += val;
  3346. break;
  3347. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3348. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3349. if(sd->state.lr_flag != 2)
  3350. sd->subsize[type2]+=val;
  3351. break;
  3352. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3353. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3354. if(sd->state.lr_flag != 2)
  3355. sd->subrace2[type2]+=val;
  3356. break;
  3357. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3358. if(sd->state.lr_flag == 2)
  3359. break;
  3360. if (!itemdb_exists(type2)) {
  3361. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3362. break;
  3363. }
  3364. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3365. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3366. break;
  3367. }
  3368. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3369. break;
  3370. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3371. if (sd->state.lr_flag == 2)
  3372. break;
  3373. if (!type2 || !itemdb_group_exists(type2)) {
  3374. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3375. break;
  3376. }
  3377. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3378. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3379. break;
  3380. }
  3381. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3382. break;
  3383. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3384. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3385. if(sd->state.lr_flag != 2)
  3386. sd->expaddrace[type2]+=val;
  3387. break;
  3388. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3389. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3390. if(sd->state.lr_flag != 2)
  3391. sd->expaddclass[type2]+=val;
  3392. break;
  3393. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3394. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3395. if(sd->state.lr_flag != 2)
  3396. sd->sp_gain_race[type2]+=val;
  3397. break;
  3398. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3399. if (sd->state.lr_flag != 2)
  3400. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3401. break;
  3402. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3403. if (sd->state.lr_flag != 2)
  3404. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3405. break;
  3406. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3407. if(sd->state.lr_flag != 2) {
  3408. sd->sp_loss.value = type2;
  3409. sd->sp_loss.rate = val;
  3410. }
  3411. break;
  3412. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3413. if(sd->state.lr_flag != 2) {
  3414. sd->sp_regen.value = type2;
  3415. sd->sp_regen.rate = val;
  3416. }
  3417. break;
  3418. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3419. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3420. if(!sd->state.lr_flag) {
  3421. sd->right_weapon.hp_drain_race[type2] += val;
  3422. }
  3423. else if(sd->state.lr_flag == 1) {
  3424. sd->left_weapon.hp_drain_race[type2] += val;
  3425. }
  3426. break;
  3427. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3428. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3429. if(!sd->state.lr_flag) {
  3430. sd->right_weapon.sp_drain_race[type2] += val;
  3431. }
  3432. else if(sd->state.lr_flag == 1) {
  3433. sd->left_weapon.sp_drain_race[type2] += val;
  3434. }
  3435. break;
  3436. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3437. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3438. if(!sd->state.lr_flag) {
  3439. sd->right_weapon.hp_drain_class[type2] += val;
  3440. }
  3441. else if(sd->state.lr_flag == 1) {
  3442. sd->left_weapon.hp_drain_class[type2] += val;
  3443. }
  3444. break;
  3445. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3446. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3447. if(!sd->state.lr_flag) {
  3448. sd->right_weapon.sp_drain_class[type2] += val;
  3449. }
  3450. else if(sd->state.lr_flag == 1) {
  3451. sd->left_weapon.sp_drain_class[type2] += val;
  3452. }
  3453. break;
  3454. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3455. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3456. if(sd->state.lr_flag != 2)
  3457. sd->ignore_mdef_by_race[type2] += val;
  3458. break;
  3459. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3460. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3461. if(sd->state.lr_flag != 2)
  3462. sd->ignore_mdef_by_class[type2] += val;
  3463. break;
  3464. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3465. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3466. if(sd->state.lr_flag != 2)
  3467. sd->ignore_def_by_race[type2] += val;
  3468. break;
  3469. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3470. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3471. if (sd->state.lr_flag != 2)
  3472. sd->ignore_def_by_class[type2] += val;
  3473. break;
  3474. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3475. if(sd->state.lr_flag == 2)
  3476. break;
  3477. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3478. ShowWarning("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3479. break;
  3480. }
  3481. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3482. break;
  3483. case SP_SKILL_DELAY:
  3484. if(sd->state.lr_flag == 2)
  3485. break;
  3486. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3487. ShowWarning("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3488. break;
  3489. }
  3490. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3491. break;
  3492. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3493. if(sd->state.lr_flag == 2)
  3494. break;
  3495. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3496. ShowWarning("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3497. break;
  3498. }
  3499. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3500. break;
  3501. #ifdef RENEWAL_CAST
  3502. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3503. if(sd->state.lr_flag == 2)
  3504. break;
  3505. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3506. ShowWarning("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3507. break;
  3508. }
  3509. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3510. break;
  3511. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3512. if(sd->state.lr_flag == 2)
  3513. break;
  3514. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3515. ShowWarning("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3516. break;
  3517. }
  3518. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3519. break;
  3520. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3521. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3522. if(sd->state.lr_flag == 2)
  3523. break;
  3524. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3525. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3526. break;
  3527. }
  3528. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3529. break;
  3530. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3531. if(sd->state.lr_flag == 2)
  3532. break;
  3533. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3534. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3535. break;
  3536. }
  3537. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3538. break;
  3539. #else
  3540. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3541. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3542. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3543. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3544. break;
  3545. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3546. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3547. if(sd->state.lr_flag == 2)
  3548. break;
  3549. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3550. ShowWarning("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", MAX_PC_BONUS, type2, val);
  3551. break;
  3552. }
  3553. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3554. break;
  3555. #endif
  3556. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3557. if(sd->state.lr_flag == 2)
  3558. break;
  3559. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3560. ShowWarning("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3561. break;
  3562. }
  3563. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3564. break;
  3565. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3566. if (sd->subskill.size() == MAX_PC_BONUS) {
  3567. ShowWarning("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3568. break;
  3569. }
  3570. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3571. break;
  3572. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3573. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3574. sd->subdefele[type2] += val;
  3575. break;
  3576. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3577. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3578. sd->coma_class[type2] += val;
  3579. sd->special_state.bonus_coma = 1;
  3580. break;
  3581. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3582. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3583. sd->coma_race[type2] += val;
  3584. sd->special_state.bonus_coma = 1;
  3585. break;
  3586. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3587. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3588. if(sd->state.lr_flag != 2)
  3589. sd->magic_addrace2[type2] += val;
  3590. break;
  3591. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3592. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3593. if (sd->state.lr_flag != 2)
  3594. sd->ignore_mdef_by_race2[type2] += val;
  3595. break;
  3596. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3597. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3598. if (sd->state.lr_flag != 2)
  3599. sd->dropaddrace[type2] += val;
  3600. break;
  3601. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3602. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3603. if (sd->state.lr_flag != 2)
  3604. sd->dropaddclass[type2] += val;
  3605. break;
  3606. default:
  3607. if (running_npc_stat_calc_event) {
  3608. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3609. }
  3610. else if (current_equip_combo_pos > 0) {
  3611. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3612. }
  3613. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3614. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3615. }
  3616. else {
  3617. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3618. }
  3619. break;
  3620. }
  3621. }
  3622. /**
  3623. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3624. * @param sd
  3625. * @param type Bonus type used by bBonusName
  3626. * @param type2
  3627. * @param type3
  3628. * @param val Value that usually for rate or fixed value
  3629. */
  3630. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3631. {
  3632. nullpo_retv(sd);
  3633. switch(type){
  3634. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3635. if(sd->state.lr_flag != 2)
  3636. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3637. break;
  3638. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3639. if(sd->state.lr_flag != 2)
  3640. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3641. break;
  3642. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3643. if(sd->state.lr_flag != 2)
  3644. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3645. break;
  3646. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3647. if(sd->state.lr_flag != 2)
  3648. {
  3649. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3650. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3651. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3652. }
  3653. break;
  3654. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3655. if(sd->state.lr_flag != 2)
  3656. {
  3657. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3658. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3659. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3660. }
  3661. break;
  3662. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3663. if (sd->state.lr_flag != 2)
  3664. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3665. break;
  3666. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3667. if (sd->state.lr_flag != 2)
  3668. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3669. break;
  3670. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3671. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3672. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3673. break;
  3674. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3675. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3676. if(sd->state.lr_flag != 2)
  3677. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3678. break;
  3679. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3680. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3681. if( sd->state.lr_flag != 2 )
  3682. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3683. break;
  3684. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3685. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3686. if (sd->state.lr_flag != 2)
  3687. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3688. break;
  3689. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3690. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3691. if (sd->state.lr_flag != 2)
  3692. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3693. break;
  3694. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3695. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3696. if(sd->state.lr_flag != 2) {
  3697. sd->sp_vanish_race[type2].rate += type3;
  3698. sd->sp_vanish_race[type2].per += val;
  3699. }
  3700. break;
  3701. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3702. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3703. if(sd->state.lr_flag != 2) {
  3704. sd->hp_vanish_race[type2].rate += type3;
  3705. sd->hp_vanish_race[type2].per += val;
  3706. }
  3707. break;
  3708. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3709. if(sd->state.lr_flag != 2) {
  3710. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3711. }
  3712. break;
  3713. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3714. if(sd->state.lr_flag != 2) {
  3715. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3716. }
  3717. break;
  3718. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3719. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3720. if (sd->state.lr_flag == 2)
  3721. break;
  3722. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3723. //if (type3 > sd->norecover_state_race[type2].rate) {
  3724. // sd->norecover_state_race[type2].rate = type3;
  3725. // sd->norecover_state_race[type2].tick = val;
  3726. // break;
  3727. //}
  3728. sd->norecover_state_race[type2].rate = type3;
  3729. sd->norecover_state_race[type2].tick = val;
  3730. break;
  3731. default:
  3732. if (running_npc_stat_calc_event) {
  3733. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3734. }
  3735. else if (current_equip_combo_pos > 0) {
  3736. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3737. }
  3738. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3739. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3740. }
  3741. else {
  3742. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3743. }
  3744. break;
  3745. }
  3746. }
  3747. /**
  3748. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3749. * @param sd
  3750. * @param type Bonus type used by bBonusName
  3751. * @param type2
  3752. * @param type3
  3753. * @param type4
  3754. * @param val Value that usually for rate or fixed value
  3755. */
  3756. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3757. {
  3758. nullpo_retv(sd);
  3759. switch(type){
  3760. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3761. if(sd->state.lr_flag != 2)
  3762. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3763. break;
  3764. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3765. if(sd->state.lr_flag != 2)
  3766. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3767. break;
  3768. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3769. if(sd->state.lr_flag != 2)
  3770. {
  3771. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3772. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3773. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3774. }
  3775. break;
  3776. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3777. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3778. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3779. break;
  3780. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3781. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3782. if (sd->state.lr_flag != 2)
  3783. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3784. break;
  3785. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3786. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3787. if( sd->state.lr_flag != 2 )
  3788. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3789. break;
  3790. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3791. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3792. if(sd->state.lr_flag == 2)
  3793. break;
  3794. sd->def_set_race[type2].rate = type3;
  3795. sd->def_set_race[type2].tick = type4;
  3796. sd->def_set_race[type2].value = val;
  3797. break;
  3798. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3799. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3800. if(sd->state.lr_flag == 2)
  3801. break;
  3802. sd->mdef_set_race[type2].rate = type3;
  3803. sd->mdef_set_race[type2].tick = type4;
  3804. sd->mdef_set_race[type2].value = val;
  3805. break;
  3806. default:
  3807. if (running_npc_stat_calc_event) {
  3808. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3809. }
  3810. else if (current_equip_combo_pos > 0) {
  3811. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3812. }
  3813. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3814. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3815. }
  3816. else {
  3817. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3818. }
  3819. break;
  3820. }
  3821. }
  3822. /**
  3823. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3824. * @param sd
  3825. * @param type Bonus type used by bBonusName
  3826. * @param type2
  3827. * @param type3
  3828. * @param type4
  3829. * @param val Value that usually for rate or fixed value
  3830. */
  3831. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3832. {
  3833. nullpo_retv(sd);
  3834. switch(type){
  3835. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3836. if(sd->state.lr_flag != 2)
  3837. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3838. break;
  3839. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3840. if(sd->state.lr_flag != 2)
  3841. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3842. break;
  3843. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3844. if(sd->state.lr_flag != 2)
  3845. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3846. break;
  3847. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3848. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3849. if( sd->state.lr_flag != 2 )
  3850. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3851. break;
  3852. default:
  3853. if (running_npc_stat_calc_event) {
  3854. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3855. }
  3856. else if (current_equip_combo_pos > 0) {
  3857. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3858. }
  3859. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3860. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3861. }
  3862. else {
  3863. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3864. }
  3865. break;
  3866. }
  3867. }
  3868. /*==========================================
  3869. * Grants a player a given skill. Flag values are:
  3870. * 0 - Grant permanent skill to be bound to skill tree
  3871. * 1 - Grant an item skill (temporary)
  3872. * 2 - Like 1, except the level granted can stack with previously learned level.
  3873. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3874. *------------------------------------------*/
  3875. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3876. uint16 idx = 0;
  3877. nullpo_ret(sd);
  3878. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3879. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3880. return false;
  3881. }
  3882. if (level > MAX_SKILL_LEVEL) {
  3883. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3884. return false;
  3885. }
  3886. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3887. ShowWarning("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d. Set to max level.\n", level, MAX_SKILL_LEVEL, sd->status.skill[idx].lv);
  3888. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3889. }
  3890. switch (type) {
  3891. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3892. sd->status.skill[idx].id = skill_id;
  3893. sd->status.skill[idx].lv = level;
  3894. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3895. if (level == 0) { //Remove skill.
  3896. sd->status.skill[idx].id = 0;
  3897. clif_deleteskill(sd,skill_id);
  3898. } else
  3899. clif_addskill(sd,skill_id);
  3900. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3901. status_calc_pc(sd, SCO_NONE);
  3902. break;
  3903. case ADDSKILL_TEMP: //Item bonus skill.
  3904. if (sd->status.skill[idx].id != 0) {
  3905. if (sd->status.skill[idx].lv >= level)
  3906. return true;
  3907. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3908. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3909. } else {
  3910. sd->status.skill[idx].id = skill_id;
  3911. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3912. }
  3913. sd->status.skill[idx].lv = level;
  3914. break;
  3915. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3916. if (sd->status.skill[idx].id != 0) {
  3917. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3918. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3919. } else {
  3920. sd->status.skill[idx].id = skill_id;
  3921. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3922. }
  3923. sd->status.skill[idx].lv += level;
  3924. break;
  3925. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3926. sd->status.skill[idx].id = skill_id;
  3927. sd->status.skill[idx].lv = level;
  3928. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3929. if (level == 0) { //Remove skill.
  3930. sd->status.skill[idx].id = 0;
  3931. clif_deleteskill(sd,skill_id);
  3932. } else
  3933. clif_addskill(sd,skill_id);
  3934. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3935. status_calc_pc(sd, SCO_NONE);
  3936. break;
  3937. default:
  3938. return false;
  3939. }
  3940. return true;
  3941. }
  3942. /*==========================================
  3943. * Append a card to an item ?
  3944. *------------------------------------------*/
  3945. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3946. {
  3947. int i;
  3948. unsigned short nameid;
  3949. nullpo_ret(sd);
  3950. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3951. return 0; //Invalid item index.
  3952. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3953. return 0; //Invalid card index.
  3954. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3955. return 0; // target item missing
  3956. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3957. return 0; // target card missing
  3958. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3959. return 0; // only weapons and armor are allowed
  3960. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3961. return 0; // must be a card
  3962. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3963. return 0; // target must be identified
  3964. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3965. return 0; // card slots reserved for other purposes
  3966. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3967. return 0; // card cannot be compounded on this item type
  3968. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3969. return 0; // attempted to place shield card on left-hand weapon.
  3970. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3971. return 0; // item must be unequipped
  3972. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3973. if( i == sd->inventory_data[idx_equip]->slot )
  3974. return 0; // no free slots
  3975. // remember the card id to insert
  3976. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3977. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3978. {// failed
  3979. clif_insert_card(sd,idx_equip,idx_card,1);
  3980. }
  3981. else
  3982. {// success
  3983. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3984. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3985. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3986. clif_insert_card(sd,idx_equip,idx_card,0);
  3987. }
  3988. return 0;
  3989. }
  3990. /**
  3991. * Returns the count of unidentified items with the option to identify too.
  3992. * @param sd: Player data
  3993. * @param identify_item: Whether or not to identify any unidentified items
  3994. * @return Unidentified items count
  3995. */
  3996. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3997. {
  3998. int unidentified_count = 0;
  3999. for (int i = 0; i < MAX_INVENTORY; i++) {
  4000. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4001. if (identify_item == true) {
  4002. sd->inventory.u.items_inventory[i].identify = 1;
  4003. clif_item_identified(sd,i,0);
  4004. }
  4005. unidentified_count++;
  4006. }
  4007. }
  4008. return unidentified_count;
  4009. }
  4010. //
  4011. // Items
  4012. //
  4013. /*==========================================
  4014. * Update buying value by skills
  4015. *------------------------------------------*/
  4016. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4017. {
  4018. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4019. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4020. rate1 = 5+skill*2-((skill==10)? 1:0);
  4021. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4022. rate2 = 5+skill*4;
  4023. if(rate1 < rate2) rate1 = rate2;
  4024. if(rate1)
  4025. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4026. if(val < battle_config.min_shop_buy)
  4027. val = battle_config.min_shop_buy;
  4028. return val;
  4029. }
  4030. /*==========================================
  4031. * Update selling value by skills
  4032. *------------------------------------------*/
  4033. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4034. {
  4035. int skill,val = orig_value,rate = 0;
  4036. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4037. rate = 5+skill*2-((skill==10)? 1:0);
  4038. if(rate)
  4039. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4040. if (val < battle_config.min_shop_sell)
  4041. val = battle_config.min_shop_sell;
  4042. return val;
  4043. }
  4044. /*==========================================
  4045. * Checking if we have enough place on inventory for new item
  4046. * Make sure to take 30k as limit (for client I guess)
  4047. * @param sd
  4048. * @param nameid
  4049. * @param amount
  4050. * @return e_chkitem_result
  4051. *------------------------------------------*/
  4052. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4053. {
  4054. int i;
  4055. struct item_data* data;
  4056. nullpo_ret(sd);
  4057. if(amount > MAX_AMOUNT)
  4058. return CHKADDITEM_OVERAMOUNT;
  4059. data = itemdb_search(nameid);
  4060. if(!itemdb_isstackable2(data))
  4061. return CHKADDITEM_NEW;
  4062. if( data->stack.inventory && amount > data->stack.amount )
  4063. return CHKADDITEM_OVERAMOUNT;
  4064. if (data->flag.guid)
  4065. return CHKADDITEM_NEW;
  4066. for(i=0;i<MAX_INVENTORY;i++){
  4067. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4068. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4069. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4070. return CHKADDITEM_OVERAMOUNT;
  4071. return CHKADDITEM_EXIST;
  4072. }
  4073. }
  4074. return CHKADDITEM_NEW;
  4075. }
  4076. /*==========================================
  4077. * Return number of available place in inventory
  4078. * Each non stackable item will reduce place by 1
  4079. * @param sd
  4080. * @return Number of empty slots
  4081. *------------------------------------------*/
  4082. uint8 pc_inventoryblank(struct map_session_data *sd)
  4083. {
  4084. uint8 i, b;
  4085. nullpo_ret(sd);
  4086. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4087. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4088. b++;
  4089. }
  4090. return b;
  4091. }
  4092. /**
  4093. * Attempts to remove zeny from player
  4094. * @param sd: Player
  4095. * @param zeny: Zeny removed
  4096. * @param type: Log type
  4097. * @param tsd: (optional) From who to log (if null take sd)
  4098. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4099. */
  4100. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4101. {
  4102. nullpo_retr(2,sd);
  4103. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4104. if( zeny < 0 )
  4105. {
  4106. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4107. return 1;
  4108. }
  4109. if( sd->status.zeny < zeny )
  4110. return 1; //Not enough.
  4111. sd->status.zeny -= zeny;
  4112. clif_updatestatus(sd,SP_ZENY);
  4113. if(!tsd) tsd = sd;
  4114. log_zeny(sd, type, tsd, -zeny);
  4115. if( zeny > 0 && sd->state.showzeny ) {
  4116. char output[255];
  4117. sprintf(output, "Removed %dz.", zeny);
  4118. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4119. }
  4120. return 0;
  4121. }
  4122. /**
  4123. * Attempts to give zeny to player
  4124. * @param sd: Player
  4125. * @param type: Log type
  4126. * @param tsd: (optional) From who to log (if null take sd)
  4127. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4128. */
  4129. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4130. {
  4131. nullpo_retr(-1,sd);
  4132. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4133. if( zeny < 0 )
  4134. {
  4135. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4136. return 1;
  4137. }
  4138. if( zeny > MAX_ZENY - sd->status.zeny )
  4139. zeny = MAX_ZENY - sd->status.zeny;
  4140. sd->status.zeny += zeny;
  4141. clif_updatestatus(sd,SP_ZENY);
  4142. if(!tsd) tsd = sd;
  4143. log_zeny(sd, type, tsd, zeny);
  4144. if( zeny > 0 && sd->state.showzeny ) {
  4145. char output[255];
  4146. sprintf(output, "Gained %dz.", zeny);
  4147. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4148. }
  4149. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4150. return 0;
  4151. }
  4152. /**
  4153. * Attempts to remove Cash Points from player
  4154. * @param sd: Player
  4155. * @param price: Total points (cash + kafra) the player has to pay
  4156. * @param points: Kafra points the player has to pay
  4157. * @param type: Log type
  4158. * @return -1: Not enough points, otherwise success (cash+points)
  4159. */
  4160. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4161. {
  4162. int cash;
  4163. nullpo_retr(-1,sd);
  4164. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4165. cash = price-points;
  4166. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4167. {
  4168. 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);
  4169. return -1;
  4170. }
  4171. if( cash ){
  4172. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4173. sd->cashPoints -= cash;
  4174. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4175. }
  4176. if( points ){
  4177. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4178. sd->kafraPoints -= points;
  4179. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4180. }
  4181. if( battle_config.cashshop_show_points )
  4182. {
  4183. char output[CHAT_SIZE_MAX];
  4184. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints); // Used %d kafra points and %d cash points. %d kafra and %d cash points remaining.
  4185. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4186. }
  4187. return cash+points;
  4188. }
  4189. /**
  4190. * Attempts to give Cash Points to player
  4191. * @param sd: Player
  4192. * @param cash: Cash points the player gets
  4193. * @param points: Kafra points the player gets
  4194. * @param type: Log type
  4195. * @return -1: Error, otherwise success (cash or points)
  4196. */
  4197. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4198. {
  4199. char output[CHAT_SIZE_MAX];
  4200. nullpo_retr(-1,sd);
  4201. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4202. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4203. if( cash > 0 )
  4204. {
  4205. if( cash > MAX_ZENY-sd->cashPoints )
  4206. {
  4207. 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);
  4208. cash = MAX_ZENY-sd->cashPoints;
  4209. }
  4210. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4211. sd->cashPoints += cash;
  4212. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4213. if( battle_config.cashshop_show_points )
  4214. {
  4215. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4216. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4217. }
  4218. return cash;
  4219. }
  4220. if( points > 0 )
  4221. {
  4222. if( points > MAX_ZENY-sd->kafraPoints )
  4223. {
  4224. 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);
  4225. points = MAX_ZENY-sd->kafraPoints;
  4226. }
  4227. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4228. sd->kafraPoints += points;
  4229. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4230. if( battle_config.cashshop_show_points )
  4231. {
  4232. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4233. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4234. }
  4235. return points;
  4236. }
  4237. return -1; //shouldn't happen but just in case
  4238. }
  4239. /**
  4240. * Searching a specified itemid in inventory and return his stored index
  4241. * @param sd Player
  4242. * @param nameid Find this Item!
  4243. * @return Stored index in inventory, or -1 if not found.
  4244. **/
  4245. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4246. short i;
  4247. nullpo_retr(-1, sd);
  4248. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4249. return ( i < MAX_INVENTORY ) ? i : -1;
  4250. }
  4251. /** Attempt to add a new item to player inventory
  4252. * @param sd
  4253. * @param item_data
  4254. * @param amount
  4255. * @param log_type
  4256. * @return
  4257. * 0 = success
  4258. * 1 = invalid itemid not found or negative amount
  4259. * 2 = overweight
  4260. * 3 = ?
  4261. * 4 = no free place found
  4262. * 5 = max amount reached
  4263. * 6 = ?
  4264. * 7 = stack limitation
  4265. */
  4266. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4267. struct item_data *id;
  4268. int16 i;
  4269. unsigned int w;
  4270. nullpo_retr(ADDITEM_INVALID, sd);
  4271. nullpo_retr(ADDITEM_INVALID, item);
  4272. if( item->nameid == 0 || amount <= 0 )
  4273. return ADDITEM_INVALID;
  4274. if( amount > MAX_AMOUNT )
  4275. return ADDITEM_OVERAMOUNT;
  4276. id = itemdb_search(item->nameid);
  4277. if( id->stack.inventory && amount > id->stack.amount )
  4278. {// item stack limitation
  4279. return ADDITEM_STACKLIMIT;
  4280. }
  4281. w = id->weight*amount;
  4282. if(sd->weight + w > sd->max_weight)
  4283. return ADDITEM_OVERWEIGHT;
  4284. i = MAX_INVENTORY;
  4285. if (id->flag.guid && !item->unique_id)
  4286. item->unique_id = pc_generate_unique_id(sd);
  4287. // Stackable | Non Rental
  4288. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4289. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4290. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4291. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4292. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4293. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4294. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4295. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4296. return ADDITEM_OVERAMOUNT;
  4297. sd->inventory.u.items_inventory[i].amount += amount;
  4298. clif_additem(sd,i,amount,0);
  4299. break;
  4300. }
  4301. }
  4302. }
  4303. if (i >= MAX_INVENTORY) {
  4304. i = pc_search_inventory(sd,0);
  4305. if( i < 0 )
  4306. return ADDITEM_OVERITEM;
  4307. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4308. // clear equip and favorite fields first, just in case
  4309. if( item->equip )
  4310. sd->inventory.u.items_inventory[i].equip = 0;
  4311. if( item->favorite )
  4312. sd->inventory.u.items_inventory[i].favorite = 0;
  4313. if( item->equipSwitch )
  4314. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4315. sd->inventory.u.items_inventory[i].amount = amount;
  4316. sd->inventory_data[i] = id;
  4317. sd->last_addeditem_index = i;
  4318. if (!itemdb_isstackable2(id) || id->flag.guid)
  4319. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4320. clif_additem(sd,i,amount,0);
  4321. }
  4322. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4323. sd->weight += w;
  4324. clif_updatestatus(sd,SP_WEIGHT);
  4325. //Auto-equip
  4326. if(id->flag.autoequip)
  4327. pc_equipitem(sd, i, id->equip);
  4328. /* rental item check */
  4329. if( item->expire_time ) {
  4330. if( time(NULL) > item->expire_time ) {
  4331. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4332. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4333. } else {
  4334. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4335. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4336. pc_inventory_rental_add(sd, seconds);
  4337. }
  4338. }
  4339. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4340. pc_show_questinfo(sd);
  4341. return ADDITEM_SUCCESS;
  4342. }
  4343. /*==========================================
  4344. * Remove an item at index n from inventory by amount.
  4345. * @param sd
  4346. * @param n Item index in inventory
  4347. * @param amount
  4348. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4349. * @param reason Delete reason
  4350. * @param log_type e_log_pick_type
  4351. * @return 1 - invalid itemid or negative amount; 0 - Success
  4352. *------------------------------------------*/
  4353. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4354. {
  4355. nullpo_retr(1, sd);
  4356. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == NULL)
  4357. return 1;
  4358. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4359. sd->inventory.u.items_inventory[n].amount -= amount;
  4360. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4361. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4362. if(sd->inventory.u.items_inventory[n].equip)
  4363. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4364. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4365. sd->inventory_data[n] = NULL;
  4366. }
  4367. if(!(type&1))
  4368. clif_delitem(sd,n,amount,reason);
  4369. if(!(type&2))
  4370. clif_updatestatus(sd,SP_WEIGHT);
  4371. pc_show_questinfo(sd);
  4372. return 0;
  4373. }
  4374. /*==========================================
  4375. * Attempt to drop an item.
  4376. * @param sd
  4377. * @param n Item index in inventory
  4378. * @param amount Amount of item
  4379. * @return False = fail; True = success
  4380. *------------------------------------------*/
  4381. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4382. {
  4383. nullpo_retr(1, sd);
  4384. if(n < 0 || n >= MAX_INVENTORY)
  4385. return false;
  4386. if(amount <= 0)
  4387. return false;
  4388. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4389. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4390. sd->inventory.u.items_inventory[n].amount < amount ||
  4391. sd->state.trading || sd->state.vending ||
  4392. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4393. )
  4394. return false;
  4395. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4396. return false;
  4397. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4398. {
  4399. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4400. return false; //Can't drop items in nodrop mapflag maps.
  4401. }
  4402. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4403. {
  4404. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4405. return false;
  4406. }
  4407. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4408. return false;
  4409. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4410. clif_dropitem(sd, n, amount);
  4411. return true;
  4412. }
  4413. /*==========================================
  4414. * Attempt to pick up an item.
  4415. * @param sd
  4416. * @param fitem Item that will be picked
  4417. * @return False = fail; True = success
  4418. *------------------------------------------*/
  4419. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4420. {
  4421. int flag = 0;
  4422. t_tick tick = gettick();
  4423. struct party_data *p = NULL;
  4424. nullpo_ret(sd);
  4425. nullpo_ret(fitem);
  4426. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4427. return false; // Distance is too far
  4428. if (sd->sc.cant.pickup)
  4429. return false;
  4430. if (sd->status.party_id)
  4431. p = party_search(sd->status.party_id);
  4432. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4433. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4434. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4435. if (!(p && p->party.item&1 &&
  4436. first_sd && first_sd->status.party_id == sd->status.party_id
  4437. ))
  4438. return false;
  4439. }
  4440. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4441. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4442. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4443. if (!(p && p->party.item&1 &&
  4444. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4445. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4446. ))
  4447. return false;
  4448. }
  4449. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4450. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4451. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4452. if(!(p && p->party.item&1 &&
  4453. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4454. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4455. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4456. ))
  4457. return false;
  4458. }
  4459. }
  4460. }
  4461. }
  4462. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4463. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4464. clif_additem(sd,0,0,flag);
  4465. return true;
  4466. }
  4467. //Display pickup animation.
  4468. pc_stop_attack(sd);
  4469. clif_takeitem(&sd->bl,&fitem->bl);
  4470. if (fitem->mob_id &&
  4471. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4472. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4473. )
  4474. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4475. map_clearflooritem(&fitem->bl);
  4476. return true;
  4477. }
  4478. /*==========================================
  4479. * Check if item is usable.
  4480. * Return:
  4481. * false = no
  4482. * true = yes
  4483. *------------------------------------------*/
  4484. bool pc_isUseitem(struct map_session_data *sd,int n)
  4485. {
  4486. struct item_data *item;
  4487. unsigned short nameid;
  4488. nullpo_ret(sd);
  4489. item = sd->inventory_data[n];
  4490. nameid = sd->inventory.u.items_inventory[n].nameid;
  4491. if( item == NULL )
  4492. return false;
  4493. //Not consumable item
  4494. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4495. return false;
  4496. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4497. return true;
  4498. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4499. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4500. return false;
  4501. //Prevent mass item usage. [Skotlex]
  4502. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4503. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4504. )
  4505. return false;
  4506. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4507. clif_msg(sd,ITEM_NOUSE_SITTING);
  4508. return false; // You cannot use this item while sitting.
  4509. }
  4510. if (sd->state.storage_flag && item->type != IT_CASH) {
  4511. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4512. return false; // You cannot use this item while storage is open.
  4513. }
  4514. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4515. return false;
  4516. switch( nameid ) {
  4517. case ITEMID_WING_OF_FLY:
  4518. case ITEMID_GIANT_FLY_WING:
  4519. case ITEMID_N_FLY_WING:
  4520. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4521. clif_skill_teleportmessage(sd,0);
  4522. return false;
  4523. }
  4524. if (nameid == ITEMID_GIANT_FLY_WING) {
  4525. struct party_data *pd = party_search(sd->status.party_id);
  4526. if (pd) {
  4527. int i;
  4528. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4529. if (i == MAX_PARTY) { // User is not party leader
  4530. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4531. break;
  4532. }
  4533. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd && pd->data[i].sd != sd && pd->data[i].sd->bl.m == sd->bl.m && !pc_isdead(pd->data[i].sd));
  4534. if (i == MAX_PARTY) { // No party members found on same map
  4535. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4536. break;
  4537. }
  4538. } else {
  4539. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4540. break;
  4541. }
  4542. }
  4543. // Fall through
  4544. case ITEMID_WING_OF_BUTTERFLY:
  4545. case ITEMID_N_BUTTERFLY_WING:
  4546. case ITEMID_DUN_TELE_SCROLL1:
  4547. case ITEMID_DUN_TELE_SCROLL2:
  4548. case ITEMID_DUN_TELE_SCROLL3:
  4549. case ITEMID_WOB_RUNE:
  4550. case ITEMID_WOB_SCHWALTZ:
  4551. case ITEMID_WOB_RACHEL:
  4552. case ITEMID_WOB_LOCAL:
  4553. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4554. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4555. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4556. return false;
  4557. }
  4558. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4559. return false;
  4560. break;
  4561. case ITEMID_MERCENARY_RED_POTION:
  4562. case ITEMID_MERCENARY_BLUE_POTION:
  4563. case ITEMID_M_CENTER_POTION:
  4564. case ITEMID_M_AWAKENING_POTION:
  4565. case ITEMID_M_BERSERK_POTION:
  4566. if( sd->md == NULL || sd->md->db == NULL )
  4567. return false;
  4568. if( sd->md->sc.data[SC_BERSERK] )
  4569. return false;
  4570. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4571. return false;
  4572. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4573. return false;
  4574. break;
  4575. case ITEMID_NEURALIZER:
  4576. if( !mapdata->flag[MF_RESET] )
  4577. return false;
  4578. break;
  4579. }
  4580. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4581. return false; // Mercenary Scrolls
  4582. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4583. if( pc_is90overweight(sd) ) {
  4584. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4585. return false;
  4586. }
  4587. if( !pc_inventoryblank(sd) ) {
  4588. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4589. return false;
  4590. }
  4591. }
  4592. //Gender check
  4593. if(item->sex != 2 && sd->status.sex != item->sex)
  4594. return false;
  4595. //Required level check
  4596. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4597. return false;
  4598. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4599. return false;
  4600. //Not equipable by class. [Skotlex]
  4601. if (!(
  4602. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4603. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4604. ))
  4605. return false;
  4606. if (sd->sc.count && (
  4607. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4608. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4609. sd->sc.data[SC_TRICKDEAD] ||
  4610. sd->sc.data[SC_HIDING] ||
  4611. sd->sc.data[SC__SHADOWFORM] ||
  4612. sd->sc.data[SC__INVISIBILITY] ||
  4613. sd->sc.data[SC__MANHOLE] ||
  4614. sd->sc.data[SC_DEEPSLEEP] ||
  4615. sd->sc.data[SC_CRYSTALIZE] ||
  4616. sd->sc.data[SC_KAGEHUMI] ||
  4617. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4618. sd->sc.data[SC_KINGS_GRACE] ||
  4619. sd->sc.data[SC_SUHIDE]))
  4620. return false;
  4621. if (!pc_isItemClass(sd,item))
  4622. return false;
  4623. //Dead Branch items
  4624. if( item->flag.dead_branch )
  4625. log_branch(sd);
  4626. return true;
  4627. }
  4628. /*==========================================
  4629. * Last checks to use an item.
  4630. * Return:
  4631. * 0 = fail
  4632. * 1 = success
  4633. *------------------------------------------*/
  4634. int pc_useitem(struct map_session_data *sd,int n)
  4635. {
  4636. t_tick tick = gettick();
  4637. int amount;
  4638. unsigned short nameid;
  4639. struct script_code *script;
  4640. struct item item;
  4641. struct item_data *id;
  4642. nullpo_ret(sd);
  4643. if (sd->npc_id) {
  4644. if (sd->progressbar.npc_id) {
  4645. clif_progressbar_abort(sd);
  4646. return 0; // First item use attempt cancels the progress bar
  4647. }
  4648. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4649. #ifdef RENEWAL
  4650. clif_msg( sd, WORK_IN_PROGRESS );
  4651. #endif
  4652. return 0;
  4653. }
  4654. }
  4655. item = sd->inventory.u.items_inventory[n];
  4656. id = sd->inventory_data[n];
  4657. if (item.nameid == 0 || item.amount <= 0)
  4658. return 0;
  4659. if( !pc_isUseitem(sd,n) )
  4660. return 0;
  4661. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4662. nameid = id->nameid;
  4663. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4664. return 0;
  4665. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4666. if( id->flag.delay_consume ) {
  4667. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4668. return 0;
  4669. else if( pc_issit(sd) )
  4670. return 0;
  4671. }
  4672. //Since most delay-consume items involve using a "skill-type" target cursor,
  4673. //perform a skill-use check before going through. [Skotlex]
  4674. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4675. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4676. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4677. return 0;
  4678. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4679. return 0;
  4680. /* on restricted maps the item is consumed but the effect is not used */
  4681. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4682. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4683. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4684. clif_useitemack(sd,n,item.amount-1,true);
  4685. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4686. }
  4687. return 0;/* regardless, effect is not run */
  4688. }
  4689. sd->itemid = item.nameid;
  4690. sd->itemindex = n;
  4691. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4692. sd->catch_target_class = PET_CATCH_FAIL;
  4693. amount = item.amount;
  4694. script = id->script;
  4695. //Check if the item is to be consumed immediately [Skotlex]
  4696. if (id->flag.delay_consume)
  4697. clif_useitemack(sd, n, amount, true);
  4698. else
  4699. {
  4700. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4701. {
  4702. clif_useitemack(sd, n, amount - 1, true);
  4703. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4704. }
  4705. else
  4706. clif_useitemack(sd, n, 0, false);
  4707. }
  4708. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4709. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4710. //Update item use time.
  4711. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4712. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4713. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4714. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4715. potion_flag = 0;
  4716. return 1;
  4717. }
  4718. /**
  4719. * Add item on cart for given index.
  4720. * @param sd
  4721. * @param item
  4722. * @param amount
  4723. * @param log_type
  4724. * @return See pc.hpp::e_additem_result
  4725. */
  4726. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4727. {
  4728. struct item_data *data;
  4729. int i,w;
  4730. nullpo_retr(ADDITEM_INVALID, sd);
  4731. nullpo_retr(ADDITEM_INVALID, item);
  4732. if(item->nameid == 0 || amount <= 0)
  4733. return ADDITEM_INVALID;
  4734. if (itemdb_ishatched_egg(item))
  4735. return ADDITEM_INVALID;
  4736. data = itemdb_search(item->nameid);
  4737. if( data->stack.cart && amount > data->stack.amount )
  4738. {// item stack limitation
  4739. return ADDITEM_STACKLIMIT;
  4740. }
  4741. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4742. { // Check item trade restrictions [Skotlex]
  4743. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4744. return ADDITEM_INVALID;
  4745. }
  4746. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4747. return ADDITEM_OVERWEIGHT;
  4748. i = MAX_CART;
  4749. if( itemdb_isstackable2(data) && !item->expire_time )
  4750. {
  4751. for (i = 0; i < MAX_CART; i++) {
  4752. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4753. && sd->cart.u.items_cart[i].bound == item->bound
  4754. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4755. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4756. )
  4757. break;
  4758. }
  4759. }
  4760. if( i < MAX_CART )
  4761. {// item already in cart, stack it
  4762. if( amount > MAX_AMOUNT - sd->cart.u.items_cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->cart.u.items_cart[i].amount ) )
  4763. return ADDITEM_OVERAMOUNT; // no slot
  4764. sd->cart.u.items_cart[i].amount += amount;
  4765. clif_cart_additem(sd,i,amount,0);
  4766. }
  4767. else
  4768. {// item not stackable or not present, add it
  4769. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4770. if( i == MAX_CART )
  4771. return ADDITEM_OVERAMOUNT; // no slot
  4772. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4773. sd->cart.u.items_cart[i].id = 0;
  4774. sd->cart.u.items_cart[i].amount = amount;
  4775. sd->cart_num++;
  4776. clif_cart_additem(sd,i,amount,0);
  4777. }
  4778. sd->cart.u.items_cart[i].favorite = 0; // clear
  4779. sd->cart.u.items_cart[i].equipSwitch = 0;
  4780. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4781. sd->cart_weight += w;
  4782. clif_updatestatus(sd,SP_CARTINFO);
  4783. return ADDITEM_SUCCESS;
  4784. }
  4785. /*==========================================
  4786. * Delete item on cart for given index.
  4787. *------------------------------------------*/
  4788. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4789. {
  4790. nullpo_retv(sd);
  4791. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4792. sd->cart.u.items_cart[n].amount < amount)
  4793. return;
  4794. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4795. sd->cart.u.items_cart[n].amount -= amount;
  4796. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4797. if(sd->cart.u.items_cart[n].amount <= 0) {
  4798. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4799. sd->cart_num--;
  4800. }
  4801. if(!type) {
  4802. clif_cart_delitem(sd,n,amount);
  4803. clif_updatestatus(sd,SP_CARTINFO);
  4804. }
  4805. }
  4806. /*==========================================
  4807. * Transfer item from inventory to cart.
  4808. *------------------------------------------*/
  4809. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4810. {
  4811. nullpo_retv(sd);
  4812. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4813. return;
  4814. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4815. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4816. return;
  4817. if( item_data->equipSwitch ){
  4818. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4819. return;
  4820. }
  4821. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4822. if (flag == ADDITEM_SUCCESS)
  4823. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4824. else {
  4825. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4826. clif_additem(sd, idx, amount, 0);
  4827. clif_delitem(sd, idx, amount, 0);
  4828. }
  4829. }
  4830. /*==========================================
  4831. * Get number of item in cart.
  4832. * Return:
  4833. -1 = itemid not found or no amount found
  4834. x = remaining itemid on cart after get
  4835. *------------------------------------------*/
  4836. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4837. {
  4838. struct item* item_data;
  4839. nullpo_retr(-1, sd);
  4840. item_data = &sd->cart.u.items_cart[idx];
  4841. if( item_data->nameid == 0 || item_data->amount == 0 )
  4842. return -1;
  4843. return item_data->amount - amount;
  4844. }
  4845. /*==========================================
  4846. * Retrieve an item at index idx from cart.
  4847. *------------------------------------------*/
  4848. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4849. {
  4850. nullpo_retv(sd);
  4851. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4852. return;
  4853. struct item *item_data=&sd->cart.u.items_cart[idx];
  4854. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4855. return;
  4856. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4857. if (flag == ADDITEM_SUCCESS)
  4858. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4859. else {
  4860. clif_cart_delitem(sd, idx, amount);
  4861. clif_additem(sd, idx, amount, flag);
  4862. clif_cart_additem(sd, idx, amount, 0);
  4863. }
  4864. }
  4865. /*==========================================
  4866. * Bound Item Check
  4867. * Type:
  4868. * 1 Account Bound
  4869. * 2 Guild Bound
  4870. * 3 Party Bound
  4871. * 4 Character Bound
  4872. *------------------------------------------*/
  4873. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4874. {
  4875. int i = 0, j = 0;
  4876. for(i = 0; i < MAX_INVENTORY; i++) {
  4877. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0 && sd->inventory.u.items_inventory[i].bound == type) {
  4878. idxlist[j] = i;
  4879. j++;
  4880. }
  4881. }
  4882. return j;
  4883. }
  4884. /*==========================================
  4885. * Display item stolen msg to player sd
  4886. *------------------------------------------*/
  4887. int pc_show_steal(struct block_list *bl,va_list ap)
  4888. {
  4889. struct map_session_data *sd;
  4890. int itemid;
  4891. struct item_data *item=NULL;
  4892. char output[100];
  4893. sd=va_arg(ap,struct map_session_data *);
  4894. itemid=va_arg(ap,int);
  4895. if((item=itemdb_exists(itemid))==NULL)
  4896. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4897. else
  4898. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4899. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4900. return 0;
  4901. }
  4902. /**
  4903. * Steal an item from bl (mob).
  4904. * @param sd: Player data
  4905. * @param bl: Object to steal from
  4906. * @param skill_lv: Level of skill used
  4907. * @return True on success or false otherwise
  4908. */
  4909. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4910. {
  4911. int i,itemid;
  4912. double rate;
  4913. unsigned char flag = 0;
  4914. struct status_data *sd_status, *md_status;
  4915. struct mob_data *md;
  4916. struct item tmp_item;
  4917. if(!sd || !bl || bl->type!=BL_MOB)
  4918. return false;
  4919. md = (TBL_MOB *)bl;
  4920. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4921. return false;
  4922. sd_status= status_get_status_data(&sd->bl);
  4923. md_status= status_get_status_data(bl);
  4924. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4925. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4926. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4927. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4928. ) { //Can't steal from
  4929. md->state.steal_flag = UCHAR_MAX;
  4930. return false;
  4931. }
  4932. // base skill success chance (percentual)
  4933. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4934. rate += sd->bonus.add_steal_rate;
  4935. if( rate < 1
  4936. #ifdef RENEWAL
  4937. || rnd()%100 >= rate
  4938. #endif
  4939. )
  4940. return false;
  4941. // Try dropping one item, in the order from first to last possible slot.
  4942. // Droprate is affected by the skill success rate.
  4943. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4944. if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p
  4945. #ifndef RENEWAL
  4946. * rate/100.
  4947. #endif
  4948. )
  4949. break;
  4950. if( i == MAX_STEAL_DROP )
  4951. return false;
  4952. itemid = md->db->dropitem[i].nameid;
  4953. memset(&tmp_item,0,sizeof(tmp_item));
  4954. tmp_item.nameid = itemid;
  4955. tmp_item.amount = 1;
  4956. tmp_item.identify = itemdb_isidentified(itemid);
  4957. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4958. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4959. //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?
  4960. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4961. if(flag) { //Failed to steal due to overweight
  4962. clif_additem(sd,0,0,flag);
  4963. return false;
  4964. }
  4965. if(battle_config.show_steal_in_same_party)
  4966. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4967. //Logs items, Stolen from mobs [Lupus]
  4968. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4969. //A Rare Steal Global Announce by Lupus
  4970. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4971. struct item_data *i_data;
  4972. char message[128];
  4973. i_data = itemdb_search(itemid);
  4974. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  4975. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4976. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4977. }
  4978. return true;
  4979. }
  4980. /*==========================================
  4981. * Stole zeny from bl (mob)
  4982. * return
  4983. * 0 = fail
  4984. * 1 = success
  4985. *------------------------------------------*/
  4986. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4987. {
  4988. int rate, target_lv;
  4989. struct mob_data *md;
  4990. if(!sd || !target || target->type != BL_MOB)
  4991. return 0;
  4992. md = (TBL_MOB*)target;
  4993. target_lv = status_get_lv(target);
  4994. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  4995. return 0;
  4996. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4997. if(rnd()%1000 < rate)
  4998. {
  4999. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5000. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5001. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5002. md->state.steal_coin_flag = 1;
  5003. return 1;
  5004. }
  5005. return 0;
  5006. }
  5007. /*==========================================
  5008. * Set's a player position.
  5009. * @param sd
  5010. * @param mapindex
  5011. * @param x
  5012. * @param y
  5013. * @param clrtype
  5014. * @return SETPOS_OK Success
  5015. * SETPOS_MAPINDEX Invalid map index
  5016. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5017. * SETPOS_AUTOTRADE Player is in autotrade state
  5018. *------------------------------------------*/
  5019. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5020. {
  5021. nullpo_retr(SETPOS_OK,sd);
  5022. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5023. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5024. return SETPOS_MAPINDEX;
  5025. }
  5026. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5027. return SETPOS_AUTOTRADE;
  5028. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5029. pc_setstand(sd, true);
  5030. pc_setrestartvalue(sd,1);
  5031. }
  5032. int16 m = map_mapindex2mapid(mapindex);
  5033. struct map_data *mapdata = map_getmapdata(m);
  5034. sd->state.changemap = (sd->mapindex != mapindex);
  5035. sd->state.warping = 1;
  5036. sd->state.workinprogress = WIP_DISABLE_NONE;
  5037. if( sd->state.changemap ) { // Misc map-changing settings
  5038. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5039. if (curr_map_instance_id != new_map_instance_id) {
  5040. if (curr_map_instance_id) // Update instance timer for the map on leave
  5041. instance_delusers(curr_map_instance_id);
  5042. if (new_map_instance_id) // Update instance timer for the map on enter
  5043. instance_addusers(new_map_instance_id);
  5044. }
  5045. sd->state.pmap = sd->bl.m;
  5046. if (sd->sc.count) { // Cancel some map related stuff.
  5047. if (sd->sc.data[SC_JAILED])
  5048. return SETPOS_MAPINDEX; //You may not get out!
  5049. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5050. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5051. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5052. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5053. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5054. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5055. if (sd->sc.data[SC_KNOWLEDGE]) {
  5056. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5057. if (sce->timer != INVALID_TIMER)
  5058. delete_timer(sce->timer, status_change_timer);
  5059. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5060. }
  5061. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5062. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5063. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5064. }
  5065. for(int i = 0; i < EQI_MAX; i++ ) {
  5066. if( sd->equip_index[i] >= 0 )
  5067. if( pc_isequip(sd,sd->equip_index[i]) )
  5068. pc_unequipitem(sd,sd->equip_index[i],2);
  5069. }
  5070. if (battle_config.clear_unit_onwarp&BL_PC)
  5071. skill_clear_unitgroup(&sd->bl);
  5072. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5073. guild_send_dot_remove(sd);
  5074. bg_send_dot_remove(sd);
  5075. if (sd->regen.state.gc)
  5076. sd->regen.state.gc = 0;
  5077. // make sure vending is allowed here
  5078. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5079. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5080. vending_closevending(sd);
  5081. }
  5082. channel_pcquit(sd,4); //quit map chan
  5083. }
  5084. if( m < 0 )
  5085. {
  5086. uint32 ip;
  5087. uint16 port;
  5088. struct script_state *st;
  5089. //if can't find any map-servers, just abort setting position.
  5090. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5091. return SETPOS_NO_MAPSERVER;
  5092. if (sd->npc_id){
  5093. npc_event_dequeue(sd,false);
  5094. st = sd->st;
  5095. }else{
  5096. st = nullptr;
  5097. }
  5098. npc_script_event(sd, NPCE_LOGOUT);
  5099. //remove from map, THEN change x/y coordinates
  5100. unit_remove_map_pc(sd,clrtype);
  5101. sd->mapindex = mapindex;
  5102. sd->bl.x=x;
  5103. sd->bl.y=y;
  5104. pc_clean_skilltree(sd);
  5105. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5106. chrif_changemapserver(sd, ip, (short)port);
  5107. //Free session data from this map server [Kevin]
  5108. unit_free_pc(sd);
  5109. if( st ){
  5110. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5111. st->state = END;
  5112. }
  5113. return SETPOS_OK;
  5114. }
  5115. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5116. {
  5117. 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);
  5118. x = y = 0; // make it random
  5119. }
  5120. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5121. int c=0;
  5122. do {
  5123. x = rnd()%(mapdata->xs-2)+1;
  5124. y = rnd()%(mapdata->ys-2)+1;
  5125. c++;
  5126. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5127. ShowError("pc_setpos: couldn't found a valid coordinates for player '%s' (%d:%d) on (%s), preventing warp\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex));
  5128. return SETPOS_OK; //preventing warp
  5129. //break; //allow warp anyway
  5130. }
  5131. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5132. }
  5133. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5134. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5135. vending_closevending(sd);
  5136. }
  5137. if(sd->bl.prev != NULL){
  5138. unit_remove_map_pc(sd,clrtype);
  5139. clif_changemap(sd,m,x,y); // [MouseJstr]
  5140. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5141. sd->state.rewarp = 1;
  5142. sd->mapindex = mapindex;
  5143. sd->bl.m = m;
  5144. sd->bl.x = sd->ud.to_x = x;
  5145. sd->bl.y = sd->ud.to_y = y;
  5146. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5147. { // Increased guild castle regen [Valaris]
  5148. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5149. if(gc && gc->guild_id == sd->status.guild_id)
  5150. sd->regen.state.gc = 1;
  5151. }
  5152. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5153. {
  5154. sd->pd->bl.m = m;
  5155. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5156. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5157. sd->pd->ud.dir = sd->ud.dir;
  5158. }
  5159. if( hom_is_active(sd->hd) )
  5160. {
  5161. sd->hd->bl.m = m;
  5162. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5163. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5164. sd->hd->ud.dir = sd->ud.dir;
  5165. }
  5166. if( sd->md )
  5167. {
  5168. sd->md->bl.m = m;
  5169. sd->md->bl.x = sd->md->ud.to_x = x;
  5170. sd->md->bl.y = sd->md->ud.to_y = y;
  5171. sd->md->ud.dir = sd->ud.dir;
  5172. }
  5173. if( sd->ed ) {
  5174. sd->ed->bl.m = m;
  5175. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5176. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5177. sd->ed->ud.dir = sd->ud.dir;
  5178. }
  5179. pc_cell_basilica(sd);
  5180. //check if we gonna be rewarped [lighta]
  5181. if(npc_check_areanpc(1,m,x,y,0)){
  5182. sd->count_rewarp++;
  5183. }
  5184. else
  5185. sd->count_rewarp = 0;
  5186. return SETPOS_OK;
  5187. }
  5188. /*==========================================
  5189. * Warp player sd to random location on current map.
  5190. * May fail if no walkable cell found (1000 attempts).
  5191. * Return:
  5192. * 0 = Success
  5193. * 1,2,3 = Fail
  5194. *------------------------------------------*/
  5195. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5196. {
  5197. int x,y,i=0;
  5198. nullpo_ret(sd);
  5199. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5200. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5201. return 3;
  5202. do {
  5203. x = rnd()%(mapdata->xs-2)+1;
  5204. y = rnd()%(mapdata->ys-2)+1;
  5205. } while((map_getcell(sd->bl.m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,sd->bl.m,x,y,1))) && (i++) < 1000);
  5206. if (i < 1000)
  5207. return pc_setpos(sd,mapdata->index,x,y,type);
  5208. return 3;
  5209. }
  5210. /*==========================================
  5211. * Records a memo point at sd's current position
  5212. * pos - entry to replace, (-1: shift oldest entry out)
  5213. *------------------------------------------*/
  5214. bool pc_memo(struct map_session_data* sd, int pos)
  5215. {
  5216. int skill;
  5217. nullpo_ret(sd);
  5218. // check mapflags
  5219. if( sd->bl.m >= 0 && (map_getmapflag(sd->bl.m, MF_NOMEMO) || map_getmapflag(sd->bl.m, MF_NOWARPTO)) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  5220. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5221. return false;
  5222. }
  5223. // check inputs
  5224. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5225. return false; // invalid input
  5226. // check required skill level
  5227. skill = pc_checkskill(sd, AL_WARP);
  5228. if( skill < 1 ) {
  5229. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5230. return false;
  5231. }
  5232. if( skill < 2 || skill - 2 < pos ) {
  5233. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5234. return false;
  5235. }
  5236. if( pos == -1 )
  5237. {
  5238. uint8 i;
  5239. // prevent memo-ing the same map multiple times
  5240. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5241. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5242. pos = 0;
  5243. }
  5244. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5245. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5246. return false;
  5247. }
  5248. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5249. sd->status.memo_point[pos].x = sd->bl.x;
  5250. sd->status.memo_point[pos].y = sd->bl.y;
  5251. clif_skill_memomessage(sd, 0);
  5252. return true;
  5253. }
  5254. //
  5255. // Skills
  5256. //
  5257. /**
  5258. * Get the skill current cooldown for player.
  5259. * (get the db base cooldown for skill + player specific cooldown)
  5260. * @param sd : player pointer
  5261. * @param id : skill id
  5262. * @param lv : skill lv
  5263. * @return player skill cooldown
  5264. */
  5265. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5266. uint16 idx = skill_get_index(skill_id);
  5267. int cooldown = 0;
  5268. if (!idx) return 0;
  5269. if (skill_db[idx]->cooldown[skill_lv - 1])
  5270. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5271. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5272. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5273. for (auto &it : sd->skillcooldown) {
  5274. if (it.id == skill_id) {
  5275. cooldown += it.val;
  5276. cooldown = max(0, cooldown);
  5277. break;
  5278. }
  5279. }
  5280. return cooldown;
  5281. }
  5282. /*==========================================
  5283. * Return player sd skill_lv learned for given skill
  5284. *------------------------------------------*/
  5285. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5286. {
  5287. uint16 idx = 0;
  5288. if (sd == NULL)
  5289. return 0;
  5290. if ((idx = skill_get_index(skill_id)) == 0) {
  5291. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5292. return 0;
  5293. }
  5294. if (SKILL_CHK_GUILD(skill_id) ) {
  5295. struct guild *g;
  5296. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5297. return guild_checkskill(g,skill_id);
  5298. return 0;
  5299. }
  5300. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5301. }
  5302. /**
  5303. * Check if we still have the correct weapon to continue the skill (actually status)
  5304. * If not ending it
  5305. * @param sd
  5306. * @return 0:error, 1:check done
  5307. */
  5308. static void pc_checkallowskill(struct map_session_data *sd)
  5309. {
  5310. const enum sc_type scw_list[] = {
  5311. SC_TWOHANDQUICKEN,
  5312. SC_ONEHAND,
  5313. SC_AURABLADE,
  5314. SC_PARRYING,
  5315. SC_SPEARQUICKEN,
  5316. SC_ADRENALINE,
  5317. SC_ADRENALINE2,
  5318. SC_DANCING,
  5319. SC_GATLINGFEVER,
  5320. };
  5321. uint8 i;
  5322. nullpo_retv(sd);
  5323. if(!sd->sc.count)
  5324. return;
  5325. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5326. { // Skills requiring specific weapon types
  5327. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5328. continue;
  5329. if(sd->sc.data[scw_list[i]] &&
  5330. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5331. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5332. }
  5333. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5334. // Spurt requires bare hands (feet, in fact xD)
  5335. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5336. if(sd->status.shield <= 0) { // Skills requiring a shield
  5337. const enum sc_type scs_list[] = {
  5338. SC_AUTOGUARD,
  5339. SC_DEFENDER,
  5340. SC_REFLECTSHIELD,
  5341. SC_REFLECTDAMAGE
  5342. };
  5343. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5344. if(sd->sc.data[scs_list[i]])
  5345. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5346. }
  5347. }
  5348. /*==========================================
  5349. * Return equipped index of item on player sd at pos
  5350. * Return
  5351. * -1 : Nothing equipped
  5352. * idx : (this index could be used in inventory to found item_data)
  5353. *------------------------------------------*/
  5354. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5355. {
  5356. uint8 i;
  5357. nullpo_retr(-1, sd);
  5358. for(i=0;i<EQI_MAX;i++){
  5359. if(pos & equip_bitmask[i]){
  5360. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5361. // Check all if any match is found
  5362. continue;
  5363. }
  5364. return sd->equip_index[i];
  5365. }
  5366. }
  5367. return -1;
  5368. }
  5369. /*==========================================
  5370. * Check if sd has nameid equipped somewhere
  5371. * @sd : the player session
  5372. * @nameid : id of the item to check
  5373. * @min : : see pc.hpp enum equip_index from ? to @max
  5374. * @max : see pc.hpp enum equip_index for @min to ?
  5375. * -return true,false
  5376. *------------------------------------------*/
  5377. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5378. {
  5379. int i;
  5380. for(i = min; i < max; i++) {
  5381. if(equip_bitmask[i]) {
  5382. int idx = sd->equip_index[i];
  5383. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5384. return true;
  5385. }
  5386. }
  5387. return false;
  5388. }
  5389. /*==========================================
  5390. * Convert's from the client's lame Job ID system
  5391. * to the map server's 'makes sense' system. [Skotlex]
  5392. *------------------------------------------*/
  5393. int pc_jobid2mapid(unsigned short b_class)
  5394. {
  5395. switch(b_class)
  5396. {
  5397. //Novice And 1-1 Jobs
  5398. case JOB_NOVICE: return MAPID_NOVICE;
  5399. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5400. case JOB_MAGE: return MAPID_MAGE;
  5401. case JOB_ARCHER: return MAPID_ARCHER;
  5402. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5403. case JOB_MERCHANT: return MAPID_MERCHANT;
  5404. case JOB_THIEF: return MAPID_THIEF;
  5405. case JOB_TAEKWON: return MAPID_TAEKWON;
  5406. case JOB_WEDDING: return MAPID_WEDDING;
  5407. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5408. case JOB_NINJA: return MAPID_NINJA;
  5409. case JOB_XMAS: return MAPID_XMAS;
  5410. case JOB_SUMMER: return MAPID_SUMMER;
  5411. case JOB_HANBOK: return MAPID_HANBOK;
  5412. case JOB_GANGSI: return MAPID_GANGSI;
  5413. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5414. case JOB_SUMMER2: return MAPID_SUMMER2;
  5415. //2-1 Jobs
  5416. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5417. case JOB_KNIGHT: return MAPID_KNIGHT;
  5418. case JOB_WIZARD: return MAPID_WIZARD;
  5419. case JOB_HUNTER: return MAPID_HUNTER;
  5420. case JOB_PRIEST: return MAPID_PRIEST;
  5421. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5422. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5423. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5424. case JOB_KAGEROU:
  5425. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5426. case JOB_REBELLION: return MAPID_REBELLION;
  5427. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5428. //2-2 Jobs
  5429. case JOB_CRUSADER: return MAPID_CRUSADER;
  5430. case JOB_SAGE: return MAPID_SAGE;
  5431. case JOB_BARD:
  5432. case JOB_DANCER: return MAPID_BARDDANCER;
  5433. case JOB_MONK: return MAPID_MONK;
  5434. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5435. case JOB_ROGUE: return MAPID_ROGUE;
  5436. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5437. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5438. //Trans Novice And Trans 1-1 Jobs
  5439. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5440. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5441. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5442. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5443. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5444. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5445. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5446. //Trans 2-1 Jobs
  5447. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5448. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5449. case JOB_SNIPER: return MAPID_SNIPER;
  5450. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5451. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5452. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5453. //Trans 2-2 Jobs
  5454. case JOB_PALADIN: return MAPID_PALADIN;
  5455. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5456. case JOB_CLOWN:
  5457. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5458. case JOB_CHAMPION: return MAPID_CHAMPION;
  5459. case JOB_CREATOR: return MAPID_CREATOR;
  5460. case JOB_STALKER: return MAPID_STALKER;
  5461. //Baby Novice And Baby 1-1 Jobs
  5462. case JOB_BABY: return MAPID_BABY;
  5463. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5464. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5465. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5466. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5467. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5468. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5469. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5470. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5471. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5472. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5473. //Baby 2-1 Jobs
  5474. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5475. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5476. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5477. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5478. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5479. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5480. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5481. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5482. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5483. case JOB_BABY_KAGEROU:
  5484. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5485. //Baby 2-2 Jobs
  5486. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5487. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5488. case JOB_BABY_BARD:
  5489. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5490. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5491. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5492. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5493. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5494. //3-1 Jobs
  5495. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5496. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5497. case JOB_WARLOCK: return MAPID_WARLOCK;
  5498. case JOB_RANGER: return MAPID_RANGER;
  5499. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5500. case JOB_MECHANIC: return MAPID_MECHANIC;
  5501. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5502. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5503. //3-2 Jobs
  5504. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5505. case JOB_SORCERER: return MAPID_SORCERER;
  5506. case JOB_MINSTREL:
  5507. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5508. case JOB_SURA: return MAPID_SURA;
  5509. case JOB_GENETIC: return MAPID_GENETIC;
  5510. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5511. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5512. //Trans 3-1 Jobs
  5513. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5514. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5515. case JOB_RANGER_T: return MAPID_RANGER_T;
  5516. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5517. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5518. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5519. //Trans 3-2 Jobs
  5520. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5521. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5522. case JOB_MINSTREL_T:
  5523. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5524. case JOB_SURA_T: return MAPID_SURA_T;
  5525. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5526. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5527. //Baby 3-1 Jobs
  5528. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5529. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5530. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5531. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5532. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5533. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5534. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5535. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5536. //Baby 3-2 Jobs
  5537. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5538. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5539. case JOB_BABY_MINSTREL:
  5540. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5541. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5542. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5543. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5544. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5545. //Doram Jobs
  5546. case JOB_SUMMONER: return MAPID_SUMMONER;
  5547. default:
  5548. return -1;
  5549. }
  5550. }
  5551. //Reverts the map-style class id to the client-style one.
  5552. int pc_mapid2jobid(unsigned short class_, int sex)
  5553. {
  5554. switch(class_) {
  5555. //Novice And 1-1 Jobs
  5556. case MAPID_NOVICE: return JOB_NOVICE;
  5557. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5558. case MAPID_MAGE: return JOB_MAGE;
  5559. case MAPID_ARCHER: return JOB_ARCHER;
  5560. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5561. case MAPID_MERCHANT: return JOB_MERCHANT;
  5562. case MAPID_THIEF: return JOB_THIEF;
  5563. case MAPID_TAEKWON: return JOB_TAEKWON;
  5564. case MAPID_WEDDING: return JOB_WEDDING;
  5565. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5566. case MAPID_NINJA: return JOB_NINJA;
  5567. case MAPID_XMAS: return JOB_XMAS;
  5568. case MAPID_SUMMER: return JOB_SUMMER;
  5569. case MAPID_HANBOK: return JOB_HANBOK;
  5570. case MAPID_GANGSI: return JOB_GANGSI;
  5571. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5572. case MAPID_SUMMER2: return JOB_SUMMER2;
  5573. //2-1 Jobs
  5574. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5575. case MAPID_KNIGHT: return JOB_KNIGHT;
  5576. case MAPID_WIZARD: return JOB_WIZARD;
  5577. case MAPID_HUNTER: return JOB_HUNTER;
  5578. case MAPID_PRIEST: return JOB_PRIEST;
  5579. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5580. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5581. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5582. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5583. case MAPID_REBELLION: return JOB_REBELLION;
  5584. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5585. //2-2 Jobs
  5586. case MAPID_CRUSADER: return JOB_CRUSADER;
  5587. case MAPID_SAGE: return JOB_SAGE;
  5588. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5589. case MAPID_MONK: return JOB_MONK;
  5590. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5591. case MAPID_ROGUE: return JOB_ROGUE;
  5592. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5593. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5594. //Trans Novice And Trans 2-1 Jobs
  5595. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5596. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5597. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5598. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5599. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5600. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5601. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5602. //Trans 2-1 Jobs
  5603. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5604. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5605. case MAPID_SNIPER: return JOB_SNIPER;
  5606. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5607. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5608. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5609. //Trans 2-2 Jobs
  5610. case MAPID_PALADIN: return JOB_PALADIN;
  5611. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5612. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5613. case MAPID_CHAMPION: return JOB_CHAMPION;
  5614. case MAPID_CREATOR: return JOB_CREATOR;
  5615. case MAPID_STALKER: return JOB_STALKER;
  5616. //Baby Novice And Baby 1-1 Jobs
  5617. case MAPID_BABY: return JOB_BABY;
  5618. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5619. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5620. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5621. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5622. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5623. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5624. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5625. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5626. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5627. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5628. //Baby 2-1 Jobs
  5629. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5630. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5631. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5632. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5633. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5634. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5635. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5636. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5637. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5638. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5639. //Baby 2-2 Jobs
  5640. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5641. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5642. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5643. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5644. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5645. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5646. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5647. //3-1 Jobs
  5648. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5649. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5650. case MAPID_WARLOCK: return JOB_WARLOCK;
  5651. case MAPID_RANGER: return JOB_RANGER;
  5652. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5653. case MAPID_MECHANIC: return JOB_MECHANIC;
  5654. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5655. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5656. //3-2 Jobs
  5657. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5658. case MAPID_SORCERER: return JOB_SORCERER;
  5659. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5660. case MAPID_SURA: return JOB_SURA;
  5661. case MAPID_GENETIC: return JOB_GENETIC;
  5662. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5663. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5664. //Trans 3-1 Jobs
  5665. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5666. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5667. case MAPID_RANGER_T: return JOB_RANGER_T;
  5668. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5669. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5670. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5671. //Trans 3-2 Jobs
  5672. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5673. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5674. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5675. case MAPID_SURA_T: return JOB_SURA_T;
  5676. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5677. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5678. //Baby 3-1 Jobs
  5679. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5680. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5681. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5682. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5683. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5684. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5685. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5686. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5687. //Baby 3-2 Jobs
  5688. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5689. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5690. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5691. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5692. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5693. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5694. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5695. //Doram Jobs
  5696. case MAPID_SUMMONER: return JOB_SUMMONER;
  5697. default:
  5698. return -1;
  5699. }
  5700. }
  5701. /*====================================================
  5702. * This function return the name of the job (by [Yor])
  5703. *----------------------------------------------------*/
  5704. const char* job_name(int class_)
  5705. {
  5706. switch (class_) {
  5707. case JOB_NOVICE:
  5708. case JOB_SWORDMAN:
  5709. case JOB_MAGE:
  5710. case JOB_ARCHER:
  5711. case JOB_ACOLYTE:
  5712. case JOB_MERCHANT:
  5713. case JOB_THIEF:
  5714. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5715. case JOB_KNIGHT:
  5716. case JOB_PRIEST:
  5717. case JOB_WIZARD:
  5718. case JOB_BLACKSMITH:
  5719. case JOB_HUNTER:
  5720. case JOB_ASSASSIN:
  5721. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5722. case JOB_KNIGHT2:
  5723. return msg_txt(NULL,557);
  5724. case JOB_CRUSADER:
  5725. case JOB_MONK:
  5726. case JOB_SAGE:
  5727. case JOB_ROGUE:
  5728. case JOB_ALCHEMIST:
  5729. case JOB_BARD:
  5730. case JOB_DANCER:
  5731. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5732. case JOB_CRUSADER2:
  5733. return msg_txt(NULL,563);
  5734. case JOB_WEDDING:
  5735. case JOB_SUPER_NOVICE:
  5736. case JOB_GUNSLINGER:
  5737. case JOB_NINJA:
  5738. case JOB_XMAS:
  5739. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5740. case JOB_SUMMER:
  5741. case JOB_SUMMER2:
  5742. return msg_txt(NULL,621);
  5743. case JOB_HANBOK:
  5744. return msg_txt(NULL,694);
  5745. case JOB_OKTOBERFEST:
  5746. return msg_txt(NULL,696);
  5747. case JOB_NOVICE_HIGH:
  5748. case JOB_SWORDMAN_HIGH:
  5749. case JOB_MAGE_HIGH:
  5750. case JOB_ARCHER_HIGH:
  5751. case JOB_ACOLYTE_HIGH:
  5752. case JOB_MERCHANT_HIGH:
  5753. case JOB_THIEF_HIGH:
  5754. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5755. case JOB_LORD_KNIGHT:
  5756. case JOB_HIGH_PRIEST:
  5757. case JOB_HIGH_WIZARD:
  5758. case JOB_WHITESMITH:
  5759. case JOB_SNIPER:
  5760. case JOB_ASSASSIN_CROSS:
  5761. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5762. case JOB_LORD_KNIGHT2:
  5763. return msg_txt(NULL,582);
  5764. case JOB_PALADIN:
  5765. case JOB_CHAMPION:
  5766. case JOB_PROFESSOR:
  5767. case JOB_STALKER:
  5768. case JOB_CREATOR:
  5769. case JOB_CLOWN:
  5770. case JOB_GYPSY:
  5771. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5772. case JOB_PALADIN2:
  5773. return msg_txt(NULL,588);
  5774. case JOB_BABY:
  5775. case JOB_BABY_SWORDMAN:
  5776. case JOB_BABY_MAGE:
  5777. case JOB_BABY_ARCHER:
  5778. case JOB_BABY_ACOLYTE:
  5779. case JOB_BABY_MERCHANT:
  5780. case JOB_BABY_THIEF:
  5781. return msg_txt(NULL,595 - JOB_BABY + class_);
  5782. case JOB_BABY_KNIGHT:
  5783. case JOB_BABY_PRIEST:
  5784. case JOB_BABY_WIZARD:
  5785. case JOB_BABY_BLACKSMITH:
  5786. case JOB_BABY_HUNTER:
  5787. case JOB_BABY_ASSASSIN:
  5788. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5789. case JOB_BABY_KNIGHT2:
  5790. return msg_txt(NULL,602);
  5791. case JOB_BABY_CRUSADER:
  5792. case JOB_BABY_MONK:
  5793. case JOB_BABY_SAGE:
  5794. case JOB_BABY_ROGUE:
  5795. case JOB_BABY_ALCHEMIST:
  5796. case JOB_BABY_BARD:
  5797. case JOB_BABY_DANCER:
  5798. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5799. case JOB_BABY_CRUSADER2:
  5800. return msg_txt(NULL,608);
  5801. case JOB_SUPER_BABY:
  5802. return msg_txt(NULL,615);
  5803. case JOB_TAEKWON:
  5804. return msg_txt(NULL,616);
  5805. case JOB_STAR_GLADIATOR:
  5806. case JOB_STAR_GLADIATOR2:
  5807. return msg_txt(NULL,617);
  5808. case JOB_SOUL_LINKER:
  5809. return msg_txt(NULL,618);
  5810. case JOB_GANGSI:
  5811. case JOB_DEATH_KNIGHT:
  5812. case JOB_DARK_COLLECTOR:
  5813. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5814. case JOB_RUNE_KNIGHT:
  5815. case JOB_WARLOCK:
  5816. case JOB_RANGER:
  5817. case JOB_ARCH_BISHOP:
  5818. case JOB_MECHANIC:
  5819. case JOB_GUILLOTINE_CROSS:
  5820. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5821. case JOB_RUNE_KNIGHT_T:
  5822. case JOB_WARLOCK_T:
  5823. case JOB_RANGER_T:
  5824. case JOB_ARCH_BISHOP_T:
  5825. case JOB_MECHANIC_T:
  5826. case JOB_GUILLOTINE_CROSS_T:
  5827. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5828. case JOB_ROYAL_GUARD:
  5829. case JOB_SORCERER:
  5830. case JOB_MINSTREL:
  5831. case JOB_WANDERER:
  5832. case JOB_SURA:
  5833. case JOB_GENETIC:
  5834. case JOB_SHADOW_CHASER:
  5835. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5836. case JOB_ROYAL_GUARD_T:
  5837. case JOB_SORCERER_T:
  5838. case JOB_MINSTREL_T:
  5839. case JOB_WANDERER_T:
  5840. case JOB_SURA_T:
  5841. case JOB_GENETIC_T:
  5842. case JOB_SHADOW_CHASER_T:
  5843. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5844. case JOB_RUNE_KNIGHT2:
  5845. case JOB_RUNE_KNIGHT_T2:
  5846. return msg_txt(NULL,625);
  5847. case JOB_ROYAL_GUARD2:
  5848. case JOB_ROYAL_GUARD_T2:
  5849. return msg_txt(NULL,631);
  5850. case JOB_RANGER2:
  5851. case JOB_RANGER_T2:
  5852. return msg_txt(NULL,627);
  5853. case JOB_MECHANIC2:
  5854. case JOB_MECHANIC_T2:
  5855. return msg_txt(NULL,629);
  5856. case JOB_BABY_RUNE:
  5857. case JOB_BABY_WARLOCK:
  5858. case JOB_BABY_RANGER:
  5859. case JOB_BABY_BISHOP:
  5860. case JOB_BABY_MECHANIC:
  5861. case JOB_BABY_CROSS:
  5862. case JOB_BABY_GUARD:
  5863. case JOB_BABY_SORCERER:
  5864. case JOB_BABY_MINSTREL:
  5865. case JOB_BABY_WANDERER:
  5866. case JOB_BABY_SURA:
  5867. case JOB_BABY_GENETIC:
  5868. case JOB_BABY_CHASER:
  5869. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5870. case JOB_BABY_RUNE2:
  5871. return msg_txt(NULL,638);
  5872. case JOB_BABY_GUARD2:
  5873. return msg_txt(NULL,644);
  5874. case JOB_BABY_RANGER2:
  5875. return msg_txt(NULL,640);
  5876. case JOB_BABY_MECHANIC2:
  5877. return msg_txt(NULL,642);
  5878. case JOB_SUPER_NOVICE_E:
  5879. case JOB_SUPER_BABY_E:
  5880. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5881. case JOB_KAGEROU:
  5882. case JOB_OBORO:
  5883. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5884. case JOB_REBELLION:
  5885. return msg_txt(NULL,695);
  5886. case JOB_SUMMONER:
  5887. return msg_txt(NULL,697);
  5888. case JOB_BABY_SUMMONER:
  5889. return msg_txt(NULL,698);
  5890. case JOB_BABY_NINJA:
  5891. return msg_txt(NULL,699);
  5892. case JOB_BABY_KAGEROU:
  5893. case JOB_BABY_OBORO:
  5894. case JOB_BABY_TAEKWON:
  5895. case JOB_BABY_STAR_GLADIATOR:
  5896. case JOB_BABY_SOUL_LINKER:
  5897. case JOB_BABY_GUNSLINGER:
  5898. case JOB_BABY_REBELLION:
  5899. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5900. case JOB_BABY_STAR_GLADIATOR2:
  5901. return msg_txt(NULL,756);
  5902. case JOB_STAR_EMPEROR:
  5903. case JOB_SOUL_REAPER:
  5904. case JOB_BABY_STAR_EMPEROR:
  5905. case JOB_BABY_SOUL_REAPER:
  5906. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5907. case JOB_STAR_EMPEROR2:
  5908. return msg_txt(NULL,782);
  5909. case JOB_BABY_STAR_EMPEROR2:
  5910. return msg_txt(NULL,784);
  5911. default:
  5912. return msg_txt(NULL,655);
  5913. }
  5914. }
  5915. /*====================================================
  5916. * Timered function to make id follow a target.
  5917. * @id = bl.id (player only atm)
  5918. * target is define in sd->followtarget (bl.id)
  5919. * used by pc_follow
  5920. *----------------------------------------------------*/
  5921. TIMER_FUNC(pc_follow_timer){
  5922. struct map_session_data *sd;
  5923. struct block_list *tbl;
  5924. sd = map_id2sd(id);
  5925. nullpo_ret(sd);
  5926. if (sd->followtimer != tid){
  5927. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5928. sd->followtimer = INVALID_TIMER;
  5929. return 0;
  5930. }
  5931. sd->followtimer = INVALID_TIMER;
  5932. tbl = map_id2bl(sd->followtarget);
  5933. if (tbl == NULL || pc_isdead(sd))
  5934. {
  5935. pc_stop_following(sd);
  5936. return 0;
  5937. }
  5938. // either player or target is currently detached from map blocks (could be teleporting),
  5939. // but still connected to this map, so we'll just increment the timer and check back later
  5940. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5941. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5942. {
  5943. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5944. if (!check_distance_bl(&sd->bl, tbl, 5))
  5945. unit_walktobl(&sd->bl, tbl, 5, 0);
  5946. } else
  5947. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5948. }
  5949. sd->followtimer = add_timer(
  5950. tick + 1000, // increase time a bit to loosen up map's load
  5951. pc_follow_timer, sd->bl.id, 0);
  5952. return 0;
  5953. }
  5954. int pc_stop_following (struct map_session_data *sd)
  5955. {
  5956. nullpo_ret(sd);
  5957. if (sd->followtimer != INVALID_TIMER) {
  5958. delete_timer(sd->followtimer,pc_follow_timer);
  5959. sd->followtimer = INVALID_TIMER;
  5960. }
  5961. sd->followtarget = -1;
  5962. sd->ud.target_to = 0;
  5963. unit_stop_walking(&sd->bl, 1);
  5964. return 0;
  5965. }
  5966. int pc_follow(struct map_session_data *sd,int target_id)
  5967. {
  5968. struct block_list *bl = map_id2bl(target_id);
  5969. if (bl == NULL /*|| bl->type != BL_PC*/)
  5970. return 1;
  5971. if (sd->followtimer != INVALID_TIMER)
  5972. pc_stop_following(sd);
  5973. sd->followtarget = target_id;
  5974. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5975. return 0;
  5976. }
  5977. int pc_checkbaselevelup(struct map_session_data *sd) {
  5978. unsigned int next = pc_nextbaseexp(sd);
  5979. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5980. return 0;
  5981. do {
  5982. sd->status.base_exp -= next;
  5983. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5984. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_base > 0 && sd->status.base_level >= battle_config.multi_level_up_base ) ) && sd->status.base_exp > next-1 )
  5985. sd->status.base_exp = next-1;
  5986. next = pc_gets_status_point(sd->status.base_level);
  5987. sd->status.base_level++;
  5988. sd->status.status_point += next;
  5989. if( pc_is_maxbaselv(sd) ){
  5990. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5991. break;
  5992. }
  5993. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5994. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5995. status_calc_pet(sd->pd,SCO_NONE);
  5996. clif_updatestatus(sd,SP_STATUSPOINT);
  5997. clif_updatestatus(sd,SP_BASELEVEL);
  5998. clif_updatestatus(sd,SP_BASEEXP);
  5999. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6000. status_calc_pc(sd,SCO_FORCE);
  6001. status_percent_heal(&sd->bl,100,100);
  6002. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6003. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6004. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6005. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6006. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6007. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6008. if (sd->state.snovice_dead_flag)
  6009. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6010. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6011. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6012. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6013. }
  6014. clif_misceffect(&sd->bl,0);
  6015. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6016. if(sd->status.party_id)
  6017. party_send_levelup(sd);
  6018. pc_baselevelchanged(sd);
  6019. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6020. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6021. return 1;
  6022. }
  6023. void pc_baselevelchanged(struct map_session_data *sd) {
  6024. uint8 i;
  6025. for( i = 0; i < EQI_MAX; i++ ) {
  6026. if( sd->equip_index[i] >= 0 ) {
  6027. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6028. pc_unequipitem(sd, sd->equip_index[i], 3);
  6029. }
  6030. }
  6031. pc_show_questinfo(sd);
  6032. }
  6033. int pc_checkjoblevelup(struct map_session_data *sd)
  6034. {
  6035. unsigned int next = pc_nextjobexp(sd);
  6036. nullpo_ret(sd);
  6037. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6038. return 0;
  6039. do {
  6040. sd->status.job_exp -= next;
  6041. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6042. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_job > 0 && sd->status.job_level >= battle_config.multi_level_up_job ) ) && sd->status.job_exp > next-1 )
  6043. sd->status.job_exp = next-1;
  6044. sd->status.job_level ++;
  6045. sd->status.skill_point ++;
  6046. if( pc_is_maxjoblv(sd) ){
  6047. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6048. break;
  6049. }
  6050. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6051. clif_updatestatus(sd,SP_JOBLEVEL);
  6052. clif_updatestatus(sd,SP_JOBEXP);
  6053. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6054. clif_updatestatus(sd,SP_SKILLPOINT);
  6055. status_calc_pc(sd,SCO_FORCE);
  6056. clif_misceffect(&sd->bl,1);
  6057. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  6058. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6059. npc_script_event(sd, NPCE_JOBLVUP);
  6060. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6061. pc_show_questinfo(sd);
  6062. return 1;
  6063. }
  6064. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6065. * @param sd Player
  6066. * @param base_exp Base EXP before peronal bonuses
  6067. * @param job_exp Job EXP before peronal bonuses
  6068. * @param src Block list that affecting the exp calculation
  6069. */
  6070. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6071. {
  6072. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6073. if (src) {
  6074. struct status_data *status = status_get_status_data(src);
  6075. if( sd->expaddrace[status->race] )
  6076. bonus += sd->expaddrace[status->race];
  6077. if( sd->expaddrace[RC_ALL] )
  6078. bonus += sd->expaddrace[RC_ALL];
  6079. if( sd->expaddclass[status->class_] )
  6080. bonus += sd->expaddclass[status->class_];
  6081. if( sd->expaddclass[CLASS_ALL] )
  6082. bonus += sd->expaddclass[CLASS_ALL];
  6083. if (battle_config.pk_mode &&
  6084. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6085. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6086. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6087. vip_bonus_base = battle_config.vip_base_exp_increase;
  6088. vip_bonus_job = battle_config.vip_job_exp_increase;
  6089. }
  6090. }
  6091. // Give EXPBOOST for quests even if src is NULL.
  6092. if (sd->sc.data[SC_EXPBOOST]) {
  6093. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6094. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6095. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6096. }
  6097. if (*base_exp) {
  6098. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6099. *base_exp = cap_value(exp, 1, UINT_MAX);
  6100. }
  6101. // Give JEXPBOOST for quests even if src is NULL.
  6102. if (sd->sc.data[SC_JEXPBOOST])
  6103. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6104. if (*job_exp) {
  6105. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6106. *job_exp = cap_value(exp, 1, UINT_MAX);
  6107. }
  6108. return;
  6109. }
  6110. /**
  6111. * Show EXP gained by player in percentage by @showexp
  6112. * @param sd Player
  6113. * @param base_exp Base EXP gained/loss
  6114. * @param next_base_exp Base EXP needed for next base level
  6115. * @param job_exp Job EXP gained/loss
  6116. * @param next_job_exp Job EXP needed for next job level
  6117. * @param lost True:EXP penalty, lose EXP
  6118. **/
  6119. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  6120. char output[CHAT_SIZE_MAX];
  6121. nullpo_retv(sd);
  6122. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6123. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6124. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6125. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6126. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6127. }
  6128. /**
  6129. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6130. * @param sd Player
  6131. * @param src EXP source
  6132. * @param base_exp Base EXP gained
  6133. * @param base_exp Job EXP gained
  6134. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6135. * @return
  6136. **/
  6137. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6138. {
  6139. unsigned int nextb = 0, nextj = 0;
  6140. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6141. nullpo_retv(sd);
  6142. if(sd->bl.prev == NULL || pc_isdead(sd))
  6143. return;
  6144. if (!(exp_flag&2)) {
  6145. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6146. return; // no exp on pvp maps
  6147. if (sd->status.guild_id>0)
  6148. base_exp -= guild_payexp(sd,base_exp);
  6149. }
  6150. flag = ((base_exp) ? 1 : 0) |
  6151. ((job_exp) ? 2 : 0) |
  6152. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6153. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6154. if (!(exp_flag&2))
  6155. pc_calcexp(sd, &base_exp, &job_exp, src);
  6156. nextb = pc_nextbaseexp(sd);
  6157. nextj = pc_nextjobexp(sd);
  6158. if (flag&4){
  6159. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6160. base_exp = 0;
  6161. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6162. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6163. }
  6164. if (flag&8){
  6165. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6166. job_exp = 0;
  6167. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6168. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6169. }
  6170. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6171. //Note that this value should never be greater than the original
  6172. //therefore no overflow checks are needed. [Skotlex]
  6173. if (nextb > 0) {
  6174. float nextbp = (float) base_exp / (float) nextb;
  6175. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6176. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6177. }
  6178. if (nextj > 0) {
  6179. float nextjp = (float) job_exp / (float) nextj;
  6180. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6181. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6182. }
  6183. }
  6184. // Give EXP for Base Level
  6185. if (base_exp) {
  6186. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6187. sd->status.base_exp = UINT32_MAX;
  6188. else
  6189. sd->status.base_exp += base_exp;
  6190. if (!pc_checkbaselevelup(sd))
  6191. clif_updatestatus(sd,SP_BASEEXP);
  6192. }
  6193. // Give EXP for Job Level
  6194. if (job_exp) {
  6195. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6196. sd->status.job_exp = UINT32_MAX;
  6197. else
  6198. sd->status.job_exp += job_exp;
  6199. if (!pc_checkjoblevelup(sd))
  6200. clif_updatestatus(sd,SP_JOBEXP);
  6201. }
  6202. if (flag&1)
  6203. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6204. if (flag&2)
  6205. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6206. if (sd->state.showexp && (base_exp || job_exp))
  6207. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6208. }
  6209. /**
  6210. * Lost Base/Job EXP from a player
  6211. * @param sd Player
  6212. * @param base_exp Base EXP lost
  6213. * @param job_exp Job EXP lost
  6214. **/
  6215. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6216. nullpo_retv(sd);
  6217. if (base_exp) {
  6218. base_exp = u32min(sd->status.base_exp, base_exp);
  6219. sd->status.base_exp -= base_exp;
  6220. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6221. clif_updatestatus(sd, SP_BASEEXP);
  6222. }
  6223. if (job_exp) {
  6224. job_exp = u32min(sd->status.job_exp, job_exp);
  6225. sd->status.job_exp -= job_exp;
  6226. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6227. clif_updatestatus(sd, SP_JOBEXP);
  6228. }
  6229. if (sd->state.showexp && (base_exp || job_exp))
  6230. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6231. }
  6232. /**
  6233. * Returns max base level for this character's class.
  6234. * @param class_: Player's class
  6235. * @return Max Base Level
  6236. */
  6237. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6238. return job_info[pc_class2idx(class_)].max_level[0];
  6239. }
  6240. /**
  6241. * Returns max base level for this character.
  6242. * @param sd Player
  6243. * @return Max Base Level
  6244. **/
  6245. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6246. return pc_class_maxbaselv(sd->status.class_);
  6247. }
  6248. /**
  6249. * Returns max job level for this character's class.
  6250. * @param class_: Player's class
  6251. * @return Max Job Level
  6252. */
  6253. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6254. return job_info[pc_class2idx(class_)].max_level[1];
  6255. }
  6256. /**
  6257. * Returns max job level for this character.
  6258. * @param sd Player
  6259. * @return Max Job Level
  6260. **/
  6261. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6262. return pc_class_maxjoblv(sd->status.class_);
  6263. }
  6264. /**
  6265. * Check if player is reached max base level
  6266. * @param sd
  6267. * @return True if reached max level
  6268. **/
  6269. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6270. nullpo_retr(false, sd);
  6271. return (sd->status.base_level >= pc_maxbaselv(sd));
  6272. }
  6273. /**
  6274. * Check if player is reached max base level
  6275. * @param sd
  6276. * @return True if reached max level
  6277. **/
  6278. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6279. nullpo_retr(false, sd);
  6280. return (sd->status.job_level >= pc_maxjoblv(sd));
  6281. }
  6282. /**
  6283. * Base exp needed for player to level up.
  6284. * @param sd
  6285. * @return Base EXP needed for next base level
  6286. **/
  6287. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6288. nullpo_ret(sd);
  6289. if (sd->status.base_level == 0) // Is this something that possible?
  6290. return 0;
  6291. if (pc_is_maxbaselv(sd))
  6292. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6293. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6294. }
  6295. /**
  6296. * Job exp needed for player to level up.
  6297. * @param sd
  6298. * @return Job EXP needed for next job level
  6299. **/
  6300. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6301. nullpo_ret(sd);
  6302. if (sd->status.job_level == 0) // Is this something that possible?
  6303. return 0;
  6304. if (pc_is_maxjoblv(sd))
  6305. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6306. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6307. }
  6308. /// Returns the value of the specified stat.
  6309. static int pc_getstat(struct map_session_data* sd, int type)
  6310. {
  6311. nullpo_retr(-1, sd);
  6312. switch( type ) {
  6313. case SP_STR: return sd->status.str;
  6314. case SP_AGI: return sd->status.agi;
  6315. case SP_VIT: return sd->status.vit;
  6316. case SP_INT: return sd->status.int_;
  6317. case SP_DEX: return sd->status.dex;
  6318. case SP_LUK: return sd->status.luk;
  6319. default:
  6320. return -1;
  6321. }
  6322. }
  6323. /// Sets the specified stat to the specified value.
  6324. /// Returns the new value.
  6325. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6326. {
  6327. nullpo_retr(-1, sd);
  6328. switch( type ) {
  6329. case SP_STR: sd->status.str = val; break;
  6330. case SP_AGI: sd->status.agi = val; break;
  6331. case SP_VIT: sd->status.vit = val; break;
  6332. case SP_INT: sd->status.int_ = val; break;
  6333. case SP_DEX: sd->status.dex = val; break;
  6334. case SP_LUK: sd->status.luk = val; break;
  6335. default:
  6336. return -1;
  6337. }
  6338. return val;
  6339. }
  6340. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6341. int pc_gets_status_point(int level)
  6342. {
  6343. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6344. return (statp[level+1] - statp[level]);
  6345. else //Default increase
  6346. return ((level+15) / 5);
  6347. }
  6348. #ifdef RENEWAL_STAT
  6349. /// Renewal status point cost formula
  6350. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6351. #else
  6352. /// Pre-Renewal status point cost formula
  6353. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6354. #endif
  6355. /// Returns the number of stat points needed to change the specified stat by val.
  6356. /// If val is negative, returns the number of stat points that would be needed to
  6357. /// raise the specified stat from (current value - val) to current value.
  6358. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6359. {
  6360. int low, high, sp = 0, max = 0;
  6361. if ( val == 0 )
  6362. return 0;
  6363. low = pc_getstat(sd,type);
  6364. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6365. if ( low >= max && val > 0 )
  6366. return 0; // Official servers show '0' when max is reached
  6367. high = low + val;
  6368. if ( val < 0 )
  6369. SWAP(low, high);
  6370. for ( ; low < high; low++ )
  6371. sp += PC_STATUS_POINT_COST(low);
  6372. return sp;
  6373. }
  6374. /**
  6375. * Returns the value the specified stat can be increased by with the current
  6376. * amount of available status points for the current character's class.
  6377. *
  6378. * @param sd The target character.
  6379. * @param type Stat to verify.
  6380. * @return Maximum value the stat could grow by.
  6381. */
  6382. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6383. {
  6384. int base, final_val, status_points, max_param;
  6385. nullpo_ret(sd);
  6386. base = final_val = pc_getstat(sd, type);
  6387. status_points = sd->status.status_point;
  6388. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6389. while (final_val <= max_param && status_points >= 0) {
  6390. status_points -= PC_STATUS_POINT_COST(final_val);
  6391. final_val++;
  6392. }
  6393. final_val--;
  6394. return (final_val > base ? final_val-base : 0);
  6395. }
  6396. /**
  6397. * Raises a stat by the specified amount.
  6398. *
  6399. * Obeys max_parameter limits.
  6400. * Subtracts status points according to the cost of the increased stat points.
  6401. *
  6402. * @param sd The target character.
  6403. * @param type The stat to change (see enum _sp)
  6404. * @param increase The stat increase (strictly positive) amount.
  6405. * @retval true if the stat was increased by any amount.
  6406. * @retval false if there were no changes.
  6407. */
  6408. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6409. {
  6410. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6411. nullpo_ret(sd);
  6412. // check conditions
  6413. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6414. clif_statusupack(sd, type, 0, 0);
  6415. return false;
  6416. }
  6417. // check limits
  6418. current = pc_getstat(sd, type);
  6419. max_increase = pc_maxparameterincrease(sd, type);
  6420. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6421. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6422. clif_statusupack(sd, type, 0, 0);
  6423. return false;
  6424. }
  6425. // check status points
  6426. needed_points = pc_need_status_point(sd, type, increase);
  6427. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6428. clif_statusupack(sd, type, 0, 0);
  6429. return false;
  6430. }
  6431. // set new values
  6432. final_value = pc_setstat(sd, type, current + increase);
  6433. sd->status.status_point -= needed_points;
  6434. status_calc_pc(sd,SCO_NONE);
  6435. // update increase cost indicator
  6436. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6437. // update statpoint count
  6438. clif_updatestatus(sd, SP_STATUSPOINT);
  6439. // update stat value
  6440. clif_statusupack(sd, type, 1, final_value); // required
  6441. if( final_value > 255 )
  6442. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6443. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6444. return true;
  6445. }
  6446. /**
  6447. * Raises a stat by the specified amount.
  6448. *
  6449. * Obeys max_parameter limits.
  6450. * Does not subtract status points for the cost of the modified stat points.
  6451. *
  6452. * @param sd The target character.
  6453. * @param type The stat to change (see enum _sp)
  6454. * @param val The stat increase (or decrease) amount.
  6455. * @return the stat increase amount.
  6456. * @retval 0 if no changes were made.
  6457. */
  6458. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6459. {
  6460. int max, need;
  6461. nullpo_ret(sd);
  6462. if( type < SP_STR || type > SP_LUK )
  6463. {
  6464. clif_statusupack(sd,type,0,0);
  6465. return 0;
  6466. }
  6467. need = pc_need_status_point(sd,type,1);
  6468. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6469. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6470. status_calc_pc(sd,SCO_NONE);
  6471. // update increase cost indicator
  6472. if( need != pc_need_status_point(sd,type,1) )
  6473. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6474. // update stat value
  6475. clif_statusupack(sd,type,1,val); // required
  6476. if( val > 255 )
  6477. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6478. return val;
  6479. }
  6480. /*==========================================
  6481. * Update skill_lv for player sd
  6482. * Skill point allocation
  6483. *------------------------------------------*/
  6484. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6485. {
  6486. uint16 idx = skill_get_index(skill_id);
  6487. nullpo_retv(sd);
  6488. if (!idx) {
  6489. if (skill_id)
  6490. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6491. return;
  6492. }
  6493. // Level up guild skill
  6494. if (SKILL_CHK_GUILD(skill_id)) {
  6495. guild_skillup(sd, skill_id);
  6496. return;
  6497. }
  6498. // Level up homunculus skill
  6499. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6500. hom_skillup(sd->hd, skill_id);
  6501. return;
  6502. }
  6503. else {
  6504. if( sd->status.skill_point > 0 &&
  6505. sd->status.skill[idx].id &&
  6506. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6507. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6508. {
  6509. int lv, range, upgradable;
  6510. sd->status.skill[idx].lv++;
  6511. sd->status.skill_point--;
  6512. if( !skill_get_inf(skill_id) )
  6513. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6514. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6515. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6516. else
  6517. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6518. lv = sd->status.skill[idx].lv;
  6519. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6520. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6521. clif_skillup(sd,skill_id,lv,range,upgradable);
  6522. clif_updatestatus(sd,SP_SKILLPOINT);
  6523. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6524. clif_updatestatus(sd,SP_CARTINFO);
  6525. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6526. clif_skillinfoblock(sd);
  6527. }
  6528. //else
  6529. // ShowDebug("Skill Level up failed. ID:%d idx:%d (CID=%d. AID=%d)\n", skill_id, idx, sd->status.char_id, sd->status.account_id);
  6530. }
  6531. }
  6532. /*==========================================
  6533. * /allskill
  6534. *------------------------------------------*/
  6535. int pc_allskillup(struct map_session_data *sd)
  6536. {
  6537. int i;
  6538. nullpo_ret(sd);
  6539. for (i = 0; i < MAX_SKILL; i++) {
  6540. 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) {
  6541. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6542. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6543. if (sd->status.skill[i].lv == 0)
  6544. sd->status.skill[i].id = 0;
  6545. }
  6546. }
  6547. if (!pc_grant_allskills(sd, true)) {
  6548. uint16 sk_id;
  6549. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6550. int inf2 = 0;
  6551. uint16 sk_idx = 0;
  6552. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6553. continue;
  6554. inf2 = skill_get_inf2(sk_id);
  6555. if (
  6556. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6557. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6558. sk_id == SG_DEVIL
  6559. )
  6560. continue; //Cannot be learned normally.
  6561. sd->status.skill[sk_idx].id = sk_id;
  6562. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6563. }
  6564. }
  6565. status_calc_pc(sd,SCO_NONE);
  6566. //Required because if you could level up all skills previously,
  6567. //the update will not be sent as only the lv variable changes.
  6568. clif_skillinfoblock(sd);
  6569. return 0;
  6570. }
  6571. /*==========================================
  6572. * /resetlvl
  6573. *------------------------------------------*/
  6574. int pc_resetlvl(struct map_session_data* sd,int type)
  6575. {
  6576. int i;
  6577. nullpo_ret(sd);
  6578. if (type != 3) //Also reset skills
  6579. pc_resetskill(sd, 0);
  6580. if(type == 1){
  6581. sd->status.skill_point=0;
  6582. sd->status.base_level=1;
  6583. sd->status.job_level=1;
  6584. sd->status.base_exp=0;
  6585. sd->status.job_exp=0;
  6586. if(sd->sc.option !=0)
  6587. sd->sc.option = 0;
  6588. sd->status.str=1;
  6589. sd->status.agi=1;
  6590. sd->status.vit=1;
  6591. sd->status.int_=1;
  6592. sd->status.dex=1;
  6593. sd->status.luk=1;
  6594. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6595. sd->status.status_point=100; // not 88 [celest]
  6596. // give platinum skills upon changing
  6597. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6598. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6599. }
  6600. }
  6601. if(type == 2){
  6602. sd->status.skill_point=0;
  6603. sd->status.base_level=1;
  6604. sd->status.job_level=1;
  6605. sd->status.base_exp=0;
  6606. sd->status.job_exp=0;
  6607. }
  6608. if(type == 3){
  6609. sd->status.base_level=1;
  6610. sd->status.base_exp=0;
  6611. }
  6612. if(type == 4){
  6613. sd->status.job_level=1;
  6614. sd->status.job_exp=0;
  6615. }
  6616. clif_updatestatus(sd,SP_STATUSPOINT);
  6617. clif_updatestatus(sd,SP_STR);
  6618. clif_updatestatus(sd,SP_AGI);
  6619. clif_updatestatus(sd,SP_VIT);
  6620. clif_updatestatus(sd,SP_INT);
  6621. clif_updatestatus(sd,SP_DEX);
  6622. clif_updatestatus(sd,SP_LUK);
  6623. clif_updatestatus(sd,SP_BASELEVEL);
  6624. clif_updatestatus(sd,SP_JOBLEVEL);
  6625. clif_updatestatus(sd,SP_STATUSPOINT);
  6626. clif_updatestatus(sd,SP_BASEEXP);
  6627. clif_updatestatus(sd,SP_JOBEXP);
  6628. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6629. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6630. clif_updatestatus(sd,SP_SKILLPOINT);
  6631. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6632. clif_updatestatus(sd,SP_UAGI);
  6633. clif_updatestatus(sd,SP_UVIT);
  6634. clif_updatestatus(sd,SP_UINT);
  6635. clif_updatestatus(sd,SP_UDEX);
  6636. clif_updatestatus(sd,SP_ULUK); // End Addition
  6637. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6638. if(sd->equip_index[i] >= 0)
  6639. if(pc_isequip(sd,sd->equip_index[i]))
  6640. pc_unequipitem(sd,sd->equip_index[i],2);
  6641. }
  6642. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6643. party_send_levelup(sd);
  6644. status_calc_pc(sd, SCO_FORCE);
  6645. clif_skillinfoblock(sd);
  6646. return 0;
  6647. }
  6648. /*==========================================
  6649. * /resetstate
  6650. *------------------------------------------*/
  6651. int pc_resetstate(struct map_session_data* sd)
  6652. {
  6653. nullpo_ret(sd);
  6654. if (battle_config.use_statpoint_table)
  6655. { // New statpoint table used here - Dexity
  6656. if (sd->status.base_level > MAX_LEVEL)
  6657. { //statp[] goes out of bounds, can't reset!
  6658. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6659. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6660. return 0;
  6661. }
  6662. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6663. }
  6664. else
  6665. {
  6666. int add=0;
  6667. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6668. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6669. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6670. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6671. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6672. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6673. sd->status.status_point+=add;
  6674. }
  6675. pc_setstat(sd, SP_STR, 1);
  6676. pc_setstat(sd, SP_AGI, 1);
  6677. pc_setstat(sd, SP_VIT, 1);
  6678. pc_setstat(sd, SP_INT, 1);
  6679. pc_setstat(sd, SP_DEX, 1);
  6680. pc_setstat(sd, SP_LUK, 1);
  6681. clif_updatestatus(sd,SP_STR);
  6682. clif_updatestatus(sd,SP_AGI);
  6683. clif_updatestatus(sd,SP_VIT);
  6684. clif_updatestatus(sd,SP_INT);
  6685. clif_updatestatus(sd,SP_DEX);
  6686. clif_updatestatus(sd,SP_LUK);
  6687. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6688. clif_updatestatus(sd,SP_UAGI);
  6689. clif_updatestatus(sd,SP_UVIT);
  6690. clif_updatestatus(sd,SP_UINT);
  6691. clif_updatestatus(sd,SP_UDEX);
  6692. clif_updatestatus(sd,SP_ULUK); // End Addition
  6693. clif_updatestatus(sd,SP_STATUSPOINT);
  6694. if( sd->mission_mobid ) { //bugreport:2200
  6695. sd->mission_mobid = 0;
  6696. sd->mission_count = 0;
  6697. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6698. }
  6699. status_calc_pc(sd, SCO_NONE);
  6700. return 1;
  6701. }
  6702. /*==========================================
  6703. * /resetskill
  6704. * if flag&1, perform block resync and status_calc call.
  6705. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6706. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6707. *------------------------------------------*/
  6708. int pc_resetskill(struct map_session_data* sd, int flag)
  6709. {
  6710. int i, skill_point=0;
  6711. nullpo_ret(sd);
  6712. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6713. return 0;
  6714. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6715. /**
  6716. * It has been confirmed on official servers that when you reset skills with a ranked Taekwon your skills are not reset (because you have all of them anyway)
  6717. **/
  6718. if( pc_is_taekwon_ranker(sd) )
  6719. return 0;
  6720. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6721. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6722. i = sd->sc.option;
  6723. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6724. i &= ~OPTION_RIDING;
  6725. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6726. i &= ~OPTION_FALCON;
  6727. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6728. i &= ~OPTION_DRAGON;
  6729. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6730. i &= ~OPTION_WUG;
  6731. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6732. i &= ~OPTION_WUGRIDER;
  6733. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6734. i &= ~OPTION_MADOGEAR;
  6735. #ifndef NEW_CARTS
  6736. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6737. i &= ~OPTION_CART;
  6738. #else
  6739. if( sd->sc.data[SC_PUSH_CART] )
  6740. pc_setcart(sd, 0);
  6741. #endif
  6742. if( i != sd->sc.option )
  6743. pc_setoption(sd, i);
  6744. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6745. hom_vaporize(sd, HOM_ST_ACTIVE);
  6746. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6747. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6748. }
  6749. for( i = 1; i < MAX_SKILL; i++ )
  6750. {
  6751. uint8 lv = sd->status.skill[i].lv;
  6752. int inf2;
  6753. uint16 skill_id = skill_idx2id(i);
  6754. if (lv == 0 || skill_id == 0)
  6755. continue;
  6756. inf2 = skill_get_inf2(skill_id);
  6757. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6758. continue;
  6759. // Don't reset trick dead if not a novice/baby
  6760. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6761. {
  6762. sd->status.skill[i].lv = 0;
  6763. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6764. continue;
  6765. }
  6766. // do not reset basic skill
  6767. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6768. continue;
  6769. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6770. continue;
  6771. if( flag&4 && !skill_ischangesex(skill_id) )
  6772. continue;
  6773. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6774. { //Only handle quest skills in a special way when you can't learn them manually
  6775. if( battle_config.quest_skill_reset && !(flag&2) )
  6776. { //Wipe them
  6777. sd->status.skill[i].lv = 0;
  6778. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6779. }
  6780. continue;
  6781. }
  6782. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6783. skill_point += lv;
  6784. else
  6785. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6786. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6787. if( !(flag&2) )
  6788. {// reset
  6789. sd->status.skill[i].lv = 0;
  6790. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6791. }
  6792. }
  6793. if( flag&2 || !skill_point ) return skill_point;
  6794. sd->status.skill_point += skill_point;
  6795. if (flag&1) {
  6796. clif_updatestatus(sd,SP_SKILLPOINT);
  6797. clif_skillinfoblock(sd);
  6798. status_calc_pc(sd, SCO_FORCE);
  6799. }
  6800. return skill_point;
  6801. }
  6802. /*==========================================
  6803. * /resetfeel [Komurka]
  6804. *------------------------------------------*/
  6805. int pc_resetfeel(struct map_session_data* sd)
  6806. {
  6807. int i;
  6808. nullpo_ret(sd);
  6809. for (i=0; i<MAX_PC_FEELHATE; i++)
  6810. {
  6811. sd->feel_map[i].m = -1;
  6812. sd->feel_map[i].index = 0;
  6813. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6814. }
  6815. return 0;
  6816. }
  6817. int pc_resethate(struct map_session_data* sd)
  6818. {
  6819. int i;
  6820. nullpo_ret(sd);
  6821. for (i=0; i<3; i++)
  6822. {
  6823. sd->hate_mob[i] = -1;
  6824. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6825. }
  6826. return 0;
  6827. }
  6828. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6829. {
  6830. int bonus = 0;
  6831. nullpo_ret(sd);
  6832. skill_id = skill_dummy2skill_id(skill_id);
  6833. for (auto &it : sd->skillatk) {
  6834. if (it.id == skill_id) {
  6835. bonus += it.val;
  6836. break;
  6837. }
  6838. }
  6839. return bonus;
  6840. }
  6841. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6842. {
  6843. int bonus = 0;
  6844. nullpo_ret(sd);
  6845. skill_id = skill_dummy2skill_id(skill_id);
  6846. for (auto &it : sd->subskill) {
  6847. if (it.id == skill_id) {
  6848. bonus += it.val;
  6849. break;
  6850. }
  6851. }
  6852. return bonus;
  6853. }
  6854. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6855. int bonus = sd->bonus.add_heal_rate;
  6856. nullpo_ret(sd);
  6857. skill_id = skill_dummy2skill_id(skill_id);
  6858. if( bonus ) {
  6859. switch( skill_id ) {
  6860. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6861. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6862. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6863. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6864. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6865. }
  6866. }
  6867. for (auto &it : sd->skillheal) {
  6868. if (it.id == skill_id) {
  6869. bonus += it.val;
  6870. break;
  6871. }
  6872. }
  6873. return bonus;
  6874. }
  6875. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6876. int bonus = sd->bonus.add_heal2_rate;
  6877. skill_id = skill_dummy2skill_id(skill_id);
  6878. for (auto &it : sd->skillheal2) {
  6879. if (it.id == skill_id) {
  6880. bonus += it.val;
  6881. break;
  6882. }
  6883. }
  6884. return bonus;
  6885. }
  6886. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6887. {
  6888. if( !pc_isdead(sd) )
  6889. return; // not applicable
  6890. if( sd->bg_id && bg_member_respawn(sd) )
  6891. return; // member revived by battleground
  6892. pc_setstand(sd, true);
  6893. pc_setrestartvalue(sd,3);
  6894. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6895. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6896. }
  6897. static TIMER_FUNC(pc_respawn_timer){
  6898. struct map_session_data *sd = map_id2sd(id);
  6899. if( sd != NULL )
  6900. {
  6901. sd->pvp_point=0;
  6902. sd->respawn_tid = INVALID_TIMER;
  6903. pc_respawn(sd,CLR_OUTSIGHT);
  6904. }
  6905. return 0;
  6906. }
  6907. /*==========================================
  6908. * Invoked when a player has received damage
  6909. *------------------------------------------*/
  6910. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6911. {
  6912. if (sp) clif_updatestatus(sd,SP_SP);
  6913. if (hp) clif_updatestatus(sd,SP_HP);
  6914. else return;
  6915. if (!src)
  6916. return;
  6917. if( pc_issit(sd) ) {
  6918. pc_setstand(sd, true);
  6919. skill_sit(sd,0);
  6920. }
  6921. if (sd->progressbar.npc_id)
  6922. clif_progressbar_abort(sd);
  6923. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6924. pet_target_check(sd->pd,src,1);
  6925. if( sd->status.ele_id > 0 )
  6926. elemental_set_target(sd,src);
  6927. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6928. sd->canlog_tick = gettick();
  6929. }
  6930. TIMER_FUNC(pc_close_npc_timer){
  6931. TBL_PC *sd = map_id2sd(id);
  6932. if(sd) pc_close_npc(sd,data);
  6933. return 0;
  6934. }
  6935. /**
  6936. * Method to properly close a NPC for player and clear anything related.
  6937. * @param sd: Player attached
  6938. * @param flag: Method of closure
  6939. * 1: Produce a close button and end the NPC
  6940. * 2: End the NPC (best for no dialog windows)
  6941. */
  6942. void pc_close_npc(struct map_session_data *sd,int flag)
  6943. {
  6944. nullpo_retv(sd);
  6945. if (sd->npc_id || sd->npc_shopid) {
  6946. if (sd->state.using_fake_npc) {
  6947. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6948. sd->state.using_fake_npc = 0;
  6949. }
  6950. if (sd->st) {
  6951. if(sd->st->state == RUN){ //wait ending code execution
  6952. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6953. return;
  6954. }
  6955. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6956. sd->st->mes_active = 0;
  6957. }
  6958. sd->state.menu_or_input = 0;
  6959. sd->npc_menu = 0;
  6960. sd->npc_shopid = 0;
  6961. #ifdef SECURE_NPCTIMEOUT
  6962. sd->npc_idle_timer = INVALID_TIMER;
  6963. #endif
  6964. if (sd->st) {
  6965. if (sd->st->state == CLOSE) {
  6966. clif_scriptclose(sd, sd->npc_id);
  6967. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6968. sd->st->state = END; // Force to end now
  6969. }
  6970. if (sd->st->state == END) { // free attached scripts that are waiting
  6971. script_free_state(sd->st);
  6972. sd->st = NULL;
  6973. sd->npc_id = 0;
  6974. }
  6975. }
  6976. }
  6977. }
  6978. /*==========================================
  6979. * Invoked when a player has negative current hp
  6980. *------------------------------------------*/
  6981. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6982. {
  6983. int i=0,k=0;
  6984. t_tick tick = gettick();
  6985. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6986. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6987. // Super Novices have no kill or die functions attached when saved by their angel
  6988. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6989. unsigned int exp = pc_nextbaseexp(sd);
  6990. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6991. sd->state.snovice_dead_flag = 1;
  6992. pc_setrestartvalue(sd,1);
  6993. status_percent_heal(&sd->bl, 100, 100);
  6994. clif_resurrection(&sd->bl, 1);
  6995. if(battle_config.pc_invincible_time)
  6996. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6997. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6998. if(mapdata_flag_gvg2(mapdata))
  6999. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7000. return 0;
  7001. }
  7002. }
  7003. for(k = 0; k < MAX_DEVOTION; k++) {
  7004. if (sd->devotion[k]){
  7005. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7006. if (devsd)
  7007. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7008. sd->devotion[k] = 0;
  7009. }
  7010. }
  7011. if(sd->shadowform_id) { //if we were target of shadowform
  7012. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7013. sd->shadowform_id = 0; //should be remove on status end anyway
  7014. }
  7015. if(sd->status.pet_id > 0 && sd->pd) {
  7016. struct pet_data *pd = sd->pd;
  7017. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7018. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7019. if( pd->pet.intimate < 0 )
  7020. pd->pet.intimate = 0;
  7021. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7022. }
  7023. if( sd->pd->target_id ) // Unlock all targets...
  7024. pet_unlocktarget(sd->pd);
  7025. }
  7026. if (hom_is_active(sd->hd) && battle_config.homunculus_auto_vapor && get_percentage(sd->hd->battle_status.hp, sd->hd->battle_status.max_hp) >= battle_config.homunculus_auto_vapor)
  7027. hom_vaporize(sd, HOM_ST_ACTIVE);
  7028. if( sd->md )
  7029. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7030. if( sd->ed )
  7031. elemental_delete(sd->ed);
  7032. // Leave duel if you die [LuzZza]
  7033. if(battle_config.duel_autoleave_when_die) {
  7034. if(sd->duel_group > 0)
  7035. duel_leave(sd->duel_group, sd);
  7036. if(sd->duel_invite > 0)
  7037. duel_reject(sd->duel_invite, sd);
  7038. }
  7039. pc_close_npc(sd,2); //close npc if we were using one
  7040. /* e.g. not killed thru pc_damage */
  7041. if( pc_issit(sd) ) {
  7042. clif_status_load(&sd->bl,EFST_SIT,0);
  7043. }
  7044. pc_setdead(sd);
  7045. clif_party_dead( sd );
  7046. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7047. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7048. //Reset menu skills/item skills
  7049. if ((sd->skillitem) != 0)
  7050. sd->skillitem = sd->skillitemlv = 0;
  7051. if ((sd->menuskill_id) != 0)
  7052. sd->menuskill_id = sd->menuskill_val = 0;
  7053. //Reset ticks.
  7054. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7055. if ( sd->spiritball !=0 )
  7056. pc_delspiritball(sd,sd->spiritball,0);
  7057. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7058. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7059. if (src)
  7060. switch (src->type) {
  7061. case BL_MOB:
  7062. {
  7063. struct mob_data *md=(struct mob_data *)src;
  7064. if(md->target_id==sd->bl.id)
  7065. mob_unlocktarget(md,tick);
  7066. if(battle_config.mobs_level_up && md->status.hp &&
  7067. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7068. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7069. ) { // monster level up [Valaris]
  7070. clif_misceffect(&md->bl,0);
  7071. md->level++;
  7072. status_calc_mob(md, SCO_NONE);
  7073. status_percent_heal(src,10,0);
  7074. if( battle_config.show_mob_info&4 )
  7075. {// update name with new level
  7076. clif_name_area(&md->bl);
  7077. }
  7078. }
  7079. src = battle_get_master(src); // Maybe Player Summon
  7080. }
  7081. break;
  7082. case BL_PET: //Pass on to master...
  7083. case BL_HOM:
  7084. case BL_MER:
  7085. src = battle_get_master(src);
  7086. break;
  7087. }
  7088. if (src && src->type == BL_PC) {
  7089. struct map_session_data *ssd = (struct map_session_data *)src;
  7090. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7091. npc_script_event(ssd, NPCE_KILLPC);
  7092. if (battle_config.pk_mode&2) {
  7093. ssd->status.manner -= 5;
  7094. if(ssd->status.manner < 0)
  7095. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7096. #if 0
  7097. // PK/Karma system code (not enabled yet) [celest]
  7098. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7099. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7100. // karma going down = more 'good' / more honourable.
  7101. // The Karma System way...
  7102. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7103. sd->status.karma--;
  7104. ssd->status.karma--;
  7105. }
  7106. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7107. ssd->status.karma++;
  7108. // or the PK System way...
  7109. if (sd->status.karma > 0) // player killed is dishonourable?
  7110. ssd->status.karma--; // honour points earned
  7111. sd->status.karma++; // honour points lost
  7112. // To-do: Receive exp on certain occasions
  7113. #endif
  7114. }
  7115. }
  7116. if(battle_config.bone_drop==2
  7117. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7118. {
  7119. struct item item_tmp;
  7120. memset(&item_tmp,0,sizeof(item_tmp));
  7121. item_tmp.nameid=ITEMID_SKULL_;
  7122. item_tmp.identify=1;
  7123. item_tmp.card[0]=CARD0_CREATE;
  7124. item_tmp.card[1]=0;
  7125. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7126. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7127. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7128. }
  7129. //Remove bonus_script when dead
  7130. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7131. // changed penalty options, added death by player if pk_mode [Valaris]
  7132. if(battle_config.death_penalty_type
  7133. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7134. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7135. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7136. {
  7137. uint32 base_penalty = 0;
  7138. uint32 job_penalty = 0;
  7139. uint32 zeny_penalty = 0;
  7140. if (pc_isvip(sd)) { // EXP penalty for VIP
  7141. base_penalty = battle_config.vip_exp_penalty_base;
  7142. job_penalty = battle_config.vip_exp_penalty_job;
  7143. zeny_penalty = battle_config.vip_zeny_penalty;
  7144. } else {
  7145. base_penalty = battle_config.death_penalty_base;
  7146. job_penalty = battle_config.death_penalty_job;
  7147. zeny_penalty = battle_config.zeny_penalty;
  7148. }
  7149. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7150. switch (battle_config.death_penalty_type) {
  7151. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7152. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7153. }
  7154. if (base_penalty){ //recheck after altering to speedup
  7155. if (battle_config.pk_mode && src && src->type==BL_PC)
  7156. base_penalty *= 2;
  7157. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7158. }
  7159. }
  7160. else
  7161. base_penalty = 0;
  7162. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7163. switch (battle_config.death_penalty_type) {
  7164. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7165. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7166. }
  7167. if (job_penalty) {
  7168. if (battle_config.pk_mode && src && src->type==BL_PC)
  7169. job_penalty *= 2;
  7170. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7171. }
  7172. }
  7173. else
  7174. job_penalty = 0;
  7175. if (base_penalty || job_penalty)
  7176. pc_lostexp(sd, base_penalty, job_penalty);
  7177. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7178. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7179. if(zeny_penalty)
  7180. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7181. }
  7182. }
  7183. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7184. for (const auto &it : mapdata->drop_list) {
  7185. int id = it.drop_id, per = it.drop_per;
  7186. enum e_nightmare_drop_type type = it.drop_type;
  7187. if(id == 0)
  7188. continue;
  7189. if(id == -1){
  7190. int eq_num=0,eq_n[MAX_INVENTORY];
  7191. memset(eq_n,0,sizeof(eq_n));
  7192. for(i=0;i<MAX_INVENTORY;i++) {
  7193. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7194. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7195. || type&NMDT_ALL)
  7196. {
  7197. int l;
  7198. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7199. if( l < MAX_INVENTORY )
  7200. eq_n[l] = i;
  7201. eq_num++;
  7202. }
  7203. }
  7204. if(eq_num > 0){
  7205. int n = eq_n[rnd()%eq_num];
  7206. if(rnd()%10000 < per) {
  7207. if(sd->inventory.u.items_inventory[n].equip)
  7208. pc_unequipitem(sd,n,3);
  7209. pc_dropitem(sd,n,1);
  7210. }
  7211. }
  7212. }
  7213. else if(id > 0) {
  7214. for(i=0;i<MAX_INVENTORY;i++){
  7215. if(sd->inventory.u.items_inventory[i].nameid == id
  7216. && rnd()%10000 < per
  7217. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7218. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7219. || type&NMDT_ALL) ){
  7220. if(sd->inventory.u.items_inventory[i].equip)
  7221. pc_unequipitem(sd,i,3);
  7222. pc_dropitem(sd,i,1);
  7223. break;
  7224. }
  7225. }
  7226. }
  7227. }
  7228. }
  7229. // pvp
  7230. // disable certain pvp functions on pk_mode [Valaris]
  7231. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7232. sd->pvp_point -= 5;
  7233. sd->pvp_lost++;
  7234. if( src && src->type == BL_PC ) {
  7235. struct map_session_data *ssd = (struct map_session_data *)src;
  7236. ssd->pvp_point++;
  7237. ssd->pvp_won++;
  7238. }
  7239. if( sd->pvp_point < 0 ) {
  7240. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7241. return 1|8;
  7242. }
  7243. }
  7244. //GvG
  7245. if( mapdata_flag_gvg2(mapdata) ) {
  7246. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7247. return 1|8;
  7248. }
  7249. else if( sd->bg_id ) {
  7250. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7251. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7252. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7253. return 1|8;
  7254. }
  7255. }
  7256. //Reset "can log out" tick.
  7257. if( battle_config.prevent_logout )
  7258. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7259. return 1;
  7260. }
  7261. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7262. if(hp) clif_updatestatus(sd,SP_HP);
  7263. if(sp) clif_updatestatus(sd,SP_SP);
  7264. pc_setstand(sd, true);
  7265. if(battle_config.pc_invincible_time > 0)
  7266. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7267. if( sd->state.gmaster_flag ) {
  7268. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7269. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7270. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7271. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7272. }
  7273. }
  7274. bool pc_revive_item(struct map_session_data *sd) {
  7275. nullpo_retr(false, sd);
  7276. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7277. return false;
  7278. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7279. return false;
  7280. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7281. uint8 hp = 100, sp = 100;
  7282. if (item_position < 0) {
  7283. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7284. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7285. sp = 0;
  7286. }
  7287. else
  7288. return false;
  7289. }
  7290. if (!status_revive(&sd->bl, hp, sp))
  7291. return false;
  7292. if (item_position < 0)
  7293. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7294. else
  7295. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7296. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7297. return true;
  7298. }
  7299. // script
  7300. //
  7301. /*==========================================
  7302. * script reading pc status registry
  7303. *------------------------------------------*/
  7304. int pc_readparam(struct map_session_data* sd,int type)
  7305. {
  7306. int val = 0;
  7307. nullpo_ret(sd);
  7308. switch(type) {
  7309. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7310. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7311. case SP_ZENY: val = sd->status.zeny; break;
  7312. case SP_BASELEVEL: val = sd->status.base_level; break;
  7313. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7314. case SP_CLASS: val = sd->status.class_; break;
  7315. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7316. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7317. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7318. case SP_SEX: val = sd->status.sex; break;
  7319. case SP_WEIGHT: val = sd->weight; break;
  7320. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7321. case SP_BASEEXP: val = sd->status.base_exp; break;
  7322. case SP_JOBEXP: val = sd->status.job_exp; break;
  7323. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7324. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7325. case SP_HP: val = sd->battle_status.hp; break;
  7326. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7327. case SP_SP: val = sd->battle_status.sp; break;
  7328. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7329. case SP_STR: val = sd->status.str; break;
  7330. case SP_AGI: val = sd->status.agi; break;
  7331. case SP_VIT: val = sd->status.vit; break;
  7332. case SP_INT: val = sd->status.int_; break;
  7333. case SP_DEX: val = sd->status.dex; break;
  7334. case SP_LUK: val = sd->status.luk; break;
  7335. case SP_KARMA: val = sd->status.karma; break;
  7336. case SP_MANNER: val = sd->status.manner; break;
  7337. case SP_FAME: val = sd->status.fame; break;
  7338. case SP_KILLERRID: val = sd->killerrid; break;
  7339. case SP_KILLEDRID: val = sd->killedrid; break;
  7340. case SP_KILLEDGID: val = sd->killedgid; break;
  7341. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7342. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7343. case SP_CHARRENAME: val = sd->status.rename; break;
  7344. case SP_CHARFONT: val = sd->status.font; break;
  7345. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7346. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7347. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7348. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7349. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7350. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7351. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7352. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7353. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7354. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7355. case SP_BASE_ATK:
  7356. #ifdef RENEWAL
  7357. val = sd->bonus.eatk;
  7358. #else
  7359. val = sd->battle_status.batk;
  7360. #endif
  7361. break;
  7362. case SP_DEF1: val = sd->battle_status.def; break;
  7363. case SP_DEF2: val = sd->battle_status.def2; break;
  7364. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7365. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7366. case SP_HIT: val = sd->battle_status.hit; break;
  7367. case SP_FLEE1: val = sd->battle_status.flee; break;
  7368. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7369. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7370. case SP_MAXHPRATE: val = sd->hprate; break;
  7371. case SP_MAXSPRATE: val = sd->sprate; break;
  7372. case SP_SPRATE: val = sd->dsprate; break;
  7373. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7374. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7375. case SP_ASPD_RATE:
  7376. #ifndef RENEWAL_ASPD
  7377. val = sd->battle_status.aspd_rate;
  7378. #else
  7379. val = sd->battle_status.aspd_rate2;
  7380. #endif
  7381. break;
  7382. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7383. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7384. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7385. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7386. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7387. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7388. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7389. case SP_MATK_RATE: val = sd->matk_rate; break;
  7390. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7391. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7392. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7393. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7394. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7395. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7396. case SP_HIT_RATE: val = sd->hit_rate; break;
  7397. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7398. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7399. case SP_DEF_RATE: val = sd->def_rate; break;
  7400. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7401. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7402. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7403. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7404. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7405. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7406. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7407. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7408. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7409. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7410. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7411. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7412. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7413. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7414. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7415. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7416. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7417. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7418. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7419. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7420. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7421. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7422. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7423. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7424. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7425. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7426. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7427. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7428. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7429. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7430. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7431. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7432. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7433. case SP_DELAYRATE: val = sd->delayrate; break;
  7434. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7435. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7436. case SP_UNSTRIPABLE:
  7437. case SP_UNSTRIPABLE_ARMOR:
  7438. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7439. break;
  7440. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7441. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7442. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7443. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7444. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7445. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7446. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7447. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7448. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7449. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7450. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7451. case SP_EMATK: val = sd->bonus.ematk; break;
  7452. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7453. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7454. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7455. case SP_CASTRATE:
  7456. case SP_VARCASTRATE:
  7457. #ifdef RENEWAL_CAST
  7458. val = sd->bonus.varcastrate; break;
  7459. #else
  7460. val = sd->castrate; break;
  7461. #endif
  7462. default:
  7463. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7464. return -1;
  7465. }
  7466. return val;
  7467. }
  7468. /*==========================================
  7469. * script set pc status registry
  7470. *------------------------------------------*/
  7471. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7472. {
  7473. nullpo_retr(false,sd);
  7474. switch(type){
  7475. case SP_BASELEVEL:
  7476. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7477. val = pc_maxbaselv(sd);
  7478. if ((unsigned int)val > sd->status.base_level) {
  7479. int i = 0;
  7480. int stat=0;
  7481. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7482. stat += pc_gets_status_point(sd->status.base_level + i);
  7483. sd->status.status_point += stat;
  7484. }
  7485. sd->status.base_level = (unsigned int)val;
  7486. sd->status.base_exp = 0;
  7487. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7488. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7489. clif_updatestatus(sd, SP_STATUSPOINT);
  7490. clif_updatestatus(sd, SP_BASEEXP);
  7491. status_calc_pc(sd, SCO_FORCE);
  7492. if(sd->status.party_id)
  7493. party_send_levelup(sd);
  7494. break;
  7495. case SP_JOBLEVEL:
  7496. if ((unsigned int)val >= sd->status.job_level) {
  7497. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7498. sd->status.skill_point += val - sd->status.job_level;
  7499. clif_updatestatus(sd, SP_SKILLPOINT);
  7500. }
  7501. sd->status.job_level = (unsigned int)val;
  7502. sd->status.job_exp = 0;
  7503. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7504. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7505. clif_updatestatus(sd, SP_JOBEXP);
  7506. status_calc_pc(sd, SCO_FORCE);
  7507. break;
  7508. case SP_SKILLPOINT:
  7509. sd->status.skill_point = val;
  7510. break;
  7511. case SP_STATUSPOINT:
  7512. sd->status.status_point = val;
  7513. break;
  7514. case SP_ZENY:
  7515. if( val < 0 )
  7516. return false;// can't set negative zeny
  7517. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7518. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7519. break;
  7520. case SP_BASEEXP:
  7521. {
  7522. val = cap_value(val, 0, INT_MAX);
  7523. if ((unsigned int)val < sd->status.base_exp) // Lost
  7524. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7525. else // Gained
  7526. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7527. }
  7528. return true;
  7529. case SP_JOBEXP:
  7530. {
  7531. val = cap_value(val, 0, INT_MAX);
  7532. if ((unsigned int)val < sd->status.job_exp) // Lost
  7533. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7534. else // Gained
  7535. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7536. }
  7537. return true;
  7538. case SP_SEX:
  7539. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7540. break;
  7541. case SP_WEIGHT:
  7542. sd->weight = val;
  7543. break;
  7544. case SP_MAXWEIGHT:
  7545. sd->max_weight = val;
  7546. break;
  7547. case SP_HP:
  7548. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7549. break;
  7550. case SP_MAXHP:
  7551. if (sd->status.base_level < 100)
  7552. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7553. else if (sd->status.base_level < 151)
  7554. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7555. else
  7556. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7557. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7558. {
  7559. sd->battle_status.hp = sd->battle_status.max_hp;
  7560. clif_updatestatus(sd, SP_HP);
  7561. }
  7562. break;
  7563. case SP_SP:
  7564. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7565. break;
  7566. case SP_MAXSP:
  7567. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7568. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7569. {
  7570. sd->battle_status.sp = sd->battle_status.max_sp;
  7571. clif_updatestatus(sd, SP_SP);
  7572. }
  7573. break;
  7574. case SP_STR:
  7575. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7576. break;
  7577. case SP_AGI:
  7578. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7579. break;
  7580. case SP_VIT:
  7581. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7582. break;
  7583. case SP_INT:
  7584. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7585. break;
  7586. case SP_DEX:
  7587. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7588. break;
  7589. case SP_LUK:
  7590. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7591. break;
  7592. case SP_KARMA:
  7593. sd->status.karma = val;
  7594. break;
  7595. case SP_MANNER:
  7596. sd->status.manner = val;
  7597. if( val < 0 )
  7598. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7599. else {
  7600. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7601. clif_manner_message(sd, 5);
  7602. }
  7603. return true; // status_change_start/status_change_end already sends packets warning the client
  7604. case SP_FAME:
  7605. sd->status.fame = val;
  7606. break;
  7607. case SP_KILLERRID:
  7608. sd->killerrid = val;
  7609. return true;
  7610. case SP_KILLEDRID:
  7611. sd->killedrid = val;
  7612. return true;
  7613. case SP_KILLEDGID:
  7614. sd->killedgid = val;
  7615. return true;
  7616. case SP_CHARMOVE:
  7617. sd->status.character_moves = val;
  7618. return true;
  7619. case SP_CHARRENAME:
  7620. sd->status.rename = val;
  7621. return true;
  7622. case SP_CHARFONT:
  7623. sd->status.font = val;
  7624. clif_font(sd);
  7625. return true;
  7626. case SP_BANK_VAULT:
  7627. if (val < 0)
  7628. return false;
  7629. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7630. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7631. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7632. return true;
  7633. case SP_ROULETTE_BRONZE:
  7634. sd->roulette_point.bronze = val;
  7635. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7636. return true;
  7637. case SP_ROULETTE_SILVER:
  7638. sd->roulette_point.silver = val;
  7639. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7640. return true;
  7641. case SP_ROULETTE_GOLD:
  7642. sd->roulette_point.gold = val;
  7643. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7644. return true;
  7645. case SP_CASHPOINTS:
  7646. if (val < 0)
  7647. return false;
  7648. if (!sd->state.connect_new)
  7649. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7650. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7651. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7652. return true;
  7653. case SP_KAFRAPOINTS:
  7654. if (val < 0)
  7655. return false;
  7656. if (!sd->state.connect_new)
  7657. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7658. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7659. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7660. return true;
  7661. case SP_PCDIECOUNTER:
  7662. if (val < 0)
  7663. return false;
  7664. if (sd->die_counter == val)
  7665. return true;
  7666. sd->die_counter = val;
  7667. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7668. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7669. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7670. return true;
  7671. case SP_COOKMASTERY:
  7672. if (val < 0)
  7673. return false;
  7674. if (sd->cook_mastery == val)
  7675. return true;
  7676. val = cap_value(val, 0, 1999);
  7677. sd->cook_mastery = val;
  7678. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7679. return true;
  7680. default:
  7681. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7682. return false;
  7683. }
  7684. clif_updatestatus(sd,type);
  7685. return true;
  7686. }
  7687. /*==========================================
  7688. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7689. *------------------------------------------*/
  7690. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7691. {
  7692. if (type&2) {
  7693. if (hp || type&4)
  7694. clif_heal(sd->fd,SP_HP,hp);
  7695. if (sp)
  7696. clif_heal(sd->fd,SP_SP,sp);
  7697. } else {
  7698. if(hp)
  7699. clif_updatestatus(sd,SP_HP);
  7700. if(sp)
  7701. clif_updatestatus(sd,SP_SP);
  7702. }
  7703. return;
  7704. }
  7705. /**
  7706. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7707. * @param sd: Player data
  7708. * @param itemid: Item ID
  7709. * @param hp: HP to heal
  7710. * @param sp: SP to heal
  7711. * @return Amount healed to an object
  7712. */
  7713. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7714. {
  7715. int bonus, tmp, penalty = 0;
  7716. if (hp) {
  7717. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7718. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7719. if (potion_flag == 2) {
  7720. bonus += 50;
  7721. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7722. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7723. }
  7724. //All item bonuses.
  7725. bonus += sd->bonus.itemhealrate2;
  7726. //Item Group bonuses
  7727. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7728. //Individual item bonuses.
  7729. for(const auto &it : sd->itemhealrate) {
  7730. if (it.id == itemid) {
  7731. bonus += it.val;
  7732. break;
  7733. }
  7734. }
  7735. // Recovery Potion
  7736. if (sd->sc.data[SC_INCHEALRATE])
  7737. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7738. // 2014 Halloween Event : Pumpkin Bonus
  7739. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7740. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7741. tmp = hp * bonus / 100; // Overflow check
  7742. if (bonus != 100 && tmp > hp)
  7743. hp = tmp;
  7744. }
  7745. if (sp) {
  7746. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7747. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7748. if (potion_flag == 2)
  7749. bonus += 50;
  7750. tmp = sp * bonus / 100; // Overflow check
  7751. if (bonus != 100 && tmp > sp)
  7752. sp = tmp;
  7753. }
  7754. if (sd->sc.count) {
  7755. // Critical Wound and Death Hurt stack
  7756. if (sd->sc.data[SC_CRITICALWOUND])
  7757. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7758. if (sd->sc.data[SC_DEATHHURT])
  7759. penalty += 20;
  7760. if (sd->sc.data[SC_NORECOVER_STATE])
  7761. penalty = 100;
  7762. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7763. hp += hp / 2; // 1.5 times
  7764. sp -= sp / 2;
  7765. }
  7766. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7767. hp += hp / 10;
  7768. sp += sp / 10;
  7769. }
  7770. if (penalty > 0) {
  7771. hp -= hp * penalty / 100;
  7772. sp -= sp * penalty / 100;
  7773. }
  7774. #ifdef RENEWAL
  7775. if (sd->sc.data[SC_EXTREMITYFIST2])
  7776. sp = 0;
  7777. #endif
  7778. if (sd->sc.data[SC_BITESCAR])
  7779. hp = 0;
  7780. }
  7781. return status_heal(&sd->bl, hp, sp, 1);
  7782. }
  7783. /*==========================================
  7784. * HP/SP Recovery
  7785. * Heal player hp nad/or sp by rate
  7786. *------------------------------------------*/
  7787. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7788. {
  7789. nullpo_ret(sd);
  7790. if (hp > 100) hp = 100;
  7791. else if (hp <-100) hp = -100;
  7792. if (sp > 100) sp = 100;
  7793. else if (sp <-100) sp = -100;
  7794. if(hp >= 0 && sp >= 0) //Heal
  7795. return status_percent_heal(&sd->bl, hp, sp);
  7796. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7797. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7798. //Crossed signs
  7799. if(hp) {
  7800. if(hp > 0)
  7801. status_percent_heal(&sd->bl, hp, 0);
  7802. else
  7803. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7804. }
  7805. if(sp) {
  7806. if(sp > 0)
  7807. status_percent_heal(&sd->bl, 0, sp);
  7808. else
  7809. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7810. }
  7811. return 0;
  7812. }
  7813. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7814. {
  7815. struct mob_data *md;
  7816. int flag;
  7817. md = (struct mob_data *)bl;
  7818. nullpo_ret(md);
  7819. flag = va_arg(ap, int);
  7820. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7821. status_kill(&md->bl);
  7822. return 1;
  7823. }
  7824. /**
  7825. * Called when player changes job
  7826. * Rewrote to make it tidider [Celest]
  7827. * @param sd
  7828. * @param job JOB ID. See enum e_job
  7829. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7830. * @return True if success, false if failed
  7831. **/
  7832. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7833. {
  7834. int i, fame_flag = 0;
  7835. int b_class;
  7836. nullpo_retr(false,sd);
  7837. if (job < 0)
  7838. return false;
  7839. //Normalize job.
  7840. b_class = pc_jobid2mapid(job);
  7841. if (b_class == -1)
  7842. return false;
  7843. switch (upper) {
  7844. case 1:
  7845. b_class|= JOBL_UPPER;
  7846. break;
  7847. case 2:
  7848. b_class|= JOBL_BABY;
  7849. break;
  7850. }
  7851. //This will automatically adjust bard/dancer classes to the correct gender
  7852. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7853. job = pc_mapid2jobid(b_class, sd->status.sex);
  7854. if (job == -1)
  7855. return false;
  7856. if ((unsigned short)b_class == sd->class_)
  7857. return false; //Nothing to change.
  7858. // changing from 1st to 2nd job
  7859. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7860. sd->change_level_2nd = sd->status.job_level;
  7861. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7862. }
  7863. // changing from 2nd to 3rd job
  7864. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7865. sd->change_level_3rd = sd->status.job_level;
  7866. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7867. }
  7868. if(sd->cloneskill_idx > 0) {
  7869. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7870. sd->status.skill[sd->cloneskill_idx].id = 0;
  7871. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7872. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7873. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7874. }
  7875. sd->cloneskill_idx = 0;
  7876. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7877. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7878. }
  7879. if(sd->reproduceskill_idx > 0) {
  7880. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7881. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7882. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7883. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7884. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7885. }
  7886. sd->reproduceskill_idx = 0;
  7887. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7888. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7889. }
  7890. // Give or reduce transcendent status points
  7891. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7892. sd->status.status_point += battle_config.transcendent_status_points;
  7893. clif_updatestatus(sd,SP_STATUSPOINT);
  7894. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7895. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7896. // The player already used his bonus points, so we have to reset his status points
  7897. pc_resetstate(sd);
  7898. }
  7899. sd->status.status_point -= battle_config.transcendent_status_points;
  7900. clif_updatestatus(sd,SP_STATUSPOINT);
  7901. }
  7902. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7903. const int class_ = pc_class2idx(sd->status.class_);
  7904. uint16 skill_id;
  7905. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7906. //Remove status specific to your current tree skills.
  7907. enum sc_type sc = status_skill2sc(skill_id);
  7908. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7909. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7910. }
  7911. }
  7912. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7913. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7914. pc_resetfeel(sd);
  7915. }
  7916. // Reset body style to 0 before changing job to avoid
  7917. // errors since not every job has a alternate outfit.
  7918. sd->status.body = 0;
  7919. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7920. sd->status.class_ = job;
  7921. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7922. sd->class_ = (unsigned short)b_class;
  7923. sd->status.job_level=1;
  7924. sd->status.job_exp=0;
  7925. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7926. sd->status.base_level = pc_maxbaselv(sd);
  7927. sd->status.base_exp=0;
  7928. pc_resetstate(sd);
  7929. clif_updatestatus(sd,SP_STATUSPOINT);
  7930. clif_updatestatus(sd,SP_BASELEVEL);
  7931. clif_updatestatus(sd,SP_BASEEXP);
  7932. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7933. }
  7934. clif_updatestatus(sd,SP_JOBLEVEL);
  7935. clif_updatestatus(sd,SP_JOBEXP);
  7936. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7937. for(i=0;i<EQI_MAX;i++) {
  7938. if(sd->equip_index[i] >= 0)
  7939. if(pc_isequip(sd,sd->equip_index[i]))
  7940. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7941. }
  7942. //Change look, if disguised, you need to undisguise
  7943. //to correctly calculate new job sprite without
  7944. if (sd->disguise)
  7945. pc_disguise(sd, 0);
  7946. status_set_viewdata(&sd->bl, job);
  7947. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7948. #if PACKETVER >= 20151001
  7949. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7950. #endif
  7951. if(sd->vd.cloth_color)
  7952. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7953. /*
  7954. if(sd->vd.body_style)
  7955. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7956. */
  7957. //Update skill tree.
  7958. pc_calc_skilltree(sd);
  7959. clif_skillinfoblock(sd);
  7960. if (sd->ed)
  7961. elemental_delete(sd->ed);
  7962. if (sd->state.vending)
  7963. vending_closevending(sd);
  7964. if (sd->state.buyingstore)
  7965. buyingstore_close(sd);
  7966. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7967. //Remove peco/cart/falcon
  7968. i = sd->sc.option;
  7969. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7970. i&=~OPTION_RIDING;
  7971. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7972. i&=~OPTION_FALCON;
  7973. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7974. i&=~OPTION_DRAGON;
  7975. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7976. i&=~OPTION_WUGRIDER;
  7977. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7978. i&=~OPTION_WUG;
  7979. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7980. i&=~OPTION_MADOGEAR;
  7981. #ifndef NEW_CARTS
  7982. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7983. i&=~OPTION_CART;
  7984. #else
  7985. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7986. pc_setcart(sd, 0);
  7987. #endif
  7988. if(i != sd->sc.option)
  7989. pc_setoption(sd, i);
  7990. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7991. hom_vaporize(sd, HOM_ST_ACTIVE);
  7992. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7993. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7994. if(sd->status.manner < 0)
  7995. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7996. status_calc_pc(sd,SCO_FORCE);
  7997. pc_checkallowskill(sd);
  7998. pc_equiplookall(sd);
  7999. pc_show_questinfo(sd);
  8000. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8001. if( sd->status.party_id ){
  8002. struct party_data* p;
  8003. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8004. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8005. if( i < MAX_PARTY ){
  8006. p->party.member[i].class_ = sd->status.class_;
  8007. clif_party_job_and_level(sd);
  8008. }
  8009. }
  8010. }
  8011. chrif_save(sd, CSAVE_NORMAL);
  8012. //if you were previously famous, not anymore.
  8013. if (fame_flag)
  8014. chrif_buildfamelist();
  8015. else if (sd->status.fame > 0) {
  8016. //It may be that now they are famous?
  8017. switch (sd->class_&MAPID_UPPERMASK) {
  8018. case MAPID_BLACKSMITH:
  8019. case MAPID_ALCHEMIST:
  8020. case MAPID_TAEKWON:
  8021. chrif_buildfamelist();
  8022. break;
  8023. }
  8024. }
  8025. return true;
  8026. }
  8027. /*==========================================
  8028. * Tell client player sd has change equipement
  8029. *------------------------------------------*/
  8030. void pc_equiplookall(struct map_session_data *sd)
  8031. {
  8032. nullpo_retv(sd);
  8033. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8034. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8035. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8036. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8037. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8038. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8039. }
  8040. /*==========================================
  8041. * Tell client player sd has change look (hair,equip...)
  8042. *------------------------------------------*/
  8043. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8044. nullpo_retv(sd);
  8045. switch(type) {
  8046. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8047. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8048. if (sd->status.hair != val) {
  8049. sd->status.hair = val;
  8050. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8051. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8052. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8053. }
  8054. break;
  8055. case LOOK_WEAPON:
  8056. sd->status.weapon = val;
  8057. break;
  8058. case LOOK_HEAD_BOTTOM:
  8059. sd->status.head_bottom = val;
  8060. sd->setlook_head_bottom = val;
  8061. break;
  8062. case LOOK_HEAD_TOP:
  8063. sd->status.head_top = val;
  8064. sd->setlook_head_top = val;
  8065. break;
  8066. case LOOK_HEAD_MID:
  8067. sd->status.head_mid = val;
  8068. sd->setlook_head_mid = val;
  8069. break;
  8070. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8071. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8072. if (sd->status.hair_color != val) {
  8073. sd->status.hair_color = val;
  8074. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8075. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8076. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8077. }
  8078. break;
  8079. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8080. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8081. sd->status.clothes_color = val;
  8082. break;
  8083. case LOOK_SHIELD:
  8084. sd->status.shield = val;
  8085. break;
  8086. case LOOK_SHOES:
  8087. break;
  8088. case LOOK_ROBE:
  8089. sd->status.robe = val;
  8090. sd->setlook_robe = val;
  8091. break;
  8092. case LOOK_BODY2:
  8093. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8094. sd->status.body = val;
  8095. break;
  8096. }
  8097. clif_changelook(&sd->bl, type, val);
  8098. }
  8099. /*==========================================
  8100. * Give an option (type) to player (sd) and display it to client
  8101. *------------------------------------------*/
  8102. void pc_setoption(struct map_session_data *sd,int type)
  8103. {
  8104. int p_type, new_look=0;
  8105. nullpo_retv(sd);
  8106. p_type = sd->sc.option;
  8107. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8108. sd->sc.option=type;
  8109. clif_changeoption(&sd->bl);
  8110. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8111. { // Mounting
  8112. clif_status_load(&sd->bl,EFST_RIDING,1);
  8113. status_calc_pc(sd,SCO_NONE);
  8114. }
  8115. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8116. { // Dismount
  8117. clif_status_load(&sd->bl,EFST_RIDING,0);
  8118. status_calc_pc(sd,SCO_NONE);
  8119. }
  8120. #ifndef NEW_CARTS
  8121. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8122. clif_cartlist(sd);
  8123. clif_updatestatus(sd, SP_CARTINFO);
  8124. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8125. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8126. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8127. clif_clearcart(sd->fd);
  8128. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8129. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8130. }
  8131. #endif
  8132. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8133. clif_status_load(&sd->bl,EFST_FALCON,1);
  8134. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8135. clif_status_load(&sd->bl,EFST_FALCON,0);
  8136. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8137. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8138. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8139. status_calc_pc(sd,SCO_NONE);
  8140. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8141. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8142. status_calc_pc(sd,SCO_NONE);
  8143. }
  8144. }
  8145. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8146. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8147. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8148. status_calc_pc(sd,SCO_NONE);
  8149. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8150. int skill_id = status_sc2skill(statuses[i]);
  8151. if (skill_id >= 0 && !(skill_get_inf3(skill_id)&INF3_USABLE_MADO))
  8152. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8153. }
  8154. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8155. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8156. status_calc_pc(sd,SCO_NONE);
  8157. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8158. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8159. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8160. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8161. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8162. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8163. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8164. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8165. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8166. }
  8167. }
  8168. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8169. new_look = JOB_STAR_GLADIATOR2;
  8170. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8171. new_look = -1;
  8172. if (sd->disguise || !new_look)
  8173. return; //Disguises break sprite changes
  8174. if (new_look < 0) { //Restore normal look.
  8175. status_set_viewdata(&sd->bl, sd->status.class_);
  8176. new_look = sd->vd.class_;
  8177. }
  8178. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8179. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8180. if (sd->vd.cloth_color)
  8181. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8182. if( sd->vd.body_style )
  8183. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8184. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8185. }
  8186. /**
  8187. * Give player a cart
  8188. * @param sd Player
  8189. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8190. **/
  8191. bool pc_setcart(struct map_session_data *sd,int type) {
  8192. #ifndef NEW_CARTS
  8193. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8194. int option;
  8195. #endif
  8196. nullpo_retr(false,sd);
  8197. if( type < 0 || type > MAX_CARTS )
  8198. return false;// Never trust the values sent by the client! [Skotlex]
  8199. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8200. return false;// Push cart is required
  8201. #ifdef NEW_CARTS
  8202. switch( type ) {
  8203. case 0:
  8204. if( !sd->sc.data[SC_PUSH_CART] )
  8205. return 0;
  8206. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8207. clif_clearcart(sd->fd);
  8208. break;
  8209. default:/* everything else is an allowed ID so we can move on */
  8210. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8211. clif_cartlist(sd);
  8212. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8213. }
  8214. clif_updatestatus(sd, SP_CARTINFO);
  8215. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8216. break;
  8217. }
  8218. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8219. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8220. #else
  8221. // Update option
  8222. option = sd->sc.option;
  8223. option &= ~OPTION_CART;// clear cart bits
  8224. option |= cart[type]; // set cart
  8225. pc_setoption(sd, option);
  8226. #endif
  8227. return true;
  8228. }
  8229. /*==========================================
  8230. * Give player a falcon
  8231. *------------------------------------------*/
  8232. void pc_setfalcon(struct map_session_data* sd, int flag)
  8233. {
  8234. if( flag ){
  8235. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8236. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8237. } else if( pc_isfalcon(sd) ){
  8238. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8239. }
  8240. }
  8241. /*==========================================
  8242. * Set player riding
  8243. *------------------------------------------*/
  8244. void pc_setriding(struct map_session_data* sd, int flag)
  8245. {
  8246. if( sd->sc.data[SC_ALL_RIDING] )
  8247. return;
  8248. if( flag ){
  8249. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8250. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8251. } else if( pc_isriding(sd) ){
  8252. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8253. }
  8254. }
  8255. /*==========================================
  8256. * Give player a mado
  8257. *------------------------------------------*/
  8258. void pc_setmadogear(struct map_session_data* sd, int flag)
  8259. {
  8260. if( flag ){
  8261. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8262. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8263. } else if( pc_ismadogear(sd) ){
  8264. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8265. }
  8266. }
  8267. /*==========================================
  8268. * Check if player can drop an item
  8269. *------------------------------------------*/
  8270. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8271. {
  8272. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8273. return false;
  8274. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8275. return false;
  8276. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8277. }
  8278. /**
  8279. * Determines whether a player can attack based on status changes
  8280. * Why not use status_check_skilluse?
  8281. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8282. * Even ground-based attacks should be blocked by these statuses
  8283. * Called from unit_attack and unit_attack_timer_sub
  8284. * @retval true Can attack
  8285. **/
  8286. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8287. nullpo_retr(false, sd);
  8288. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8289. return false;
  8290. if (sd->state.block_action & PCBLOCK_ATTACK)
  8291. return false;
  8292. if( sd->sc.data[SC_BASILICA] ||
  8293. sd->sc.data[SC__SHADOWFORM] ||
  8294. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8295. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8296. sd->sc.data[SC_CRYSTALIZE] ||
  8297. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8298. sd->sc.data[SC_TRICKDEAD] ||
  8299. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8300. sd->sc.data[SC_BLADESTOP] ||
  8301. sd->sc.data[SC_DEEPSLEEP] ||
  8302. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8303. sd->sc.data[SC_KINGS_GRACE] )
  8304. return false;
  8305. return true;
  8306. }
  8307. /*==========================================
  8308. * Read '@type' variables (temporary numeric char reg)
  8309. *------------------------------------------*/
  8310. int pc_readreg(struct map_session_data* sd, int64 reg)
  8311. {
  8312. return i64db_iget(sd->regs.vars, reg);
  8313. }
  8314. /*==========================================
  8315. * Set '@type' variables (temporary numeric char reg)
  8316. *------------------------------------------*/
  8317. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8318. {
  8319. unsigned int index = script_getvaridx(reg);
  8320. nullpo_retr(false, sd);
  8321. if( val ) {
  8322. i64db_iput(sd->regs.vars, reg, val);
  8323. if( index )
  8324. script_array_update(&sd->regs, reg, false);
  8325. } else {
  8326. i64db_remove(sd->regs.vars, reg);
  8327. if( index )
  8328. script_array_update(&sd->regs, reg, true);
  8329. }
  8330. return true;
  8331. }
  8332. /*==========================================
  8333. * Read '@type$' variables (temporary string char reg)
  8334. *------------------------------------------*/
  8335. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8336. {
  8337. struct script_reg_str *p = NULL;
  8338. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8339. return p ? p->value : NULL;
  8340. }
  8341. /*==========================================
  8342. * Set '@type$' variables (temporary string char reg)
  8343. *------------------------------------------*/
  8344. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8345. {
  8346. struct script_reg_str *p = NULL;
  8347. unsigned int index = script_getvaridx(reg);
  8348. DBData prev;
  8349. nullpo_retr(false, sd);
  8350. if( str[0] ) {
  8351. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8352. p->value = aStrdup(str);
  8353. p->flag.type = 1;
  8354. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8355. p = (struct script_reg_str *)db_data2ptr(&prev);
  8356. if( p->value )
  8357. aFree(p->value);
  8358. ers_free(str_reg_ers, p);
  8359. } else {
  8360. if( index )
  8361. script_array_update(&sd->regs, reg, false);
  8362. }
  8363. } else {
  8364. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8365. p = (struct script_reg_str *)db_data2ptr(&prev);
  8366. if( p->value )
  8367. aFree(p->value);
  8368. ers_free(str_reg_ers, p);
  8369. if( index )
  8370. script_array_update(&sd->regs, reg, true);
  8371. }
  8372. }
  8373. return true;
  8374. }
  8375. /**
  8376. * Serves the following variable types:
  8377. * - 'type' (permanent numeric char reg)
  8378. * - '#type' (permanent numeric account reg)
  8379. * - '##type' (permanent numeric account reg2)
  8380. **/
  8381. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8382. {
  8383. struct script_reg_num *p = NULL;
  8384. if (!sd->vars_ok) {
  8385. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8386. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8387. //intif->request_registry(sd,type==3?4:type);
  8388. set_eof(sd->fd);
  8389. return 0;
  8390. }
  8391. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8392. return p ? p->value : 0;
  8393. }
  8394. /**
  8395. * Serves the following variable types:
  8396. * - 'type$' (permanent str char reg)
  8397. * - '#type$' (permanent str account reg)
  8398. * - '##type$' (permanent str account reg2)
  8399. **/
  8400. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8401. {
  8402. struct script_reg_str *p = NULL;
  8403. if (!sd->vars_ok) {
  8404. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8405. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8406. //intif->request_registry(sd,type==3?4:type);
  8407. set_eof(sd->fd);
  8408. return NULL;
  8409. }
  8410. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8411. return p ? p->value : NULL;
  8412. }
  8413. /**
  8414. * Serves the following variable types:
  8415. * - 'type' (permanent numeric char reg)
  8416. * - '#type' (permanent numeric account reg)
  8417. * - '##type' (permanent numeric account reg2)
  8418. **/
  8419. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8420. {
  8421. struct script_reg_num *p = NULL;
  8422. const char *regname = get_str(script_getvarid(reg));
  8423. unsigned int index = script_getvaridx(reg);
  8424. if ( !reg_load && !sd->vars_ok ) {
  8425. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8426. return 0;
  8427. }
  8428. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8429. if( val ) {
  8430. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8431. script_array_update(&sd->regs, reg, false);
  8432. p->value = val;
  8433. } else {
  8434. p->value = 0;
  8435. if( index )
  8436. script_array_update(&sd->regs, reg, true);
  8437. }
  8438. if (!reg_load)
  8439. p->flag.update = 1;/* either way, it will require either delete or replace */
  8440. } else if( val ) {
  8441. DBData prev;
  8442. if( index )
  8443. script_array_update(&sd->regs, reg, false);
  8444. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8445. p->value = val;
  8446. if (!reg_load)
  8447. p->flag.update = 1;
  8448. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8449. p = (struct script_reg_num *)db_data2ptr(&prev);
  8450. ers_free(num_reg_ers, p);
  8451. }
  8452. }
  8453. if (!reg_load && p)
  8454. sd->vars_dirty = true;
  8455. return 1;
  8456. }
  8457. /**
  8458. * Serves the following variable types:
  8459. * - 'type$' (permanent str char reg)
  8460. * - '#type$' (permanent str account reg)
  8461. * - '##type$' (permanent str account reg2)
  8462. **/
  8463. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8464. {
  8465. struct script_reg_str *p = NULL;
  8466. const char *regname = get_str(script_getvarid(reg));
  8467. unsigned int index = script_getvaridx(reg);
  8468. size_t vlen = 0;
  8469. if (!reg_load && !sd->vars_ok) {
  8470. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8471. return 0;
  8472. }
  8473. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8474. {
  8475. ShowError("pc_check_RegistryVariableLength: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%zu\n", sd->status.account_id, sd->status.char_id, val, vlen);
  8476. return 0;
  8477. }
  8478. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8479. if( val[0] ) {
  8480. if( p->value )
  8481. aFree(p->value);
  8482. else if ( index ) // an entry that was deleted, so we reset
  8483. script_array_update(&sd->regs, reg, false);
  8484. p->value = aStrdup(val);
  8485. } else {
  8486. if (p->value)
  8487. aFree(p->value);
  8488. p->value = NULL;
  8489. if( index )
  8490. script_array_update(&sd->regs, reg, true);
  8491. }
  8492. if( !reg_load )
  8493. p->flag.update = 1; // either way, it will require either delete or replace
  8494. } else if( val[0] ) {
  8495. DBData prev;
  8496. if( index )
  8497. script_array_update(&sd->regs, reg, false);
  8498. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8499. p->value = aStrdup(val);
  8500. if( !reg_load )
  8501. p->flag.update = 1;
  8502. p->flag.type = 1;
  8503. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8504. p = (struct script_reg_str *)db_data2ptr(&prev);
  8505. if( p->value )
  8506. aFree(p->value);
  8507. ers_free(str_reg_ers, p);
  8508. }
  8509. }
  8510. if( !reg_load && p )
  8511. sd->vars_dirty = true;
  8512. return 1;
  8513. }
  8514. /**
  8515. * Set value of player variable
  8516. * @param sd Player
  8517. * @param reg Variable name
  8518. * @param value
  8519. * @return True if success, false if failed.
  8520. **/
  8521. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8522. char prefix = reg[0];
  8523. nullpo_retr(false, sd);
  8524. if (reg[strlen(reg)-1] == '$') {
  8525. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8526. return false;
  8527. }
  8528. val = cap_value(val, INT_MIN, INT_MAX);
  8529. switch (prefix) {
  8530. case '.':
  8531. case '\'':
  8532. case '$':
  8533. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8534. return false;
  8535. case '@':
  8536. return pc_setreg(sd, add_str(reg), val);
  8537. case '#':
  8538. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8539. default:
  8540. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8541. }
  8542. return false;
  8543. }
  8544. /**
  8545. * Get value of player variable
  8546. * @param sd Player
  8547. * @param reg Variable name
  8548. * @return Variable value or 0 if failed.
  8549. **/
  8550. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8551. char prefix = reg[0];
  8552. nullpo_ret(sd);
  8553. if (reg[strlen(reg)-1] == '$') {
  8554. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8555. return 0;
  8556. }
  8557. switch (prefix) {
  8558. case '.':
  8559. case '\'':
  8560. case '$':
  8561. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8562. return 0;
  8563. case '@':
  8564. return pc_readreg(sd, add_str(reg));
  8565. case '#':
  8566. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8567. default:
  8568. return pc_readglobalreg(sd, add_str(reg));
  8569. }
  8570. return 0;
  8571. }
  8572. /*==========================================
  8573. * Exec eventtimer for player sd (retrieved from map_session (id))
  8574. *------------------------------------------*/
  8575. static TIMER_FUNC(pc_eventtimer){
  8576. struct map_session_data *sd=map_id2sd(id);
  8577. char *p = (char *)data;
  8578. int i;
  8579. if(sd==NULL)
  8580. return 0;
  8581. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8582. if( i < MAX_EVENTTIMER )
  8583. {
  8584. sd->eventtimer[i] = INVALID_TIMER;
  8585. sd->eventcount--;
  8586. npc_event(sd,p,0);
  8587. }
  8588. else
  8589. ShowError("pc_eventtimer: no such event timer\n");
  8590. if (p) aFree(p);
  8591. return 0;
  8592. }
  8593. /*==========================================
  8594. * Add eventtimer for player sd ?
  8595. *------------------------------------------*/
  8596. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8597. {
  8598. int i;
  8599. nullpo_retr(false,sd);
  8600. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8601. if( i == MAX_EVENTTIMER )
  8602. return false;
  8603. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8604. sd->eventcount++;
  8605. return true;
  8606. }
  8607. /*==========================================
  8608. * Del eventtimer for player sd ?
  8609. *------------------------------------------*/
  8610. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8611. {
  8612. char* p = NULL;
  8613. int i;
  8614. nullpo_retr(false,sd);
  8615. if (sd->eventcount == 0)
  8616. return false;
  8617. // find the named event timer
  8618. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8619. sd->eventtimer[i] != INVALID_TIMER &&
  8620. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8621. strcmp(p, name) == 0
  8622. );
  8623. if( i == MAX_EVENTTIMER )
  8624. return false; // not found
  8625. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8626. sd->eventtimer[i] = INVALID_TIMER;
  8627. if(sd->eventcount > 0)
  8628. sd->eventcount--;
  8629. aFree(p);
  8630. return true;
  8631. }
  8632. /*==========================================
  8633. * Update eventtimer count for player sd
  8634. *------------------------------------------*/
  8635. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8636. {
  8637. int i;
  8638. nullpo_retv(sd);
  8639. for(i=0;i<MAX_EVENTTIMER;i++)
  8640. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8641. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8642. addt_tickimer(sd->eventtimer[i],tick);
  8643. break;
  8644. }
  8645. }
  8646. /*==========================================
  8647. * Remove all eventtimer for player sd
  8648. *------------------------------------------*/
  8649. void pc_cleareventtimer(struct map_session_data *sd)
  8650. {
  8651. int i;
  8652. nullpo_retv(sd);
  8653. if (sd->eventcount == 0)
  8654. return;
  8655. for(i=0;i<MAX_EVENTTIMER;i++){
  8656. if( sd->eventtimer[i] != INVALID_TIMER ){
  8657. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8658. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8659. sd->eventtimer[i] = INVALID_TIMER;
  8660. if(sd->eventcount > 0) //avoid looping to max val
  8661. sd->eventcount--;
  8662. if (p) aFree(p);
  8663. }
  8664. }
  8665. }
  8666. /**
  8667. * Called when an item with combo is worn
  8668. * @param *sd
  8669. * @param *data struct item_data
  8670. * @return success numbers of succeed combo
  8671. */
  8672. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8673. uint16 i;
  8674. int success = 0;
  8675. for( i = 0; i < data->combos_count; i++ ) {
  8676. struct itemchk {
  8677. int idx;
  8678. unsigned short nameid;
  8679. short card[MAX_SLOTS];
  8680. } *combo_idx;
  8681. int idx, j;
  8682. int nb_itemCombo;
  8683. unsigned int pos = 0;
  8684. /* ensure this isn't a duplicate combo */
  8685. if( sd->combos.bonus != NULL ) {
  8686. int x;
  8687. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8688. /* found a match, skip this combo */
  8689. if( x < sd->combos.count )
  8690. continue;
  8691. }
  8692. nb_itemCombo = data->combos[i]->count;
  8693. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8694. continue;
  8695. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8696. for(j=0; j < nb_itemCombo; j++){
  8697. combo_idx[j].idx=-1;
  8698. combo_idx[j].nameid=-1;
  8699. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8700. }
  8701. for( j = 0; j < nb_itemCombo; j++ ) {
  8702. uint16 id = data->combos[i]->nameid[j], k;
  8703. bool found = false;
  8704. for( k = 0; k < EQI_MAX; k++ ) {
  8705. short index = sd->equip_index[k];
  8706. if( index < 0 )
  8707. continue;
  8708. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8709. continue;
  8710. if (!sd->inventory_data[index] )
  8711. continue;
  8712. if ( itemdb_type(id) != IT_CARD ) {
  8713. if ( sd->inventory_data[index]->nameid != id )
  8714. continue;
  8715. if(j>0){ //check if this item not already used
  8716. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8717. uint8 z;
  8718. for (z = 0; z < nb_itemCombo-1; z++)
  8719. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8720. do_continue=true;
  8721. if(do_continue)
  8722. continue;
  8723. }
  8724. combo_idx[j].nameid = id;
  8725. combo_idx[j].idx = index;
  8726. pos |= sd->inventory.u.items_inventory[index].equip;
  8727. found = true;
  8728. break;
  8729. } else { //Cards and enchants
  8730. uint16 z;
  8731. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8732. continue;
  8733. for (z = 0; z < MAX_SLOTS; z++) {
  8734. bool do_continue=false;
  8735. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8736. continue;
  8737. if(j>0){
  8738. int c1, c2;
  8739. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8740. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8741. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8742. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8743. do_continue = true;
  8744. break;
  8745. }
  8746. }
  8747. }
  8748. }
  8749. }
  8750. if(do_continue)
  8751. continue;
  8752. combo_idx[j].nameid = id;
  8753. combo_idx[j].idx = index;
  8754. combo_idx[j].card[z] = id;
  8755. pos |= sd->inventory.u.items_inventory[index].equip;
  8756. found = true;
  8757. break;
  8758. }
  8759. }
  8760. }
  8761. if( !found )
  8762. break;/* we haven't found all the ids for this combo, so we can return */
  8763. }
  8764. aFree(combo_idx);
  8765. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8766. if( j < nb_itemCombo )
  8767. continue;
  8768. /* we got here, means all items in the combo are matching */
  8769. idx = sd->combos.count;
  8770. if( sd->combos.bonus == NULL ) {
  8771. CREATE(sd->combos.bonus, struct script_code *, 1);
  8772. CREATE(sd->combos.id, unsigned short, 1);
  8773. CREATE(sd->combos.pos, unsigned int, 1);
  8774. sd->combos.count = 1;
  8775. } else {
  8776. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8777. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8778. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8779. }
  8780. /* we simply copy the pointer */
  8781. sd->combos.bonus[idx] = data->combos[i]->script;
  8782. /* save this combo's id */
  8783. sd->combos.id[idx] = data->combos[i]->id;
  8784. /* save pos of combo*/
  8785. sd->combos.pos[idx] = pos;
  8786. success++;
  8787. }
  8788. return success;
  8789. }
  8790. /**
  8791. * Called when an item with combo is removed
  8792. * @param *sd
  8793. * @param *data struct item_data
  8794. * @return retval numbers of removed combo
  8795. */
  8796. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8797. int i, retval = 0;
  8798. if( sd->combos.bonus == NULL )
  8799. return 0;/* nothing to do here, player has no combos */
  8800. for( i = 0; i < data->combos_count; i++ ) {
  8801. /* check if this combo exists in this user */
  8802. int x = 0, cursor = 0, j;
  8803. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8804. /* no match, skip this combo */
  8805. if(x >= sd->combos.count)
  8806. continue;
  8807. sd->combos.bonus[x] = NULL;
  8808. sd->combos.id[x] = 0;
  8809. sd->combos.pos[x] = 0;
  8810. retval++;
  8811. /* check if combo requirements still fit */
  8812. if( pc_checkcombo( sd, data ) )
  8813. continue;
  8814. /* move next value to empty slot */
  8815. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8816. if( sd->combos.bonus[j] == NULL )
  8817. continue;
  8818. if( cursor != j ) {
  8819. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8820. sd->combos.id[cursor] = sd->combos.id[j];
  8821. sd->combos.pos[cursor] = sd->combos.pos[j];
  8822. }
  8823. cursor++;
  8824. }
  8825. /* it's empty, we can clear all the memory */
  8826. if( (sd->combos.count = cursor) == 0 ) {
  8827. aFree(sd->combos.bonus);
  8828. aFree(sd->combos.id);
  8829. aFree(sd->combos.pos);
  8830. sd->combos.bonus = NULL;
  8831. sd->combos.id = NULL;
  8832. sd->combos.pos = NULL;
  8833. return retval; /* we also can return at this point for we have no more combos to check */
  8834. }
  8835. }
  8836. return retval;
  8837. }
  8838. /**
  8839. * Load combo data(s) of player
  8840. * @param *sd
  8841. * @return ret numbers of succeed combo
  8842. */
  8843. int pc_load_combo(struct map_session_data *sd) {
  8844. int i, ret = 0;
  8845. for( i = 0; i < EQI_MAX; i++ ) {
  8846. struct item_data *id = NULL;
  8847. short idx = sd->equip_index[i];
  8848. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8849. continue;
  8850. if( id->combos_count )
  8851. ret += pc_checkcombo(sd,id);
  8852. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8853. struct item_data *data;
  8854. int j;
  8855. for( j = 0; j < MAX_SLOTS; j++ ) {
  8856. if (!sd->inventory.u.items_inventory[idx].card[j])
  8857. continue;
  8858. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8859. if( data->combos_count )
  8860. ret += pc_checkcombo(sd,data);
  8861. }
  8862. }
  8863. }
  8864. }
  8865. return ret;
  8866. }
  8867. /*==========================================
  8868. * Equip item on player sd at req_pos from inventory index n
  8869. * return: false - fail; true - success
  8870. *------------------------------------------*/
  8871. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8872. {
  8873. int i, pos, flag = 0, iflag;
  8874. struct item_data *id;
  8875. uint8 res = ITEM_EQUIP_ACK_OK;
  8876. short* equip_index;
  8877. nullpo_retr(false,sd);
  8878. if( n < 0 || n >= MAX_INVENTORY ) {
  8879. if( equipswitch ){
  8880. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8881. }else{
  8882. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8883. }
  8884. return false;
  8885. }
  8886. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8887. if( equipswitch ){
  8888. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8889. }else{
  8890. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8891. }
  8892. return false;
  8893. }
  8894. if (!(id = sd->inventory_data[n]))
  8895. return false;
  8896. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8897. if(battle_config.battle_log && !equipswitch)
  8898. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8899. if((res = pc_isequip(sd,n))) {
  8900. if( equipswitch ){
  8901. clif_equipswitch_add( sd, n, req_pos, res );
  8902. }else{
  8903. clif_equipitemack(sd,n,0,res); // fail
  8904. }
  8905. return false;
  8906. }
  8907. if( equipswitch && id->type == IT_AMMO ){
  8908. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8909. return false;
  8910. }
  8911. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8912. if( equipswitch ){
  8913. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8914. }else{
  8915. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8916. }
  8917. return false;
  8918. }
  8919. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8920. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8921. if( equipswitch ){
  8922. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8923. }else{
  8924. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8925. }
  8926. return false;
  8927. }
  8928. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  8929. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8930. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8931. clif_notify_bindOnEquip(sd,n);
  8932. }
  8933. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  8934. pos = req_pos&EQP_ACC;
  8935. if (pos == EQP_ACC) //User specified both slots.
  8936. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8937. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  8938. if (!sd->inventory.u.items_inventory[n].card[i])
  8939. continue;
  8940. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  8941. if (card_data) {
  8942. int card_pos = card_data->equip;
  8943. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  8944. pos = card_pos; // Use the card's equip position
  8945. break;
  8946. }
  8947. }
  8948. }
  8949. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8950. pos = (req_pos&EQP_ARMS);
  8951. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8952. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8953. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  8954. pos = req_pos&EQP_SHADOW_ACC;
  8955. if (pos == EQP_SHADOW_ACC)
  8956. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8957. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8958. pos = (req_pos&EQP_SHADOW_ARMS);
  8959. if( pos == EQP_SHADOW_ARMS )
  8960. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8961. }
  8962. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8963. //Update skill-block range database when weapon range changes. [Skotlex]
  8964. i = equip_index[EQI_HAND_R];
  8965. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8966. flag = 1;
  8967. else
  8968. flag = id->range != sd->inventory_data[i]->range;
  8969. }
  8970. if( equipswitch ){
  8971. for( i = 0; i < EQI_MAX; i++ ){
  8972. if( pos&equip_bitmask[i] ){
  8973. // If there was already an item assigned to this slot
  8974. if( sd->equip_switch_index[i] >= 0 ){
  8975. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  8976. }
  8977. // Assign the new index to it
  8978. sd->equip_switch_index[i] = n;
  8979. }
  8980. }
  8981. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  8982. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  8983. return true;
  8984. }else{
  8985. for(i=0;i<EQI_MAX;i++) {
  8986. if(pos & equip_bitmask[i]) {
  8987. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8988. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8989. sd->equip_index[i] = n;
  8990. }
  8991. }
  8992. pc_equipswitch_remove(sd, n);
  8993. if(pos==EQP_AMMO) {
  8994. clif_arrowequip(sd,n);
  8995. clif_arrow_fail(sd,3);
  8996. }
  8997. else
  8998. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8999. sd->inventory.u.items_inventory[n].equip = pos;
  9000. }
  9001. if(pos & EQP_HAND_R) {
  9002. if(id)
  9003. sd->weapontype1 = id->look;
  9004. else
  9005. sd->weapontype1 = 0;
  9006. pc_calcweapontype(sd);
  9007. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9008. }
  9009. if(pos & EQP_HAND_L) {
  9010. if(id) {
  9011. if(id->type == IT_WEAPON) {
  9012. sd->status.shield = 0;
  9013. sd->weapontype2 = id->look;
  9014. }
  9015. else
  9016. if(id->type == IT_ARMOR) {
  9017. sd->status.shield = id->look;
  9018. sd->weapontype2 = 0;
  9019. }
  9020. }
  9021. else
  9022. sd->status.shield = sd->weapontype2 = 0;
  9023. pc_calcweapontype(sd);
  9024. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9025. }
  9026. if(pos & EQP_SHOES)
  9027. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9028. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9029. short idx = sd->equip_index[EQI_AMMO];
  9030. if (idx >= 0) {
  9031. switch (sd->inventory_data[idx]->look) {
  9032. case AMMO_ARROW:
  9033. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9034. pc_unequipitem(sd, idx, 2 | 4);
  9035. break;
  9036. case AMMO_BULLET:
  9037. case AMMO_SHELL:
  9038. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9039. #ifdef RENEWAL
  9040. && id->look != W_GRENADE
  9041. #endif
  9042. )
  9043. pc_unequipitem(sd, idx, 2 | 4);
  9044. break;
  9045. #ifndef RENEWAL
  9046. case AMMO_GRENADE:
  9047. if (id->look != W_GRENADE)
  9048. pc_unequipitem(sd, idx, 2 | 4);
  9049. break;
  9050. #endif
  9051. }
  9052. }
  9053. }
  9054. pc_set_costume_view(sd);
  9055. pc_checkallowskill(sd); //Check if status changes should be halted.
  9056. iflag = sd->npc_item_flag;
  9057. /* check for combos (MUST be before status_calc_pc) */
  9058. if( id->combos_count )
  9059. pc_checkcombo(sd,id);
  9060. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9061. ; //No cards
  9062. else {
  9063. for( i = 0; i < MAX_SLOTS; i++ ) {
  9064. struct item_data *data;
  9065. if (!sd->inventory.u.items_inventory[n].card[i])
  9066. continue;
  9067. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9068. if( data->combos_count )
  9069. pc_checkcombo(sd,data);
  9070. }
  9071. }
  9072. }
  9073. status_calc_pc(sd,SCO_NONE);
  9074. if (flag) //Update skill data
  9075. clif_skillinfoblock(sd);
  9076. //OnEquip script [Skotlex]
  9077. if (id) {
  9078. //only run the script if item isn't restricted
  9079. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9080. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9081. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9082. ; //No cards
  9083. else {
  9084. for( i = 0; i < MAX_SLOTS; i++ ) {
  9085. struct item_data *data;
  9086. if (!sd->inventory.u.items_inventory[n].card[i])
  9087. continue;
  9088. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9089. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9090. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9091. }
  9092. }
  9093. }
  9094. }
  9095. sd->npc_item_flag = iflag;
  9096. return true;
  9097. }
  9098. /**
  9099. * Recalculate player status on unequip
  9100. * @param sd: Player data
  9101. * @param n: Item inventory index
  9102. * @param flag: Whether to recalculate a player's status or not
  9103. * @return True on success or false on failure
  9104. */
  9105. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9106. int i, iflag;
  9107. bool status_calc = false;
  9108. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9109. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9110. sd->inventory.u.items_inventory[n].equip = 0;
  9111. pc_checkallowskill(sd);
  9112. iflag = sd->npc_item_flag;
  9113. /* check for combos (MUST be before status_calc_pc) */
  9114. if (sd->inventory_data[n]) {
  9115. if (sd->inventory_data[n]->combos_count) {
  9116. if (pc_removecombo(sd, sd->inventory_data[n]))
  9117. status_calc = true;
  9118. }
  9119. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9120. ; //No cards
  9121. else {
  9122. for (i = 0; i < MAX_SLOTS; i++) {
  9123. struct item_data *data;
  9124. if (!sd->inventory.u.items_inventory[n].card[i])
  9125. continue;
  9126. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9127. if (data->combos_count) {
  9128. if (pc_removecombo(sd, data))
  9129. status_calc = true;
  9130. }
  9131. }
  9132. }
  9133. }
  9134. }
  9135. if (status_calc)
  9136. status_calc_pc(sd, SCO_NONE);
  9137. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9138. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9139. //OnUnEquip script [Skotlex]
  9140. if (sd->inventory_data[n]) {
  9141. if (sd->inventory_data[n]->unequip_script)
  9142. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9143. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9144. ; //No cards
  9145. else {
  9146. for (i = 0; i < MAX_SLOTS; i++) {
  9147. struct item_data *data;
  9148. if (!sd->inventory.u.items_inventory[n].card[i])
  9149. continue;
  9150. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9151. if (data->unequip_script)
  9152. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9153. }
  9154. }
  9155. }
  9156. }
  9157. if (flag & 1)
  9158. status_calc_pc(sd, SCO_FORCE);
  9159. sd->npc_item_flag = iflag;
  9160. }
  9161. /**
  9162. * Called when attempting to unequip an item from a player
  9163. * @param sd: Player data
  9164. * @param n: Item inventory index
  9165. * @param flag: Type of unequip
  9166. * 0 - only unequip
  9167. * 1 - calculate status after unequipping
  9168. * 2 - force unequip
  9169. * 4 - unequip by switching equipment
  9170. * @return True on success or false on failure
  9171. */
  9172. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9173. int i, pos;
  9174. nullpo_retr(false,sd);
  9175. if (n < 0 || n >= MAX_INVENTORY) {
  9176. clif_unequipitemack(sd,0,0,0);
  9177. return false;
  9178. }
  9179. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9180. clif_unequipitemack(sd,n,0,0);
  9181. return false; //Nothing to unequip
  9182. }
  9183. // status change that makes player cannot unequip equipment
  9184. if (!(flag&2) && sd->sc.count &&
  9185. (sd->sc.data[SC_BERSERK] ||
  9186. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9187. sd->sc.data[SC__BLOODYLUST] ||
  9188. sd->sc.data[SC_KYOUGAKU] ||
  9189. (sd->sc.data[SC_PYROCLASTIC] &&
  9190. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9191. {
  9192. clif_unequipitemack(sd,n,0,0);
  9193. return false;
  9194. }
  9195. if (battle_config.battle_log)
  9196. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9197. for(i = 0; i < EQI_MAX; i++) {
  9198. if (pos & equip_bitmask[i])
  9199. sd->equip_index[i] = -1;
  9200. }
  9201. if(pos & EQP_HAND_R) {
  9202. sd->weapontype1 = 0;
  9203. sd->status.weapon = sd->weapontype2;
  9204. pc_calcweapontype(sd);
  9205. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9206. if( !battle_config.dancing_weaponswitch_fix )
  9207. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9208. #ifdef RENEWAL
  9209. if (battle_config.switch_remove_edp&2) {
  9210. #else
  9211. if (battle_config.switch_remove_edp&1) {
  9212. #endif
  9213. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9214. }
  9215. }
  9216. if(pos & EQP_HAND_L) {
  9217. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9218. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9219. sd->status.shield = sd->weapontype2 = 0;
  9220. pc_calcweapontype(sd);
  9221. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9222. }
  9223. if(pos & EQP_SHOES)
  9224. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9225. clif_unequipitemack(sd,n,pos,1);
  9226. pc_set_costume_view(sd);
  9227. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9228. // On weapon change (right and left hand)
  9229. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9230. if (battle_config.ammo_unequip && !(flag & 4)) {
  9231. switch (sd->inventory_data[n]->look) {
  9232. case W_BOW:
  9233. case W_MUSICAL:
  9234. case W_WHIP:
  9235. case W_REVOLVER:
  9236. case W_RIFLE:
  9237. case W_GATLING:
  9238. case W_SHOTGUN:
  9239. case W_GRENADE: {
  9240. short idx = sd->equip_index[EQI_AMMO];
  9241. if (idx >= 0) {
  9242. sd->equip_index[EQI_AMMO] = -1;
  9243. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9244. pc_unequipitem_sub(sd, idx, 0);
  9245. }
  9246. }
  9247. break;
  9248. }
  9249. }
  9250. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9251. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9252. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9253. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9254. }
  9255. // On armor change
  9256. if (pos & EQP_ARMOR) {
  9257. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9258. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9259. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9260. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9261. }
  9262. // On ammo change
  9263. if (sd->inventory_data[n]->type == IT_AMMO && (sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET || sd->inventory_data[n]->nameid != ITEMID_PURIFICATION_BULLET || sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET_))
  9264. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9265. pc_unequipitem_sub(sd, n, flag);
  9266. return true;
  9267. }
  9268. int pc_equipswitch( struct map_session_data* sd, int index ){
  9269. // Get the target equip mask
  9270. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9271. // Get the currently equipped item
  9272. short equippedItem = pc_checkequip( sd, position, true );
  9273. // No item equipped at the target
  9274. if( equippedItem == -1 ){
  9275. // Remove it from the equip switch
  9276. pc_equipswitch_remove( sd, index );
  9277. pc_equipitem( sd, index, position );
  9278. return position;
  9279. }else{
  9280. std::map<int, int> unequipped;
  9281. int unequipped_position = 0;
  9282. // Unequip all items that interfere
  9283. for( int i = 0; i < EQI_MAX; i++ ){
  9284. int unequip_index = sd->equip_index[i];
  9285. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9286. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9287. // Store the unequipped index and position mask for later
  9288. unequipped[unequip_index] = unequip_item->equip;
  9289. // Keep the position for later
  9290. unequipped_position |= unequip_item->equip;
  9291. // Unequip the item
  9292. pc_unequipitem( sd, unequip_index, 0 );
  9293. }
  9294. }
  9295. int all_position = position | unequipped_position;
  9296. // Equip everything that is hit by the mask
  9297. for( int i = 0; i < EQI_MAX; i++ ){
  9298. int exchange_index = sd->equip_switch_index[i];
  9299. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9300. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9301. // Store the target position
  9302. int exchange_position = exchange_item->equipSwitch;
  9303. // Remove the item from equip switch
  9304. pc_equipswitch_remove( sd, exchange_index );
  9305. // Equip the item at the destinated position
  9306. pc_equipitem( sd, exchange_index, exchange_position );
  9307. }
  9308. }
  9309. // Place all unequipped items into the equip switch window
  9310. for( std::pair<int, int> pair : unequipped ){
  9311. int unequipped_index = pair.first;
  9312. int unequipped_position = pair.second;
  9313. // Rebuild the index cache
  9314. for( int i = 0; i < EQI_MAX; i++ ){
  9315. if( unequipped_position & equip_bitmask[i] ){
  9316. sd->equip_switch_index[i] = unequipped_index;
  9317. }
  9318. }
  9319. // Set the correct position mask
  9320. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9321. // Notify the client
  9322. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9323. }
  9324. return all_position;
  9325. }
  9326. }
  9327. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9328. struct item* item = &sd->inventory.u.items_inventory[index];
  9329. if( !item->equipSwitch ){
  9330. return;
  9331. }
  9332. for( int i = 0; i < EQI_MAX; i++ ){
  9333. // If a match is found
  9334. if( sd->equip_switch_index[i] == index ){
  9335. // Remove it from the slot
  9336. sd->equip_switch_index[i] = -1;
  9337. }
  9338. }
  9339. // Send out one packet for all slots using the current item's mask
  9340. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9341. item->equipSwitch = 0;
  9342. }
  9343. /*==========================================
  9344. * Checking if player (sd) has an invalid item
  9345. * and is unequiped on map load (item_noequip)
  9346. *------------------------------------------*/
  9347. void pc_checkitem(struct map_session_data *sd) {
  9348. int i, calc_flag = 0;
  9349. struct item* it;
  9350. nullpo_retv(sd);
  9351. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9352. return;
  9353. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9354. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9355. it = &sd->inventory.u.items_inventory[i];
  9356. if( it->nameid == 0 )
  9357. continue;
  9358. if( !it->equip )
  9359. continue;
  9360. if( it->equip&~pc_equippoint(sd,i) ) {
  9361. pc_unequipitem(sd, i, 2);
  9362. calc_flag = 1;
  9363. continue;
  9364. }
  9365. if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) {
  9366. pc_unequipitem(sd, i, 2);
  9367. calc_flag = 1;
  9368. continue;
  9369. }
  9370. }
  9371. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9372. it = &sd->inventory.u.items_inventory[i];
  9373. if( it->nameid == 0 )
  9374. continue;
  9375. if( !it->equipSwitch )
  9376. continue;
  9377. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9378. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9379. for( int j = 0; j < EQI_MAX; j++ ){
  9380. if( sd->equip_switch_index[j] == i ){
  9381. sd->equip_switch_index[j] = -1;
  9382. }
  9383. }
  9384. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9385. continue;
  9386. }
  9387. }
  9388. if( calc_flag && sd->state.active ) {
  9389. pc_checkallowskill(sd);
  9390. status_calc_pc(sd,SCO_NONE);
  9391. }
  9392. }
  9393. /*==========================================
  9394. * Checks for unavailable items and removes them.
  9395. * @param sd: Player data
  9396. * @param type Forced check:
  9397. * 1 - Inventory
  9398. * 2 - Cart
  9399. * 4 - Storage
  9400. *------------------------------------------*/
  9401. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9402. {
  9403. int i;
  9404. unsigned short nameid;
  9405. char output[256];
  9406. nullpo_retv(sd);
  9407. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9408. for(i = 0; i < MAX_INVENTORY; i++) {
  9409. nameid = sd->inventory.u.items_inventory[i].nameid;
  9410. if (!nameid)
  9411. continue;
  9412. if (!itemdb_available(nameid)) {
  9413. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9414. clif_displaymessage(sd->fd, output);
  9415. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  9416. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9417. continue;
  9418. }
  9419. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9420. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9421. }
  9422. }
  9423. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9424. for(i = 0; i < MAX_CART; i++) {
  9425. nameid = sd->cart.u.items_cart[i].nameid;
  9426. if (!nameid)
  9427. continue;
  9428. if (!itemdb_available(nameid)) {
  9429. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9430. clif_displaymessage(sd->fd, output);
  9431. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  9432. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9433. continue;
  9434. }
  9435. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9436. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9437. }
  9438. }
  9439. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9440. for(i = 0; i < sd->storage.max_amount; i++) {
  9441. nameid = sd->storage.u.items_storage[i].nameid;
  9442. if (!nameid)
  9443. continue;
  9444. if (!itemdb_available(nameid)) {
  9445. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9446. clif_displaymessage(sd->fd, output);
  9447. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  9448. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9449. continue;
  9450. }
  9451. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9452. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9453. }
  9454. }
  9455. }
  9456. /*==========================================
  9457. * Update PVP rank for sd1 in cmp to sd2
  9458. *------------------------------------------*/
  9459. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9460. {
  9461. struct map_session_data *sd1,*sd2;
  9462. sd1=(struct map_session_data *)bl;
  9463. sd2=va_arg(ap,struct map_session_data *);
  9464. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9465. {// cannot register pvp rank for hidden GMs
  9466. return 0;
  9467. }
  9468. if( sd1->pvp_point > sd2->pvp_point )
  9469. sd2->pvp_rank++;
  9470. return 0;
  9471. }
  9472. /*==========================================
  9473. * Calculate new rank beetween all present players (map_foreachinallarea)
  9474. * and display result
  9475. *------------------------------------------*/
  9476. int pc_calc_pvprank(struct map_session_data *sd)
  9477. {
  9478. int old = sd->pvp_rank;
  9479. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9480. sd->pvp_rank=1;
  9481. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9482. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9483. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9484. return sd->pvp_rank;
  9485. }
  9486. /*==========================================
  9487. * Calculate next sd ranking calculation from config
  9488. *------------------------------------------*/
  9489. TIMER_FUNC(pc_calc_pvprank_timer){
  9490. struct map_session_data *sd;
  9491. sd=map_id2sd(id);
  9492. if(sd==NULL)
  9493. return 0;
  9494. sd->pvp_timer = INVALID_TIMER;
  9495. if( pc_isinvisible(sd) )
  9496. {// do not calculate the pvp rank for a hidden GM
  9497. return 0;
  9498. }
  9499. if( pc_calc_pvprank(sd) > 0 )
  9500. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9501. return 0;
  9502. }
  9503. /*==========================================
  9504. * Checking if sd is married
  9505. * Return:
  9506. * partner_id = yes
  9507. * 0 = no
  9508. *------------------------------------------*/
  9509. int pc_ismarried(struct map_session_data *sd)
  9510. {
  9511. if(sd == NULL)
  9512. return -1;
  9513. if(sd->status.partner_id > 0)
  9514. return sd->status.partner_id;
  9515. else
  9516. return 0;
  9517. }
  9518. /*==========================================
  9519. * Marry player sd to player dstsd
  9520. * Return:
  9521. * false = fail
  9522. * true = success
  9523. *------------------------------------------*/
  9524. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9525. {
  9526. if(sd == NULL || dstsd == NULL ||
  9527. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9528. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9529. return false;
  9530. sd->status.partner_id = dstsd->status.char_id;
  9531. dstsd->status.partner_id = sd->status.char_id;
  9532. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9533. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9534. return true;
  9535. }
  9536. /*==========================================
  9537. * Divorce sd from its partner
  9538. * Return:
  9539. * false = fail
  9540. * true = success
  9541. *------------------------------------------*/
  9542. bool pc_divorce(struct map_session_data *sd)
  9543. {
  9544. struct map_session_data *p_sd;
  9545. int i;
  9546. if( sd == NULL || !pc_ismarried(sd) )
  9547. return false;
  9548. if( !sd->status.partner_id )
  9549. return false; // Char is not married
  9550. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9551. { // Lets char server do the divorce
  9552. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9553. return false; // No char server connected
  9554. return true;
  9555. }
  9556. // Both players online, lets do the divorce manually
  9557. sd->status.partner_id = 0;
  9558. p_sd->status.partner_id = 0;
  9559. for( i = 0; i < MAX_INVENTORY; i++ )
  9560. {
  9561. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9562. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9563. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9564. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9565. }
  9566. clif_divorced(sd, p_sd->status.name);
  9567. clif_divorced(p_sd, sd->status.name);
  9568. return true;
  9569. }
  9570. /**
  9571. * Get the partner map_session_data of a player
  9572. * @param sd : the husband|wife session
  9573. * @return partner session or NULL
  9574. */
  9575. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9576. if (!sd || !pc_ismarried(sd))
  9577. return NULL;
  9578. return map_charid2sd(sd->status.partner_id);
  9579. }
  9580. /**
  9581. * Get the father map_session_data of a player
  9582. * @param sd : the baby session
  9583. * @return father session or NULL
  9584. */
  9585. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9586. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9587. return NULL;
  9588. return map_charid2sd(sd->status.father);
  9589. }
  9590. /**
  9591. * Get the mother map_session_data of a player
  9592. * @param sd : the baby session
  9593. * @return mother session or NULL
  9594. */
  9595. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9596. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9597. return NULL;
  9598. return map_charid2sd(sd->status.mother);
  9599. }
  9600. /*==========================================
  9601. * Get sd children charid. (Need to be married)
  9602. *------------------------------------------*/
  9603. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9604. {
  9605. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9606. // charid2sd returns NULL if not found
  9607. return NULL;
  9608. return map_charid2sd(sd->status.child);
  9609. }
  9610. /*==========================================
  9611. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9612. *------------------------------------------*/
  9613. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9614. {
  9615. int hp = 0, sp = 0;
  9616. if( pc_isdead(sd) )
  9617. return;
  9618. if (sd->hp_loss.value) {
  9619. sd->hp_loss.tick += diff_tick;
  9620. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9621. hp += sd->hp_loss.value;
  9622. sd->hp_loss.tick -= sd->hp_loss.rate;
  9623. }
  9624. if(hp >= sd->battle_status.hp)
  9625. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9626. }
  9627. if (sd->sp_loss.value) {
  9628. sd->sp_loss.tick += diff_tick;
  9629. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9630. sp += sd->sp_loss.value;
  9631. sd->sp_loss.tick -= sd->sp_loss.rate;
  9632. }
  9633. }
  9634. if (hp > 0 || sp > 0)
  9635. status_zap(&sd->bl, hp, sp);
  9636. }
  9637. //Character regen. Flag is used to know which types of regen can take place.
  9638. //&1: HP regen
  9639. //&2: SP regen
  9640. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9641. {
  9642. int hp = 0, sp = 0;
  9643. if (sd->hp_regen.value) {
  9644. sd->hp_regen.tick += diff_tick;
  9645. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9646. hp += sd->hp_regen.value;
  9647. sd->hp_regen.tick -= sd->hp_regen.rate;
  9648. }
  9649. }
  9650. if (sd->sp_regen.value) {
  9651. sd->sp_regen.tick += diff_tick;
  9652. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9653. sp += sd->sp_regen.value;
  9654. sd->sp_regen.tick -= sd->sp_regen.rate;
  9655. }
  9656. }
  9657. if (sd->percent_hp_regen.value) {
  9658. sd->percent_hp_regen.tick += diff_tick;
  9659. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9660. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9661. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9662. }
  9663. }
  9664. if (sd->percent_sp_regen.value) {
  9665. sd->percent_sp_regen.tick += diff_tick;
  9666. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9667. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9668. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9669. }
  9670. }
  9671. if (hp > 0 || sp > 0)
  9672. status_heal(&sd->bl, hp, sp, 0);
  9673. }
  9674. /*==========================================
  9675. * Memo player sd savepoint. (map,x,y)
  9676. *------------------------------------------*/
  9677. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9678. {
  9679. nullpo_retv(sd);
  9680. sd->status.save_point.map = mapindex;
  9681. sd->status.save_point.x = x;
  9682. sd->status.save_point.y = y;
  9683. }
  9684. /*==========================================
  9685. * Save 1 player data at autosave interval
  9686. *------------------------------------------*/
  9687. static TIMER_FUNC(pc_autosave){
  9688. int interval;
  9689. struct s_mapiterator* iter;
  9690. struct map_session_data* sd;
  9691. static int last_save_id = 0, save_flag = 0;
  9692. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9693. save_flag = 0;
  9694. else
  9695. save_flag = 1; //Noone was saved, so save first found char.
  9696. iter = mapit_getallusers();
  9697. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9698. {
  9699. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9700. continue;
  9701. if(sd->bl.id == last_save_id && save_flag != 1) {
  9702. save_flag = 1;
  9703. continue;
  9704. }
  9705. if(save_flag != 1) //Not our turn to save yet.
  9706. continue;
  9707. //Save char.
  9708. last_save_id = sd->bl.id;
  9709. save_flag = 2;
  9710. if (pc_isvip(sd)) // Check if we're still VIP
  9711. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9712. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9713. break;
  9714. }
  9715. mapit_free(iter);
  9716. interval = autosave_interval/(map_usercount()+1);
  9717. if(interval < minsave_interval)
  9718. interval = minsave_interval;
  9719. add_timer(gettick()+interval,pc_autosave,0,0);
  9720. return 0;
  9721. }
  9722. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9723. {
  9724. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9725. { //Night/day state does not match.
  9726. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9727. sd->state.night = night_flag;
  9728. return 1;
  9729. }
  9730. return 0;
  9731. }
  9732. /*================================================
  9733. * timer to do the day [Yor]
  9734. * data: 0 = called by timer, 1 = gmcommand/script
  9735. *------------------------------------------------*/
  9736. TIMER_FUNC(map_day_timer){
  9737. char tmp_soutput[1024];
  9738. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9739. return 0;
  9740. if (!night_flag)
  9741. return 0; //Already day.
  9742. night_flag = 0; // 0=day, 1=night [Yor]
  9743. map_foreachpc(pc_daynight_timer_sub);
  9744. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9745. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9746. return 0;
  9747. }
  9748. /*================================================
  9749. * timer to do the night [Yor]
  9750. * data: 0 = called by timer, 1 = gmcommand/script
  9751. *------------------------------------------------*/
  9752. TIMER_FUNC(map_night_timer){
  9753. char tmp_soutput[1024];
  9754. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9755. return 0;
  9756. if (night_flag)
  9757. return 0; //Already nigth.
  9758. night_flag = 1; // 0=day, 1=night [Yor]
  9759. map_foreachpc(pc_daynight_timer_sub);
  9760. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9761. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9762. return 0;
  9763. }
  9764. /**
  9765. * Attempt to stand up a player
  9766. * @param sd
  9767. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9768. * @return True if success, Fals if failed
  9769. */
  9770. bool pc_setstand(struct map_session_data *sd, bool force){
  9771. nullpo_ret(sd);
  9772. // Cannot stand yet
  9773. // TODO: Move to SCS_NOSTAND [Cydh]
  9774. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9775. return false;
  9776. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9777. clif_status_load(&sd->bl,EFST_SIT,0);
  9778. clif_standing(&sd->bl); //Inform area PC is standing
  9779. //Reset sitting tick.
  9780. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9781. if( pc_isdead( sd ) ){
  9782. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9783. clif_party_dead( sd );
  9784. }else{
  9785. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9786. }
  9787. return true;
  9788. }
  9789. /**
  9790. * Calculate Overheat value
  9791. * @param sd: Player data
  9792. * @param heat: Amount of Heat to adjust
  9793. **/
  9794. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9795. struct status_change_entry *sce = NULL;
  9796. int16 limit[] = { 150, 200, 280, 360, 450 };
  9797. uint16 skill_lv;
  9798. nullpo_retv(sd);
  9799. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9800. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9801. sce->val1 += heat;
  9802. sce->val1 = cap_value(sce->val1, 0, 1000);
  9803. if (sd->sc.data[SC_OVERHEAT])
  9804. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9805. if (sce->val1 > limit[skill_lv])
  9806. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9807. } else if (heat > 0)
  9808. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9809. }
  9810. /**
  9811. * Check if player is autolooting given itemID.
  9812. */
  9813. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9814. {
  9815. uint8 i = 0;
  9816. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9817. return true;
  9818. if (!sd->state.autolooting)
  9819. return false;
  9820. if (sd->state.autolooting)
  9821. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9822. return (i != AUTOLOOTITEM_SIZE);
  9823. }
  9824. /**
  9825. * Checks if player can use @/#command
  9826. * @param sd Player map session data
  9827. * @param command Command name without @/# and params
  9828. * @param type is it atcommand or charcommand
  9829. */
  9830. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9831. {
  9832. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9833. }
  9834. /**
  9835. * Checks if commands used by a player should be logged
  9836. * according to their group setting.
  9837. * @param sd Player map session data
  9838. */
  9839. bool pc_should_log_commands(struct map_session_data *sd)
  9840. {
  9841. return pc_group_should_log_commands(pc_get_group_id(sd));
  9842. }
  9843. /**
  9844. * Spirit Charm expiration timer.
  9845. * @see TimerFunc
  9846. */
  9847. static TIMER_FUNC(pc_spiritcharm_timer){
  9848. struct map_session_data *sd;
  9849. int i;
  9850. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9851. return 1;
  9852. if (sd->spiritcharm <= 0) {
  9853. ShowError("pc_spiritcharm_timer: %d spiritcharm's available. (aid=%d cid=%d tid=%d)\n", sd->spiritcharm, sd->status.account_id, sd->status.char_id, tid);
  9854. sd->spiritcharm = 0;
  9855. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9856. return 0;
  9857. }
  9858. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9859. if (i == sd->spiritcharm) {
  9860. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9861. return 0;
  9862. }
  9863. sd->spiritcharm--;
  9864. if (i != sd->spiritcharm)
  9865. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9866. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9867. if (sd->spiritcharm <= 0)
  9868. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9869. clif_spiritcharm(sd);
  9870. return 0;
  9871. }
  9872. /**
  9873. * Adds a spirit charm.
  9874. * @param sd: Target character
  9875. * @param interval: Duration
  9876. * @param max: Maximum amount of charms to add
  9877. * @param type: Charm type (@see spirit_charm_types)
  9878. */
  9879. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9880. {
  9881. int tid, i;
  9882. nullpo_retv(sd);
  9883. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9884. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9885. if (max > MAX_SPIRITCHARM)
  9886. max = MAX_SPIRITCHARM;
  9887. if (sd->spiritcharm < 0)
  9888. sd->spiritcharm = 0;
  9889. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9890. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9891. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9892. sd->spiritcharm--;
  9893. if (sd->spiritcharm != 0)
  9894. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9895. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9896. }
  9897. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9898. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spiritcharm_timer[i])->tick) < 0);
  9899. if (i != sd->spiritcharm)
  9900. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9901. sd->spiritcharm_timer[i] = tid;
  9902. sd->spiritcharm++;
  9903. sd->spiritcharm_type = type;
  9904. clif_spiritcharm(sd);
  9905. }
  9906. /**
  9907. * Removes one or more spirit charms.
  9908. * @param sd: The target character
  9909. * @param count: Amount of charms to remove
  9910. * @param type: Type of charm to remove
  9911. */
  9912. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9913. {
  9914. int i;
  9915. nullpo_retv(sd);
  9916. if (sd->spiritcharm_type != type)
  9917. return;
  9918. if (sd->spiritcharm <= 0) {
  9919. sd->spiritcharm = 0;
  9920. return;
  9921. }
  9922. if (count <= 0)
  9923. return;
  9924. if (count > sd->spiritcharm)
  9925. count = sd->spiritcharm;
  9926. sd->spiritcharm -= count;
  9927. if (count > MAX_SPIRITCHARM)
  9928. count = MAX_SPIRITCHARM;
  9929. for (i = 0; i < count; i++) {
  9930. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9931. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9932. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9933. }
  9934. }
  9935. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9936. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9937. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9938. }
  9939. if (sd->spiritcharm <= 0)
  9940. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9941. clif_spiritcharm(sd);
  9942. }
  9943. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9944. /**
  9945. * Renewal EXP/Item Drop rate modifier based on level penalty
  9946. * @param level_diff: Monster and Player level difference
  9947. * @param mob_class: Monster class
  9948. * @param mode: Monster mode
  9949. * @param type: 1 - EXP, 2 - Item Drop
  9950. * @return Penalty rate
  9951. */
  9952. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9953. {
  9954. int rate = 100;
  9955. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9956. return rate;
  9957. if (level_diff < 0)
  9958. level_diff = MAX_LEVEL + (~level_diff + 1);
  9959. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9960. return rate;
  9961. return 100; // Penalty not found, return default
  9962. }
  9963. #endif
  9964. int pc_split_str(char *str,char **val,int num)
  9965. {
  9966. int i;
  9967. for (i=0; i<num && str; i++){
  9968. val[i] = str;
  9969. str = strchr(str,',');
  9970. if (str && i<num-1) //Do not remove a trailing comma.
  9971. *str++=0;
  9972. }
  9973. return i;
  9974. }
  9975. int pc_split_atoi(char* str, int* val, char sep, int max)
  9976. {
  9977. int i,j;
  9978. for (i=0; i<max; i++) {
  9979. if (!str) break;
  9980. val[i] = atoi(str);
  9981. str = strchr(str,sep);
  9982. if (str)
  9983. *str++=0;
  9984. }
  9985. //Zero up the remaining.
  9986. for(j=i; j < max; j++)
  9987. val[j] = 0;
  9988. return i;
  9989. }
  9990. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9991. {
  9992. static int warning=0;
  9993. int i,j;
  9994. for (i=0; i<max; i++) {
  9995. double f;
  9996. if (!str) break;
  9997. f = atof(str);
  9998. if (f < 0)
  9999. val[i] = 0;
  10000. else if (f > UINT_MAX) {
  10001. val[i] = UINT_MAX;
  10002. if (!warning) {
  10003. warning = 1;
  10004. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10005. }
  10006. } else
  10007. val[i] = (unsigned int)f;
  10008. str = strchr(str,sep);
  10009. if (str)
  10010. *str++=0;
  10011. }
  10012. //Zero up the remaining.
  10013. for(j=i; j < max; j++)
  10014. val[j] = 0;
  10015. return i;
  10016. }
  10017. /*==========================================
  10018. * sub DB reading.
  10019. * Function used to read skill_tree.txt
  10020. *------------------------------------------*/
  10021. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10022. {
  10023. uint32 baselv, joblv, baselv_max, joblv_max;
  10024. uint16 skill_id, skill_lv, skill_lv_max;
  10025. int idx, class_;
  10026. unsigned int i, offset, skill_idx;
  10027. class_ = atoi(fields[0]);
  10028. skill_id = (uint16)atoi(fields[1]);
  10029. skill_lv = (uint16)atoi(fields[2]);
  10030. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10031. baselv = (uint32)atoi(fields[3]);
  10032. joblv = (uint32)atoi(fields[4]);
  10033. offset = 5;
  10034. }
  10035. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10036. baselv = joblv = 0;
  10037. offset = 3;
  10038. }
  10039. else {
  10040. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10041. return false;
  10042. }
  10043. if(!pcdb_checkid(class_))
  10044. {
  10045. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10046. return false;
  10047. }
  10048. idx = pc_class2idx(class_);
  10049. if (!skill_get_index(skill_id)) {
  10050. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  10051. return false;
  10052. }
  10053. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10054. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10055. skill_lv = skill_lv_max;
  10056. }
  10057. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10058. ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
  10059. baselv = baselv_max;
  10060. }
  10061. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10062. ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
  10063. joblv = joblv_max;
  10064. }
  10065. //This is to avoid adding two lines for the same skill. [Skotlex]
  10066. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
  10067. if( skill_idx == MAX_SKILL_TREE )
  10068. {
  10069. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
  10070. return false;
  10071. }
  10072. else if(skill_tree[idx][skill_idx].skill_id)
  10073. {
  10074. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10075. }
  10076. skill_tree[idx][skill_idx].skill_id = skill_id;
  10077. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10078. skill_tree[idx][skill_idx].baselv = baselv;
  10079. skill_tree[idx][skill_idx].joblv = joblv;
  10080. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10081. {
  10082. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10083. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10084. if (skill_id == 0) {
  10085. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10086. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10087. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10088. }
  10089. continue;
  10090. }
  10091. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10092. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10093. return false;
  10094. }
  10095. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10096. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10097. skill_lv = skill_lv_max;
  10098. }
  10099. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10100. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10101. }
  10102. return true;
  10103. }
  10104. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10105. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10106. {
  10107. int type, class_, diff;
  10108. type = atoi(fields[0]); //1=experience, 2=item drop
  10109. class_ = atoi(fields[1]);
  10110. diff = atoi(fields[2]);
  10111. if( type != 1 && type != 2 ){
  10112. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10113. return false;
  10114. }
  10115. if( !CHK_CLASS(class_) ){
  10116. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10117. return false;
  10118. }
  10119. diff = min(diff, MAX_LEVEL);
  10120. if( diff < 0 )
  10121. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10122. level_penalty[type][class_][diff] = atoi(fields[3]);
  10123. return true;
  10124. }
  10125. #endif
  10126. /** [Cydh]
  10127. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10128. * @param level Base level of player
  10129. * @param class_ Job ID @see enum e_job
  10130. * @return base_hp
  10131. */
  10132. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10133. double base_hp;
  10134. uint16 i, idx = pc_class2idx(class_);
  10135. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10136. #ifndef RENEWAL
  10137. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10138. #endif
  10139. for (i = 2; i <= level; i++)
  10140. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10141. if (class_ == JOB_SUMMONER)
  10142. base_hp += floor((base_hp / 2) + 0.5);
  10143. return (unsigned int)base_hp;
  10144. }
  10145. /** [Playtester]
  10146. * Calculates base sp of player.
  10147. * @param level Base level of player
  10148. * @param class_ Job ID @see enum e_job
  10149. * @return base_sp
  10150. */
  10151. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10152. double base_sp;
  10153. uint16 idx = pc_class2idx(class_);
  10154. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10155. switch (class_) {
  10156. case JOB_NINJA:
  10157. if (level >= 10)
  10158. base_sp -= 22;
  10159. else
  10160. base_sp = 11 + 3*level;
  10161. break;
  10162. case JOB_GUNSLINGER:
  10163. if (level > 10)
  10164. base_sp -= 18;
  10165. else
  10166. base_sp = 9 + 3*level;
  10167. break;
  10168. case JOB_SUMMONER:
  10169. base_sp -= floor(base_sp / 2);
  10170. break;
  10171. }
  10172. return (unsigned int)base_sp;
  10173. }
  10174. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10175. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10176. int idx, class_;
  10177. unsigned int i;
  10178. class_ = atoi(fields[0]);
  10179. if (!pcdb_checkid(class_)) {
  10180. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10181. return false;
  10182. }
  10183. idx = pc_class2idx(class_);
  10184. job_info[idx].max_weight_base = atoi(fields[1]);
  10185. job_info[idx].hp_factor = atoi(fields[2]);
  10186. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10187. job_info[idx].sp_factor = atoi(fields[4]);
  10188. #ifdef RENEWAL_ASPD
  10189. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10190. #else
  10191. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10192. #endif
  10193. {
  10194. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10195. }
  10196. return true;
  10197. }
  10198. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10199. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10200. {
  10201. int idx, class_, i;
  10202. class_ = atoi(fields[0]);
  10203. if(!pcdb_checkid(class_))
  10204. {
  10205. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10206. return false;
  10207. }
  10208. idx = pc_class2idx(class_);
  10209. for(i = 1; i < columns; i++)
  10210. {
  10211. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10212. }
  10213. return true;
  10214. }
  10215. //Reading job_exp.txt line
  10216. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10217. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10218. {
  10219. int idx, i, type;
  10220. int job_id,job_count,jobs[CLASS_COUNT];
  10221. unsigned int ui, maxlvl;
  10222. maxlvl = atoi(fields[0]);
  10223. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10224. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10225. return false;
  10226. }
  10227. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10228. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10229. return false;
  10230. }
  10231. type = atoi(fields[2]);
  10232. if(type < 0 || type > 1){
  10233. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10234. return false;
  10235. }
  10236. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10237. if (job_count < 1)
  10238. return false;
  10239. job_id = jobs[0];
  10240. if(!pcdb_checkid(job_id)){
  10241. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10242. return false;
  10243. }
  10244. idx = pc_class2idx(job_id);
  10245. job_info[idx].max_level[type] = maxlvl;
  10246. for(i=0; i<maxlvl; i++)
  10247. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10248. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10249. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10250. //should look like this:
  10251. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10252. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10253. job_info[idx].max_level[type]--;
  10254. if (job_info[idx].max_level[type] < maxlvl) {
  10255. ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
  10256. ShowInfo("Filling the missing values with the last exp entry.\n");
  10257. //Fill the requested values with the last entry.
  10258. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10259. for (; ui+2 < maxlvl; ui++)
  10260. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10261. job_info[idx].max_level[type] = maxlvl;
  10262. }
  10263. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10264. for (i = 1; i < job_count; i++) {
  10265. job_id = jobs[i];
  10266. if (!pcdb_checkid(job_id)) {
  10267. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10268. continue;
  10269. }
  10270. idx = pc_class2idx(job_id);
  10271. memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
  10272. job_info[idx].max_level[type] = maxlvl;
  10273. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10274. }
  10275. return true;
  10276. }
  10277. /**
  10278. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10279. * startlvl,endlvl,class,type,values...
  10280. */
  10281. #ifdef HP_SP_TABLES
  10282. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10283. {
  10284. int i, startlvl, endlvl;
  10285. int job_count,jobs[CLASS_COUNT];
  10286. short type;
  10287. startlvl = atoi(fields[0]);
  10288. if(startlvl<1){
  10289. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10290. return false;
  10291. }
  10292. endlvl = atoi(fields[1]);
  10293. if(endlvl<1 || endlvl<startlvl){
  10294. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10295. return false;
  10296. }
  10297. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10298. ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
  10299. return false;
  10300. }
  10301. type = atoi(fields[3]);
  10302. if(type < 0 || type > 1){
  10303. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10304. return false;
  10305. }
  10306. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10307. if (job_count < 1)
  10308. return false;
  10309. for (i = 0; i < job_count; i++) {
  10310. int idx, job_id = jobs[i], use_endlvl;
  10311. if (!pcdb_checkid(job_id)) {
  10312. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10313. return false;
  10314. }
  10315. idx = pc_class2idx(job_id);
  10316. if (startlvl > job_info[idx].max_level[0]) {
  10317. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10318. return false;
  10319. }
  10320. //Just read until available max level for this job, don't use MAX_LEVEL!
  10321. use_endlvl = endlvl;
  10322. if (use_endlvl > job_info[idx].max_level[0])
  10323. use_endlvl = job_info[idx].max_level[0];
  10324. if(type == 0) { //hp type
  10325. uint16 j;
  10326. for(j = 0; j < use_endlvl; j++) {
  10327. if (atoi(fields[j+4])) {
  10328. uint16 lvl_idx = startlvl-1+j;
  10329. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10330. //Tells if this HP is lower than previous level (but not for 99->100)
  10331. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10332. ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10333. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10334. }
  10335. }
  10336. }
  10337. else { //sp type
  10338. uint16 j;
  10339. for(j = 0; j < use_endlvl; j++) {
  10340. if (atoi(fields[j+4])) {
  10341. uint16 lvl_idx = startlvl-1+j;
  10342. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10343. //Tells if this SP is lower than previous level (but not for 99->100)
  10344. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10345. ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10346. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10347. }
  10348. }
  10349. }
  10350. }
  10351. return true;
  10352. }
  10353. #endif
  10354. /** [Cydh]
  10355. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10356. */
  10357. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10358. {
  10359. int idx, class_;
  10360. uint16 str, agi, vit, int_, dex, luk;
  10361. script_get_constant(trim(fields[0]),&class_);
  10362. if ((idx = pc_class2idx(class_)) < 0) {
  10363. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10364. return false;
  10365. }
  10366. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10367. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10368. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10369. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10370. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10371. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10372. job_info[idx].max_param.str = str;
  10373. job_info[idx].max_param.agi = agi;
  10374. job_info[idx].max_param.vit = vit;
  10375. job_info[idx].max_param.int_ = int_;
  10376. job_info[idx].max_param.dex = dex;
  10377. job_info[idx].max_param.luk = luk;
  10378. return true;
  10379. }
  10380. /**
  10381. * Read job_noenter_map.txt
  10382. **/
  10383. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10384. int idx, class_ = -1;
  10385. if (ISDIGIT(str[0][0])) {
  10386. class_ = atoi(str[0]);
  10387. } else {
  10388. if (!script_get_constant(str[0], &class_)) {
  10389. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10390. return false;
  10391. }
  10392. }
  10393. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10394. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10395. return false;
  10396. }
  10397. job_info[idx].noenter_map.zone = atoi(str[1]);
  10398. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10399. return true;
  10400. }
  10401. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10402. int i=1;
  10403. char line[24000]; //FIXME this seem too big
  10404. FILE *fp;
  10405. sprintf(line, "%s/statpoint.txt", basedir);
  10406. fp=fopen(line,"r");
  10407. if(fp == NULL){
  10408. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10409. return max(last_s,i);
  10410. } else {
  10411. int entries=0;
  10412. while(fgets(line, sizeof(line), fp))
  10413. {
  10414. int stat;
  10415. trim(line);
  10416. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10417. continue;
  10418. if ((stat=strtoul(line,NULL,10))<0)
  10419. stat=0;
  10420. if (i > MAX_LEVEL)
  10421. break;
  10422. statp[i]=stat;
  10423. i++;
  10424. entries++;
  10425. }
  10426. fclose(fp);
  10427. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10428. }
  10429. return max(last_s,i);
  10430. }
  10431. /*==========================================
  10432. * pc DB reading.
  10433. * job_exp.txt - required experience values
  10434. * skill_tree.txt - skill tree for every class
  10435. * attr_fix.txt - elemental adjustment table
  10436. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10437. * job_db2.txt - job,stats bonuses/lvl
  10438. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10439. *------------------------------------------*/
  10440. void pc_readdb(void) {
  10441. int i, k, s = 1;
  10442. const char* dbsubpath[] = {
  10443. "",
  10444. "/" DBIMPORT,
  10445. //add other path here
  10446. };
  10447. //reset
  10448. memset(job_info,0,sizeof(job_info)); // job_info table
  10449. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10450. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10451. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10452. for( k=1; k < 3; k++ ){ // fill in the blanks
  10453. int j;
  10454. for( j = 0; j < CLASS_ALL; j++ ){
  10455. int tmp = 0;
  10456. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10457. if( i == MAX_LEVEL+1 )
  10458. tmp = level_penalty[k][j][0];// reset
  10459. if( level_penalty[k][j][i] > 0 )
  10460. tmp = level_penalty[k][j][i];
  10461. else
  10462. level_penalty[k][j][i] = tmp;
  10463. }
  10464. }
  10465. }
  10466. #endif
  10467. // reset then read statspoint
  10468. memset(statp,0,sizeof(statp));
  10469. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10470. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10471. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10472. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10473. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10474. if(i==0) {
  10475. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10476. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10477. }
  10478. else {
  10479. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10480. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10481. }
  10482. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10483. if (i == 0)
  10484. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10485. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10486. #else
  10487. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10488. #endif
  10489. else
  10490. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10491. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10492. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10493. #ifdef HP_SP_TABLES
  10494. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
  10495. #endif
  10496. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10497. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10498. aFree(dbsubpath1);
  10499. aFree(dbsubpath2);
  10500. }
  10501. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10502. memset(skill_tree, 0, sizeof(skill_tree));
  10503. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
  10504. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
  10505. // generate the remaining parts of the db if necessary
  10506. k = battle_config.use_statpoint_table; //save setting
  10507. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10508. statp[0] = 45; // seed value
  10509. for (; s <= MAX_LEVEL; s++)
  10510. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10511. battle_config.use_statpoint_table = k; //restore setting
  10512. //Checking if all class have their data
  10513. for (i = 0; i < JOB_MAX; i++) {
  10514. int idx;
  10515. uint16 j;
  10516. if (!pcdb_checkid(i))
  10517. continue;
  10518. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10519. continue; //Classes that do not need exp tables.
  10520. idx = pc_class2idx(i);
  10521. if (!job_info[idx].max_level[0])
  10522. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10523. if (!job_info[idx].max_level[1])
  10524. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10525. //Init and checking the empty value of Base HP/SP [Cydh]
  10526. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10527. if (job_info[idx].base_hp[j] == 0)
  10528. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10529. if (job_info[idx].base_sp[j] == 0)
  10530. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10531. }
  10532. }
  10533. }
  10534. // Read MOTD on startup. [Valaris]
  10535. int pc_read_motd(void)
  10536. {
  10537. FILE* fp;
  10538. // clear old MOTD
  10539. memset(motd_text, 0, sizeof(motd_text));
  10540. // read current MOTD
  10541. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10542. {
  10543. unsigned int entries = 0;
  10544. char buf[CHAT_SIZE_MAX];
  10545. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10546. {
  10547. unsigned int lines = 0;
  10548. size_t len;
  10549. lines++;
  10550. if( buf[0] == '/' && buf[1] == '/' )
  10551. continue;
  10552. len = strlen(buf);
  10553. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10554. len--;
  10555. if( len ) {
  10556. char * ptr;
  10557. buf[len] = 0;
  10558. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10559. 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);
  10560. }
  10561. else {// empty line
  10562. buf[0] = ' ';
  10563. buf[1] = 0;
  10564. }
  10565. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10566. entries++;
  10567. }
  10568. fclose(fp);
  10569. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10570. }
  10571. else
  10572. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10573. return 0;
  10574. }
  10575. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10576. int i,cursor = 0;
  10577. struct item_cd* cd = NULL;
  10578. if( load ) {
  10579. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10580. // no item cooldown is associated with this character
  10581. return;
  10582. }
  10583. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10584. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10585. sd->item_delay[cursor].tick = cd->tick[i];
  10586. sd->item_delay[cursor].nameid = cd->nameid[i];
  10587. cursor++;
  10588. }
  10589. }
  10590. idb_remove(itemcd_db,sd->status.char_id);
  10591. } else {
  10592. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10593. // create a new skill cooldown object for map storage
  10594. CREATE( cd, struct item_cd, 1 );
  10595. idb_put( itemcd_db, sd->status.char_id, cd );
  10596. }
  10597. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10598. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10599. cd->tick[cursor] = sd->item_delay[i].tick;
  10600. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10601. cursor++;
  10602. }
  10603. }
  10604. }
  10605. return;
  10606. }
  10607. /**
  10608. * Add item delay to player's item delay data
  10609. * @param sd Player
  10610. * @param id Item data
  10611. * @param tick Current tick
  10612. * @param n Item index in inventory
  10613. * @return 0: No delay, can consume item.
  10614. * 1: Has delay, cancel consumption.
  10615. **/
  10616. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10617. int i;
  10618. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10619. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10620. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10621. if( i < MAX_ITEMDELAYS ) {
  10622. if( sd->item_delay[i].nameid ) {// found
  10623. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10624. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10625. char e_msg[CHAT_SIZE_MAX];
  10626. if( e_tick > 99 )
  10627. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10628. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10629. else
  10630. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10631. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10632. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10633. return 1; // Delay has not expired yet
  10634. }
  10635. } else {// not yet used item (all slots are initially empty)
  10636. sd->item_delay[i].nameid = id->nameid;
  10637. }
  10638. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10639. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10640. } else {// should not happen
  10641. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10642. }
  10643. //clean up used delays so we can give room for more
  10644. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10645. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10646. sd->item_delay[i].tick = 0;
  10647. sd->item_delay[i].nameid = 0;
  10648. }
  10649. }
  10650. return 0;
  10651. }
  10652. /**
  10653. * Check if player has delay to reuse item
  10654. * @param sd Player
  10655. * @param id Item data
  10656. * @param tick Current tick
  10657. * @param n Item index in inventory
  10658. * @return 0: No delay, can consume item.
  10659. * 1: Has delay, cancel consumption.
  10660. **/
  10661. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10662. struct status_change *sc = NULL;
  10663. nullpo_retr(0, sd);
  10664. nullpo_retr(0, id);
  10665. // Do normal delay assignment
  10666. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10667. return pc_itemcd_add(sd, id, tick, n);
  10668. // Send reply of delay remains
  10669. if (sc->data[id->delay_sc]) {
  10670. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10671. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10672. return 1;
  10673. }
  10674. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10675. return 0;
  10676. }
  10677. /**
  10678. * Clear the dmglog data from player
  10679. * @param sd
  10680. * @param md
  10681. **/
  10682. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10683. {
  10684. uint8 i;
  10685. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10686. if (i < DAMAGELOG_SIZE) {
  10687. md->dmglog[i].id = 0;
  10688. md->dmglog[i].dmg = 0;
  10689. md->dmglog[i].flag = 0;
  10690. }
  10691. }
  10692. /**
  10693. * Add log to player's dmglog
  10694. * @param sd
  10695. * @param id Monster's GID
  10696. **/
  10697. void pc_damage_log_add(struct map_session_data *sd, int id)
  10698. {
  10699. uint8 i = 0;
  10700. if (!sd || !id)
  10701. return;
  10702. //Only store new data, don't need to renew the old one with same id
  10703. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10704. if (i < DAMAGELOG_SIZE_PC)
  10705. return;
  10706. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10707. if (sd->dmglog[i] == 0) {
  10708. sd->dmglog[i] = id;
  10709. return;
  10710. }
  10711. }
  10712. }
  10713. /**
  10714. * Clear dmglog data from player
  10715. * @param sd
  10716. * @param id Monster's id
  10717. **/
  10718. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10719. {
  10720. uint8 i;
  10721. struct mob_data *md = NULL;
  10722. if (!sd)
  10723. return;
  10724. if (!id) {
  10725. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10726. if( !sd->dmglog[i] ) //skip the empty value
  10727. continue;
  10728. if ((md = map_id2md(sd->dmglog[i])))
  10729. pc_clear_log_damage_sub(sd->status.char_id,md);
  10730. sd->dmglog[i] = 0;
  10731. }
  10732. }
  10733. else {
  10734. if ((md = map_id2md(id)))
  10735. pc_clear_log_damage_sub(sd->status.char_id,md);
  10736. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10737. if (i < DAMAGELOG_SIZE_PC)
  10738. sd->dmglog[i] = 0;
  10739. }
  10740. }
  10741. /**
  10742. * Status change data arrived from char-server
  10743. * @param sd: Player data
  10744. */
  10745. void pc_scdata_received(struct map_session_data *sd) {
  10746. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10747. clif_weight_limit( sd );
  10748. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10749. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10750. }
  10751. sd->state.pc_loaded = true;
  10752. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10753. sd->state.connect_new = 1;
  10754. clif_parse_LoadEndAck(sd->fd, sd);
  10755. }
  10756. if (pc_iscarton(sd)) {
  10757. sd->cart_weight_max = 0; // Force a client refesh
  10758. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10759. }
  10760. }
  10761. /**
  10762. * Check player account expiration time and rental item expirations
  10763. * @param sd: Player data
  10764. */
  10765. void pc_check_expiration(struct map_session_data *sd) {
  10766. #ifndef ENABLE_SC_SAVING
  10767. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10768. #endif
  10769. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10770. time_t exp_time = sd->expiration_time;
  10771. char tmpstr[1024];
  10772. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10773. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10774. pc_expire_check(sd);
  10775. }
  10776. }
  10777. TIMER_FUNC(pc_expiration_timer){
  10778. struct map_session_data *sd = map_id2sd(id);
  10779. if( !sd ) return 0;
  10780. sd->expiration_tid = INVALID_TIMER;
  10781. if( sd->fd )
  10782. clif_authfail_fd(sd->fd,10);
  10783. map_quit(sd);
  10784. return 0;
  10785. }
  10786. TIMER_FUNC(pc_autotrade_timer){
  10787. struct map_session_data *sd = map_id2sd(id);
  10788. if (!sd)
  10789. return 0;
  10790. sd->autotrade_tid = INVALID_TIMER;
  10791. if (sd->state.autotrade&2)
  10792. vending_reopen(sd);
  10793. if (sd->state.autotrade&4)
  10794. buyingstore_reopen(sd);
  10795. if (!sd->vender_id && !sd->buyer_id) {
  10796. sd->state.autotrade = 0;
  10797. map_quit(sd);
  10798. }
  10799. return 0;
  10800. }
  10801. /* this timer exists only when a character with a expire timer > 24h is online */
  10802. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10803. TIMER_FUNC(pc_global_expiration_timer){
  10804. struct s_mapiterator* iter;
  10805. struct map_session_data* sd;
  10806. iter = mapit_getallusers();
  10807. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10808. if( sd->expiration_time )
  10809. pc_expire_check(sd);
  10810. mapit_free(iter);
  10811. return 0;
  10812. }
  10813. void pc_expire_check(struct map_session_data *sd) {
  10814. /* ongoing timer */
  10815. if( sd->expiration_tid != INVALID_TIMER )
  10816. return;
  10817. /* not within the next 24h, enable the global check */
  10818. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10819. /* global check not running, enable */
  10820. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10821. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10822. return;
  10823. }
  10824. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10825. }
  10826. /**
  10827. * Deposit some money to bank
  10828. * @param sd
  10829. * @param money Amount of money to deposit
  10830. **/
  10831. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10832. unsigned int limit_check = money + sd->bank_vault;
  10833. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10834. return BDA_OVERFLOW;
  10835. } else if ( money > sd->status.zeny ) {
  10836. return BDA_NO_MONEY;
  10837. }
  10838. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10839. return BDA_NO_MONEY;
  10840. sd->bank_vault += money;
  10841. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10842. if( save_settings&CHARSAVE_BANK )
  10843. chrif_save(sd, CSAVE_NORMAL);
  10844. return BDA_SUCCESS;
  10845. }
  10846. /**
  10847. * Withdraw money from bank
  10848. * @param sd
  10849. * @param money Amount of money that will be withdrawn
  10850. **/
  10851. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10852. unsigned int limit_check = money + sd->status.zeny;
  10853. if( money <= 0 ) {
  10854. return BWA_UNKNOWN_ERROR;
  10855. } else if ( money > sd->bank_vault ) {
  10856. return BWA_NO_MONEY;
  10857. } else if ( limit_check > MAX_ZENY ) {
  10858. /* no official response for this scenario exists. */
  10859. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10860. return BWA_UNKNOWN_ERROR;
  10861. }
  10862. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10863. return BWA_NO_MONEY;
  10864. sd->bank_vault -= money;
  10865. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10866. if( save_settings&CHARSAVE_BANK )
  10867. chrif_save(sd, CSAVE_NORMAL);
  10868. return BWA_SUCCESS;
  10869. }
  10870. /**
  10871. * Clear Crimson Marker data from caster
  10872. * @param sd: Player
  10873. **/
  10874. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10875. uint8 i;
  10876. if (!sd)
  10877. return;
  10878. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10879. struct block_list *bl = NULL;
  10880. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10881. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10882. sd->c_marker[i] = 0;
  10883. }
  10884. }
  10885. /**
  10886. * Show version to player
  10887. * @param sd: Player
  10888. **/
  10889. void pc_show_version(struct map_session_data *sd) {
  10890. const char* svn = get_svn_revision();
  10891. char buf[CHAT_SIZE_MAX];
  10892. if( svn[0] != UNKNOWN_VERSION )
  10893. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10894. else {
  10895. const char* git = get_git_hash();
  10896. if( git[0] != UNKNOWN_VERSION )
  10897. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10898. else
  10899. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10900. }
  10901. clif_displaymessage(sd->fd,buf);
  10902. }
  10903. /**
  10904. * Run bonus_script on player
  10905. * @param sd
  10906. * @author [Cydh]
  10907. **/
  10908. void pc_bonus_script(struct map_session_data *sd) {
  10909. t_tick now = gettick();
  10910. struct linkdb_node *node = NULL, *next = NULL;
  10911. if (!sd || !(node = sd->bonus_script.head))
  10912. return;
  10913. while (node) {
  10914. struct s_bonus_script_entry *entry = NULL;
  10915. next = node->next;
  10916. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10917. // Only start timer for new bonus_script
  10918. if (entry->tid == INVALID_TIMER) {
  10919. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10920. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10921. entry->tick += now;
  10922. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10923. }
  10924. if (entry->script)
  10925. run_script(entry->script, 0, sd->bl.id, 0);
  10926. else
  10927. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10928. }
  10929. node = next;
  10930. }
  10931. }
  10932. /**
  10933. * Add bonus_script to player
  10934. * @param sd Player
  10935. * @param script_str Script string
  10936. * @param dur Duration in ms
  10937. * @param icon EFST
  10938. * @param flag Flags @see enum e_bonus_script_flags
  10939. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10940. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10941. * @author [Cydh]
  10942. **/
  10943. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_types icon, uint16 flag, uint8 type) {
  10944. struct script_code *script = NULL;
  10945. struct linkdb_node *node = NULL;
  10946. struct s_bonus_script_entry *entry = NULL;
  10947. if (!sd)
  10948. return NULL;
  10949. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10950. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10951. return NULL;
  10952. }
  10953. // Duplication checks
  10954. if ((node = sd->bonus_script.head)) {
  10955. while (node) {
  10956. entry = (struct s_bonus_script_entry *)node->data;
  10957. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10958. t_tick newdur = gettick() + dur;
  10959. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10960. sett_tickimer(entry->tid, newdur);
  10961. script_free_code(script);
  10962. return NULL;
  10963. }
  10964. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10965. break;
  10966. else { // No duplicate bonus
  10967. script_free_code(script);
  10968. return NULL;
  10969. }
  10970. }
  10971. node = node->next;
  10972. }
  10973. }
  10974. CREATE(entry, struct s_bonus_script_entry, 1);
  10975. entry->script_buf = StringBuf_Malloc();
  10976. StringBuf_AppendStr(entry->script_buf, script_str);
  10977. entry->tid = INVALID_TIMER;
  10978. entry->flag = flag;
  10979. entry->icon = icon;
  10980. entry->tick = dur; // Use duration first, on run change to expire time
  10981. entry->type = type;
  10982. entry->script = script;
  10983. sd->bonus_script.count++;
  10984. return entry;
  10985. }
  10986. /**
  10987. * Remove bonus_script data from player
  10988. * @param sd: Target player
  10989. * @param list: Bonus script entry from player
  10990. * @author [Cydh]
  10991. **/
  10992. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10993. if (entry->tid != INVALID_TIMER)
  10994. delete_timer(entry->tid, pc_bonus_script_timer);
  10995. if (entry->script)
  10996. script_free_code(entry->script);
  10997. if (entry->script_buf)
  10998. StringBuf_Free(entry->script_buf);
  10999. if (sd) {
  11000. if (entry->icon != EFST_BLANK)
  11001. clif_status_load(&sd->bl, entry->icon, 0);
  11002. if (sd->bonus_script.count > 0)
  11003. sd->bonus_script.count--;
  11004. }
  11005. aFree(entry);
  11006. }
  11007. /**
  11008. * Do final process if no entry left
  11009. * @param sd
  11010. **/
  11011. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11012. if (sd->bonus_script.count == 0) {
  11013. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11014. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11015. linkdb_final(&sd->bonus_script.head);
  11016. }
  11017. }
  11018. /**
  11019. * Timer for bonus_script
  11020. * @param tid
  11021. * @param tick
  11022. * @param id
  11023. * @param data
  11024. * @author [Cydh]
  11025. **/
  11026. TIMER_FUNC(pc_bonus_script_timer){
  11027. struct map_session_data *sd;
  11028. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11029. sd = map_id2sd(id);
  11030. if (!sd) {
  11031. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11032. return 0;
  11033. }
  11034. if (tid == INVALID_TIMER)
  11035. return 0;
  11036. if (!sd->bonus_script.head || entry == NULL) {
  11037. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11038. return 0;
  11039. }
  11040. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11041. pc_bonus_script_free_entry(sd, entry);
  11042. pc_bonus_script_check_final(sd);
  11043. status_calc_pc(sd,SCO_NONE);
  11044. return 0;
  11045. }
  11046. /**
  11047. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11048. * @param sd: Target player
  11049. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11050. * @author [Cydh]
  11051. **/
  11052. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11053. struct linkdb_node *node = NULL;
  11054. uint16 count = 0;
  11055. if (!sd || !(node = sd->bonus_script.head))
  11056. return;
  11057. while (node) {
  11058. struct linkdb_node *next = node->next;
  11059. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11060. if (entry && (
  11061. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11062. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11063. (flag&entry->flag) || // Matched flag
  11064. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11065. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11066. )
  11067. )
  11068. {
  11069. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11070. pc_bonus_script_free_entry(sd, entry);
  11071. count++;
  11072. }
  11073. node = next;
  11074. }
  11075. pc_bonus_script_check_final(sd);
  11076. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11077. status_calc_pc(sd,SCO_NONE);
  11078. }
  11079. /** [Cydh]
  11080. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11081. * @param sd: Target player
  11082. */
  11083. void pc_cell_basilica(struct map_session_data *sd) {
  11084. nullpo_retv(sd);
  11085. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11086. if (sd->sc.data[SC_BASILICA])
  11087. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  11088. }
  11089. else if (!sd->sc.data[SC_BASILICA])
  11090. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,INFINITE_TICK);
  11091. }
  11092. /** [Cydh]
  11093. * Get maximum specified parameter for specified class
  11094. * @param class_: sd->class
  11095. * @param sex: sd->status.sex
  11096. * @param flag: parameter will be checked
  11097. * @return max_param
  11098. */
  11099. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11100. int idx = -1, class_ = sd->class_;
  11101. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11102. short max_param = 0;
  11103. switch (param) {
  11104. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11105. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11106. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11107. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11108. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11109. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11110. }
  11111. if (max_param > 0)
  11112. return max_param;
  11113. }
  11114. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  11115. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  11116. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  11117. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  11118. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  11119. }
  11120. /**
  11121. * Get max ASPD for player based on Class
  11122. * @param sd Player
  11123. * @return ASPD
  11124. */
  11125. short pc_maxaspd(struct map_session_data *sd) {
  11126. nullpo_ret(sd);
  11127. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11128. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11129. battle_config.max_aspd ));
  11130. }
  11131. /**
  11132. * Calculates total item-group related bonuses for the given item
  11133. * @param sd Player
  11134. * @param nameid Item ID
  11135. * @return Heal rate
  11136. **/
  11137. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11138. if (sd->itemgrouphealrate.empty())
  11139. return 0;
  11140. short bonus = 0;
  11141. for (const auto &it : sd->itemgrouphealrate) {
  11142. uint16 group_id = it.id, i;
  11143. struct s_item_group_db *group = NULL;
  11144. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11145. continue;
  11146. for (i = 0; i < group->random[0].data_qty; i++) {
  11147. if (group->random[0].data[i].nameid == nameid) {
  11148. bonus += it.val;
  11149. break;
  11150. }
  11151. }
  11152. }
  11153. return bonus;
  11154. }
  11155. /**
  11156. * Calculates total item-group related bonuses for the given item group
  11157. * @param sd Player
  11158. * @param group_id Item Group ID
  11159. * @return Heal rate
  11160. **/
  11161. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11162. if (sd->itemgrouphealrate.empty())
  11163. return 0;
  11164. for (const auto &it : sd->itemgrouphealrate) {
  11165. if (it.id == group_id)
  11166. return it.val;
  11167. }
  11168. return 0;
  11169. }
  11170. /**
  11171. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11172. * @param eqi Item EQI of enum equip_index
  11173. * @param *equip_index Player's equip_index[]
  11174. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11175. * @return True if item in same inventory index, False if doesn't
  11176. */
  11177. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11178. if (index < 0 || index >= MAX_INVENTORY)
  11179. return true;
  11180. // Dual weapon checks
  11181. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11182. return true;
  11183. // Headgear with Mid & Low location
  11184. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11185. return true;
  11186. // Headgear with Top & Mid or Low location
  11187. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11188. return true;
  11189. // Headgear with Mid & Low location
  11190. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11191. return true;
  11192. // Headgear with Top & Mid or Low location
  11193. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11194. return true;
  11195. return false;
  11196. }
  11197. /**
  11198. * Generate Unique item ID for player
  11199. * @param sd : Player
  11200. * @return A generated Unique item ID
  11201. */
  11202. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11203. nullpo_ret(sd);
  11204. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11205. }
  11206. /**
  11207. * Validating skill from player after logged on
  11208. * @param sd
  11209. **/
  11210. void pc_validate_skill(struct map_session_data *sd) {
  11211. if (sd) {
  11212. uint16 i = 0, count = 0;
  11213. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11214. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11215. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11216. for (i = 0; i < MAX_SKILL; i++) {
  11217. uint16 idx = 0;
  11218. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11219. continue;
  11220. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11221. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11222. count++;
  11223. }
  11224. else
  11225. ShowWarning("pc_validate_skill: Removing invalid skill '%d' from player (AID=%d CID=%d).\n", tmp_skills[i].id, sd->status.account_id, sd->status.char_id);
  11226. }
  11227. }
  11228. }
  11229. /**
  11230. * Toggle to remember if the questinfo is displayed yet or not.
  11231. * @param qi_display Display flag
  11232. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11233. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11234. **/
  11235. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11236. if (show) {
  11237. // Check if need to be displayed
  11238. if ((*qi_display) != 1) {
  11239. (*qi_display) = 1;
  11240. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11241. }
  11242. }
  11243. else {
  11244. // Check if need to be hide
  11245. if ((*qi_display) != 0) {
  11246. (*qi_display) = 0;
  11247. #if PACKETVER >= 20120410
  11248. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11249. #else
  11250. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11251. #endif
  11252. }
  11253. }
  11254. }
  11255. /**
  11256. * Show available NPC Quest / Event Icon Check [Kisuka]
  11257. * @param sd Player
  11258. **/
  11259. void pc_show_questinfo(struct map_session_data *sd) {
  11260. #if PACKETVER >= 20090218
  11261. nullpo_retv(sd);
  11262. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11263. return;
  11264. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11265. nullpo_retv(mapdata);
  11266. if (mapdata->qi_data.empty())
  11267. return;
  11268. if (mapdata->qi_data.size() != sd->qi_count)
  11269. return; // init was not called yet
  11270. struct s_questinfo *qi = nullptr;
  11271. bool show;
  11272. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11273. qi = &mapdata->qi_data[i];
  11274. if (!qi)
  11275. continue;
  11276. if (!qi->condition)
  11277. show = true;
  11278. else {
  11279. if (achievement_check_condition(qi->condition, sd))
  11280. show = true;
  11281. else
  11282. show = false;
  11283. }
  11284. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11285. }
  11286. #endif
  11287. }
  11288. /**
  11289. * Reinit the questinfo for player when changing map
  11290. * @param sd Player
  11291. **/
  11292. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11293. #if PACKETVER >= 20090218
  11294. nullpo_retv(sd);
  11295. if (sd->qi_display) {
  11296. aFree(sd->qi_display);
  11297. sd->qi_display = nullptr;
  11298. }
  11299. sd->qi_count = 0;
  11300. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11301. return;
  11302. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11303. nullpo_retv(mapdata);
  11304. if (mapdata->qi_data.empty())
  11305. return;
  11306. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11307. #endif
  11308. }
  11309. /**
  11310. * Check if a job is allowed to enter the map
  11311. * @param jobid Job ID see enum e_job or sd->status.class_
  11312. * @param m ID -an index- for direct indexing map[] array
  11313. * @return 1 if job is allowed, 0 otherwise
  11314. **/
  11315. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11316. uint16 idx = 0;
  11317. // Map is other map server.
  11318. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11319. if (m < 0)
  11320. return true;
  11321. struct map_data *mapdata = map_getmapdata(m);
  11322. if (!mapdata->cell)
  11323. return false;
  11324. if (!pcdb_checkid(jobid))
  11325. return false;
  11326. idx = pc_class2idx(jobid);
  11327. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11328. return true;
  11329. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11330. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11331. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11332. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11333. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11334. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11335. )
  11336. return false;
  11337. return true;
  11338. }
  11339. /**
  11340. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11341. * @param sd
  11342. **/
  11343. void pc_set_costume_view(struct map_session_data *sd) {
  11344. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11345. struct item_data *id = NULL;
  11346. nullpo_retv(sd);
  11347. head_low = sd->status.head_bottom;
  11348. head_mid = sd->status.head_mid;
  11349. head_top = sd->status.head_top;
  11350. robe = sd->status.robe;
  11351. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11352. //Added check to prevent sending the same look on multiple slots ->
  11353. //causes client to redraw item on top of itself. (suggested by Lupus)
  11354. // Normal headgear checks
  11355. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11356. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11357. sd->status.head_bottom = id->look;
  11358. else
  11359. sd->status.head_bottom = 0;
  11360. }
  11361. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11362. if (!(id->equip&(EQP_HEAD_TOP)))
  11363. sd->status.head_mid = id->look;
  11364. else
  11365. sd->status.head_mid = 0;
  11366. }
  11367. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11368. sd->status.head_top = id->look;
  11369. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11370. sd->status.robe = id->look;
  11371. // Costumes check
  11372. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11373. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11374. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11375. sd->status.head_bottom = id->look;
  11376. else
  11377. sd->status.head_bottom = 0;
  11378. }
  11379. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11380. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11381. sd->status.head_mid = id->look;
  11382. else
  11383. sd->status.head_mid = 0;
  11384. }
  11385. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11386. sd->status.head_top = id->look;
  11387. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11388. sd->status.robe = id->look;
  11389. }
  11390. if (sd->setlook_head_bottom)
  11391. sd->status.head_bottom = sd->setlook_head_bottom;
  11392. if (sd->setlook_head_mid)
  11393. sd->status.head_mid = sd->setlook_head_mid;
  11394. if (sd->setlook_head_top)
  11395. sd->status.head_top = sd->setlook_head_top;
  11396. if (sd->setlook_robe)
  11397. sd->status.robe = sd->setlook_robe;
  11398. if (head_low != sd->status.head_bottom)
  11399. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11400. if (head_mid != sd->status.head_mid)
  11401. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11402. if (head_top != sd->status.head_top)
  11403. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11404. if (robe != sd->status.robe)
  11405. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11406. }
  11407. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11408. uint32 date = date_get(DT_YYYYMMDD);
  11409. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11410. std::shared_ptr<s_attendance_period> period = pair.second;
  11411. if( period->start <= date && period->end >= date ){
  11412. return period;
  11413. }
  11414. }
  11415. return nullptr;
  11416. }
  11417. bool pc_attendance_enabled(){
  11418. // Check if the attendance feature is disabled
  11419. if( !battle_config.feature_attendance ){
  11420. return false;
  11421. }
  11422. // Check if there is a running attendance period
  11423. return pc_attendance_period() != nullptr;
  11424. }
  11425. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11426. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11427. }
  11428. int32 pc_attendance_counter( struct map_session_data* sd ){
  11429. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11430. // No running attendance period
  11431. if( period == nullptr ){
  11432. return 0;
  11433. }
  11434. // Get the counter for the current period
  11435. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11436. // Check if we have a remaining counter from a previous period
  11437. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11438. // Reset the counter to zero
  11439. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11440. return 0;
  11441. }
  11442. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11443. }
  11444. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11445. // If the user's group does not have the permission
  11446. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11447. return;
  11448. }
  11449. // Check if the attendance feature is disabled
  11450. if( !pc_attendance_enabled() ){
  11451. return;
  11452. }
  11453. // Check if the user already got his reward today
  11454. if( pc_attendance_rewarded_today( sd ) ){
  11455. return;
  11456. }
  11457. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11458. attendance_counter += 1;
  11459. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11460. if( period == nullptr ){
  11461. return;
  11462. }
  11463. if( period->rewards.size() < attendance_counter ){
  11464. return;
  11465. }
  11466. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11467. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11468. if( save_settings&CHARSAVE_ATTENDANCE )
  11469. chrif_save(sd, CSAVE_NORMAL);
  11470. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11471. struct mail_message msg;
  11472. memset( &msg, 0, sizeof( struct mail_message ) );
  11473. msg.dest_id = sd->status.char_id;
  11474. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11475. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11476. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11477. msg.item[0].nameid = reward->item_id;
  11478. msg.item[0].amount = reward->amount;
  11479. msg.item[0].identify = 1;
  11480. msg.status = MAIL_NEW;
  11481. msg.type = MAIL_INBOX_NORMAL;
  11482. msg.timestamp = time(NULL);
  11483. intif_Mail_send(0, &msg);
  11484. clif_attendence_response( sd, attendance_counter );
  11485. }
  11486. /*==========================================
  11487. * pc Init/Terminate
  11488. *------------------------------------------*/
  11489. void do_final_pc(void) {
  11490. db_destroy(itemcd_db);
  11491. do_final_pc_groups();
  11492. ers_destroy(pc_sc_display_ers);
  11493. ers_destroy(num_reg_ers);
  11494. ers_destroy(str_reg_ers);
  11495. attendance_db.clear();
  11496. }
  11497. void do_init_pc(void) {
  11498. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11499. pc_readdb();
  11500. pc_read_motd(); // Read MOTD [Valaris]
  11501. attendance_db.load();
  11502. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11503. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11504. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11505. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11506. add_timer_func_list(pc_autosave, "pc_autosave");
  11507. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11508. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11509. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11510. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11511. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11512. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11513. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11514. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11515. // 0=day, 1=night [Yor]
  11516. night_flag = battle_config.night_at_start ? 1 : 0;
  11517. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11518. int day_duration = battle_config.day_duration;
  11519. int night_duration = battle_config.night_duration;
  11520. // add night/day timer [Yor]
  11521. add_timer_func_list(map_day_timer, "map_day_timer");
  11522. add_timer_func_list(map_night_timer, "map_night_timer");
  11523. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11524. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11525. }
  11526. do_init_pc_groups();
  11527. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11528. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11529. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11530. ers_chunk_size(pc_sc_display_ers, 150);
  11531. ers_chunk_size(num_reg_ers, 300);
  11532. ers_chunk_size(str_reg_ers, 50);
  11533. }